PageRenderTime 63ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/scm-test/src/main/java/sonia/scm/repository/RepositoryManagerTestBase.java

https://bitbucket.org/sdorra/scm-manager/
Java | 591 lines | 260 code | 80 blank | 251 comment | 7 complexity | 06118087ee381cc953e06935f584f713 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. /**
  2. * Copyright (c) 2010, Sebastian Sdorra
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice,
  9. * this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright notice,
  11. * this list of conditions and the following disclaimer in the documentation
  12. * and/or other materials provided with the distribution.
  13. * 3. Neither the name of SCM-Manager; nor the names of its
  14. * contributors may be used to endorse or promote products derived from this
  15. * software without specific prior written permission.
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  18. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  19. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  20. * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
  21. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  22. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  23. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
  24. * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  25. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  26. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  27. *
  28. * http://bitbucket.org/sdorra/scm-manager
  29. *
  30. */
  31. package sonia.scm.repository;
  32. //~--- non-JDK imports --------------------------------------------------------
  33. import com.github.legman.Subscribe;
  34. import org.apache.shiro.subject.Subject;
  35. import org.junit.Before;
  36. import org.junit.Test;
  37. import sonia.scm.HandlerEventType;
  38. import sonia.scm.Manager;
  39. import sonia.scm.ManagerTestBase;
  40. import sonia.scm.event.ScmEventBus;
  41. import sonia.scm.repository.api.HookContext;
  42. import sonia.scm.util.MockUtil;
  43. import static org.junit.Assert.*;
  44. //~--- JDK imports ------------------------------------------------------------
  45. import java.io.IOException;
  46. import java.util.Collection;
  47. /**
  48. *
  49. * @author Sebastian Sdorra
  50. */
  51. public abstract class RepositoryManagerTestBase
  52. extends ManagerTestBase<Repository, RepositoryException>
  53. {
  54. /**
  55. * Method description
  56. *
  57. *
  58. * @param repository
  59. *
  60. * @return
  61. */
  62. public abstract HookContext createHookContext(Repository repository);
  63. /**
  64. * Method description
  65. *
  66. *
  67. * @param archiveEnabled
  68. *
  69. * @return
  70. */
  71. protected abstract RepositoryManager createRepositoryManager(
  72. boolean archiveEnabled);
  73. /**
  74. * Method description
  75. *
  76. *
  77. * @throws IOException
  78. * @throws RepositoryException
  79. */
  80. @Test
  81. public void testCreate() throws RepositoryException, IOException
  82. {
  83. Repository heartOfGold = createTestRepository();
  84. Repository dbRepo = manager.get(heartOfGold.getId());
  85. assertNotNull(dbRepo);
  86. assertRepositoriesEquals(dbRepo, heartOfGold);
  87. }
  88. /**
  89. * Method description
  90. *
  91. *
  92. * @throws IOException
  93. * @throws RepositoryException
  94. */
  95. @Test(expected = RepositoryAllreadyExistExeption.class)
  96. public void testCreateExisting() throws RepositoryException, IOException
  97. {
  98. createTestRepository();
  99. createTestRepository();
  100. }
  101. /**
  102. * Method description
  103. *
  104. *
  105. * @throws IOException
  106. * @throws RepositoryException
  107. */
  108. @Test
  109. public void testDelete() throws RepositoryException, IOException
  110. {
  111. delete(manager, createTestRepository());
  112. }
  113. /**
  114. * Method description
  115. *
  116. *
  117. * @throws IOException
  118. * @throws RepositoryException
  119. */
  120. @Test(expected = RepositoryIsNotArchivedException.class)
  121. public void testDeleteNonArchived() throws RepositoryException, IOException
  122. {
  123. delete(createRepositoryManager(true), createTestRepository());
  124. }
  125. /**
  126. * Method description
  127. *
  128. *
  129. * @throws IOException
  130. * @throws RepositoryException
  131. */
  132. @Test(expected = RepositoryNotFoundException.class)
  133. public void testDeleteNotFound() throws RepositoryException, IOException
  134. {
  135. manager.delete(createRepositoryWithId());
  136. }
  137. /**
  138. * Method description
  139. *
  140. *
  141. * @throws IOException
  142. * @throws RepositoryException
  143. */
  144. @Test
  145. public void testDeleteWithEnabledArchive()
  146. throws RepositoryException, IOException
  147. {
  148. Repository repository = createTestRepository();
  149. repository.setArchived(true);
  150. RepositoryManager drm = createRepositoryManager(true);
  151. drm.init(contextProvider);
  152. delete(drm, repository);
  153. }
  154. /**
  155. * Method description
  156. *
  157. *
  158. * @throws IOException
  159. * @throws RepositoryException
  160. */
  161. @Test
  162. public void testGet() throws RepositoryException, IOException
  163. {
  164. Repository heartOfGold = createTestRepository();
  165. String id = heartOfGold.getId();
  166. String description = heartOfGold.getDescription();
  167. assertNotNull(description);
  168. // test for reference
  169. heartOfGold.setDescription("prototype ship");
  170. heartOfGold = manager.get(id);
  171. assertNotNull(heartOfGold);
  172. assertEquals(description, heartOfGold.getDescription());
  173. }
  174. /**
  175. * Method description
  176. *
  177. *
  178. * @throws IOException
  179. * @throws RepositoryException
  180. */
  181. @Test
  182. public void testGetAll() throws RepositoryException, IOException
  183. {
  184. Repository heartOfGold = createTestRepository();
  185. Repository happyVerticalPeopleTransporter = createSecondTestRepository();
  186. boolean foundHeart = false;
  187. boolean foundTransporter = false;
  188. Collection<Repository> repositories = manager.getAll();
  189. assertNotNull(repositories);
  190. assertFalse(repositories.isEmpty());
  191. assertTrue(repositories.size() >= 2);
  192. Repository heartReference = null;
  193. for (Repository repository : repositories)
  194. {
  195. if (repository.getId().equals(heartOfGold.getId()))
  196. {
  197. assertRepositoriesEquals(heartOfGold, repository);
  198. foundHeart = true;
  199. heartReference = repository;
  200. }
  201. else if (
  202. repository.getId().equals(happyVerticalPeopleTransporter.getId()))
  203. {
  204. assertRepositoriesEquals(happyVerticalPeopleTransporter, repository);
  205. foundTransporter = true;
  206. }
  207. }
  208. assertTrue(foundHeart);
  209. assertTrue(foundTransporter);
  210. // test for reference
  211. assertNotSame(heartOfGold, heartReference);
  212. heartReference.setDescription("prototype ship");
  213. assertFalse(
  214. heartOfGold.getDescription().equals(heartReference.getDescription()));
  215. }
  216. /**
  217. * Method description
  218. *
  219. *
  220. * @throws IOException
  221. * @throws RepositoryException
  222. */
  223. @Test
  224. public void testListener() throws RepositoryException, IOException
  225. {
  226. RepositoryManager repoManager = createRepositoryManager(false);
  227. repoManager.init(contextProvider);
  228. TestListener listener = new TestListener();
  229. ScmEventBus.getInstance().register(listener);
  230. Repository repository = RepositoryTestData.create42Puzzle();
  231. repoManager.create(repository);
  232. assertRepositoriesEquals(repository, listener.preRepository);
  233. assertSame(HandlerEventType.BEFORE_CREATE, listener.preEvent);
  234. assertRepositoriesEquals(repository, listener.postRepository);
  235. assertSame(HandlerEventType.CREATE, listener.postEvent);
  236. repository.setDescription("changed description");
  237. repoManager.modify(repository);
  238. assertRepositoriesEquals(repository, listener.preRepository);
  239. assertSame(HandlerEventType.BEFORE_MODIFY, listener.preEvent);
  240. assertRepositoriesEquals(repository, listener.postRepository);
  241. assertSame(HandlerEventType.MODIFY, listener.postEvent);
  242. repoManager.delete(repository);
  243. assertRepositoriesEquals(repository, listener.preRepository);
  244. assertSame(HandlerEventType.BEFORE_DELETE, listener.preEvent);
  245. assertRepositoriesEquals(repository, listener.postRepository);
  246. assertSame(HandlerEventType.DELETE, listener.postEvent);
  247. }
  248. /**
  249. * Method description
  250. *
  251. *
  252. * @throws IOException
  253. * @throws RepositoryException
  254. */
  255. @Test
  256. public void testModify() throws RepositoryException, IOException
  257. {
  258. Repository heartOfGold = createTestRepository();
  259. heartOfGold.setDescription("prototype ship");
  260. manager.modify(heartOfGold);
  261. Repository hearReference = manager.get(heartOfGold.getId());
  262. assertNotNull(hearReference);
  263. assertEquals(hearReference.getDescription(), "prototype ship");
  264. }
  265. /**
  266. * Method description
  267. *
  268. *
  269. * @throws IOException
  270. * @throws RepositoryException
  271. */
  272. @Test(expected = RepositoryNotFoundException.class)
  273. public void testModifyNotFound() throws RepositoryException, IOException
  274. {
  275. manager.modify(createRepositoryWithId());
  276. }
  277. /**
  278. * Method description
  279. *
  280. *
  281. * @throws IOException
  282. * @throws RepositoryException
  283. */
  284. @Test
  285. public void testRefresh() throws RepositoryException, IOException
  286. {
  287. Repository heartOfGold = createTestRepository();
  288. String description = heartOfGold.getDescription();
  289. heartOfGold.setDescription("prototype ship");
  290. manager.refresh(heartOfGold);
  291. assertEquals(description, heartOfGold.getDescription());
  292. }
  293. /**
  294. * Method description
  295. *
  296. *
  297. * @throws IOException
  298. * @throws RepositoryException
  299. */
  300. @Test(expected = RepositoryNotFoundException.class)
  301. public void testRefreshNotFound() throws RepositoryException, IOException
  302. {
  303. manager.refresh(createRepositoryWithId());
  304. }
  305. /**
  306. * Method description
  307. *
  308. *
  309. * @throws IOException
  310. * @throws RepositoryException
  311. */
  312. @Test
  313. public void testRepositoryHook() throws RepositoryException, IOException
  314. {
  315. CountingReceiveHook hook = new CountingReceiveHook();
  316. RepositoryManager repoManager = createRepositoryManager(false);
  317. ScmEventBus.getInstance().register(hook);
  318. assertEquals(0, hook.eventsReceived);
  319. Repository repository = createTestRepository();
  320. HookContext ctx = createHookContext(repository);
  321. repoManager.fireHookEvent(new RepositoryHookEvent(ctx, repository,
  322. RepositoryHookType.POST_RECEIVE));
  323. assertEquals(1, hook.eventsReceived);
  324. repoManager.fireHookEvent(new RepositoryHookEvent(ctx, repository,
  325. RepositoryHookType.POST_RECEIVE));
  326. assertEquals(2, hook.eventsReceived);
  327. }
  328. //~--- set methods ----------------------------------------------------------
  329. /**
  330. * Method description
  331. *
  332. */
  333. @Before
  334. public void setAdminSubject()
  335. {
  336. Subject admin = MockUtil.createAdminSubject();
  337. setSubject(admin);
  338. }
  339. //~--- methods --------------------------------------------------------------
  340. /**
  341. * Method description
  342. *
  343. *
  344. * @param repo
  345. * @param other
  346. */
  347. private void assertRepositoriesEquals(Repository repo, Repository other)
  348. {
  349. assertEquals(repo.getId(), other.getId());
  350. assertEquals(repo.getName(), other.getName());
  351. assertEquals(repo.getDescription(), other.getDescription());
  352. assertEquals(repo.getContact(), other.getContact());
  353. assertEquals(repo.getCreationDate(), other.getCreationDate());
  354. assertEquals(repo.getLastModified(), other.getLastModified());
  355. }
  356. /**
  357. * Method description
  358. *
  359. *
  360. *
  361. * @param repository
  362. * @return
  363. *
  364. * @throws IOException
  365. * @throws RepositoryException
  366. */
  367. private Repository createRepository(Repository repository)
  368. throws RepositoryException, IOException
  369. {
  370. manager.create(repository);
  371. assertNotNull(repository.getId());
  372. assertNotNull(manager.get(repository.getId()));
  373. assertTrue(repository.getCreationDate() > 0);
  374. return repository;
  375. }
  376. /**
  377. * Method description
  378. *
  379. *
  380. * @return
  381. */
  382. private Repository createRepositoryWithId()
  383. {
  384. Repository repository = RepositoryTestData.createHeartOfGold();
  385. repository.setId("abc");
  386. return repository;
  387. }
  388. /**
  389. * Method description
  390. *
  391. *
  392. * @return
  393. *
  394. * @throws IOException
  395. * @throws RepositoryException
  396. */
  397. private Repository createSecondTestRepository()
  398. throws RepositoryException, IOException
  399. {
  400. return createRepository(
  401. RepositoryTestData.createHappyVerticalPeopleTransporter());
  402. }
  403. /**
  404. * Method description
  405. *
  406. *
  407. * @return
  408. *
  409. * @throws IOException
  410. * @throws RepositoryException
  411. */
  412. private Repository createTestRepository()
  413. throws RepositoryException, IOException
  414. {
  415. return createRepository(RepositoryTestData.createHeartOfGold());
  416. }
  417. /**
  418. * Method description
  419. *
  420. *
  421. * @param manager
  422. * @param repository
  423. *
  424. * @throws IOException
  425. * @throws RepositoryException
  426. */
  427. private void delete(Manager<Repository, RepositoryException> manager,
  428. Repository repository)
  429. throws RepositoryException, IOException
  430. {
  431. String id = repository.getId();
  432. manager.delete(repository);
  433. assertNull(manager.get(id));
  434. }
  435. //~--- inner classes --------------------------------------------------------
  436. /**
  437. * Class description
  438. *
  439. *
  440. * @version Enter version here..., 13/01/29
  441. * @author Enter your name here...
  442. */
  443. private static class CountingReceiveHook
  444. {
  445. /**
  446. * Method description
  447. *
  448. *
  449. * @param event
  450. */
  451. @Subscribe(async = false)
  452. public void onEvent(PostReceiveRepositoryHookEvent event)
  453. {
  454. eventsReceived++;
  455. }
  456. /**
  457. * Method description
  458. *
  459. *
  460. * @param event
  461. */
  462. @Subscribe(async = false)
  463. public void onEvent(PreReceiveRepositoryHookEvent event)
  464. {
  465. eventsReceived++;
  466. }
  467. //~--- fields -------------------------------------------------------------
  468. /** Field description */
  469. private int eventsReceived = 0;
  470. }
  471. /**
  472. * Class description
  473. *
  474. *
  475. * @version Enter version here..., 13/01/29
  476. * @author Enter your name here...
  477. */
  478. private static class TestListener
  479. {
  480. /**
  481. * Method description
  482. *
  483. *
  484. * @param repository
  485. * @param event
  486. */
  487. @Subscribe(async = false)
  488. public void onEvent(RepositoryEvent event)
  489. {
  490. if (event.getEventType().isPost())
  491. {
  492. this.postRepository = event.getItem();
  493. this.postEvent = event.getEventType();
  494. }
  495. else if (event.getEventType().isPre())
  496. {
  497. this.preRepository = event.getItem();
  498. this.preEvent = event.getEventType();
  499. }
  500. }
  501. //~--- fields -------------------------------------------------------------
  502. /** Field description */
  503. private HandlerEventType postEvent;
  504. /** Field description */
  505. private Repository postRepository;
  506. /** Field description */
  507. private HandlerEventType preEvent;
  508. /** Field description */
  509. private Repository preRepository;
  510. }
  511. }