1 package org.apache.maven.archiva.indexing.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.indexing.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 );
173 public void testIndexedPlugin()
174 throws RepositoryIndexException, IOException, XmlPullParserException
176 Artifact artifact = createArtifact( "test-plugin" );
178 RepositoryIndexRecord record = factory.createRecord( artifact );
180 MinimalArtifactIndexRecord expectedRecord = new MinimalArtifactIndexRecord();
181 expectedRecord.setMd5Checksum( "3530896791670ebb45e17708e5d52c40" );
182 expectedRecord.setFilename( repository.pathOf( artifact ) );
183 expectedRecord.setLastModified( artifact.getFile().lastModified() );
184 expectedRecord.setSize( artifact.getFile().length() );
185 expectedRecord.setClasses( Collections.singletonList( "org.apache.maven.archiva.record.MyMojo" ) );
187 assertEquals( "check record", expectedRecord, record );
190 public void testCorruptJar()
191 throws RepositoryIndexException
193 Artifact artifact = createArtifact( "test-corrupt-jar" );
195 RepositoryIndexRecord record = factory.createRecord( artifact );
197 assertNull( "Confirm no record is returned", record );
200 public void testNonJar()
201 throws RepositoryIndexException
203 Artifact artifact = createArtifact( "test-dll", "1.0.1.34", "dll" );
205 RepositoryIndexRecord record = factory.createRecord( artifact );
207 assertNull( "Confirm no record is returned", record );
210 public void testMissingFile()
211 throws RepositoryIndexException
213 Artifact artifact = createArtifact( "test-foo" );
215 RepositoryIndexRecord record = factory.createRecord( artifact );
217 assertNull( "Confirm no record is returned", record );
220 private Artifact createArtifact( String artifactId )
222 return createArtifact( artifactId, "1.0", "jar" );
225 private Artifact createArtifact( String artifactId, String version, String type )
227 return createArtifact( artifactId, version, type, null );
230 private Artifact createArtifact( String artifactId, String version, String type, String classifier )
232 Artifact artifact = artifactFactory.createDependencyArtifact( TEST_GROUP_ID, artifactId,
233 VersionRange.createFromVersion( version ), type,
234 classifier, Artifact.SCOPE_RUNTIME );
235 artifact.isSnapshot();
236 artifact.setFile( new File( repository.getBasedir(), repository.pathOf( artifact ) ) );
237 artifact.setRepository( repository );