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 public void testExcludeMetadataFile()
183 assertVersions( "include_xml", "1.0", new String[]{ "1.0" } );
187 private void assertVersions( String artifactId, String version, String[] expectedVersions )
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() );
195 // Request the versions.
197 // Sort the list (for asserts later)
198 final VersionComparator comparator = new VersionComparator( );
200 ItemSelector selector = ArchivaItemSelector.builder( )
201 .withNamespace( "org.apache.archiva.metadata.tests" )
202 .withProjectId( artifactId )
203 .withVersion( version )
205 List<String> versions = repoContent.getVersions( selector ).stream()
206 .map(v -> v.getVersion()).sorted( comparator ).collect( Collectors.toList());
207 assertArrayEquals( expectedVersions, versions.toArray( ) );
212 private void assertArtifactVersions( String artifactId, String version, String[] expectedVersions )
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() );
220 // Request the versions.
222 // Sort the list (for asserts later)
223 final VersionComparator comparator = new VersionComparator( );
225 ItemSelector selector = ArchivaItemSelector.builder( )
226 .withNamespace( "org.apache.archiva.metadata.tests" )
227 .withProjectId( artifactId )
228 .withVersion( version )
230 List<String> versions = repoContent.getArtifactVersions( selector ).stream()
231 .sorted( comparator ).collect( Collectors.toList());
232 assertArrayEquals( expectedVersions, versions.toArray( ) );
238 public void getTestGetProjectWithIllegalArgs() {
239 ItemSelector selector = ArchivaItemSelector.builder( )
240 .withNamespace( "org.apache" )
241 .withVersion( "1.0" )
245 repoContent.getProject( selector );
246 assertFalse( "Should throw IllegalArgumentException if no project id is given", true );
247 } catch (IllegalArgumentException e) {
249 assertTrue( e.getMessage( ).contains( "Project id must be set" ) );
254 public void getTestGetVersionWithIllegalArgs() {
255 ItemSelector selector = ArchivaItemSelector.builder( )
256 .withNamespace( "org.apache.maven" )
257 .withVersion( "1.0" )
261 repoContent.getVersion( selector );
262 assertFalse( "Should throw IllegalArgumentException if no project id is given", true );
263 } catch (IllegalArgumentException e) {
265 assertTrue( e.getMessage( ).contains( "Project id must be set" ) );
269 selector = ArchivaItemSelector.builder( )
270 .withNamespace( "org.apache.maven" )
271 .withProjectId( "shared" )
275 repoContent.getVersion( selector );
276 assertFalse( "Should throw IllegalArgumentException if no version is given", true );
277 } catch (IllegalArgumentException e) {
279 assertTrue( e.getMessage( ).contains( "Version must be set" ) );
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")
293 repoContent.getArtifact( selector );
294 assertFalse( "Should throw IllegalArgumentException if no project id is given", true );
295 } catch (IllegalArgumentException e) {
297 assertTrue( e.getMessage( ).contains( "Project id must be set" ) );
301 selector = ArchivaItemSelector.builder( )
302 .withNamespace( "org.apache.maven" )
303 .withProjectId( "shared" )
304 .withArtifactId( "shared" )
305 .withArtifactVersion("1.0")
309 repoContent.getArtifact( selector );
310 assertFalse( "Should throw IllegalArgumentException if no version is given", true );
311 } catch (IllegalArgumentException e) {
313 assertTrue( e.getMessage( ).contains( "Version must be set" ) );
316 selector = ArchivaItemSelector.builder( )
317 .withNamespace( "org.apache.maven" )
318 .withProjectId( "shared" )
320 .withArtifactVersion("1.0")
324 repoContent.getArtifact( selector );
325 assertFalse( "Should throw IllegalArgumentException if no artifact id is given", true );
326 } catch (IllegalArgumentException e) {
328 assertTrue( e.getMessage( ).contains( "Artifact id must be set" ) );
335 public void testGetProjects() {
336 ItemSelector selector = ArchivaItemSelector.builder( )
337 .withNamespace( "org.apache.maven" ).build();
338 Namespace ns = repoContent.getNamespace( selector );
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",
346 Object[] actual = projects.stream( ).map( p -> p.getId( ) ).sorted( ).toArray( );
347 assertArrayEquals( expected, actual);
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",
360 Object[] actual = projects.stream( ).map( p -> p.getId( ) ).sorted( ).toArray( );
361 assertArrayEquals( expected, actual);
365 public void testGetVersionsWithIllegalSelector() {
366 ItemSelector selector = ArchivaItemSelector.builder( )
367 .withNamespace( "org.apache.maven" ).build();
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( ) );
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( ) );
389 protected ArtifactReference toArtifactReference( String path )
390 throws LayoutException
392 return repoContent.toArtifactReference( path );
396 protected ItemSelector toItemSelector( String path ) throws LayoutException
398 return repoContent.toItemSelector( path );
402 protected String toPath( ArtifactReference 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( ).getVersion( ) );
516 assertEquals( "1.3-20070725.210059-1", artifact.getArtifactVersion( ) );
517 assertEquals( ".pom", artifact.getRemainder( ) );
518 assertEquals( "axis2", artifact.getId( ) );
519 assertEquals( "axis2", artifact.getVersion( ).getProject( ).getId( ) );
520 assertEquals( "org.apache.axis2", artifact.getVersion( ).getProject( ).getNamespace( ).getNamespace( ) );
521 assertEquals( "", artifact.getClassifier( ) );
522 assertEquals( "pom", artifact.getType( ) );
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( ).getVersion( ) );
532 assertEquals( "1.3-20070725.210059-1", artifact.getArtifactVersion( ) );
533 assertEquals( ".pom.md5", artifact.getRemainder( ) );
534 assertEquals( "axis2", artifact.getId( ) );
535 assertEquals( "axis2", artifact.getVersion( ).getProject( ).getId( ) );
536 assertEquals( "org.apache.axis2", artifact.getVersion( ).getProject( ).getNamespace( ).getNamespace( ) );
537 assertEquals( "", artifact.getClassifier( ) );
538 assertEquals( "md5", artifact.getType( ) );
542 artifact = results.stream( ).filter( a -> a.getFileName( ).equals( "maven-metadata.xml" ) )
543 .findFirst( ).get( );
544 assertNotNull( artifact );
545 assertEquals( BaseArtifactTypes.METADATA, artifact.getDataType( ) );
546 assertEquals( "1.3-SNAPSHOT", artifact.getVersion( ).getVersion( ) );
547 assertEquals( "xml", artifact.getExtension( ) );
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.getVersion( ) );
924 Project project = result.get( 40 ).adapt( Project.class );
925 assertNotNull( project );
926 assertEquals( "axis2", project.getId( ) );
927 assertTrue( result.stream( ).anyMatch( a -> "axis2-1.3-20070725.210059-1.pom".equals( a.getAsset( ).getName( ) ) ) );
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 Artifact );
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() {
1119 StorageAsset path = repoContent.getRepository( ).getAsset( "/org/apache/axis2" );
1120 Namespace ns = repoContent.getNamespaceFromPath( path );
1121 assertNotNull( ns );
1122 assertEquals( "org.apache.axis2", ns.getNamespace( ) );
1127 public void testArtifactListWithArtifactSelectorAndRelated() {
1128 ItemSelector selector = ArchivaItemSelector.builder( )
1129 .withNamespace( "org.apache.maven" )
1130 .withProjectId( "samplejar" )
1131 .withVersion( "1.0" )
1132 .withArtifactVersion( "1.0" )
1133 .withArtifactId( "samplejar" )
1134 .withExtension( "jar" )
1135 .includeRelatedArtifacts()
1138 List<? extends Artifact> results = repoContent.getArtifacts( selector );
1140 assertNotNull( results );
1141 assertEquals( 3, results.size( ) );
1143 Artifact artifact = results.stream( ).filter( a -> a.getFileName( ).equalsIgnoreCase( "samplejar-1.0.jar" ) )
1145 assertNotNull( artifact );
1146 assertEquals( BaseArtifactTypes.MAIN, artifact.getDataType( ) );
1148 artifact = results.stream( ).filter( a -> a.getFileName( ).equalsIgnoreCase( "samplejar-1.0.jar.md5" ) )
1150 assertNotNull( artifact );
1151 assertEquals( BaseArtifactTypes.RELATED, artifact.getDataType( ) );
1152 assertEquals( "md5", artifact.getExtension( ) );
1154 artifact = results.stream( ).filter( a -> a.getFileName( ).equalsIgnoreCase( "samplejar-1.0.jar.sha1" ) )
1156 assertNotNull( artifact );
1157 assertEquals( BaseArtifactTypes.RELATED, artifact.getDataType( ) );
1158 assertEquals( "sha1", artifact.getExtension( ) );
1162 private Path copyRepository(String repoName) throws IOException, URISyntaxException
1164 Path tempDir = Files.createTempDirectory( "archiva-repocontent" );
1165 Path repoSource = Paths.get( Thread.currentThread( ).getContextClassLoader( ).getResource( "repositories/" + repoName ).toURI( ) );
1166 assertTrue( Files.exists( repoSource ) );
1167 FileUtils.copyDirectory( repoSource.toFile( ), tempDir.toFile() );
1171 private ManagedRepository createManagedRepoWithContent(String sourceRepoName) throws IOException, URISyntaxException
1173 Path repoDir = copyRepository( sourceRepoName );
1174 MavenManagedRepository repo = createRepository( sourceRepoName, sourceRepoName, repoDir );
1175 ManagedDefaultRepositoryContent deleteRepoContent = new ManagedDefaultRepositoryContent( repo, artifactMappingProviders, fileTypes, fileLockManager );
1176 deleteRepoContent.setMavenContentHelper( contentHelper );
1181 public void deleteNamespaceItem() throws IOException, URISyntaxException, ItemNotFoundException
1183 ManagedRepository repo = createManagedRepoWithContent( "delete-repository" );
1184 ManagedRepositoryContent myRepoContent = repo.getContent( );
1185 Path repoRoot = repo.getAsset( "" ).getFilePath( );
1186 assertTrue( Files.exists(repoRoot.resolve( "org/apache/maven" )) );
1187 ArchivaItemSelector selector = ArchivaItemSelector.builder( )
1188 .withNamespace( "org.apache.maven" ).build();
1189 ContentItem item = myRepoContent.getItem( selector );
1190 assertTrue( item instanceof Namespace );
1191 myRepoContent.deleteItem( item );
1192 assertFalse( Files.exists(repoRoot.resolve( "org/apache/maven" )) );
1193 assertTrue( Files.exists(repoRoot.resolve( "org/apache" )) );
1195 // Sub namespaces are deleted too
1196 assertTrue( Files.exists(repoRoot.resolve( "org/apache/test/samplejar" )) );
1197 assertTrue( Files.exists(repoRoot.resolve( "org/apache/test/sub/samplejar" )) );
1198 selector = ArchivaItemSelector.builder( )
1199 .withNamespace( "org.apache.test" ).build();
1200 item = myRepoContent.getItem( selector );
1201 assertTrue( item instanceof Namespace );
1202 myRepoContent.deleteItem( item );
1203 assertFalse( Files.exists(repoRoot.resolve( "org/apache/test/samplejar" )) );
1204 assertFalse( Files.exists(repoRoot.resolve( "org/apache/test/sub/samplejar" )) );
1208 public void deleteProjectItem() throws IOException, URISyntaxException, ItemNotFoundException
1210 ManagedRepository repo = createManagedRepoWithContent( "delete-repository" );
1211 ManagedRepositoryContent myRepoContent = repo.getContent( );
1212 Path repoRoot = repo.getAsset( "" ).getFilePath( );
1213 assertTrue( Files.exists(repoRoot.resolve( "org/apache/maven/A" )) );
1214 ArchivaItemSelector selector = ArchivaItemSelector.builder( )
1215 .withNamespace( "org.apache.maven" )
1216 .withProjectId( "A" ).build();
1217 ContentItem item = myRepoContent.getItem( selector );
1218 assertTrue( item instanceof Project );
1219 myRepoContent.deleteItem( item );
1220 assertTrue( Files.exists(repoRoot.resolve( "org/apache/maven" )) );
1221 assertTrue( Files.exists( repoRoot.resolve( "org/apache/maven/samplejar/1.0" ) ) );
1222 assertTrue( Files.exists( repoRoot.resolve( "org/apache/maven/samplejar/2.0" ) ) );
1223 assertFalse( Files.exists(repoRoot.resolve( "org/apache/maven/A" )) );
1225 assertTrue( Files.exists(repoRoot.resolve( "org/apache/test/samplejar" )) );
1226 assertTrue( Files.exists(repoRoot.resolve( "org/apache/test/sub/samplejar" )) );
1227 selector = ArchivaItemSelector.builder( )
1228 .withNamespace( "org.apache.test" )
1229 .withProjectId( "samplejar" ).build();
1230 item = myRepoContent.getItem( selector );
1231 assertTrue( item instanceof Project );
1232 myRepoContent.deleteItem( item );
1233 assertFalse( Files.exists(repoRoot.resolve( "org/apache/test/samplejar" )) );
1234 assertTrue( Files.exists(repoRoot.resolve( "org/apache/test/sub/samplejar" )) );
1238 public void deleteVersionItem() throws IOException, URISyntaxException, ItemNotFoundException
1240 ManagedRepository repo = createManagedRepoWithContent( "delete-repository" );
1241 ManagedRepositoryContent myRepoContent = repo.getContent( );
1242 Path repoRoot = repo.getAsset( "" ).getFilePath( );
1243 assertTrue( Files.exists(repoRoot.resolve( "org/apache/maven/A/1.0" )) );
1244 ArchivaItemSelector selector = ArchivaItemSelector.builder( )
1245 .withNamespace( "org.apache.maven" )
1246 .withProjectId( "A" )
1247 .withVersion( "1.0" ).build();
1248 ContentItem item = myRepoContent.getItem( selector );
1249 assertTrue( item instanceof Version );
1250 myRepoContent.deleteItem( item );
1251 assertTrue( Files.exists(repoRoot.resolve( "org/apache/maven/A" )) );
1252 assertTrue( Files.exists( repoRoot.resolve( "org/apache/maven/samplejar/1.0" ) ) );
1253 assertTrue( Files.exists( repoRoot.resolve( "org/apache/maven/samplejar/2.0" ) ) );
1254 assertFalse( Files.exists(repoRoot.resolve( "org/apache/maven/A/1.0" )) );
1256 assertTrue( Files.exists(repoRoot.resolve( "org/apache/test/samplejar" )) );
1257 assertTrue( Files.exists(repoRoot.resolve( "org/apache/test/sub/samplejar" )) );
1258 selector = ArchivaItemSelector.builder( )
1259 .withNamespace( "org.apache.test" )
1260 .withProjectId( "samplejar" )
1261 .withVersion( "2.0" ).build();
1262 item = myRepoContent.getItem( selector );
1263 assertTrue( item instanceof Version );
1264 myRepoContent.deleteItem( item );
1265 assertFalse( Files.exists(repoRoot.resolve( "org/apache/test/samplejar/2.0" )) );
1266 assertTrue( Files.exists(repoRoot.resolve( "org/apache/test/samplejar/1.0" )) );
1267 assertTrue( Files.exists(repoRoot.resolve( "org/apache/test/sub/samplejar/1.0" )) );
1268 assertTrue( Files.exists(repoRoot.resolve( "org/apache/test/sub/samplejar/2.0" )) );
1272 public void deleteArtifactItem() throws IOException, URISyntaxException, ItemNotFoundException
1274 ManagedRepository repo = createManagedRepoWithContent( "delete-repository" );
1275 ManagedRepositoryContent myRepoContent = repo.getContent( );
1276 Path repoRoot = repo.getAsset( "" ).getFilePath( );
1277 assertTrue( Files.exists(repoRoot.resolve( "org/apache/maven/A/1.0/A-1.0.pom" )) );
1278 assertTrue( Files.exists(repoRoot.resolve( "org/apache/maven/A/1.0/A-1.0.war" )) );
1279 ArchivaItemSelector selector = ArchivaItemSelector.builder( )
1280 .withNamespace( "org.apache.maven" )
1281 .withProjectId( "A" )
1282 .withVersion( "1.0" )
1283 .withArtifactId( "A" )
1284 .withArtifactVersion( "1.0" )
1285 .withExtension( "pom" )
1287 ContentItem item = myRepoContent.getItem( selector );
1288 assertTrue( item instanceof Artifact );
1289 myRepoContent.deleteItem( item );
1290 assertTrue( Files.exists( repoRoot.resolve( "org/apache/maven/samplejar/1.0" ) ) );
1291 assertTrue( Files.exists( repoRoot.resolve( "org/apache/maven/samplejar/2.0" ) ) );
1292 assertFalse( Files.exists(repoRoot.resolve( "org/apache/maven/A/1.0/A-1.0.pom" )) );
1293 assertTrue( Files.exists(repoRoot.resolve( "org/apache/maven/A/1.0/A-1.0.war" )) );
1296 assertTrue( Files.exists(repoRoot.resolve( "org/apache/test/samplejar/1.0/samplejar-1.0.jar" )) );
1297 assertTrue( Files.exists(repoRoot.resolve( "org/apache/test/samplejar/1.0/samplejar-1.0.jar.md5" )) );
1298 assertTrue( Files.exists(repoRoot.resolve( "org/apache/test/samplejar/1.0/samplejar-1.0.jar.sha1" )) );
1299 assertTrue( Files.exists(repoRoot.resolve( "org/apache/test/samplejar/1.0/samplejar-1.0.pom" )) );
1300 assertTrue( Files.exists(repoRoot.resolve( "org/apache/test/samplejar/1.0/samplejar-1.0-source.jar" )) );
1301 selector = ArchivaItemSelector.builder( )
1302 .withNamespace( "org.apache.test" )
1303 .withProjectId( "samplejar" )
1304 .withVersion( "1.0" )
1305 .withArtifactId( "samplejar" )
1306 .withArtifactVersion( "1.0" )
1307 .withExtension( "jar" )
1309 item = myRepoContent.getItem( selector );
1310 assertTrue( item instanceof Artifact );
1311 myRepoContent.deleteItem( item );
1312 assertFalse( Files.exists(repoRoot.resolve( "org/apache/test/samplejar/1.0/samplejar-1.0.jar" )) );
1313 assertTrue( Files.exists(repoRoot.resolve( "org/apache/test/samplejar/1.0/samplejar-1.0.jar.md5" )) );
1314 assertTrue( Files.exists(repoRoot.resolve( "org/apache/test/samplejar/1.0/samplejar-1.0.jar.sha1" )) );
1315 assertTrue( Files.exists(repoRoot.resolve( "org/apache/test/samplejar/1.0/samplejar-1.0.pom" )) );
1316 assertTrue( Files.exists(repoRoot.resolve( "org/apache/test/samplejar/1.0/samplejar-1.0-source.jar" )) );
1317 assertTrue( Files.exists(repoRoot.resolve( "org/apache/test/samplejar/2.0" )) );
1318 assertTrue( Files.exists(repoRoot.resolve( "org/apache/test/sub/samplejar/1.0" )) );
1319 assertTrue( Files.exists(repoRoot.resolve( "org/apache/test/sub/samplejar/2.0" )) );
1321 selector = ArchivaItemSelector.builder( )
1322 .withNamespace( "org.apache.test" )
1323 .withProjectId( "samplejar" )
1324 .withVersion( "1.0" )
1325 .withArtifactId( "samplejar" )
1326 .withArtifactVersion( "1.0" )
1327 .withClassifier( "source" )
1328 .withExtension( "jar" )
1330 item = myRepoContent.getItem( selector );
1331 assertTrue( item instanceof Artifact );
1332 myRepoContent.deleteItem( item );
1333 assertFalse( Files.exists(repoRoot.resolve( "org/apache/test/samplejar/1.0/samplejar-1.0.jar" )) );
1334 assertTrue( Files.exists(repoRoot.resolve( "org/apache/test/samplejar/1.0/samplejar-1.0.jar.md5" )) );
1335 assertTrue( Files.exists(repoRoot.resolve( "org/apache/test/samplejar/1.0/samplejar-1.0.jar.sha1" )) );
1336 assertTrue( Files.exists(repoRoot.resolve( "org/apache/test/samplejar/1.0/samplejar-1.0.pom" )) );
1337 assertFalse( Files.exists(repoRoot.resolve( "org/apache/test/samplejar/1.0/samplejar-1.0-source.jar" )) );
1338 assertTrue( Files.exists(repoRoot.resolve( "org/apache/test/samplejar/1.0/samplejar-1.0-source.jar.sha1" )) );
1339 assertTrue( Files.exists(repoRoot.resolve( "org/apache/test/samplejar/2.0" )) );
1340 assertTrue( Files.exists(repoRoot.resolve( "org/apache/test/sub/samplejar/1.0" )) );
1341 assertTrue( Files.exists(repoRoot.resolve( "org/apache/test/sub/samplejar/2.0" )) );
1343 selector = ArchivaItemSelector.builder( )
1344 .withNamespace( "org.apache.test" )
1345 .withProjectId( "samplejar" )
1346 .withVersion( "1.0" )
1347 .withArtifactId( "samplejar" )
1348 .withArtifactVersion( "1.0" )
1349 .withExtension( "jar.md5" )
1351 item = myRepoContent.getItem( selector );
1352 assertTrue( item instanceof Artifact );
1353 myRepoContent.deleteItem( item );
1354 assertFalse( Files.exists(repoRoot.resolve( "org/apache/test/samplejar/1.0/samplejar-1.0.jar" )) );
1355 assertFalse( Files.exists(repoRoot.resolve( "org/apache/test/samplejar/1.0/samplejar-1.0.jar.md5" )) );
1356 assertTrue( Files.exists(repoRoot.resolve( "org/apache/test/samplejar/1.0/samplejar-1.0.jar.sha1" )) );
1357 assertTrue( Files.exists(repoRoot.resolve( "org/apache/test/samplejar/1.0/samplejar-1.0.pom" )) );
1358 assertFalse( Files.exists(repoRoot.resolve( "org/apache/test/samplejar/1.0/samplejar-1.0-source.jar" )) );
1359 assertTrue( Files.exists(repoRoot.resolve( "org/apache/test/samplejar/1.0/samplejar-1.0-source.jar.sha1" )) );
1360 assertTrue( Files.exists(repoRoot.resolve( "org/apache/test/samplejar/2.0" )) );
1361 assertTrue( Files.exists(repoRoot.resolve( "org/apache/test/sub/samplejar/1.0" )) );
1362 assertTrue( Files.exists(repoRoot.resolve( "org/apache/test/sub/samplejar/2.0" )) );
1368 public void deleteItemNotFound() throws IOException, URISyntaxException, ItemNotFoundException
1370 ManagedRepository repo = createManagedRepoWithContent( "delete-repository" );
1371 ManagedRepositoryContent myRepoContent = repo.getContent( );
1372 Path repoRoot = repo.getAsset( "" ).getFilePath( );
1374 ArchivaItemSelector selector = ArchivaItemSelector.builder( )
1375 .withNamespace( "org.apache.test2" )
1378 ContentItem item = myRepoContent.getItem( selector );
1379 assertTrue( item instanceof Namespace );
1382 myRepoContent.deleteItem( item );
1383 assertTrue( "ItemNotFoundException expected for non existing namespace", false );
1384 } catch ( ItemNotFoundException e) {
1387 selector = ArchivaItemSelector.builder( )
1388 .withNamespace( "org.apache.test" )
1389 .withProjectId( "samplejar2" )
1391 item = myRepoContent.getItem( selector );
1392 assertTrue( item instanceof Project );
1395 myRepoContent.deleteItem( item );
1396 assertTrue( "ItemNotFoundException expected for non existing project", false );
1397 } catch ( ItemNotFoundException e) {
1400 selector = ArchivaItemSelector.builder( )
1401 .withNamespace( "org.apache.test" )
1402 .withProjectId( "samplejar" )
1405 item = myRepoContent.getItem( selector );
1406 assertTrue( item instanceof Version );
1409 myRepoContent.deleteItem( item );
1410 assertTrue( "ItemNotFoundException expected for non existing version", false );
1411 } catch ( ItemNotFoundException e) {
1414 selector = ArchivaItemSelector.builder( )
1415 .withNamespace( "org.apache.test" )
1416 .withProjectId( "samplejar" )
1418 .withArtifactId( "samplejar" )
1419 .withArtifactVersion( "1.0" )
1420 .withExtension( "jax" )
1422 item = myRepoContent.getItem( selector );
1423 assertTrue( item instanceof Artifact );
1426 myRepoContent.deleteItem( item );
1427 assertTrue( "ItemNotFoundException expected for non existing artifact", false );
1428 } catch ( ItemNotFoundException e) {
1435 public void testAddArtifact() throws IOException, URISyntaxException, LayoutException
1437 ManagedRepository repo = createManagedRepoWithContent( "delete-repository" );
1438 ManagedRepositoryContent myRepoContent = repo.getContent( );
1439 BaseRepositoryContentLayout layout = myRepoContent.getLayout( BaseRepositoryContentLayout.class );
1440 Path repoRoot = repo.getAsset( "" ).getFilePath( );
1442 Path tmpFile = Files.createTempFile( "archiva-mvn-repotest", "jar" );
1443 try( OutputStream outputStream = Files.newOutputStream( tmpFile ))
1445 for ( int i = 0; i < 255; i++ )
1447 outputStream.write( "test.test.test\n".getBytes( Charset.forName( "UTF-8" ) ) );
1451 Path file = repoRoot.resolve( "org/apache/maven/samplejar/2.0/samplejar-2.0.jar" );
1452 FileTime lmt = Files.getLastModifiedTime( file );
1453 ArchivaItemSelector selector = ArchivaItemSelector.builder( )
1454 .withNamespace( "org.apache.maven" )
1455 .withProjectId( "samplejar" )
1456 .withVersion( "2.0" )
1457 .withArtifactId( "samplejar" )
1458 .withArtifactVersion( "2.0" )
1459 .withExtension( "jar" )
1461 Artifact artifact = layout.getArtifact( selector );
1462 layout.addArtifact( tmpFile, artifact );
1463 FileTime lmtAfter = Files.getLastModifiedTime( file );
1464 assertNotEquals( lmtAfter, lmt );
1465 Reader ln = Files.newBufferedReader( file, Charset.forName( "UTF-8" ) );
1466 char[] content = new char[50];
1468 assertTrue( new String( content ).startsWith( "test.test.test" ) );
1470 tmpFile = Files.createTempFile( "archiva-mvn-repotest", "jar" );
1471 try( OutputStream outputStream = Files.newOutputStream( tmpFile ))
1473 for ( int i = 0; i < 255; i++ )
1475 outputStream.write( "test.test.test\n".getBytes( Charset.forName( "UTF-8" ) ) );
1478 file = repoRoot.resolve( "org/apache/maven/samplejar/2.0/samplejar-2.0.test" );
1479 assertFalse( Files.exists( file ) );
1480 assertTrue( Files.exists( tmpFile ) );
1481 selector = ArchivaItemSelector.builder( )
1482 .withNamespace( "org.apache.maven" )
1483 .withProjectId( "samplejar" )
1484 .withVersion( "2.0" )
1485 .withArtifactId( "samplejar" )
1486 .withArtifactVersion( "2.0" )
1487 .withExtension( "test" )
1489 artifact = layout.getArtifact( selector );
1490 layout.addArtifact( tmpFile, artifact );
1491 ln = Files.newBufferedReader( file, Charset.forName( "UTF-8" ) );
1493 assertTrue( new String( content ).startsWith( "test.test.test" ) );
1497 public void getExistingMetadataItem() {
1498 // org/apache/maven/some-ejb/1.0
1499 ArchivaItemSelector versionSelector = ArchivaItemSelector.builder( )
1500 .withNamespace( "org.apache.maven" )
1501 .withProjectId( "some-ejb" )
1502 .withVersion( "1.0" ).build( );
1503 Version version = repoContent.getVersion( versionSelector );
1504 DataItem metaData = repoContent.getMetadataItem( version );
1505 assertTrue( metaData.exists( ) );
1506 assertEquals( "/org/apache/maven/some-ejb/1.0/maven-metadata.xml", metaData.getAsset( ).getPath( ) );
1510 public void getNonExistingMetadataItem() {
1511 // org/apache/maven/some-ejb/1.0
1512 ArchivaItemSelector versionSelector = ArchivaItemSelector.builder( )
1513 .withNamespace( "javax.sql" )
1514 .withProjectId( "jdbc" )
1515 .withVersion( "2.0" ).build( );
1516 Version version = repoContent.getVersion( versionSelector );
1517 DataItem metaData = repoContent.getMetadataItem( version );
1518 assertFalse( metaData.exists( ) );
1519 assertEquals( "/javax/sql/jdbc/2.0/maven-metadata.xml", metaData.getAsset( ).getPath( ) );