1 package org.apache.maven.archiva.indexer.record;
4 * Copyright 2005-2006 The Apache Software Foundation.
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
19 import org.apache.maven.archiva.indexer.RepositoryIndexException;
20 import org.apache.maven.artifact.Artifact;
21 import org.apache.maven.artifact.factory.ArtifactFactory;
22 import org.apache.maven.artifact.repository.ArtifactRepository;
23 import org.apache.maven.artifact.repository.ArtifactRepositoryFactory;
24 import org.apache.maven.artifact.repository.layout.ArtifactRepositoryLayout;
25 import org.apache.maven.artifact.versioning.VersionRange;
26 import org.codehaus.plexus.PlexusTestCase;
27 import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
30 import java.io.IOException;
31 import java.util.Arrays;
32 import java.util.Collections;
33 import java.util.List;
36 * Test the minimal artifact index record.
38 * @author <a href="mailto:brett@apache.org">Brett Porter</a>
40 public class MinimalArtifactIndexRecordFactoryTest
41 extends PlexusTestCase
43 private RepositoryIndexRecordFactory factory;
45 private ArtifactRepository repository;
47 private ArtifactFactory artifactFactory;
49 private static final String TEST_GROUP_ID = "org.apache.maven.archiva.record";
51 private static final List JAR_CLASS_LIST = Arrays.asList( new String[]{"A", "b.B", "b.c.C"} );
53 protected void setUp()
58 factory = (RepositoryIndexRecordFactory) lookup( RepositoryIndexRecordFactory.ROLE, "minimal" );
60 artifactFactory = (ArtifactFactory) lookup( ArtifactFactory.ROLE );
62 ArtifactRepositoryFactory repositoryFactory =
63 (ArtifactRepositoryFactory) lookup( ArtifactRepositoryFactory.ROLE );
65 ArtifactRepositoryLayout layout = (ArtifactRepositoryLayout) lookup( ArtifactRepositoryLayout.ROLE, "default" );
67 File file = getTestFile( "src/test/managed-repository" );
69 repositoryFactory.createArtifactRepository( "test", file.toURI().toURL().toString(), layout, null, null );
72 public void testIndexedJar()
73 throws RepositoryIndexException
75 Artifact artifact = createArtifact( "test-jar" );
77 RepositoryIndexRecord record = factory.createRecord( artifact );
79 MinimalArtifactIndexRecord expectedRecord = new MinimalArtifactIndexRecord();
80 expectedRecord.setMd5Checksum( "3a0adc365f849366cd8b633cad155cb7" );
81 expectedRecord.setFilename( repository.pathOf( artifact ) );
82 expectedRecord.setLastModified( artifact.getFile().lastModified() );
83 expectedRecord.setSize( artifact.getFile().length() );
84 expectedRecord.setClasses( JAR_CLASS_LIST );
86 assertEquals( "check record", expectedRecord, record );
89 public void testIndexedJarWithClassifier()
90 throws RepositoryIndexException
92 Artifact artifact = createArtifact( "test-jar", "1.0", "jar", "jdk14" );
94 RepositoryIndexRecord record = factory.createRecord( artifact );
96 MinimalArtifactIndexRecord expectedRecord = new MinimalArtifactIndexRecord();
97 expectedRecord.setMd5Checksum( "3a0adc365f849366cd8b633cad155cb7" );
98 expectedRecord.setFilename( repository.pathOf( artifact ) );
99 expectedRecord.setLastModified( artifact.getFile().lastModified() );
100 expectedRecord.setSize( artifact.getFile().length() );
101 expectedRecord.setClasses( JAR_CLASS_LIST );
103 assertEquals( "check record", expectedRecord, record );
106 public void testIndexedJarAndPom()
107 throws RepositoryIndexException
109 Artifact artifact = createArtifact( "test-jar-and-pom", "1.0-alpha-1", "jar" );
111 RepositoryIndexRecord record = factory.createRecord( artifact );
113 MinimalArtifactIndexRecord expectedRecord = new MinimalArtifactIndexRecord();
114 expectedRecord.setMd5Checksum( "3a0adc365f849366cd8b633cad155cb7" );
115 expectedRecord.setFilename( repository.pathOf( artifact ) );
116 expectedRecord.setLastModified( artifact.getFile().lastModified() );
117 expectedRecord.setSize( artifact.getFile().length() );
118 expectedRecord.setClasses( JAR_CLASS_LIST );
120 assertEquals( "check record", expectedRecord, record );
123 public void testIndexedJarAndPomWithClassifier()
124 throws RepositoryIndexException
126 Artifact artifact = createArtifact( "test-jar-and-pom", "1.0-alpha-1", "jar", "jdk14" );
128 RepositoryIndexRecord record = factory.createRecord( artifact );
130 MinimalArtifactIndexRecord expectedRecord = new MinimalArtifactIndexRecord();
131 expectedRecord.setMd5Checksum( "3a0adc365f849366cd8b633cad155cb7" );
132 expectedRecord.setFilename( repository.pathOf( artifact ) );
133 expectedRecord.setLastModified( artifact.getFile().lastModified() );
134 expectedRecord.setSize( artifact.getFile().length() );
135 expectedRecord.setClasses( JAR_CLASS_LIST );
137 assertEquals( "check record", expectedRecord, record );
140 public void testIndexedPom()
141 throws RepositoryIndexException
143 Artifact artifact = createArtifact( "test-pom", "1.0", "pom" );
145 RepositoryIndexRecord record = factory.createRecord( artifact );
147 assertNull( "Check no record", record );
150 public void testNonIndexedPom()
151 throws RepositoryIndexException
153 // If we pass in only the POM that belongs to a JAR, then expect null not the POM
154 Artifact artifact = createArtifact( "test-jar-and-pom", "1.0-alpha-1", "pom" );
156 RepositoryIndexRecord record = factory.createRecord( artifact );
158 assertNull( "Check no record", record );
160 artifact = createArtifact( "test-plugin", "1.0", "pom" );
162 record = factory.createRecord( artifact );
164 assertNull( "Check no record", record );
166 artifact = createArtifact( "test-archetype", "1.0", "pom" );
168 record = factory.createRecord( artifact );
170 assertNull( "Check no record", record );
172 artifact = createArtifact( "test-skin", "1.0", "pom" );
174 record = factory.createRecord( artifact );
176 assertNull( "Check no record", record );
179 public void testIndexedPlugin()
180 throws RepositoryIndexException, IOException, XmlPullParserException
182 Artifact artifact = createArtifact( "test-plugin" );
184 RepositoryIndexRecord record = factory.createRecord( artifact );
186 MinimalArtifactIndexRecord expectedRecord = new MinimalArtifactIndexRecord();
187 expectedRecord.setMd5Checksum( "3530896791670ebb45e17708e5d52c40" );
188 expectedRecord.setFilename( repository.pathOf( artifact ) );
189 expectedRecord.setLastModified( artifact.getFile().lastModified() );
190 expectedRecord.setSize( artifact.getFile().length() );
191 expectedRecord.setClasses( Collections.singletonList( "org.apache.maven.archiva.record.MyMojo" ) );
193 assertEquals( "check record", expectedRecord, record );
196 public void testCorruptJar()
197 throws RepositoryIndexException
199 Artifact artifact = createArtifact( "test-corrupt-jar" );
201 RepositoryIndexRecord record = factory.createRecord( artifact );
203 assertNull( "Confirm no record is returned", record );
206 public void testNonJar()
207 throws RepositoryIndexException
209 Artifact artifact = createArtifact( "test-dll", "1.0.1.34", "dll" );
211 RepositoryIndexRecord record = factory.createRecord( artifact );
213 assertNull( "Confirm no record is returned", record );
216 public void testMissingFile()
217 throws RepositoryIndexException
219 Artifact artifact = createArtifact( "test-foo" );
221 RepositoryIndexRecord record = factory.createRecord( artifact );
223 assertNull( "Confirm no record is returned", record );
226 private Artifact createArtifact( String artifactId )
228 return createArtifact( artifactId, "1.0", "jar" );
231 private Artifact createArtifact( String artifactId, String version, String type )
233 return createArtifact( artifactId, version, type, null );
236 private Artifact createArtifact( String artifactId, String version, String type, String classifier )
238 Artifact artifact = artifactFactory.createDependencyArtifact( TEST_GROUP_ID, artifactId,
239 VersionRange.createFromVersion( version ), type,
240 classifier, Artifact.SCOPE_RUNTIME );
241 artifact.isSnapshot();
242 artifact.setFile( new File( repository.getBasedir(), repository.pathOf( artifact ) ) );
243 artifact.setRepository( repository );