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.

DefaultArtifactDiscovererTest.java 8.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. package org.apache.maven.repository.discovery;
  2. /*
  3. * Copyright 2001-2005 The Apache Software Foundation.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. import org.apache.maven.artifact.Artifact;
  18. import org.apache.maven.artifact.factory.ArtifactFactory;
  19. import org.codehaus.plexus.PlexusTestCase;
  20. import java.io.File;
  21. import java.util.Iterator;
  22. import java.util.List;
  23. /**
  24. * Test the default artifact discoverer.
  25. *
  26. * @author <a href="mailto:brett@apache.org">Brett Porter</a>
  27. * @version $Id$
  28. * @todo other tests for kickouts to do here, along the lines of wrong artifactId, parse classifiers, locate poms
  29. */
  30. public class DefaultArtifactDiscovererTest
  31. extends PlexusTestCase
  32. {
  33. private ArtifactDiscoverer discoverer;
  34. private ArtifactFactory factory;
  35. private File repositoryLocation;
  36. protected void setUp()
  37. throws Exception
  38. {
  39. super.setUp();
  40. discoverer = (ArtifactDiscoverer) lookup( ArtifactDiscoverer.ROLE, "default" );
  41. factory = (ArtifactFactory) lookup( ArtifactFactory.ROLE );
  42. repositoryLocation = getTestFile( "src/test/repository" );
  43. }
  44. public void testDefaultExcludes()
  45. {
  46. List artifacts = discoverer.discoverArtifacts( repositoryLocation, null, false );
  47. assertNotNull( "Check artifacts not null", artifacts );
  48. boolean found = false;
  49. for ( Iterator i = discoverer.getExcludedPathsIterator(); i.hasNext() && !found; )
  50. {
  51. String path = (String) i.next();
  52. found = path.indexOf( ".svn" ) >= 0;
  53. }
  54. assertTrue( "Check exclusion was found", found );
  55. for ( Iterator i = artifacts.iterator(); i.hasNext(); )
  56. {
  57. Artifact a = (Artifact) i.next();
  58. assertFalse( "Check not .svn", a.getFile().getPath().indexOf( ".svn" ) >= 0 );
  59. }
  60. }
  61. public void testStandardExcludes()
  62. {
  63. List artifacts = discoverer.discoverArtifacts( repositoryLocation, null, false );
  64. assertNotNull( "Check artifacts not null", artifacts );
  65. boolean found = false;
  66. for ( Iterator i = discoverer.getExcludedPathsIterator(); i.hasNext() && !found; )
  67. {
  68. String path = (String) i.next();
  69. found = path.equals( "KEYS" );
  70. }
  71. assertTrue( "Check exclusion was found", found );
  72. for ( Iterator i = artifacts.iterator(); i.hasNext(); )
  73. {
  74. Artifact a = (Artifact) i.next();
  75. assertFalse( "Check not KEYS", a.getFile().getName().equals( "KEYS" ) );
  76. }
  77. }
  78. public void testBlacklistedExclude()
  79. {
  80. List artifacts = discoverer.discoverArtifacts( repositoryLocation, "javax/**", false );
  81. assertNotNull( "Check artifacts not null", artifacts );
  82. boolean found = false;
  83. for ( Iterator i = discoverer.getExcludedPathsIterator(); i.hasNext() && !found; )
  84. {
  85. String path = (String) i.next();
  86. found = path.replace( '\\', '/' ).equals( "javax/sql/jdbc/2.0/jdbc-2.0.jar" );
  87. }
  88. assertTrue( "Check exclusion was found", found );
  89. assertFalse( "Check jdbc not included", artifacts.contains( createArtifact( "javax.sql", "jdbc", "2.0" ) ) );
  90. }
  91. public void testKickoutWithShortPath()
  92. {
  93. List artifacts = discoverer.discoverArtifacts( repositoryLocation, null, false );
  94. assertNotNull( "Check artifacts not null", artifacts );
  95. boolean found = false;
  96. for ( Iterator i = discoverer.getKickedOutPathsIterator(); i.hasNext() && !found; )
  97. {
  98. String path = (String) i.next();
  99. found = path.replace( '\\', '/' ).equals( "invalid/invalid-1.0.jar" );
  100. }
  101. assertTrue( "Check kickout was found", found );
  102. for ( Iterator i = artifacts.iterator(); i.hasNext(); )
  103. {
  104. Artifact a = (Artifact) i.next();
  105. assertFalse( "Check not invalid-1.0.jar", a.getFile().getName().equals( "invalid-1.0.jar" ) );
  106. }
  107. }
  108. public void testKickoutWithWrongArtifactId()
  109. {
  110. List artifacts = discoverer.discoverArtifacts( repositoryLocation, null, false );
  111. assertNotNull( "Check artifacts not null", artifacts );
  112. boolean found = false;
  113. for ( Iterator i = discoverer.getKickedOutPathsIterator(); i.hasNext() && !found; )
  114. {
  115. String path = (String) i.next();
  116. found = path.replace( '\\', '/' ).equals(
  117. "org/apache/maven/test/1.0-SNAPSHOT/wrong-artifactId-1.0-20050611.112233-1.jar" );
  118. }
  119. assertTrue( "Check kickout was found", found );
  120. for ( Iterator i = artifacts.iterator(); i.hasNext(); )
  121. {
  122. Artifact a = (Artifact) i.next();
  123. assertFalse( "Check not wrong jar",
  124. a.getFile().getName().equals( "wrong-artifactId-1.0-20050611.112233-1.jar" ) );
  125. }
  126. }
  127. public void testKickoutWithWrongVersion()
  128. {
  129. List artifacts = discoverer.discoverArtifacts( repositoryLocation, null, false );
  130. assertNotNull( "Check artifacts not null", artifacts );
  131. boolean found = false;
  132. for ( Iterator i = discoverer.getKickedOutPathsIterator(); i.hasNext() && !found; )
  133. {
  134. String path = (String) i.next();
  135. found = path.replace( '\\', '/' ).equals( "invalid/invalid/1.0/invalid-2.0.jar" );
  136. }
  137. assertTrue( "Check kickout was found", found );
  138. for ( Iterator i = artifacts.iterator(); i.hasNext(); )
  139. {
  140. Artifact a = (Artifact) i.next();
  141. assertFalse( "Check not 'invalid-2.0.jar'", a.getFile().getName().equals( "invalid-2.0.jar" ) );
  142. }
  143. }
  144. public void testKickoutWithWrongSnapshotVersion()
  145. {
  146. List artifacts = discoverer.discoverArtifacts( repositoryLocation, null, false );
  147. assertNotNull( "Check artifacts not null", artifacts );
  148. boolean found = false;
  149. for ( Iterator i = discoverer.getKickedOutPathsIterator(); i.hasNext() && !found; )
  150. {
  151. String path = (String) i.next();
  152. found = path.replace( '\\', '/' ).equals( "invalid/invalid/1.0-SNAPSHOT/invalid-1.0.jar" );
  153. }
  154. assertTrue( "Check kickout was found", found );
  155. for ( Iterator i = artifacts.iterator(); i.hasNext(); )
  156. {
  157. Artifact a = (Artifact) i.next();
  158. assertFalse( "Check not 'invalid-1.0.jar'", a.getFile().getName().equals( "invalid-1.0.jar" ) );
  159. }
  160. }
  161. public void testSnapshotInclusion()
  162. {
  163. List artifacts = discoverer.discoverArtifacts( repositoryLocation, null, true );
  164. assertNotNull( "Check artifacts not null", artifacts );
  165. assertTrue( "Check normal included", artifacts.contains( createArtifact( "javax.sql", "jdbc", "2.0" ) ) );
  166. assertTrue( "Check snapshot included",
  167. artifacts.contains( createArtifact( "org.apache.maven", "test", "1.0-20050611.112233-1" ) ) );
  168. }
  169. public void testSnapshotExclusion()
  170. {
  171. List artifacts = discoverer.discoverArtifacts( repositoryLocation, null, false );
  172. assertNotNull( "Check artifacts not null", artifacts );
  173. assertTrue( "Check normal included", artifacts.contains( createArtifact( "javax.sql", "jdbc", "2.0" ) ) );
  174. assertFalse( "Check snapshot included",
  175. artifacts.contains( createArtifact( "org.apache.maven", "test", "1.0-SNAPSHOT" ) ) );
  176. }
  177. private Artifact createArtifact( String groupId, String artifactId, String version )
  178. {
  179. return factory.createArtifact( groupId, artifactId, version, null, "jar" );
  180. }
  181. }