You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

RepositoryRegistryTest.java 21KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511
  1. package org.apache.archiva.repository;
  2. /*
  3. * Licensed to the Apache Software Foundation (ASF) under one
  4. * or more contributor license agreements. See the NOTICE file
  5. * distributed with this work for additional information
  6. * regarding copyright ownership. The ASF licenses this file
  7. * to you under the Apache License, Version 2.0 (the
  8. * "License"); you may not use this file except in compliance
  9. * with the License. You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing,
  14. * software distributed under the License is distributed on an
  15. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  16. * KIND, either express or implied. See the License for the
  17. * specific language governing permissions and limitations
  18. * under the License.
  19. */
  20. import org.apache.archiva.configuration.ArchivaConfiguration;
  21. import org.apache.archiva.configuration.Configuration;
  22. import org.apache.archiva.configuration.ManagedRepositoryConfiguration;
  23. import org.apache.archiva.configuration.RemoteRepositoryConfiguration;
  24. import org.apache.archiva.test.utils.ArchivaSpringJUnit4ClassRunner;
  25. import org.junit.After;
  26. import org.junit.AfterClass;
  27. import org.junit.Before;
  28. import org.junit.BeforeClass;
  29. import org.junit.Test;
  30. import org.junit.runner.RunWith;
  31. import org.springframework.test.context.ContextConfiguration;
  32. import javax.inject.Inject;
  33. import java.io.IOException;
  34. import java.net.URISyntaxException;
  35. import java.net.URL;
  36. import java.nio.file.Files;
  37. import java.nio.file.Path;
  38. import java.nio.file.Paths;
  39. import java.nio.file.StandardCopyOption;
  40. import java.util.Collection;
  41. import static org.junit.Assert.*;
  42. /**
  43. * Test for RepositoryRegistry
  44. */
  45. @RunWith(ArchivaSpringJUnit4ClassRunner.class)
  46. @ContextConfiguration(locations = { "classpath*:/META-INF/spring-context.xml", "classpath:/spring-context.xml" })
  47. public class RepositoryRegistryTest
  48. {
  49. @Inject
  50. RepositoryRegistry repositoryRegistry;
  51. @Inject
  52. ArchivaConfiguration archivaConfiguration;
  53. private static final Path userCfg = Paths.get(System.getProperty( "user.home" ), ".m2/archiva.xml");
  54. private static Path cfgCopy;
  55. private static Path archivaCfg;
  56. @BeforeClass
  57. public static void classSetup() throws IOException, URISyntaxException
  58. {
  59. URL archivaCfgUri = Thread.currentThread().getContextClassLoader().getResource( "archiva.xml" );
  60. if (archivaCfgUri!=null) {
  61. archivaCfg = Paths.get(archivaCfgUri.toURI());
  62. cfgCopy = Files.createTempFile( "archiva-backup", ".xml" );
  63. Files.copy( archivaCfg, cfgCopy, StandardCopyOption.REPLACE_EXISTING);
  64. }
  65. }
  66. @AfterClass
  67. public static void classTearDown() throws IOException
  68. {
  69. if (cfgCopy!=null) {
  70. Files.deleteIfExists( cfgCopy );
  71. }
  72. }
  73. @Before
  74. public void setUp( ) throws Exception
  75. {
  76. assertNotNull( repositoryRegistry );
  77. Files.deleteIfExists( userCfg );
  78. URL archivaCfgUri = Thread.currentThread().getContextClassLoader().getResource( "archiva.xml" );
  79. if (archivaCfgUri!=null) {
  80. archivaCfg = Paths.get(archivaCfgUri.toURI());
  81. if (Files.exists(cfgCopy))
  82. {
  83. Files.copy( cfgCopy, archivaCfg , StandardCopyOption.REPLACE_EXISTING);
  84. }
  85. }
  86. archivaConfiguration.reload();
  87. repositoryRegistry.reload();
  88. }
  89. @After
  90. public void tearDown( ) throws Exception
  91. {
  92. Files.deleteIfExists( userCfg );
  93. if (cfgCopy!=null && Files.exists(cfgCopy)) {
  94. Files.copy(cfgCopy, archivaCfg, StandardCopyOption.REPLACE_EXISTING);
  95. }
  96. }
  97. @Test
  98. public void getRepositories( ) throws Exception
  99. {
  100. Collection<Repository> repos = repositoryRegistry.getRepositories( );
  101. assertEquals( 5, repos.size( ) );
  102. assertTrue(repos.stream().anyMatch( rep -> rep.getId().equals("internal") ));
  103. assertTrue( repos.stream( ).anyMatch( rep -> rep.getId( ).equals( "snapshots") ) );
  104. assertTrue(repos.stream().anyMatch( rep -> rep.getId().equals( "central") ));
  105. }
  106. @Test
  107. public void getManagedRepositories( ) throws Exception
  108. {
  109. Collection<ManagedRepository> repos = repositoryRegistry.getManagedRepositories();
  110. assertEquals( 4, repos.size( ) );
  111. assertTrue(repos.stream().anyMatch( rep -> rep.getId().equals("internal") ));
  112. assertTrue( repos.stream( ).anyMatch( rep -> rep.getId( ).equals( "snapshots") ) );
  113. }
  114. @Test
  115. public void getRemoteRepositories( ) throws Exception
  116. {
  117. Collection<RemoteRepository> repos = repositoryRegistry.getRemoteRepositories( );
  118. assertEquals( 1, repos.size( ) );
  119. assertTrue(repos.stream().anyMatch( rep -> rep.getId().equals( "central") ));
  120. }
  121. @Test
  122. public void getRepository( ) throws Exception
  123. {
  124. Repository repo = repositoryRegistry.getRepository( "internal" );
  125. assertNotNull(repo);
  126. assertEquals("internal", repo.getId());
  127. assertEquals("Archiva Managed Internal Repository", repo.getName());
  128. assertEquals("This is internal repository.", repo.getDescription());
  129. assertEquals( "default", repo.getLayout( ) );
  130. assertEquals("0 0 * * * ?", repo.getSchedulingDefinition());
  131. assertTrue(repo instanceof ManagedRepository);
  132. assertTrue( repo.hasIndex( ) );
  133. assertTrue(repo.isScanned());
  134. assertEquals(RepositoryType.MAVEN, repo.getType());
  135. }
  136. @Test
  137. public void getManagedRepository( ) throws Exception
  138. {
  139. ManagedRepository repo = repositoryRegistry.getManagedRepository( "internal" );
  140. assertNotNull(repo);
  141. assertEquals("internal", repo.getId());
  142. assertEquals("Archiva Managed Internal Repository", repo.getName());
  143. assertEquals("This is internal repository.", repo.getDescription());
  144. assertEquals( "default", repo.getLayout( ) );
  145. assertEquals("0 0 * * * ?", repo.getSchedulingDefinition());
  146. assertTrue( repo.hasIndex( ) );
  147. assertTrue(repo.isScanned());
  148. assertEquals(RepositoryType.MAVEN, repo.getType());
  149. assertTrue(repo.getActiveReleaseSchemes().contains(ReleaseScheme.RELEASE));
  150. assertFalse( repo.getActiveReleaseSchemes( ).contains( ReleaseScheme.SNAPSHOT ) );
  151. assertNotNull(repo.getContent());
  152. assertNull(repositoryRegistry.getManagedRepository( "xyu" ));
  153. }
  154. @Test
  155. public void getRemoteRepository( ) throws Exception
  156. {
  157. RemoteRepository repo = repositoryRegistry.getRemoteRepository( "central" );
  158. assertNotNull(repo);
  159. assertEquals("central", repo.getId());
  160. assertEquals("Central Repository", repo.getName());
  161. assertEquals("", repo.getDescription());
  162. assertEquals( "default", repo.getLayout( ) );
  163. assertEquals("0 0 08 ? * SUN", repo.getSchedulingDefinition());
  164. assertTrue( repo.hasIndex( ) );
  165. assertTrue(repo.isScanned());
  166. assertEquals(RepositoryType.MAVEN, repo.getType());
  167. assertEquals(35, repo.getTimeout().getSeconds());
  168. }
  169. @Test
  170. public void putManagedRepository( ) throws Exception
  171. {
  172. BasicManagedRepository managedRepository = BasicManagedRepository.newFilesystemInstance("test001", "Test repo", archivaConfiguration.getRepositoryBaseDir().resolve("test001"));
  173. managedRepository.setDescription( managedRepository.getPrimaryLocale(), "This is just a test" );
  174. repositoryRegistry.putRepository(managedRepository);
  175. assertNotNull(managedRepository.getContent());
  176. assertEquals(6, repositoryRegistry.getRepositories().size());
  177. managedRepository = BasicManagedRepository.newFilesystemInstance("central", "Test repo", archivaConfiguration.getRepositoryBaseDir().resolve("central"));
  178. managedRepository.setDescription( managedRepository.getPrimaryLocale(), "This is just a test" );
  179. ManagedRepository updatedRepo = null;
  180. try {
  181. repositoryRegistry.putRepository( managedRepository );
  182. throw new RuntimeException("Repository exception should be thrown, if there exists a remote repository already with that id");
  183. } catch (RepositoryException e) {
  184. // OK
  185. }
  186. managedRepository = BasicManagedRepository.newFilesystemInstance("internal", "Test repo", archivaConfiguration.getRepositoryBaseDir().resolve("internal"));
  187. managedRepository.setDescription( managedRepository.getPrimaryLocale(), "This is just a test" );
  188. updatedRepo = repositoryRegistry.putRepository( managedRepository );
  189. assertTrue(updatedRepo==managedRepository);
  190. assertNotNull(managedRepository.getContent());
  191. assertEquals(6, repositoryRegistry.getRepositories().size());
  192. ManagedRepository managedRepository1 = repositoryRegistry.getManagedRepository( "internal" );
  193. assertEquals("Test repo", managedRepository1.getName());
  194. assertTrue(managedRepository1==managedRepository);
  195. }
  196. @Test
  197. public void putManagedRepositoryFromConfig( ) throws Exception
  198. {
  199. ManagedRepositoryConfiguration cfg = new ManagedRepositoryConfiguration();
  200. cfg.setId("test002");
  201. cfg.setName("This is test 002");
  202. ManagedRepository repo = repositoryRegistry.putRepository( cfg );
  203. assertNotNull(repo);
  204. assertEquals("test002", repo.getId());
  205. assertEquals("This is test 002", repo.getName());
  206. assertNotNull(repo.getContent());
  207. archivaConfiguration.reload();
  208. Collection<ManagedRepository> repos = repositoryRegistry.getManagedRepositories();
  209. assertEquals(5, repos.size());
  210. ManagedRepository internalRepo = repositoryRegistry.getManagedRepository( "internal" );
  211. cfg = new ManagedRepositoryConfiguration();
  212. cfg.setId("internal");
  213. cfg.setName("This is internal test 002");
  214. repo = repositoryRegistry.putRepository( cfg );
  215. assertTrue(internalRepo==repo);
  216. assertEquals("This is internal test 002",repo.getName());
  217. assertEquals(5, repositoryRegistry.getManagedRepositories().size());
  218. repositoryRegistry.reload();
  219. assertEquals(5, repositoryRegistry.getManagedRepositories().size());
  220. }
  221. @Test
  222. public void putManagedRepositoryFromConfigWithoutSave( ) throws Exception
  223. {
  224. Configuration configuration = archivaConfiguration.getConfiguration();
  225. ManagedRepositoryConfiguration cfg = new ManagedRepositoryConfiguration();
  226. cfg.setId("test002");
  227. cfg.setName("This is test 002");
  228. ManagedRepository repo = repositoryRegistry.putRepository( cfg, configuration );
  229. assertNotNull(repo);
  230. assertEquals("test002", repo.getId());
  231. assertEquals("This is test 002", repo.getName());
  232. assertNotNull(repo.getContent());
  233. archivaConfiguration.reload();
  234. assertEquals(3, archivaConfiguration.getConfiguration().getManagedRepositories().size());
  235. Collection<ManagedRepository> repos = repositoryRegistry.getManagedRepositories();
  236. assertEquals(5, repos.size());
  237. ManagedRepository internalRepo = repositoryRegistry.getManagedRepository( "internal" );
  238. cfg = new ManagedRepositoryConfiguration();
  239. cfg.setId("internal");
  240. cfg.setName("This is internal test 002");
  241. repo = repositoryRegistry.putRepository( cfg, configuration );
  242. assertTrue(internalRepo==repo);
  243. assertEquals("This is internal test 002",repo.getName());
  244. assertEquals(5, repositoryRegistry.getManagedRepositories().size());
  245. repositoryRegistry.reload();
  246. assertEquals(4, repositoryRegistry.getManagedRepositories().size());
  247. }
  248. @Test
  249. public void putRemoteRepository( ) throws Exception
  250. {
  251. BasicRemoteRepository remoteRepository = BasicRemoteRepository.newFilesystemInstance( "test001", "Test repo", archivaConfiguration.getRemoteRepositoryBaseDir() );
  252. remoteRepository.setDescription( remoteRepository.getPrimaryLocale(), "This is just a test" );
  253. RemoteRepository newRepo = repositoryRegistry.putRepository(remoteRepository);
  254. assertTrue(remoteRepository==newRepo);
  255. assertNotNull(remoteRepository.getContent());
  256. assertEquals(6, repositoryRegistry.getRepositories().size());
  257. remoteRepository = BasicRemoteRepository.newFilesystemInstance( "internal", "Test repo", archivaConfiguration.getRemoteRepositoryBaseDir() );
  258. remoteRepository.setDescription( remoteRepository.getPrimaryLocale(), "This is just a test" );
  259. RemoteRepository updatedRepo = null;
  260. try
  261. {
  262. updatedRepo = repositoryRegistry.putRepository( remoteRepository );
  263. throw new RuntimeException("Should throw repository exception, if repository exists already and is not the same type.");
  264. } catch (RepositoryException e) {
  265. // OK
  266. }
  267. remoteRepository = BasicRemoteRepository.newFilesystemInstance( "central", "Test repo", archivaConfiguration.getRemoteRepositoryBaseDir() );
  268. remoteRepository.setDescription( remoteRepository.getPrimaryLocale(), "This is just a test" );
  269. updatedRepo = repositoryRegistry.putRepository( remoteRepository );
  270. assertTrue(updatedRepo==remoteRepository);
  271. assertNotNull(remoteRepository.getContent());
  272. assertEquals(6, repositoryRegistry.getRepositories().size());
  273. RemoteRepository remoteRepository1 = repositoryRegistry.getRemoteRepository( "central" );
  274. assertEquals("Test repo", remoteRepository1.getName());
  275. assertTrue(remoteRepository1==remoteRepository);
  276. }
  277. @Test
  278. public void putRemoteRepositoryFromConfig( ) throws Exception
  279. {
  280. RemoteRepositoryConfiguration cfg = new RemoteRepositoryConfiguration();
  281. cfg.setId("test002");
  282. cfg.setName("This is test 002");
  283. RemoteRepository repo = repositoryRegistry.putRepository( cfg );
  284. assertNotNull(repo);
  285. assertEquals("test002", repo.getId());
  286. assertEquals("This is test 002", repo.getName());
  287. assertNotNull(repo.getContent());
  288. archivaConfiguration.reload();
  289. Collection<RemoteRepository> repos = repositoryRegistry.getRemoteRepositories();
  290. assertEquals(2, repos.size());
  291. RemoteRepository internalRepo = repositoryRegistry.getRemoteRepository( "central" );
  292. cfg = new RemoteRepositoryConfiguration();
  293. cfg.setId("central");
  294. cfg.setName("This is central test 002");
  295. repo = repositoryRegistry.putRepository( cfg );
  296. assertTrue(internalRepo==repo);
  297. assertEquals("This is central test 002",repo.getName());
  298. assertEquals(2, repositoryRegistry.getRemoteRepositories().size());
  299. repositoryRegistry.reload();
  300. assertEquals(2, repositoryRegistry.getRemoteRepositories().size());
  301. }
  302. @Test
  303. public void putRemoteRepositoryFromConfigWithoutSave( ) throws Exception
  304. {
  305. Configuration configuration = archivaConfiguration.getConfiguration();
  306. RemoteRepositoryConfiguration cfg = new RemoteRepositoryConfiguration();
  307. cfg.setId("test002");
  308. cfg.setName("This is test 002");
  309. RemoteRepository repo = repositoryRegistry.putRepository( cfg, configuration );
  310. assertNotNull(repo);
  311. assertEquals("test002", repo.getId());
  312. assertEquals("This is test 002", repo.getName());
  313. assertNotNull(repo.getContent());
  314. archivaConfiguration.reload();
  315. assertEquals(1, archivaConfiguration.getConfiguration().getRemoteRepositories().size());
  316. Collection<RemoteRepository> repos = repositoryRegistry.getRemoteRepositories();
  317. assertEquals(2, repos.size());
  318. RemoteRepository internalRepo = repositoryRegistry.getRemoteRepository( "central" );
  319. cfg = new RemoteRepositoryConfiguration();
  320. cfg.setId("central");
  321. cfg.setName("This is central test 002");
  322. repo = repositoryRegistry.putRepository( cfg, configuration );
  323. assertTrue(internalRepo==repo);
  324. assertEquals("This is central test 002",repo.getName());
  325. assertEquals(2, repositoryRegistry.getRemoteRepositories().size());
  326. repositoryRegistry.reload();
  327. assertEquals(1, repositoryRegistry.getRemoteRepositories().size());
  328. }
  329. @Test
  330. public void removeRepository( ) throws Exception
  331. {
  332. assertEquals(5, repositoryRegistry.getRepositories().size());
  333. Repository repo = repositoryRegistry.getRepository( "snapshots" );
  334. repositoryRegistry.removeRepository( repo );
  335. assertEquals(4, repositoryRegistry.getRepositories().size());
  336. assertTrue( repositoryRegistry.getRepositories( ).stream( ).noneMatch( rep -> rep.getId( ).equals( "snapshots" ) ) );
  337. archivaConfiguration.reload();
  338. repositoryRegistry.reload();
  339. assertEquals(4, repositoryRegistry.getRepositories().size());
  340. }
  341. @Test
  342. public void removeManagedRepository( ) throws Exception
  343. {
  344. assertEquals(4, repositoryRegistry.getManagedRepositories().size());
  345. ManagedRepository repo = repositoryRegistry.getManagedRepository( "snapshots" );
  346. repositoryRegistry.removeRepository( repo );
  347. assertEquals(3, repositoryRegistry.getManagedRepositories().size());
  348. assertTrue( repositoryRegistry.getManagedRepositories( ).stream( ).noneMatch( rep -> rep.getId( ).equals( "snapshots" ) ) );
  349. archivaConfiguration.reload();
  350. repositoryRegistry.reload();
  351. assertEquals(3, repositoryRegistry.getManagedRepositories().size());
  352. }
  353. @Test
  354. public void removeManagedRepositoryWithoutSave( ) throws Exception
  355. {
  356. Configuration configuration = archivaConfiguration.getConfiguration();
  357. assertEquals(4, repositoryRegistry.getManagedRepositories().size());
  358. ManagedRepository repo = repositoryRegistry.getManagedRepository( "snapshots" );
  359. repositoryRegistry.removeRepository( repo, configuration );
  360. assertEquals(3, repositoryRegistry.getManagedRepositories().size());
  361. assertTrue( repositoryRegistry.getManagedRepositories( ).stream( ).noneMatch( rep -> rep.getId( ).equals( "snapshots" ) ) );
  362. archivaConfiguration.reload();
  363. repositoryRegistry.reload();
  364. assertEquals(4, repositoryRegistry.getManagedRepositories().size());
  365. }
  366. @Test
  367. public void removeRemoteRepository( ) throws Exception
  368. {
  369. assertEquals(1, repositoryRegistry.getRemoteRepositories().size());
  370. RemoteRepository repo = repositoryRegistry.getRemoteRepository( "central" );
  371. repositoryRegistry.removeRepository( repo );
  372. assertEquals(0, repositoryRegistry.getRemoteRepositories().size());
  373. assertTrue( repositoryRegistry.getRemoteRepositories( ).stream( ).noneMatch( rep -> rep.getId( ).equals( "central" ) ) );
  374. archivaConfiguration.reload();
  375. repositoryRegistry.reload();
  376. assertEquals(0, repositoryRegistry.getRemoteRepositories().size());
  377. }
  378. @Test
  379. public void removeRemoteRepositoryWithoutSave( ) throws Exception
  380. {
  381. Configuration configuration = archivaConfiguration.getConfiguration();
  382. assertEquals(1, repositoryRegistry.getRemoteRepositories().size());
  383. RemoteRepository repo = repositoryRegistry.getRemoteRepository( "central" );
  384. repositoryRegistry.removeRepository( repo, configuration );
  385. assertEquals(0, repositoryRegistry.getRemoteRepositories().size());
  386. assertTrue( repositoryRegistry.getRemoteRepositories( ).stream( ).noneMatch( rep -> rep.getId( ).equals( "central" ) ) );
  387. archivaConfiguration.reload();
  388. repositoryRegistry.reload();
  389. assertEquals(1, repositoryRegistry.getRemoteRepositories().size());
  390. }
  391. @Test
  392. public void cloneManagedRepo( ) throws Exception
  393. {
  394. ManagedRepository managedRepository = repositoryRegistry.getManagedRepository( "internal" );
  395. try
  396. {
  397. repositoryRegistry.clone(managedRepository, "snapshots");
  398. throw new RuntimeException("RepositoryRegistry exception should be thrown if id exists already.");
  399. }
  400. catch ( RepositoryException e )
  401. {
  402. // OK
  403. }
  404. try
  405. {
  406. repositoryRegistry.clone(managedRepository, "central");
  407. throw new RuntimeException("RepositoryRegistry exception should be thrown if id exists already.");
  408. }
  409. catch ( RepositoryException e )
  410. {
  411. // OK
  412. }
  413. ManagedRepository clone = repositoryRegistry.clone( managedRepository, "newinternal" );
  414. assertNotNull(clone);
  415. assertNull(clone.getContent());
  416. assertEquals("Archiva Managed Internal Repository", clone.getName());
  417. assertFalse(managedRepository==clone);
  418. }
  419. @Test
  420. public void cloneRemoteRepo( ) throws Exception
  421. {
  422. RemoteRepository remoteRepository = repositoryRegistry.getRemoteRepository( "central" );
  423. try
  424. {
  425. repositoryRegistry.clone(remoteRepository, "snapshots");
  426. throw new RuntimeException("RepositoryRegistry exception should be thrown if id exists already.");
  427. }
  428. catch ( RepositoryException e )
  429. {
  430. // OK
  431. }
  432. try
  433. {
  434. repositoryRegistry.clone(remoteRepository, "central");
  435. throw new RuntimeException("RepositoryRegistry exception should be thrown if id exists already.");
  436. }
  437. catch ( RepositoryException e )
  438. {
  439. // OK
  440. }
  441. RemoteRepository clone = repositoryRegistry.clone( remoteRepository, "newCentral" );
  442. assertNotNull(clone);
  443. assertNull(clone.getContent());
  444. assertEquals("Central Repository", clone.getName());
  445. assertFalse(remoteRepository==clone);
  446. }
  447. }