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.

MavenRepositoryRequestInfoTest.java 22KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561
  1. package org.apache.archiva.repository.content.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.ArchivaConfiguration;
  22. import org.apache.archiva.configuration.FileType;
  23. import org.apache.archiva.configuration.FileTypes;
  24. import org.apache.archiva.metadata.repository.storage.maven2.ArtifactMappingProvider;
  25. import org.apache.archiva.model.ArtifactReference;
  26. import org.apache.archiva.repository.LayoutException;
  27. import org.apache.archiva.repository.ManagedRepositoryContent;
  28. import org.apache.archiva.repository.RepositoryContentProvider;
  29. import org.apache.archiva.repository.maven2.MavenManagedRepository;
  30. import org.apache.archiva.test.utils.ArchivaSpringJUnit4ClassRunner;
  31. import org.apache.commons.lang.StringUtils;
  32. import org.junit.Before;
  33. import org.junit.Test;
  34. import org.junit.runner.RunWith;
  35. import org.springframework.context.ApplicationContext;
  36. import org.springframework.test.context.ContextConfiguration;
  37. import javax.inject.Inject;
  38. import javax.inject.Named;
  39. import java.nio.file.Path;
  40. import java.nio.file.Paths;
  41. import java.util.List;
  42. import static org.junit.Assert.*;
  43. /**
  44. * RepositoryRequestTest
  45. */
  46. @RunWith( ArchivaSpringJUnit4ClassRunner.class )
  47. @ContextConfiguration( { "classpath*:/META-INF/spring-context.xml",
  48. "classpath:/spring-context-repo-request-test.xml" } )
  49. public class MavenRepositoryRequestInfoTest
  50. {
  51. @Inject
  52. protected ApplicationContext applicationContext;
  53. @Inject
  54. FileTypes fileTypes;
  55. @Inject
  56. @Named( "archivaConfiguration#repo-request-test" )
  57. private ArchivaConfiguration archivaConfiguration;
  58. @Inject
  59. List<? extends ArtifactMappingProvider> artifactMappingProviders;
  60. private MavenRepositoryRequestInfo repoRequest;
  61. protected MavenManagedRepository createRepository( String id, String name, Path location )
  62. {
  63. MavenManagedRepository repo = new MavenManagedRepository( id, name, location.getParent().toAbsolutePath());
  64. repo.setLocation( location.toAbsolutePath().toUri() );
  65. return repo;
  66. }
  67. @Before
  68. public void setUp()
  69. throws Exception
  70. {
  71. Path repoDir = Paths.get( "src/test/repositories/default-repository" );
  72. MavenManagedRepository repository = createRepository( "testRepo", "Unit Test Repo", repoDir );
  73. FileType fileType = archivaConfiguration.getConfiguration().getRepositoryScanning().getFileTypes().get( 0 );
  74. fileType.addPattern( "**/*.xml" );
  75. assertEquals( FileTypes.ARTIFACTS, fileType.getId() );
  76. fileTypes.afterConfigurationChange( null, "fileType", null );
  77. ManagedDefaultRepositoryContent repoContent = new ManagedDefaultRepositoryContent(artifactMappingProviders, fileTypes);
  78. //repoContent = (ManagedRepositoryContent) lookup( ManagedRepositoryContent.class, "default" );
  79. repoContent.setRepository( repository );
  80. repository.setContent(repoContent);
  81. repoRequest = new MavenRepositoryRequestInfo(repository);
  82. }
  83. @Test
  84. public void testInvalidRequestEmptyPath()
  85. {
  86. assertInvalidRequest( "" );
  87. }
  88. @Test
  89. public void testInvalidRequestSlashOnly()
  90. {
  91. assertInvalidRequest( "//" );
  92. }
  93. @Test
  94. public void testInvalidRequestNoArtifactId()
  95. {
  96. assertInvalidRequest( "groupId/jars/-1.0.jar" );
  97. }
  98. @Test
  99. public void testInvalidRequestTooShort()
  100. {
  101. assertInvalidRequest( "org.apache.maven.test/artifactId-2.0.jar" );
  102. }
  103. @Test
  104. public void testInvalidDefaultRequestBadLocation()
  105. {
  106. assertInvalidRequest( "invalid/invalid/1.0-20050611.123456-1/invalid-1.0-20050611.123456-1.jar" );
  107. }
  108. @Test( expected = LayoutException.class )
  109. public void testValidLegacyGanymed()
  110. throws Exception
  111. {
  112. assertValid( "ch.ethz.ganymed/jars/ganymed-ssh2-build210.jar", "ch.ethz.ganymed", "ganymed-ssh2", "build210",
  113. null, "jar" );
  114. }
  115. @Test
  116. public void testValidDefaultGanymed()
  117. throws Exception
  118. {
  119. assertValid( "ch/ethz/ganymed/ganymed-ssh2/build210/ganymed-ssh2-build210.jar", "ch.ethz.ganymed",
  120. "ganymed-ssh2", "build210", null, "jar" );
  121. }
  122. @Test( expected = LayoutException.class )
  123. public void testValidLegacyJavaxComm()
  124. throws Exception
  125. {
  126. assertValid( "javax/jars/comm-3.0-u1.jar", "javax", "comm", "3.0-u1", null, "jar" );
  127. }
  128. @Test
  129. public void testValidDefaultJavaxComm()
  130. throws Exception
  131. {
  132. assertValid( "javax/comm/3.0-u1/comm-3.0-u1.jar", "javax", "comm", "3.0-u1", null, "jar" );
  133. }
  134. @Test( expected = LayoutException.class )
  135. public void testValidLegacyJavaxPersistence()
  136. throws Exception
  137. {
  138. assertValid( "javax.persistence/jars/ejb-3.0-public_review.jar", "javax.persistence", "ejb",
  139. "3.0-public_review", null, "jar" );
  140. }
  141. @Test
  142. public void testValidDefaultJavaxPersistence()
  143. throws Exception
  144. {
  145. assertValid( "javax/persistence/ejb/3.0-public_review/ejb-3.0-public_review.jar", "javax.persistence", "ejb",
  146. "3.0-public_review", null, "jar" );
  147. }
  148. @Test( expected = LayoutException.class )
  149. public void testValidLegacyMavenTestPlugin()
  150. throws Exception
  151. {
  152. assertValid( "maven/jars/maven-test-plugin-1.8.2.jar", "maven", "maven-test-plugin", "1.8.2", null, "jar" );
  153. }
  154. @Test
  155. public void testValidDefaultMavenTestPlugin()
  156. throws Exception
  157. {
  158. assertValid( "maven/maven-test-plugin/1.8.2/maven-test-plugin-1.8.2.pom", "maven", "maven-test-plugin", "1.8.2",
  159. null, "pom" );
  160. }
  161. @Test( expected = LayoutException.class )
  162. public void testValidLegacyCommonsLangJavadoc()
  163. throws Exception
  164. {
  165. assertValid( "commons-lang/javadoc.jars/commons-lang-2.1-javadoc.jar", "commons-lang", "commons-lang", "2.1",
  166. "javadoc", "javadoc" );
  167. }
  168. @Test
  169. public void testValidDefaultCommonsLangJavadoc()
  170. throws Exception
  171. {
  172. assertValid( "commons-lang/commons-lang/2.1/commons-lang-2.1-javadoc.jar", "commons-lang", "commons-lang",
  173. "2.1", "javadoc", "javadoc" );
  174. }
  175. @Test( expected = LayoutException.class )
  176. public void testValidLegacyDerbyPom()
  177. throws Exception
  178. {
  179. assertValid( "org.apache.derby/poms/derby-10.2.2.0.pom", "org.apache.derby", "derby", "10.2.2.0", null, "pom" );
  180. // Starting slash should not prevent detection.
  181. assertValid( "/org.apache.derby/poms/derby-10.2.2.0.pom", "org.apache.derby", "derby", "10.2.2.0", null,
  182. "pom" );
  183. }
  184. @Test
  185. public void testValidDefaultDerbyPom()
  186. throws Exception
  187. {
  188. assertValid( "org/apache/derby/derby/10.2.2.0/derby-10.2.2.0.pom", "org.apache.derby", "derby", "10.2.2.0",
  189. null, "pom" );
  190. }
  191. @Test( expected = LayoutException.class )
  192. public void testValidLegacyGeronimoEjbSpec()
  193. throws Exception
  194. {
  195. assertValid( "org.apache.geronimo.specs/jars/geronimo-ejb_2.1_spec-1.0.1.jar", "org.apache.geronimo.specs",
  196. "geronimo-ejb_2.1_spec", "1.0.1", null, "jar" );
  197. }
  198. @Test
  199. public void testValidDefaultGeronimoEjbSpec()
  200. throws Exception
  201. {
  202. assertValid( "org/apache/geronimo/specs/geronimo-ejb_2.1_spec/1.0.1/geronimo-ejb_2.1_spec-1.0.1.jar",
  203. "org.apache.geronimo.specs", "geronimo-ejb_2.1_spec", "1.0.1", null, "jar" );
  204. }
  205. @Test( expected = LayoutException.class )
  206. public void testValidLegacyLdapSnapshot()
  207. throws Exception
  208. {
  209. assertValid( "directory-clients/poms/ldap-clients-0.9.1-SNAPSHOT.pom", "directory-clients", "ldap-clients",
  210. "0.9.1-SNAPSHOT", null, "pom" );
  211. }
  212. @Test
  213. public void testValidDefaultLdapSnapshot()
  214. throws Exception
  215. {
  216. assertValid( "directory-clients/ldap-clients/0.9.1-SNAPSHOT/ldap-clients-0.9.1-SNAPSHOT.pom",
  217. "directory-clients", "ldap-clients", "0.9.1-SNAPSHOT", null, "pom" );
  218. }
  219. @Test( expected = LayoutException.class )
  220. public void testValidLegacyTestArchSnapshot()
  221. throws Exception
  222. {
  223. assertValid( "test.maven-arch/poms/test-arch-2.0.3-SNAPSHOT.pom", "test.maven-arch", "test-arch",
  224. "2.0.3-SNAPSHOT", null, "pom" );
  225. }
  226. @Test
  227. public void testValidDefaultTestArchSnapshot()
  228. throws Exception
  229. {
  230. assertValid( "test/maven-arch/test-arch/2.0.3-SNAPSHOT/test-arch-2.0.3-SNAPSHOT.pom", "test.maven-arch",
  231. "test-arch", "2.0.3-SNAPSHOT", null, "pom" );
  232. }
  233. @Test( expected = LayoutException.class )
  234. public void testValidLegacyOddDottedArtifactId()
  235. throws Exception
  236. {
  237. assertValid( "com.company.department/poms/com.company.department.project-0.2.pom", "com.company.department",
  238. "com.company.department.project", "0.2", null, "pom" );
  239. }
  240. @Test
  241. public void testValidDefaultOddDottedArtifactId()
  242. throws Exception
  243. {
  244. assertValid( "com/company/department/com.company.department.project/0.2/com.company.department.project-0.2.pom",
  245. "com.company.department", "com.company.department.project", "0.2", null, "pom" );
  246. }
  247. @Test( expected = LayoutException.class )
  248. public void testValidLegacyTimestampedSnapshot()
  249. throws Exception
  250. {
  251. assertValid( "org.apache.archiva.test/jars/redonkulous-3.1-beta-1-20050831.101112-42.jar",
  252. "org.apache.archiva.test", "redonkulous", "3.1-beta-1-20050831.101112-42", null, "jar" );
  253. }
  254. @Test
  255. public void testValidDefaultTimestampedSnapshot()
  256. throws Exception
  257. {
  258. assertValid(
  259. "org/apache/archiva/test/redonkulous/3.1-beta-1-SNAPSHOT/redonkulous-3.1-beta-1-20050831.101112-42.jar",
  260. "org.apache.archiva.test", "redonkulous", "3.1-beta-1-20050831.101112-42", null, "jar" );
  261. }
  262. @Test
  263. public void testIsSupportFile()
  264. {
  265. assertTrue( repoRequest.isSupportFile( "org/apache/derby/derby/10.2.2.0/derby-10.2.2.0-bin.tar.gz.sha1" ) );
  266. assertTrue( repoRequest.isSupportFile( "org/apache/derby/derby/10.2.2.0/derby-10.2.2.0-bin.tar.gz.md5" ) );
  267. assertTrue( repoRequest.isSupportFile( "org/apache/derby/derby/10.2.2.0/derby-10.2.2.0-bin.tar.gz.asc" ) );
  268. assertTrue( repoRequest.isSupportFile( "org/apache/derby/derby/10.2.2.0/derby-10.2.2.0-bin.tar.gz.pgp" ) );
  269. assertTrue( repoRequest.isSupportFile( "org/apache/derby/derby/10.2.2.0/maven-metadata.xml.sha1" ) );
  270. assertTrue( repoRequest.isSupportFile( "org/apache/derby/derby/10.2.2.0/maven-metadata.xml.md5" ) );
  271. assertFalse( repoRequest.isSupportFile( "test.maven-arch/poms/test-arch-2.0.3-SNAPSHOT.pom" ) );
  272. assertFalse(
  273. repoRequest.isSupportFile( "test/maven-arch/test-arch/2.0.3-SNAPSHOT/test-arch-2.0.3-SNAPSHOT.jar" ) );
  274. assertFalse( repoRequest.isSupportFile( "org/apache/archiva/archiva-api/1.0/archiva-api-1.0.xml.zip" ) );
  275. assertFalse( repoRequest.isSupportFile( "org/apache/derby/derby/10.2.2.0/derby-10.2.2.0-bin.tar.gz" ) );
  276. assertFalse( repoRequest.isSupportFile( "org/apache/derby/derby/10.2.2.0/maven-metadata.xml" ) );
  277. assertFalse( repoRequest.isSupportFile( "org/apache/derby/derby/maven-metadata.xml" ) );
  278. }
  279. @Test
  280. public void testIsMetadata()
  281. {
  282. assertTrue( repoRequest.isMetadata( "org/apache/derby/derby/10.2.2.0/maven-metadata.xml" ) );
  283. assertTrue( repoRequest.isMetadata( "org/apache/derby/derby/maven-metadata.xml" ) );
  284. assertFalse( repoRequest.isMetadata( "test.maven-arch/poms/test-arch-2.0.3-SNAPSHOT.pom" ) );
  285. assertFalse(
  286. repoRequest.isMetadata( "test/maven-arch/test-arch/2.0.3-SNAPSHOT/test-arch-2.0.3-SNAPSHOT.jar" ) );
  287. assertFalse( repoRequest.isMetadata( "org/apache/archiva/archiva-api/1.0/archiva-api-1.0.xml.zip" ) );
  288. assertFalse( repoRequest.isMetadata( "org/apache/derby/derby/10.2.2.0/derby-10.2.2.0-bin.tar.gz" ) );
  289. assertFalse( repoRequest.isMetadata( "org/apache/derby/derby/10.2.2.0/derby-10.2.2.0-bin.tar.gz.pgp" ) );
  290. assertFalse( repoRequest.isMetadata( "org/apache/derby/derby/10.2.2.0/maven-metadata.xml.sha1" ) );
  291. }
  292. @Test
  293. public void testIsMetadataSupportFile()
  294. {
  295. assertFalse( repoRequest.isMetadataSupportFile( "org/apache/derby/derby/10.2.2.0/maven-metadata.xml" ) );
  296. assertFalse( repoRequest.isMetadataSupportFile( "org/apache/derby/derby/maven-metadata.xml" ) );
  297. assertTrue( repoRequest.isMetadataSupportFile( "org/apache/derby/derby/maven-metadata.xml.sha1" ) );
  298. assertTrue( repoRequest.isMetadataSupportFile( "org/apache/derby/derby/maven-metadata.xml.md5" ) );
  299. assertFalse( repoRequest.isMetadataSupportFile( "test.maven-arch/poms/test-arch-2.0.3-SNAPSHOT.pom" ) );
  300. assertFalse( repoRequest.isMetadataSupportFile(
  301. "test/maven-arch/test-arch/2.0.3-SNAPSHOT/test-arch-2.0.3-SNAPSHOT.jar" ) );
  302. assertFalse(
  303. repoRequest.isMetadataSupportFile( "org/apache/archiva/archiva-api/1.0/archiva-api-1.0.xml.zip" ) );
  304. assertFalse( repoRequest.isMetadataSupportFile( "org/apache/derby/derby/10.2.2.0/derby-10.2.2.0-bin.tar.gz" ) );
  305. assertFalse(
  306. repoRequest.isMetadataSupportFile( "org/apache/derby/derby/10.2.2.0/derby-10.2.2.0-bin.tar.gz.pgp" ) );
  307. assertTrue( repoRequest.isMetadataSupportFile( "org/apache/derby/derby/10.2.2.0/maven-metadata.xml.sha1" ) );
  308. assertTrue( repoRequest.isMetadataSupportFile( "org/apache/derby/derby/10.2.2.0/maven-metadata.xml.md5" ) );
  309. }
  310. @Test
  311. public void testIsDefault()
  312. {
  313. assertNotEquals( "default", repoRequest.getLayout( "test.maven-arch/poms/test-arch-2.0.3-SNAPSHOT.pom" ) );
  314. assertNotEquals("default", repoRequest.getLayout( "directory-clients/poms/ldap-clients-0.9.1-SNAPSHOT.pom" ) );
  315. assertNotEquals("default", repoRequest.getLayout( "commons-lang/jars/commons-lang-2.1-javadoc.jar" ) );
  316. assertEquals("default", repoRequest.getLayout( "test/maven-arch/test-arch/2.0.3-SNAPSHOT/test-arch-2.0.3-SNAPSHOT.jar" ) );
  317. assertEquals("default", repoRequest.getLayout( "org/apache/archiva/archiva-api/1.0/archiva-api-1.0.xml.zip" ) );
  318. assertEquals("default", repoRequest.getLayout( "org/apache/derby/derby/10.2.2.0/derby-10.2.2.0-bin.tar.gz" ) );
  319. assertEquals("default", repoRequest.getLayout( "org/apache/derby/derby/10.2.2.0/derby-10.2.2.0-bin.tar.gz.pgp" ) );
  320. assertEquals("default", repoRequest.getLayout( "org/apache/derby/derby/10.2.2.0/maven-metadata.xml.sha1" ) );
  321. assertEquals("default", repoRequest.getLayout( "eclipse/jdtcore/maven-metadata.xml" ) );
  322. assertEquals("default", repoRequest.getLayout( "eclipse/jdtcore/maven-metadata.xml.sha1" ) );
  323. assertEquals("default", repoRequest.getLayout( "eclipse/jdtcore/maven-metadata.xml.md5" ) );
  324. assertNotEquals("default", repoRequest.getLayout( null ) );
  325. assertNotEquals("default", repoRequest.getLayout( "" ) );
  326. assertNotEquals("default", repoRequest.getLayout( "foo" ) );
  327. assertNotEquals("default", repoRequest.getLayout( "some.short/path" ) );
  328. }
  329. @Test
  330. public void testIsLegacy()
  331. {
  332. assertEquals("legacy", repoRequest.getLayout( "test.maven-arch/poms/test-arch-2.0.3-SNAPSHOT.pom" ) );
  333. assertEquals("legacy", repoRequest.getLayout( "directory-clients/poms/ldap-clients-0.9.1-SNAPSHOT.pom" ) );
  334. assertEquals("legacy", repoRequest.getLayout( "commons-lang/jars/commons-lang-2.1-javadoc.jar" ) );
  335. assertNotEquals("legacy", repoRequest.getLayout( "test/maven-arch/test-arch/2.0.3-SNAPSHOT/test-arch-2.0.3-SNAPSHOT.jar" ) );
  336. assertNotEquals("legacy", repoRequest.getLayout( "org/apache/archiva/archiva-api/1.0/archiva-api-1.0.xml.zip" ) );
  337. assertNotEquals("legacy", repoRequest.getLayout( "org/apache/derby/derby/10.2.2.0/derby-10.2.2.0-bin.tar.gz" ) );
  338. assertNotEquals("legacy", repoRequest.getLayout( "org/apache/derby/derby/10.2.2.0/derby-10.2.2.0-bin.tar.gz.pgp" ) );
  339. assertNotEquals("legacy", repoRequest.getLayout( "org/apache/derby/derby/10.2.2.0/maven-metadata.xml.sha1" ) );
  340. assertNotEquals("legacy", repoRequest.getLayout( null ) );
  341. assertNotEquals("legacy", repoRequest.getLayout( "" ) );
  342. assertNotEquals("legacy", repoRequest.getLayout( "some.short/path" ) );
  343. }
  344. private ManagedRepositoryContent createManagedRepo( String layout )
  345. throws Exception
  346. {
  347. Path repoRoot = Paths.get( FileUtils.getBasedir() + "/target/test-repo" );
  348. return createManagedRepositoryContent( "test-internal", "Internal Test Repo", repoRoot, layout );
  349. }
  350. /**
  351. * [MRM-481] Artifact requests with a .xml.zip extension fail with a 404 Error
  352. */
  353. @Test
  354. public void testToNativePathArtifactDefaultToDefaultDualExtension()
  355. throws Exception
  356. {
  357. ManagedRepositoryContent repository = createManagedRepo( "default" );
  358. // Test (artifact) default to default - dual extension
  359. assertEquals( "org/project/example-presentation/3.2/example-presentation-3.2.xml.zip",
  360. repoRequest.toNativePath( "org/project/example-presentation/3.2/example-presentation-3.2.xml.zip") );
  361. }
  362. @Test
  363. public void testToNativePathMetadataDefaultToDefault()
  364. throws Exception
  365. {
  366. ManagedRepositoryContent repository = createManagedRepo( "default" );
  367. // Test (metadata) default to default
  368. assertEquals( "org/apache/derby/derby/10.2.2.0/maven-metadata.xml.sha1",
  369. repoRequest.toNativePath( "org/apache/derby/derby/10.2.2.0/maven-metadata.xml.sha1") );
  370. }
  371. @Test
  372. public void testNativePathBadRequestTooShort()
  373. throws Exception
  374. {
  375. ManagedRepositoryContent repository = createManagedRepo( "default" );
  376. // Test bad request path (too short)
  377. try
  378. {
  379. repoRequest.toNativePath( "org.apache.derby/license.txt");
  380. fail( "Should have thrown an exception about a too short path." );
  381. }
  382. catch ( LayoutException e )
  383. {
  384. // expected path.
  385. }
  386. }
  387. @Test
  388. public void testNativePathBadRequestBlank()
  389. throws Exception
  390. {
  391. ManagedRepositoryContent repository = createManagedRepo( "default" );
  392. // Test bad request path (too short)
  393. try
  394. {
  395. repoRequest.toNativePath( "");
  396. fail( "Should have thrown an exception about an blank request." );
  397. }
  398. catch ( LayoutException e )
  399. {
  400. // expected path.
  401. }
  402. }
  403. @Test
  404. public void testNativePathBadRequestNull()
  405. throws Exception
  406. {
  407. ManagedRepositoryContent repository = createManagedRepo( "default" );
  408. // Test bad request path (too short)
  409. try
  410. {
  411. repoRequest.toNativePath( null);
  412. fail( "Should have thrown an exception about an null request." );
  413. }
  414. catch ( LayoutException e )
  415. {
  416. // expected path.
  417. }
  418. }
  419. @Test
  420. public void testNativePathBadRequestUnknownType()
  421. throws Exception
  422. {
  423. ManagedRepositoryContent repository = createManagedRepo( "default" );
  424. // Test bad request path (too short)
  425. try
  426. {
  427. repoRequest.toNativePath( "org/apache/derby/derby/10.2.2.0/license.txt");
  428. fail( "Should have thrown an exception about an invalid type." );
  429. }
  430. catch ( LayoutException e )
  431. {
  432. // expected path.
  433. }
  434. }
  435. private void assertValid( String path, String groupId, String artifactId, String version, String classifier,
  436. String type )
  437. throws Exception
  438. {
  439. String expectedId =
  440. "ArtifactReference - " + groupId + ":" + artifactId + ":" + version + ":" + ( classifier != null ?
  441. classifier + ":" : "" ) + type;
  442. ArtifactReference reference = repoRequest.toArtifactReference( path );
  443. assertNotNull( expectedId + " - Should not be null.", reference );
  444. assertEquals( expectedId + " - Group ID", groupId, reference.getGroupId() );
  445. assertEquals( expectedId + " - Artifact ID", artifactId, reference.getArtifactId() );
  446. if ( StringUtils.isNotBlank( classifier ) )
  447. {
  448. assertEquals( expectedId + " - Classifier", classifier, reference.getClassifier() );
  449. }
  450. assertEquals( expectedId + " - Version ID", version, reference.getVersion() );
  451. assertEquals( expectedId + " - Type", type, reference.getType() );
  452. }
  453. private void assertInvalidRequest( String path )
  454. {
  455. try
  456. {
  457. repoRequest.toArtifactReference( path );
  458. fail( "Expected a LayoutException on an invalid path [" + path + "]" );
  459. }
  460. catch ( LayoutException e )
  461. {
  462. /* expected path */
  463. }
  464. }
  465. protected ManagedRepositoryContent createManagedRepositoryContent( String id, String name, Path location,
  466. String layout )
  467. throws Exception
  468. {
  469. MavenManagedRepository repo = new MavenManagedRepository( id, name, archivaConfiguration.getRepositoryBaseDir());
  470. repo.setLocation( location.toAbsolutePath().toUri() );
  471. repo.setLayout( layout );
  472. RepositoryContentProvider provider = applicationContext.getBean( "repositoryContentProvider#maven", RepositoryContentProvider.class );
  473. ManagedRepositoryContent repoContent =
  474. provider.createManagedContent( repo );
  475. return repoContent;
  476. }
  477. }