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