]> source.dussan.org Git - archiva.git/blob
d86e75930920a11018f72770a9a4ae62d0cc61fe
[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.metadata.maven.MavenMetadataReader;
27 import org.apache.archiva.model.ArtifactReference;
28 import org.apache.archiva.model.ProjectReference;
29 import org.apache.archiva.model.VersionedReference;
30 import org.apache.archiva.repository.EditableManagedRepository;
31 import org.apache.archiva.repository.ManagedRepositoryContent;
32 import org.apache.archiva.repository.LayoutException;
33 import org.apache.archiva.repository.ManagedRepository;
34 import org.apache.archiva.repository.BaseRepositoryContentLayout;
35 import org.apache.archiva.repository.RepositoryContent;
36 import org.apache.archiva.repository.content.Artifact;
37 import org.apache.archiva.repository.content.BaseArtifactTypes;
38 import org.apache.archiva.repository.content.ContentItem;
39 import org.apache.archiva.repository.content.DataItem;
40 import org.apache.archiva.repository.content.ItemNotFoundException;
41 import org.apache.archiva.repository.content.ItemSelector;
42 import org.apache.archiva.repository.content.Namespace;
43 import org.apache.archiva.repository.content.Project;
44 import org.apache.archiva.repository.content.Version;
45 import org.apache.archiva.repository.content.base.ArchivaContentItem;
46 import org.apache.archiva.repository.content.base.ArchivaItemSelector;
47 import org.apache.archiva.repository.maven.MavenManagedRepository;
48 import org.apache.archiva.repository.maven.metadata.storage.ArtifactMappingProvider;
49 import org.apache.archiva.repository.storage.StorageAsset;
50 import org.apache.commons.io.FileUtils;
51 import org.junit.Before;
52 import org.junit.Test;
53
54 import javax.inject.Inject;
55 import javax.inject.Named;
56 import java.io.IOException;
57 import java.io.OutputStream;
58 import java.io.Reader;
59 import java.net.URISyntaxException;
60 import java.nio.charset.Charset;
61 import java.nio.file.Files;
62 import java.nio.file.Path;
63 import java.nio.file.Paths;
64 import java.nio.file.attribute.FileTime;
65 import java.util.List;
66 import java.util.stream.Collectors;
67 import java.util.stream.Stream;
68
69 import static org.junit.Assert.*;
70
71 /**
72  * ManagedDefaultRepositoryContentTest
73  */
74 public class ManagedDefaultRepositoryContentTest
75     extends AbstractBaseRepositoryContentLayoutTest
76 {
77     private ManagedDefaultRepositoryContent repoContent;
78
79     @Inject
80     FileTypes fileTypes;
81
82     @Inject
83     @Named ( "archivaConfiguration#default" )
84     ArchivaConfiguration archivaConfiguration;
85
86     @Inject
87     List<? extends ArtifactMappingProvider> artifactMappingProviders;
88
89     @Inject
90     MavenContentHelper contentHelper;
91
92     @Inject
93     @Named( "metadataReader#maven" )
94     MavenMetadataReader metadataReader;
95
96     @Inject
97     FileLockManager fileLockManager;
98
99     private Path getRepositoryPath(String repoName) {
100         try
101         {
102             return Paths.get( Thread.currentThread( ).getContextClassLoader( ).getResource( "repositories/" + repoName ).toURI( ) );
103         }
104         catch ( URISyntaxException e )
105         {
106             throw new RuntimeException( "Could not resolve repository path " + e.getMessage( ), e );
107         }
108     }
109
110     @Before
111     public void setUp()
112         throws Exception
113     {
114         Path repoDir = getRepositoryPath( "default-repository" );
115
116         MavenManagedRepository repository = createRepository( "testRepo", "Unit Test Repo", repoDir );
117
118         FileType fileType = archivaConfiguration.getConfiguration().getRepositoryScanning().getFileTypes().get( 0 );
119         fileType.addPattern( "**/*.xml" );
120         assertEquals( FileTypes.ARTIFACTS, fileType.getId() );
121
122         fileTypes.afterConfigurationChange( null, "fileType", null );
123
124         repoContent = new ManagedDefaultRepositoryContent(repository, artifactMappingProviders, fileTypes, fileLockManager);
125         repoContent.setMavenContentHelper( contentHelper );
126         repoContent.setMetadataReader( metadataReader );
127         
128         //repoContent = (ManagedRepositoryContent) lookup( ManagedRepositoryContent.class, "default" );
129     }
130
131     @Test
132     public void testGetVersionsSnapshotA()
133         throws Exception
134     {
135         assertArtifactVersions( "snap_shots_a", "1.0-alpha-11-SNAPSHOT",
136                         new String[]{ "1.0-alpha-11-SNAPSHOT", "1.0-alpha-11-20070221.194724-2",
137                             "1.0-alpha-11-20070302.212723-3", "1.0-alpha-11-20070303.152828-4",
138                             "1.0-alpha-11-20070305.215149-5", "1.0-alpha-11-20070307.170909-6",
139                             "1.0-alpha-11-20070314.211405-9", "1.0-alpha-11-20070316.175232-11" } );
140     }
141
142     @Test
143     public void testToMetadataPathFromProjectReference()
144     {
145         ProjectReference reference = new ProjectReference();
146         reference.setGroupId( "com.foo" );
147         reference.setArtifactId( "foo-tool" );
148
149         assertEquals( "com/foo/foo-tool/maven-metadata.xml", repoContent.toMetadataPath( reference ) );
150     }
151
152     @Test
153     public void testToMetadataPathFromVersionReference()
154     {
155         VersionedReference reference = new VersionedReference();
156         reference.setGroupId( "com.foo" );
157         reference.setArtifactId( "foo-tool" );
158         reference.setVersion( "1.0" );
159
160         assertEquals( "com/foo/foo-tool/1.0/maven-metadata.xml", repoContent.toMetadataPath( reference ) );
161     }
162
163     @Test
164     @Override
165     public void testToPathOnNullArtifactReference()
166     {
167         try
168         {
169             ArtifactReference reference = null;
170             repoContent.toPath( reference );
171             fail( "Should have failed due to null artifact reference." );
172         }
173         catch ( IllegalArgumentException e )
174         {
175             /* expected path */
176         }
177     }
178
179     @Override
180     protected Artifact createArtifact( String groupId, String artifactId, String version, String classifier, String type ) throws LayoutException
181     {
182         ItemSelector selector = createItemSelector( groupId, artifactId, version, classifier, type );
183         return repoContent.getLayout( BaseRepositoryContentLayout.class ).getArtifact( selector );
184     }
185
186     @Test
187     public void testExcludeMetadataFile()
188         throws Exception
189     {
190         assertVersions( "include_xml", "1.0", new String[]{ "1.0" } );
191     }
192
193
194     private void assertVersions( String artifactId, String version, String[] expectedVersions )
195         throws Exception
196     {
197         // Use the test metadata-repository, which is already setup for
198         // These kind of version tests.
199         Path repoDir = getRepositoryPath( "metadata-repository" );
200         ((EditableManagedRepository)repoContent.getRepository()).setLocation( repoDir.toAbsolutePath().toUri() );
201
202         // Request the versions.
203
204         // Sort the list (for asserts later)
205         final VersionComparator comparator = new VersionComparator( );
206
207         ItemSelector selector = ArchivaItemSelector.builder( )
208             .withNamespace( "org.apache.archiva.metadata.tests" )
209             .withProjectId( artifactId )
210             .withVersion( version )
211             .build( );
212         List<String> versions = repoContent.getVersions( selector ).stream()
213             .map(v -> v.getId()).sorted( comparator ).collect( Collectors.toList());
214         assertArrayEquals( expectedVersions, versions.toArray( ) );
215
216
217     }
218
219     private void assertArtifactVersions( String artifactId, String version, String[] expectedVersions )
220         throws Exception
221     {
222         // Use the test metadata-repository, which is already setup for
223         // These kind of version tests.
224         Path repoDir = getRepositoryPath( "metadata-repository" );
225         ((EditableManagedRepository)repoContent.getRepository()).setLocation( repoDir.toAbsolutePath().toUri() );
226
227         // Request the versions.
228
229         // Sort the list (for asserts later)
230         final VersionComparator comparator = new VersionComparator( );
231
232         ItemSelector selector = ArchivaItemSelector.builder( )
233             .withNamespace( "org.apache.archiva.metadata.tests" )
234             .withProjectId( artifactId )
235             .withVersion( version )
236             .build( );
237         List<String> versions = repoContent.getArtifactVersions( selector ).stream()
238             .sorted( comparator ).collect( Collectors.toList());
239         assertArrayEquals( expectedVersions, versions.toArray( ) );
240
241
242     }
243
244     @Test
245     public void getTestGetProjectWithIllegalArgs() {
246         ItemSelector selector = ArchivaItemSelector.builder( )
247             .withNamespace( "org.apache" )
248             .withVersion( "1.0" )
249             .build();
250         try
251         {
252             repoContent.getProject( selector );
253             assertFalse( "Should throw IllegalArgumentException if no project id is given", true );
254         } catch (IllegalArgumentException e) {
255             // Everything fine
256             assertTrue( e.getMessage( ).contains( "Project id must be set" ) );
257         }
258     }
259
260     @Test
261     public void getTestGetVersionWithIllegalArgs() {
262         ItemSelector selector = ArchivaItemSelector.builder( )
263             .withNamespace( "org.apache.maven" )
264             .withVersion( "1.0" )
265             .build();
266         try
267         {
268             repoContent.getVersion( selector );
269             assertFalse( "Should throw IllegalArgumentException if no project id is given", true );
270         } catch (IllegalArgumentException e) {
271             // Everything fine
272             assertTrue( e.getMessage( ).contains( "Project id must be set" ) );
273         }
274
275
276         selector = ArchivaItemSelector.builder( )
277             .withNamespace( "org.apache.maven" )
278             .withProjectId( "shared" )
279             .build();
280         try
281         {
282             repoContent.getVersion( selector );
283             assertFalse( "Should throw IllegalArgumentException if no version is given", true );
284         } catch (IllegalArgumentException e) {
285             // Everything fine
286             assertTrue( e.getMessage( ).contains( "Version must be set" ) );
287         }
288     }
289
290     @Test
291     public void getTestGetArtifactWithIllegalArgs() {
292         ItemSelector selector = ArchivaItemSelector.builder( )
293             .withNamespace( "org.apache.maven" )
294             .withVersion( "1.0" )
295             .withArtifactId( "shared" )
296             .withArtifactVersion("1.0")
297             .build();
298         try
299         {
300             repoContent.getArtifact( selector );
301             assertFalse( "Should throw IllegalArgumentException if no project id is given", true );
302         } catch (IllegalArgumentException e) {
303             // Everything fine
304             assertTrue( e.getMessage( ).contains( "Project id must be set" ) );
305         }
306
307
308         selector = ArchivaItemSelector.builder( )
309             .withNamespace( "org.apache.maven" )
310             .withProjectId( "shared" )
311             .withArtifactId( "shared" )
312             .withArtifactVersion("1.0")
313             .build();
314         try
315         {
316             repoContent.getArtifact( selector );
317             assertFalse( "Should throw IllegalArgumentException if no version is given", true );
318         } catch (IllegalArgumentException e) {
319             // Everything fine
320             assertTrue( e.getMessage( ).contains( "Version must be set" ) );
321         }
322
323         selector = ArchivaItemSelector.builder( )
324             .withNamespace( "org.apache.maven" )
325             .withProjectId( "shared" )
326             .withVersion("1.0")
327             .withArtifactVersion("1.0")
328             .build();
329         try
330         {
331             repoContent.getArtifact( selector );
332             assertFalse( "Should throw IllegalArgumentException if no artifact id is given", true );
333         } catch (IllegalArgumentException e) {
334             // Everything fine
335             assertTrue( e.getMessage( ).contains( "Artifact id must be set" ) );
336         }
337
338
339     }
340
341     @Test
342     public void testGetProjects() {
343         ItemSelector selector = ArchivaItemSelector.builder( )
344             .withNamespace( "org.apache.maven" ).build();
345         Namespace ns = repoContent.getNamespace( selector );
346         assertNotNull( ns );
347         List<? extends Project> projects = repoContent.getProjects( ns );
348         assertEquals( 12, projects.size( ) );
349         String[] expected = new String[]{
350             "A", "B", "C", "archiva", "discovery", "maven-parent", "samplejar", "shared", "some-ejb", "test",
351             "testing", "update"
352         };
353         Object[] actual = projects.stream( ).map( p -> p.getId( ) ).sorted( ).toArray( );
354         assertArrayEquals( expected, actual);
355     }
356
357     @Test
358     public void testGetProjectsWithSelector() {
359         ItemSelector selector = ArchivaItemSelector.builder( )
360             .withNamespace( "org.apache.maven" ).build();
361         List<? extends Project> projects = repoContent.getProjects( selector );
362         assertEquals( 12, projects.size( ) );
363         String[] expected = new String[]{
364             "A", "B", "C", "archiva", "discovery", "maven-parent", "samplejar", "shared", "some-ejb", "test",
365             "testing", "update"
366         };
367         Object[] actual = projects.stream( ).map( p -> p.getId( ) ).sorted( ).toArray( );
368         assertArrayEquals( expected, actual);
369     }
370
371     @Test
372     public void testGetVersionsWithIllegalSelector() {
373         ItemSelector selector = ArchivaItemSelector.builder( )
374             .withNamespace( "org.apache.maven" ).build();
375         try
376         {
377             List<? extends Version> versions = repoContent.getVersions( selector );
378             assertFalse( "IllegalArgumentException expected, when project id not set", true );
379         } catch (IllegalArgumentException e) {
380             assertEquals( "Project id not set, while retrieving versions.", e.getMessage( ) );
381         }
382     }
383
384     @Test
385     public void testGetVersionsWithSelector() {
386         ItemSelector selector = ArchivaItemSelector.builder( )
387             .withNamespace( "org.apache.maven" )
388             .withProjectId( "samplejar" ).build();
389         List<? extends Version> versions = repoContent.getVersions( selector );
390         assertNotNull( versions );
391         assertEquals( 2, versions.size( ) );
392     }
393
394
395     @Override
396     protected ItemSelector toItemSelector( String path ) throws LayoutException
397     {
398         return repoContent.toItemSelector( path );
399     }
400
401     @Override
402     protected String toPath( Artifact reference )
403     {
404         return repoContent.toPath( reference );
405     }
406
407     @Override
408     protected String toPath( ItemSelector selector ) {
409         return repoContent.toPath( selector );
410     }
411
412     @Override
413     protected ManagedRepositoryContent getManaged( )
414     {
415         return repoContent;
416     }
417
418     @Override
419     protected RepositoryContent getContent( )
420     {
421         return repoContent;
422     }
423
424     private Path setupRepoCopy( String source, String target) throws IOException
425     {
426         Path defaultRepo = getRepositoryPath( source );
427         Path newRepo = defaultRepo.getParent( ).resolve( target );
428         FileUtils.copyDirectory( defaultRepo.toFile( ), newRepo.toFile( ) );
429
430         MavenManagedRepository repository = createRepository( "testRepo", "Unit Test Repo", newRepo );
431
432         FileType fileType = archivaConfiguration.getConfiguration().getRepositoryScanning().getFileTypes().get( 0 );
433         fileType.addPattern( "**/*.xml" );
434         assertEquals( FileTypes.ARTIFACTS, fileType.getId() );
435
436         fileTypes.afterConfigurationChange( null, "fileType", null );
437
438         repoContent = new ManagedDefaultRepositoryContent(repository, artifactMappingProviders, fileTypes, fileLockManager);
439         return newRepo;
440
441     }
442
443
444     @Test
445     public void testGetArtifactStreamWithVersionSelector() {
446         ItemSelector selector = ArchivaItemSelector.builder( )
447             .withNamespace( "javax.sql" )
448             .withProjectId( "jdbc" )
449             .withVersion( "2.0" ).build();
450         try(Stream<? extends Artifact> stream = repoContent.newArtifactStream( selector ))
451         {
452             assertNotNull( stream );
453             List<? extends Artifact> results = stream.collect( Collectors.toList( ) );
454             checkArtifactListWithVersionSelector1( results );
455         }
456     }
457
458     @Test
459     public void testGetArtifactListWithVersionSelector() {
460         ItemSelector selector = ArchivaItemSelector.builder( )
461             .withNamespace( "javax.sql" )
462             .withProjectId( "jdbc" )
463             .withVersion( "2.0" ).build();
464         List<? extends Artifact> results = repoContent.getArtifacts( selector );
465         checkArtifactListWithVersionSelector1( results );
466     }
467
468     private void checkArtifactListWithVersionSelector1( List<? extends Artifact> results )
469     {
470         assertNotNull( results );
471         assertEquals( 2, results.size( ) );
472         Artifact mainArtifact = results.stream( ).filter( a -> a.getFileName( ).equals( "jdbc-2.0.jar" ) ).findFirst( ).get( );
473         assertNotNull( mainArtifact );
474         assertEquals( BaseArtifactTypes.MAIN, mainArtifact.getDataType( ) );
475         Artifact metaArtifact = results.stream( ).filter( a -> a.getFileName( ).equals( "maven-metadata-repository.xml" ) ).findFirst( ).get( );
476         assertNotNull( metaArtifact );
477         assertEquals( MavenTypes.REPOSITORY_METADATA, metaArtifact.getDataType( ) );
478     }
479
480     @Test
481     public void testGetArtifactStreamWithVersionSelector2() {
482         ItemSelector selector = ArchivaItemSelector.builder( )
483             .withNamespace( "org.apache.axis2" )
484             .withProjectId( "axis2" )
485             .withVersion( "1.3-SNAPSHOT" ).build();
486         try(Stream<? extends Artifact> stream = repoContent.newArtifactStream( selector ))
487         {
488             assertNotNull( stream );
489             List<? extends Artifact> results = stream.collect( Collectors.toList( ) );
490             checkArtifactListWithVersionSelector2( results );
491         }
492     }
493
494     @Test
495     public void testGetArtifactListWithVersionSelector2() {
496         ItemSelector selector = ArchivaItemSelector.builder( )
497             .withNamespace( "org.apache.axis2" )
498             .withProjectId( "axis2" )
499             .withVersion( "1.3-SNAPSHOT" ).build();
500         List<? extends Artifact> results = repoContent.getArtifacts( selector );
501         checkArtifactListWithVersionSelector2( results );
502     }
503
504     private void checkArtifactListWithVersionSelector2( List<? extends Artifact> results )
505     {
506         assertNotNull( results );
507         assertEquals( 39, results.size( ) );
508
509         Artifact artifact = results.stream( ).filter( a -> a.getFileName( ).equals( "axis2-1.3-20070725.210059-1.pom" ) )
510             .findFirst( ).get( );
511
512         assertNotNull( artifact );
513         assertEquals( "pom", artifact.getExtension( ) );
514         assertEquals( BaseArtifactTypes.MAIN, artifact.getDataType( ) );
515         assertEquals( "1.3-SNAPSHOT", artifact.getVersion( ).getId( ) );
516         assertEquals( "1.3-20070725.210059-1", artifact.getArtifactVersion( ) );
517         assertEquals( ".pom", artifact.getRemainder( ) );
518         assertEquals( "axis2", artifact.getId( ) );
519         assertEquals( "axis2", artifact.getVersion( ).getProject( ).getId( ) );
520         assertEquals( "org.apache.axis2", artifact.getVersion( ).getProject( ).getNamespace( ).getId( ) );
521         assertEquals( "", artifact.getClassifier( ) );
522         assertEquals( "pom", artifact.getType( ) );
523
524         artifact = null;
525         artifact = results.stream( ).filter( a -> a.getFileName( ).equals( "axis2-1.3-20070725.210059-1.pom.md5" ) )
526             .findFirst( ).get( );
527
528         assertNotNull( artifact );
529         assertEquals( "md5", artifact.getExtension( ) );
530         assertEquals( BaseArtifactTypes.RELATED, artifact.getDataType( ) );
531         assertEquals( "1.3-SNAPSHOT", artifact.getVersion( ).getId( ) );
532         assertEquals( "1.3-20070725.210059-1", artifact.getArtifactVersion( ) );
533         assertEquals( ".pom.md5", artifact.getRemainder( ) );
534         assertEquals( "axis2", artifact.getId( ) );
535         assertEquals( "axis2", artifact.getVersion( ).getProject( ).getId( ) );
536         assertEquals( "org.apache.axis2", artifact.getVersion( ).getProject( ).getNamespace( ).getId( ) );
537         assertEquals( "", artifact.getClassifier( ) );
538         assertEquals( "md5", artifact.getType( ) );
539
540
541         artifact = null;
542         artifact = results.stream( ).filter( a -> a.getFileName( ).equals( "maven-metadata.xml" ) )
543             .findFirst( ).get( );
544         assertNotNull( artifact );
545         assertEquals( BaseArtifactTypes.METADATA, artifact.getDataType( ) );
546         assertEquals( "1.3-SNAPSHOT", artifact.getVersion( ).getId( ) );
547         assertEquals( "xml", artifact.getExtension( ) );
548     }
549
550     @Test
551     public void testGetArtifactListWithArtifactSelector1() {
552         ItemSelector selector = ArchivaItemSelector.builder( )
553             .withNamespace( "org.apache.axis2" )
554             .withProjectId( "axis2" )
555             .withVersion( "1.3-SNAPSHOT" )
556             .withArtifactVersion( "1.3-20070731.113304-21" )
557             .withExtension( "pom" )
558             .build( );
559         List<? extends Artifact> results = repoContent.getArtifacts( selector );
560         checkArtifactListWithArtifactSelector1( results );
561     }
562
563     @Test
564     public void testGetArtifactStreamWithArtifactSelector1() {
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         try(Stream<? extends Artifact> results = repoContent.newArtifactStream( selector ))
573         {
574             checkArtifactListWithArtifactSelector1( results.collect( Collectors.toList()) );
575         }
576     }
577
578     private void checkArtifactListWithArtifactSelector1( List<? extends Artifact> results )
579     {
580         assertNotNull( results );
581         assertEquals( 1, results.size( ) );
582         Artifact artifact = results.get( 0 );
583         assertEquals( "pom", artifact.getExtension( ) );
584         assertEquals( BaseArtifactTypes.MAIN, artifact.getDataType( ) );
585     }
586
587     @Test
588     public void testGetArtifactListWithArtifactSelector2() {
589         ItemSelector selector = ArchivaItemSelector.builder( )
590             .withNamespace( "org.apache.axis2" )
591             .withProjectId( "axis2" )
592             .withVersion( "1.3-SNAPSHOT" )
593             .withArtifactVersion( "1.3-20070731.113304-21" )
594             .withExtension( "pom" )
595             .includeRelatedArtifacts()
596             .build( );
597         List<? extends Artifact> results = repoContent.getArtifacts( selector );
598
599         checkArtifactListWithArtifactSelector2( results );
600
601     }
602
603     @Test
604     public void testGetArtifactStreamWithArtifactSelector2() {
605         ItemSelector selector = ArchivaItemSelector.builder( )
606             .withNamespace( "org.apache.axis2" )
607             .withProjectId( "axis2" )
608             .withVersion( "1.3-SNAPSHOT" )
609             .withArtifactVersion( "1.3-20070731.113304-21" )
610             .withExtension( "pom" )
611             .includeRelatedArtifacts()
612             .build( );
613         try(Stream<? extends Artifact> results = repoContent.newArtifactStream( selector ))
614         {
615             checkArtifactListWithArtifactSelector2( results.collect( Collectors.toList()) );
616         }
617     }
618
619     private void checkArtifactListWithArtifactSelector2( List<? extends Artifact> results )
620     {
621         assertNotNull( results );
622         assertEquals( 3, results.size( ) );
623         Artifact artifact = results.stream( ).filter( a -> a.getFileName( ).equalsIgnoreCase( "axis2-1.3-20070731.113304-21.pom" ) )
624             .findFirst( ).get( );
625         assertNotNull( artifact );
626         assertEquals( "pom", artifact.getExtension( ) );
627         assertEquals( BaseArtifactTypes.MAIN, artifact.getDataType( ) );
628
629         artifact = results.stream( ).filter( a -> a.getFileName( ).equalsIgnoreCase( "axis2-1.3-20070731.113304-21.pom.sha1" ) )
630             .findFirst( ).get( );
631         assertNotNull( artifact );
632         assertEquals( "sha1", artifact.getExtension( ) );
633         assertEquals( BaseArtifactTypes.RELATED, artifact.getDataType( ) );
634     }
635
636
637     @Test
638     public void testArtifactListWithProjectSelector() {
639         ItemSelector selector = ArchivaItemSelector.builder( )
640             .withNamespace( "org.apache.maven.shared" )
641             .withProjectId( "maven-downloader" )
642             .build( );
643         List<? extends Artifact> results = repoContent.getArtifacts( selector );
644         checkArtifactListWithProjectSelector( results );
645
646     }
647
648     @Test
649     public void testArtifactStreamWithProjectSelector() {
650         ItemSelector selector = ArchivaItemSelector.builder( )
651             .withNamespace( "org.apache.maven.shared" )
652             .withProjectId( "maven-downloader" )
653             .build( );
654         Stream<? extends Artifact> results = repoContent.newArtifactStream( selector );
655         checkArtifactListWithProjectSelector( results.collect( Collectors.toList()) );
656
657     }
658
659     private void checkArtifactListWithProjectSelector( List<? extends Artifact> results )
660     {
661         assertNotNull( results );
662         assertEquals( 27, results.size( ) );
663
664         Artifact artifact = results.stream( ).filter( a -> a.getFileName( ).equalsIgnoreCase( "maven-metadata.xml" ) )
665             .findFirst( ).get( );
666         assertNotNull( artifact );
667         assertEquals( "xml", artifact.getExtension( ) );
668         assertEquals( BaseArtifactTypes.METADATA, artifact.getDataType( ) );
669
670         artifact = results.stream( ).filter( a -> a.getFileName( ).equalsIgnoreCase( "maven-downloader-1.0-sources.jar" ) )
671             .findFirst( ).get( );
672         assertNotNull( artifact );
673         assertEquals( BaseArtifactTypes.MAIN, artifact.getDataType( ) );
674         assertEquals( "sources", artifact.getClassifier( ) );
675         assertEquals( "java-source", artifact.getType( ) );
676
677         artifact = results.stream( ).filter( a -> a.getFileName( ).equalsIgnoreCase( "maven-downloader-1.0-sources.jar.sha1" ) )
678             .findFirst( ).get( );
679         assertNotNull( artifact );
680         assertEquals( BaseArtifactTypes.RELATED, artifact.getDataType( ) );
681         assertEquals( "sources", artifact.getClassifier( ) );
682         assertEquals( "sha1", artifact.getType( ) );
683         assertEquals( ".jar.sha1", artifact.getRemainder( ) );
684     }
685
686     @Test
687     public void testArtifactListWithNamespaceSelector() {
688         ItemSelector selector = ArchivaItemSelector.builder( )
689             .withNamespace( "org.multilevel" )
690             .build( );
691         List<? extends Artifact> results = repoContent.getArtifacts( selector );
692         assertNotNull( results );
693         assertEquals( 3, results.size( ) );
694         assertTrue( results.get( 0 ).getFileName( ).startsWith( "testproj1" ) );
695     }
696
697     @Test
698     public void testArtifactListWithNamespaceSelectorRecursive() {
699         ItemSelector selector = ArchivaItemSelector.builder( )
700             .withNamespace( "org.multilevel" )
701             .recurse()
702             .build( );
703         List<? extends Artifact> results = repoContent.getArtifacts( selector );
704         checkArtifactListWithNamespaceSelectorRecursive( results );
705     }
706
707     @Test
708     public void testArtifactStreamWithNamespaceSelectorRecursive() {
709         ItemSelector selector = ArchivaItemSelector.builder( )
710             .withNamespace( "org.multilevel" )
711             .recurse()
712             .build( );
713         Stream<? extends Artifact> results = repoContent.newArtifactStream( selector );
714         checkArtifactListWithNamespaceSelectorRecursive( results.collect( Collectors.toList()) );
715     }
716
717     private void checkArtifactListWithNamespaceSelectorRecursive( List<? extends Artifact> results )
718     {
719         assertNotNull( results );
720         assertEquals( 6, results.size( ) );
721
722         Artifact artifact = results.stream( ).filter( a -> a.getFileName( ).equalsIgnoreCase( "testproj2-1.0.pom" ) )
723             .findFirst( ).get( );
724         assertNotNull( artifact );
725         assertEquals( 6, artifact.getAsset( ).getParent( ).getPath( ).split( "/" ).length );
726
727         artifact = results.stream( ).filter( a -> a.getFileName( ).equalsIgnoreCase( "testproj1-1.0.pom" ) )
728             .findFirst( ).get( );
729         assertNotNull( artifact );
730         assertEquals( 5, artifact.getAsset( ).getParent( ).getPath( ).split( "/" ).length );
731     }
732
733
734     @Test
735     public void testArtifactListWithArtifactSelector1() {
736         ItemSelector selector = ArchivaItemSelector.builder( )
737             .withNamespace( "org.apache.maven" )
738             .withProjectId( "test" )
739             .withVersion( "1.0-SNAPSHOT" )
740             .withArtifactId( "test" )
741             .withArtifactVersion( "1.0-20050611.112233-1" )
742             .build( );
743
744         List<? extends Artifact> results = repoContent.getArtifacts( selector );
745
746         assertNotNull( results );
747         assertEquals( 1, results.size( ) );
748
749         Artifact artifact = results.stream( ).filter( a -> a.getFileName( ).equalsIgnoreCase( "test-1.0-20050611.112233-1.jar" ) )
750             .findFirst().get();
751         assertNotNull( artifact );
752         assertEquals( "", artifact.getClassifier( ) );
753     }
754
755     @Test
756     public void testArtifactListWithArtifactSelector2() {
757         ItemSelector selector = ArchivaItemSelector.builder( )
758             .withNamespace( "org.apache.maven" )
759             .withProjectId( "test" )
760             .withVersion( "1.0-SNAPSHOT" )
761             .withClassifier( "*" )
762             .withArtifactId( "test" )
763             .withArtifactVersion( "1.0-20050611.112233-1" )
764             .build( );
765
766         List<? extends Artifact> results = repoContent.getArtifacts( selector );
767
768         assertNotNull( results );
769         assertEquals( 2, results.size( ) );
770
771         Artifact artifact = results.stream( ).filter( a -> a.getFileName( ).equalsIgnoreCase( "test-1.0-20050611.112233-1-javadoc.jar" ) )
772             .findFirst().get();
773         assertNotNull( artifact );
774         assertEquals( "javadoc", artifact.getClassifier( ) );
775         assertEquals( "javadoc", artifact.getType( ) );
776     }
777
778     @Test
779     public void testArtifactListWithArtifactSelector3() {
780         ItemSelector selector = ArchivaItemSelector.builder( )
781             .withNamespace( "org.apache.maven" )
782             .withProjectId( "test" )
783             .withVersion( "1.0-SNAPSHOT" )
784             .withClassifier( "*" )
785             .withArtifactVersion( "1.0-20050611.112233-1" )
786             .build( );
787
788         List<? extends Artifact> results = repoContent.getArtifacts( selector );
789
790         assertNotNull( results );
791         assertEquals( 3, results.size( ) );
792
793         Artifact artifact = results.stream( ).filter( a -> a.getFileName( ).equalsIgnoreCase( "test-1.0-20050611.112233-1-javadoc.jar" ) )
794             .findFirst().get();
795         assertNotNull( artifact );
796         assertEquals( "javadoc", artifact.getClassifier( ) );
797         assertEquals( "javadoc", artifact.getType( ) );
798
799         artifact = results.stream( ).filter( a -> a.getFileName( ).equalsIgnoreCase( "wrong-artifactId-1.0-20050611.112233-1.jar" ) )
800             .findFirst().get();
801         assertNotNull( artifact );
802         assertEquals( "", artifact.getClassifier( ) );
803         assertEquals( "wrong-artifactId", artifact.getId( ) );
804     }
805
806     @Test
807     public void testArtifactListWithArtifactSelector4() {
808         ItemSelector selector = ArchivaItemSelector.builder( )
809             .withNamespace( "org.apache.maven" )
810             .withProjectId( "test" )
811             .withVersion( "1.0-SNAPSHOT" )
812             .withClassifier( "" )
813             .build( );
814
815         List<? extends Artifact> results = repoContent.getArtifacts( selector );
816
817         assertNotNull( results );
818         assertEquals( 5, results.size( ) );
819
820         Artifact artifact = results.stream( ).filter( a -> a.getFileName( ).equalsIgnoreCase( "test-1.0-20050611.112233-1-javadoc.jar" ) )
821             .findFirst().get();
822         assertNotNull( artifact );
823         assertEquals( "javadoc", artifact.getClassifier( ) );
824         assertEquals( "javadoc", artifact.getType( ) );
825
826         artifact = results.stream( ).filter( a -> a.getFileName( ).equalsIgnoreCase( "wrong-artifactId-1.0-20050611.112233-1.jar" ) )
827             .findFirst().get();
828         assertNotNull( artifact );
829         assertEquals( "", artifact.getClassifier( ) );
830         assertEquals( "wrong-artifactId", artifact.getId( ) );
831
832         artifact = results.stream( ).filter( a -> a.getFileName( ).equalsIgnoreCase( "wrong-artifactId-1.0-20050611.1122x-1.jar" ) )
833             .findFirst().get();
834         assertNotNull( artifact );
835         assertEquals( "", artifact.getClassifier( ) );
836         assertEquals( "wrong-artifactId", artifact.getId( ) );
837         assertEquals( "", artifact.getArtifactVersion( ) );
838
839         artifact = results.stream( ).filter( a -> a.getFileName( ).equalsIgnoreCase( "test-1.0-20050611.1122x-1.jar" ) )
840             .findFirst().get();
841         assertNotNull( artifact );
842         assertEquals( "", artifact.getClassifier( ) );
843         assertEquals( "test", artifact.getId( ) );
844         assertEquals( "1.0-20050611.1122x-1", artifact.getArtifactVersion( ) );
845
846     }
847
848     @Test
849     public void testArtifactListWithArtifactSelectorWithClassifier() {
850         ItemSelector selector = ArchivaItemSelector.builder( )
851             .withNamespace( "org.apache.maven" )
852             .withProjectId( "test" )
853             .withVersion( "1.0-SNAPSHOT" )
854             .withArtifactId( "test" )
855             .withClassifier( "javadoc" )
856             .withArtifactVersion( "1.0-20050611.112233-1" )
857             .build( );
858
859         List<? extends Artifact> results = repoContent.getArtifacts( selector );
860
861         assertNotNull( results );
862         assertEquals( 1, results.size( ) );
863
864         Artifact artifact = results.stream( ).filter( a -> a.getFileName( ).equalsIgnoreCase( "test-1.0-20050611.112233-1-javadoc.jar" ) )
865             .findFirst().get();
866         assertNotNull( artifact );
867         assertEquals( "javadoc", artifact.getClassifier( ) );
868         assertEquals( "javadoc", artifact.getType( ) );
869     }
870
871     @Test
872     public void testArtifactListWithArtifactSelectorWrongArtifact() {
873         ItemSelector selector = ArchivaItemSelector.builder( )
874             .withNamespace( "org.apache.maven" )
875             .withProjectId( "test" )
876             .withVersion( "1.0-SNAPSHOT" )
877             .withArtifactId( "wrong-artifactId" )
878             .withArtifactVersion( "1.0-20050611.112233-1" )
879             .build( );
880
881         List<? extends Artifact> results = repoContent.getArtifacts( selector );
882
883         assertNotNull( results );
884         assertEquals( 1, results.size( ) );
885
886         Artifact artifact = results.stream( ).filter( a -> a.getFileName( ).equalsIgnoreCase( "wrong-artifactId-1.0-20050611.112233-1.jar" ) )
887             .findFirst().get();
888         assertNotNull( artifact );
889     }
890
891     @Test
892     public void testArtifactListWithArtifactSelectorVersionPattern() {
893         ItemSelector selector = ArchivaItemSelector.builder( )
894             .withNamespace( "org.apache.maven" )
895             .withProjectId( "test" )
896             .withVersion( "1.0-SNAPSHOT" )
897             .withArtifactVersion( "1.0-*" )
898             .build( );
899
900         List<? extends Artifact> results = repoContent.getArtifacts( selector );
901
902         assertNotNull( results );
903         assertEquals( 5, results.size( ) );
904
905         Artifact artifact = results.stream( ).filter( a -> a.getFileName( ).equalsIgnoreCase( "wrong-artifactId-1.0-20050611.112233-1.jar" ) )
906             .findFirst().get();
907         assertNotNull( artifact );
908     }
909
910
911     @Test
912     public void testNewItemStreamWithNamespace1() {
913         ItemSelector selector = ArchivaItemSelector.builder( )
914             .withNamespace( "org.apache.axis2" )
915             .build();
916
917         Stream<? extends ContentItem> stream = repoContent.newItemStream( selector, false );
918         List<? extends ContentItem> result = stream.collect( Collectors.toList( ) );
919         assertEquals( 41, result.size( ) );
920         ContentItem item = result.get( 39 );
921         Version version = item.adapt( Version.class );
922         assertNotNull( version );
923         assertEquals( "1.3-SNAPSHOT", version.getId( ) );
924         Project project = result.get( 40 ).adapt( Project.class );
925         assertNotNull( project );
926         assertEquals( "axis2", project.getId( ) );
927         assertTrue( result.stream( ).anyMatch( a -> "axis2-1.3-20070725.210059-1.pom".equals( a.getAsset( ).getName( ) ) ) );
928     }
929
930     @Test
931     public void testNewItemStreamWithNamespace2() {
932         ItemSelector selector = ArchivaItemSelector.builder( )
933             .withNamespace( "org.apache.maven" )
934             .recurse()
935             .build();
936
937         Stream<? extends ContentItem> stream = repoContent.newItemStream( selector, false );
938         List<? extends ContentItem> result = stream.collect( Collectors.toList( ) );
939         assertEquals( 170, result.size( ) );
940         assertEquals( 92, result.stream( ).filter( a -> a instanceof DataItem ).count( ) );
941     }
942
943     @Test
944     public void testGetArtifactFromContentItem() {
945         ItemSelector selector = ArchivaItemSelector.builder( )
946             .withNamespace( "org.apache.maven" ).build();
947         Namespace ns = repoContent.getNamespace( selector );
948         List<? extends Artifact> artifacts = repoContent.getArtifacts( ns );
949         assertNotNull( artifacts );
950         assertEquals( 39, artifacts.size( ) );
951         List<? extends Artifact> artifacts2 = repoContent.getArtifacts( (ContentItem)ns );
952         assertArrayEquals( artifacts.toArray(), artifacts2.toArray() );
953
954         selector = ArchivaItemSelector.builder( )
955             .withNamespace( "org.apache.maven.shared" )
956             .withProjectId( "maven-downloader" )
957             .build();
958         Project project = repoContent.getProject( selector );
959         artifacts = repoContent.getArtifacts( project );
960         assertNotNull( artifacts );
961         assertEquals( 27, artifacts.size( ) );
962         artifacts2 = repoContent.getArtifacts( (ContentItem)project );
963         assertArrayEquals( artifacts.toArray(), artifacts2.toArray() );
964
965         selector = ArchivaItemSelector.builder( )
966             .withNamespace( "org.apache.maven.shared" )
967             .withProjectId( "maven-downloader" )
968             .withVersion( "1.1" )
969             .build( );
970         Version version = repoContent.getVersion( selector );
971         artifacts = repoContent.getArtifacts( version );
972         assertNotNull( artifacts );
973         assertEquals( 12, artifacts.size( ) );
974         artifacts2 = repoContent.getArtifacts( (ContentItem)version );
975         assertArrayEquals( artifacts.toArray(), artifacts2.toArray() );
976
977     }
978
979     @Test
980     public void testGetRelatedArtifactsFromArtifact() {
981         ItemSelector selector = ArchivaItemSelector.builder( )
982             .withNamespace( "org.apache.maven.shared" )
983             .withProjectId( "maven-downloader" )
984             .withVersion( "1.1" )
985             .withExtension( "jar" )
986             .withArtifactId( "maven-downloader" ).build( );
987
988         Artifact artifact = repoContent.getArtifact( selector );
989         assertNotNull( artifact );
990         List<? extends Artifact> artifacts = repoContent.getArtifacts( artifact );
991         assertNotNull( artifacts );
992         assertEquals( 2, artifacts.size( ) );
993
994     }
995
996     @Test
997     public void testToItemFromPath() throws LayoutException
998     {
999         String path = "/org/apache/maven/shared";
1000         ContentItem item = repoContent.toItem( path );
1001         assertNotNull( item );
1002         assertTrue( item instanceof ArchivaContentItem );
1003
1004         path = "/org/apache/maven/shared/maven-downloader";
1005         item = repoContent.toItem( path );
1006         assertNotNull( item );
1007         assertTrue( item instanceof ArchivaContentItem );
1008
1009         path = "/org/apache/maven/shared/maven-downloader/1.1";
1010         item = repoContent.toItem( path );
1011         assertNotNull( item );
1012         assertTrue( item instanceof ArchivaContentItem );
1013
1014         path = "/org/apache/maven/shared/maven-downloader/1.1/maven-downloader-1.1.jar";
1015         item = repoContent.toItem( path );
1016         assertNotNull( item );
1017         assertTrue( item instanceof DataItem );
1018
1019     }
1020
1021     @Test
1022     public void testToItemFromAssetPath() throws LayoutException
1023     {
1024         StorageAsset path = repoContent.getRepository().getAsset("/org/apache/maven/shared");
1025         ContentItem item = repoContent.toItem( path );
1026         assertNotNull( item );
1027         assertTrue( item instanceof ArchivaContentItem );
1028
1029         path = repoContent.getRepository( ).getAsset( "/org/apache/maven/shared/maven-downloader" );
1030         item = repoContent.toItem( path );
1031         assertNotNull( item );
1032         assertTrue( item instanceof ArchivaContentItem );
1033
1034         path = repoContent.getRepository( ).getAsset( "/org/apache/maven/shared/maven-downloader/1.1" );
1035         item = repoContent.toItem( path );
1036         assertNotNull( item );
1037         assertTrue( item instanceof ArchivaContentItem );
1038
1039         path = repoContent.getRepository( ).getAsset( "/org/apache/maven/shared/maven-downloader/1.1/maven-downloader-1.1.jar" );
1040         item = repoContent.toItem( path );
1041         assertNotNull( item );
1042         assertTrue( item instanceof DataItem );
1043
1044     }
1045
1046     @Test
1047     public void testHasContent() throws LayoutException
1048     {
1049         ItemSelector selector = ArchivaItemSelector.builder( )
1050             .withNamespace( "org.apache.maven.shared" )
1051             .withProjectId( "maven-downloader" )
1052             .withVersion( "1.1" )
1053             .withArtifactId( "maven-downloader" )
1054             .withExtension( "jar" )
1055             .build();
1056
1057         assertTrue( repoContent.hasContent( selector ) );
1058
1059         selector = ArchivaItemSelector.builder( )
1060             .withNamespace( "org.apache.maven.shared" )
1061             .withProjectId( "maven-downloader" )
1062             .withVersion( "1.1" )
1063             .withArtifactId( "maven-downloader" )
1064             .withExtension( "zip" )
1065             .build();
1066
1067         assertFalse( repoContent.hasContent( selector ) );
1068
1069     }
1070
1071     @Test
1072     public void testGetItemWithNamespaceSelector() {
1073         ItemSelector selector = ArchivaItemSelector.builder( )
1074             .withNamespace( "org.apache.maven" )
1075             .build( );
1076         ContentItem item = repoContent.getItem( selector );
1077         assertNotNull( item );
1078         assertTrue( item instanceof Namespace );
1079     }
1080
1081     @Test
1082     public void testGetItemWithProjectSelector() {
1083         ItemSelector selector = ArchivaItemSelector.builder( )
1084             .withNamespace( "org.apache.maven" )
1085             .withProjectId( "shared" )
1086             .build( );
1087         ContentItem item = repoContent.getItem( selector );
1088         assertNotNull( item );
1089         assertTrue( item instanceof Project );
1090     }
1091
1092     @Test
1093     public void testGetItemWithVersionSelector() {
1094         ItemSelector selector = ArchivaItemSelector.builder( )
1095             .withNamespace( "org.apache.maven" )
1096             .withProjectId( "samplejar" )
1097             .withVersion("2.0")
1098             .build( );
1099         ContentItem item = repoContent.getItem( selector );
1100         assertNotNull( item );
1101         assertTrue( item instanceof Version );
1102     }
1103
1104     @Test
1105     public void testGetItemWithArtifactSelector() {
1106         ItemSelector selector = ArchivaItemSelector.builder( )
1107             .withNamespace( "org.apache.maven" )
1108             .withProjectId( "samplejar" )
1109             .withVersion("2.0")
1110             .withArtifactId( "samplejar" )
1111             .build( );
1112         ContentItem item = repoContent.getItem( selector );
1113         assertNotNull( item );
1114         assertTrue( item instanceof Artifact );
1115     }
1116
1117     @Test
1118     public void testGetNamespaceFromPath() throws LayoutException
1119     {
1120         StorageAsset path = repoContent.getRepository( ).getAsset( "/org/apache/axis2" );
1121         Namespace ns = repoContent.getNamespaceFromPath( path );
1122         assertNotNull( ns );
1123         assertEquals( "org.apache.axis2", ns.getId( ) );
1124
1125     }
1126
1127     @Test
1128     public void testArtifactListWithArtifactSelectorAndRelated() {
1129         ItemSelector selector = ArchivaItemSelector.builder( )
1130             .withNamespace( "org.apache.maven" )
1131             .withProjectId( "samplejar" )
1132             .withVersion( "1.0" )
1133             .withArtifactVersion( "1.0" )
1134             .withArtifactId( "samplejar" )
1135             .withExtension( "jar" )
1136             .includeRelatedArtifacts()
1137             .build( );
1138
1139         List<? extends Artifact> results = repoContent.getArtifacts( selector );
1140
1141         assertNotNull( results );
1142         assertEquals( 3, results.size( ) );
1143
1144         Artifact artifact = results.stream( ).filter( a -> a.getFileName( ).equalsIgnoreCase( "samplejar-1.0.jar" ) )
1145             .findFirst().get();
1146         assertNotNull( artifact );
1147         assertEquals( BaseArtifactTypes.MAIN, artifact.getDataType( ) );
1148
1149         artifact = results.stream( ).filter( a -> a.getFileName( ).equalsIgnoreCase( "samplejar-1.0.jar.md5" ) )
1150             .findFirst().get();
1151         assertNotNull( artifact );
1152         assertEquals( BaseArtifactTypes.RELATED, artifact.getDataType( ) );
1153         assertEquals( "md5", artifact.getExtension( ) );
1154
1155         artifact = results.stream( ).filter( a -> a.getFileName( ).equalsIgnoreCase( "samplejar-1.0.jar.sha1" ) )
1156             .findFirst().get();
1157         assertNotNull( artifact );
1158         assertEquals( BaseArtifactTypes.RELATED, artifact.getDataType( ) );
1159         assertEquals( "sha1", artifact.getExtension( ) );
1160
1161     }
1162
1163     private Path copyRepository(String repoName) throws IOException, URISyntaxException
1164     {
1165         Path tempDir = Files.createTempDirectory( "archiva-repocontent" );
1166         Path repoSource = Paths.get( Thread.currentThread( ).getContextClassLoader( ).getResource( "repositories/" + repoName ).toURI( ) );
1167         assertTrue( Files.exists( repoSource ) );
1168         FileUtils.copyDirectory( repoSource.toFile( ), tempDir.toFile() );
1169         return tempDir;
1170     }
1171
1172     private ManagedRepository createManagedRepoWithContent(String sourceRepoName) throws IOException, URISyntaxException
1173     {
1174         Path repoDir = copyRepository( sourceRepoName );
1175         MavenManagedRepository repo = createRepository( sourceRepoName, sourceRepoName, repoDir );
1176         ManagedDefaultRepositoryContent deleteRepoContent = new ManagedDefaultRepositoryContent( repo, artifactMappingProviders, fileTypes, fileLockManager );
1177         deleteRepoContent.setMavenContentHelper( contentHelper );
1178         return repo;
1179     }
1180
1181     @Test
1182     public void deleteNamespaceItem() throws IOException, URISyntaxException, ItemNotFoundException
1183     {
1184         ManagedRepository repo = createManagedRepoWithContent( "delete-repository" );
1185         ManagedRepositoryContent myRepoContent = repo.getContent( );
1186         Path repoRoot = repo.getRoot().getFilePath( );
1187         assertTrue( Files.exists(repoRoot.resolve( "org/apache/maven" )) );
1188         ArchivaItemSelector selector = ArchivaItemSelector.builder( )
1189             .withNamespace( "org.apache.maven" ).build();
1190         ContentItem item = myRepoContent.getItem( selector );
1191         assertTrue( item instanceof Namespace );
1192         myRepoContent.deleteItem( item );
1193         assertFalse( Files.exists(repoRoot.resolve( "org/apache/maven" )) );
1194         assertTrue( Files.exists(repoRoot.resolve( "org/apache" )) );
1195
1196         // Sub namespaces are deleted too
1197         assertTrue( Files.exists(repoRoot.resolve( "org/apache/test/samplejar" )) );
1198         assertTrue( Files.exists(repoRoot.resolve( "org/apache/test/sub/samplejar" )) );
1199         selector = ArchivaItemSelector.builder( )
1200             .withNamespace( "org.apache.test" ).build();
1201         item = myRepoContent.getItem( selector );
1202         assertTrue( item instanceof Namespace );
1203         myRepoContent.deleteItem( item );
1204         assertFalse( Files.exists(repoRoot.resolve( "org/apache/test/samplejar" )) );
1205         assertFalse( Files.exists(repoRoot.resolve( "org/apache/test/sub/samplejar" )) );
1206     }
1207
1208     @Test
1209     public void deleteProjectItem() throws IOException, URISyntaxException, ItemNotFoundException
1210     {
1211         ManagedRepository repo = createManagedRepoWithContent( "delete-repository" );
1212         ManagedRepositoryContent myRepoContent = repo.getContent( );
1213         Path repoRoot = repo.getRoot().getFilePath( );
1214         assertTrue( Files.exists(repoRoot.resolve( "org/apache/maven/A" )) );
1215         ArchivaItemSelector selector = ArchivaItemSelector.builder( )
1216             .withNamespace( "org.apache.maven" )
1217             .withProjectId( "A" ).build();
1218         ContentItem item = myRepoContent.getItem( selector );
1219         assertTrue( item instanceof Project );
1220         myRepoContent.deleteItem( item );
1221         assertTrue( Files.exists(repoRoot.resolve( "org/apache/maven" )) );
1222         assertTrue( Files.exists( repoRoot.resolve( "org/apache/maven/samplejar/1.0" ) ) );
1223         assertTrue( Files.exists( repoRoot.resolve( "org/apache/maven/samplejar/2.0" ) ) );
1224         assertFalse( Files.exists(repoRoot.resolve( "org/apache/maven/A" )) );
1225
1226         assertTrue( Files.exists(repoRoot.resolve( "org/apache/test/samplejar" )) );
1227         assertTrue( Files.exists(repoRoot.resolve( "org/apache/test/sub/samplejar" )) );
1228         selector = ArchivaItemSelector.builder( )
1229             .withNamespace( "org.apache.test" )
1230             .withProjectId( "samplejar" ).build();
1231         item = myRepoContent.getItem( selector );
1232         assertTrue( item instanceof Project );
1233         myRepoContent.deleteItem( item );
1234         assertFalse( Files.exists(repoRoot.resolve( "org/apache/test/samplejar" )) );
1235         assertTrue( Files.exists(repoRoot.resolve( "org/apache/test/sub/samplejar" )) );
1236     }
1237
1238     @Test
1239     public void deleteVersionItem() throws IOException, URISyntaxException, ItemNotFoundException
1240     {
1241         ManagedRepository repo = createManagedRepoWithContent( "delete-repository" );
1242         ManagedRepositoryContent myRepoContent = repo.getContent( );
1243         Path repoRoot = repo.getRoot().getFilePath( );
1244         assertTrue( Files.exists(repoRoot.resolve( "org/apache/maven/A/1.0" )) );
1245         ArchivaItemSelector selector = ArchivaItemSelector.builder( )
1246             .withNamespace( "org.apache.maven" )
1247             .withProjectId( "A" )
1248             .withVersion( "1.0" ).build();
1249         ContentItem item = myRepoContent.getItem( selector );
1250         assertTrue( item instanceof Version );
1251         myRepoContent.deleteItem( item );
1252         assertTrue( Files.exists(repoRoot.resolve( "org/apache/maven/A" )) );
1253         assertTrue( Files.exists( repoRoot.resolve( "org/apache/maven/samplejar/1.0" ) ) );
1254         assertTrue( Files.exists( repoRoot.resolve( "org/apache/maven/samplejar/2.0" ) ) );
1255         assertFalse( Files.exists(repoRoot.resolve( "org/apache/maven/A/1.0" )) );
1256
1257         assertTrue( Files.exists(repoRoot.resolve( "org/apache/test/samplejar" )) );
1258         assertTrue( Files.exists(repoRoot.resolve( "org/apache/test/sub/samplejar" )) );
1259         selector = ArchivaItemSelector.builder( )
1260             .withNamespace( "org.apache.test" )
1261             .withProjectId( "samplejar" )
1262             .withVersion( "2.0" ).build();
1263         item = myRepoContent.getItem( selector );
1264         assertTrue( item instanceof Version );
1265         myRepoContent.deleteItem( item );
1266         assertFalse( Files.exists(repoRoot.resolve( "org/apache/test/samplejar/2.0" )) );
1267         assertTrue( Files.exists(repoRoot.resolve( "org/apache/test/samplejar/1.0" )) );
1268         assertTrue( Files.exists(repoRoot.resolve( "org/apache/test/sub/samplejar/1.0" )) );
1269         assertTrue( Files.exists(repoRoot.resolve( "org/apache/test/sub/samplejar/2.0" )) );
1270     }
1271
1272     @Test
1273     public void deleteArtifactItem() throws IOException, URISyntaxException, ItemNotFoundException
1274     {
1275         ManagedRepository repo = createManagedRepoWithContent( "delete-repository" );
1276         ManagedRepositoryContent myRepoContent = repo.getContent( );
1277         Path repoRoot = repo.getRoot().getFilePath( );
1278         assertTrue( Files.exists(repoRoot.resolve( "org/apache/maven/A/1.0/A-1.0.pom" )) );
1279         assertTrue( Files.exists(repoRoot.resolve( "org/apache/maven/A/1.0/A-1.0.war" )) );
1280         ArchivaItemSelector selector = ArchivaItemSelector.builder( )
1281             .withNamespace( "org.apache.maven" )
1282             .withProjectId( "A" )
1283             .withVersion( "1.0" )
1284             .withArtifactId( "A" )
1285             .withArtifactVersion( "1.0" )
1286             .withExtension( "pom" )
1287             .build();
1288         ContentItem item = myRepoContent.getItem( selector );
1289         assertTrue( item instanceof Artifact );
1290         myRepoContent.deleteItem( item );
1291         assertTrue( Files.exists( repoRoot.resolve( "org/apache/maven/samplejar/1.0" ) ) );
1292         assertTrue( Files.exists( repoRoot.resolve( "org/apache/maven/samplejar/2.0" ) ) );
1293         assertFalse( Files.exists(repoRoot.resolve( "org/apache/maven/A/1.0/A-1.0.pom" )) );
1294         assertTrue( Files.exists(repoRoot.resolve( "org/apache/maven/A/1.0/A-1.0.war" )) );
1295
1296
1297         assertTrue( Files.exists(repoRoot.resolve( "org/apache/test/samplejar/1.0/samplejar-1.0.jar" )) );
1298         assertTrue( Files.exists(repoRoot.resolve( "org/apache/test/samplejar/1.0/samplejar-1.0.jar.md5" )) );
1299         assertTrue( Files.exists(repoRoot.resolve( "org/apache/test/samplejar/1.0/samplejar-1.0.jar.sha1" )) );
1300         assertTrue( Files.exists(repoRoot.resolve( "org/apache/test/samplejar/1.0/samplejar-1.0.pom" )) );
1301         assertTrue( Files.exists(repoRoot.resolve( "org/apache/test/samplejar/1.0/samplejar-1.0-source.jar" )) );
1302         selector = ArchivaItemSelector.builder( )
1303             .withNamespace( "org.apache.test" )
1304             .withProjectId( "samplejar" )
1305             .withVersion( "1.0" )
1306             .withArtifactId( "samplejar" )
1307             .withArtifactVersion( "1.0" )
1308             .withExtension( "jar" )
1309             .build();
1310         item = myRepoContent.getItem( selector );
1311         assertTrue( item instanceof Artifact );
1312         myRepoContent.deleteItem( item );
1313         assertFalse( Files.exists(repoRoot.resolve( "org/apache/test/samplejar/1.0/samplejar-1.0.jar" )) );
1314         assertTrue( Files.exists(repoRoot.resolve( "org/apache/test/samplejar/1.0/samplejar-1.0.jar.md5" )) );
1315         assertTrue( Files.exists(repoRoot.resolve( "org/apache/test/samplejar/1.0/samplejar-1.0.jar.sha1" )) );
1316         assertTrue( Files.exists(repoRoot.resolve( "org/apache/test/samplejar/1.0/samplejar-1.0.pom" )) );
1317         assertTrue( Files.exists(repoRoot.resolve( "org/apache/test/samplejar/1.0/samplejar-1.0-source.jar" )) );
1318         assertTrue( Files.exists(repoRoot.resolve( "org/apache/test/samplejar/2.0" )) );
1319         assertTrue( Files.exists(repoRoot.resolve( "org/apache/test/sub/samplejar/1.0" )) );
1320         assertTrue( Files.exists(repoRoot.resolve( "org/apache/test/sub/samplejar/2.0" )) );
1321
1322         selector = ArchivaItemSelector.builder( )
1323             .withNamespace( "org.apache.test" )
1324             .withProjectId( "samplejar" )
1325             .withVersion( "1.0" )
1326             .withArtifactId( "samplejar" )
1327             .withArtifactVersion( "1.0" )
1328             .withClassifier( "source" )
1329             .withExtension( "jar" )
1330             .build();
1331         item = myRepoContent.getItem( selector );
1332         assertTrue( item instanceof Artifact );
1333         myRepoContent.deleteItem( item );
1334         assertFalse( Files.exists(repoRoot.resolve( "org/apache/test/samplejar/1.0/samplejar-1.0.jar" )) );
1335         assertTrue( Files.exists(repoRoot.resolve( "org/apache/test/samplejar/1.0/samplejar-1.0.jar.md5" )) );
1336         assertTrue( Files.exists(repoRoot.resolve( "org/apache/test/samplejar/1.0/samplejar-1.0.jar.sha1" )) );
1337         assertTrue( Files.exists(repoRoot.resolve( "org/apache/test/samplejar/1.0/samplejar-1.0.pom" )) );
1338         assertFalse( Files.exists(repoRoot.resolve( "org/apache/test/samplejar/1.0/samplejar-1.0-source.jar" )) );
1339         assertTrue( Files.exists(repoRoot.resolve( "org/apache/test/samplejar/1.0/samplejar-1.0-source.jar.sha1" )) );
1340         assertTrue( Files.exists(repoRoot.resolve( "org/apache/test/samplejar/2.0" )) );
1341         assertTrue( Files.exists(repoRoot.resolve( "org/apache/test/sub/samplejar/1.0" )) );
1342         assertTrue( Files.exists(repoRoot.resolve( "org/apache/test/sub/samplejar/2.0" )) );
1343
1344         selector = ArchivaItemSelector.builder( )
1345             .withNamespace( "org.apache.test" )
1346             .withProjectId( "samplejar" )
1347             .withVersion( "1.0" )
1348             .withArtifactId( "samplejar" )
1349             .withArtifactVersion( "1.0" )
1350             .withExtension( "jar.md5" )
1351             .build();
1352         item = myRepoContent.getItem( selector );
1353         assertTrue( item instanceof Artifact );
1354         myRepoContent.deleteItem( item );
1355         assertFalse( Files.exists(repoRoot.resolve( "org/apache/test/samplejar/1.0/samplejar-1.0.jar" )) );
1356         assertFalse( Files.exists(repoRoot.resolve( "org/apache/test/samplejar/1.0/samplejar-1.0.jar.md5" )) );
1357         assertTrue( Files.exists(repoRoot.resolve( "org/apache/test/samplejar/1.0/samplejar-1.0.jar.sha1" )) );
1358         assertTrue( Files.exists(repoRoot.resolve( "org/apache/test/samplejar/1.0/samplejar-1.0.pom" )) );
1359         assertFalse( Files.exists(repoRoot.resolve( "org/apache/test/samplejar/1.0/samplejar-1.0-source.jar" )) );
1360         assertTrue( Files.exists(repoRoot.resolve( "org/apache/test/samplejar/1.0/samplejar-1.0-source.jar.sha1" )) );
1361         assertTrue( Files.exists(repoRoot.resolve( "org/apache/test/samplejar/2.0" )) );
1362         assertTrue( Files.exists(repoRoot.resolve( "org/apache/test/sub/samplejar/1.0" )) );
1363         assertTrue( Files.exists(repoRoot.resolve( "org/apache/test/sub/samplejar/2.0" )) );
1364
1365
1366     }
1367
1368     @Test
1369     public void deleteItemNotFound() throws IOException, URISyntaxException, ItemNotFoundException
1370     {
1371         ManagedRepository repo = createManagedRepoWithContent( "delete-repository" );
1372         ManagedRepositoryContent myRepoContent = repo.getContent( );
1373         Path repoRoot = repo.getRoot().getFilePath( );
1374
1375         ArchivaItemSelector selector = ArchivaItemSelector.builder( )
1376             .withNamespace( "org.apache.test2" )
1377             .build( );
1378
1379         ContentItem item = myRepoContent.getItem( selector );
1380         assertTrue( item instanceof Namespace );
1381         try
1382         {
1383             myRepoContent.deleteItem( item );
1384             assertTrue( "ItemNotFoundException expected for non existing namespace", false );
1385         } catch ( ItemNotFoundException e) {
1386         }
1387
1388         selector = ArchivaItemSelector.builder( )
1389             .withNamespace( "org.apache.test" )
1390             .withProjectId( "samplejar2" )
1391             .build( );
1392         item = myRepoContent.getItem( selector );
1393         assertTrue( item instanceof Project );
1394         try
1395         {
1396             myRepoContent.deleteItem( item );
1397             assertTrue( "ItemNotFoundException expected for non existing project", false );
1398         } catch ( ItemNotFoundException e) {
1399         }
1400
1401         selector = ArchivaItemSelector.builder( )
1402             .withNamespace( "org.apache.test" )
1403             .withProjectId( "samplejar" )
1404             .withVersion("1.1")
1405             .build( );
1406         item = myRepoContent.getItem( selector );
1407         assertTrue( item instanceof Version );
1408         try
1409         {
1410             myRepoContent.deleteItem( item );
1411             assertTrue( "ItemNotFoundException expected for non existing version", false );
1412         } catch ( ItemNotFoundException e) {
1413         }
1414
1415         selector = ArchivaItemSelector.builder( )
1416             .withNamespace( "org.apache.test" )
1417             .withProjectId( "samplejar" )
1418             .withVersion("1.0")
1419             .withArtifactId( "samplejar" )
1420             .withArtifactVersion( "1.0" )
1421             .withExtension( "jax" )
1422             .build( );
1423         item = myRepoContent.getItem( selector );
1424         assertTrue( item instanceof Artifact );
1425         try
1426         {
1427             myRepoContent.deleteItem( item );
1428             assertTrue( "ItemNotFoundException expected for non existing artifact", false );
1429         } catch ( ItemNotFoundException e) {
1430         }
1431
1432     }
1433
1434
1435     @Test
1436     public void testAddArtifact() throws IOException, URISyntaxException, LayoutException
1437     {
1438         ManagedRepository repo = createManagedRepoWithContent( "delete-repository" );
1439         ManagedRepositoryContent myRepoContent = repo.getContent( );
1440         BaseRepositoryContentLayout layout = myRepoContent.getLayout( BaseRepositoryContentLayout.class );
1441         Path repoRoot = repo.getRoot().getFilePath( );
1442
1443         Path tmpFile = Files.createTempFile( "archiva-mvn-repotest", "jar" );
1444         try( OutputStream outputStream = Files.newOutputStream( tmpFile ))
1445         {
1446             for ( int i = 0; i < 255; i++ )
1447             {
1448                 outputStream.write( "test.test.test\n".getBytes( Charset.forName( "UTF-8" ) ) );
1449             }
1450         }
1451
1452         Path file = repoRoot.resolve( "org/apache/maven/samplejar/2.0/samplejar-2.0.jar" );
1453         FileTime lmt = Files.getLastModifiedTime( file );
1454         ArchivaItemSelector selector = ArchivaItemSelector.builder( )
1455             .withNamespace( "org.apache.maven" )
1456             .withProjectId( "samplejar" )
1457             .withVersion( "2.0" )
1458             .withArtifactId( "samplejar" )
1459             .withArtifactVersion( "2.0" )
1460             .withExtension( "jar" )
1461             .build( );
1462         Artifact artifact = layout.getArtifact( selector );
1463         layout.addArtifact( tmpFile, artifact );
1464         FileTime lmtAfter = Files.getLastModifiedTime( file );
1465         assertNotEquals( lmtAfter, lmt );
1466         Reader ln = Files.newBufferedReader( file, Charset.forName( "UTF-8" ) );
1467         char[] content = new char[50];
1468         ln.read( content );
1469         assertTrue( new String( content ).startsWith( "test.test.test" ) );
1470
1471         tmpFile = Files.createTempFile( "archiva-mvn-repotest", "jar" );
1472         try( OutputStream outputStream = Files.newOutputStream( tmpFile ))
1473         {
1474             for ( int i = 0; i < 255; i++ )
1475             {
1476                 outputStream.write( "test.test.test\n".getBytes( Charset.forName( "UTF-8" ) ) );
1477             }
1478         }
1479         file = repoRoot.resolve( "org/apache/maven/samplejar/2.0/samplejar-2.0.test" );
1480         assertFalse( Files.exists( file ) );
1481         assertTrue( Files.exists( tmpFile ) );
1482         selector = ArchivaItemSelector.builder( )
1483             .withNamespace( "org.apache.maven" )
1484             .withProjectId( "samplejar" )
1485             .withVersion( "2.0" )
1486             .withArtifactId( "samplejar" )
1487             .withArtifactVersion( "2.0" )
1488             .withExtension( "test" )
1489             .build( );
1490         artifact = layout.getArtifact( selector );
1491         layout.addArtifact( tmpFile, artifact );
1492         ln = Files.newBufferedReader( file, Charset.forName( "UTF-8" ) );
1493         ln.read( content );
1494         assertTrue( new String( content ).startsWith( "test.test.test" ) );
1495     }
1496
1497     @Test
1498     public void getExistingMetadataItem() {
1499         // org/apache/maven/some-ejb/1.0
1500         ArchivaItemSelector versionSelector = ArchivaItemSelector.builder( )
1501             .withNamespace( "org.apache.maven" )
1502             .withProjectId( "some-ejb" )
1503             .withVersion( "1.0" ).build( );
1504         Version version = repoContent.getVersion( versionSelector );
1505         DataItem metaData = repoContent.getMetadataItem( version );
1506         assertTrue( metaData.exists( ) );
1507         assertEquals( "/org/apache/maven/some-ejb/1.0/maven-metadata.xml", metaData.getAsset( ).getPath( ) );
1508     }
1509
1510     @Test
1511     public void getNonExistingMetadataItem() {
1512         // org/apache/maven/some-ejb/1.0
1513         ArchivaItemSelector versionSelector = ArchivaItemSelector.builder( )
1514             .withNamespace( "javax.sql" )
1515             .withProjectId( "jdbc" )
1516             .withVersion( "2.0" ).build( );
1517         Version version = repoContent.getVersion( versionSelector );
1518         DataItem metaData = repoContent.getMetadataItem( version );
1519         assertFalse( metaData.exists( ) );
1520         assertEquals( "/javax/sql/jdbc/2.0/maven-metadata.xml", metaData.getAsset( ).getPath( ) );
1521     }
1522
1523 }