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