]> source.dussan.org Git - archiva.git/blob
f40700f116f3f1767f743dfbb3504aaf24db1749
[archiva.git] /
1 package org.apache.maven.archiva.indexer.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.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;
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         artifact = createArtifact( "test-skin", "1.0", "pom" );
173
174         record = factory.createRecord( artifact );
175
176         assertNull( "Check no record", record );
177     }
178
179     public void testIndexedPlugin()
180         throws RepositoryIndexException, IOException, XmlPullParserException
181     {
182         Artifact artifact = createArtifact( "test-plugin" );
183
184         RepositoryIndexRecord record = factory.createRecord( artifact );
185
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" ) );
192
193         assertEquals( "check record", expectedRecord, record );
194     }
195
196     public void testCorruptJar()
197         throws RepositoryIndexException
198     {
199         Artifact artifact = createArtifact( "test-corrupt-jar" );
200
201         RepositoryIndexRecord record = factory.createRecord( artifact );
202
203         assertNull( "Confirm no record is returned", record );
204     }
205
206     public void testNonJar()
207         throws RepositoryIndexException
208     {
209         Artifact artifact = createArtifact( "test-dll", "1.0.1.34", "dll" );
210
211         RepositoryIndexRecord record = factory.createRecord( artifact );
212
213         assertNull( "Confirm no record is returned", record );
214     }
215
216     public void testMissingFile()
217         throws RepositoryIndexException
218     {
219         Artifact artifact = createArtifact( "test-foo" );
220
221         RepositoryIndexRecord record = factory.createRecord( artifact );
222
223         assertNull( "Confirm no record is returned", record );
224     }
225
226     private Artifact createArtifact( String artifactId )
227     {
228         return createArtifact( artifactId, "1.0", "jar" );
229     }
230
231     private Artifact createArtifact( String artifactId, String version, String type )
232     {
233         return createArtifact( artifactId, version, type, null );
234     }
235
236     private Artifact createArtifact( String artifactId, String version, String type, String classifier )
237     {
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 );
244         return artifact;
245     }
246 }