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 20KB

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