1 package org.apache.maven.archiva.indexer.record;
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.maven.archiva.indexer.RepositoryIndexException;
23 import org.apache.maven.artifact.Artifact;
24 import org.apache.maven.artifact.factory.ArtifactFactory;
25 import org.apache.maven.artifact.repository.ArtifactRepository;
26 import org.apache.maven.artifact.repository.ArtifactRepositoryFactory;
27 import org.apache.maven.artifact.repository.layout.ArtifactRepositoryLayout;
28 import org.apache.maven.artifact.versioning.VersionRange;
29 import org.codehaus.plexus.PlexusTestCase;
30 import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
33 import java.io.IOException;
34 import java.util.Arrays;
35 import java.util.Collections;
36 import java.util.List;
39 * Test the minimal artifact index record.
41 * @author <a href="mailto:brett@apache.org">Brett Porter</a>
43 public class MinimalArtifactIndexRecordFactoryTest
44 extends PlexusTestCase
46 private RepositoryIndexRecordFactory factory;
48 private ArtifactRepository repository;
50 private ArtifactFactory artifactFactory;
52 private static final String TEST_GROUP_ID = "org.apache.maven.archiva.record";
54 private static final List JAR_CLASS_LIST = Arrays.asList( new String[]{"A", "b.B", "b.c.C"} );
56 protected void setUp()
61 factory = (RepositoryIndexRecordFactory) lookup( RepositoryIndexRecordFactory.ROLE, "minimal" );
63 artifactFactory = (ArtifactFactory) lookup( ArtifactFactory.ROLE );
65 ArtifactRepositoryFactory repositoryFactory =
66 (ArtifactRepositoryFactory) lookup( ArtifactRepositoryFactory.ROLE );
68 ArtifactRepositoryLayout layout = (ArtifactRepositoryLayout) lookup( ArtifactRepositoryLayout.ROLE, "default" );
70 File file = getTestFile( "src/test/managed-repository" );
72 repositoryFactory.createArtifactRepository( "test", file.toURI().toURL().toString(), layout, null, null );
75 public void testIndexedJar()
76 throws RepositoryIndexException
78 Artifact artifact = createArtifact( "test-jar" );
80 RepositoryIndexRecord record = factory.createRecord( artifact );
82 MinimalArtifactIndexRecord expectedRecord = new MinimalArtifactIndexRecord();
83 expectedRecord.setMd5Checksum( "3a0adc365f849366cd8b633cad155cb7" );
84 expectedRecord.setFilename( repository.pathOf( artifact ) );
85 expectedRecord.setLastModified( artifact.getFile().lastModified() );
86 expectedRecord.setSize( artifact.getFile().length() );
87 expectedRecord.setClasses( JAR_CLASS_LIST );
89 assertEquals( "check record", expectedRecord, record );
92 public void testIndexedJarWithClassifier()
93 throws RepositoryIndexException
95 Artifact artifact = createArtifact( "test-jar", "1.0", "jar", "jdk14" );
97 RepositoryIndexRecord record = factory.createRecord( artifact );
99 MinimalArtifactIndexRecord expectedRecord = new MinimalArtifactIndexRecord();
100 expectedRecord.setMd5Checksum( "3a0adc365f849366cd8b633cad155cb7" );
101 expectedRecord.setFilename( repository.pathOf( artifact ) );
102 expectedRecord.setLastModified( artifact.getFile().lastModified() );
103 expectedRecord.setSize( artifact.getFile().length() );
104 expectedRecord.setClasses( JAR_CLASS_LIST );
106 assertEquals( "check record", expectedRecord, record );
109 public void testIndexedJarAndPom()
110 throws RepositoryIndexException
112 Artifact artifact = createArtifact( "test-jar-and-pom", "1.0-alpha-1", "jar" );
114 RepositoryIndexRecord record = factory.createRecord( artifact );
116 MinimalArtifactIndexRecord expectedRecord = new MinimalArtifactIndexRecord();
117 expectedRecord.setMd5Checksum( "3a0adc365f849366cd8b633cad155cb7" );
118 expectedRecord.setFilename( repository.pathOf( artifact ) );
119 expectedRecord.setLastModified( artifact.getFile().lastModified() );
120 expectedRecord.setSize( artifact.getFile().length() );
121 expectedRecord.setClasses( JAR_CLASS_LIST );
123 assertEquals( "check record", expectedRecord, record );
126 public void testIndexedJarAndPomWithClassifier()
127 throws RepositoryIndexException
129 Artifact artifact = createArtifact( "test-jar-and-pom", "1.0-alpha-1", "jar", "jdk14" );
131 RepositoryIndexRecord record = factory.createRecord( artifact );
133 MinimalArtifactIndexRecord expectedRecord = new MinimalArtifactIndexRecord();
134 expectedRecord.setMd5Checksum( "3a0adc365f849366cd8b633cad155cb7" );
135 expectedRecord.setFilename( repository.pathOf( artifact ) );
136 expectedRecord.setLastModified( artifact.getFile().lastModified() );
137 expectedRecord.setSize( artifact.getFile().length() );
138 expectedRecord.setClasses( JAR_CLASS_LIST );
140 assertEquals( "check record", expectedRecord, record );
143 public void testIndexedPom()
144 throws RepositoryIndexException
146 Artifact artifact = createArtifact( "test-pom", "1.0", "pom" );
148 RepositoryIndexRecord record = factory.createRecord( artifact );
150 assertNull( "Check no record", record );
153 public void testNonIndexedPom()
154 throws RepositoryIndexException
156 // If we pass in only the POM that belongs to a JAR, then expect null not the POM
157 Artifact artifact = createArtifact( "test-jar-and-pom", "1.0-alpha-1", "pom" );
159 RepositoryIndexRecord record = factory.createRecord( artifact );
161 assertNull( "Check no record", record );
163 artifact = createArtifact( "test-plugin", "1.0", "pom" );
165 record = factory.createRecord( artifact );
167 assertNull( "Check no record", record );
169 artifact = createArtifact( "test-archetype", "1.0", "pom" );
171 record = factory.createRecord( artifact );
173 assertNull( "Check no record", record );
175 artifact = createArtifact( "test-skin", "1.0", "pom" );
177 record = factory.createRecord( artifact );
179 assertNull( "Check no record", record );
182 public void testIndexedPlugin()
183 throws RepositoryIndexException, IOException, XmlPullParserException
185 Artifact artifact = createArtifact( "test-plugin" );
187 RepositoryIndexRecord record = factory.createRecord( artifact );
189 MinimalArtifactIndexRecord expectedRecord = new MinimalArtifactIndexRecord();
190 expectedRecord.setMd5Checksum( "3530896791670ebb45e17708e5d52c40" );
191 expectedRecord.setFilename( repository.pathOf( artifact ) );
192 expectedRecord.setLastModified( artifact.getFile().lastModified() );
193 expectedRecord.setSize( artifact.getFile().length() );
194 expectedRecord.setClasses( Collections.singletonList( "org.apache.maven.archiva.record.MyMojo" ) );
196 assertEquals( "check record", expectedRecord, record );
199 public void testCorruptJar()
200 throws RepositoryIndexException
202 Artifact artifact = createArtifact( "test-corrupt-jar" );
204 RepositoryIndexRecord record = factory.createRecord( artifact );
206 assertNull( "Confirm no record is returned", record );
209 public void testNonJar()
210 throws RepositoryIndexException
212 Artifact artifact = createArtifact( "test-dll", "1.0.1.34", "dll" );
214 RepositoryIndexRecord record = factory.createRecord( artifact );
216 assertNull( "Confirm no record is returned", record );
219 public void testMissingFile()
220 throws RepositoryIndexException
222 Artifact artifact = createArtifact( "test-foo" );
224 RepositoryIndexRecord record = factory.createRecord( artifact );
226 assertNull( "Confirm no record is returned", record );
229 private Artifact createArtifact( String artifactId )
231 return createArtifact( artifactId, "1.0", "jar" );
234 private Artifact createArtifact( String artifactId, String version, String type )
236 return createArtifact( artifactId, version, type, null );
239 private Artifact createArtifact( String artifactId, String version, String type, String classifier )
241 Artifact artifact = artifactFactory.createDependencyArtifact( TEST_GROUP_ID, artifactId,
242 VersionRange.createFromVersion( version ), type,
243 classifier, Artifact.SCOPE_RUNTIME );
244 artifact.isSnapshot();
245 artifact.setFile( new File( repository.getBasedir(), repository.pathOf( artifact ) ) );
246 artifact.setRepository( repository );