]> source.dussan.org Git - archiva.git/blob
5b13edf88fdcf5b33173cd973dcedb030ac15040
[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     @Test
180     public void testExcludeMetadataFile()
181         throws Exception
182     {
183         assertVersions( "include_xml", "1.0", new String[]{ "1.0" } );
184     }
185
186
187     private void assertVersions( String artifactId, String version, String[] expectedVersions )
188         throws Exception
189     {
190         // Use the test metadata-repository, which is already setup for
191         // These kind of version tests.
192         Path repoDir = getRepositoryPath( "metadata-repository" );
193         ((EditableManagedRepository)repoContent.getRepository()).setLocation( repoDir.toAbsolutePath().toUri() );
194
195         // Request the versions.
196
197         // Sort the list (for asserts later)
198         final VersionComparator comparator = new VersionComparator( );
199
200         ItemSelector selector = ArchivaItemSelector.builder( )
201             .withNamespace( "org.apache.archiva.metadata.tests" )
202             .withProjectId( artifactId )
203             .withVersion( version )
204             .build( );
205         List<String> versions = repoContent.getVersions( selector ).stream()
206             .map(v -> v.getVersion()).sorted( comparator ).collect( Collectors.toList());
207         assertArrayEquals( expectedVersions, versions.toArray( ) );
208
209
210     }
211
212     private void assertArtifactVersions( String artifactId, String version, String[] expectedVersions )
213         throws Exception
214     {
215         // Use the test metadata-repository, which is already setup for
216         // These kind of version tests.
217         Path repoDir = getRepositoryPath( "metadata-repository" );
218         ((EditableManagedRepository)repoContent.getRepository()).setLocation( repoDir.toAbsolutePath().toUri() );
219
220         // Request the versions.
221
222         // Sort the list (for asserts later)
223         final VersionComparator comparator = new VersionComparator( );
224
225         ItemSelector selector = ArchivaItemSelector.builder( )
226             .withNamespace( "org.apache.archiva.metadata.tests" )
227             .withProjectId( artifactId )
228             .withVersion( version )
229             .build( );
230         List<String> versions = repoContent.getArtifactVersions( selector ).stream()
231             .sorted( comparator ).collect( Collectors.toList());
232         assertArrayEquals( expectedVersions, versions.toArray( ) );
233
234
235     }
236
237     @Test
238     public void getTestGetProjectWithIllegalArgs() {
239         ItemSelector selector = ArchivaItemSelector.builder( )
240             .withNamespace( "org.apache" )
241             .withVersion( "1.0" )
242             .build();
243         try
244         {
245             repoContent.getProject( selector );
246             assertFalse( "Should throw IllegalArgumentException if no project id is given", true );
247         } catch (IllegalArgumentException e) {
248             // Everything fine
249             assertTrue( e.getMessage( ).contains( "Project id must be set" ) );
250         }
251     }
252
253     @Test
254     public void getTestGetVersionWithIllegalArgs() {
255         ItemSelector selector = ArchivaItemSelector.builder( )
256             .withNamespace( "org.apache.maven" )
257             .withVersion( "1.0" )
258             .build();
259         try
260         {
261             repoContent.getVersion( selector );
262             assertFalse( "Should throw IllegalArgumentException if no project id is given", true );
263         } catch (IllegalArgumentException e) {
264             // Everything fine
265             assertTrue( e.getMessage( ).contains( "Project id must be set" ) );
266         }
267
268
269         selector = ArchivaItemSelector.builder( )
270             .withNamespace( "org.apache.maven" )
271             .withProjectId( "shared" )
272             .build();
273         try
274         {
275             repoContent.getVersion( selector );
276             assertFalse( "Should throw IllegalArgumentException if no version is given", true );
277         } catch (IllegalArgumentException e) {
278             // Everything fine
279             assertTrue( e.getMessage( ).contains( "Version must be set" ) );
280         }
281     }
282
283     @Test
284     public void getTestGetArtifactWithIllegalArgs() {
285         ItemSelector selector = ArchivaItemSelector.builder( )
286             .withNamespace( "org.apache.maven" )
287             .withVersion( "1.0" )
288             .withArtifactId( "shared" )
289             .withArtifactVersion("1.0")
290             .build();
291         try
292         {
293             repoContent.getArtifact( selector );
294             assertFalse( "Should throw IllegalArgumentException if no project id is given", true );
295         } catch (IllegalArgumentException e) {
296             // Everything fine
297             assertTrue( e.getMessage( ).contains( "Project id must be set" ) );
298         }
299
300
301         selector = ArchivaItemSelector.builder( )
302             .withNamespace( "org.apache.maven" )
303             .withProjectId( "shared" )
304             .withArtifactId( "shared" )
305             .withArtifactVersion("1.0")
306             .build();
307         try
308         {
309             repoContent.getArtifact( selector );
310             assertFalse( "Should throw IllegalArgumentException if no version is given", true );
311         } catch (IllegalArgumentException e) {
312             // Everything fine
313             assertTrue( e.getMessage( ).contains( "Version must be set" ) );
314         }
315
316         selector = ArchivaItemSelector.builder( )
317             .withNamespace( "org.apache.maven" )
318             .withProjectId( "shared" )
319             .withVersion("1.0")
320             .withArtifactVersion("1.0")
321             .build();
322         try
323         {
324             repoContent.getArtifact( selector );
325             assertFalse( "Should throw IllegalArgumentException if no artifact id is given", true );
326         } catch (IllegalArgumentException e) {
327             // Everything fine
328             assertTrue( e.getMessage( ).contains( "Artifact id must be set" ) );
329         }
330
331
332     }
333
334     @Test
335     public void testGetProjects() {
336         ItemSelector selector = ArchivaItemSelector.builder( )
337             .withNamespace( "org.apache.maven" ).build();
338         Namespace ns = repoContent.getNamespace( selector );
339         assertNotNull( ns );
340         List<? extends Project> projects = repoContent.getProjects( ns );
341         assertEquals( 12, projects.size( ) );
342         String[] expected = new String[]{
343             "A", "B", "C", "archiva", "discovery", "maven-parent", "samplejar", "shared", "some-ejb", "test",
344             "testing", "update"
345         };
346         Object[] actual = projects.stream( ).map( p -> p.getId( ) ).sorted( ).toArray( );
347         assertArrayEquals( expected, actual);
348     }
349
350     @Test
351     public void testGetProjectsWithSelector() {
352         ItemSelector selector = ArchivaItemSelector.builder( )
353             .withNamespace( "org.apache.maven" ).build();
354         List<? extends Project> projects = repoContent.getProjects( selector );
355         assertEquals( 12, projects.size( ) );
356         String[] expected = new String[]{
357             "A", "B", "C", "archiva", "discovery", "maven-parent", "samplejar", "shared", "some-ejb", "test",
358             "testing", "update"
359         };
360         Object[] actual = projects.stream( ).map( p -> p.getId( ) ).sorted( ).toArray( );
361         assertArrayEquals( expected, actual);
362     }
363
364     @Test
365     public void testGetVersionsWithIllegalSelector() {
366         ItemSelector selector = ArchivaItemSelector.builder( )
367             .withNamespace( "org.apache.maven" ).build();
368         try
369         {
370             List<? extends Version> versions = repoContent.getVersions( selector );
371             assertFalse( "IllegalArgumentException expected, when project id not set", true );
372         } catch (IllegalArgumentException e) {
373             assertEquals( "Project id not set, while retrieving versions.", e.getMessage( ) );
374         }
375     }
376
377     @Test
378     public void testGetVersionsWithSelector() {
379         ItemSelector selector = ArchivaItemSelector.builder( )
380             .withNamespace( "org.apache.maven" )
381             .withProjectId( "samplejar" ).build();
382         List<? extends Version> versions = repoContent.getVersions( selector );
383         assertNotNull( versions );
384         assertEquals( 2, versions.size( ) );
385     }
386
387
388     @Override
389     protected ArtifactReference toArtifactReference( String path )
390         throws LayoutException
391     {
392         return repoContent.toArtifactReference( path );
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( ArtifactReference 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( ).getVersion( ) );
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( ).getNamespace( ) );
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( ).getVersion( ) );
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( ).getNamespace( ) );
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( ).getVersion( ) );
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.getVersion( ) );
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 Artifact );
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() {
1119         StorageAsset path = repoContent.getRepository( ).getAsset( "/org/apache/axis2" );
1120         Namespace ns = repoContent.getNamespaceFromPath( path );
1121         assertNotNull( ns );
1122         assertEquals( "org.apache.axis2", ns.getNamespace( ) );
1123
1124     }
1125
1126     @Test
1127     public void testArtifactListWithArtifactSelectorAndRelated() {
1128         ItemSelector selector = ArchivaItemSelector.builder( )
1129             .withNamespace( "org.apache.maven" )
1130             .withProjectId( "samplejar" )
1131             .withVersion( "1.0" )
1132             .withArtifactVersion( "1.0" )
1133             .withArtifactId( "samplejar" )
1134             .withExtension( "jar" )
1135             .includeRelatedArtifacts()
1136             .build( );
1137
1138         List<? extends Artifact> results = repoContent.getArtifacts( selector );
1139
1140         assertNotNull( results );
1141         assertEquals( 3, results.size( ) );
1142
1143         Artifact artifact = results.stream( ).filter( a -> a.getFileName( ).equalsIgnoreCase( "samplejar-1.0.jar" ) )
1144             .findFirst().get();
1145         assertNotNull( artifact );
1146         assertEquals( BaseArtifactTypes.MAIN, artifact.getDataType( ) );
1147
1148         artifact = results.stream( ).filter( a -> a.getFileName( ).equalsIgnoreCase( "samplejar-1.0.jar.md5" ) )
1149             .findFirst().get();
1150         assertNotNull( artifact );
1151         assertEquals( BaseArtifactTypes.RELATED, artifact.getDataType( ) );
1152         assertEquals( "md5", artifact.getExtension( ) );
1153
1154         artifact = results.stream( ).filter( a -> a.getFileName( ).equalsIgnoreCase( "samplejar-1.0.jar.sha1" ) )
1155             .findFirst().get();
1156         assertNotNull( artifact );
1157         assertEquals( BaseArtifactTypes.RELATED, artifact.getDataType( ) );
1158         assertEquals( "sha1", artifact.getExtension( ) );
1159
1160     }
1161
1162     private Path copyRepository(String repoName) throws IOException, URISyntaxException
1163     {
1164         Path tempDir = Files.createTempDirectory( "archiva-repocontent" );
1165         Path repoSource = Paths.get( Thread.currentThread( ).getContextClassLoader( ).getResource( "repositories/" + repoName ).toURI( ) );
1166         assertTrue( Files.exists( repoSource ) );
1167         FileUtils.copyDirectory( repoSource.toFile( ), tempDir.toFile() );
1168         return tempDir;
1169     }
1170
1171     private ManagedRepository createManagedRepoWithContent(String sourceRepoName) throws IOException, URISyntaxException
1172     {
1173         Path repoDir = copyRepository( sourceRepoName );
1174         MavenManagedRepository repo = createRepository( sourceRepoName, sourceRepoName, repoDir );
1175         ManagedDefaultRepositoryContent deleteRepoContent = new ManagedDefaultRepositoryContent( repo, artifactMappingProviders, fileTypes, fileLockManager );
1176         deleteRepoContent.setMavenContentHelper( contentHelper );
1177         return repo;
1178     }
1179
1180     @Test
1181     public void deleteNamespaceItem() throws IOException, URISyntaxException, ItemNotFoundException
1182     {
1183         ManagedRepository repo = createManagedRepoWithContent( "delete-repository" );
1184         ManagedRepositoryContent myRepoContent = repo.getContent( );
1185         Path repoRoot = repo.getAsset( "" ).getFilePath( );
1186         assertTrue( Files.exists(repoRoot.resolve( "org/apache/maven" )) );
1187         ArchivaItemSelector selector = ArchivaItemSelector.builder( )
1188             .withNamespace( "org.apache.maven" ).build();
1189         ContentItem item = myRepoContent.getItem( selector );
1190         assertTrue( item instanceof Namespace );
1191         myRepoContent.deleteItem( item );
1192         assertFalse( Files.exists(repoRoot.resolve( "org/apache/maven" )) );
1193         assertTrue( Files.exists(repoRoot.resolve( "org/apache" )) );
1194
1195         // Sub namespaces are deleted too
1196         assertTrue( Files.exists(repoRoot.resolve( "org/apache/test/samplejar" )) );
1197         assertTrue( Files.exists(repoRoot.resolve( "org/apache/test/sub/samplejar" )) );
1198         selector = ArchivaItemSelector.builder( )
1199             .withNamespace( "org.apache.test" ).build();
1200         item = myRepoContent.getItem( selector );
1201         assertTrue( item instanceof Namespace );
1202         myRepoContent.deleteItem( item );
1203         assertFalse( Files.exists(repoRoot.resolve( "org/apache/test/samplejar" )) );
1204         assertFalse( Files.exists(repoRoot.resolve( "org/apache/test/sub/samplejar" )) );
1205     }
1206
1207     @Test
1208     public void deleteProjectItem() throws IOException, URISyntaxException, ItemNotFoundException
1209     {
1210         ManagedRepository repo = createManagedRepoWithContent( "delete-repository" );
1211         ManagedRepositoryContent myRepoContent = repo.getContent( );
1212         Path repoRoot = repo.getAsset( "" ).getFilePath( );
1213         assertTrue( Files.exists(repoRoot.resolve( "org/apache/maven/A" )) );
1214         ArchivaItemSelector selector = ArchivaItemSelector.builder( )
1215             .withNamespace( "org.apache.maven" )
1216             .withProjectId( "A" ).build();
1217         ContentItem item = myRepoContent.getItem( selector );
1218         assertTrue( item instanceof Project );
1219         myRepoContent.deleteItem( item );
1220         assertTrue( Files.exists(repoRoot.resolve( "org/apache/maven" )) );
1221         assertTrue( Files.exists( repoRoot.resolve( "org/apache/maven/samplejar/1.0" ) ) );
1222         assertTrue( Files.exists( repoRoot.resolve( "org/apache/maven/samplejar/2.0" ) ) );
1223         assertFalse( Files.exists(repoRoot.resolve( "org/apache/maven/A" )) );
1224
1225         assertTrue( Files.exists(repoRoot.resolve( "org/apache/test/samplejar" )) );
1226         assertTrue( Files.exists(repoRoot.resolve( "org/apache/test/sub/samplejar" )) );
1227         selector = ArchivaItemSelector.builder( )
1228             .withNamespace( "org.apache.test" )
1229             .withProjectId( "samplejar" ).build();
1230         item = myRepoContent.getItem( selector );
1231         assertTrue( item instanceof Project );
1232         myRepoContent.deleteItem( item );
1233         assertFalse( Files.exists(repoRoot.resolve( "org/apache/test/samplejar" )) );
1234         assertTrue( Files.exists(repoRoot.resolve( "org/apache/test/sub/samplejar" )) );
1235     }
1236
1237     @Test
1238     public void deleteVersionItem() throws IOException, URISyntaxException, ItemNotFoundException
1239     {
1240         ManagedRepository repo = createManagedRepoWithContent( "delete-repository" );
1241         ManagedRepositoryContent myRepoContent = repo.getContent( );
1242         Path repoRoot = repo.getAsset( "" ).getFilePath( );
1243         assertTrue( Files.exists(repoRoot.resolve( "org/apache/maven/A/1.0" )) );
1244         ArchivaItemSelector selector = ArchivaItemSelector.builder( )
1245             .withNamespace( "org.apache.maven" )
1246             .withProjectId( "A" )
1247             .withVersion( "1.0" ).build();
1248         ContentItem item = myRepoContent.getItem( selector );
1249         assertTrue( item instanceof Version );
1250         myRepoContent.deleteItem( item );
1251         assertTrue( Files.exists(repoRoot.resolve( "org/apache/maven/A" )) );
1252         assertTrue( Files.exists( repoRoot.resolve( "org/apache/maven/samplejar/1.0" ) ) );
1253         assertTrue( Files.exists( repoRoot.resolve( "org/apache/maven/samplejar/2.0" ) ) );
1254         assertFalse( Files.exists(repoRoot.resolve( "org/apache/maven/A/1.0" )) );
1255
1256         assertTrue( Files.exists(repoRoot.resolve( "org/apache/test/samplejar" )) );
1257         assertTrue( Files.exists(repoRoot.resolve( "org/apache/test/sub/samplejar" )) );
1258         selector = ArchivaItemSelector.builder( )
1259             .withNamespace( "org.apache.test" )
1260             .withProjectId( "samplejar" )
1261             .withVersion( "2.0" ).build();
1262         item = myRepoContent.getItem( selector );
1263         assertTrue( item instanceof Version );
1264         myRepoContent.deleteItem( item );
1265         assertFalse( Files.exists(repoRoot.resolve( "org/apache/test/samplejar/2.0" )) );
1266         assertTrue( Files.exists(repoRoot.resolve( "org/apache/test/samplejar/1.0" )) );
1267         assertTrue( Files.exists(repoRoot.resolve( "org/apache/test/sub/samplejar/1.0" )) );
1268         assertTrue( Files.exists(repoRoot.resolve( "org/apache/test/sub/samplejar/2.0" )) );
1269     }
1270
1271     @Test
1272     public void deleteArtifactItem() throws IOException, URISyntaxException, ItemNotFoundException
1273     {
1274         ManagedRepository repo = createManagedRepoWithContent( "delete-repository" );
1275         ManagedRepositoryContent myRepoContent = repo.getContent( );
1276         Path repoRoot = repo.getAsset( "" ).getFilePath( );
1277         assertTrue( 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         ArchivaItemSelector selector = ArchivaItemSelector.builder( )
1280             .withNamespace( "org.apache.maven" )
1281             .withProjectId( "A" )
1282             .withVersion( "1.0" )
1283             .withArtifactId( "A" )
1284             .withArtifactVersion( "1.0" )
1285             .withExtension( "pom" )
1286             .build();
1287         ContentItem item = myRepoContent.getItem( selector );
1288         assertTrue( item instanceof Artifact );
1289         myRepoContent.deleteItem( item );
1290         assertTrue( Files.exists( repoRoot.resolve( "org/apache/maven/samplejar/1.0" ) ) );
1291         assertTrue( Files.exists( repoRoot.resolve( "org/apache/maven/samplejar/2.0" ) ) );
1292         assertFalse( Files.exists(repoRoot.resolve( "org/apache/maven/A/1.0/A-1.0.pom" )) );
1293         assertTrue( Files.exists(repoRoot.resolve( "org/apache/maven/A/1.0/A-1.0.war" )) );
1294
1295
1296         assertTrue( Files.exists(repoRoot.resolve( "org/apache/test/samplejar/1.0/samplejar-1.0.jar" )) );
1297         assertTrue( Files.exists(repoRoot.resolve( "org/apache/test/samplejar/1.0/samplejar-1.0.jar.md5" )) );
1298         assertTrue( Files.exists(repoRoot.resolve( "org/apache/test/samplejar/1.0/samplejar-1.0.jar.sha1" )) );
1299         assertTrue( Files.exists(repoRoot.resolve( "org/apache/test/samplejar/1.0/samplejar-1.0.pom" )) );
1300         assertTrue( Files.exists(repoRoot.resolve( "org/apache/test/samplejar/1.0/samplejar-1.0-source.jar" )) );
1301         selector = ArchivaItemSelector.builder( )
1302             .withNamespace( "org.apache.test" )
1303             .withProjectId( "samplejar" )
1304             .withVersion( "1.0" )
1305             .withArtifactId( "samplejar" )
1306             .withArtifactVersion( "1.0" )
1307             .withExtension( "jar" )
1308             .build();
1309         item = myRepoContent.getItem( selector );
1310         assertTrue( item instanceof Artifact );
1311         myRepoContent.deleteItem( item );
1312         assertFalse( Files.exists(repoRoot.resolve( "org/apache/test/samplejar/1.0/samplejar-1.0.jar" )) );
1313         assertTrue( Files.exists(repoRoot.resolve( "org/apache/test/samplejar/1.0/samplejar-1.0.jar.md5" )) );
1314         assertTrue( Files.exists(repoRoot.resolve( "org/apache/test/samplejar/1.0/samplejar-1.0.jar.sha1" )) );
1315         assertTrue( Files.exists(repoRoot.resolve( "org/apache/test/samplejar/1.0/samplejar-1.0.pom" )) );
1316         assertTrue( Files.exists(repoRoot.resolve( "org/apache/test/samplejar/1.0/samplejar-1.0-source.jar" )) );
1317         assertTrue( Files.exists(repoRoot.resolve( "org/apache/test/samplejar/2.0" )) );
1318         assertTrue( Files.exists(repoRoot.resolve( "org/apache/test/sub/samplejar/1.0" )) );
1319         assertTrue( Files.exists(repoRoot.resolve( "org/apache/test/sub/samplejar/2.0" )) );
1320
1321         selector = ArchivaItemSelector.builder( )
1322             .withNamespace( "org.apache.test" )
1323             .withProjectId( "samplejar" )
1324             .withVersion( "1.0" )
1325             .withArtifactId( "samplejar" )
1326             .withArtifactVersion( "1.0" )
1327             .withClassifier( "source" )
1328             .withExtension( "jar" )
1329             .build();
1330         item = myRepoContent.getItem( selector );
1331         assertTrue( item instanceof Artifact );
1332         myRepoContent.deleteItem( item );
1333         assertFalse( Files.exists(repoRoot.resolve( "org/apache/test/samplejar/1.0/samplejar-1.0.jar" )) );
1334         assertTrue( Files.exists(repoRoot.resolve( "org/apache/test/samplejar/1.0/samplejar-1.0.jar.md5" )) );
1335         assertTrue( Files.exists(repoRoot.resolve( "org/apache/test/samplejar/1.0/samplejar-1.0.jar.sha1" )) );
1336         assertTrue( Files.exists(repoRoot.resolve( "org/apache/test/samplejar/1.0/samplejar-1.0.pom" )) );
1337         assertFalse( Files.exists(repoRoot.resolve( "org/apache/test/samplejar/1.0/samplejar-1.0-source.jar" )) );
1338         assertTrue( Files.exists(repoRoot.resolve( "org/apache/test/samplejar/1.0/samplejar-1.0-source.jar.sha1" )) );
1339         assertTrue( Files.exists(repoRoot.resolve( "org/apache/test/samplejar/2.0" )) );
1340         assertTrue( Files.exists(repoRoot.resolve( "org/apache/test/sub/samplejar/1.0" )) );
1341         assertTrue( Files.exists(repoRoot.resolve( "org/apache/test/sub/samplejar/2.0" )) );
1342
1343         selector = ArchivaItemSelector.builder( )
1344             .withNamespace( "org.apache.test" )
1345             .withProjectId( "samplejar" )
1346             .withVersion( "1.0" )
1347             .withArtifactId( "samplejar" )
1348             .withArtifactVersion( "1.0" )
1349             .withExtension( "jar.md5" )
1350             .build();
1351         item = myRepoContent.getItem( selector );
1352         assertTrue( item instanceof Artifact );
1353         myRepoContent.deleteItem( item );
1354         assertFalse( Files.exists(repoRoot.resolve( "org/apache/test/samplejar/1.0/samplejar-1.0.jar" )) );
1355         assertFalse( Files.exists(repoRoot.resolve( "org/apache/test/samplejar/1.0/samplejar-1.0.jar.md5" )) );
1356         assertTrue( Files.exists(repoRoot.resolve( "org/apache/test/samplejar/1.0/samplejar-1.0.jar.sha1" )) );
1357         assertTrue( Files.exists(repoRoot.resolve( "org/apache/test/samplejar/1.0/samplejar-1.0.pom" )) );
1358         assertFalse( Files.exists(repoRoot.resolve( "org/apache/test/samplejar/1.0/samplejar-1.0-source.jar" )) );
1359         assertTrue( Files.exists(repoRoot.resolve( "org/apache/test/samplejar/1.0/samplejar-1.0-source.jar.sha1" )) );
1360         assertTrue( Files.exists(repoRoot.resolve( "org/apache/test/samplejar/2.0" )) );
1361         assertTrue( Files.exists(repoRoot.resolve( "org/apache/test/sub/samplejar/1.0" )) );
1362         assertTrue( Files.exists(repoRoot.resolve( "org/apache/test/sub/samplejar/2.0" )) );
1363
1364
1365     }
1366
1367     @Test
1368     public void deleteItemNotFound() throws IOException, URISyntaxException, ItemNotFoundException
1369     {
1370         ManagedRepository repo = createManagedRepoWithContent( "delete-repository" );
1371         ManagedRepositoryContent myRepoContent = repo.getContent( );
1372         Path repoRoot = repo.getAsset( "" ).getFilePath( );
1373
1374         ArchivaItemSelector selector = ArchivaItemSelector.builder( )
1375             .withNamespace( "org.apache.test2" )
1376             .build( );
1377
1378         ContentItem item = myRepoContent.getItem( selector );
1379         assertTrue( item instanceof Namespace );
1380         try
1381         {
1382             myRepoContent.deleteItem( item );
1383             assertTrue( "ItemNotFoundException expected for non existing namespace", false );
1384         } catch ( ItemNotFoundException e) {
1385         }
1386
1387         selector = ArchivaItemSelector.builder( )
1388             .withNamespace( "org.apache.test" )
1389             .withProjectId( "samplejar2" )
1390             .build( );
1391         item = myRepoContent.getItem( selector );
1392         assertTrue( item instanceof Project );
1393         try
1394         {
1395             myRepoContent.deleteItem( item );
1396             assertTrue( "ItemNotFoundException expected for non existing project", false );
1397         } catch ( ItemNotFoundException e) {
1398         }
1399
1400         selector = ArchivaItemSelector.builder( )
1401             .withNamespace( "org.apache.test" )
1402             .withProjectId( "samplejar" )
1403             .withVersion("1.1")
1404             .build( );
1405         item = myRepoContent.getItem( selector );
1406         assertTrue( item instanceof Version );
1407         try
1408         {
1409             myRepoContent.deleteItem( item );
1410             assertTrue( "ItemNotFoundException expected for non existing version", false );
1411         } catch ( ItemNotFoundException e) {
1412         }
1413
1414         selector = ArchivaItemSelector.builder( )
1415             .withNamespace( "org.apache.test" )
1416             .withProjectId( "samplejar" )
1417             .withVersion("1.0")
1418             .withArtifactId( "samplejar" )
1419             .withArtifactVersion( "1.0" )
1420             .withExtension( "jax" )
1421             .build( );
1422         item = myRepoContent.getItem( selector );
1423         assertTrue( item instanceof Artifact );
1424         try
1425         {
1426             myRepoContent.deleteItem( item );
1427             assertTrue( "ItemNotFoundException expected for non existing artifact", false );
1428         } catch ( ItemNotFoundException e) {
1429         }
1430
1431     }
1432
1433
1434     @Test
1435     public void testAddArtifact() throws IOException, URISyntaxException, LayoutException
1436     {
1437         ManagedRepository repo = createManagedRepoWithContent( "delete-repository" );
1438         ManagedRepositoryContent myRepoContent = repo.getContent( );
1439         BaseRepositoryContentLayout layout = myRepoContent.getLayout( BaseRepositoryContentLayout.class );
1440         Path repoRoot = repo.getAsset( "" ).getFilePath( );
1441
1442         Path tmpFile = Files.createTempFile( "archiva-mvn-repotest", "jar" );
1443         try( OutputStream outputStream = Files.newOutputStream( tmpFile ))
1444         {
1445             for ( int i = 0; i < 255; i++ )
1446             {
1447                 outputStream.write( "test.test.test\n".getBytes( Charset.forName( "UTF-8" ) ) );
1448             }
1449         }
1450
1451         Path file = repoRoot.resolve( "org/apache/maven/samplejar/2.0/samplejar-2.0.jar" );
1452         FileTime lmt = Files.getLastModifiedTime( file );
1453         ArchivaItemSelector selector = ArchivaItemSelector.builder( )
1454             .withNamespace( "org.apache.maven" )
1455             .withProjectId( "samplejar" )
1456             .withVersion( "2.0" )
1457             .withArtifactId( "samplejar" )
1458             .withArtifactVersion( "2.0" )
1459             .withExtension( "jar" )
1460             .build( );
1461         Artifact artifact = layout.getArtifact( selector );
1462         layout.addArtifact( tmpFile, artifact );
1463         FileTime lmtAfter = Files.getLastModifiedTime( file );
1464         assertNotEquals( lmtAfter, lmt );
1465         Reader ln = Files.newBufferedReader( file, Charset.forName( "UTF-8" ) );
1466         char[] content = new char[50];
1467         ln.read( content );
1468         assertTrue( new String( content ).startsWith( "test.test.test" ) );
1469
1470         tmpFile = Files.createTempFile( "archiva-mvn-repotest", "jar" );
1471         try( OutputStream outputStream = Files.newOutputStream( tmpFile ))
1472         {
1473             for ( int i = 0; i < 255; i++ )
1474             {
1475                 outputStream.write( "test.test.test\n".getBytes( Charset.forName( "UTF-8" ) ) );
1476             }
1477         }
1478         file = repoRoot.resolve( "org/apache/maven/samplejar/2.0/samplejar-2.0.test" );
1479         assertFalse( Files.exists( file ) );
1480         assertTrue( Files.exists( tmpFile ) );
1481         selector = ArchivaItemSelector.builder( )
1482             .withNamespace( "org.apache.maven" )
1483             .withProjectId( "samplejar" )
1484             .withVersion( "2.0" )
1485             .withArtifactId( "samplejar" )
1486             .withArtifactVersion( "2.0" )
1487             .withExtension( "test" )
1488             .build( );
1489         artifact = layout.getArtifact( selector );
1490         layout.addArtifact( tmpFile, artifact );
1491         ln = Files.newBufferedReader( file, Charset.forName( "UTF-8" ) );
1492         ln.read( content );
1493         assertTrue( new String( content ).startsWith( "test.test.test" ) );
1494     }
1495
1496     @Test
1497     public void getExistingMetadataItem() {
1498         // org/apache/maven/some-ejb/1.0
1499         ArchivaItemSelector versionSelector = ArchivaItemSelector.builder( )
1500             .withNamespace( "org.apache.maven" )
1501             .withProjectId( "some-ejb" )
1502             .withVersion( "1.0" ).build( );
1503         Version version = repoContent.getVersion( versionSelector );
1504         DataItem metaData = repoContent.getMetadataItem( version );
1505         assertTrue( metaData.exists( ) );
1506         assertEquals( "/org/apache/maven/some-ejb/1.0/maven-metadata.xml", metaData.getAsset( ).getPath( ) );
1507     }
1508
1509     @Test
1510     public void getNonExistingMetadataItem() {
1511         // org/apache/maven/some-ejb/1.0
1512         ArchivaItemSelector versionSelector = ArchivaItemSelector.builder( )
1513             .withNamespace( "javax.sql" )
1514             .withProjectId( "jdbc" )
1515             .withVersion( "2.0" ).build( );
1516         Version version = repoContent.getVersion( versionSelector );
1517         DataItem metaData = repoContent.getMetadataItem( version );
1518         assertFalse( metaData.exists( ) );
1519         assertEquals( "/javax/sql/jdbc/2.0/maven-metadata.xml", metaData.getAsset( ).getPath( ) );
1520     }
1521
1522 }