]> source.dussan.org Git - archiva.git/blob
9f35d9440f99616d50bc126f890d01435e6e6a14
[archiva.git] /
1 package org.apache.maven.archiva.indexing.record;
2
3 /*
4  * Copyright 2005-2006 The Apache Software Foundation.
5  *
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
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
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.
17  */
18
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;
28
29 import java.io.File;
30 import java.io.IOException;
31 import java.util.Arrays;
32 import java.util.Collections;
33 import java.util.List;
34
35 /**
36  * Test the minimal artifact index record.
37  *
38  * @author <a href="mailto:brett@apache.org">Brett Porter</a>
39  */
40 public class MinimalArtifactIndexRecordFactoryTest
41     extends PlexusTestCase
42 {
43     private RepositoryIndexRecordFactory factory;
44
45     private ArtifactRepository repository;
46
47     private ArtifactFactory artifactFactory;
48
49     private static final String TEST_GROUP_ID = "org.apache.maven.archiva.record";
50
51     private static final List JAR_CLASS_LIST = Arrays.asList( new String[]{"A", "b.B", "b.c.C"} );
52
53     protected void setUp()
54         throws Exception
55     {
56         super.setUp();
57
58         factory = (RepositoryIndexRecordFactory) lookup( RepositoryIndexRecordFactory.ROLE, "minimal" );
59
60         artifactFactory = (ArtifactFactory) lookup( ArtifactFactory.ROLE );
61
62         ArtifactRepositoryFactory repositoryFactory =
63             (ArtifactRepositoryFactory) lookup( ArtifactRepositoryFactory.ROLE );
64
65         ArtifactRepositoryLayout layout = (ArtifactRepositoryLayout) lookup( ArtifactRepositoryLayout.ROLE, "default" );
66
67         File file = getTestFile( "src/test/managed-repository" );
68         repository =
69             repositoryFactory.createArtifactRepository( "test", file.toURI().toURL().toString(), layout, null, null );
70     }
71
72     public void testIndexedJar()
73         throws RepositoryIndexException
74     {
75         Artifact artifact = createArtifact( "test-jar" );
76
77         RepositoryIndexRecord record = factory.createRecord( artifact );
78
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 );
85
86         assertEquals( "check record", expectedRecord, record );
87     }
88
89     public void testIndexedJarWithClassifier()
90         throws RepositoryIndexException
91     {
92         Artifact artifact = createArtifact( "test-jar", "1.0", "jar", "jdk14" );
93
94         RepositoryIndexRecord record = factory.createRecord( artifact );
95
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 );
102
103         assertEquals( "check record", expectedRecord, record );
104     }
105
106     public void testIndexedJarAndPom()
107         throws RepositoryIndexException
108     {
109         Artifact artifact = createArtifact( "test-jar-and-pom", "1.0-alpha-1", "jar" );
110
111         RepositoryIndexRecord record = factory.createRecord( artifact );
112
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 );
119
120         assertEquals( "check record", expectedRecord, record );
121     }
122
123     public void testIndexedJarAndPomWithClassifier()
124         throws RepositoryIndexException
125     {
126         Artifact artifact = createArtifact( "test-jar-and-pom", "1.0-alpha-1", "jar", "jdk14" );
127
128         RepositoryIndexRecord record = factory.createRecord( artifact );
129
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 );
136
137         assertEquals( "check record", expectedRecord, record );
138     }
139
140     public void testIndexedPom()
141         throws RepositoryIndexException
142     {
143         Artifact artifact = createArtifact( "test-pom", "1.0", "pom" );
144
145         RepositoryIndexRecord record = factory.createRecord( artifact );
146
147         assertNull( "Check no record", record );
148     }
149
150     public void testNonIndexedPom()
151         throws RepositoryIndexException
152     {
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" );
155
156         RepositoryIndexRecord record = factory.createRecord( artifact );
157
158         assertNull( "Check no record", record );
159
160         artifact = createArtifact( "test-plugin", "1.0", "pom" );
161
162         record = factory.createRecord( artifact );
163
164         assertNull( "Check no record", record );
165
166         artifact = createArtifact( "test-archetype", "1.0", "pom" );
167
168         record = factory.createRecord( artifact );
169
170         assertNull( "Check no record", record );
171     }
172
173     public void testIndexedPlugin()
174         throws RepositoryIndexException, IOException, XmlPullParserException
175     {
176         Artifact artifact = createArtifact( "test-plugin" );
177
178         RepositoryIndexRecord record = factory.createRecord( artifact );
179
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" ) );
186
187         assertEquals( "check record", expectedRecord, record );
188     }
189
190     public void testCorruptJar()
191         throws RepositoryIndexException
192     {
193         Artifact artifact = createArtifact( "test-corrupt-jar" );
194
195         RepositoryIndexRecord record = factory.createRecord( artifact );
196
197         assertNull( "Confirm no record is returned", record );
198     }
199
200     public void testNonJar()
201         throws RepositoryIndexException
202     {
203         Artifact artifact = createArtifact( "test-dll", "1.0.1.34", "dll" );
204
205         RepositoryIndexRecord record = factory.createRecord( artifact );
206
207         assertNull( "Confirm no record is returned", record );
208     }
209
210     public void testMissingFile()
211         throws RepositoryIndexException
212     {
213         Artifact artifact = createArtifact( "test-foo" );
214
215         RepositoryIndexRecord record = factory.createRecord( artifact );
216
217         assertNull( "Confirm no record is returned", record );
218     }
219
220     private Artifact createArtifact( String artifactId )
221     {
222         return createArtifact( artifactId, "1.0", "jar" );
223     }
224
225     private Artifact createArtifact( String artifactId, String version, String type )
226     {
227         return createArtifact( artifactId, version, type, null );
228     }
229
230     private Artifact createArtifact( String artifactId, String version, String type, String classifier )
231     {
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 );
238         return artifact;
239     }
240 }