1 package org.apache.maven.archiva.consumers;
4 * Licensed to the Apache Software Foundation (ASF) under one
5 * or more contributor license agreements. See the NOTICE file
6 * distributed with this work for additional information
7 * regarding copyright ownership. The ASF licenses this file
8 * to you under the Apache License, Version 2.0 (the
9 * "License"); you may not use this file except in compliance
10 * with the License. You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 * KIND, either express or implied. See the License for the
18 * specific language governing permissions and limitations
22 import org.apache.commons.lang.StringUtils;
23 import org.apache.maven.archiva.common.utils.BaseFile;
24 import org.apache.maven.archiva.model.ArchivaRepository;
25 import org.apache.maven.artifact.Artifact;
27 import java.util.ArrayList;
28 import java.util.Collection;
29 import java.util.Iterator;
30 import java.util.List;
33 * GenericArtifactConsumerTest
35 * @author <a href="mailto:joakim@erdfelt.com">Joakim Erdfelt</a>
38 public class GenericArtifactConsumerTest
39 extends AbstractGenericConsumerTestCase
41 private MockArtifactConsumer getMockArtifactConsumer()
44 return (MockArtifactConsumer) consumerFactory.createConsumer( "mock-artifact" );
47 public void testScanLegacy()
50 ArchivaRepository repository = getLegacyRepository();
51 List consumers = new ArrayList();
53 MockArtifactConsumer mockConsumer = getMockArtifactConsumer();
54 mockConsumer.init( repository );
56 consumers.add( mockConsumer );
58 List files = getLegacyLayoutArtifactPaths();
59 for ( Iterator it = files.iterator(); it.hasNext(); )
61 String path = (String) it.next();
64 mockConsumer.processFile( new BaseFile( repository.getRepositoryURL().getPath(), path ) );
66 catch ( ConsumerException e )
68 mockConsumer.getProblemsTracker().addProblem( e );
72 assertNotNull( consumers );
74 FileProblemsTracker tracker = mockConsumer.getProblemsTracker();
76 assertTracker( tracker, 16 );
78 assertHasFailureMessage( "Path does not match a legacy repository path for an artifact",
79 "invalid/invalid-1.0.jar", tracker );
80 assertHasFailureMessage( "Path filename version is empty", "invalid/jars/invalid.jar", tracker );
81 assertHasFailureMessage( "Path does not match a legacy repository path for an artifact",
82 "invalid/jars/1.0/invalid-1.0.jar", tracker );
84 assertEquals( 10, mockConsumer.getArtifactMap().size() );
87 public void testScanDefault()
90 ArchivaRepository repository = getDefaultRepository();
91 List consumers = new ArrayList();
93 MockArtifactConsumer mockConsumer = getMockArtifactConsumer();
94 mockConsumer.init( repository );
96 consumers.add( mockConsumer );
98 List files = getDefaultLayoutArtifactPaths();
99 for ( Iterator it = files.iterator(); it.hasNext(); )
101 String path = (String) it.next();
104 mockConsumer.processFile( new BaseFile( repository.getRepositoryURL().getPath(), path ) );
106 catch ( ConsumerException e )
108 mockConsumer.getProblemsTracker().addProblem( e );
112 // Test gathered information from Mock consumer.
114 assertNotNull( consumers );
116 FileProblemsTracker tracker = mockConsumer.getProblemsTracker();
118 assertTracker( tracker, 21 );
120 assertHasFailureMessage( "Failed to create a snapshot artifact: invalid:invalid:jar:1.0:runtime",
121 "invalid/invalid/1.0-SNAPSHOT/invalid-1.0.jar", tracker );
122 assertHasFailureMessage( "Path is too short to build an artifact from.", "invalid/invalid-1.0.jar", tracker );
123 assertHasFailureMessage( "Built artifact version does not match path version",
124 "invalid/invalid/1.0/invalid-2.0.jar", tracker );
126 assertEquals( 25, mockConsumer.getArtifactMap().size() );
128 // Test for known include artifacts
130 Collection artifacts = mockConsumer.getArtifactMap().values();
131 assertHasArtifact( "org.apache.maven", "testing", "1.0", "jar", null, artifacts );
132 assertHasArtifact( "org.apache.maven", "some-ejb", "1.0", "jar", "client", artifacts );
133 assertHasArtifact( "org.apache.maven", "testing", "1.0", "java-source", "sources", artifacts );
134 assertHasArtifact( "org.apache.maven", "testing", "1.0", "java-source", "test-sources", artifacts );
135 assertHasArtifact( "org.apache.maven", "testing", "1.0", "distribution-zip", null, artifacts );
136 assertHasArtifact( "org.apache.maven", "testing", "1.0", "distribution-tgz", null, artifacts );
137 assertHasArtifact( "javax.sql", "jdbc", "2.0", "jar", null, artifacts );
138 assertHasArtifact( "org.apache.maven", "test", "1.0-20050611.112233-1", "jar", null, artifacts );
139 assertHasArtifact( "org.apache.maven", "test", "1.0-20050611.112233-1", "jar", "javadoc", artifacts );
141 // Test for known excluded files and dirs to validate exclusions.
143 Iterator it = mockConsumer.getArtifactMap().values().iterator();
144 while ( it.hasNext() )
146 Artifact a = (Artifact) it.next();
147 assertTrue( "Artifact " + a + " should have it's .getFile() set.", a.getFile() != null );
148 assertTrue( "Artifact " + a + " should have it's .getRepository() set.", a.getRepository() != null );
149 assertTrue( "Artifact " + a + " should have non-null repository url.", a.getRepository().getUrl() != null );
150 assertFalse( "Check not CVS", a.getFile().getPath().indexOf( "CVS" ) >= 0 );
151 assertFalse( "Check not .svn", a.getFile().getPath().indexOf( ".svn" ) >= 0 );
155 private void dumpProblems( FileProblemsTracker tracker )
158 System.out.println( "-- ProblemTracker dump -------------------------" );
159 for ( Iterator itPaths = tracker.getPaths().iterator(); itPaths.hasNext(); )
161 String path = (String) itPaths.next();
162 System.out.println( " [" + problemNum + "]: " + path );
165 for ( Iterator itProblems = tracker.getProblems( path ).iterator(); itProblems.hasNext(); )
167 String message = (String) itProblems.next();
168 System.out.println( " [" + messageNum + "]: " + message );
176 private void assertTracker( FileProblemsTracker tracker, int expectedProblemCount )
178 assertNotNull( "ProblemsTracker should not be null.", tracker );
180 int actualProblemCount = tracker.getProblemCount();
181 if ( expectedProblemCount != actualProblemCount )
183 dumpProblems( tracker );
184 fail( "Problem count (across all paths) expected:<" + expectedProblemCount + ">, actual:<"
185 + actualProblemCount + ">" );
189 private void assertHasFailureMessage( String message, String path, FileProblemsTracker tracker )
191 if ( !tracker.hasProblems( path ) )
193 fail( "There are no messages for expected path [" + path + "]" );
196 assertTrue( "Unable to find message [" + message + "] in path [" + path + "]", tracker.getProblems( path )
197 .contains( message ) );
200 private void assertHasArtifact( String groupId, String artifactId, String version, String type, String classifier,
201 Collection collection )
203 for ( Iterator it = collection.iterator(); it.hasNext(); )
205 Artifact artifact = (Artifact) it.next();
206 if ( StringUtils.equals( groupId, artifact.getGroupId() )
207 && StringUtils.equals( artifactId, artifact.getArtifactId() )
208 && StringUtils.equals( version, artifact.getVersion() )
209 && StringUtils.equals( type, artifact.getType() )
210 && StringUtils.equals( classifier, artifact.getClassifier() ) )
217 fail( "Was unable to find artifact " + groupId + ":" + artifactId + ":" + version + ":" + type + ":"