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

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