]> source.dussan.org Git - archiva.git/blob
d32d0f7321e3a64958480f241f2a6ec8f136e438
[archiva.git] /
1 package org.apache.archiva.repository.maven.content;
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  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  */
20
21 import org.apache.archiva.common.filelock.FileLockManager;
22 import org.apache.archiva.common.utils.VersionComparator;
23 import org.apache.archiva.configuration.ArchivaConfiguration;
24 import org.apache.archiva.configuration.FileType;
25 import org.apache.archiva.configuration.FileTypes;
26 import org.apache.archiva.model.ArtifactReference;
27 import org.apache.archiva.model.ProjectReference;
28 import org.apache.archiva.model.VersionedReference;
29 import org.apache.archiva.repository.EditableManagedRepository;
30 import org.apache.archiva.repository.LayoutException;
31 import org.apache.archiva.repository.ManagedRepositoryContent;
32 import org.apache.archiva.repository.RepositoryContent;
33 import org.apache.archiva.repository.content.Artifact;
34 import org.apache.archiva.repository.content.BaseArtifactTypes;
35 import org.apache.archiva.repository.content.ItemSelector;
36 import org.apache.archiva.repository.content.Project;
37 import org.apache.archiva.repository.content.base.ArchivaItemSelector;
38 import org.apache.archiva.repository.maven.MavenManagedRepository;
39 import org.apache.archiva.repository.maven.metadata.storage.ArtifactMappingProvider;
40 import org.apache.commons.io.FileUtils;
41 import org.junit.Before;
42 import org.junit.Test;
43
44 import javax.inject.Inject;
45 import javax.inject.Named;
46 import java.io.IOException;
47 import java.net.URISyntaxException;
48 import java.nio.file.Files;
49 import java.nio.file.Path;
50 import java.nio.file.Paths;
51 import java.util.ArrayList;
52 import java.util.Arrays;
53 import java.util.Collections;
54 import java.util.List;
55 import java.util.Set;
56 import java.util.stream.Collectors;
57 import java.util.stream.Stream;
58
59 import static org.junit.Assert.*;
60
61 /**
62  * ManagedDefaultRepositoryContentTest
63  */
64 public class ManagedDefaultRepositoryContentTest
65     extends AbstractManagedRepositoryContentTest
66 {
67     private ManagedDefaultRepositoryContent repoContent;
68
69     @Inject
70     FileTypes fileTypes;
71
72     @Inject
73     @Named ( "archivaConfiguration#default" )
74     ArchivaConfiguration archivaConfiguration;
75
76     @Inject
77     List<? extends ArtifactMappingProvider> artifactMappingProviders;
78
79     @Inject
80     MavenContentHelper contentHelper;
81
82     @Inject
83     FileLockManager fileLockManager;
84
85     private Path getRepositoryPath(String repoName) {
86         try
87         {
88             return Paths.get( Thread.currentThread( ).getContextClassLoader( ).getResource( "repositories/" + repoName ).toURI( ) );
89         }
90         catch ( URISyntaxException e )
91         {
92             throw new RuntimeException( "Could not resolve repository path " + e.getMessage( ), e );
93         }
94     }
95
96     @Before
97     public void setUp()
98         throws Exception
99     {
100         Path repoDir = getRepositoryPath( "default-repository" );
101
102         MavenManagedRepository repository = createRepository( "testRepo", "Unit Test Repo", repoDir );
103
104         FileType fileType = archivaConfiguration.getConfiguration().getRepositoryScanning().getFileTypes().get( 0 );
105         fileType.addPattern( "**/*.xml" );
106         assertEquals( FileTypes.ARTIFACTS, fileType.getId() );
107
108         fileTypes.afterConfigurationChange( null, "fileType", null );
109
110         repoContent = new ManagedDefaultRepositoryContent(repository, artifactMappingProviders, fileTypes, fileLockManager);
111         repoContent.setMavenContentHelper( contentHelper );
112         
113         //repoContent = (ManagedRepositoryContent) lookup( ManagedRepositoryContent.class, "default" );
114     }
115
116     @Test
117     public void testGetVersionsBadArtifact()
118         throws Exception
119     {
120         assertGetVersions( "bad_artifact", Collections.emptyList() );
121     }
122
123     @Test
124     public void testGetVersionsMissingMultipleVersions()
125         throws Exception
126     {
127         assertGetVersions( "missing_metadata_b", Arrays.asList( "1.0", "1.0.1", "2.0", "2.0.1", "2.0-20070821-dev" ) );
128     }
129
130     @Test
131     public void testGetVersionsSimple()
132         throws Exception
133     {
134         assertVersions( "proxied_multi", "2.1", new String[]{ "2.1" } );
135     }
136
137     @Test
138     public void testGetVersionsSimpleYetIncomplete()
139         throws Exception
140     {
141         assertGetVersions( "incomplete_metadata_a", Collections.singletonList( "1.0" ) );
142     }
143
144     @Test
145     public void testGetVersionsSimpleYetMissing()
146         throws Exception
147     {
148         assertGetVersions( "missing_metadata_a", Collections.singletonList( "1.0" ) );
149     }
150
151     @Test
152     public void testGetVersionsSnapshotA()
153         throws Exception
154     {
155         assertVersions( "snap_shots_a", "1.0-alpha-11-SNAPSHOT",
156                         new String[]{ "1.0-alpha-11-SNAPSHOT", "1.0-alpha-11-20070221.194724-2",
157                             "1.0-alpha-11-20070302.212723-3", "1.0-alpha-11-20070303.152828-4",
158                             "1.0-alpha-11-20070305.215149-5", "1.0-alpha-11-20070307.170909-6",
159                             "1.0-alpha-11-20070314.211405-9", "1.0-alpha-11-20070316.175232-11" } );
160     }
161
162     @Test
163     public void testToMetadataPathFromProjectReference()
164     {
165         ProjectReference reference = new ProjectReference();
166         reference.setGroupId( "com.foo" );
167         reference.setArtifactId( "foo-tool" );
168
169         assertEquals( "com/foo/foo-tool/maven-metadata.xml", repoContent.toMetadataPath( reference ) );
170     }
171
172     @Test
173     public void testToMetadataPathFromVersionReference()
174     {
175         VersionedReference reference = new VersionedReference();
176         reference.setGroupId( "com.foo" );
177         reference.setArtifactId( "foo-tool" );
178         reference.setVersion( "1.0" );
179
180         assertEquals( "com/foo/foo-tool/1.0/maven-metadata.xml", repoContent.toMetadataPath( reference ) );
181     }
182
183     @Test
184     @Override
185     public void testToPathOnNullArtifactReference()
186     {
187         try
188         {
189             ArtifactReference reference = null;
190             repoContent.toPath( reference );
191             fail( "Should have failed due to null artifact reference." );
192         }
193         catch ( IllegalArgumentException e )
194         {
195             /* expected path */
196         }
197     }
198
199     @Test
200     public void testExcludeMetadataFile()
201         throws Exception
202     {
203         assertVersions( "include_xml", "1.0", new String[]{ "1.0" } );
204     }
205
206     private void assertGetVersions( String artifactId, List<String> expectedVersions )
207         throws Exception
208     {
209         ProjectReference reference = new ProjectReference();
210         reference.setGroupId( "org.apache.archiva.metadata.tests" );
211         reference.setArtifactId( artifactId );
212
213         // Use the test metadata-repository, which is already setup for
214         // These kind of version tests.
215         Path repoDir = getRepositoryPath( "metadata-repository" );
216         (( EditableManagedRepository)repoContent.getRepository()).setLocation( repoDir.toAbsolutePath().toUri() );
217
218         // Request the versions.
219         Set<String> testedVersionSet = repoContent.getVersions( reference );
220
221         // Sort the list (for asserts)
222         VersionComparator comparator = new VersionComparator( );
223         List<String> testedVersions = new ArrayList<>();
224         testedVersions.addAll( testedVersionSet );
225         Collections.sort( testedVersions, comparator );
226
227         // Test the expected array of versions, to the actual tested versions
228         assertEquals( "available versions", expectedVersions, testedVersions );
229
230         ItemSelector selector = ArchivaItemSelector.builder( )
231             .withNamespace( "org.apache.archiva.metadata.tests" )
232             .withProjectId( artifactId )
233             .build( );
234         Project project = repoContent.getProject( selector );
235         assertNotNull( project );
236         List<String> versions = repoContent.getVersions( project ).stream().map(v -> v.getVersion()).sorted( comparator ).collect( Collectors.toList());
237         assertArrayEquals( expectedVersions.toArray(), versions.toArray( ) );
238     }
239
240     private void assertVersions( String artifactId, String version, String[] expectedVersions )
241         throws Exception
242     {
243         VersionedReference reference = new VersionedReference();
244         reference.setGroupId( "org.apache.archiva.metadata.tests" );
245         reference.setArtifactId( artifactId );
246         reference.setVersion( version );
247
248         // Use the test metadata-repository, which is already setup for
249         // These kind of version tests.
250         Path repoDir = getRepositoryPath( "metadata-repository" );
251         ((EditableManagedRepository)repoContent.getRepository()).setLocation( repoDir.toAbsolutePath().toUri() );
252
253         // Request the versions.
254         Set<String> testedVersionSet = repoContent.getVersions( reference );
255
256         // Sort the list (for asserts later)
257         final VersionComparator comparator = new VersionComparator( );
258         List<String> testedVersions = new ArrayList<>();
259         testedVersions.addAll( testedVersionSet );
260         Collections.sort( testedVersions, comparator );
261
262         // Test the expected array of versions, to the actual tested versions
263         assertEquals( "Assert Versions: length/size", expectedVersions.length, testedVersions.size() );
264
265         for ( int i = 0; i < expectedVersions.length; i++ )
266         {
267             String actualVersion = testedVersions.get( i );
268             assertEquals( "Versions[" + i + "]", expectedVersions[i], actualVersion );
269         }
270
271
272         ItemSelector selector = ArchivaItemSelector.builder( )
273             .withNamespace( "org.apache.archiva.metadata.tests" )
274             .withProjectId( artifactId )
275             .withVersion( version )
276             .build( );
277         List<String> versions = repoContent.getVersions( selector ).stream()
278             .map(v -> v.getVersion()).sorted( comparator ).collect( Collectors.toList());
279         assertArrayEquals( expectedVersions, versions.toArray( ) );
280
281
282     }
283
284
285     @Override
286     protected ArtifactReference toArtifactReference( String path )
287         throws LayoutException
288     {
289         return repoContent.toArtifactReference( path );
290     }
291
292     @Override
293     protected ItemSelector toItemSelector( String path ) throws LayoutException
294     {
295         return repoContent.toItemSelector( path );
296     }
297
298     @Override
299     protected String toPath( ArtifactReference reference )
300     {
301         return repoContent.toPath( reference );
302     }
303
304     @Override
305     protected String toPath( ItemSelector selector ) {
306         return repoContent.toPath( selector );
307     }
308
309     @Override
310     protected ManagedRepositoryContent getManaged( )
311     {
312         return repoContent;
313     }
314
315     @Override
316     protected RepositoryContent getContent( )
317     {
318         return repoContent;
319     }
320
321     private Path setupRepoCopy( String source, String target) throws IOException
322     {
323         Path defaultRepo = getRepositoryPath( source );
324         Path newRepo = defaultRepo.getParent( ).resolve( target );
325         FileUtils.copyDirectory( defaultRepo.toFile( ), newRepo.toFile( ) );
326
327         MavenManagedRepository repository = createRepository( "testRepo", "Unit Test Repo", newRepo );
328
329         FileType fileType = archivaConfiguration.getConfiguration().getRepositoryScanning().getFileTypes().get( 0 );
330         fileType.addPattern( "**/*.xml" );
331         assertEquals( FileTypes.ARTIFACTS, fileType.getId() );
332
333         fileTypes.afterConfigurationChange( null, "fileType", null );
334
335         repoContent = new ManagedDefaultRepositoryContent(repository, artifactMappingProviders, fileTypes, fileLockManager);
336         return newRepo;
337
338     }
339
340     @Test
341     public void testDeleteArtifactWithType() throws IOException, org.apache.archiva.repository.ContentNotFoundException, org.apache.archiva.repository.ContentAccessException
342     {
343         Path deleteRepo = setupRepoCopy( "delete-repository", "delete-repository-2" );
344         assertTrue( Files.exists( deleteRepo.resolve( "org/apache/maven/samplejar/1.0/samplejar-1.0-source.jar" ) ) );
345         assertTrue( Files.exists( deleteRepo.resolve( "org/apache/maven/samplejar/1.0/samplejar-1.0.jar" ) ) );
346         assertTrue( Files.exists( deleteRepo.resolve( "org/apache/maven/samplejar/1.0/samplejar-1.0.jar.md5" ) ) );
347         assertTrue( Files.exists( deleteRepo.resolve( "org/apache/maven/samplejar/1.0/samplejar-1.0.jar.sha1" ) ) );
348         assertTrue( Files.exists( deleteRepo.resolve( "org/apache/maven/samplejar/1.0/samplejar-1.0.pom" ) ) );
349
350         ArtifactReference ref = new ArtifactReference( );
351         ref.setGroupId( "org.apache.maven" );
352         ref.setArtifactId( "samplejar" );
353         ref.setVersion( "1.0" );
354         ref.setType( "jar" );
355
356         repoContent.deleteArtifact( ref );
357
358         assertTrue( Files.exists( deleteRepo.resolve( "org/apache/maven/samplejar/1.0/samplejar-1.0-source.jar" ) ) );
359         assertFalse( Files.exists( deleteRepo.resolve( "org/apache/maven/samplejar/1.0/samplejar-1.0.jar" ) ) );
360         assertTrue( Files.exists( deleteRepo.resolve( "org/apache/maven/samplejar/1.0/samplejar-1.0.jar.md5" ) ) );
361         assertTrue( Files.exists( deleteRepo.resolve( "org/apache/maven/samplejar/1.0/samplejar-1.0.jar.sha1" ) ) );
362         assertTrue( Files.exists( deleteRepo.resolve( "org/apache/maven/samplejar/1.0/samplejar-1.0.pom" ) ) );
363
364
365     }
366
367
368     @Test
369     public void testDeleteArtifactWithClassifier() throws IOException, org.apache.archiva.repository.ContentNotFoundException, org.apache.archiva.repository.ContentAccessException
370     {
371         Path deleteRepo = setupRepoCopy( "default-repository", "default-repository-2" );
372         assertTrue( Files.exists( deleteRepo.resolve( "org/apache/maven/samplejar/1.0/samplejar-1.0-source.jar" ) ) );
373         assertTrue( Files.exists( deleteRepo.resolve( "org/apache/maven/samplejar/1.0/samplejar-1.0-source.jar.sha1" ) ) );
374         assertTrue( Files.exists( deleteRepo.resolve( "org/apache/maven/samplejar/1.0/samplejar-1.0.jar" ) ) );
375         assertTrue( Files.exists( deleteRepo.resolve( "org/apache/maven/samplejar/1.0/samplejar-1.0.jar.md5" ) ) );
376         assertTrue( Files.exists( deleteRepo.resolve( "org/apache/maven/samplejar/1.0/samplejar-1.0.jar.sha1" ) ) );
377         assertTrue( Files.exists( deleteRepo.resolve( "org/apache/maven/samplejar/1.0/samplejar-1.0.pom" ) ) );
378
379         ArtifactReference ref = new ArtifactReference( );
380         ref.setGroupId( "org.apache.maven" );
381         ref.setArtifactId( "samplejar" );
382         ref.setVersion( "1.0" );
383         ref.setClassifier( "source" );
384         ref.setType( "jar" );
385
386         repoContent.deleteArtifact( ref );
387
388         assertFalse( Files.exists( deleteRepo.resolve( "org/apache/maven/samplejar/1.0/samplejar-1.0-source.jar" ) ) );
389         assertTrue( Files.exists( deleteRepo.resolve( "org/apache/maven/samplejar/1.0/samplejar-1.0-source.jar.sha1" ) ) );
390         assertTrue( Files.exists( deleteRepo.resolve( "org/apache/maven/samplejar/1.0/samplejar-1.0.jar" ) ) );
391         assertTrue( Files.exists( deleteRepo.resolve( "org/apache/maven/samplejar/1.0/samplejar-1.0.jar.md5" ) ) );
392         assertTrue( Files.exists( deleteRepo.resolve( "org/apache/maven/samplejar/1.0/samplejar-1.0.jar.sha1" ) ) );
393         assertTrue( Files.exists( deleteRepo.resolve( "org/apache/maven/samplejar/1.0/samplejar-1.0.pom" ) ) );
394
395     }
396
397     @Test
398     public void testDeleteArtifactWithoutType() throws IOException, org.apache.archiva.repository.ContentNotFoundException, org.apache.archiva.repository.ContentAccessException
399     {
400         Path deleteRepo = setupRepoCopy( "default-repository", "default-repository-2" );
401         assertTrue( Files.exists( deleteRepo.resolve( "org/apache/maven/samplejar/1.0/samplejar-1.0-source.jar" ) ) );
402         assertTrue( Files.exists( deleteRepo.resolve( "org/apache/maven/samplejar/1.0/samplejar-1.0-source.jar.sha1" ) ) );
403         assertTrue( Files.exists( deleteRepo.resolve( "org/apache/maven/samplejar/1.0/samplejar-1.0.jar" ) ) );
404         assertTrue( Files.exists( deleteRepo.resolve( "org/apache/maven/samplejar/1.0/samplejar-1.0.jar.md5" ) ) );
405         assertTrue( Files.exists( deleteRepo.resolve( "org/apache/maven/samplejar/1.0/samplejar-1.0.jar.sha1" ) ) );
406         assertTrue( Files.exists( deleteRepo.resolve( "org/apache/maven/samplejar/1.0/samplejar-1.0.pom" ) ) );
407
408         ArtifactReference ref = new ArtifactReference( );
409         ref.setGroupId( "org.apache.maven" );
410         ref.setArtifactId( "samplejar" );
411         ref.setVersion( "1.0" );
412
413         repoContent.deleteArtifact( ref );
414
415         assertFalse( Files.exists( deleteRepo.resolve( "org/apache/maven/samplejar/1.0" ) ) );
416         assertTrue( Files.exists( deleteRepo.resolve( "org/apache/maven/samplejar" ) ) );
417
418     }
419
420
421     @Test
422     public void testDeleteVersion() throws IOException, org.apache.archiva.repository.ContentNotFoundException, org.apache.archiva.repository.ContentAccessException
423     {
424         Path deleteRepo = setupRepoCopy( "delete-repository", "delete-repository-2" );
425         assertTrue( Files.exists( deleteRepo.resolve( "org/apache/maven/samplejar/1.0/samplejar-1.0-source.jar" ) ) );
426         assertTrue( Files.exists( deleteRepo.resolve( "org/apache/maven/samplejar/1.0/samplejar-1.0.jar" ) ) );
427         assertTrue( Files.exists( deleteRepo.resolve( "org/apache/maven/samplejar/1.0/samplejar-1.0.jar.md5" ) ) );
428         assertTrue( Files.exists( deleteRepo.resolve( "org/apache/maven/samplejar/1.0/samplejar-1.0.jar.sha1" ) ) );
429         assertTrue( Files.exists( deleteRepo.resolve( "org/apache/maven/samplejar/1.0/samplejar-1.0.pom" ) ) );
430
431         VersionedReference ref = new VersionedReference( ).groupId( "org.apache.maven" ).artifactId( "samplejar" ).version( "1.0" );
432
433         repoContent.deleteVersion( ref );
434
435         assertFalse( Files.exists( deleteRepo.resolve( "org/apache/maven/samplejar/1.0" ) ) );
436
437     }
438
439     @Test
440     public void testDeleteProject() throws IOException, org.apache.archiva.repository.ContentNotFoundException, org.apache.archiva.repository.ContentAccessException
441     {
442         Path deleteRepo = setupRepoCopy( "delete-repository", "delete-repository-2" );
443         assertTrue( Files.exists( deleteRepo.resolve( "org/apache/maven/samplejar/1.0/samplejar-1.0-source.jar" ) ) );
444         assertTrue( Files.exists( deleteRepo.resolve( "org/apache/maven/samplejar/1.0/samplejar-1.0.jar" ) ) );
445         assertTrue( Files.exists( deleteRepo.resolve( "org/apache/maven/samplejar/1.0/samplejar-1.0.jar.md5" ) ) );
446         assertTrue( Files.exists( deleteRepo.resolve( "org/apache/maven/samplejar/1.0/samplejar-1.0.jar.sha1" ) ) );
447         assertTrue( Files.exists( deleteRepo.resolve( "org/apache/maven/samplejar/1.0/samplejar-1.0.pom" ) ) );
448
449         ProjectReference ref = new ProjectReference( ).groupId( "org.apache.maven" ).artifactId( "samplejar" );
450
451         repoContent.deleteProject( ref );
452
453         assertFalse( Files.exists( deleteRepo.resolve( "org/apache/maven/samplejar/1.0" ) ) );
454
455     }
456
457     @Test
458     public void testGetArtifactStreamWithVersionSelector() {
459         ItemSelector selector = ArchivaItemSelector.builder( )
460             .withNamespace( "javax.sql" )
461             .withProjectId( "jdbc" )
462             .withVersion( "2.0" ).build();
463         try(Stream<? extends Artifact> stream = repoContent.newArtifactStream( selector ))
464         {
465             assertNotNull( stream );
466             List<? extends Artifact> results = stream.collect( Collectors.toList( ) );
467             checkArtifactListWithVersionSelector1( results );
468         }
469     }
470
471     @Test
472     public void testGetArtifactListWithVersionSelector() {
473         ItemSelector selector = ArchivaItemSelector.builder( )
474             .withNamespace( "javax.sql" )
475             .withProjectId( "jdbc" )
476             .withVersion( "2.0" ).build();
477         List<? extends Artifact> results = repoContent.getArtifacts( selector );
478         checkArtifactListWithVersionSelector1( results );
479     }
480
481     private void checkArtifactListWithVersionSelector1( List<? extends Artifact> results )
482     {
483         assertNotNull( results );
484         assertEquals( 2, results.size( ) );
485         Artifact mainArtifact = results.stream( ).filter( a -> a.getFileName( ).equals( "jdbc-2.0.jar" ) ).findFirst( ).get( );
486         assertNotNull( mainArtifact );
487         assertEquals( BaseArtifactTypes.MAIN, mainArtifact.getArtifactType( ) );
488         Artifact metaArtifact = results.stream( ).filter( a -> a.getFileName( ).equals( "maven-metadata-repository.xml" ) ).findFirst( ).get( );
489         assertNotNull( metaArtifact );
490         assertEquals( MavenTypes.REPOSITORY_METADATA, metaArtifact.getArtifactType( ) );
491     }
492
493     @Test
494     public void testGetArtifactStreamWithVersionSelector2() {
495         ItemSelector selector = ArchivaItemSelector.builder( )
496             .withNamespace( "org.apache.axis2" )
497             .withProjectId( "axis2" )
498             .withVersion( "1.3-SNAPSHOT" ).build();
499         try(Stream<? extends Artifact> stream = repoContent.newArtifactStream( selector ))
500         {
501             assertNotNull( stream );
502             List<? extends Artifact> results = stream.collect( Collectors.toList( ) );
503             checkArtifactListWithVersionSelector2( results );
504         }
505     }
506
507     @Test
508     public void testGetArtifactListWithVersionSelector2() {
509         ItemSelector selector = ArchivaItemSelector.builder( )
510             .withNamespace( "org.apache.axis2" )
511             .withProjectId( "axis2" )
512             .withVersion( "1.3-SNAPSHOT" ).build();
513         List<? extends Artifact> results = repoContent.getArtifacts( selector );
514         checkArtifactListWithVersionSelector2( results );
515     }
516
517     private void checkArtifactListWithVersionSelector2( List<? extends Artifact> results )
518     {
519         assertNotNull( results );
520         assertEquals( 39, results.size( ) );
521
522         Artifact artifact = results.stream( ).filter( a -> a.getFileName( ).equals( "axis2-1.3-20070725.210059-1.pom" ) )
523             .findFirst( ).get( );
524
525         assertNotNull( artifact );
526         assertEquals( "pom", artifact.getExtension( ) );
527         assertEquals( BaseArtifactTypes.MAIN, artifact.getArtifactType( ) );
528         assertEquals( "1.3-SNAPSHOT", artifact.getVersion( ).getVersion( ) );
529         assertEquals( "1.3-20070725.210059-1", artifact.getArtifactVersion( ) );
530         assertEquals( ".pom", artifact.getRemainder( ) );
531         assertEquals( "axis2", artifact.getId( ) );
532         assertEquals( "axis2", artifact.getVersion( ).getProject( ).getId( ) );
533         assertEquals( "org.apache.axis2", artifact.getVersion( ).getProject( ).getNamespace( ).getNamespace( ) );
534         assertEquals( "", artifact.getClassifier( ) );
535         assertEquals( "pom", artifact.getType( ) );
536
537         artifact = null;
538         artifact = results.stream( ).filter( a -> a.getFileName( ).equals( "axis2-1.3-20070725.210059-1.pom.md5" ) )
539             .findFirst( ).get( );
540
541         assertNotNull( artifact );
542         assertEquals( "md5", artifact.getExtension( ) );
543         assertEquals( BaseArtifactTypes.RELATED, artifact.getArtifactType( ) );
544         assertEquals( "1.3-SNAPSHOT", artifact.getVersion( ).getVersion( ) );
545         assertEquals( "1.3-20070725.210059-1", artifact.getArtifactVersion( ) );
546         assertEquals( ".pom.md5", artifact.getRemainder( ) );
547         assertEquals( "axis2", artifact.getId( ) );
548         assertEquals( "axis2", artifact.getVersion( ).getProject( ).getId( ) );
549         assertEquals( "org.apache.axis2", artifact.getVersion( ).getProject( ).getNamespace( ).getNamespace( ) );
550         assertEquals( "", artifact.getClassifier( ) );
551         assertEquals( "md5", artifact.getType( ) );
552
553
554         artifact = null;
555         artifact = results.stream( ).filter( a -> a.getFileName( ).equals( "maven-metadata.xml" ) )
556             .findFirst( ).get( );
557         assertNotNull( artifact );
558         assertEquals( BaseArtifactTypes.METADATA, artifact.getArtifactType( ) );
559         assertEquals( "1.3-SNAPSHOT", artifact.getVersion( ).getVersion( ) );
560         assertEquals( "xml", artifact.getExtension( ) );
561     }
562
563     @Test
564     public void testGetArtifactListWithArtifactSelector1() {
565         ItemSelector selector = ArchivaItemSelector.builder( )
566             .withNamespace( "org.apache.axis2" )
567             .withProjectId( "axis2" )
568             .withVersion( "1.3-SNAPSHOT" )
569             .withArtifactVersion( "1.3-20070731.113304-21" )
570             .withExtension( "pom" )
571             .build( );
572         List<? extends Artifact> results = repoContent.getArtifacts( selector );
573         checkArtifactListWithArtifactSelector1( results );
574     }
575
576     @Test
577     public void testGetArtifactStreamWithArtifactSelector1() {
578         ItemSelector selector = ArchivaItemSelector.builder( )
579             .withNamespace( "org.apache.axis2" )
580             .withProjectId( "axis2" )
581             .withVersion( "1.3-SNAPSHOT" )
582             .withArtifactVersion( "1.3-20070731.113304-21" )
583             .withExtension( "pom" )
584             .build( );
585         try(Stream<? extends Artifact> results = repoContent.newArtifactStream( selector ))
586         {
587             checkArtifactListWithArtifactSelector1( results.collect( Collectors.toList()) );
588         }
589     }
590
591     private void checkArtifactListWithArtifactSelector1( List<? extends Artifact> results )
592     {
593         assertNotNull( results );
594         assertEquals( 1, results.size( ) );
595         Artifact artifact = results.get( 0 );
596         assertEquals( "pom", artifact.getExtension( ) );
597         assertEquals( BaseArtifactTypes.MAIN, artifact.getArtifactType( ) );
598     }
599
600     @Test
601     public void testGetArtifactListWithArtifactSelector2() {
602         ItemSelector selector = ArchivaItemSelector.builder( )
603             .withNamespace( "org.apache.axis2" )
604             .withProjectId( "axis2" )
605             .withVersion( "1.3-SNAPSHOT" )
606             .withArtifactVersion( "1.3-20070731.113304-21" )
607             .withExtension( "pom" )
608             .includeRelatedArtifacts()
609             .build( );
610         List<? extends Artifact> results = repoContent.getArtifacts( selector );
611
612         checkArtifactListWithArtifactSelector2( results );
613
614     }
615
616     @Test
617     public void testGetArtifactStreamWithArtifactSelector2() {
618         ItemSelector selector = ArchivaItemSelector.builder( )
619             .withNamespace( "org.apache.axis2" )
620             .withProjectId( "axis2" )
621             .withVersion( "1.3-SNAPSHOT" )
622             .withArtifactVersion( "1.3-20070731.113304-21" )
623             .withExtension( "pom" )
624             .includeRelatedArtifacts()
625             .build( );
626         try(Stream<? extends Artifact> results = repoContent.newArtifactStream( selector ))
627         {
628             checkArtifactListWithArtifactSelector2( results.collect( Collectors.toList()) );
629         }
630     }
631
632     private void checkArtifactListWithArtifactSelector2( List<? extends Artifact> results )
633     {
634         assertNotNull( results );
635         assertEquals( 3, results.size( ) );
636         Artifact artifact = results.stream( ).filter( a -> a.getFileName( ).equalsIgnoreCase( "axis2-1.3-20070731.113304-21.pom" ) )
637             .findFirst( ).get( );
638         assertNotNull( artifact );
639         assertEquals( "pom", artifact.getExtension( ) );
640         assertEquals( BaseArtifactTypes.MAIN, artifact.getArtifactType( ) );
641
642         artifact = results.stream( ).filter( a -> a.getFileName( ).equalsIgnoreCase( "axis2-1.3-20070731.113304-21.pom.sha1" ) )
643             .findFirst( ).get( );
644         assertNotNull( artifact );
645         assertEquals( "sha1", artifact.getExtension( ) );
646         assertEquals( BaseArtifactTypes.RELATED, artifact.getArtifactType( ) );
647     }
648
649
650     @Test
651     public void testArtifactListWithProjectSelector() {
652         ItemSelector selector = ArchivaItemSelector.builder( )
653             .withNamespace( "org.apache.maven.shared" )
654             .withProjectId( "maven-downloader" )
655             .build( );
656         List<? extends Artifact> results = repoContent.getArtifacts( selector );
657         checkArtifactListWithProjectSelector( results );
658
659     }
660
661     @Test
662     public void testArtifactStreamWithProjectSelector() {
663         ItemSelector selector = ArchivaItemSelector.builder( )
664             .withNamespace( "org.apache.maven.shared" )
665             .withProjectId( "maven-downloader" )
666             .build( );
667         Stream<? extends Artifact> results = repoContent.newArtifactStream( selector );
668         checkArtifactListWithProjectSelector( results.collect( Collectors.toList()) );
669
670     }
671
672     private void checkArtifactListWithProjectSelector( List<? extends Artifact> results )
673     {
674         assertNotNull( results );
675         assertEquals( 27, results.size( ) );
676
677         Artifact artifact = results.stream( ).filter( a -> a.getFileName( ).equalsIgnoreCase( "maven-metadata.xml" ) )
678             .findFirst( ).get( );
679         assertNotNull( artifact );
680         assertEquals( "xml", artifact.getExtension( ) );
681         assertEquals( BaseArtifactTypes.METADATA, artifact.getArtifactType( ) );
682
683         artifact = results.stream( ).filter( a -> a.getFileName( ).equalsIgnoreCase( "maven-downloader-1.0-sources.jar" ) )
684             .findFirst( ).get( );
685         assertNotNull( artifact );
686         assertEquals( BaseArtifactTypes.MAIN, artifact.getArtifactType( ) );
687         assertEquals( "sources", artifact.getClassifier( ) );
688         assertEquals( "java-source", artifact.getType( ) );
689
690         artifact = results.stream( ).filter( a -> a.getFileName( ).equalsIgnoreCase( "maven-downloader-1.0-sources.jar.sha1" ) )
691             .findFirst( ).get( );
692         assertNotNull( artifact );
693         assertEquals( BaseArtifactTypes.RELATED, artifact.getArtifactType( ) );
694         assertEquals( "sources", artifact.getClassifier( ) );
695         assertEquals( "sha1", artifact.getType( ) );
696         assertEquals( ".jar.sha1", artifact.getRemainder( ) );
697     }
698
699     @Test
700     public void testArtifactListWithNamespaceSelector() {
701         ItemSelector selector = ArchivaItemSelector.builder( )
702             .withNamespace( "org.multilevel" )
703             .build( );
704         List<? extends Artifact> results = repoContent.getArtifacts( selector );
705         assertNotNull( results );
706         assertEquals( 3, results.size( ) );
707         assertTrue( results.get( 0 ).getFileName( ).startsWith( "testproj1" ) );
708     }
709
710     @Test
711     public void testArtifactListWithNamespaceSelectorRecursive() {
712         ItemSelector selector = ArchivaItemSelector.builder( )
713             .withNamespace( "org.multilevel" )
714             .recurse()
715             .build( );
716         List<? extends Artifact> results = repoContent.getArtifacts( selector );
717         checkArtifactListWithNamespaceSelectorRecursive( results );
718     }
719
720     @Test
721     public void testArtifactStreamWithNamespaceSelectorRecursive() {
722         ItemSelector selector = ArchivaItemSelector.builder( )
723             .withNamespace( "org.multilevel" )
724             .recurse()
725             .build( );
726         Stream<? extends Artifact> results = repoContent.newArtifactStream( selector );
727         checkArtifactListWithNamespaceSelectorRecursive( results.collect( Collectors.toList()) );
728     }
729
730     private void checkArtifactListWithNamespaceSelectorRecursive( List<? extends Artifact> results )
731     {
732         assertNotNull( results );
733         assertEquals( 6, results.size( ) );
734
735         Artifact artifact = results.stream( ).filter( a -> a.getFileName( ).equalsIgnoreCase( "testproj2-1.0.pom" ) )
736             .findFirst( ).get( );
737         assertNotNull( artifact );
738         assertEquals( 6, artifact.getAsset( ).getParent( ).getPath( ).split( "/" ).length );
739
740         artifact = results.stream( ).filter( a -> a.getFileName( ).equalsIgnoreCase( "testproj1-1.0.pom" ) )
741             .findFirst( ).get( );
742         assertNotNull( artifact );
743         assertEquals( 5, artifact.getAsset( ).getParent( ).getPath( ).split( "/" ).length );
744     }
745
746
747     @Test
748     public void testArtifactListWithArtifactSelector1() {
749         ItemSelector selector = ArchivaItemSelector.builder( )
750             .withNamespace( "org.apache.maven" )
751             .withProjectId( "test" )
752             .withVersion( "1.0-SNAPSHOT" )
753             .withArtifactId( "test" )
754             .withArtifactVersion( "1.0-20050611.112233-1" )
755             .build( );
756
757         List<? extends Artifact> results = repoContent.getArtifacts( selector );
758
759         assertNotNull( results );
760         assertEquals( 1, results.size( ) );
761
762         Artifact artifact = results.stream( ).filter( a -> a.getFileName( ).equalsIgnoreCase( "test-1.0-20050611.112233-1.jar" ) )
763             .findFirst().get();
764         assertNotNull( artifact );
765         assertEquals( "", artifact.getClassifier( ) );
766     }
767
768     @Test
769     public void testArtifactListWithArtifactSelector2() {
770         ItemSelector selector = ArchivaItemSelector.builder( )
771             .withNamespace( "org.apache.maven" )
772             .withProjectId( "test" )
773             .withVersion( "1.0-SNAPSHOT" )
774             .withClassifier( "*" )
775             .withArtifactId( "test" )
776             .withArtifactVersion( "1.0-20050611.112233-1" )
777             .build( );
778
779         List<? extends Artifact> results = repoContent.getArtifacts( selector );
780
781         assertNotNull( results );
782         assertEquals( 2, results.size( ) );
783
784         Artifact artifact = results.stream( ).filter( a -> a.getFileName( ).equalsIgnoreCase( "test-1.0-20050611.112233-1-javadoc.jar" ) )
785             .findFirst().get();
786         assertNotNull( artifact );
787         assertEquals( "javadoc", artifact.getClassifier( ) );
788         assertEquals( "javadoc", artifact.getType( ) );
789     }
790
791     @Test
792     public void testArtifactListWithArtifactSelector3() {
793         ItemSelector selector = ArchivaItemSelector.builder( )
794             .withNamespace( "org.apache.maven" )
795             .withProjectId( "test" )
796             .withVersion( "1.0-SNAPSHOT" )
797             .withClassifier( "*" )
798             .withArtifactVersion( "1.0-20050611.112233-1" )
799             .build( );
800
801         List<? extends Artifact> results = repoContent.getArtifacts( selector );
802
803         assertNotNull( results );
804         assertEquals( 3, results.size( ) );
805
806         Artifact artifact = results.stream( ).filter( a -> a.getFileName( ).equalsIgnoreCase( "test-1.0-20050611.112233-1-javadoc.jar" ) )
807             .findFirst().get();
808         assertNotNull( artifact );
809         assertEquals( "javadoc", artifact.getClassifier( ) );
810         assertEquals( "javadoc", artifact.getType( ) );
811
812         artifact = results.stream( ).filter( a -> a.getFileName( ).equalsIgnoreCase( "wrong-artifactId-1.0-20050611.112233-1.jar" ) )
813             .findFirst().get();
814         assertNotNull( artifact );
815         assertEquals( "", artifact.getClassifier( ) );
816         assertEquals( "wrong-artifactId", artifact.getId( ) );
817     }
818
819     @Test
820     public void testArtifactListWithArtifactSelectorAndRelated() {
821         ItemSelector selector = ArchivaItemSelector.builder( )
822             .withNamespace( "org.apache.maven" )
823             .withProjectId( "samplejar" )
824             .withVersion( "1.0" )
825             .withArtifactVersion( "1.0" )
826             .withArtifactId( "samplejar" )
827             .withExtension( "jar" )
828             .includeRelatedArtifacts()
829             .build( );
830
831         List<? extends Artifact> results = repoContent.getArtifacts( selector );
832
833         assertNotNull( results );
834         assertEquals( 3, results.size( ) );
835
836         Artifact artifact = results.stream( ).filter( a -> a.getFileName( ).equalsIgnoreCase( "samplejar-1.0.jar" ) )
837             .findFirst().get();
838         assertNotNull( artifact );
839         assertEquals( BaseArtifactTypes.MAIN, artifact.getArtifactType( ) );
840
841         artifact = results.stream( ).filter( a -> a.getFileName( ).equalsIgnoreCase( "samplejar-1.0.jar.md5" ) )
842             .findFirst().get();
843         assertNotNull( artifact );
844         assertEquals( BaseArtifactTypes.RELATED, artifact.getArtifactType( ) );
845         assertEquals( "md5", artifact.getExtension( ) );
846
847         artifact = results.stream( ).filter( a -> a.getFileName( ).equalsIgnoreCase( "samplejar-1.0.jar.sha1" ) )
848             .findFirst().get();
849         assertNotNull( artifact );
850         assertEquals( BaseArtifactTypes.RELATED, artifact.getArtifactType( ) );
851         assertEquals( "sha1", artifact.getExtension( ) );
852
853     }
854
855 }