]> source.dussan.org Git - archiva.git/blob
e4728f2aaa477553a0aa197d4041988461feca47
[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     @Test
444     public void testDeleteArtifactWithType() throws IOException, org.apache.archiva.repository.ContentNotFoundException, org.apache.archiva.repository.ContentAccessException
445     {
446         Path deleteRepo = setupRepoCopy( "delete-repository", "delete-repository-2" );
447         assertTrue( Files.exists( deleteRepo.resolve( "org/apache/maven/samplejar/1.0/samplejar-1.0-source.jar" ) ) );
448         assertTrue( Files.exists( deleteRepo.resolve( "org/apache/maven/samplejar/1.0/samplejar-1.0.jar" ) ) );
449         assertTrue( Files.exists( deleteRepo.resolve( "org/apache/maven/samplejar/1.0/samplejar-1.0.jar.md5" ) ) );
450         assertTrue( Files.exists( deleteRepo.resolve( "org/apache/maven/samplejar/1.0/samplejar-1.0.jar.sha1" ) ) );
451         assertTrue( Files.exists( deleteRepo.resolve( "org/apache/maven/samplejar/1.0/samplejar-1.0.pom" ) ) );
452
453         ArtifactReference ref = new ArtifactReference( );
454         ref.setGroupId( "org.apache.maven" );
455         ref.setArtifactId( "samplejar" );
456         ref.setVersion( "1.0" );
457         ref.setType( "jar" );
458
459         repoContent.deleteArtifact( ref );
460
461         assertTrue( Files.exists( deleteRepo.resolve( "org/apache/maven/samplejar/1.0/samplejar-1.0-source.jar" ) ) );
462         assertFalse( Files.exists( deleteRepo.resolve( "org/apache/maven/samplejar/1.0/samplejar-1.0.jar" ) ) );
463         assertTrue( Files.exists( deleteRepo.resolve( "org/apache/maven/samplejar/1.0/samplejar-1.0.jar.md5" ) ) );
464         assertTrue( Files.exists( deleteRepo.resolve( "org/apache/maven/samplejar/1.0/samplejar-1.0.jar.sha1" ) ) );
465         assertTrue( Files.exists( deleteRepo.resolve( "org/apache/maven/samplejar/1.0/samplejar-1.0.pom" ) ) );
466
467
468     }
469
470
471     @Test
472     public void testDeleteArtifactWithClassifier() throws IOException, org.apache.archiva.repository.ContentNotFoundException, org.apache.archiva.repository.ContentAccessException
473     {
474         Path deleteRepo = setupRepoCopy( "default-repository", "default-repository-2" );
475         assertTrue( Files.exists( deleteRepo.resolve( "org/apache/maven/samplejar/1.0/samplejar-1.0-source.jar" ) ) );
476         assertTrue( Files.exists( deleteRepo.resolve( "org/apache/maven/samplejar/1.0/samplejar-1.0-source.jar.sha1" ) ) );
477         assertTrue( Files.exists( deleteRepo.resolve( "org/apache/maven/samplejar/1.0/samplejar-1.0.jar" ) ) );
478         assertTrue( Files.exists( deleteRepo.resolve( "org/apache/maven/samplejar/1.0/samplejar-1.0.jar.md5" ) ) );
479         assertTrue( Files.exists( deleteRepo.resolve( "org/apache/maven/samplejar/1.0/samplejar-1.0.jar.sha1" ) ) );
480         assertTrue( Files.exists( deleteRepo.resolve( "org/apache/maven/samplejar/1.0/samplejar-1.0.pom" ) ) );
481
482         ArtifactReference ref = new ArtifactReference( );
483         ref.setGroupId( "org.apache.maven" );
484         ref.setArtifactId( "samplejar" );
485         ref.setVersion( "1.0" );
486         ref.setClassifier( "source" );
487         ref.setType( "jar" );
488
489         repoContent.deleteArtifact( ref );
490
491         assertFalse( Files.exists( deleteRepo.resolve( "org/apache/maven/samplejar/1.0/samplejar-1.0-source.jar" ) ) );
492         assertTrue( Files.exists( deleteRepo.resolve( "org/apache/maven/samplejar/1.0/samplejar-1.0-source.jar.sha1" ) ) );
493         assertTrue( Files.exists( deleteRepo.resolve( "org/apache/maven/samplejar/1.0/samplejar-1.0.jar" ) ) );
494         assertTrue( Files.exists( deleteRepo.resolve( "org/apache/maven/samplejar/1.0/samplejar-1.0.jar.md5" ) ) );
495         assertTrue( Files.exists( deleteRepo.resolve( "org/apache/maven/samplejar/1.0/samplejar-1.0.jar.sha1" ) ) );
496         assertTrue( Files.exists( deleteRepo.resolve( "org/apache/maven/samplejar/1.0/samplejar-1.0.pom" ) ) );
497
498     }
499
500     @Test
501     public void testDeleteArtifactWithoutType() throws IOException, org.apache.archiva.repository.ContentNotFoundException, org.apache.archiva.repository.ContentAccessException
502     {
503         Path deleteRepo = setupRepoCopy( "default-repository", "default-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-source.jar.sha1" ) ) );
506         assertTrue( Files.exists( deleteRepo.resolve( "org/apache/maven/samplejar/1.0/samplejar-1.0.jar" ) ) );
507         assertTrue( Files.exists( deleteRepo.resolve( "org/apache/maven/samplejar/1.0/samplejar-1.0.jar.md5" ) ) );
508         assertTrue( Files.exists( deleteRepo.resolve( "org/apache/maven/samplejar/1.0/samplejar-1.0.jar.sha1" ) ) );
509         assertTrue( Files.exists( deleteRepo.resolve( "org/apache/maven/samplejar/1.0/samplejar-1.0.pom" ) ) );
510
511         ArtifactReference ref = new ArtifactReference( );
512         ref.setGroupId( "org.apache.maven" );
513         ref.setArtifactId( "samplejar" );
514         ref.setVersion( "1.0" );
515
516         repoContent.deleteArtifact( ref );
517
518         assertFalse( Files.exists( deleteRepo.resolve( "org/apache/maven/samplejar/1.0" ) ) );
519         assertTrue( Files.exists( deleteRepo.resolve( "org/apache/maven/samplejar" ) ) );
520
521     }
522
523
524     @Test
525     public void testDeleteVersion() throws IOException, org.apache.archiva.repository.ContentNotFoundException, org.apache.archiva.repository.ContentAccessException
526     {
527         Path deleteRepo = setupRepoCopy( "delete-repository", "delete-repository-2" );
528         assertTrue( Files.exists( deleteRepo.resolve( "org/apache/maven/samplejar/1.0/samplejar-1.0-source.jar" ) ) );
529         assertTrue( Files.exists( deleteRepo.resolve( "org/apache/maven/samplejar/1.0/samplejar-1.0.jar" ) ) );
530         assertTrue( Files.exists( deleteRepo.resolve( "org/apache/maven/samplejar/1.0/samplejar-1.0.jar.md5" ) ) );
531         assertTrue( Files.exists( deleteRepo.resolve( "org/apache/maven/samplejar/1.0/samplejar-1.0.jar.sha1" ) ) );
532         assertTrue( Files.exists( deleteRepo.resolve( "org/apache/maven/samplejar/1.0/samplejar-1.0.pom" ) ) );
533
534         VersionedReference ref = new VersionedReference( ).groupId( "org.apache.maven" ).artifactId( "samplejar" ).version( "1.0" );
535
536         repoContent.deleteVersion( ref );
537
538         assertFalse( Files.exists( deleteRepo.resolve( "org/apache/maven/samplejar/1.0" ) ) );
539
540     }
541
542     @Test
543     public void testDeleteProject() throws IOException, org.apache.archiva.repository.ContentNotFoundException, org.apache.archiva.repository.ContentAccessException
544     {
545         Path deleteRepo = setupRepoCopy( "delete-repository", "delete-repository-2" );
546         assertTrue( Files.exists( deleteRepo.resolve( "org/apache/maven/samplejar/1.0/samplejar-1.0-source.jar" ) ) );
547         assertTrue( Files.exists( deleteRepo.resolve( "org/apache/maven/samplejar/1.0/samplejar-1.0.jar" ) ) );
548         assertTrue( Files.exists( deleteRepo.resolve( "org/apache/maven/samplejar/1.0/samplejar-1.0.jar.md5" ) ) );
549         assertTrue( Files.exists( deleteRepo.resolve( "org/apache/maven/samplejar/1.0/samplejar-1.0.jar.sha1" ) ) );
550         assertTrue( Files.exists( deleteRepo.resolve( "org/apache/maven/samplejar/1.0/samplejar-1.0.pom" ) ) );
551
552         ProjectReference ref = new ProjectReference( ).groupId( "org.apache.maven" ).artifactId( "samplejar" );
553
554         repoContent.deleteProject( ref );
555
556         assertFalse( Files.exists( deleteRepo.resolve( "org/apache/maven/samplejar/1.0" ) ) );
557
558     }
559
560     @Test
561     public void testGetArtifactStreamWithVersionSelector() {
562         ItemSelector selector = ArchivaItemSelector.builder( )
563             .withNamespace( "javax.sql" )
564             .withProjectId( "jdbc" )
565             .withVersion( "2.0" ).build();
566         try(Stream<? extends Artifact> stream = repoContent.newArtifactStream( selector ))
567         {
568             assertNotNull( stream );
569             List<? extends Artifact> results = stream.collect( Collectors.toList( ) );
570             checkArtifactListWithVersionSelector1( results );
571         }
572     }
573
574     @Test
575     public void testGetArtifactListWithVersionSelector() {
576         ItemSelector selector = ArchivaItemSelector.builder( )
577             .withNamespace( "javax.sql" )
578             .withProjectId( "jdbc" )
579             .withVersion( "2.0" ).build();
580         List<? extends Artifact> results = repoContent.getArtifacts( selector );
581         checkArtifactListWithVersionSelector1( results );
582     }
583
584     private void checkArtifactListWithVersionSelector1( List<? extends Artifact> results )
585     {
586         assertNotNull( results );
587         assertEquals( 2, results.size( ) );
588         Artifact mainArtifact = results.stream( ).filter( a -> a.getFileName( ).equals( "jdbc-2.0.jar" ) ).findFirst( ).get( );
589         assertNotNull( mainArtifact );
590         assertEquals( BaseArtifactTypes.MAIN, mainArtifact.getDataType( ) );
591         Artifact metaArtifact = results.stream( ).filter( a -> a.getFileName( ).equals( "maven-metadata-repository.xml" ) ).findFirst( ).get( );
592         assertNotNull( metaArtifact );
593         assertEquals( MavenTypes.REPOSITORY_METADATA, metaArtifact.getDataType( ) );
594     }
595
596     @Test
597     public void testGetArtifactStreamWithVersionSelector2() {
598         ItemSelector selector = ArchivaItemSelector.builder( )
599             .withNamespace( "org.apache.axis2" )
600             .withProjectId( "axis2" )
601             .withVersion( "1.3-SNAPSHOT" ).build();
602         try(Stream<? extends Artifact> stream = repoContent.newArtifactStream( selector ))
603         {
604             assertNotNull( stream );
605             List<? extends Artifact> results = stream.collect( Collectors.toList( ) );
606             checkArtifactListWithVersionSelector2( results );
607         }
608     }
609
610     @Test
611     public void testGetArtifactListWithVersionSelector2() {
612         ItemSelector selector = ArchivaItemSelector.builder( )
613             .withNamespace( "org.apache.axis2" )
614             .withProjectId( "axis2" )
615             .withVersion( "1.3-SNAPSHOT" ).build();
616         List<? extends Artifact> results = repoContent.getArtifacts( selector );
617         checkArtifactListWithVersionSelector2( results );
618     }
619
620     private void checkArtifactListWithVersionSelector2( List<? extends Artifact> results )
621     {
622         assertNotNull( results );
623         assertEquals( 39, results.size( ) );
624
625         Artifact artifact = results.stream( ).filter( a -> a.getFileName( ).equals( "axis2-1.3-20070725.210059-1.pom" ) )
626             .findFirst( ).get( );
627
628         assertNotNull( artifact );
629         assertEquals( "pom", artifact.getExtension( ) );
630         assertEquals( BaseArtifactTypes.MAIN, artifact.getDataType( ) );
631         assertEquals( "1.3-SNAPSHOT", artifact.getVersion( ).getVersion( ) );
632         assertEquals( "1.3-20070725.210059-1", artifact.getArtifactVersion( ) );
633         assertEquals( ".pom", artifact.getRemainder( ) );
634         assertEquals( "axis2", artifact.getId( ) );
635         assertEquals( "axis2", artifact.getVersion( ).getProject( ).getId( ) );
636         assertEquals( "org.apache.axis2", artifact.getVersion( ).getProject( ).getNamespace( ).getNamespace( ) );
637         assertEquals( "", artifact.getClassifier( ) );
638         assertEquals( "pom", artifact.getType( ) );
639
640         artifact = null;
641         artifact = results.stream( ).filter( a -> a.getFileName( ).equals( "axis2-1.3-20070725.210059-1.pom.md5" ) )
642             .findFirst( ).get( );
643
644         assertNotNull( artifact );
645         assertEquals( "md5", artifact.getExtension( ) );
646         assertEquals( BaseArtifactTypes.RELATED, artifact.getDataType( ) );
647         assertEquals( "1.3-SNAPSHOT", artifact.getVersion( ).getVersion( ) );
648         assertEquals( "1.3-20070725.210059-1", artifact.getArtifactVersion( ) );
649         assertEquals( ".pom.md5", artifact.getRemainder( ) );
650         assertEquals( "axis2", artifact.getId( ) );
651         assertEquals( "axis2", artifact.getVersion( ).getProject( ).getId( ) );
652         assertEquals( "org.apache.axis2", artifact.getVersion( ).getProject( ).getNamespace( ).getNamespace( ) );
653         assertEquals( "", artifact.getClassifier( ) );
654         assertEquals( "md5", artifact.getType( ) );
655
656
657         artifact = null;
658         artifact = results.stream( ).filter( a -> a.getFileName( ).equals( "maven-metadata.xml" ) )
659             .findFirst( ).get( );
660         assertNotNull( artifact );
661         assertEquals( BaseArtifactTypes.METADATA, artifact.getDataType( ) );
662         assertEquals( "1.3-SNAPSHOT", artifact.getVersion( ).getVersion( ) );
663         assertEquals( "xml", artifact.getExtension( ) );
664     }
665
666     @Test
667     public void testGetArtifactListWithArtifactSelector1() {
668         ItemSelector selector = ArchivaItemSelector.builder( )
669             .withNamespace( "org.apache.axis2" )
670             .withProjectId( "axis2" )
671             .withVersion( "1.3-SNAPSHOT" )
672             .withArtifactVersion( "1.3-20070731.113304-21" )
673             .withExtension( "pom" )
674             .build( );
675         List<? extends Artifact> results = repoContent.getArtifacts( selector );
676         checkArtifactListWithArtifactSelector1( results );
677     }
678
679     @Test
680     public void testGetArtifactStreamWithArtifactSelector1() {
681         ItemSelector selector = ArchivaItemSelector.builder( )
682             .withNamespace( "org.apache.axis2" )
683             .withProjectId( "axis2" )
684             .withVersion( "1.3-SNAPSHOT" )
685             .withArtifactVersion( "1.3-20070731.113304-21" )
686             .withExtension( "pom" )
687             .build( );
688         try(Stream<? extends Artifact> results = repoContent.newArtifactStream( selector ))
689         {
690             checkArtifactListWithArtifactSelector1( results.collect( Collectors.toList()) );
691         }
692     }
693
694     private void checkArtifactListWithArtifactSelector1( List<? extends Artifact> results )
695     {
696         assertNotNull( results );
697         assertEquals( 1, results.size( ) );
698         Artifact artifact = results.get( 0 );
699         assertEquals( "pom", artifact.getExtension( ) );
700         assertEquals( BaseArtifactTypes.MAIN, artifact.getDataType( ) );
701     }
702
703     @Test
704     public void testGetArtifactListWithArtifactSelector2() {
705         ItemSelector selector = ArchivaItemSelector.builder( )
706             .withNamespace( "org.apache.axis2" )
707             .withProjectId( "axis2" )
708             .withVersion( "1.3-SNAPSHOT" )
709             .withArtifactVersion( "1.3-20070731.113304-21" )
710             .withExtension( "pom" )
711             .includeRelatedArtifacts()
712             .build( );
713         List<? extends Artifact> results = repoContent.getArtifacts( selector );
714
715         checkArtifactListWithArtifactSelector2( results );
716
717     }
718
719     @Test
720     public void testGetArtifactStreamWithArtifactSelector2() {
721         ItemSelector selector = ArchivaItemSelector.builder( )
722             .withNamespace( "org.apache.axis2" )
723             .withProjectId( "axis2" )
724             .withVersion( "1.3-SNAPSHOT" )
725             .withArtifactVersion( "1.3-20070731.113304-21" )
726             .withExtension( "pom" )
727             .includeRelatedArtifacts()
728             .build( );
729         try(Stream<? extends Artifact> results = repoContent.newArtifactStream( selector ))
730         {
731             checkArtifactListWithArtifactSelector2( results.collect( Collectors.toList()) );
732         }
733     }
734
735     private void checkArtifactListWithArtifactSelector2( List<? extends Artifact> results )
736     {
737         assertNotNull( results );
738         assertEquals( 3, results.size( ) );
739         Artifact artifact = results.stream( ).filter( a -> a.getFileName( ).equalsIgnoreCase( "axis2-1.3-20070731.113304-21.pom" ) )
740             .findFirst( ).get( );
741         assertNotNull( artifact );
742         assertEquals( "pom", artifact.getExtension( ) );
743         assertEquals( BaseArtifactTypes.MAIN, artifact.getDataType( ) );
744
745         artifact = results.stream( ).filter( a -> a.getFileName( ).equalsIgnoreCase( "axis2-1.3-20070731.113304-21.pom.sha1" ) )
746             .findFirst( ).get( );
747         assertNotNull( artifact );
748         assertEquals( "sha1", artifact.getExtension( ) );
749         assertEquals( BaseArtifactTypes.RELATED, artifact.getDataType( ) );
750     }
751
752
753     @Test
754     public void testArtifactListWithProjectSelector() {
755         ItemSelector selector = ArchivaItemSelector.builder( )
756             .withNamespace( "org.apache.maven.shared" )
757             .withProjectId( "maven-downloader" )
758             .build( );
759         List<? extends Artifact> results = repoContent.getArtifacts( selector );
760         checkArtifactListWithProjectSelector( results );
761
762     }
763
764     @Test
765     public void testArtifactStreamWithProjectSelector() {
766         ItemSelector selector = ArchivaItemSelector.builder( )
767             .withNamespace( "org.apache.maven.shared" )
768             .withProjectId( "maven-downloader" )
769             .build( );
770         Stream<? extends Artifact> results = repoContent.newArtifactStream( selector );
771         checkArtifactListWithProjectSelector( results.collect( Collectors.toList()) );
772
773     }
774
775     private void checkArtifactListWithProjectSelector( List<? extends Artifact> results )
776     {
777         assertNotNull( results );
778         assertEquals( 27, results.size( ) );
779
780         Artifact artifact = results.stream( ).filter( a -> a.getFileName( ).equalsIgnoreCase( "maven-metadata.xml" ) )
781             .findFirst( ).get( );
782         assertNotNull( artifact );
783         assertEquals( "xml", artifact.getExtension( ) );
784         assertEquals( BaseArtifactTypes.METADATA, artifact.getDataType( ) );
785
786         artifact = results.stream( ).filter( a -> a.getFileName( ).equalsIgnoreCase( "maven-downloader-1.0-sources.jar" ) )
787             .findFirst( ).get( );
788         assertNotNull( artifact );
789         assertEquals( BaseArtifactTypes.MAIN, artifact.getDataType( ) );
790         assertEquals( "sources", artifact.getClassifier( ) );
791         assertEquals( "java-source", artifact.getType( ) );
792
793         artifact = results.stream( ).filter( a -> a.getFileName( ).equalsIgnoreCase( "maven-downloader-1.0-sources.jar.sha1" ) )
794             .findFirst( ).get( );
795         assertNotNull( artifact );
796         assertEquals( BaseArtifactTypes.RELATED, artifact.getDataType( ) );
797         assertEquals( "sources", artifact.getClassifier( ) );
798         assertEquals( "sha1", artifact.getType( ) );
799         assertEquals( ".jar.sha1", artifact.getRemainder( ) );
800     }
801
802     @Test
803     public void testArtifactListWithNamespaceSelector() {
804         ItemSelector selector = ArchivaItemSelector.builder( )
805             .withNamespace( "org.multilevel" )
806             .build( );
807         List<? extends Artifact> results = repoContent.getArtifacts( selector );
808         assertNotNull( results );
809         assertEquals( 3, results.size( ) );
810         assertTrue( results.get( 0 ).getFileName( ).startsWith( "testproj1" ) );
811     }
812
813     @Test
814     public void testArtifactListWithNamespaceSelectorRecursive() {
815         ItemSelector selector = ArchivaItemSelector.builder( )
816             .withNamespace( "org.multilevel" )
817             .recurse()
818             .build( );
819         List<? extends Artifact> results = repoContent.getArtifacts( selector );
820         checkArtifactListWithNamespaceSelectorRecursive( results );
821     }
822
823     @Test
824     public void testArtifactStreamWithNamespaceSelectorRecursive() {
825         ItemSelector selector = ArchivaItemSelector.builder( )
826             .withNamespace( "org.multilevel" )
827             .recurse()
828             .build( );
829         Stream<? extends Artifact> results = repoContent.newArtifactStream( selector );
830         checkArtifactListWithNamespaceSelectorRecursive( results.collect( Collectors.toList()) );
831     }
832
833     private void checkArtifactListWithNamespaceSelectorRecursive( List<? extends Artifact> results )
834     {
835         assertNotNull( results );
836         assertEquals( 6, results.size( ) );
837
838         Artifact artifact = results.stream( ).filter( a -> a.getFileName( ).equalsIgnoreCase( "testproj2-1.0.pom" ) )
839             .findFirst( ).get( );
840         assertNotNull( artifact );
841         assertEquals( 6, artifact.getAsset( ).getParent( ).getPath( ).split( "/" ).length );
842
843         artifact = results.stream( ).filter( a -> a.getFileName( ).equalsIgnoreCase( "testproj1-1.0.pom" ) )
844             .findFirst( ).get( );
845         assertNotNull( artifact );
846         assertEquals( 5, artifact.getAsset( ).getParent( ).getPath( ).split( "/" ).length );
847     }
848
849
850     @Test
851     public void testArtifactListWithArtifactSelector1() {
852         ItemSelector selector = ArchivaItemSelector.builder( )
853             .withNamespace( "org.apache.maven" )
854             .withProjectId( "test" )
855             .withVersion( "1.0-SNAPSHOT" )
856             .withArtifactId( "test" )
857             .withArtifactVersion( "1.0-20050611.112233-1" )
858             .build( );
859
860         List<? extends Artifact> results = repoContent.getArtifacts( selector );
861
862         assertNotNull( results );
863         assertEquals( 1, results.size( ) );
864
865         Artifact artifact = results.stream( ).filter( a -> a.getFileName( ).equalsIgnoreCase( "test-1.0-20050611.112233-1.jar" ) )
866             .findFirst().get();
867         assertNotNull( artifact );
868         assertEquals( "", artifact.getClassifier( ) );
869     }
870
871     @Test
872     public void testArtifactListWithArtifactSelector2() {
873         ItemSelector selector = ArchivaItemSelector.builder( )
874             .withNamespace( "org.apache.maven" )
875             .withProjectId( "test" )
876             .withVersion( "1.0-SNAPSHOT" )
877             .withClassifier( "*" )
878             .withArtifactId( "test" )
879             .withArtifactVersion( "1.0-20050611.112233-1" )
880             .build( );
881
882         List<? extends Artifact> results = repoContent.getArtifacts( selector );
883
884         assertNotNull( results );
885         assertEquals( 2, results.size( ) );
886
887         Artifact artifact = results.stream( ).filter( a -> a.getFileName( ).equalsIgnoreCase( "test-1.0-20050611.112233-1-javadoc.jar" ) )
888             .findFirst().get();
889         assertNotNull( artifact );
890         assertEquals( "javadoc", artifact.getClassifier( ) );
891         assertEquals( "javadoc", artifact.getType( ) );
892     }
893
894     @Test
895     public void testArtifactListWithArtifactSelector3() {
896         ItemSelector selector = ArchivaItemSelector.builder( )
897             .withNamespace( "org.apache.maven" )
898             .withProjectId( "test" )
899             .withVersion( "1.0-SNAPSHOT" )
900             .withClassifier( "*" )
901             .withArtifactVersion( "1.0-20050611.112233-1" )
902             .build( );
903
904         List<? extends Artifact> results = repoContent.getArtifacts( selector );
905
906         assertNotNull( results );
907         assertEquals( 3, results.size( ) );
908
909         Artifact artifact = results.stream( ).filter( a -> a.getFileName( ).equalsIgnoreCase( "test-1.0-20050611.112233-1-javadoc.jar" ) )
910             .findFirst().get();
911         assertNotNull( artifact );
912         assertEquals( "javadoc", artifact.getClassifier( ) );
913         assertEquals( "javadoc", artifact.getType( ) );
914
915         artifact = results.stream( ).filter( a -> a.getFileName( ).equalsIgnoreCase( "wrong-artifactId-1.0-20050611.112233-1.jar" ) )
916             .findFirst().get();
917         assertNotNull( artifact );
918         assertEquals( "", artifact.getClassifier( ) );
919         assertEquals( "wrong-artifactId", artifact.getId( ) );
920     }
921
922     @Test
923     public void testArtifactListWithArtifactSelector4() {
924         ItemSelector selector = ArchivaItemSelector.builder( )
925             .withNamespace( "org.apache.maven" )
926             .withProjectId( "test" )
927             .withVersion( "1.0-SNAPSHOT" )
928             .withClassifier( "" )
929             .build( );
930
931         List<? extends Artifact> results = repoContent.getArtifacts( selector );
932
933         assertNotNull( results );
934         assertEquals( 5, results.size( ) );
935
936         Artifact artifact = results.stream( ).filter( a -> a.getFileName( ).equalsIgnoreCase( "test-1.0-20050611.112233-1-javadoc.jar" ) )
937             .findFirst().get();
938         assertNotNull( artifact );
939         assertEquals( "javadoc", artifact.getClassifier( ) );
940         assertEquals( "javadoc", artifact.getType( ) );
941
942         artifact = results.stream( ).filter( a -> a.getFileName( ).equalsIgnoreCase( "wrong-artifactId-1.0-20050611.112233-1.jar" ) )
943             .findFirst().get();
944         assertNotNull( artifact );
945         assertEquals( "", artifact.getClassifier( ) );
946         assertEquals( "wrong-artifactId", artifact.getId( ) );
947
948         artifact = results.stream( ).filter( a -> a.getFileName( ).equalsIgnoreCase( "wrong-artifactId-1.0-20050611.1122x-1.jar" ) )
949             .findFirst().get();
950         assertNotNull( artifact );
951         assertEquals( "", artifact.getClassifier( ) );
952         assertEquals( "wrong-artifactId", artifact.getId( ) );
953         assertEquals( "", artifact.getArtifactVersion( ) );
954
955         artifact = results.stream( ).filter( a -> a.getFileName( ).equalsIgnoreCase( "test-1.0-20050611.1122x-1.jar" ) )
956             .findFirst().get();
957         assertNotNull( artifact );
958         assertEquals( "", artifact.getClassifier( ) );
959         assertEquals( "test", artifact.getId( ) );
960         assertEquals( "1.0-20050611.1122x-1", artifact.getArtifactVersion( ) );
961
962     }
963
964     @Test
965     public void testArtifactListWithArtifactSelectorWithClassifier() {
966         ItemSelector selector = ArchivaItemSelector.builder( )
967             .withNamespace( "org.apache.maven" )
968             .withProjectId( "test" )
969             .withVersion( "1.0-SNAPSHOT" )
970             .withArtifactId( "test" )
971             .withClassifier( "javadoc" )
972             .withArtifactVersion( "1.0-20050611.112233-1" )
973             .build( );
974
975         List<? extends Artifact> results = repoContent.getArtifacts( selector );
976
977         assertNotNull( results );
978         assertEquals( 1, results.size( ) );
979
980         Artifact artifact = results.stream( ).filter( a -> a.getFileName( ).equalsIgnoreCase( "test-1.0-20050611.112233-1-javadoc.jar" ) )
981             .findFirst().get();
982         assertNotNull( artifact );
983         assertEquals( "javadoc", artifact.getClassifier( ) );
984         assertEquals( "javadoc", artifact.getType( ) );
985     }
986
987     @Test
988     public void testArtifactListWithArtifactSelectorWrongArtifact() {
989         ItemSelector selector = ArchivaItemSelector.builder( )
990             .withNamespace( "org.apache.maven" )
991             .withProjectId( "test" )
992             .withVersion( "1.0-SNAPSHOT" )
993             .withArtifactId( "wrong-artifactId" )
994             .withArtifactVersion( "1.0-20050611.112233-1" )
995             .build( );
996
997         List<? extends Artifact> results = repoContent.getArtifacts( selector );
998
999         assertNotNull( results );
1000         assertEquals( 1, results.size( ) );
1001
1002         Artifact artifact = results.stream( ).filter( a -> a.getFileName( ).equalsIgnoreCase( "wrong-artifactId-1.0-20050611.112233-1.jar" ) )
1003             .findFirst().get();
1004         assertNotNull( artifact );
1005     }
1006
1007     @Test
1008     public void testArtifactListWithArtifactSelectorVersionPattern() {
1009         ItemSelector selector = ArchivaItemSelector.builder( )
1010             .withNamespace( "org.apache.maven" )
1011             .withProjectId( "test" )
1012             .withVersion( "1.0-SNAPSHOT" )
1013             .withArtifactVersion( "1.0-*" )
1014             .build( );
1015
1016         List<? extends Artifact> results = repoContent.getArtifacts( selector );
1017
1018         assertNotNull( results );
1019         assertEquals( 5, results.size( ) );
1020
1021         Artifact artifact = results.stream( ).filter( a -> a.getFileName( ).equalsIgnoreCase( "wrong-artifactId-1.0-20050611.112233-1.jar" ) )
1022             .findFirst().get();
1023         assertNotNull( artifact );
1024     }
1025
1026
1027     @Test
1028     public void testNewItemStreamWithNamespace1() {
1029         ItemSelector selector = ArchivaItemSelector.builder( )
1030             .withNamespace( "org.apache.axis2" )
1031             .build();
1032
1033         Stream<? extends ContentItem> stream = repoContent.newItemStream( selector, false );
1034         List<? extends ContentItem> result = stream.collect( Collectors.toList( ) );
1035         assertEquals( 41, result.size( ) );
1036         ContentItem item = result.get( 39 );
1037         Version version = item.adapt( Version.class );
1038         assertNotNull( version );
1039         assertEquals( "1.3-SNAPSHOT", version.getVersion( ) );
1040         Project project = result.get( 40 ).adapt( Project.class );
1041         assertNotNull( project );
1042         assertEquals( "axis2", project.getId( ) );
1043         assertTrue( result.stream( ).anyMatch( a -> "axis2-1.3-20070725.210059-1.pom".equals( a.getAsset( ).getName( ) ) ) );
1044     }
1045
1046     @Test
1047     public void testNewItemStreamWithNamespace2() {
1048         ItemSelector selector = ArchivaItemSelector.builder( )
1049             .withNamespace( "org.apache.maven" )
1050             .recurse()
1051             .build();
1052
1053         Stream<? extends ContentItem> stream = repoContent.newItemStream( selector, false );
1054         List<? extends ContentItem> result = stream.collect( Collectors.toList( ) );
1055         assertEquals( 170, result.size( ) );
1056         assertEquals( 92, result.stream( ).filter( a -> a instanceof DataItem ).count( ) );
1057     }
1058
1059     @Test
1060     public void testGetArtifactFromContentItem() {
1061         ItemSelector selector = ArchivaItemSelector.builder( )
1062             .withNamespace( "org.apache.maven" ).build();
1063         Namespace ns = repoContent.getNamespace( selector );
1064         List<? extends Artifact> artifacts = repoContent.getArtifacts( ns );
1065         assertNotNull( artifacts );
1066         assertEquals( 39, artifacts.size( ) );
1067         List<? extends Artifact> artifacts2 = repoContent.getArtifacts( (ContentItem)ns );
1068         assertArrayEquals( artifacts.toArray(), artifacts2.toArray() );
1069
1070         selector = ArchivaItemSelector.builder( )
1071             .withNamespace( "org.apache.maven.shared" )
1072             .withProjectId( "maven-downloader" )
1073             .build();
1074         Project project = repoContent.getProject( selector );
1075         artifacts = repoContent.getArtifacts( project );
1076         assertNotNull( artifacts );
1077         assertEquals( 27, artifacts.size( ) );
1078         artifacts2 = repoContent.getArtifacts( (ContentItem)project );
1079         assertArrayEquals( artifacts.toArray(), artifacts2.toArray() );
1080
1081         selector = ArchivaItemSelector.builder( )
1082             .withNamespace( "org.apache.maven.shared" )
1083             .withProjectId( "maven-downloader" )
1084             .withVersion( "1.1" )
1085             .build( );
1086         Version version = repoContent.getVersion( selector );
1087         artifacts = repoContent.getArtifacts( version );
1088         assertNotNull( artifacts );
1089         assertEquals( 12, artifacts.size( ) );
1090         artifacts2 = repoContent.getArtifacts( (ContentItem)version );
1091         assertArrayEquals( artifacts.toArray(), artifacts2.toArray() );
1092
1093     }
1094
1095     @Test
1096     public void testGetRelatedArtifactsFromArtifact() {
1097         ItemSelector selector = ArchivaItemSelector.builder( )
1098             .withNamespace( "org.apache.maven.shared" )
1099             .withProjectId( "maven-downloader" )
1100             .withVersion( "1.1" )
1101             .withExtension( "jar" )
1102             .withArtifactId( "maven-downloader" ).build( );
1103
1104         Artifact artifact = repoContent.getArtifact( selector );
1105         assertNotNull( artifact );
1106         List<? extends Artifact> artifacts = repoContent.getArtifacts( artifact );
1107         assertNotNull( artifacts );
1108         assertEquals( 2, artifacts.size( ) );
1109
1110     }
1111
1112     @Test
1113     public void testToItemFromPath() throws LayoutException
1114     {
1115         String path = "/org/apache/maven/shared";
1116         ContentItem item = repoContent.toItem( path );
1117         assertNotNull( item );
1118         assertTrue( item instanceof ArchivaContentItem );
1119
1120         path = "/org/apache/maven/shared/maven-downloader";
1121         item = repoContent.toItem( path );
1122         assertNotNull( item );
1123         assertTrue( item instanceof ArchivaContentItem );
1124
1125         path = "/org/apache/maven/shared/maven-downloader/1.1";
1126         item = repoContent.toItem( path );
1127         assertNotNull( item );
1128         assertTrue( item instanceof ArchivaContentItem );
1129
1130         path = "/org/apache/maven/shared/maven-downloader/1.1/maven-downloader-1.1.jar";
1131         item = repoContent.toItem( path );
1132         assertNotNull( item );
1133         assertTrue( item instanceof Artifact );
1134
1135     }
1136
1137     @Test
1138     public void testToItemFromAssetPath() throws LayoutException
1139     {
1140         StorageAsset path = repoContent.getRepository().getAsset("/org/apache/maven/shared");
1141         ContentItem item = repoContent.toItem( path );
1142         assertNotNull( item );
1143         assertTrue( item instanceof ArchivaContentItem );
1144
1145         path = repoContent.getRepository( ).getAsset( "/org/apache/maven/shared/maven-downloader" );
1146         item = repoContent.toItem( path );
1147         assertNotNull( item );
1148         assertTrue( item instanceof ArchivaContentItem );
1149
1150         path = repoContent.getRepository( ).getAsset( "/org/apache/maven/shared/maven-downloader/1.1" );
1151         item = repoContent.toItem( path );
1152         assertNotNull( item );
1153         assertTrue( item instanceof ArchivaContentItem );
1154
1155         path = repoContent.getRepository( ).getAsset( "/org/apache/maven/shared/maven-downloader/1.1/maven-downloader-1.1.jar" );
1156         item = repoContent.toItem( path );
1157         assertNotNull( item );
1158         assertTrue( item instanceof DataItem );
1159
1160     }
1161
1162     @Test
1163     public void testHasContent() throws LayoutException
1164     {
1165         ItemSelector selector = ArchivaItemSelector.builder( )
1166             .withNamespace( "org.apache.maven.shared" )
1167             .withProjectId( "maven-downloader" )
1168             .withVersion( "1.1" )
1169             .withArtifactId( "maven-downloader" )
1170             .withExtension( "jar" )
1171             .build();
1172
1173         assertTrue( repoContent.hasContent( selector ) );
1174
1175         selector = ArchivaItemSelector.builder( )
1176             .withNamespace( "org.apache.maven.shared" )
1177             .withProjectId( "maven-downloader" )
1178             .withVersion( "1.1" )
1179             .withArtifactId( "maven-downloader" )
1180             .withExtension( "zip" )
1181             .build();
1182
1183         assertFalse( repoContent.hasContent( selector ) );
1184
1185     }
1186
1187     @Test
1188     public void testGetItemWithNamespaceSelector() {
1189         ItemSelector selector = ArchivaItemSelector.builder( )
1190             .withNamespace( "org.apache.maven" )
1191             .build( );
1192         ContentItem item = repoContent.getItem( selector );
1193         assertNotNull( item );
1194         assertTrue( item instanceof Namespace );
1195     }
1196
1197     @Test
1198     public void testGetItemWithProjectSelector() {
1199         ItemSelector selector = ArchivaItemSelector.builder( )
1200             .withNamespace( "org.apache.maven" )
1201             .withProjectId( "shared" )
1202             .build( );
1203         ContentItem item = repoContent.getItem( selector );
1204         assertNotNull( item );
1205         assertTrue( item instanceof Project );
1206     }
1207
1208     @Test
1209     public void testGetItemWithVersionSelector() {
1210         ItemSelector selector = ArchivaItemSelector.builder( )
1211             .withNamespace( "org.apache.maven" )
1212             .withProjectId( "samplejar" )
1213             .withVersion("2.0")
1214             .build( );
1215         ContentItem item = repoContent.getItem( selector );
1216         assertNotNull( item );
1217         assertTrue( item instanceof Version );
1218     }
1219
1220     @Test
1221     public void testGetItemWithArtifactSelector() {
1222         ItemSelector selector = ArchivaItemSelector.builder( )
1223             .withNamespace( "org.apache.maven" )
1224             .withProjectId( "samplejar" )
1225             .withVersion("2.0")
1226             .withArtifactId( "samplejar" )
1227             .build( );
1228         ContentItem item = repoContent.getItem( selector );
1229         assertNotNull( item );
1230         assertTrue( item instanceof Artifact );
1231     }
1232
1233     @Test
1234     public void testGetNamespaceFromPath() {
1235         StorageAsset path = repoContent.getRepository( ).getAsset( "/org/apache/axis2" );
1236         Namespace ns = repoContent.getNamespaceFromPath( path );
1237         assertNotNull( ns );
1238         assertEquals( "org.apache.axis2", ns.getNamespace( ) );
1239
1240     }
1241
1242     @Test
1243     public void testArtifactListWithArtifactSelectorAndRelated() {
1244         ItemSelector selector = ArchivaItemSelector.builder( )
1245             .withNamespace( "org.apache.maven" )
1246             .withProjectId( "samplejar" )
1247             .withVersion( "1.0" )
1248             .withArtifactVersion( "1.0" )
1249             .withArtifactId( "samplejar" )
1250             .withExtension( "jar" )
1251             .includeRelatedArtifacts()
1252             .build( );
1253
1254         List<? extends Artifact> results = repoContent.getArtifacts( selector );
1255
1256         assertNotNull( results );
1257         assertEquals( 3, results.size( ) );
1258
1259         Artifact artifact = results.stream( ).filter( a -> a.getFileName( ).equalsIgnoreCase( "samplejar-1.0.jar" ) )
1260             .findFirst().get();
1261         assertNotNull( artifact );
1262         assertEquals( BaseArtifactTypes.MAIN, artifact.getDataType( ) );
1263
1264         artifact = results.stream( ).filter( a -> a.getFileName( ).equalsIgnoreCase( "samplejar-1.0.jar.md5" ) )
1265             .findFirst().get();
1266         assertNotNull( artifact );
1267         assertEquals( BaseArtifactTypes.RELATED, artifact.getDataType( ) );
1268         assertEquals( "md5", artifact.getExtension( ) );
1269
1270         artifact = results.stream( ).filter( a -> a.getFileName( ).equalsIgnoreCase( "samplejar-1.0.jar.sha1" ) )
1271             .findFirst().get();
1272         assertNotNull( artifact );
1273         assertEquals( BaseArtifactTypes.RELATED, artifact.getDataType( ) );
1274         assertEquals( "sha1", artifact.getExtension( ) );
1275
1276     }
1277
1278     private Path copyRepository(String repoName) throws IOException, URISyntaxException
1279     {
1280         Path tempDir = Files.createTempDirectory( "archiva-repocontent" );
1281         Path repoSource = Paths.get( Thread.currentThread( ).getContextClassLoader( ).getResource( "repositories/" + repoName ).toURI( ) );
1282         assertTrue( Files.exists( repoSource ) );
1283         FileUtils.copyDirectory( repoSource.toFile( ), tempDir.toFile() );
1284         return tempDir;
1285     }
1286
1287     private ManagedRepository createManagedRepoWithContent(String sourceRepoName) throws IOException, URISyntaxException
1288     {
1289         Path repoDir = copyRepository( sourceRepoName );
1290         MavenManagedRepository repo = createRepository( sourceRepoName, sourceRepoName, repoDir );
1291         ManagedDefaultRepositoryContent deleteRepoContent = new ManagedDefaultRepositoryContent( repo, artifactMappingProviders, fileTypes, fileLockManager );
1292         deleteRepoContent.setMavenContentHelper( contentHelper );
1293         return repo;
1294     }
1295
1296     @Test
1297     public void deleteNamespaceItem() throws IOException, URISyntaxException, ItemNotFoundException
1298     {
1299         ManagedRepository repo = createManagedRepoWithContent( "delete-repository" );
1300         ManagedRepositoryContent myRepoContent = repo.getContent( );
1301         Path repoRoot = repo.getAsset( "" ).getFilePath( );
1302         assertTrue( Files.exists(repoRoot.resolve( "org/apache/maven" )) );
1303         ArchivaItemSelector selector = ArchivaItemSelector.builder( )
1304             .withNamespace( "org.apache.maven" ).build();
1305         ContentItem item = myRepoContent.getItem( selector );
1306         assertTrue( item instanceof Namespace );
1307         myRepoContent.deleteItem( item );
1308         assertFalse( Files.exists(repoRoot.resolve( "org/apache/maven" )) );
1309         assertTrue( Files.exists(repoRoot.resolve( "org/apache" )) );
1310
1311         // Sub namespaces are deleted too
1312         assertTrue( Files.exists(repoRoot.resolve( "org/apache/test/samplejar" )) );
1313         assertTrue( Files.exists(repoRoot.resolve( "org/apache/test/sub/samplejar" )) );
1314         selector = ArchivaItemSelector.builder( )
1315             .withNamespace( "org.apache.test" ).build();
1316         item = myRepoContent.getItem( selector );
1317         assertTrue( item instanceof Namespace );
1318         myRepoContent.deleteItem( item );
1319         assertFalse( Files.exists(repoRoot.resolve( "org/apache/test/samplejar" )) );
1320         assertFalse( Files.exists(repoRoot.resolve( "org/apache/test/sub/samplejar" )) );
1321     }
1322
1323     @Test
1324     public void deleteProjectItem() throws IOException, URISyntaxException, ItemNotFoundException
1325     {
1326         ManagedRepository repo = createManagedRepoWithContent( "delete-repository" );
1327         ManagedRepositoryContent myRepoContent = repo.getContent( );
1328         Path repoRoot = repo.getAsset( "" ).getFilePath( );
1329         assertTrue( Files.exists(repoRoot.resolve( "org/apache/maven/A" )) );
1330         ArchivaItemSelector selector = ArchivaItemSelector.builder( )
1331             .withNamespace( "org.apache.maven" )
1332             .withProjectId( "A" ).build();
1333         ContentItem item = myRepoContent.getItem( selector );
1334         assertTrue( item instanceof Project );
1335         myRepoContent.deleteItem( item );
1336         assertTrue( Files.exists(repoRoot.resolve( "org/apache/maven" )) );
1337         assertTrue( Files.exists( repoRoot.resolve( "org/apache/maven/samplejar/1.0" ) ) );
1338         assertTrue( Files.exists( repoRoot.resolve( "org/apache/maven/samplejar/2.0" ) ) );
1339         assertFalse( Files.exists(repoRoot.resolve( "org/apache/maven/A" )) );
1340
1341         assertTrue( Files.exists(repoRoot.resolve( "org/apache/test/samplejar" )) );
1342         assertTrue( Files.exists(repoRoot.resolve( "org/apache/test/sub/samplejar" )) );
1343         selector = ArchivaItemSelector.builder( )
1344             .withNamespace( "org.apache.test" )
1345             .withProjectId( "samplejar" ).build();
1346         item = myRepoContent.getItem( selector );
1347         assertTrue( item instanceof Project );
1348         myRepoContent.deleteItem( item );
1349         assertFalse( Files.exists(repoRoot.resolve( "org/apache/test/samplejar" )) );
1350         assertTrue( Files.exists(repoRoot.resolve( "org/apache/test/sub/samplejar" )) );
1351     }
1352
1353     @Test
1354     public void deleteVersionItem() throws IOException, URISyntaxException, ItemNotFoundException
1355     {
1356         ManagedRepository repo = createManagedRepoWithContent( "delete-repository" );
1357         ManagedRepositoryContent myRepoContent = repo.getContent( );
1358         Path repoRoot = repo.getAsset( "" ).getFilePath( );
1359         assertTrue( Files.exists(repoRoot.resolve( "org/apache/maven/A/1.0" )) );
1360         ArchivaItemSelector selector = ArchivaItemSelector.builder( )
1361             .withNamespace( "org.apache.maven" )
1362             .withProjectId( "A" )
1363             .withVersion( "1.0" ).build();
1364         ContentItem item = myRepoContent.getItem( selector );
1365         assertTrue( item instanceof Version );
1366         myRepoContent.deleteItem( item );
1367         assertTrue( Files.exists(repoRoot.resolve( "org/apache/maven/A" )) );
1368         assertTrue( Files.exists( repoRoot.resolve( "org/apache/maven/samplejar/1.0" ) ) );
1369         assertTrue( Files.exists( repoRoot.resolve( "org/apache/maven/samplejar/2.0" ) ) );
1370         assertFalse( Files.exists(repoRoot.resolve( "org/apache/maven/A/1.0" )) );
1371
1372         assertTrue( Files.exists(repoRoot.resolve( "org/apache/test/samplejar" )) );
1373         assertTrue( Files.exists(repoRoot.resolve( "org/apache/test/sub/samplejar" )) );
1374         selector = ArchivaItemSelector.builder( )
1375             .withNamespace( "org.apache.test" )
1376             .withProjectId( "samplejar" )
1377             .withVersion( "2.0" ).build();
1378         item = myRepoContent.getItem( selector );
1379         assertTrue( item instanceof Version );
1380         myRepoContent.deleteItem( item );
1381         assertFalse( Files.exists(repoRoot.resolve( "org/apache/test/samplejar/2.0" )) );
1382         assertTrue( Files.exists(repoRoot.resolve( "org/apache/test/samplejar/1.0" )) );
1383         assertTrue( Files.exists(repoRoot.resolve( "org/apache/test/sub/samplejar/1.0" )) );
1384         assertTrue( Files.exists(repoRoot.resolve( "org/apache/test/sub/samplejar/2.0" )) );
1385     }
1386
1387     @Test
1388     public void deleteArtifactItem() throws IOException, URISyntaxException, ItemNotFoundException
1389     {
1390         ManagedRepository repo = createManagedRepoWithContent( "delete-repository" );
1391         ManagedRepositoryContent myRepoContent = repo.getContent( );
1392         Path repoRoot = repo.getAsset( "" ).getFilePath( );
1393         assertTrue( Files.exists(repoRoot.resolve( "org/apache/maven/A/1.0/A-1.0.pom" )) );
1394         assertTrue( Files.exists(repoRoot.resolve( "org/apache/maven/A/1.0/A-1.0.war" )) );
1395         ArchivaItemSelector selector = ArchivaItemSelector.builder( )
1396             .withNamespace( "org.apache.maven" )
1397             .withProjectId( "A" )
1398             .withVersion( "1.0" )
1399             .withArtifactId( "A" )
1400             .withArtifactVersion( "1.0" )
1401             .withExtension( "pom" )
1402             .build();
1403         ContentItem item = myRepoContent.getItem( selector );
1404         assertTrue( item instanceof Artifact );
1405         myRepoContent.deleteItem( item );
1406         assertTrue( Files.exists( repoRoot.resolve( "org/apache/maven/samplejar/1.0" ) ) );
1407         assertTrue( Files.exists( repoRoot.resolve( "org/apache/maven/samplejar/2.0" ) ) );
1408         assertFalse( Files.exists(repoRoot.resolve( "org/apache/maven/A/1.0/A-1.0.pom" )) );
1409         assertTrue( Files.exists(repoRoot.resolve( "org/apache/maven/A/1.0/A-1.0.war" )) );
1410
1411
1412         assertTrue( Files.exists(repoRoot.resolve( "org/apache/test/samplejar/1.0/samplejar-1.0.jar" )) );
1413         assertTrue( Files.exists(repoRoot.resolve( "org/apache/test/samplejar/1.0/samplejar-1.0.jar.md5" )) );
1414         assertTrue( Files.exists(repoRoot.resolve( "org/apache/test/samplejar/1.0/samplejar-1.0.jar.sha1" )) );
1415         assertTrue( Files.exists(repoRoot.resolve( "org/apache/test/samplejar/1.0/samplejar-1.0.pom" )) );
1416         assertTrue( Files.exists(repoRoot.resolve( "org/apache/test/samplejar/1.0/samplejar-1.0-source.jar" )) );
1417         selector = ArchivaItemSelector.builder( )
1418             .withNamespace( "org.apache.test" )
1419             .withProjectId( "samplejar" )
1420             .withVersion( "1.0" )
1421             .withArtifactId( "samplejar" )
1422             .withArtifactVersion( "1.0" )
1423             .withExtension( "jar" )
1424             .build();
1425         item = myRepoContent.getItem( selector );
1426         assertTrue( item instanceof Artifact );
1427         myRepoContent.deleteItem( item );
1428         assertFalse( Files.exists(repoRoot.resolve( "org/apache/test/samplejar/1.0/samplejar-1.0.jar" )) );
1429         assertTrue( Files.exists(repoRoot.resolve( "org/apache/test/samplejar/1.0/samplejar-1.0.jar.md5" )) );
1430         assertTrue( Files.exists(repoRoot.resolve( "org/apache/test/samplejar/1.0/samplejar-1.0.jar.sha1" )) );
1431         assertTrue( Files.exists(repoRoot.resolve( "org/apache/test/samplejar/1.0/samplejar-1.0.pom" )) );
1432         assertTrue( Files.exists(repoRoot.resolve( "org/apache/test/samplejar/1.0/samplejar-1.0-source.jar" )) );
1433         assertTrue( Files.exists(repoRoot.resolve( "org/apache/test/samplejar/2.0" )) );
1434         assertTrue( Files.exists(repoRoot.resolve( "org/apache/test/sub/samplejar/1.0" )) );
1435         assertTrue( Files.exists(repoRoot.resolve( "org/apache/test/sub/samplejar/2.0" )) );
1436
1437         selector = ArchivaItemSelector.builder( )
1438             .withNamespace( "org.apache.test" )
1439             .withProjectId( "samplejar" )
1440             .withVersion( "1.0" )
1441             .withArtifactId( "samplejar" )
1442             .withArtifactVersion( "1.0" )
1443             .withClassifier( "source" )
1444             .withExtension( "jar" )
1445             .build();
1446         item = myRepoContent.getItem( selector );
1447         assertTrue( item instanceof Artifact );
1448         myRepoContent.deleteItem( item );
1449         assertFalse( Files.exists(repoRoot.resolve( "org/apache/test/samplejar/1.0/samplejar-1.0.jar" )) );
1450         assertTrue( Files.exists(repoRoot.resolve( "org/apache/test/samplejar/1.0/samplejar-1.0.jar.md5" )) );
1451         assertTrue( Files.exists(repoRoot.resolve( "org/apache/test/samplejar/1.0/samplejar-1.0.jar.sha1" )) );
1452         assertTrue( Files.exists(repoRoot.resolve( "org/apache/test/samplejar/1.0/samplejar-1.0.pom" )) );
1453         assertFalse( Files.exists(repoRoot.resolve( "org/apache/test/samplejar/1.0/samplejar-1.0-source.jar" )) );
1454         assertTrue( Files.exists(repoRoot.resolve( "org/apache/test/samplejar/1.0/samplejar-1.0-source.jar.sha1" )) );
1455         assertTrue( Files.exists(repoRoot.resolve( "org/apache/test/samplejar/2.0" )) );
1456         assertTrue( Files.exists(repoRoot.resolve( "org/apache/test/sub/samplejar/1.0" )) );
1457         assertTrue( Files.exists(repoRoot.resolve( "org/apache/test/sub/samplejar/2.0" )) );
1458
1459         selector = ArchivaItemSelector.builder( )
1460             .withNamespace( "org.apache.test" )
1461             .withProjectId( "samplejar" )
1462             .withVersion( "1.0" )
1463             .withArtifactId( "samplejar" )
1464             .withArtifactVersion( "1.0" )
1465             .withExtension( "jar.md5" )
1466             .build();
1467         item = myRepoContent.getItem( selector );
1468         assertTrue( item instanceof Artifact );
1469         myRepoContent.deleteItem( item );
1470         assertFalse( Files.exists(repoRoot.resolve( "org/apache/test/samplejar/1.0/samplejar-1.0.jar" )) );
1471         assertFalse( Files.exists(repoRoot.resolve( "org/apache/test/samplejar/1.0/samplejar-1.0.jar.md5" )) );
1472         assertTrue( Files.exists(repoRoot.resolve( "org/apache/test/samplejar/1.0/samplejar-1.0.jar.sha1" )) );
1473         assertTrue( Files.exists(repoRoot.resolve( "org/apache/test/samplejar/1.0/samplejar-1.0.pom" )) );
1474         assertFalse( Files.exists(repoRoot.resolve( "org/apache/test/samplejar/1.0/samplejar-1.0-source.jar" )) );
1475         assertTrue( Files.exists(repoRoot.resolve( "org/apache/test/samplejar/1.0/samplejar-1.0-source.jar.sha1" )) );
1476         assertTrue( Files.exists(repoRoot.resolve( "org/apache/test/samplejar/2.0" )) );
1477         assertTrue( Files.exists(repoRoot.resolve( "org/apache/test/sub/samplejar/1.0" )) );
1478         assertTrue( Files.exists(repoRoot.resolve( "org/apache/test/sub/samplejar/2.0" )) );
1479
1480
1481     }
1482
1483     @Test
1484     public void deleteItemNotFound() throws IOException, URISyntaxException, ItemNotFoundException
1485     {
1486         ManagedRepository repo = createManagedRepoWithContent( "delete-repository" );
1487         ManagedRepositoryContent myRepoContent = repo.getContent( );
1488         Path repoRoot = repo.getAsset( "" ).getFilePath( );
1489
1490         ArchivaItemSelector selector = ArchivaItemSelector.builder( )
1491             .withNamespace( "org.apache.test2" )
1492             .build( );
1493
1494         ContentItem item = myRepoContent.getItem( selector );
1495         assertTrue( item instanceof Namespace );
1496         try
1497         {
1498             myRepoContent.deleteItem( item );
1499             assertTrue( "ItemNotFoundException expected for non existing namespace", false );
1500         } catch ( ItemNotFoundException e) {
1501         }
1502
1503         selector = ArchivaItemSelector.builder( )
1504             .withNamespace( "org.apache.test" )
1505             .withProjectId( "samplejar2" )
1506             .build( );
1507         item = myRepoContent.getItem( selector );
1508         assertTrue( item instanceof Project );
1509         try
1510         {
1511             myRepoContent.deleteItem( item );
1512             assertTrue( "ItemNotFoundException expected for non existing project", false );
1513         } catch ( ItemNotFoundException e) {
1514         }
1515
1516         selector = ArchivaItemSelector.builder( )
1517             .withNamespace( "org.apache.test" )
1518             .withProjectId( "samplejar" )
1519             .withVersion("1.1")
1520             .build( );
1521         item = myRepoContent.getItem( selector );
1522         assertTrue( item instanceof Version );
1523         try
1524         {
1525             myRepoContent.deleteItem( item );
1526             assertTrue( "ItemNotFoundException expected for non existing version", false );
1527         } catch ( ItemNotFoundException e) {
1528         }
1529
1530         selector = ArchivaItemSelector.builder( )
1531             .withNamespace( "org.apache.test" )
1532             .withProjectId( "samplejar" )
1533             .withVersion("1.0")
1534             .withArtifactId( "samplejar" )
1535             .withArtifactVersion( "1.0" )
1536             .withExtension( "jax" )
1537             .build( );
1538         item = myRepoContent.getItem( selector );
1539         assertTrue( item instanceof Artifact );
1540         try
1541         {
1542             myRepoContent.deleteItem( item );
1543             assertTrue( "ItemNotFoundException expected for non existing artifact", false );
1544         } catch ( ItemNotFoundException e) {
1545         }
1546
1547     }
1548
1549
1550     @Test
1551     public void testAddArtifact() throws IOException, URISyntaxException, LayoutException
1552     {
1553         ManagedRepository repo = createManagedRepoWithContent( "delete-repository" );
1554         ManagedRepositoryContent myRepoContent = repo.getContent( );
1555         BaseRepositoryContentLayout layout = myRepoContent.getLayout( BaseRepositoryContentLayout.class );
1556         Path repoRoot = repo.getAsset( "" ).getFilePath( );
1557
1558         Path tmpFile = Files.createTempFile( "archiva-mvn-repotest", "jar" );
1559         try( OutputStream outputStream = Files.newOutputStream( tmpFile ))
1560         {
1561             for ( int i = 0; i < 255; i++ )
1562             {
1563                 outputStream.write( "test.test.test\n".getBytes( Charset.forName( "UTF-8" ) ) );
1564             }
1565         }
1566
1567         Path file = repoRoot.resolve( "org/apache/maven/samplejar/2.0/samplejar-2.0.jar" );
1568         FileTime lmt = Files.getLastModifiedTime( file );
1569         ArchivaItemSelector selector = ArchivaItemSelector.builder( )
1570             .withNamespace( "org.apache.maven" )
1571             .withProjectId( "samplejar" )
1572             .withVersion( "2.0" )
1573             .withArtifactId( "samplejar" )
1574             .withArtifactVersion( "2.0" )
1575             .withExtension( "jar" )
1576             .build( );
1577         Artifact artifact = layout.getArtifact( selector );
1578         layout.addArtifact( tmpFile, artifact );
1579         FileTime lmtAfter = Files.getLastModifiedTime( file );
1580         assertNotEquals( lmtAfter, lmt );
1581         Reader ln = Files.newBufferedReader( file, Charset.forName( "UTF-8" ) );
1582         char[] content = new char[50];
1583         ln.read( content );
1584         assertTrue( new String( content ).startsWith( "test.test.test" ) );
1585
1586         tmpFile = Files.createTempFile( "archiva-mvn-repotest", "jar" );
1587         try( OutputStream outputStream = Files.newOutputStream( tmpFile ))
1588         {
1589             for ( int i = 0; i < 255; i++ )
1590             {
1591                 outputStream.write( "test.test.test\n".getBytes( Charset.forName( "UTF-8" ) ) );
1592             }
1593         }
1594         file = repoRoot.resolve( "org/apache/maven/samplejar/2.0/samplejar-2.0.test" );
1595         assertFalse( Files.exists( file ) );
1596         assertTrue( Files.exists( tmpFile ) );
1597         selector = ArchivaItemSelector.builder( )
1598             .withNamespace( "org.apache.maven" )
1599             .withProjectId( "samplejar" )
1600             .withVersion( "2.0" )
1601             .withArtifactId( "samplejar" )
1602             .withArtifactVersion( "2.0" )
1603             .withExtension( "test" )
1604             .build( );
1605         artifact = layout.getArtifact( selector );
1606         layout.addArtifact( tmpFile, artifact );
1607         ln = Files.newBufferedReader( file, Charset.forName( "UTF-8" ) );
1608         ln.read( content );
1609         assertTrue( new String( content ).startsWith( "test.test.test" ) );
1610     }
1611
1612     @Test
1613     public void getExistingMetadataItem() {
1614         // org/apache/maven/some-ejb/1.0
1615         ArchivaItemSelector versionSelector = ArchivaItemSelector.builder( )
1616             .withNamespace( "org.apache.maven" )
1617             .withProjectId( "some-ejb" )
1618             .withVersion( "1.0" ).build( );
1619         Version version = repoContent.getVersion( versionSelector );
1620         DataItem metaData = repoContent.getMetadataItem( version );
1621         assertTrue( metaData.exists( ) );
1622         assertEquals( "/org/apache/maven/some-ejb/1.0/maven-metadata.xml", metaData.getAsset( ).getPath( ) );
1623     }
1624
1625     @Test
1626     public void getNonExistingMetadataItem() {
1627         // org/apache/maven/some-ejb/1.0
1628         ArchivaItemSelector versionSelector = ArchivaItemSelector.builder( )
1629             .withNamespace( "javax.sql" )
1630             .withProjectId( "jdbc" )
1631             .withVersion( "2.0" ).build( );
1632         Version version = repoContent.getVersion( versionSelector );
1633         DataItem metaData = repoContent.getMetadataItem( version );
1634         assertFalse( metaData.exists( ) );
1635         assertEquals( "/javax/sql/jdbc/2.0/maven-metadata.xml", metaData.getAsset( ).getPath( ) );
1636     }
1637
1638 }