1 package org.apache.archiva.repository.maven.content;
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
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
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;
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;
69 import static org.junit.Assert.*;
72 * ManagedDefaultRepositoryContentTest
74 public class ManagedDefaultRepositoryContentTest
75 extends AbstractBaseRepositoryContentLayoutTest
77 private ManagedDefaultRepositoryContent repoContent;
83 @Named ( "archivaConfiguration#default" )
84 ArchivaConfiguration archivaConfiguration;
87 List<? extends ArtifactMappingProvider> artifactMappingProviders;
90 MavenContentHelper contentHelper;
93 @Named( "metadataReader#maven" )
94 MavenMetadataReader metadataReader;
97 FileLockManager fileLockManager;
99 private Path getRepositoryPath(String repoName) {
102 return Paths.get( Thread.currentThread( ).getContextClassLoader( ).getResource( "repositories/" + repoName ).toURI( ) );
104 catch ( URISyntaxException e )
106 throw new RuntimeException( "Could not resolve repository path " + e.getMessage( ), e );
114 Path repoDir = getRepositoryPath( "default-repository" );
116 MavenManagedRepository repository = createRepository( "testRepo", "Unit Test Repo", repoDir );
118 FileType fileType = archivaConfiguration.getConfiguration().getRepositoryScanning().getFileTypes().get( 0 );
119 fileType.addPattern( "**/*.xml" );
120 assertEquals( FileTypes.ARTIFACTS, fileType.getId() );
122 fileTypes.afterConfigurationChange( null, "fileType", null );
124 repoContent = new ManagedDefaultRepositoryContent(repository, artifactMappingProviders, fileTypes, fileLockManager);
125 repoContent.setMavenContentHelper( contentHelper );
126 repoContent.setMetadataReader( metadataReader );
128 //repoContent = (ManagedRepositoryContent) lookup( ManagedRepositoryContent.class, "default" );
132 public void testGetVersionsSnapshotA()
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" } );
143 public void testToMetadataPathFromProjectReference()
145 ProjectReference reference = new ProjectReference();
146 reference.setGroupId( "com.foo" );
147 reference.setArtifactId( "foo-tool" );
149 assertEquals( "com/foo/foo-tool/maven-metadata.xml", repoContent.toMetadataPath( reference ) );
153 public void testToMetadataPathFromVersionReference()
155 VersionedReference reference = new VersionedReference();
156 reference.setGroupId( "com.foo" );
157 reference.setArtifactId( "foo-tool" );
158 reference.setVersion( "1.0" );
160 assertEquals( "com/foo/foo-tool/1.0/maven-metadata.xml", repoContent.toMetadataPath( reference ) );
165 public void testToPathOnNullArtifactReference()
169 ArtifactReference reference = null;
170 repoContent.toPath( reference );
171 fail( "Should have failed due to null artifact reference." );
173 catch ( IllegalArgumentException e )
180 protected Artifact createArtifact( String groupId, String artifactId, String version, String classifier, String type ) throws LayoutException
182 ItemSelector selector = createItemSelector( groupId, artifactId, version, classifier, type );
183 return repoContent.getLayout( BaseRepositoryContentLayout.class ).getArtifact( selector );
187 public void testExcludeMetadataFile()
190 assertVersions( "include_xml", "1.0", new String[]{ "1.0" } );
194 private void assertVersions( String artifactId, String version, String[] expectedVersions )
197 // Use the test metadata-repository, which is already setup for
198 // These kind of version tests.
199 Path repoDir = getRepositoryPath( "metadata-repository" );
200 ((EditableManagedRepository)repoContent.getRepository()).setLocation( repoDir.toAbsolutePath().toUri() );
202 // Request the versions.
204 // Sort the list (for asserts later)
205 final VersionComparator comparator = new VersionComparator( );
207 ItemSelector selector = ArchivaItemSelector.builder( )
208 .withNamespace( "org.apache.archiva.metadata.tests" )
209 .withProjectId( artifactId )
210 .withVersion( version )
212 List<String> versions = repoContent.getVersions( selector ).stream()
213 .map(v -> v.getId()).sorted( comparator ).collect( Collectors.toList());
214 assertArrayEquals( expectedVersions, versions.toArray( ) );
219 private void assertArtifactVersions( String artifactId, String version, String[] expectedVersions )
222 // Use the test metadata-repository, which is already setup for
223 // These kind of version tests.
224 Path repoDir = getRepositoryPath( "metadata-repository" );
225 ((EditableManagedRepository)repoContent.getRepository()).setLocation( repoDir.toAbsolutePath().toUri() );
227 // Request the versions.
229 // Sort the list (for asserts later)
230 final VersionComparator comparator = new VersionComparator( );
232 ItemSelector selector = ArchivaItemSelector.builder( )
233 .withNamespace( "org.apache.archiva.metadata.tests" )
234 .withProjectId( artifactId )
235 .withVersion( version )
237 List<String> versions = repoContent.getArtifactVersions( selector ).stream()
238 .sorted( comparator ).collect( Collectors.toList());
239 assertArrayEquals( expectedVersions, versions.toArray( ) );
245 public void getTestGetProjectWithIllegalArgs() {
246 ItemSelector selector = ArchivaItemSelector.builder( )
247 .withNamespace( "org.apache" )
248 .withVersion( "1.0" )
252 repoContent.getProject( selector );
253 assertFalse( "Should throw IllegalArgumentException if no project id is given", true );
254 } catch (IllegalArgumentException e) {
256 assertTrue( e.getMessage( ).contains( "Project id must be set" ) );
261 public void getTestGetVersionWithIllegalArgs() {
262 ItemSelector selector = ArchivaItemSelector.builder( )
263 .withNamespace( "org.apache.maven" )
264 .withVersion( "1.0" )
268 repoContent.getVersion( selector );
269 assertFalse( "Should throw IllegalArgumentException if no project id is given", true );
270 } catch (IllegalArgumentException e) {
272 assertTrue( e.getMessage( ).contains( "Project id must be set" ) );
276 selector = ArchivaItemSelector.builder( )
277 .withNamespace( "org.apache.maven" )
278 .withProjectId( "shared" )
282 repoContent.getVersion( selector );
283 assertFalse( "Should throw IllegalArgumentException if no version is given", true );
284 } catch (IllegalArgumentException e) {
286 assertTrue( e.getMessage( ).contains( "Version must be set" ) );
291 public void getTestGetArtifactWithIllegalArgs() {
292 ItemSelector selector = ArchivaItemSelector.builder( )
293 .withNamespace( "org.apache.maven" )
294 .withVersion( "1.0" )
295 .withArtifactId( "shared" )
296 .withArtifactVersion("1.0")
300 repoContent.getArtifact( selector );
301 assertFalse( "Should throw IllegalArgumentException if no project id is given", true );
302 } catch (IllegalArgumentException e) {
304 assertTrue( e.getMessage( ).contains( "Project id must be set" ) );
308 selector = ArchivaItemSelector.builder( )
309 .withNamespace( "org.apache.maven" )
310 .withProjectId( "shared" )
311 .withArtifactId( "shared" )
312 .withArtifactVersion("1.0")
316 repoContent.getArtifact( selector );
317 assertFalse( "Should throw IllegalArgumentException if no version is given", true );
318 } catch (IllegalArgumentException e) {
320 assertTrue( e.getMessage( ).contains( "Version must be set" ) );
323 selector = ArchivaItemSelector.builder( )
324 .withNamespace( "org.apache.maven" )
325 .withProjectId( "shared" )
327 .withArtifactVersion("1.0")
331 repoContent.getArtifact( selector );
332 assertFalse( "Should throw IllegalArgumentException if no artifact id is given", true );
333 } catch (IllegalArgumentException e) {
335 assertTrue( e.getMessage( ).contains( "Artifact id must be set" ) );
342 public void testGetProjects() {
343 ItemSelector selector = ArchivaItemSelector.builder( )
344 .withNamespace( "org.apache.maven" ).build();
345 Namespace ns = repoContent.getNamespace( selector );
347 List<? extends Project> projects = repoContent.getProjects( ns );
348 assertEquals( 12, projects.size( ) );
349 String[] expected = new String[]{
350 "A", "B", "C", "archiva", "discovery", "maven-parent", "samplejar", "shared", "some-ejb", "test",
353 Object[] actual = projects.stream( ).map( p -> p.getId( ) ).sorted( ).toArray( );
354 assertArrayEquals( expected, actual);
358 public void testGetProjectsWithSelector() {
359 ItemSelector selector = ArchivaItemSelector.builder( )
360 .withNamespace( "org.apache.maven" ).build();
361 List<? extends Project> projects = repoContent.getProjects( selector );
362 assertEquals( 12, projects.size( ) );
363 String[] expected = new String[]{
364 "A", "B", "C", "archiva", "discovery", "maven-parent", "samplejar", "shared", "some-ejb", "test",
367 Object[] actual = projects.stream( ).map( p -> p.getId( ) ).sorted( ).toArray( );
368 assertArrayEquals( expected, actual);
372 public void testGetVersionsWithIllegalSelector() {
373 ItemSelector selector = ArchivaItemSelector.builder( )
374 .withNamespace( "org.apache.maven" ).build();
377 List<? extends Version> versions = repoContent.getVersions( selector );
378 assertFalse( "IllegalArgumentException expected, when project id not set", true );
379 } catch (IllegalArgumentException e) {
380 assertEquals( "Project id not set, while retrieving versions.", e.getMessage( ) );
385 public void testGetVersionsWithSelector() {
386 ItemSelector selector = ArchivaItemSelector.builder( )
387 .withNamespace( "org.apache.maven" )
388 .withProjectId( "samplejar" ).build();
389 List<? extends Version> versions = repoContent.getVersions( selector );
390 assertNotNull( versions );
391 assertEquals( 2, versions.size( ) );
396 protected ItemSelector toItemSelector( String path ) throws LayoutException
398 return repoContent.toItemSelector( path );
402 protected String toPath( Artifact reference )
404 return repoContent.toPath( reference );
408 protected String toPath( ItemSelector selector ) {
409 return repoContent.toPath( selector );
413 protected ManagedRepositoryContent getManaged( )
419 protected RepositoryContent getContent( )
424 private Path setupRepoCopy( String source, String target) throws IOException
426 Path defaultRepo = getRepositoryPath( source );
427 Path newRepo = defaultRepo.getParent( ).resolve( target );
428 FileUtils.copyDirectory( defaultRepo.toFile( ), newRepo.toFile( ) );
430 MavenManagedRepository repository = createRepository( "testRepo", "Unit Test Repo", newRepo );
432 FileType fileType = archivaConfiguration.getConfiguration().getRepositoryScanning().getFileTypes().get( 0 );
433 fileType.addPattern( "**/*.xml" );
434 assertEquals( FileTypes.ARTIFACTS, fileType.getId() );
436 fileTypes.afterConfigurationChange( null, "fileType", null );
438 repoContent = new ManagedDefaultRepositoryContent(repository, artifactMappingProviders, fileTypes, fileLockManager);
445 public void testGetArtifactStreamWithVersionSelector() {
446 ItemSelector selector = ArchivaItemSelector.builder( )
447 .withNamespace( "javax.sql" )
448 .withProjectId( "jdbc" )
449 .withVersion( "2.0" ).build();
450 try(Stream<? extends Artifact> stream = repoContent.newArtifactStream( selector ))
452 assertNotNull( stream );
453 List<? extends Artifact> results = stream.collect( Collectors.toList( ) );
454 checkArtifactListWithVersionSelector1( results );
459 public void testGetArtifactListWithVersionSelector() {
460 ItemSelector selector = ArchivaItemSelector.builder( )
461 .withNamespace( "javax.sql" )
462 .withProjectId( "jdbc" )
463 .withVersion( "2.0" ).build();
464 List<? extends Artifact> results = repoContent.getArtifacts( selector );
465 checkArtifactListWithVersionSelector1( results );
468 private void checkArtifactListWithVersionSelector1( List<? extends Artifact> results )
470 assertNotNull( results );
471 assertEquals( 2, results.size( ) );
472 Artifact mainArtifact = results.stream( ).filter( a -> a.getFileName( ).equals( "jdbc-2.0.jar" ) ).findFirst( ).get( );
473 assertNotNull( mainArtifact );
474 assertEquals( BaseArtifactTypes.MAIN, mainArtifact.getDataType( ) );
475 Artifact metaArtifact = results.stream( ).filter( a -> a.getFileName( ).equals( "maven-metadata-repository.xml" ) ).findFirst( ).get( );
476 assertNotNull( metaArtifact );
477 assertEquals( MavenTypes.REPOSITORY_METADATA, metaArtifact.getDataType( ) );
481 public void testGetArtifactStreamWithVersionSelector2() {
482 ItemSelector selector = ArchivaItemSelector.builder( )
483 .withNamespace( "org.apache.axis2" )
484 .withProjectId( "axis2" )
485 .withVersion( "1.3-SNAPSHOT" ).build();
486 try(Stream<? extends Artifact> stream = repoContent.newArtifactStream( selector ))
488 assertNotNull( stream );
489 List<? extends Artifact> results = stream.collect( Collectors.toList( ) );
490 checkArtifactListWithVersionSelector2( results );
495 public void testGetArtifactListWithVersionSelector2() {
496 ItemSelector selector = ArchivaItemSelector.builder( )
497 .withNamespace( "org.apache.axis2" )
498 .withProjectId( "axis2" )
499 .withVersion( "1.3-SNAPSHOT" ).build();
500 List<? extends Artifact> results = repoContent.getArtifacts( selector );
501 checkArtifactListWithVersionSelector2( results );
504 private void checkArtifactListWithVersionSelector2( List<? extends Artifact> results )
506 assertNotNull( results );
507 assertEquals( 39, results.size( ) );
509 Artifact artifact = results.stream( ).filter( a -> a.getFileName( ).equals( "axis2-1.3-20070725.210059-1.pom" ) )
510 .findFirst( ).get( );
512 assertNotNull( artifact );
513 assertEquals( "pom", artifact.getExtension( ) );
514 assertEquals( BaseArtifactTypes.MAIN, artifact.getDataType( ) );
515 assertEquals( "1.3-SNAPSHOT", artifact.getVersion( ).getId( ) );
516 assertEquals( "1.3-20070725.210059-1", artifact.getArtifactVersion( ) );
517 assertEquals( ".pom", artifact.getRemainder( ) );
518 assertEquals( "axis2", artifact.getId( ) );
519 assertEquals( "axis2", artifact.getVersion( ).getProject( ).getId( ) );
520 assertEquals( "org.apache.axis2", artifact.getVersion( ).getProject( ).getNamespace( ).getId( ) );
521 assertEquals( "", artifact.getClassifier( ) );
522 assertEquals( "pom", artifact.getType( ) );
525 artifact = results.stream( ).filter( a -> a.getFileName( ).equals( "axis2-1.3-20070725.210059-1.pom.md5" ) )
526 .findFirst( ).get( );
528 assertNotNull( artifact );
529 assertEquals( "md5", artifact.getExtension( ) );
530 assertEquals( BaseArtifactTypes.RELATED, artifact.getDataType( ) );
531 assertEquals( "1.3-SNAPSHOT", artifact.getVersion( ).getId( ) );
532 assertEquals( "1.3-20070725.210059-1", artifact.getArtifactVersion( ) );
533 assertEquals( ".pom.md5", artifact.getRemainder( ) );
534 assertEquals( "axis2", artifact.getId( ) );
535 assertEquals( "axis2", artifact.getVersion( ).getProject( ).getId( ) );
536 assertEquals( "org.apache.axis2", artifact.getVersion( ).getProject( ).getNamespace( ).getId( ) );
537 assertEquals( "", artifact.getClassifier( ) );
538 assertEquals( "md5", artifact.getType( ) );
542 artifact = results.stream( ).filter( a -> a.getFileName( ).equals( "maven-metadata.xml" ) )
543 .findFirst( ).get( );
544 assertNotNull( artifact );
545 assertEquals( BaseArtifactTypes.METADATA, artifact.getDataType( ) );
546 assertEquals( "1.3-SNAPSHOT", artifact.getVersion( ).getId( ) );
547 assertEquals( "xml", artifact.getExtension( ) );
551 public void testGetArtifactListWithArtifactSelector1() {
552 ItemSelector selector = ArchivaItemSelector.builder( )
553 .withNamespace( "org.apache.axis2" )
554 .withProjectId( "axis2" )
555 .withVersion( "1.3-SNAPSHOT" )
556 .withArtifactVersion( "1.3-20070731.113304-21" )
557 .withExtension( "pom" )
559 List<? extends Artifact> results = repoContent.getArtifacts( selector );
560 checkArtifactListWithArtifactSelector1( results );
564 public void testGetArtifactStreamWithArtifactSelector1() {
565 ItemSelector selector = ArchivaItemSelector.builder( )
566 .withNamespace( "org.apache.axis2" )
567 .withProjectId( "axis2" )
568 .withVersion( "1.3-SNAPSHOT" )
569 .withArtifactVersion( "1.3-20070731.113304-21" )
570 .withExtension( "pom" )
572 try(Stream<? extends Artifact> results = repoContent.newArtifactStream( selector ))
574 checkArtifactListWithArtifactSelector1( results.collect( Collectors.toList()) );
578 private void checkArtifactListWithArtifactSelector1( List<? extends Artifact> results )
580 assertNotNull( results );
581 assertEquals( 1, results.size( ) );
582 Artifact artifact = results.get( 0 );
583 assertEquals( "pom", artifact.getExtension( ) );
584 assertEquals( BaseArtifactTypes.MAIN, artifact.getDataType( ) );
588 public void testGetArtifactListWithArtifactSelector2() {
589 ItemSelector selector = ArchivaItemSelector.builder( )
590 .withNamespace( "org.apache.axis2" )
591 .withProjectId( "axis2" )
592 .withVersion( "1.3-SNAPSHOT" )
593 .withArtifactVersion( "1.3-20070731.113304-21" )
594 .withExtension( "pom" )
595 .includeRelatedArtifacts()
597 List<? extends Artifact> results = repoContent.getArtifacts( selector );
599 checkArtifactListWithArtifactSelector2( results );
604 public void testGetArtifactStreamWithArtifactSelector2() {
605 ItemSelector selector = ArchivaItemSelector.builder( )
606 .withNamespace( "org.apache.axis2" )
607 .withProjectId( "axis2" )
608 .withVersion( "1.3-SNAPSHOT" )
609 .withArtifactVersion( "1.3-20070731.113304-21" )
610 .withExtension( "pom" )
611 .includeRelatedArtifacts()
613 try(Stream<? extends Artifact> results = repoContent.newArtifactStream( selector ))
615 checkArtifactListWithArtifactSelector2( results.collect( Collectors.toList()) );
619 private void checkArtifactListWithArtifactSelector2( List<? extends Artifact> results )
621 assertNotNull( results );
622 assertEquals( 3, results.size( ) );
623 Artifact artifact = results.stream( ).filter( a -> a.getFileName( ).equalsIgnoreCase( "axis2-1.3-20070731.113304-21.pom" ) )
624 .findFirst( ).get( );
625 assertNotNull( artifact );
626 assertEquals( "pom", artifact.getExtension( ) );
627 assertEquals( BaseArtifactTypes.MAIN, artifact.getDataType( ) );
629 artifact = results.stream( ).filter( a -> a.getFileName( ).equalsIgnoreCase( "axis2-1.3-20070731.113304-21.pom.sha1" ) )
630 .findFirst( ).get( );
631 assertNotNull( artifact );
632 assertEquals( "sha1", artifact.getExtension( ) );
633 assertEquals( BaseArtifactTypes.RELATED, artifact.getDataType( ) );
638 public void testArtifactListWithProjectSelector() {
639 ItemSelector selector = ArchivaItemSelector.builder( )
640 .withNamespace( "org.apache.maven.shared" )
641 .withProjectId( "maven-downloader" )
643 List<? extends Artifact> results = repoContent.getArtifacts( selector );
644 checkArtifactListWithProjectSelector( results );
649 public void testArtifactStreamWithProjectSelector() {
650 ItemSelector selector = ArchivaItemSelector.builder( )
651 .withNamespace( "org.apache.maven.shared" )
652 .withProjectId( "maven-downloader" )
654 Stream<? extends Artifact> results = repoContent.newArtifactStream( selector );
655 checkArtifactListWithProjectSelector( results.collect( Collectors.toList()) );
659 private void checkArtifactListWithProjectSelector( List<? extends Artifact> results )
661 assertNotNull( results );
662 assertEquals( 27, results.size( ) );
664 Artifact artifact = results.stream( ).filter( a -> a.getFileName( ).equalsIgnoreCase( "maven-metadata.xml" ) )
665 .findFirst( ).get( );
666 assertNotNull( artifact );
667 assertEquals( "xml", artifact.getExtension( ) );
668 assertEquals( BaseArtifactTypes.METADATA, artifact.getDataType( ) );
670 artifact = results.stream( ).filter( a -> a.getFileName( ).equalsIgnoreCase( "maven-downloader-1.0-sources.jar" ) )
671 .findFirst( ).get( );
672 assertNotNull( artifact );
673 assertEquals( BaseArtifactTypes.MAIN, artifact.getDataType( ) );
674 assertEquals( "sources", artifact.getClassifier( ) );
675 assertEquals( "java-source", artifact.getType( ) );
677 artifact = results.stream( ).filter( a -> a.getFileName( ).equalsIgnoreCase( "maven-downloader-1.0-sources.jar.sha1" ) )
678 .findFirst( ).get( );
679 assertNotNull( artifact );
680 assertEquals( BaseArtifactTypes.RELATED, artifact.getDataType( ) );
681 assertEquals( "sources", artifact.getClassifier( ) );
682 assertEquals( "sha1", artifact.getType( ) );
683 assertEquals( ".jar.sha1", artifact.getRemainder( ) );
687 public void testArtifactListWithNamespaceSelector() {
688 ItemSelector selector = ArchivaItemSelector.builder( )
689 .withNamespace( "org.multilevel" )
691 List<? extends Artifact> results = repoContent.getArtifacts( selector );
692 assertNotNull( results );
693 assertEquals( 3, results.size( ) );
694 assertTrue( results.get( 0 ).getFileName( ).startsWith( "testproj1" ) );
698 public void testArtifactListWithNamespaceSelectorRecursive() {
699 ItemSelector selector = ArchivaItemSelector.builder( )
700 .withNamespace( "org.multilevel" )
703 List<? extends Artifact> results = repoContent.getArtifacts( selector );
704 checkArtifactListWithNamespaceSelectorRecursive( results );
708 public void testArtifactStreamWithNamespaceSelectorRecursive() {
709 ItemSelector selector = ArchivaItemSelector.builder( )
710 .withNamespace( "org.multilevel" )
713 Stream<? extends Artifact> results = repoContent.newArtifactStream( selector );
714 checkArtifactListWithNamespaceSelectorRecursive( results.collect( Collectors.toList()) );
717 private void checkArtifactListWithNamespaceSelectorRecursive( List<? extends Artifact> results )
719 assertNotNull( results );
720 assertEquals( 6, results.size( ) );
722 Artifact artifact = results.stream( ).filter( a -> a.getFileName( ).equalsIgnoreCase( "testproj2-1.0.pom" ) )
723 .findFirst( ).get( );
724 assertNotNull( artifact );
725 assertEquals( 6, artifact.getAsset( ).getParent( ).getPath( ).split( "/" ).length );
727 artifact = results.stream( ).filter( a -> a.getFileName( ).equalsIgnoreCase( "testproj1-1.0.pom" ) )
728 .findFirst( ).get( );
729 assertNotNull( artifact );
730 assertEquals( 5, artifact.getAsset( ).getParent( ).getPath( ).split( "/" ).length );
735 public void testArtifactListWithArtifactSelector1() {
736 ItemSelector selector = ArchivaItemSelector.builder( )
737 .withNamespace( "org.apache.maven" )
738 .withProjectId( "test" )
739 .withVersion( "1.0-SNAPSHOT" )
740 .withArtifactId( "test" )
741 .withArtifactVersion( "1.0-20050611.112233-1" )
744 List<? extends Artifact> results = repoContent.getArtifacts( selector );
746 assertNotNull( results );
747 assertEquals( 1, results.size( ) );
749 Artifact artifact = results.stream( ).filter( a -> a.getFileName( ).equalsIgnoreCase( "test-1.0-20050611.112233-1.jar" ) )
751 assertNotNull( artifact );
752 assertEquals( "", artifact.getClassifier( ) );
756 public void testArtifactListWithArtifactSelector2() {
757 ItemSelector selector = ArchivaItemSelector.builder( )
758 .withNamespace( "org.apache.maven" )
759 .withProjectId( "test" )
760 .withVersion( "1.0-SNAPSHOT" )
761 .withClassifier( "*" )
762 .withArtifactId( "test" )
763 .withArtifactVersion( "1.0-20050611.112233-1" )
766 List<? extends Artifact> results = repoContent.getArtifacts( selector );
768 assertNotNull( results );
769 assertEquals( 2, results.size( ) );
771 Artifact artifact = results.stream( ).filter( a -> a.getFileName( ).equalsIgnoreCase( "test-1.0-20050611.112233-1-javadoc.jar" ) )
773 assertNotNull( artifact );
774 assertEquals( "javadoc", artifact.getClassifier( ) );
775 assertEquals( "javadoc", artifact.getType( ) );
779 public void testArtifactListWithArtifactSelector3() {
780 ItemSelector selector = ArchivaItemSelector.builder( )
781 .withNamespace( "org.apache.maven" )
782 .withProjectId( "test" )
783 .withVersion( "1.0-SNAPSHOT" )
784 .withClassifier( "*" )
785 .withArtifactVersion( "1.0-20050611.112233-1" )
788 List<? extends Artifact> results = repoContent.getArtifacts( selector );
790 assertNotNull( results );
791 assertEquals( 3, results.size( ) );
793 Artifact artifact = results.stream( ).filter( a -> a.getFileName( ).equalsIgnoreCase( "test-1.0-20050611.112233-1-javadoc.jar" ) )
795 assertNotNull( artifact );
796 assertEquals( "javadoc", artifact.getClassifier( ) );
797 assertEquals( "javadoc", artifact.getType( ) );
799 artifact = results.stream( ).filter( a -> a.getFileName( ).equalsIgnoreCase( "wrong-artifactId-1.0-20050611.112233-1.jar" ) )
801 assertNotNull( artifact );
802 assertEquals( "", artifact.getClassifier( ) );
803 assertEquals( "wrong-artifactId", artifact.getId( ) );
807 public void testArtifactListWithArtifactSelector4() {
808 ItemSelector selector = ArchivaItemSelector.builder( )
809 .withNamespace( "org.apache.maven" )
810 .withProjectId( "test" )
811 .withVersion( "1.0-SNAPSHOT" )
812 .withClassifier( "" )
815 List<? extends Artifact> results = repoContent.getArtifacts( selector );
817 assertNotNull( results );
818 assertEquals( 5, results.size( ) );
820 Artifact artifact = results.stream( ).filter( a -> a.getFileName( ).equalsIgnoreCase( "test-1.0-20050611.112233-1-javadoc.jar" ) )
822 assertNotNull( artifact );
823 assertEquals( "javadoc", artifact.getClassifier( ) );
824 assertEquals( "javadoc", artifact.getType( ) );
826 artifact = results.stream( ).filter( a -> a.getFileName( ).equalsIgnoreCase( "wrong-artifactId-1.0-20050611.112233-1.jar" ) )
828 assertNotNull( artifact );
829 assertEquals( "", artifact.getClassifier( ) );
830 assertEquals( "wrong-artifactId", artifact.getId( ) );
832 artifact = results.stream( ).filter( a -> a.getFileName( ).equalsIgnoreCase( "wrong-artifactId-1.0-20050611.1122x-1.jar" ) )
834 assertNotNull( artifact );
835 assertEquals( "", artifact.getClassifier( ) );
836 assertEquals( "wrong-artifactId", artifact.getId( ) );
837 assertEquals( "", artifact.getArtifactVersion( ) );
839 artifact = results.stream( ).filter( a -> a.getFileName( ).equalsIgnoreCase( "test-1.0-20050611.1122x-1.jar" ) )
841 assertNotNull( artifact );
842 assertEquals( "", artifact.getClassifier( ) );
843 assertEquals( "test", artifact.getId( ) );
844 assertEquals( "1.0-20050611.1122x-1", artifact.getArtifactVersion( ) );
849 public void testArtifactListWithArtifactSelectorWithClassifier() {
850 ItemSelector selector = ArchivaItemSelector.builder( )
851 .withNamespace( "org.apache.maven" )
852 .withProjectId( "test" )
853 .withVersion( "1.0-SNAPSHOT" )
854 .withArtifactId( "test" )
855 .withClassifier( "javadoc" )
856 .withArtifactVersion( "1.0-20050611.112233-1" )
859 List<? extends Artifact> results = repoContent.getArtifacts( selector );
861 assertNotNull( results );
862 assertEquals( 1, results.size( ) );
864 Artifact artifact = results.stream( ).filter( a -> a.getFileName( ).equalsIgnoreCase( "test-1.0-20050611.112233-1-javadoc.jar" ) )
866 assertNotNull( artifact );
867 assertEquals( "javadoc", artifact.getClassifier( ) );
868 assertEquals( "javadoc", artifact.getType( ) );
872 public void testArtifactListWithArtifactSelectorWrongArtifact() {
873 ItemSelector selector = ArchivaItemSelector.builder( )
874 .withNamespace( "org.apache.maven" )
875 .withProjectId( "test" )
876 .withVersion( "1.0-SNAPSHOT" )
877 .withArtifactId( "wrong-artifactId" )
878 .withArtifactVersion( "1.0-20050611.112233-1" )
881 List<? extends Artifact> results = repoContent.getArtifacts( selector );
883 assertNotNull( results );
884 assertEquals( 1, results.size( ) );
886 Artifact artifact = results.stream( ).filter( a -> a.getFileName( ).equalsIgnoreCase( "wrong-artifactId-1.0-20050611.112233-1.jar" ) )
888 assertNotNull( artifact );
892 public void testArtifactListWithArtifactSelectorVersionPattern() {
893 ItemSelector selector = ArchivaItemSelector.builder( )
894 .withNamespace( "org.apache.maven" )
895 .withProjectId( "test" )
896 .withVersion( "1.0-SNAPSHOT" )
897 .withArtifactVersion( "1.0-*" )
900 List<? extends Artifact> results = repoContent.getArtifacts( selector );
902 assertNotNull( results );
903 assertEquals( 5, results.size( ) );
905 Artifact artifact = results.stream( ).filter( a -> a.getFileName( ).equalsIgnoreCase( "wrong-artifactId-1.0-20050611.112233-1.jar" ) )
907 assertNotNull( artifact );
912 public void testNewItemStreamWithNamespace1() {
913 ItemSelector selector = ArchivaItemSelector.builder( )
914 .withNamespace( "org.apache.axis2" )
917 Stream<? extends ContentItem> stream = repoContent.newItemStream( selector, false );
918 List<? extends ContentItem> result = stream.collect( Collectors.toList( ) );
919 assertEquals( 41, result.size( ) );
920 ContentItem item = result.get( 39 );
921 Version version = item.adapt( Version.class );
922 assertNotNull( version );
923 assertEquals( "1.3-SNAPSHOT", version.getId( ) );
924 Project project = result.get( 40 ).adapt( Project.class );
925 assertNotNull( project );
926 assertEquals( "axis2", project.getId( ) );
927 assertTrue( result.stream( ).anyMatch( a -> "axis2-1.3-20070725.210059-1.pom".equals( a.getAsset( ).getName( ) ) ) );
931 public void testNewItemStreamWithNamespace2() {
932 ItemSelector selector = ArchivaItemSelector.builder( )
933 .withNamespace( "org.apache.maven" )
937 Stream<? extends ContentItem> stream = repoContent.newItemStream( selector, false );
938 List<? extends ContentItem> result = stream.collect( Collectors.toList( ) );
939 assertEquals( 170, result.size( ) );
940 assertEquals( 92, result.stream( ).filter( a -> a instanceof DataItem ).count( ) );
944 public void testGetArtifactFromContentItem() {
945 ItemSelector selector = ArchivaItemSelector.builder( )
946 .withNamespace( "org.apache.maven" ).build();
947 Namespace ns = repoContent.getNamespace( selector );
948 List<? extends Artifact> artifacts = repoContent.getArtifacts( ns );
949 assertNotNull( artifacts );
950 assertEquals( 39, artifacts.size( ) );
951 List<? extends Artifact> artifacts2 = repoContent.getArtifacts( (ContentItem)ns );
952 assertArrayEquals( artifacts.toArray(), artifacts2.toArray() );
954 selector = ArchivaItemSelector.builder( )
955 .withNamespace( "org.apache.maven.shared" )
956 .withProjectId( "maven-downloader" )
958 Project project = repoContent.getProject( selector );
959 artifacts = repoContent.getArtifacts( project );
960 assertNotNull( artifacts );
961 assertEquals( 27, artifacts.size( ) );
962 artifacts2 = repoContent.getArtifacts( (ContentItem)project );
963 assertArrayEquals( artifacts.toArray(), artifacts2.toArray() );
965 selector = ArchivaItemSelector.builder( )
966 .withNamespace( "org.apache.maven.shared" )
967 .withProjectId( "maven-downloader" )
968 .withVersion( "1.1" )
970 Version version = repoContent.getVersion( selector );
971 artifacts = repoContent.getArtifacts( version );
972 assertNotNull( artifacts );
973 assertEquals( 12, artifacts.size( ) );
974 artifacts2 = repoContent.getArtifacts( (ContentItem)version );
975 assertArrayEquals( artifacts.toArray(), artifacts2.toArray() );
980 public void testGetRelatedArtifactsFromArtifact() {
981 ItemSelector selector = ArchivaItemSelector.builder( )
982 .withNamespace( "org.apache.maven.shared" )
983 .withProjectId( "maven-downloader" )
984 .withVersion( "1.1" )
985 .withExtension( "jar" )
986 .withArtifactId( "maven-downloader" ).build( );
988 Artifact artifact = repoContent.getArtifact( selector );
989 assertNotNull( artifact );
990 List<? extends Artifact> artifacts = repoContent.getArtifacts( artifact );
991 assertNotNull( artifacts );
992 assertEquals( 2, artifacts.size( ) );
997 public void testToItemFromPath() throws LayoutException
999 String path = "/org/apache/maven/shared";
1000 ContentItem item = repoContent.toItem( path );
1001 assertNotNull( item );
1002 assertTrue( item instanceof ArchivaContentItem );
1004 path = "/org/apache/maven/shared/maven-downloader";
1005 item = repoContent.toItem( path );
1006 assertNotNull( item );
1007 assertTrue( item instanceof ArchivaContentItem );
1009 path = "/org/apache/maven/shared/maven-downloader/1.1";
1010 item = repoContent.toItem( path );
1011 assertNotNull( item );
1012 assertTrue( item instanceof ArchivaContentItem );
1014 path = "/org/apache/maven/shared/maven-downloader/1.1/maven-downloader-1.1.jar";
1015 item = repoContent.toItem( path );
1016 assertNotNull( item );
1017 assertTrue( item instanceof DataItem );
1022 public void testToItemFromAssetPath() throws LayoutException
1024 StorageAsset path = repoContent.getRepository().getAsset("/org/apache/maven/shared");
1025 ContentItem item = repoContent.toItem( path );
1026 assertNotNull( item );
1027 assertTrue( item instanceof ArchivaContentItem );
1029 path = repoContent.getRepository( ).getAsset( "/org/apache/maven/shared/maven-downloader" );
1030 item = repoContent.toItem( path );
1031 assertNotNull( item );
1032 assertTrue( item instanceof ArchivaContentItem );
1034 path = repoContent.getRepository( ).getAsset( "/org/apache/maven/shared/maven-downloader/1.1" );
1035 item = repoContent.toItem( path );
1036 assertNotNull( item );
1037 assertTrue( item instanceof ArchivaContentItem );
1039 path = repoContent.getRepository( ).getAsset( "/org/apache/maven/shared/maven-downloader/1.1/maven-downloader-1.1.jar" );
1040 item = repoContent.toItem( path );
1041 assertNotNull( item );
1042 assertTrue( item instanceof DataItem );
1047 public void testHasContent() throws LayoutException
1049 ItemSelector selector = ArchivaItemSelector.builder( )
1050 .withNamespace( "org.apache.maven.shared" )
1051 .withProjectId( "maven-downloader" )
1052 .withVersion( "1.1" )
1053 .withArtifactId( "maven-downloader" )
1054 .withExtension( "jar" )
1057 assertTrue( repoContent.hasContent( selector ) );
1059 selector = ArchivaItemSelector.builder( )
1060 .withNamespace( "org.apache.maven.shared" )
1061 .withProjectId( "maven-downloader" )
1062 .withVersion( "1.1" )
1063 .withArtifactId( "maven-downloader" )
1064 .withExtension( "zip" )
1067 assertFalse( repoContent.hasContent( selector ) );
1072 public void testGetItemWithNamespaceSelector() {
1073 ItemSelector selector = ArchivaItemSelector.builder( )
1074 .withNamespace( "org.apache.maven" )
1076 ContentItem item = repoContent.getItem( selector );
1077 assertNotNull( item );
1078 assertTrue( item instanceof Namespace );
1082 public void testGetItemWithProjectSelector() {
1083 ItemSelector selector = ArchivaItemSelector.builder( )
1084 .withNamespace( "org.apache.maven" )
1085 .withProjectId( "shared" )
1087 ContentItem item = repoContent.getItem( selector );
1088 assertNotNull( item );
1089 assertTrue( item instanceof Project );
1093 public void testGetItemWithVersionSelector() {
1094 ItemSelector selector = ArchivaItemSelector.builder( )
1095 .withNamespace( "org.apache.maven" )
1096 .withProjectId( "samplejar" )
1099 ContentItem item = repoContent.getItem( selector );
1100 assertNotNull( item );
1101 assertTrue( item instanceof Version );
1105 public void testGetItemWithArtifactSelector() {
1106 ItemSelector selector = ArchivaItemSelector.builder( )
1107 .withNamespace( "org.apache.maven" )
1108 .withProjectId( "samplejar" )
1110 .withArtifactId( "samplejar" )
1112 ContentItem item = repoContent.getItem( selector );
1113 assertNotNull( item );
1114 assertTrue( item instanceof Artifact );
1118 public void testGetNamespaceFromPath() throws LayoutException
1120 StorageAsset path = repoContent.getRepository( ).getAsset( "/org/apache/axis2" );
1121 Namespace ns = repoContent.getNamespaceFromPath( path );
1122 assertNotNull( ns );
1123 assertEquals( "org.apache.axis2", ns.getId( ) );
1128 public void testArtifactListWithArtifactSelectorAndRelated() {
1129 ItemSelector selector = ArchivaItemSelector.builder( )
1130 .withNamespace( "org.apache.maven" )
1131 .withProjectId( "samplejar" )
1132 .withVersion( "1.0" )
1133 .withArtifactVersion( "1.0" )
1134 .withArtifactId( "samplejar" )
1135 .withExtension( "jar" )
1136 .includeRelatedArtifacts()
1139 List<? extends Artifact> results = repoContent.getArtifacts( selector );
1141 assertNotNull( results );
1142 assertEquals( 3, results.size( ) );
1144 Artifact artifact = results.stream( ).filter( a -> a.getFileName( ).equalsIgnoreCase( "samplejar-1.0.jar" ) )
1146 assertNotNull( artifact );
1147 assertEquals( BaseArtifactTypes.MAIN, artifact.getDataType( ) );
1149 artifact = results.stream( ).filter( a -> a.getFileName( ).equalsIgnoreCase( "samplejar-1.0.jar.md5" ) )
1151 assertNotNull( artifact );
1152 assertEquals( BaseArtifactTypes.RELATED, artifact.getDataType( ) );
1153 assertEquals( "md5", artifact.getExtension( ) );
1155 artifact = results.stream( ).filter( a -> a.getFileName( ).equalsIgnoreCase( "samplejar-1.0.jar.sha1" ) )
1157 assertNotNull( artifact );
1158 assertEquals( BaseArtifactTypes.RELATED, artifact.getDataType( ) );
1159 assertEquals( "sha1", artifact.getExtension( ) );
1163 private Path copyRepository(String repoName) throws IOException, URISyntaxException
1165 Path tempDir = Files.createTempDirectory( "archiva-repocontent" );
1166 Path repoSource = Paths.get( Thread.currentThread( ).getContextClassLoader( ).getResource( "repositories/" + repoName ).toURI( ) );
1167 assertTrue( Files.exists( repoSource ) );
1168 FileUtils.copyDirectory( repoSource.toFile( ), tempDir.toFile() );
1172 private ManagedRepository createManagedRepoWithContent(String sourceRepoName) throws IOException, URISyntaxException
1174 Path repoDir = copyRepository( sourceRepoName );
1175 MavenManagedRepository repo = createRepository( sourceRepoName, sourceRepoName, repoDir );
1176 ManagedDefaultRepositoryContent deleteRepoContent = new ManagedDefaultRepositoryContent( repo, artifactMappingProviders, fileTypes, fileLockManager );
1177 deleteRepoContent.setMavenContentHelper( contentHelper );
1182 public void deleteNamespaceItem() throws IOException, URISyntaxException, ItemNotFoundException
1184 ManagedRepository repo = createManagedRepoWithContent( "delete-repository" );
1185 ManagedRepositoryContent myRepoContent = repo.getContent( );
1186 Path repoRoot = repo.getRoot().getFilePath( );
1187 assertTrue( Files.exists(repoRoot.resolve( "org/apache/maven" )) );
1188 ArchivaItemSelector selector = ArchivaItemSelector.builder( )
1189 .withNamespace( "org.apache.maven" ).build();
1190 ContentItem item = myRepoContent.getItem( selector );
1191 assertTrue( item instanceof Namespace );
1192 myRepoContent.deleteItem( item );
1193 assertFalse( Files.exists(repoRoot.resolve( "org/apache/maven" )) );
1194 assertTrue( Files.exists(repoRoot.resolve( "org/apache" )) );
1196 // Sub namespaces are deleted too
1197 assertTrue( Files.exists(repoRoot.resolve( "org/apache/test/samplejar" )) );
1198 assertTrue( Files.exists(repoRoot.resolve( "org/apache/test/sub/samplejar" )) );
1199 selector = ArchivaItemSelector.builder( )
1200 .withNamespace( "org.apache.test" ).build();
1201 item = myRepoContent.getItem( selector );
1202 assertTrue( item instanceof Namespace );
1203 myRepoContent.deleteItem( item );
1204 assertFalse( Files.exists(repoRoot.resolve( "org/apache/test/samplejar" )) );
1205 assertFalse( Files.exists(repoRoot.resolve( "org/apache/test/sub/samplejar" )) );
1209 public void deleteProjectItem() throws IOException, URISyntaxException, ItemNotFoundException
1211 ManagedRepository repo = createManagedRepoWithContent( "delete-repository" );
1212 ManagedRepositoryContent myRepoContent = repo.getContent( );
1213 Path repoRoot = repo.getRoot().getFilePath( );
1214 assertTrue( Files.exists(repoRoot.resolve( "org/apache/maven/A" )) );
1215 ArchivaItemSelector selector = ArchivaItemSelector.builder( )
1216 .withNamespace( "org.apache.maven" )
1217 .withProjectId( "A" ).build();
1218 ContentItem item = myRepoContent.getItem( selector );
1219 assertTrue( item instanceof Project );
1220 myRepoContent.deleteItem( item );
1221 assertTrue( Files.exists(repoRoot.resolve( "org/apache/maven" )) );
1222 assertTrue( Files.exists( repoRoot.resolve( "org/apache/maven/samplejar/1.0" ) ) );
1223 assertTrue( Files.exists( repoRoot.resolve( "org/apache/maven/samplejar/2.0" ) ) );
1224 assertFalse( Files.exists(repoRoot.resolve( "org/apache/maven/A" )) );
1226 assertTrue( Files.exists(repoRoot.resolve( "org/apache/test/samplejar" )) );
1227 assertTrue( Files.exists(repoRoot.resolve( "org/apache/test/sub/samplejar" )) );
1228 selector = ArchivaItemSelector.builder( )
1229 .withNamespace( "org.apache.test" )
1230 .withProjectId( "samplejar" ).build();
1231 item = myRepoContent.getItem( selector );
1232 assertTrue( item instanceof Project );
1233 myRepoContent.deleteItem( item );
1234 assertFalse( Files.exists(repoRoot.resolve( "org/apache/test/samplejar" )) );
1235 assertTrue( Files.exists(repoRoot.resolve( "org/apache/test/sub/samplejar" )) );
1239 public void deleteVersionItem() throws IOException, URISyntaxException, ItemNotFoundException
1241 ManagedRepository repo = createManagedRepoWithContent( "delete-repository" );
1242 ManagedRepositoryContent myRepoContent = repo.getContent( );
1243 Path repoRoot = repo.getRoot().getFilePath( );
1244 assertTrue( Files.exists(repoRoot.resolve( "org/apache/maven/A/1.0" )) );
1245 ArchivaItemSelector selector = ArchivaItemSelector.builder( )
1246 .withNamespace( "org.apache.maven" )
1247 .withProjectId( "A" )
1248 .withVersion( "1.0" ).build();
1249 ContentItem item = myRepoContent.getItem( selector );
1250 assertTrue( item instanceof Version );
1251 myRepoContent.deleteItem( item );
1252 assertTrue( Files.exists(repoRoot.resolve( "org/apache/maven/A" )) );
1253 assertTrue( Files.exists( repoRoot.resolve( "org/apache/maven/samplejar/1.0" ) ) );
1254 assertTrue( Files.exists( repoRoot.resolve( "org/apache/maven/samplejar/2.0" ) ) );
1255 assertFalse( Files.exists(repoRoot.resolve( "org/apache/maven/A/1.0" )) );
1257 assertTrue( Files.exists(repoRoot.resolve( "org/apache/test/samplejar" )) );
1258 assertTrue( Files.exists(repoRoot.resolve( "org/apache/test/sub/samplejar" )) );
1259 selector = ArchivaItemSelector.builder( )
1260 .withNamespace( "org.apache.test" )
1261 .withProjectId( "samplejar" )
1262 .withVersion( "2.0" ).build();
1263 item = myRepoContent.getItem( selector );
1264 assertTrue( item instanceof Version );
1265 myRepoContent.deleteItem( item );
1266 assertFalse( Files.exists(repoRoot.resolve( "org/apache/test/samplejar/2.0" )) );
1267 assertTrue( Files.exists(repoRoot.resolve( "org/apache/test/samplejar/1.0" )) );
1268 assertTrue( Files.exists(repoRoot.resolve( "org/apache/test/sub/samplejar/1.0" )) );
1269 assertTrue( Files.exists(repoRoot.resolve( "org/apache/test/sub/samplejar/2.0" )) );
1273 public void deleteArtifactItem() throws IOException, URISyntaxException, ItemNotFoundException
1275 ManagedRepository repo = createManagedRepoWithContent( "delete-repository" );
1276 ManagedRepositoryContent myRepoContent = repo.getContent( );
1277 Path repoRoot = repo.getRoot().getFilePath( );
1278 assertTrue( Files.exists(repoRoot.resolve( "org/apache/maven/A/1.0/A-1.0.pom" )) );
1279 assertTrue( Files.exists(repoRoot.resolve( "org/apache/maven/A/1.0/A-1.0.war" )) );
1280 ArchivaItemSelector selector = ArchivaItemSelector.builder( )
1281 .withNamespace( "org.apache.maven" )
1282 .withProjectId( "A" )
1283 .withVersion( "1.0" )
1284 .withArtifactId( "A" )
1285 .withArtifactVersion( "1.0" )
1286 .withExtension( "pom" )
1288 ContentItem item = myRepoContent.getItem( selector );
1289 assertTrue( item instanceof Artifact );
1290 myRepoContent.deleteItem( item );
1291 assertTrue( Files.exists( repoRoot.resolve( "org/apache/maven/samplejar/1.0" ) ) );
1292 assertTrue( Files.exists( repoRoot.resolve( "org/apache/maven/samplejar/2.0" ) ) );
1293 assertFalse( Files.exists(repoRoot.resolve( "org/apache/maven/A/1.0/A-1.0.pom" )) );
1294 assertTrue( Files.exists(repoRoot.resolve( "org/apache/maven/A/1.0/A-1.0.war" )) );
1297 assertTrue( Files.exists(repoRoot.resolve( "org/apache/test/samplejar/1.0/samplejar-1.0.jar" )) );
1298 assertTrue( Files.exists(repoRoot.resolve( "org/apache/test/samplejar/1.0/samplejar-1.0.jar.md5" )) );
1299 assertTrue( Files.exists(repoRoot.resolve( "org/apache/test/samplejar/1.0/samplejar-1.0.jar.sha1" )) );
1300 assertTrue( Files.exists(repoRoot.resolve( "org/apache/test/samplejar/1.0/samplejar-1.0.pom" )) );
1301 assertTrue( Files.exists(repoRoot.resolve( "org/apache/test/samplejar/1.0/samplejar-1.0-source.jar" )) );
1302 selector = ArchivaItemSelector.builder( )
1303 .withNamespace( "org.apache.test" )
1304 .withProjectId( "samplejar" )
1305 .withVersion( "1.0" )
1306 .withArtifactId( "samplejar" )
1307 .withArtifactVersion( "1.0" )
1308 .withExtension( "jar" )
1310 item = myRepoContent.getItem( selector );
1311 assertTrue( item instanceof Artifact );
1312 myRepoContent.deleteItem( item );
1313 assertFalse( Files.exists(repoRoot.resolve( "org/apache/test/samplejar/1.0/samplejar-1.0.jar" )) );
1314 assertTrue( Files.exists(repoRoot.resolve( "org/apache/test/samplejar/1.0/samplejar-1.0.jar.md5" )) );
1315 assertTrue( Files.exists(repoRoot.resolve( "org/apache/test/samplejar/1.0/samplejar-1.0.jar.sha1" )) );
1316 assertTrue( Files.exists(repoRoot.resolve( "org/apache/test/samplejar/1.0/samplejar-1.0.pom" )) );
1317 assertTrue( Files.exists(repoRoot.resolve( "org/apache/test/samplejar/1.0/samplejar-1.0-source.jar" )) );
1318 assertTrue( Files.exists(repoRoot.resolve( "org/apache/test/samplejar/2.0" )) );
1319 assertTrue( Files.exists(repoRoot.resolve( "org/apache/test/sub/samplejar/1.0" )) );
1320 assertTrue( Files.exists(repoRoot.resolve( "org/apache/test/sub/samplejar/2.0" )) );
1322 selector = ArchivaItemSelector.builder( )
1323 .withNamespace( "org.apache.test" )
1324 .withProjectId( "samplejar" )
1325 .withVersion( "1.0" )
1326 .withArtifactId( "samplejar" )
1327 .withArtifactVersion( "1.0" )
1328 .withClassifier( "source" )
1329 .withExtension( "jar" )
1331 item = myRepoContent.getItem( selector );
1332 assertTrue( item instanceof Artifact );
1333 myRepoContent.deleteItem( item );
1334 assertFalse( Files.exists(repoRoot.resolve( "org/apache/test/samplejar/1.0/samplejar-1.0.jar" )) );
1335 assertTrue( Files.exists(repoRoot.resolve( "org/apache/test/samplejar/1.0/samplejar-1.0.jar.md5" )) );
1336 assertTrue( Files.exists(repoRoot.resolve( "org/apache/test/samplejar/1.0/samplejar-1.0.jar.sha1" )) );
1337 assertTrue( Files.exists(repoRoot.resolve( "org/apache/test/samplejar/1.0/samplejar-1.0.pom" )) );
1338 assertFalse( Files.exists(repoRoot.resolve( "org/apache/test/samplejar/1.0/samplejar-1.0-source.jar" )) );
1339 assertTrue( Files.exists(repoRoot.resolve( "org/apache/test/samplejar/1.0/samplejar-1.0-source.jar.sha1" )) );
1340 assertTrue( Files.exists(repoRoot.resolve( "org/apache/test/samplejar/2.0" )) );
1341 assertTrue( Files.exists(repoRoot.resolve( "org/apache/test/sub/samplejar/1.0" )) );
1342 assertTrue( Files.exists(repoRoot.resolve( "org/apache/test/sub/samplejar/2.0" )) );
1344 selector = ArchivaItemSelector.builder( )
1345 .withNamespace( "org.apache.test" )
1346 .withProjectId( "samplejar" )
1347 .withVersion( "1.0" )
1348 .withArtifactId( "samplejar" )
1349 .withArtifactVersion( "1.0" )
1350 .withExtension( "jar.md5" )
1352 item = myRepoContent.getItem( selector );
1353 assertTrue( item instanceof Artifact );
1354 myRepoContent.deleteItem( item );
1355 assertFalse( Files.exists(repoRoot.resolve( "org/apache/test/samplejar/1.0/samplejar-1.0.jar" )) );
1356 assertFalse( Files.exists(repoRoot.resolve( "org/apache/test/samplejar/1.0/samplejar-1.0.jar.md5" )) );
1357 assertTrue( Files.exists(repoRoot.resolve( "org/apache/test/samplejar/1.0/samplejar-1.0.jar.sha1" )) );
1358 assertTrue( Files.exists(repoRoot.resolve( "org/apache/test/samplejar/1.0/samplejar-1.0.pom" )) );
1359 assertFalse( Files.exists(repoRoot.resolve( "org/apache/test/samplejar/1.0/samplejar-1.0-source.jar" )) );
1360 assertTrue( Files.exists(repoRoot.resolve( "org/apache/test/samplejar/1.0/samplejar-1.0-source.jar.sha1" )) );
1361 assertTrue( Files.exists(repoRoot.resolve( "org/apache/test/samplejar/2.0" )) );
1362 assertTrue( Files.exists(repoRoot.resolve( "org/apache/test/sub/samplejar/1.0" )) );
1363 assertTrue( Files.exists(repoRoot.resolve( "org/apache/test/sub/samplejar/2.0" )) );
1369 public void deleteItemNotFound() throws IOException, URISyntaxException, ItemNotFoundException
1371 ManagedRepository repo = createManagedRepoWithContent( "delete-repository" );
1372 ManagedRepositoryContent myRepoContent = repo.getContent( );
1373 Path repoRoot = repo.getRoot().getFilePath( );
1375 ArchivaItemSelector selector = ArchivaItemSelector.builder( )
1376 .withNamespace( "org.apache.test2" )
1379 ContentItem item = myRepoContent.getItem( selector );
1380 assertTrue( item instanceof Namespace );
1383 myRepoContent.deleteItem( item );
1384 assertTrue( "ItemNotFoundException expected for non existing namespace", false );
1385 } catch ( ItemNotFoundException e) {
1388 selector = ArchivaItemSelector.builder( )
1389 .withNamespace( "org.apache.test" )
1390 .withProjectId( "samplejar2" )
1392 item = myRepoContent.getItem( selector );
1393 assertTrue( item instanceof Project );
1396 myRepoContent.deleteItem( item );
1397 assertTrue( "ItemNotFoundException expected for non existing project", false );
1398 } catch ( ItemNotFoundException e) {
1401 selector = ArchivaItemSelector.builder( )
1402 .withNamespace( "org.apache.test" )
1403 .withProjectId( "samplejar" )
1406 item = myRepoContent.getItem( selector );
1407 assertTrue( item instanceof Version );
1410 myRepoContent.deleteItem( item );
1411 assertTrue( "ItemNotFoundException expected for non existing version", false );
1412 } catch ( ItemNotFoundException e) {
1415 selector = ArchivaItemSelector.builder( )
1416 .withNamespace( "org.apache.test" )
1417 .withProjectId( "samplejar" )
1419 .withArtifactId( "samplejar" )
1420 .withArtifactVersion( "1.0" )
1421 .withExtension( "jax" )
1423 item = myRepoContent.getItem( selector );
1424 assertTrue( item instanceof Artifact );
1427 myRepoContent.deleteItem( item );
1428 assertTrue( "ItemNotFoundException expected for non existing artifact", false );
1429 } catch ( ItemNotFoundException e) {
1436 public void testAddArtifact() throws IOException, URISyntaxException, LayoutException
1438 ManagedRepository repo = createManagedRepoWithContent( "delete-repository" );
1439 ManagedRepositoryContent myRepoContent = repo.getContent( );
1440 BaseRepositoryContentLayout layout = myRepoContent.getLayout( BaseRepositoryContentLayout.class );
1441 Path repoRoot = repo.getRoot().getFilePath( );
1443 Path tmpFile = Files.createTempFile( "archiva-mvn-repotest", "jar" );
1444 try( OutputStream outputStream = Files.newOutputStream( tmpFile ))
1446 for ( int i = 0; i < 255; i++ )
1448 outputStream.write( "test.test.test\n".getBytes( Charset.forName( "UTF-8" ) ) );
1452 Path file = repoRoot.resolve( "org/apache/maven/samplejar/2.0/samplejar-2.0.jar" );
1453 FileTime lmt = Files.getLastModifiedTime( file );
1454 ArchivaItemSelector selector = ArchivaItemSelector.builder( )
1455 .withNamespace( "org.apache.maven" )
1456 .withProjectId( "samplejar" )
1457 .withVersion( "2.0" )
1458 .withArtifactId( "samplejar" )
1459 .withArtifactVersion( "2.0" )
1460 .withExtension( "jar" )
1462 Artifact artifact = layout.getArtifact( selector );
1463 layout.addArtifact( tmpFile, artifact );
1464 FileTime lmtAfter = Files.getLastModifiedTime( file );
1465 assertNotEquals( lmtAfter, lmt );
1466 Reader ln = Files.newBufferedReader( file, Charset.forName( "UTF-8" ) );
1467 char[] content = new char[50];
1469 assertTrue( new String( content ).startsWith( "test.test.test" ) );
1471 tmpFile = Files.createTempFile( "archiva-mvn-repotest", "jar" );
1472 try( OutputStream outputStream = Files.newOutputStream( tmpFile ))
1474 for ( int i = 0; i < 255; i++ )
1476 outputStream.write( "test.test.test\n".getBytes( Charset.forName( "UTF-8" ) ) );
1479 file = repoRoot.resolve( "org/apache/maven/samplejar/2.0/samplejar-2.0.test" );
1480 assertFalse( Files.exists( file ) );
1481 assertTrue( Files.exists( tmpFile ) );
1482 selector = ArchivaItemSelector.builder( )
1483 .withNamespace( "org.apache.maven" )
1484 .withProjectId( "samplejar" )
1485 .withVersion( "2.0" )
1486 .withArtifactId( "samplejar" )
1487 .withArtifactVersion( "2.0" )
1488 .withExtension( "test" )
1490 artifact = layout.getArtifact( selector );
1491 layout.addArtifact( tmpFile, artifact );
1492 ln = Files.newBufferedReader( file, Charset.forName( "UTF-8" ) );
1494 assertTrue( new String( content ).startsWith( "test.test.test" ) );
1498 public void getExistingMetadataItem() {
1499 // org/apache/maven/some-ejb/1.0
1500 ArchivaItemSelector versionSelector = ArchivaItemSelector.builder( )
1501 .withNamespace( "org.apache.maven" )
1502 .withProjectId( "some-ejb" )
1503 .withVersion( "1.0" ).build( );
1504 Version version = repoContent.getVersion( versionSelector );
1505 DataItem metaData = repoContent.getMetadataItem( version );
1506 assertTrue( metaData.exists( ) );
1507 assertEquals( "/org/apache/maven/some-ejb/1.0/maven-metadata.xml", metaData.getAsset( ).getPath( ) );
1511 public void getNonExistingMetadataItem() {
1512 // org/apache/maven/some-ejb/1.0
1513 ArchivaItemSelector versionSelector = ArchivaItemSelector.builder( )
1514 .withNamespace( "javax.sql" )
1515 .withProjectId( "jdbc" )
1516 .withVersion( "2.0" ).build( );
1517 Version version = repoContent.getVersion( versionSelector );
1518 DataItem metaData = repoContent.getMetadataItem( version );
1519 assertFalse( metaData.exists( ) );
1520 assertEquals( "/javax/sql/jdbc/2.0/maven-metadata.xml", metaData.getAsset( ).getPath( ) );