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.

Maven2RepositoryMetadataResolverMRM1411Test.java 21KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459
  1. package org.apache.archiva.repository.maven.metadata.storage;
  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. * Unless required by applicable law or agreed to in writing,
  13. * software distributed under the License is distributed on an
  14. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  15. * KIND, either express or implied. See the License for the
  16. * specific language governing permissions and limitations
  17. * under the License.
  18. */
  19. import junit.framework.TestCase;
  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.ProxyConnectorConfiguration;
  24. import org.apache.archiva.configuration.RemoteRepositoryConfiguration;
  25. import org.apache.archiva.filter.AllFilter;
  26. import org.apache.archiva.filter.Filter;
  27. import org.apache.archiva.metadata.model.ArtifactMetadata;
  28. import org.apache.archiva.metadata.model.Dependency;
  29. import org.apache.archiva.metadata.model.License;
  30. import org.apache.archiva.metadata.model.MailingList;
  31. import org.apache.archiva.metadata.model.ProjectVersionMetadata;
  32. import org.apache.archiva.metadata.repository.storage.ReadMetadataRequest;
  33. import org.apache.archiva.metadata.repository.storage.RepositoryStorageRuntimeException;
  34. import org.apache.archiva.proxy.maven.WagonFactory;
  35. import org.apache.archiva.proxy.maven.WagonFactoryRequest;
  36. import org.apache.archiva.repository.ReleaseScheme;
  37. import org.apache.archiva.repository.RepositoryRegistry;
  38. import org.apache.archiva.repository.base.RepositoryHandlerDependencies;
  39. import org.apache.archiva.test.utils.ArchivaSpringJUnit4ClassRunner;
  40. import org.apache.commons.io.FileUtils;
  41. import org.apache.maven.wagon.Wagon;
  42. import org.junit.Before;
  43. import org.junit.Test;
  44. import org.junit.runner.RunWith;
  45. import org.springframework.test.context.ContextConfiguration;
  46. import javax.inject.Inject;
  47. import javax.inject.Named;
  48. import java.io.IOException;
  49. import java.nio.file.Files;
  50. import java.nio.file.Path;
  51. import java.nio.file.Paths;
  52. import java.util.ArrayList;
  53. import java.util.Arrays;
  54. import java.util.HashMap;
  55. import java.util.List;
  56. import static org.mockito.Mockito.mock;
  57. import static org.mockito.Mockito.when;
  58. @RunWith ( ArchivaSpringJUnit4ClassRunner.class )
  59. @ContextConfiguration ( { "classpath*:/META-INF/spring-context.xml", "classpath:/spring-context.xml" } )
  60. public class Maven2RepositoryMetadataResolverMRM1411Test
  61. extends TestCase
  62. {
  63. private static final Filter<String> ALL = new AllFilter<String>();
  64. @Inject
  65. @Named ( "repositoryStorage#maven2")
  66. private Maven2RepositoryStorage storage;
  67. private static final String TEST_REPO_ID = "test";
  68. private static final String TEST_REMOTE_REPO_ID = "central";
  69. private static final String ASF_SCM_CONN_BASE = "scm:svn:http://svn.apache.org/repos/asf/";
  70. private static final String ASF_SCM_DEV_CONN_BASE = "scm:svn:https://svn.apache.org/repos/asf/";
  71. private static final String ASF_SCM_VIEWVC_BASE = "http://svn.apache.org/viewvc/";
  72. private static final String TEST_SCM_CONN_BASE = "scm:svn:http://svn.example.com/repos/";
  73. private static final String TEST_SCM_DEV_CONN_BASE = "scm:svn:https://svn.example.com/repos/";
  74. private static final String TEST_SCM_URL_BASE = "http://svn.example.com/repos/";
  75. private static final String EMPTY_MD5 = "d41d8cd98f00b204e9800998ecf8427e";
  76. private static final String EMPTY_SHA1 = "da39a3ee5e6b4b0d3255bfef95601890afd80709";
  77. @Inject
  78. @Named ( "archivaConfiguration#default" )
  79. private ArchivaConfiguration configuration;
  80. @Inject
  81. RepositoryRegistry repositoryRegistry;
  82. @SuppressWarnings( "unused" )
  83. @Inject
  84. RepositoryHandlerDependencies repositoryHandlerDependencies;
  85. private WagonFactory wagonFactory;
  86. ManagedRepositoryConfiguration testRepo;
  87. Configuration c;
  88. @Before
  89. @Override
  90. public void setUp()
  91. throws Exception
  92. {
  93. super.setUp();
  94. c = new Configuration();
  95. testRepo = new ManagedRepositoryConfiguration();
  96. testRepo.setId( TEST_REPO_ID );
  97. testRepo.setLocation( Paths.get( "target/test-repository" ).toAbsolutePath().toString() );
  98. testRepo.setReleases( true );
  99. testRepo.setSnapshots( true );
  100. c.addManagedRepository( testRepo );
  101. RemoteRepositoryConfiguration testRemoteRepo = new RemoteRepositoryConfiguration();
  102. testRemoteRepo.setId( TEST_REMOTE_REPO_ID );
  103. testRemoteRepo.setLayout( "default" );
  104. testRemoteRepo.setName( "Central Repository" );
  105. testRemoteRepo.setUrl( "http://central.repo.com/maven2" );
  106. testRemoteRepo.setTimeout( 10 );
  107. c.addRemoteRepository( testRemoteRepo );
  108. ProxyConnectorConfiguration proxyConnector = new ProxyConnectorConfiguration();
  109. proxyConnector.setSourceRepoId( TEST_REPO_ID );
  110. proxyConnector.setTargetRepoId( TEST_REMOTE_REPO_ID );
  111. proxyConnector.setDisabled( false );
  112. c.addProxyConnector( proxyConnector );
  113. configuration.save( c );
  114. repositoryRegistry.reload();
  115. assertTrue( c.getManagedRepositories().get( 0 ).isSnapshots() );
  116. assertTrue( c.getManagedRepositories().get( 0 ).isReleases() );
  117. wagonFactory = mock( WagonFactory.class );
  118. assertNotNull( storage );
  119. storage.setWagonFactory( wagonFactory );
  120. Wagon wagon = new MockWagon();
  121. when( wagonFactory.getWagon(
  122. new WagonFactoryRequest( "wagon#http", new HashMap<String, String>() ) ) ).thenReturn( wagon );
  123. }
  124. // Tests for MRM-1411 - START
  125. @Test
  126. public void testGetProjectVersionMetadataWithParentSuccessful()
  127. throws Exception
  128. {
  129. assertNotNull( storage );
  130. copyTestArtifactWithParent( "target/test-classes/com/example/test/test-artifact-module-a",
  131. "target/test-repository/com/example/test/test-artifact-module-a" );
  132. copyTestArtifactWithParent( "src/test/resources/com/example/test/test-artifact-parent",
  133. "target/test-repository/com/example/test/test-artifact-parent" );
  134. copyTestArtifactWithParent( "target/test-classes/com/example/test/test-artifact-root",
  135. "target/test-repository/com/example/test/test-artifact-root" );
  136. ProjectVersionMetadata metadata = storage.readProjectVersionMetadata(
  137. new ReadMetadataRequest( TEST_REPO_ID, "com.example.test", "test-artifact-module-a", "1.0" ) );
  138. MavenProjectFacet facet = (MavenProjectFacet) metadata.getFacet( MavenProjectFacet.FACET_ID );
  139. assertEquals( "jar", facet.getPackaging() );
  140. assertEquals( "http://maven.apache.org", metadata.getUrl() );
  141. assertEquals( "com.example.test", facet.getParent().getGroupId() );
  142. assertEquals( "test-artifact-root", facet.getParent().getArtifactId() );
  143. assertEquals( "1.0", facet.getParent().getVersion() );
  144. assertEquals( "test-artifact-module-a", facet.getArtifactId() );
  145. assertEquals( "com.example.test", facet.getGroupId() );
  146. assertNull( metadata.getCiManagement() );
  147. assertNotNull( metadata.getDescription() );
  148. checkApacheLicense( metadata );
  149. assertEquals( "1.0", metadata.getId() );
  150. assertEquals( "Test Artifact :: Module A", metadata.getName() );
  151. String path = "test-artifact/trunk/test-artifact-module-a";
  152. assertEquals( TEST_SCM_CONN_BASE + path, metadata.getScm().getConnection() );
  153. assertEquals( TEST_SCM_DEV_CONN_BASE + path, metadata.getScm().getDeveloperConnection() );
  154. assertEquals( TEST_SCM_URL_BASE + path, metadata.getScm().getUrl() );
  155. List<Dependency> dependencies = metadata.getDependencies();
  156. assertEquals( 2, dependencies.size() );
  157. assertDependency( dependencies.get( 0 ), "commons-io", "commons-io", "1.4" );
  158. assertDependency( dependencies.get( 1 ), "junit", "junit", "3.8.1", "test" );
  159. List<String> paths = new ArrayList<>();
  160. paths.add( "target/test-repository/com/example/test/test-artifact-module-a" );
  161. paths.add( "target/test-repository/com/example/test/test-artifact-parent" );
  162. paths.add( "target/test-repository/com/example/test/test-artifact-root" );
  163. deleteTestArtifactWithParent( paths );
  164. }
  165. @Test
  166. public void testGetProjectVersionMetadataWithParentNoRemoteReposConfigured()
  167. throws Exception
  168. {
  169. assertNotNull( storage );
  170. // remove configuration
  171. Configuration config = configuration.getConfiguration();
  172. RemoteRepositoryConfiguration remoteRepo = config.findRemoteRepositoryById( TEST_REMOTE_REPO_ID );
  173. config.removeRemoteRepository( remoteRepo );
  174. configuration.save( config );
  175. assertNotNull( storage );
  176. copyTestArtifactWithParent( "target/test-classes/com/example/test/test-artifact-module-a",
  177. "target/test-repository/com/example/test/test-artifact-module-a" );
  178. ProjectVersionMetadata metadata = storage.readProjectVersionMetadata(
  179. new ReadMetadataRequest( TEST_REPO_ID, "com.example.test", "test-artifact-module-a", "1.0" ) );
  180. assertEquals( "1.0", metadata.getId() );
  181. MavenProjectFacet facet = (MavenProjectFacet) metadata.getFacet( MavenProjectFacet.FACET_ID );
  182. assertNotNull( facet );
  183. assertEquals( "com.example.test", facet.getGroupId() );
  184. assertEquals( "test-artifact-module-a", facet.getArtifactId() );
  185. assertEquals( "jar", facet.getPackaging() );
  186. List<String> paths = new ArrayList<>();
  187. paths.add( "target/test-repository/com/example/test/test-artifact-module-a" );
  188. paths.add( "target/test-repository/com/example/test/test-artifact-parent" );
  189. paths.add( "target/test-repository/com/example/test/test-artifact-root" );
  190. deleteTestArtifactWithParent( paths );
  191. }
  192. @Test
  193. public void testGetProjectVersionMetadataWithParentNotInAnyRemoteRepo()
  194. throws Exception
  195. {
  196. assertNotNull( storage );
  197. copyTestArtifactWithParent( "target/test-classes/com/example/test/test-artifact-module-a",
  198. "target/test-repository/com/example/test/test-artifact-module-a" );
  199. ProjectVersionMetadata metadata = storage.readProjectVersionMetadata(
  200. new ReadMetadataRequest( TEST_REPO_ID, "com.example.test", "missing-parent", "1.1" ) );
  201. assertEquals( "1.1", metadata.getId() );
  202. MavenProjectFacet facet = (MavenProjectFacet) metadata.getFacet( MavenProjectFacet.FACET_ID );
  203. assertNotNull( facet );
  204. assertEquals( "com.example.test", facet.getGroupId() );
  205. assertEquals( "missing-parent", facet.getArtifactId() );
  206. assertEquals( "jar", facet.getPackaging() );
  207. List<String> paths = new ArrayList<>();
  208. paths.add( "target/test-repository/com/example/test/test-artifact-module-a" );
  209. paths.add( "target/test-repository/com/example/test/test-artifact-parent" );
  210. paths.add( "target/test-repository/com/example/test/test-artifact-root" );
  211. deleteTestArtifactWithParent( paths );
  212. }
  213. @Test
  214. public void testGetProjectVersionMetadataWithParentSnapshotVersion()
  215. throws Exception
  216. {
  217. copyTestArtifactWithParent( "target/test-classes/com/example/test/test-snapshot-artifact-module-a",
  218. "target/test-repository/com/example/test/test-snapshot-artifact-module-a" );
  219. copyTestArtifactWithParent( "src/test/resources/com/example/test/test-artifact-parent",
  220. "target/test-repository/com/example/test/test-artifact-parent" );
  221. copyTestArtifactWithParent( "target/test-classes/com/example/test/test-snapshot-artifact-root",
  222. "target/test-repository/com/example/test/test-snapshot-artifact-root" );
  223. ProjectVersionMetadata metadata = storage.readProjectVersionMetadata(
  224. new ReadMetadataRequest( TEST_REPO_ID, "com.example.test", "test-snapshot-artifact-module-a",
  225. "1.1-SNAPSHOT" ) );
  226. MavenProjectFacet facet = (MavenProjectFacet) metadata.getFacet( MavenProjectFacet.FACET_ID );
  227. assertEquals( "jar", facet.getPackaging() );
  228. assertEquals( "com.example.test", facet.getParent().getGroupId() );
  229. assertEquals( "test-snapshot-artifact-root", facet.getParent().getArtifactId() );
  230. assertEquals( "1.1-SNAPSHOT", facet.getParent().getVersion() );
  231. assertEquals( "test-snapshot-artifact-module-a", facet.getArtifactId() );
  232. assertEquals( "com.example.test", facet.getGroupId() );
  233. assertNull( metadata.getCiManagement() );
  234. assertNotNull( metadata.getDescription() );
  235. checkApacheLicense( metadata );
  236. assertEquals( "1.1-SNAPSHOT", metadata.getId() );
  237. assertEquals( "Test Snapshot Artifact :: Module A", metadata.getName() );
  238. String path = "test-snapshot-artifact/trunk/test-snapshot-artifact-module-a";
  239. assertEquals( TEST_SCM_CONN_BASE + path, metadata.getScm().getConnection() );
  240. assertEquals( TEST_SCM_DEV_CONN_BASE + path, metadata.getScm().getDeveloperConnection() );
  241. assertEquals( TEST_SCM_URL_BASE + path, metadata.getScm().getUrl() );
  242. List<Dependency> dependencies = metadata.getDependencies();
  243. assertEquals( 2, dependencies.size() );
  244. assertDependency( dependencies.get( 0 ), "commons-io", "commons-io", "1.4" );
  245. assertDependency( dependencies.get( 1 ), "junit", "junit", "3.8.1", "test" );
  246. List<String> paths = new ArrayList<>();
  247. paths.add( "target/test-repository/com/example/test/test-snapshot-artifact-module-a" );
  248. paths.add( "target/test-repository/com/example/test/test-snapshot-artifact-root" );
  249. deleteTestArtifactWithParent( paths );
  250. }
  251. @Test
  252. public void testGetProjectVersionMetadataWithParentSnapshotVersionAndSnapNotAllowed()
  253. throws Exception
  254. {
  255. testRepo.setSnapshots( false );
  256. configuration.save( c );
  257. repositoryRegistry.reload();
  258. assertFalse(repositoryRegistry.getManagedRepository(testRepo.getId()).getActiveReleaseSchemes().contains(ReleaseScheme.SNAPSHOT));
  259. assertFalse( c.getManagedRepositories().get( 0 ).isSnapshots() );
  260. copyTestArtifactWithParent( "target/test-classes/com/example/test/test-snapshot-artifact-module-a",
  261. "target/test-repository/com/example/test/test-snapshot-artifact-module-a" );
  262. try
  263. {
  264. ProjectVersionMetadata metadata = storage.readProjectVersionMetadata(
  265. new ReadMetadataRequest( TEST_REPO_ID, "com.example.test", "test-snapshot-artifact-module-a",
  266. "1.1-SNAPSHOT" ) );
  267. fail( "Should not be found" );
  268. }
  269. catch ( RepositoryStorageRuntimeException e )
  270. {
  271. }
  272. List<String> paths = new ArrayList<>();
  273. paths.add( "target/test-repository/com/example/test/test-snapshot-artifact-module-a" );
  274. paths.add( "target/test-repository/com/example/test/test-snapshot-artifact-root" );
  275. deleteTestArtifactWithParent( paths );
  276. }
  277. // Tests for MRM-1411 - END
  278. private void assertDependency( Dependency dependency, String groupId, String artifactId, String version )
  279. {
  280. assertDependency( dependency, groupId, artifactId, version, "compile" );
  281. }
  282. private void assertDependency( Dependency dependency, String groupId, String artifactId, String version,
  283. String scope )
  284. {
  285. assertEquals( artifactId, dependency.getArtifactId() );
  286. assertEquals( "jar", dependency.getType() );
  287. assertEquals( version, dependency.getVersion() );
  288. assertEquals( groupId, dependency.getNamespace() );
  289. assertEquals( scope, dependency.getScope() );
  290. assertNull( dependency.getClassifier() );
  291. assertNull( dependency.getSystemPath() );
  292. }
  293. private void assertArtifact( ArtifactMetadata artifact, String id, int size, String sha1, String md5 )
  294. {
  295. assertEquals( id, artifact.getId() );
  296. assertEquals( md5, artifact.getMd5() );
  297. assertEquals( sha1, artifact.getSha1() );
  298. assertEquals( size, artifact.getSize() );
  299. assertEquals( "org.codehaus.plexus", artifact.getNamespace() );
  300. assertEquals( "plexus-spring", artifact.getProject() );
  301. assertEquals( "1.2", artifact.getVersion() );
  302. assertEquals( TEST_REPO_ID, artifact.getRepositoryId() );
  303. }
  304. private void assertMailingList( MailingList mailingList, String name, String archive, String post, String subscribe,
  305. String unsubscribe, List<String> otherArchives, boolean allowPost )
  306. {
  307. assertEquals( archive, mailingList.getMainArchiveUrl() );
  308. if ( allowPost )
  309. {
  310. assertEquals( post, mailingList.getPostAddress() );
  311. }
  312. else
  313. {
  314. assertNull( mailingList.getPostAddress() );
  315. }
  316. assertEquals( subscribe, mailingList.getSubscribeAddress() );
  317. assertEquals( unsubscribe, mailingList.getUnsubscribeAddress() );
  318. assertEquals( name, mailingList.getName() );
  319. assertEquals( otherArchives, mailingList.getOtherArchives() );
  320. }
  321. private void assertMailingList( String prefix, MailingList mailingList, String name, boolean allowPost,
  322. String nabbleUrl )
  323. {
  324. List<String> otherArchives = new ArrayList<>();
  325. otherArchives.add( "http://www.mail-archive.com/" + prefix + "@archiva.apache.org" );
  326. if ( nabbleUrl != null )
  327. {
  328. otherArchives.add( nabbleUrl );
  329. }
  330. otherArchives.add( "http://markmail.org/list/org.apache.archiva." + prefix );
  331. assertMailingList( mailingList, name, "http://mail-archives.apache.org/mod_mbox/archiva-" + prefix + "/",
  332. prefix + "@archiva.apache.org", prefix + "-subscribe@archiva.apache.org",
  333. prefix + "-unsubscribe@archiva.apache.org", otherArchives, allowPost );
  334. }
  335. private void checkApacheLicense( ProjectVersionMetadata metadata )
  336. {
  337. assertEquals( Arrays.asList( new License( "The Apache Software License, Version 2.0",
  338. "http://www.apache.org/licenses/LICENSE-2.0.txt" ) ),
  339. metadata.getLicenses() );
  340. }
  341. private void checkOrganizationApache( ProjectVersionMetadata metadata )
  342. {
  343. assertEquals( "The Apache Software Foundation", metadata.getOrganization().getName() );
  344. assertEquals( "http://www.apache.org/", metadata.getOrganization().getUrl() );
  345. }
  346. private void deleteTestArtifactWithParent( List<String> pathsToBeDeleted )
  347. throws IOException
  348. {
  349. for ( String path : pathsToBeDeleted )
  350. {
  351. Path dir = Paths.get( org.apache.archiva.common.utils.FileUtils.getBasedir(), path );
  352. org.apache.archiva.common.utils.FileUtils.deleteDirectory( dir );
  353. assertFalse(Files.exists( dir) );
  354. }
  355. Path dest = Paths.get( org.apache.archiva.common.utils.FileUtils.getBasedir(), "target/test-repository/com/example/test/test-artifact-module-a" );
  356. Path parentPom =
  357. Paths.get( org.apache.archiva.common.utils.FileUtils.getBasedir(), "target/test-repository/com/example/test/test-artifact-parent" );
  358. Path rootPom = Paths.get( org.apache.archiva.common.utils.FileUtils.getBasedir(), "target/test-repository/com/example/test/test-artifact-root" );
  359. org.apache.archiva.common.utils.FileUtils.deleteDirectory( dest );
  360. org.apache.archiva.common.utils.FileUtils.deleteDirectory( parentPom );
  361. org.apache.archiva.common.utils.FileUtils.deleteDirectory( rootPom );
  362. assertFalse( Files.exists(dest) );
  363. assertFalse( Files.exists(parentPom) );
  364. assertFalse( Files.exists(rootPom) );
  365. }
  366. private Path copyTestArtifactWithParent( String srcPath, String destPath )
  367. throws IOException
  368. {
  369. Path src = Paths.get( org.apache.archiva.common.utils.FileUtils.getBasedir(), srcPath );
  370. Path dest = Paths.get( org.apache.archiva.common.utils.FileUtils.getBasedir(), destPath );
  371. FileUtils.copyDirectory( src.toFile(), dest.toFile() );
  372. assertTrue( Files.exists(dest) );
  373. return dest;
  374. }
  375. }