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.

MavenRepositoryProviderTest.java 17KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  1. package org.apache.archiva.repository.maven2;
  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.common.utils.FileUtils;
  21. import org.apache.archiva.configuration.ArchivaRuntimeConfiguration;
  22. import org.apache.archiva.configuration.ManagedRepositoryConfiguration;
  23. import org.apache.archiva.configuration.RemoteRepositoryConfiguration;
  24. import org.apache.archiva.configuration.RepositoryGroupConfiguration;
  25. import org.apache.archiva.metadata.repository.storage.maven2.conf.MockConfiguration;
  26. import org.apache.archiva.repository.*;
  27. import org.apache.archiva.repository.features.ArtifactCleanupFeature;
  28. import org.apache.archiva.repository.features.IndexCreationFeature;
  29. import org.apache.archiva.repository.features.RemoteIndexFeature;
  30. import org.apache.archiva.repository.features.StagingRepositoryFeature;
  31. import org.apache.archiva.repository.base.PasswordCredentials;
  32. import org.junit.After;
  33. import org.junit.Before;
  34. import org.junit.Test;
  35. import java.io.IOException;
  36. import java.net.URI;
  37. import java.net.URISyntaxException;
  38. import java.nio.file.Files;
  39. import java.nio.file.Path;
  40. import java.nio.file.Paths;
  41. import java.time.Duration;
  42. import java.time.Period;
  43. import java.time.temporal.ChronoUnit;
  44. import java.util.ArrayList;
  45. import java.util.HashMap;
  46. import java.util.Map;
  47. import static org.junit.Assert.*;
  48. /**
  49. * @author Martin Stockhammer <martin_s@apache.org>
  50. */
  51. public class MavenRepositoryProviderTest
  52. {
  53. MavenRepositoryProvider provider;
  54. RepositoryRegistry reg;
  55. Path repoLocation;
  56. @Before
  57. public void setUp()
  58. throws Exception
  59. {
  60. provider = new MavenRepositoryProvider( );
  61. MockConfiguration mockConfiguration =new MockConfiguration();
  62. mockConfiguration.getConfiguration().setArchivaRuntimeConfiguration( new ArchivaRuntimeConfiguration() );
  63. mockConfiguration.getConfiguration().getArchivaRuntimeConfiguration().setRepositoryBaseDirectory( "repositories" );
  64. provider.setArchivaConfiguration( mockConfiguration );
  65. }
  66. @After
  67. public void cleanUp() {
  68. if (repoLocation!=null && Files.exists( repoLocation )) {
  69. FileUtils.deleteQuietly( repoLocation );
  70. }
  71. }
  72. @Test
  73. public void provides( ) throws Exception
  74. {
  75. assertEquals(1, provider.provides().size());
  76. assertEquals( RepositoryType.MAVEN, provider.provides().iterator().next());
  77. }
  78. @Test
  79. public void createManagedInstance( ) throws Exception
  80. {
  81. ManagedRepositoryConfiguration repo = new ManagedRepositoryConfiguration( );
  82. repo.setId("testm001");
  83. repo.setName("Managed Test Repo 001");
  84. repo.setDescription( "This is a managed test" );
  85. repo.setRetentionPeriod( 37 );
  86. repoLocation = Files.createTempDirectory( "test-repo-001");
  87. repo.setLocation( repoLocation.toAbsolutePath().toString() );
  88. repo.setSnapshots( true );
  89. repo.setReleases( true );
  90. repo.setRefreshCronExpression( "4 0 0 ? * TUE" );
  91. repo.setScanned( true );
  92. repo.setBlockRedeployments( true );
  93. repo.setDeleteReleasedSnapshots( true );
  94. repo.setRetentionCount( 33 );
  95. repo.setSkipPackedIndexCreation( true );
  96. repo.setStageRepoNeeded( true );
  97. repo.setIndexDir( "testmanaged/.index" );
  98. repo.setLayout( "maven2" );
  99. repo.setType( RepositoryType.MAVEN.toString() );
  100. ManagedRepository mr = provider.createManagedInstance( repo );
  101. assertNotNull(mr.getLocation());
  102. String repoUri = repoLocation.toUri().toString();
  103. assertTrue(Files.exists(repoLocation));
  104. repoUri = repoUri.substring( 0, repoUri.length()-1 );
  105. assertEquals(repoUri, mr.getLocation().toString());
  106. assertEquals("This is a managed test", mr.getDescription());
  107. assertEquals("Managed Test Repo 001", mr.getName());
  108. assertEquals(2, mr.getActiveReleaseSchemes().size());
  109. assertTrue( mr.getActiveReleaseSchemes().contains( ReleaseScheme.RELEASE ));
  110. assertTrue( mr.getActiveReleaseSchemes().contains( ReleaseScheme.SNAPSHOT));
  111. assertEquals("testm001", mr.getId());
  112. assertTrue(mr.blocksRedeployments());
  113. assertEquals("4 0 0 ? * TUE", mr.getSchedulingDefinition());
  114. assertTrue(mr.isScanned());
  115. ArtifactCleanupFeature artifactCleanupFeature = mr.getFeature( ArtifactCleanupFeature.class ).get();
  116. assertEquals( Period.ofDays( 37), artifactCleanupFeature.getRetentionPeriod());
  117. assertTrue(artifactCleanupFeature.isDeleteReleasedSnapshots());
  118. assertEquals(33, artifactCleanupFeature.getRetentionCount());
  119. IndexCreationFeature indexCreationFeature = mr.getFeature( IndexCreationFeature.class ).get();
  120. assertNotNull(indexCreationFeature.getIndexPath());
  121. assertEquals("testmanaged/.index", indexCreationFeature.getIndexPath().toString());
  122. assertFalse(indexCreationFeature.getIndexPath().isAbsolute());
  123. assertTrue(indexCreationFeature.isSkipPackedIndexCreation());
  124. StagingRepositoryFeature stagingRepositoryFeature = mr.getFeature( StagingRepositoryFeature.class ).get();
  125. assertTrue(stagingRepositoryFeature.isStageRepoNeeded());
  126. assertNull(stagingRepositoryFeature.getStagingRepository());
  127. }
  128. @Test
  129. public void createRemoteInstance( ) throws Exception
  130. {
  131. RemoteRepositoryConfiguration repo = new RemoteRepositoryConfiguration( );
  132. repo.setUsername("testuser001");
  133. repo.setPassword( "pwd0000abc" );
  134. repo.setCheckPath( "test/check.html" );
  135. repo.setTimeout( 50 );
  136. repo.setUrl( "https://repo.maven.apache.org/maven2/test" );
  137. repo.setDownloadRemoteIndex( true );
  138. repo.setDownloadRemoteIndexOnStartup( true );
  139. Map<String,String> header = new HashMap<>( );
  140. header.put("header1","value1");
  141. header.put("header2","value2");
  142. repo.setExtraHeaders( header );
  143. Map<String,String> params = new HashMap<>( );
  144. params.put("param1","pval1");
  145. params.put("param2","pval2");
  146. repo.setExtraParameters( params );
  147. repo.setRefreshCronExpression( "0 1 07 ? * MON" );
  148. repo.setRemoteDownloadTimeout( 333 );
  149. repo.setRemoteIndexUrl( "testremote/.index" );
  150. repo.setDescription( "This is a test" );
  151. repo.setId( "test001" );
  152. repo.setName( "Remote Test Repo 001" );
  153. repo.setIndexDir( "testindex/.index" );
  154. repo.setLayout( "maven2" );
  155. repo.setType( RepositoryType.MAVEN.toString() );
  156. repo.setIndexDir( "local/.index" );
  157. RemoteRepository mr = provider.createRemoteInstance( repo );
  158. assertEquals("test001", mr.getId());
  159. assertEquals("This is a test", mr.getDescription());
  160. assertNotNull(mr.getLocation());
  161. assertEquals("https://repo.maven.apache.org/maven2/test", mr.getLocation().toString());
  162. assertEquals("Remote Test Repo 001", mr.getName());
  163. assertEquals("test001", mr.getId());
  164. assertEquals("0 1 07 ? * MON", mr.getSchedulingDefinition());
  165. assertEquals(50, mr.getTimeout().get( ChronoUnit.SECONDS ));
  166. assertTrue(mr.isScanned());
  167. assertNotNull(mr.getLoginCredentials());
  168. assertTrue(mr.getLoginCredentials() instanceof PasswordCredentials );
  169. PasswordCredentials creds = (PasswordCredentials) mr.getLoginCredentials();
  170. assertEquals("testuser001", creds.getUsername());
  171. assertEquals("pwd0000abc", new String(creds.getPassword()));
  172. assertEquals("value1", mr.getExtraHeaders().get("header1"));
  173. assertEquals("pval2", mr.getExtraParameters().get("param2"));
  174. assertEquals( "maven2", mr.getLayout());
  175. try
  176. {
  177. ArtifactCleanupFeature artifactCleanupFeature = mr.getFeature( ArtifactCleanupFeature.class ).get( );
  178. throw new Exception("artifactCleanupFeature should not be available");
  179. } catch ( UnsupportedFeatureException e ) {
  180. // correct
  181. }
  182. IndexCreationFeature indexCreationFeature = mr.getFeature( IndexCreationFeature.class ).get( );
  183. assertEquals("local/.index", indexCreationFeature.getIndexPath().toString());
  184. try
  185. {
  186. StagingRepositoryFeature stagingRepositoryFeature = mr.getFeature( StagingRepositoryFeature.class ).get( );
  187. throw new Exception("stagingRepositoryFeature should not be available");
  188. } catch (UnsupportedFeatureException e) {
  189. // correct
  190. }
  191. RemoteIndexFeature remoteIndexFeature = mr.getFeature( RemoteIndexFeature.class ).get();
  192. assertNull(remoteIndexFeature.getProxyId());
  193. }
  194. @Test
  195. public void getManagedConfiguration() throws Exception {
  196. MavenManagedRepository repo = MavenManagedRepository.newLocalInstance( "test01", "My Test repo", Paths.get("target/repositories") );
  197. repo.setLocation( new URI("target/this.is/a/test") );
  198. repo.setScanned( true );
  199. repo.setDescription( repo.getPrimaryLocale(), "This is a description" );
  200. repo.setLayout( "maven2" );
  201. repo.setBlocksRedeployment( true );
  202. repo.setName( repo.getPrimaryLocale(), "test0002" );
  203. repo.setSchedulingDefinition( "0 0 05 ? * WED" );
  204. repo.addActiveReleaseScheme( ReleaseScheme.RELEASE );
  205. repo.addActiveReleaseScheme( ReleaseScheme.SNAPSHOT );
  206. StagingRepositoryFeature stagingFeat = repo.getFeature( StagingRepositoryFeature.class ).get( );
  207. stagingFeat.setStageRepoNeeded( true );
  208. IndexCreationFeature indexCreationFeature = repo.getFeature( IndexCreationFeature.class ).get();
  209. indexCreationFeature.setIndexPath( new URI("test/.indexes") );
  210. indexCreationFeature.setSkipPackedIndexCreation( true );
  211. ArtifactCleanupFeature artifactCleanupFeature = repo.getFeature( ArtifactCleanupFeature.class ).get();
  212. artifactCleanupFeature.setRetentionPeriod( Period.ofDays( 5 ) );
  213. artifactCleanupFeature.setRetentionCount( 7 );
  214. artifactCleanupFeature.setDeleteReleasedSnapshots( true );
  215. ManagedRepositoryConfiguration cfg = provider.getManagedConfiguration( repo );
  216. assertEquals("target/this.is/a/test", cfg.getLocation());
  217. assertTrue(cfg.isScanned());
  218. assertEquals( "This is a description", cfg.getDescription() );
  219. assertEquals("maven2", cfg.getLayout());
  220. assertTrue(cfg.isBlockRedeployments());
  221. assertEquals("test0002", cfg.getName());
  222. assertEquals("0 0 05 ? * WED", cfg.getRefreshCronExpression());
  223. assertTrue(cfg.isStageRepoNeeded());
  224. assertEquals("test/.indexes", cfg.getIndexDir());
  225. assertTrue(cfg.isSkipPackedIndexCreation());
  226. assertEquals(5, cfg.getRetentionPeriod());
  227. assertEquals(7, cfg.getRetentionCount());
  228. assertTrue(cfg.isDeleteReleasedSnapshots());
  229. assertTrue(cfg.isReleases());
  230. assertTrue(cfg.isSnapshots());
  231. assertTrue(cfg.isScanned());
  232. }
  233. @Test
  234. public void getRemoteConfiguration() throws Exception {
  235. MavenRemoteRepository repo = MavenRemoteRepository.newLocalInstance( "test01", "My Test repo", Paths.get("target/remotes") );
  236. repo.setLocation( new URI("https://this.is/a/test") );
  237. repo.setScanned( true );
  238. repo.setDescription( repo.getPrimaryLocale(), "This is a description" );
  239. repo.setLayout( "maven2" );
  240. repo.setName( repo.getPrimaryLocale(), "test0003" );
  241. repo.setSchedulingDefinition( "0 0 05 ? * WED" );
  242. RemoteIndexFeature remoteIndexFeature = repo.getFeature( RemoteIndexFeature.class ).get();
  243. remoteIndexFeature.setProxyId( "proxyabc" );
  244. remoteIndexFeature.setDownloadTimeout( Duration.ofSeconds( 54 ) );
  245. remoteIndexFeature.setDownloadRemoteIndex( false );
  246. remoteIndexFeature.setIndexUri( new URI("/this/remote/.index") );
  247. remoteIndexFeature.setDownloadRemoteIndexOnStartup( true );
  248. IndexCreationFeature indexCreationFeature = repo.getFeature( IndexCreationFeature.class ).get();
  249. indexCreationFeature.setIndexPath( new URI("/this/local/.index") );
  250. RemoteRepositoryConfiguration cfg = provider.getRemoteConfiguration( repo );
  251. assertEquals("https://this.is/a/test", cfg.getUrl());
  252. assertEquals( "This is a description", cfg.getDescription() );
  253. assertEquals("maven2", cfg.getLayout());
  254. assertEquals("test0003", cfg.getName());
  255. assertEquals("0 0 05 ? * WED", cfg.getRefreshCronExpression());
  256. assertEquals("/this/remote/.index", cfg.getRemoteIndexUrl());
  257. assertEquals("proxyabc", cfg.getRemoteDownloadNetworkProxyId());
  258. assertEquals(54, cfg.getRemoteDownloadTimeout());
  259. assertFalse(cfg.isDownloadRemoteIndex());
  260. assertTrue(cfg.isDownloadRemoteIndexOnStartup());
  261. assertEquals("/this/local/.index", cfg.getIndexDir());
  262. }
  263. @Test
  264. public void getRepositoryGroupConfiguration() throws RepositoryException, URISyntaxException, IOException {
  265. MavenRepositoryGroup repositoryGroup = MavenRepositoryGroup.newLocalInstance("group1","group1",Paths.get("target/groups"));
  266. MavenManagedRepository repo1 = MavenManagedRepository.newLocalInstance( "test01", "My Test repo", Paths.get("target/repositories") );
  267. MavenManagedRepository repo2 = MavenManagedRepository.newLocalInstance( "test02", "My Test repo", Paths.get("target/repositories") );
  268. repositoryGroup.setDescription(repositoryGroup.getPrimaryLocale(), "Repository group");
  269. repositoryGroup.setLayout("non-default");
  270. IndexCreationFeature indexCreationFeature = repositoryGroup.getFeature( IndexCreationFeature.class ).get();
  271. indexCreationFeature.setIndexPath( new URI(".index2") );
  272. repositoryGroup.setName(repositoryGroup.getPrimaryLocale(), "Repo Group 1");
  273. repositoryGroup.setMergedIndexTTL(1005);
  274. repositoryGroup.setSchedulingDefinition("0 0 04 ? * THU");
  275. repositoryGroup.addRepository(repo1);
  276. repositoryGroup.addRepository(repo2);
  277. RepositoryGroupConfiguration cfg = provider.getRepositoryGroupConfiguration(repositoryGroup);
  278. assertEquals("group1", cfg.getId());
  279. assertEquals(".index2", cfg.getMergedIndexPath());
  280. assertEquals("0 0 04 ? * THU", cfg.getCronExpression());
  281. assertEquals("Repo Group 1", cfg.getName());
  282. assertEquals(1005, cfg.getMergedIndexTtl());
  283. assertTrue(cfg.getRepositories().contains("test01"));
  284. assertTrue(cfg.getRepositories().contains("test02"));
  285. assertEquals(2, cfg.getRepositories().size());
  286. }
  287. @Test
  288. public void createRepositoryGroup() {
  289. EditableRepositoryGroup gr = provider.createRepositoryGroup("group1", "Group 1");
  290. assertEquals("group1",gr.getId());
  291. assertEquals("Group 1", gr.getName());
  292. assertEquals(MavenRepositoryGroup.class, gr.getClass());
  293. }
  294. @Test
  295. public void createRepositoryGroupWithCfg() throws RepositoryException {
  296. RepositoryGroupConfiguration cfg = new RepositoryGroupConfiguration();
  297. cfg.setId("group2");
  298. cfg.setName("Group 2");
  299. cfg.setCronExpression("0 0 03 ? * MON");
  300. cfg.setMergedIndexTtl(504);
  301. cfg.setMergedIndexPath(".index-abc");
  302. ArrayList<String> repos = new ArrayList<>();
  303. repos.add("test01");
  304. repos.add("test02");
  305. cfg.setRepositories(repos);
  306. RepositoryGroup grp = provider.createRepositoryGroup(cfg);
  307. assertEquals("group2", grp.getId());
  308. assertEquals("Group 2", grp.getName());
  309. assertEquals("0 0 03 ? * MON", grp.getSchedulingDefinition());
  310. IndexCreationFeature indexCreationFeature = grp.getFeature( IndexCreationFeature.class ).get();
  311. try {
  312. assertEquals(new URI(".index-abc"), indexCreationFeature.getIndexPath());
  313. } catch (URISyntaxException e) {
  314. e.printStackTrace();
  315. }
  316. assertEquals(504, grp.getMergedIndexTTL());
  317. assertEquals(0, grp.getRepositories().size());
  318. // assertTrue(grp.getRepositories().stream().anyMatch(r -> "test01".equals(r.getId())));
  319. // assertTrue(grp.getRepositories().stream().anyMatch(r -> "test02".equals(r.getId())));
  320. }
  321. }