]> source.dussan.org Git - archiva.git/blob
527e77ab31f58bc201308e7bc5359ccff7532ebc
[archiva.git] /
1 package org.apache.maven.archiva.indexer.record;
2
3 /*
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
11  *
12  *   http://www.apache.org/licenses/LICENSE-2.0
13  *
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
19  * under the License.
20  */
21
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;
31
32 import java.io.File;
33 import java.io.IOException;
34 import java.util.Arrays;
35 import java.util.Collections;
36 import java.util.List;
37
38 /**
39  * Test the minimal artifact index record.
40  *
41  * @author <a href="mailto:brett@apache.org">Brett Porter</a>
42  */
43 public class MinimalArtifactIndexRecordFactoryTest
44     extends PlexusTestCase
45 {
46     private RepositoryIndexRecordFactory factory;
47
48     private ArtifactRepository repository;
49
50     private ArtifactFactory artifactFactory;
51
52     private static final String TEST_GROUP_ID = "org.apache.maven.archiva.record";
53
54     private static final List JAR_CLASS_LIST = Arrays.asList( new String[]{"A", "b.B", "b.c.C"} );
55
56     protected void setUp()
57         throws Exception
58     {
59         super.setUp();
60
61         factory = (RepositoryIndexRecordFactory) lookup( RepositoryIndexRecordFactory.ROLE, "minimal" );
62
63         artifactFactory = (ArtifactFactory) lookup( ArtifactFactory.ROLE );
64
65         ArtifactRepositoryFactory repositoryFactory =
66             (ArtifactRepositoryFactory) lookup( ArtifactRepositoryFactory.ROLE );
67
68         ArtifactRepositoryLayout layout = (ArtifactRepositoryLayout) lookup( ArtifactRepositoryLayout.ROLE, "default" );
69
70         File file = getTestFile( "src/test/managed-repository" );
71         repository =
72             repositoryFactory.createArtifactRepository( "test", file.toURI().toURL().toString(), layout, null, null );
73     }
74
75     public void testIndexedJar()
76         throws RepositoryIndexException
77     {
78         Artifact artifact = createArtifact( "test-jar" );
79
80         RepositoryIndexRecord record = factory.createRecord( artifact );
81
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 );
88
89         assertEquals( "check record", expectedRecord, record );
90     }
91
92     public void testIndexedJarWithClassifier()
93         throws RepositoryIndexException
94     {
95         Artifact artifact = createArtifact( "test-jar", "1.0", "jar", "jdk14" );
96
97         RepositoryIndexRecord record = factory.createRecord( artifact );
98
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 );
105
106         assertEquals( "check record", expectedRecord, record );
107     }
108
109     public void testIndexedJarAndPom()
110         throws RepositoryIndexException
111     {
112         Artifact artifact = createArtifact( "test-jar-and-pom", "1.0-alpha-1", "jar" );
113
114         RepositoryIndexRecord record = factory.createRecord( artifact );
115
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 );
122
123         assertEquals( "check record", expectedRecord, record );
124     }
125
126     public void testIndexedJarAndPomWithClassifier()
127         throws RepositoryIndexException
128     {
129         Artifact artifact = createArtifact( "test-jar-and-pom", "1.0-alpha-1", "jar", "jdk14" );
130
131         RepositoryIndexRecord record = factory.createRecord( artifact );
132
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 );
139
140         assertEquals( "check record", expectedRecord, record );
141     }
142
143     public void testIndexedPom()
144         throws RepositoryIndexException
145     {
146         Artifact artifact = createArtifact( "test-pom", "1.0", "pom" );
147
148         RepositoryIndexRecord record = factory.createRecord( artifact );
149
150         assertNull( "Check no record", record );
151     }
152
153     public void testNonIndexedPom()
154         throws RepositoryIndexException
155     {
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" );
158
159         RepositoryIndexRecord record = factory.createRecord( artifact );
160
161         assertNull( "Check no record", record );
162
163         artifact = createArtifact( "test-plugin", "1.0", "pom" );
164
165         record = factory.createRecord( artifact );
166
167         assertNull( "Check no record", record );
168
169         artifact = createArtifact( "test-archetype", "1.0", "pom" );
170
171         record = factory.createRecord( artifact );
172
173         assertNull( "Check no record", record );
174
175         artifact = createArtifact( "test-skin", "1.0", "pom" );
176
177         record = factory.createRecord( artifact );
178
179         assertNull( "Check no record", record );
180     }
181
182     public void testIndexedPlugin()
183         throws RepositoryIndexException, IOException, XmlPullParserException
184     {
185         Artifact artifact = createArtifact( "test-plugin" );
186
187         RepositoryIndexRecord record = factory.createRecord( artifact );
188
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" ) );
195
196         assertEquals( "check record", expectedRecord, record );
197     }
198
199     public void testCorruptJar()
200         throws RepositoryIndexException
201     {
202         Artifact artifact = createArtifact( "test-corrupt-jar" );
203
204         RepositoryIndexRecord record = factory.createRecord( artifact );
205
206         assertNull( "Confirm no record is returned", record );
207     }
208
209     public void testNonJar()
210         throws RepositoryIndexException
211     {
212         Artifact artifact = createArtifact( "test-dll", "1.0.1.34", "dll" );
213
214         RepositoryIndexRecord record = factory.createRecord( artifact );
215
216         assertNull( "Confirm no record is returned", record );
217     }
218
219     public void testMissingFile()
220         throws RepositoryIndexException
221     {
222         Artifact artifact = createArtifact( "test-foo" );
223
224         RepositoryIndexRecord record = factory.createRecord( artifact );
225
226         assertNull( "Confirm no record is returned", record );
227     }
228
229     private Artifact createArtifact( String artifactId )
230     {
231         return createArtifact( artifactId, "1.0", "jar" );
232     }
233
234     private Artifact createArtifact( String artifactId, String version, String type )
235     {
236         return createArtifact( artifactId, version, type, null );
237     }
238
239     private Artifact createArtifact( String artifactId, String version, String type, String classifier )
240     {
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 );
247         return artifact;
248     }
249 }