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.model.ArtifactReference;
27 import org.apache.archiva.model.ProjectReference;
28 import org.apache.archiva.model.VersionedReference;
29 import org.apache.archiva.repository.EditableManagedRepository;
30 import org.apache.archiva.repository.LayoutException;
31 import org.apache.archiva.repository.ManagedRepository;
32 import org.apache.archiva.repository.ManagedRepositoryContent;
33 import org.apache.archiva.repository.RepositoryContent;
34 import org.apache.archiva.repository.content.Artifact;
35 import org.apache.archiva.repository.content.BaseArtifactTypes;
36 import org.apache.archiva.repository.content.ContentItem;
37 import org.apache.archiva.repository.content.ItemNotFoundException;
38 import org.apache.archiva.repository.content.ItemSelector;
39 import org.apache.archiva.repository.content.Namespace;
40 import org.apache.archiva.repository.content.Project;
41 import org.apache.archiva.repository.content.Version;
42 import org.apache.archiva.repository.content.base.ArchivaItemSelector;
43 import org.apache.archiva.repository.maven.MavenManagedRepository;
44 import org.apache.archiva.repository.maven.metadata.storage.ArtifactMappingProvider;
45 import org.apache.archiva.repository.storage.StorageAsset;
46 import org.apache.commons.io.FileUtils;
47 import org.junit.Before;
48 import org.junit.Test;
50 import javax.inject.Inject;
51 import javax.inject.Named;
52 import java.io.IOException;
53 import java.io.OutputStream;
54 import java.io.Reader;
55 import java.net.URISyntaxException;
56 import java.nio.charset.Charset;
57 import java.nio.file.Files;
58 import java.nio.file.Path;
59 import java.nio.file.Paths;
60 import java.nio.file.attribute.FileTime;
61 import java.util.ArrayList;
62 import java.util.Arrays;
63 import java.util.Collections;
64 import java.util.List;
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 AbstractManagedRepositoryContentTest
77 private ManagedDefaultRepositoryContent repoContent;
83 @Named ( "archivaConfiguration#default" )
84 ArchivaConfiguration archivaConfiguration;
87 List<? extends ArtifactMappingProvider> artifactMappingProviders;
90 MavenContentHelper contentHelper;
93 FileLockManager fileLockManager;
95 private Path getRepositoryPath(String repoName) {
98 return Paths.get( Thread.currentThread( ).getContextClassLoader( ).getResource( "repositories/" + repoName ).toURI( ) );
100 catch ( URISyntaxException e )
102 throw new RuntimeException( "Could not resolve repository path " + e.getMessage( ), e );
110 Path repoDir = getRepositoryPath( "default-repository" );
112 MavenManagedRepository repository = createRepository( "testRepo", "Unit Test Repo", repoDir );
114 FileType fileType = archivaConfiguration.getConfiguration().getRepositoryScanning().getFileTypes().get( 0 );
115 fileType.addPattern( "**/*.xml" );
116 assertEquals( FileTypes.ARTIFACTS, fileType.getId() );
118 fileTypes.afterConfigurationChange( null, "fileType", null );
120 repoContent = new ManagedDefaultRepositoryContent(repository, artifactMappingProviders, fileTypes, fileLockManager);
121 repoContent.setMavenContentHelper( contentHelper );
123 //repoContent = (ManagedRepositoryContent) lookup( ManagedRepositoryContent.class, "default" );
127 public void testGetVersionsBadArtifact()
130 assertGetVersions( "bad_artifact", Collections.emptyList() );
134 public void testGetVersionsMissingMultipleVersions()
137 assertGetVersions( "missing_metadata_b", Arrays.asList( "1.0", "1.0.1", "2.0", "2.0.1", "2.0-20070821-dev" ) );
141 public void testGetVersionsSimple()
144 assertVersions( "proxied_multi", "2.1", new String[]{ "2.1" } );
148 public void testGetVersionsSimpleYetIncomplete()
151 assertGetVersions( "incomplete_metadata_a", Collections.singletonList( "1.0" ) );
155 public void testGetVersionsSimpleYetMissing()
158 assertGetVersions( "missing_metadata_a", Collections.singletonList( "1.0" ) );
162 public void testGetVersionsSnapshotA()
165 assertVersions( "snap_shots_a", "1.0-alpha-11-SNAPSHOT",
166 new String[]{ "1.0-alpha-11-SNAPSHOT", "1.0-alpha-11-20070221.194724-2",
167 "1.0-alpha-11-20070302.212723-3", "1.0-alpha-11-20070303.152828-4",
168 "1.0-alpha-11-20070305.215149-5", "1.0-alpha-11-20070307.170909-6",
169 "1.0-alpha-11-20070314.211405-9", "1.0-alpha-11-20070316.175232-11" } );
173 public void testToMetadataPathFromProjectReference()
175 ProjectReference reference = new ProjectReference();
176 reference.setGroupId( "com.foo" );
177 reference.setArtifactId( "foo-tool" );
179 assertEquals( "com/foo/foo-tool/maven-metadata.xml", repoContent.toMetadataPath( reference ) );
183 public void testToMetadataPathFromVersionReference()
185 VersionedReference reference = new VersionedReference();
186 reference.setGroupId( "com.foo" );
187 reference.setArtifactId( "foo-tool" );
188 reference.setVersion( "1.0" );
190 assertEquals( "com/foo/foo-tool/1.0/maven-metadata.xml", repoContent.toMetadataPath( reference ) );
195 public void testToPathOnNullArtifactReference()
199 ArtifactReference reference = null;
200 repoContent.toPath( reference );
201 fail( "Should have failed due to null artifact reference." );
203 catch ( IllegalArgumentException e )
210 public void testExcludeMetadataFile()
213 assertVersions( "include_xml", "1.0", new String[]{ "1.0" } );
216 private void assertGetVersions( String artifactId, List<String> expectedVersions )
219 ProjectReference reference = new ProjectReference();
220 reference.setGroupId( "org.apache.archiva.metadata.tests" );
221 reference.setArtifactId( artifactId );
223 // Use the test metadata-repository, which is already setup for
224 // These kind of version tests.
225 Path repoDir = getRepositoryPath( "metadata-repository" );
226 (( EditableManagedRepository)repoContent.getRepository()).setLocation( repoDir.toAbsolutePath().toUri() );
228 // Request the versions.
229 Set<String> testedVersionSet = repoContent.getVersions( reference );
231 // Sort the list (for asserts)
232 VersionComparator comparator = new VersionComparator( );
233 List<String> testedVersions = new ArrayList<>();
234 testedVersions.addAll( testedVersionSet );
235 Collections.sort( testedVersions, comparator );
237 // Test the expected array of versions, to the actual tested versions
238 assertEquals( "available versions", expectedVersions, testedVersions );
240 ItemSelector selector = ArchivaItemSelector.builder( )
241 .withNamespace( "org.apache.archiva.metadata.tests" )
242 .withProjectId( artifactId )
244 Project project = repoContent.getProject( selector );
245 assertNotNull( project );
246 List<String> versions = repoContent.getVersions( project ).stream().map(v -> v.getVersion()).sorted( comparator ).collect( Collectors.toList());
247 assertArrayEquals( expectedVersions.toArray(), versions.toArray( ) );
250 private void assertVersions( String artifactId, String version, String[] expectedVersions )
253 VersionedReference reference = new VersionedReference();
254 reference.setGroupId( "org.apache.archiva.metadata.tests" );
255 reference.setArtifactId( artifactId );
256 reference.setVersion( version );
258 // Use the test metadata-repository, which is already setup for
259 // These kind of version tests.
260 Path repoDir = getRepositoryPath( "metadata-repository" );
261 ((EditableManagedRepository)repoContent.getRepository()).setLocation( repoDir.toAbsolutePath().toUri() );
263 // Request the versions.
264 Set<String> testedVersionSet = repoContent.getVersions( reference );
266 // Sort the list (for asserts later)
267 final VersionComparator comparator = new VersionComparator( );
268 List<String> testedVersions = new ArrayList<>();
269 testedVersions.addAll( testedVersionSet );
270 Collections.sort( testedVersions, comparator );
272 // Test the expected array of versions, to the actual tested versions
273 assertEquals( "Assert Versions: length/size", expectedVersions.length, testedVersions.size() );
275 for ( int i = 0; i < expectedVersions.length; i++ )
277 String actualVersion = testedVersions.get( i );
278 assertEquals( "Versions[" + i + "]", expectedVersions[i], actualVersion );
282 ItemSelector selector = ArchivaItemSelector.builder( )
283 .withNamespace( "org.apache.archiva.metadata.tests" )
284 .withProjectId( artifactId )
285 .withVersion( version )
287 List<String> versions = repoContent.getVersions( selector ).stream()
288 .map(v -> v.getVersion()).sorted( comparator ).collect( Collectors.toList());
289 assertArrayEquals( expectedVersions, versions.toArray( ) );
295 public void getTestGetProjectWithIllegalArgs() {
296 ItemSelector selector = ArchivaItemSelector.builder( )
297 .withNamespace( "org.apache" )
298 .withVersion( "1.0" )
302 repoContent.getProject( selector );
303 assertFalse( "Should throw IllegalArgumentException if no project id is given", true );
304 } catch (IllegalArgumentException e) {
306 assertTrue( e.getMessage( ).contains( "Project id must be set" ) );
311 public void getTestGetVersionWithIllegalArgs() {
312 ItemSelector selector = ArchivaItemSelector.builder( )
313 .withNamespace( "org.apache.maven" )
314 .withVersion( "1.0" )
318 repoContent.getVersion( selector );
319 assertFalse( "Should throw IllegalArgumentException if no project id is given", true );
320 } catch (IllegalArgumentException e) {
322 assertTrue( e.getMessage( ).contains( "Project id must be set" ) );
326 selector = ArchivaItemSelector.builder( )
327 .withNamespace( "org.apache.maven" )
328 .withProjectId( "shared" )
332 repoContent.getVersion( selector );
333 assertFalse( "Should throw IllegalArgumentException if no version is given", true );
334 } catch (IllegalArgumentException e) {
336 assertTrue( e.getMessage( ).contains( "Version must be set" ) );
341 public void getTestGetArtifactWithIllegalArgs() {
342 ItemSelector selector = ArchivaItemSelector.builder( )
343 .withNamespace( "org.apache.maven" )
344 .withVersion( "1.0" )
345 .withArtifactId( "shared" )
346 .withArtifactVersion("1.0")
350 repoContent.getArtifact( selector );
351 assertFalse( "Should throw IllegalArgumentException if no project id is given", true );
352 } catch (IllegalArgumentException e) {
354 assertTrue( e.getMessage( ).contains( "Project id must be set" ) );
358 selector = ArchivaItemSelector.builder( )
359 .withNamespace( "org.apache.maven" )
360 .withProjectId( "shared" )
361 .withArtifactId( "shared" )
362 .withArtifactVersion("1.0")
366 repoContent.getArtifact( selector );
367 assertFalse( "Should throw IllegalArgumentException if no version is given", true );
368 } catch (IllegalArgumentException e) {
370 assertTrue( e.getMessage( ).contains( "Version must be set" ) );
373 selector = ArchivaItemSelector.builder( )
374 .withNamespace( "org.apache.maven" )
375 .withProjectId( "shared" )
377 .withArtifactVersion("1.0")
381 repoContent.getArtifact( selector );
382 assertFalse( "Should throw IllegalArgumentException if no artifact id is given", true );
383 } catch (IllegalArgumentException e) {
385 assertTrue( e.getMessage( ).contains( "Artifact id must be set" ) );
392 public void testGetProjects() {
393 ItemSelector selector = ArchivaItemSelector.builder( )
394 .withNamespace( "org.apache.maven" ).build();
395 Namespace ns = repoContent.getNamespace( selector );
397 List<? extends Project> projects = repoContent.getProjects( ns );
398 assertEquals( 12, projects.size( ) );
399 String[] expected = new String[]{
400 "A", "B", "C", "archiva", "discovery", "maven-parent", "samplejar", "shared", "some-ejb", "test",
403 Object[] actual = projects.stream( ).map( p -> p.getId( ) ).sorted( ).toArray( );
404 assertArrayEquals( expected, actual);
408 public void testGetProjectsWithSelector() {
409 ItemSelector selector = ArchivaItemSelector.builder( )
410 .withNamespace( "org.apache.maven" ).build();
411 List<? extends Project> projects = repoContent.getProjects( selector );
412 assertEquals( 12, projects.size( ) );
413 String[] expected = new String[]{
414 "A", "B", "C", "archiva", "discovery", "maven-parent", "samplejar", "shared", "some-ejb", "test",
417 Object[] actual = projects.stream( ).map( p -> p.getId( ) ).sorted( ).toArray( );
418 assertArrayEquals( expected, actual);
422 public void testGetVersionsWithIllegalSelector() {
423 ItemSelector selector = ArchivaItemSelector.builder( )
424 .withNamespace( "org.apache.maven" ).build();
427 List<? extends Version> versions = repoContent.getVersions( selector );
428 assertFalse( "IllegalArgumentException expected, when project id not set", true );
429 } catch (IllegalArgumentException e) {
430 assertEquals( "Project id not set, while retrieving versions.", e.getMessage( ) );
435 public void testGetVersionsWithSelector() {
436 ItemSelector selector = ArchivaItemSelector.builder( )
437 .withNamespace( "org.apache.maven" )
438 .withProjectId( "samplejar" ).build();
439 List<? extends Version> versions = repoContent.getVersions( selector );
440 assertNotNull( versions );
441 assertEquals( 2, versions.size( ) );
446 protected ArtifactReference toArtifactReference( String path )
447 throws LayoutException
449 return repoContent.toArtifactReference( path );
453 protected ItemSelector toItemSelector( String path ) throws LayoutException
455 return repoContent.toItemSelector( path );
459 protected String toPath( ArtifactReference reference )
461 return repoContent.toPath( reference );
465 protected String toPath( ItemSelector selector ) {
466 return repoContent.toPath( selector );
470 protected ManagedRepositoryContent getManaged( )
476 protected RepositoryContent getContent( )
481 private Path setupRepoCopy( String source, String target) throws IOException
483 Path defaultRepo = getRepositoryPath( source );
484 Path newRepo = defaultRepo.getParent( ).resolve( target );
485 FileUtils.copyDirectory( defaultRepo.toFile( ), newRepo.toFile( ) );
487 MavenManagedRepository repository = createRepository( "testRepo", "Unit Test Repo", newRepo );
489 FileType fileType = archivaConfiguration.getConfiguration().getRepositoryScanning().getFileTypes().get( 0 );
490 fileType.addPattern( "**/*.xml" );
491 assertEquals( FileTypes.ARTIFACTS, fileType.getId() );
493 fileTypes.afterConfigurationChange( null, "fileType", null );
495 repoContent = new ManagedDefaultRepositoryContent(repository, artifactMappingProviders, fileTypes, fileLockManager);
501 public void testDeleteArtifactWithType() throws IOException, org.apache.archiva.repository.ContentNotFoundException, org.apache.archiva.repository.ContentAccessException
503 Path deleteRepo = setupRepoCopy( "delete-repository", "delete-repository-2" );
504 assertTrue( Files.exists( deleteRepo.resolve( "org/apache/maven/samplejar/1.0/samplejar-1.0-source.jar" ) ) );
505 assertTrue( Files.exists( deleteRepo.resolve( "org/apache/maven/samplejar/1.0/samplejar-1.0.jar" ) ) );
506 assertTrue( Files.exists( deleteRepo.resolve( "org/apache/maven/samplejar/1.0/samplejar-1.0.jar.md5" ) ) );
507 assertTrue( Files.exists( deleteRepo.resolve( "org/apache/maven/samplejar/1.0/samplejar-1.0.jar.sha1" ) ) );
508 assertTrue( Files.exists( deleteRepo.resolve( "org/apache/maven/samplejar/1.0/samplejar-1.0.pom" ) ) );
510 ArtifactReference ref = new ArtifactReference( );
511 ref.setGroupId( "org.apache.maven" );
512 ref.setArtifactId( "samplejar" );
513 ref.setVersion( "1.0" );
514 ref.setType( "jar" );
516 repoContent.deleteArtifact( ref );
518 assertTrue( Files.exists( deleteRepo.resolve( "org/apache/maven/samplejar/1.0/samplejar-1.0-source.jar" ) ) );
519 assertFalse( Files.exists( deleteRepo.resolve( "org/apache/maven/samplejar/1.0/samplejar-1.0.jar" ) ) );
520 assertTrue( Files.exists( deleteRepo.resolve( "org/apache/maven/samplejar/1.0/samplejar-1.0.jar.md5" ) ) );
521 assertTrue( Files.exists( deleteRepo.resolve( "org/apache/maven/samplejar/1.0/samplejar-1.0.jar.sha1" ) ) );
522 assertTrue( Files.exists( deleteRepo.resolve( "org/apache/maven/samplejar/1.0/samplejar-1.0.pom" ) ) );
529 public void testDeleteArtifactWithClassifier() throws IOException, org.apache.archiva.repository.ContentNotFoundException, org.apache.archiva.repository.ContentAccessException
531 Path deleteRepo = setupRepoCopy( "default-repository", "default-repository-2" );
532 assertTrue( Files.exists( deleteRepo.resolve( "org/apache/maven/samplejar/1.0/samplejar-1.0-source.jar" ) ) );
533 assertTrue( Files.exists( deleteRepo.resolve( "org/apache/maven/samplejar/1.0/samplejar-1.0-source.jar.sha1" ) ) );
534 assertTrue( Files.exists( deleteRepo.resolve( "org/apache/maven/samplejar/1.0/samplejar-1.0.jar" ) ) );
535 assertTrue( Files.exists( deleteRepo.resolve( "org/apache/maven/samplejar/1.0/samplejar-1.0.jar.md5" ) ) );
536 assertTrue( Files.exists( deleteRepo.resolve( "org/apache/maven/samplejar/1.0/samplejar-1.0.jar.sha1" ) ) );
537 assertTrue( Files.exists( deleteRepo.resolve( "org/apache/maven/samplejar/1.0/samplejar-1.0.pom" ) ) );
539 ArtifactReference ref = new ArtifactReference( );
540 ref.setGroupId( "org.apache.maven" );
541 ref.setArtifactId( "samplejar" );
542 ref.setVersion( "1.0" );
543 ref.setClassifier( "source" );
544 ref.setType( "jar" );
546 repoContent.deleteArtifact( ref );
548 assertFalse( Files.exists( deleteRepo.resolve( "org/apache/maven/samplejar/1.0/samplejar-1.0-source.jar" ) ) );
549 assertTrue( Files.exists( deleteRepo.resolve( "org/apache/maven/samplejar/1.0/samplejar-1.0-source.jar.sha1" ) ) );
550 assertTrue( Files.exists( deleteRepo.resolve( "org/apache/maven/samplejar/1.0/samplejar-1.0.jar" ) ) );
551 assertTrue( Files.exists( deleteRepo.resolve( "org/apache/maven/samplejar/1.0/samplejar-1.0.jar.md5" ) ) );
552 assertTrue( Files.exists( deleteRepo.resolve( "org/apache/maven/samplejar/1.0/samplejar-1.0.jar.sha1" ) ) );
553 assertTrue( Files.exists( deleteRepo.resolve( "org/apache/maven/samplejar/1.0/samplejar-1.0.pom" ) ) );
558 public void testDeleteArtifactWithoutType() throws IOException, org.apache.archiva.repository.ContentNotFoundException, org.apache.archiva.repository.ContentAccessException
560 Path deleteRepo = setupRepoCopy( "default-repository", "default-repository-2" );
561 assertTrue( Files.exists( deleteRepo.resolve( "org/apache/maven/samplejar/1.0/samplejar-1.0-source.jar" ) ) );
562 assertTrue( Files.exists( deleteRepo.resolve( "org/apache/maven/samplejar/1.0/samplejar-1.0-source.jar.sha1" ) ) );
563 assertTrue( Files.exists( deleteRepo.resolve( "org/apache/maven/samplejar/1.0/samplejar-1.0.jar" ) ) );
564 assertTrue( Files.exists( deleteRepo.resolve( "org/apache/maven/samplejar/1.0/samplejar-1.0.jar.md5" ) ) );
565 assertTrue( Files.exists( deleteRepo.resolve( "org/apache/maven/samplejar/1.0/samplejar-1.0.jar.sha1" ) ) );
566 assertTrue( Files.exists( deleteRepo.resolve( "org/apache/maven/samplejar/1.0/samplejar-1.0.pom" ) ) );
568 ArtifactReference ref = new ArtifactReference( );
569 ref.setGroupId( "org.apache.maven" );
570 ref.setArtifactId( "samplejar" );
571 ref.setVersion( "1.0" );
573 repoContent.deleteArtifact( ref );
575 assertFalse( Files.exists( deleteRepo.resolve( "org/apache/maven/samplejar/1.0" ) ) );
576 assertTrue( Files.exists( deleteRepo.resolve( "org/apache/maven/samplejar" ) ) );
582 public void testDeleteVersion() throws IOException, org.apache.archiva.repository.ContentNotFoundException, org.apache.archiva.repository.ContentAccessException
584 Path deleteRepo = setupRepoCopy( "delete-repository", "delete-repository-2" );
585 assertTrue( Files.exists( deleteRepo.resolve( "org/apache/maven/samplejar/1.0/samplejar-1.0-source.jar" ) ) );
586 assertTrue( Files.exists( deleteRepo.resolve( "org/apache/maven/samplejar/1.0/samplejar-1.0.jar" ) ) );
587 assertTrue( Files.exists( deleteRepo.resolve( "org/apache/maven/samplejar/1.0/samplejar-1.0.jar.md5" ) ) );
588 assertTrue( Files.exists( deleteRepo.resolve( "org/apache/maven/samplejar/1.0/samplejar-1.0.jar.sha1" ) ) );
589 assertTrue( Files.exists( deleteRepo.resolve( "org/apache/maven/samplejar/1.0/samplejar-1.0.pom" ) ) );
591 VersionedReference ref = new VersionedReference( ).groupId( "org.apache.maven" ).artifactId( "samplejar" ).version( "1.0" );
593 repoContent.deleteVersion( ref );
595 assertFalse( Files.exists( deleteRepo.resolve( "org/apache/maven/samplejar/1.0" ) ) );
600 public void testDeleteProject() throws IOException, org.apache.archiva.repository.ContentNotFoundException, org.apache.archiva.repository.ContentAccessException
602 Path deleteRepo = setupRepoCopy( "delete-repository", "delete-repository-2" );
603 assertTrue( Files.exists( deleteRepo.resolve( "org/apache/maven/samplejar/1.0/samplejar-1.0-source.jar" ) ) );
604 assertTrue( Files.exists( deleteRepo.resolve( "org/apache/maven/samplejar/1.0/samplejar-1.0.jar" ) ) );
605 assertTrue( Files.exists( deleteRepo.resolve( "org/apache/maven/samplejar/1.0/samplejar-1.0.jar.md5" ) ) );
606 assertTrue( Files.exists( deleteRepo.resolve( "org/apache/maven/samplejar/1.0/samplejar-1.0.jar.sha1" ) ) );
607 assertTrue( Files.exists( deleteRepo.resolve( "org/apache/maven/samplejar/1.0/samplejar-1.0.pom" ) ) );
609 ProjectReference ref = new ProjectReference( ).groupId( "org.apache.maven" ).artifactId( "samplejar" );
611 repoContent.deleteProject( ref );
613 assertFalse( Files.exists( deleteRepo.resolve( "org/apache/maven/samplejar/1.0" ) ) );
618 public void testGetArtifactStreamWithVersionSelector() {
619 ItemSelector selector = ArchivaItemSelector.builder( )
620 .withNamespace( "javax.sql" )
621 .withProjectId( "jdbc" )
622 .withVersion( "2.0" ).build();
623 try(Stream<? extends Artifact> stream = repoContent.newArtifactStream( selector ))
625 assertNotNull( stream );
626 List<? extends Artifact> results = stream.collect( Collectors.toList( ) );
627 checkArtifactListWithVersionSelector1( results );
632 public void testGetArtifactListWithVersionSelector() {
633 ItemSelector selector = ArchivaItemSelector.builder( )
634 .withNamespace( "javax.sql" )
635 .withProjectId( "jdbc" )
636 .withVersion( "2.0" ).build();
637 List<? extends Artifact> results = repoContent.getArtifacts( selector );
638 checkArtifactListWithVersionSelector1( results );
641 private void checkArtifactListWithVersionSelector1( List<? extends Artifact> results )
643 assertNotNull( results );
644 assertEquals( 2, results.size( ) );
645 Artifact mainArtifact = results.stream( ).filter( a -> a.getFileName( ).equals( "jdbc-2.0.jar" ) ).findFirst( ).get( );
646 assertNotNull( mainArtifact );
647 assertEquals( BaseArtifactTypes.MAIN, mainArtifact.getArtifactType( ) );
648 Artifact metaArtifact = results.stream( ).filter( a -> a.getFileName( ).equals( "maven-metadata-repository.xml" ) ).findFirst( ).get( );
649 assertNotNull( metaArtifact );
650 assertEquals( MavenTypes.REPOSITORY_METADATA, metaArtifact.getArtifactType( ) );
654 public void testGetArtifactStreamWithVersionSelector2() {
655 ItemSelector selector = ArchivaItemSelector.builder( )
656 .withNamespace( "org.apache.axis2" )
657 .withProjectId( "axis2" )
658 .withVersion( "1.3-SNAPSHOT" ).build();
659 try(Stream<? extends Artifact> stream = repoContent.newArtifactStream( selector ))
661 assertNotNull( stream );
662 List<? extends Artifact> results = stream.collect( Collectors.toList( ) );
663 checkArtifactListWithVersionSelector2( results );
668 public void testGetArtifactListWithVersionSelector2() {
669 ItemSelector selector = ArchivaItemSelector.builder( )
670 .withNamespace( "org.apache.axis2" )
671 .withProjectId( "axis2" )
672 .withVersion( "1.3-SNAPSHOT" ).build();
673 List<? extends Artifact> results = repoContent.getArtifacts( selector );
674 checkArtifactListWithVersionSelector2( results );
677 private void checkArtifactListWithVersionSelector2( List<? extends Artifact> results )
679 assertNotNull( results );
680 assertEquals( 39, results.size( ) );
682 Artifact artifact = results.stream( ).filter( a -> a.getFileName( ).equals( "axis2-1.3-20070725.210059-1.pom" ) )
683 .findFirst( ).get( );
685 assertNotNull( artifact );
686 assertEquals( "pom", artifact.getExtension( ) );
687 assertEquals( BaseArtifactTypes.MAIN, artifact.getArtifactType( ) );
688 assertEquals( "1.3-SNAPSHOT", artifact.getVersion( ).getVersion( ) );
689 assertEquals( "1.3-20070725.210059-1", artifact.getArtifactVersion( ) );
690 assertEquals( ".pom", artifact.getRemainder( ) );
691 assertEquals( "axis2", artifact.getId( ) );
692 assertEquals( "axis2", artifact.getVersion( ).getProject( ).getId( ) );
693 assertEquals( "org.apache.axis2", artifact.getVersion( ).getProject( ).getNamespace( ).getNamespace( ) );
694 assertEquals( "", artifact.getClassifier( ) );
695 assertEquals( "pom", artifact.getType( ) );
698 artifact = results.stream( ).filter( a -> a.getFileName( ).equals( "axis2-1.3-20070725.210059-1.pom.md5" ) )
699 .findFirst( ).get( );
701 assertNotNull( artifact );
702 assertEquals( "md5", artifact.getExtension( ) );
703 assertEquals( BaseArtifactTypes.RELATED, artifact.getArtifactType( ) );
704 assertEquals( "1.3-SNAPSHOT", artifact.getVersion( ).getVersion( ) );
705 assertEquals( "1.3-20070725.210059-1", artifact.getArtifactVersion( ) );
706 assertEquals( ".pom.md5", artifact.getRemainder( ) );
707 assertEquals( "axis2", artifact.getId( ) );
708 assertEquals( "axis2", artifact.getVersion( ).getProject( ).getId( ) );
709 assertEquals( "org.apache.axis2", artifact.getVersion( ).getProject( ).getNamespace( ).getNamespace( ) );
710 assertEquals( "", artifact.getClassifier( ) );
711 assertEquals( "md5", artifact.getType( ) );
715 artifact = results.stream( ).filter( a -> a.getFileName( ).equals( "maven-metadata.xml" ) )
716 .findFirst( ).get( );
717 assertNotNull( artifact );
718 assertEquals( BaseArtifactTypes.METADATA, artifact.getArtifactType( ) );
719 assertEquals( "1.3-SNAPSHOT", artifact.getVersion( ).getVersion( ) );
720 assertEquals( "xml", artifact.getExtension( ) );
724 public void testGetArtifactListWithArtifactSelector1() {
725 ItemSelector selector = ArchivaItemSelector.builder( )
726 .withNamespace( "org.apache.axis2" )
727 .withProjectId( "axis2" )
728 .withVersion( "1.3-SNAPSHOT" )
729 .withArtifactVersion( "1.3-20070731.113304-21" )
730 .withExtension( "pom" )
732 List<? extends Artifact> results = repoContent.getArtifacts( selector );
733 checkArtifactListWithArtifactSelector1( results );
737 public void testGetArtifactStreamWithArtifactSelector1() {
738 ItemSelector selector = ArchivaItemSelector.builder( )
739 .withNamespace( "org.apache.axis2" )
740 .withProjectId( "axis2" )
741 .withVersion( "1.3-SNAPSHOT" )
742 .withArtifactVersion( "1.3-20070731.113304-21" )
743 .withExtension( "pom" )
745 try(Stream<? extends Artifact> results = repoContent.newArtifactStream( selector ))
747 checkArtifactListWithArtifactSelector1( results.collect( Collectors.toList()) );
751 private void checkArtifactListWithArtifactSelector1( List<? extends Artifact> results )
753 assertNotNull( results );
754 assertEquals( 1, results.size( ) );
755 Artifact artifact = results.get( 0 );
756 assertEquals( "pom", artifact.getExtension( ) );
757 assertEquals( BaseArtifactTypes.MAIN, artifact.getArtifactType( ) );
761 public void testGetArtifactListWithArtifactSelector2() {
762 ItemSelector selector = ArchivaItemSelector.builder( )
763 .withNamespace( "org.apache.axis2" )
764 .withProjectId( "axis2" )
765 .withVersion( "1.3-SNAPSHOT" )
766 .withArtifactVersion( "1.3-20070731.113304-21" )
767 .withExtension( "pom" )
768 .includeRelatedArtifacts()
770 List<? extends Artifact> results = repoContent.getArtifacts( selector );
772 checkArtifactListWithArtifactSelector2( results );
777 public void testGetArtifactStreamWithArtifactSelector2() {
778 ItemSelector selector = ArchivaItemSelector.builder( )
779 .withNamespace( "org.apache.axis2" )
780 .withProjectId( "axis2" )
781 .withVersion( "1.3-SNAPSHOT" )
782 .withArtifactVersion( "1.3-20070731.113304-21" )
783 .withExtension( "pom" )
784 .includeRelatedArtifacts()
786 try(Stream<? extends Artifact> results = repoContent.newArtifactStream( selector ))
788 checkArtifactListWithArtifactSelector2( results.collect( Collectors.toList()) );
792 private void checkArtifactListWithArtifactSelector2( List<? extends Artifact> results )
794 assertNotNull( results );
795 assertEquals( 3, results.size( ) );
796 Artifact artifact = results.stream( ).filter( a -> a.getFileName( ).equalsIgnoreCase( "axis2-1.3-20070731.113304-21.pom" ) )
797 .findFirst( ).get( );
798 assertNotNull( artifact );
799 assertEquals( "pom", artifact.getExtension( ) );
800 assertEquals( BaseArtifactTypes.MAIN, artifact.getArtifactType( ) );
802 artifact = results.stream( ).filter( a -> a.getFileName( ).equalsIgnoreCase( "axis2-1.3-20070731.113304-21.pom.sha1" ) )
803 .findFirst( ).get( );
804 assertNotNull( artifact );
805 assertEquals( "sha1", artifact.getExtension( ) );
806 assertEquals( BaseArtifactTypes.RELATED, artifact.getArtifactType( ) );
811 public void testArtifactListWithProjectSelector() {
812 ItemSelector selector = ArchivaItemSelector.builder( )
813 .withNamespace( "org.apache.maven.shared" )
814 .withProjectId( "maven-downloader" )
816 List<? extends Artifact> results = repoContent.getArtifacts( selector );
817 checkArtifactListWithProjectSelector( results );
822 public void testArtifactStreamWithProjectSelector() {
823 ItemSelector selector = ArchivaItemSelector.builder( )
824 .withNamespace( "org.apache.maven.shared" )
825 .withProjectId( "maven-downloader" )
827 Stream<? extends Artifact> results = repoContent.newArtifactStream( selector );
828 checkArtifactListWithProjectSelector( results.collect( Collectors.toList()) );
832 private void checkArtifactListWithProjectSelector( List<? extends Artifact> results )
834 assertNotNull( results );
835 assertEquals( 27, results.size( ) );
837 Artifact artifact = results.stream( ).filter( a -> a.getFileName( ).equalsIgnoreCase( "maven-metadata.xml" ) )
838 .findFirst( ).get( );
839 assertNotNull( artifact );
840 assertEquals( "xml", artifact.getExtension( ) );
841 assertEquals( BaseArtifactTypes.METADATA, artifact.getArtifactType( ) );
843 artifact = results.stream( ).filter( a -> a.getFileName( ).equalsIgnoreCase( "maven-downloader-1.0-sources.jar" ) )
844 .findFirst( ).get( );
845 assertNotNull( artifact );
846 assertEquals( BaseArtifactTypes.MAIN, artifact.getArtifactType( ) );
847 assertEquals( "sources", artifact.getClassifier( ) );
848 assertEquals( "java-source", artifact.getType( ) );
850 artifact = results.stream( ).filter( a -> a.getFileName( ).equalsIgnoreCase( "maven-downloader-1.0-sources.jar.sha1" ) )
851 .findFirst( ).get( );
852 assertNotNull( artifact );
853 assertEquals( BaseArtifactTypes.RELATED, artifact.getArtifactType( ) );
854 assertEquals( "sources", artifact.getClassifier( ) );
855 assertEquals( "sha1", artifact.getType( ) );
856 assertEquals( ".jar.sha1", artifact.getRemainder( ) );
860 public void testArtifactListWithNamespaceSelector() {
861 ItemSelector selector = ArchivaItemSelector.builder( )
862 .withNamespace( "org.multilevel" )
864 List<? extends Artifact> results = repoContent.getArtifacts( selector );
865 assertNotNull( results );
866 assertEquals( 3, results.size( ) );
867 assertTrue( results.get( 0 ).getFileName( ).startsWith( "testproj1" ) );
871 public void testArtifactListWithNamespaceSelectorRecursive() {
872 ItemSelector selector = ArchivaItemSelector.builder( )
873 .withNamespace( "org.multilevel" )
876 List<? extends Artifact> results = repoContent.getArtifacts( selector );
877 checkArtifactListWithNamespaceSelectorRecursive( results );
881 public void testArtifactStreamWithNamespaceSelectorRecursive() {
882 ItemSelector selector = ArchivaItemSelector.builder( )
883 .withNamespace( "org.multilevel" )
886 Stream<? extends Artifact> results = repoContent.newArtifactStream( selector );
887 checkArtifactListWithNamespaceSelectorRecursive( results.collect( Collectors.toList()) );
890 private void checkArtifactListWithNamespaceSelectorRecursive( List<? extends Artifact> results )
892 assertNotNull( results );
893 assertEquals( 6, results.size( ) );
895 Artifact artifact = results.stream( ).filter( a -> a.getFileName( ).equalsIgnoreCase( "testproj2-1.0.pom" ) )
896 .findFirst( ).get( );
897 assertNotNull( artifact );
898 assertEquals( 6, artifact.getAsset( ).getParent( ).getPath( ).split( "/" ).length );
900 artifact = results.stream( ).filter( a -> a.getFileName( ).equalsIgnoreCase( "testproj1-1.0.pom" ) )
901 .findFirst( ).get( );
902 assertNotNull( artifact );
903 assertEquals( 5, artifact.getAsset( ).getParent( ).getPath( ).split( "/" ).length );
908 public void testArtifactListWithArtifactSelector1() {
909 ItemSelector selector = ArchivaItemSelector.builder( )
910 .withNamespace( "org.apache.maven" )
911 .withProjectId( "test" )
912 .withVersion( "1.0-SNAPSHOT" )
913 .withArtifactId( "test" )
914 .withArtifactVersion( "1.0-20050611.112233-1" )
917 List<? extends Artifact> results = repoContent.getArtifacts( selector );
919 assertNotNull( results );
920 assertEquals( 1, results.size( ) );
922 Artifact artifact = results.stream( ).filter( a -> a.getFileName( ).equalsIgnoreCase( "test-1.0-20050611.112233-1.jar" ) )
924 assertNotNull( artifact );
925 assertEquals( "", artifact.getClassifier( ) );
929 public void testArtifactListWithArtifactSelector2() {
930 ItemSelector selector = ArchivaItemSelector.builder( )
931 .withNamespace( "org.apache.maven" )
932 .withProjectId( "test" )
933 .withVersion( "1.0-SNAPSHOT" )
934 .withClassifier( "*" )
935 .withArtifactId( "test" )
936 .withArtifactVersion( "1.0-20050611.112233-1" )
939 List<? extends Artifact> results = repoContent.getArtifacts( selector );
941 assertNotNull( results );
942 assertEquals( 2, results.size( ) );
944 Artifact artifact = results.stream( ).filter( a -> a.getFileName( ).equalsIgnoreCase( "test-1.0-20050611.112233-1-javadoc.jar" ) )
946 assertNotNull( artifact );
947 assertEquals( "javadoc", artifact.getClassifier( ) );
948 assertEquals( "javadoc", artifact.getType( ) );
952 public void testArtifactListWithArtifactSelector3() {
953 ItemSelector selector = ArchivaItemSelector.builder( )
954 .withNamespace( "org.apache.maven" )
955 .withProjectId( "test" )
956 .withVersion( "1.0-SNAPSHOT" )
957 .withClassifier( "*" )
958 .withArtifactVersion( "1.0-20050611.112233-1" )
961 List<? extends Artifact> results = repoContent.getArtifacts( selector );
963 assertNotNull( results );
964 assertEquals( 3, results.size( ) );
966 Artifact artifact = results.stream( ).filter( a -> a.getFileName( ).equalsIgnoreCase( "test-1.0-20050611.112233-1-javadoc.jar" ) )
968 assertNotNull( artifact );
969 assertEquals( "javadoc", artifact.getClassifier( ) );
970 assertEquals( "javadoc", artifact.getType( ) );
972 artifact = results.stream( ).filter( a -> a.getFileName( ).equalsIgnoreCase( "wrong-artifactId-1.0-20050611.112233-1.jar" ) )
974 assertNotNull( artifact );
975 assertEquals( "", artifact.getClassifier( ) );
976 assertEquals( "wrong-artifactId", artifact.getId( ) );
980 public void testArtifactListWithArtifactSelector4() {
981 ItemSelector selector = ArchivaItemSelector.builder( )
982 .withNamespace( "org.apache.maven" )
983 .withProjectId( "test" )
984 .withVersion( "1.0-SNAPSHOT" )
985 .withClassifier( "" )
988 List<? extends Artifact> results = repoContent.getArtifacts( selector );
990 assertNotNull( results );
991 assertEquals( 5, results.size( ) );
993 Artifact artifact = results.stream( ).filter( a -> a.getFileName( ).equalsIgnoreCase( "test-1.0-20050611.112233-1-javadoc.jar" ) )
995 assertNotNull( artifact );
996 assertEquals( "javadoc", artifact.getClassifier( ) );
997 assertEquals( "javadoc", artifact.getType( ) );
999 artifact = results.stream( ).filter( a -> a.getFileName( ).equalsIgnoreCase( "wrong-artifactId-1.0-20050611.112233-1.jar" ) )
1001 assertNotNull( artifact );
1002 assertEquals( "", artifact.getClassifier( ) );
1003 assertEquals( "wrong-artifactId", artifact.getId( ) );
1005 artifact = results.stream( ).filter( a -> a.getFileName( ).equalsIgnoreCase( "wrong-artifactId-1.0-20050611.1122x-1.jar" ) )
1007 assertNotNull( artifact );
1008 assertEquals( "", artifact.getClassifier( ) );
1009 assertEquals( "wrong-artifactId", artifact.getId( ) );
1010 assertEquals( "", artifact.getArtifactVersion( ) );
1012 artifact = results.stream( ).filter( a -> a.getFileName( ).equalsIgnoreCase( "test-1.0-20050611.1122x-1.jar" ) )
1014 assertNotNull( artifact );
1015 assertEquals( "", artifact.getClassifier( ) );
1016 assertEquals( "test", artifact.getId( ) );
1017 assertEquals( "1.0-20050611.1122x-1", artifact.getArtifactVersion( ) );
1022 public void testArtifactListWithArtifactSelectorWithClassifier() {
1023 ItemSelector selector = ArchivaItemSelector.builder( )
1024 .withNamespace( "org.apache.maven" )
1025 .withProjectId( "test" )
1026 .withVersion( "1.0-SNAPSHOT" )
1027 .withArtifactId( "test" )
1028 .withClassifier( "javadoc" )
1029 .withArtifactVersion( "1.0-20050611.112233-1" )
1032 List<? extends Artifact> results = repoContent.getArtifacts( selector );
1034 assertNotNull( results );
1035 assertEquals( 1, results.size( ) );
1037 Artifact artifact = results.stream( ).filter( a -> a.getFileName( ).equalsIgnoreCase( "test-1.0-20050611.112233-1-javadoc.jar" ) )
1039 assertNotNull( artifact );
1040 assertEquals( "javadoc", artifact.getClassifier( ) );
1041 assertEquals( "javadoc", artifact.getType( ) );
1045 public void testArtifactListWithArtifactSelectorWrongArtifact() {
1046 ItemSelector selector = ArchivaItemSelector.builder( )
1047 .withNamespace( "org.apache.maven" )
1048 .withProjectId( "test" )
1049 .withVersion( "1.0-SNAPSHOT" )
1050 .withArtifactId( "wrong-artifactId" )
1051 .withArtifactVersion( "1.0-20050611.112233-1" )
1054 List<? extends Artifact> results = repoContent.getArtifacts( selector );
1056 assertNotNull( results );
1057 assertEquals( 1, results.size( ) );
1059 Artifact artifact = results.stream( ).filter( a -> a.getFileName( ).equalsIgnoreCase( "wrong-artifactId-1.0-20050611.112233-1.jar" ) )
1061 assertNotNull( artifact );
1065 public void testArtifactListWithArtifactSelectorVersionPattern() {
1066 ItemSelector selector = ArchivaItemSelector.builder( )
1067 .withNamespace( "org.apache.maven" )
1068 .withProjectId( "test" )
1069 .withVersion( "1.0-SNAPSHOT" )
1070 .withArtifactVersion( "1.0-*" )
1073 List<? extends Artifact> results = repoContent.getArtifacts( selector );
1075 assertNotNull( results );
1076 assertEquals( 5, results.size( ) );
1078 Artifact artifact = results.stream( ).filter( a -> a.getFileName( ).equalsIgnoreCase( "wrong-artifactId-1.0-20050611.112233-1.jar" ) )
1080 assertNotNull( artifact );
1084 public void testGetArtifactFromContentItem() {
1085 ItemSelector selector = ArchivaItemSelector.builder( )
1086 .withNamespace( "org.apache.maven" ).build();
1087 Namespace ns = repoContent.getNamespace( selector );
1088 List<? extends Artifact> artifacts = repoContent.getArtifacts( ns );
1089 assertNotNull( artifacts );
1090 assertEquals( 39, artifacts.size( ) );
1091 List<? extends Artifact> artifacts2 = repoContent.getArtifacts( (ContentItem)ns );
1092 assertArrayEquals( artifacts.toArray(), artifacts2.toArray() );
1094 selector = ArchivaItemSelector.builder( )
1095 .withNamespace( "org.apache.maven.shared" )
1096 .withProjectId( "maven-downloader" )
1098 Project project = repoContent.getProject( selector );
1099 artifacts = repoContent.getArtifacts( project );
1100 assertNotNull( artifacts );
1101 assertEquals( 27, artifacts.size( ) );
1102 artifacts2 = repoContent.getArtifacts( (ContentItem)project );
1103 assertArrayEquals( artifacts.toArray(), artifacts2.toArray() );
1105 selector = ArchivaItemSelector.builder( )
1106 .withNamespace( "org.apache.maven.shared" )
1107 .withProjectId( "maven-downloader" )
1108 .withVersion( "1.1" )
1110 Version version = repoContent.getVersion( selector );
1111 artifacts = repoContent.getArtifacts( version );
1112 assertNotNull( artifacts );
1113 assertEquals( 12, artifacts.size( ) );
1114 artifacts2 = repoContent.getArtifacts( (ContentItem)version );
1115 assertArrayEquals( artifacts.toArray(), artifacts2.toArray() );
1120 public void testGetRelatedArtifactsFromArtifact() {
1121 ItemSelector selector = ArchivaItemSelector.builder( )
1122 .withNamespace( "org.apache.maven.shared" )
1123 .withProjectId( "maven-downloader" )
1124 .withVersion( "1.1" )
1125 .withExtension( "jar" )
1126 .withArtifactId( "maven-downloader" ).build( );
1128 Artifact artifact = repoContent.getArtifact( selector );
1129 assertNotNull( artifact );
1130 List<? extends Artifact> artifacts = repoContent.getArtifacts( artifact );
1131 assertNotNull( artifacts );
1132 assertEquals( 2, artifacts.size( ) );
1137 public void testToItemFromPath() throws LayoutException
1139 String path = "/org/apache/maven/shared";
1140 ContentItem item = repoContent.toItem( path );
1141 assertNotNull( item );
1142 assertTrue( item instanceof Namespace );
1144 path = "/org/apache/maven/shared/maven-downloader";
1145 item = repoContent.toItem( path );
1146 assertNotNull( item );
1147 assertTrue( item instanceof Project );
1149 path = "/org/apache/maven/shared/maven-downloader/1.1";
1150 item = repoContent.toItem( path );
1151 assertNotNull( item );
1152 assertTrue( item instanceof Version );
1154 path = "/org/apache/maven/shared/maven-downloader/1.1/maven-downloader-1.1.jar";
1155 item = repoContent.toItem( path );
1156 assertNotNull( item );
1157 assertTrue( item instanceof Artifact );
1162 public void testToItemFromAssetPath() throws LayoutException
1164 StorageAsset path = repoContent.getRepository().getAsset("/org/apache/maven/shared");
1165 ContentItem item = repoContent.toItem( path );
1166 assertNotNull( item );
1167 assertTrue( item instanceof Namespace );
1169 path = repoContent.getRepository( ).getAsset( "/org/apache/maven/shared/maven-downloader" );
1170 item = repoContent.toItem( path );
1171 assertNotNull( item );
1172 assertTrue( item instanceof Project );
1174 path = repoContent.getRepository( ).getAsset( "/org/apache/maven/shared/maven-downloader/1.1" );
1175 item = repoContent.toItem( path );
1176 assertNotNull( item );
1177 assertTrue( item instanceof Version );
1179 path = repoContent.getRepository( ).getAsset( "/org/apache/maven/shared/maven-downloader/1.1/maven-downloader-1.1.jar" );
1180 item = repoContent.toItem( path );
1181 assertNotNull( item );
1182 assertTrue( item instanceof Artifact );
1187 public void testHasContent() throws LayoutException
1189 ItemSelector selector = ArchivaItemSelector.builder( )
1190 .withNamespace( "org.apache.maven.shared" )
1191 .withProjectId( "maven-downloader" )
1192 .withVersion( "1.1" )
1193 .withArtifactId( "maven-downloader" )
1194 .withExtension( "jar" )
1197 assertTrue( repoContent.hasContent( selector ) );
1199 selector = ArchivaItemSelector.builder( )
1200 .withNamespace( "org.apache.maven.shared" )
1201 .withProjectId( "maven-downloader" )
1202 .withVersion( "1.1" )
1203 .withArtifactId( "maven-downloader" )
1204 .withExtension( "zip" )
1207 assertFalse( repoContent.hasContent( selector ) );
1212 public void testGetItemWithNamespaceSelector() {
1213 ItemSelector selector = ArchivaItemSelector.builder( )
1214 .withNamespace( "org.apache.maven" )
1216 ContentItem item = repoContent.getItem( selector );
1217 assertNotNull( item );
1218 assertTrue( item instanceof Namespace );
1222 public void testGetItemWithProjectSelector() {
1223 ItemSelector selector = ArchivaItemSelector.builder( )
1224 .withNamespace( "org.apache.maven" )
1225 .withProjectId( "shared" )
1227 ContentItem item = repoContent.getItem( selector );
1228 assertNotNull( item );
1229 assertTrue( item instanceof Project );
1233 public void testGetItemWithVersionSelector() {
1234 ItemSelector selector = ArchivaItemSelector.builder( )
1235 .withNamespace( "org.apache.maven" )
1236 .withProjectId( "samplejar" )
1239 ContentItem item = repoContent.getItem( selector );
1240 assertNotNull( item );
1241 assertTrue( item instanceof Version );
1245 public void testGetItemWithArtifactSelector() {
1246 ItemSelector selector = ArchivaItemSelector.builder( )
1247 .withNamespace( "org.apache.maven" )
1248 .withProjectId( "samplejar" )
1250 .withArtifactId( "samplejar" )
1252 ContentItem item = repoContent.getItem( selector );
1253 assertNotNull( item );
1254 assertTrue( item instanceof Artifact );
1258 public void testGetNamespaceFromPath() {
1259 StorageAsset path = repoContent.getRepository( ).getAsset( "/org/apache/axis2" );
1260 Namespace ns = repoContent.getNamespaceFromPath( path );
1261 assertNotNull( ns );
1262 assertEquals( "org.apache.axis2", ns.getNamespace( ) );
1267 public void testArtifactListWithArtifactSelectorAndRelated() {
1268 ItemSelector selector = ArchivaItemSelector.builder( )
1269 .withNamespace( "org.apache.maven" )
1270 .withProjectId( "samplejar" )
1271 .withVersion( "1.0" )
1272 .withArtifactVersion( "1.0" )
1273 .withArtifactId( "samplejar" )
1274 .withExtension( "jar" )
1275 .includeRelatedArtifacts()
1278 List<? extends Artifact> results = repoContent.getArtifacts( selector );
1280 assertNotNull( results );
1281 assertEquals( 3, results.size( ) );
1283 Artifact artifact = results.stream( ).filter( a -> a.getFileName( ).equalsIgnoreCase( "samplejar-1.0.jar" ) )
1285 assertNotNull( artifact );
1286 assertEquals( BaseArtifactTypes.MAIN, artifact.getArtifactType( ) );
1288 artifact = results.stream( ).filter( a -> a.getFileName( ).equalsIgnoreCase( "samplejar-1.0.jar.md5" ) )
1290 assertNotNull( artifact );
1291 assertEquals( BaseArtifactTypes.RELATED, artifact.getArtifactType( ) );
1292 assertEquals( "md5", artifact.getExtension( ) );
1294 artifact = results.stream( ).filter( a -> a.getFileName( ).equalsIgnoreCase( "samplejar-1.0.jar.sha1" ) )
1296 assertNotNull( artifact );
1297 assertEquals( BaseArtifactTypes.RELATED, artifact.getArtifactType( ) );
1298 assertEquals( "sha1", artifact.getExtension( ) );
1302 private Path copyRepository(String repoName) throws IOException, URISyntaxException
1304 Path tempDir = Files.createTempDirectory( "archiva-repocontent" );
1305 Path repoSource = Paths.get( Thread.currentThread( ).getContextClassLoader( ).getResource( "repositories/" + repoName ).toURI( ) );
1306 assertTrue( Files.exists( repoSource ) );
1307 FileUtils.copyDirectory( repoSource.toFile( ), tempDir.toFile() );
1311 private ManagedRepository createManagedRepoWithContent(String sourceRepoName) throws IOException, URISyntaxException
1313 Path repoDir = copyRepository( sourceRepoName );
1314 MavenManagedRepository repo = createRepository( sourceRepoName, sourceRepoName, repoDir );
1315 ManagedDefaultRepositoryContent deleteRepoContent = new ManagedDefaultRepositoryContent( repo, artifactMappingProviders, fileTypes, fileLockManager );
1316 deleteRepoContent.setMavenContentHelper( contentHelper );
1321 public void deleteNamespaceItem() throws IOException, URISyntaxException, ItemNotFoundException
1323 ManagedRepository repo = createManagedRepoWithContent( "delete-repository" );
1324 ManagedRepositoryContent myRepoContent = repo.getContent( );
1325 Path repoRoot = repo.getAsset( "" ).getFilePath( );
1326 assertTrue( Files.exists(repoRoot.resolve( "org/apache/maven" )) );
1327 ArchivaItemSelector selector = ArchivaItemSelector.builder( )
1328 .withNamespace( "org.apache.maven" ).build();
1329 ContentItem item = myRepoContent.getItem( selector );
1330 assertTrue( item instanceof Namespace );
1331 myRepoContent.deleteItem( item );
1332 assertFalse( Files.exists(repoRoot.resolve( "org/apache/maven" )) );
1333 assertTrue( Files.exists(repoRoot.resolve( "org/apache" )) );
1335 // Sub namespaces are deleted too
1336 assertTrue( Files.exists(repoRoot.resolve( "org/apache/test/samplejar" )) );
1337 assertTrue( Files.exists(repoRoot.resolve( "org/apache/test/sub/samplejar" )) );
1338 selector = ArchivaItemSelector.builder( )
1339 .withNamespace( "org.apache.test" ).build();
1340 item = myRepoContent.getItem( selector );
1341 assertTrue( item instanceof Namespace );
1342 myRepoContent.deleteItem( item );
1343 assertFalse( Files.exists(repoRoot.resolve( "org/apache/test/samplejar" )) );
1344 assertFalse( Files.exists(repoRoot.resolve( "org/apache/test/sub/samplejar" )) );
1348 public void deleteProjectItem() throws IOException, URISyntaxException, ItemNotFoundException
1350 ManagedRepository repo = createManagedRepoWithContent( "delete-repository" );
1351 ManagedRepositoryContent myRepoContent = repo.getContent( );
1352 Path repoRoot = repo.getAsset( "" ).getFilePath( );
1353 assertTrue( Files.exists(repoRoot.resolve( "org/apache/maven/A" )) );
1354 ArchivaItemSelector selector = ArchivaItemSelector.builder( )
1355 .withNamespace( "org.apache.maven" )
1356 .withProjectId( "A" ).build();
1357 ContentItem item = myRepoContent.getItem( selector );
1358 assertTrue( item instanceof Project );
1359 myRepoContent.deleteItem( item );
1360 assertTrue( Files.exists(repoRoot.resolve( "org/apache/maven" )) );
1361 assertTrue( Files.exists( repoRoot.resolve( "org/apache/maven/samplejar/1.0" ) ) );
1362 assertTrue( Files.exists( repoRoot.resolve( "org/apache/maven/samplejar/2.0" ) ) );
1363 assertFalse( Files.exists(repoRoot.resolve( "org/apache/maven/A" )) );
1365 assertTrue( Files.exists(repoRoot.resolve( "org/apache/test/samplejar" )) );
1366 assertTrue( Files.exists(repoRoot.resolve( "org/apache/test/sub/samplejar" )) );
1367 selector = ArchivaItemSelector.builder( )
1368 .withNamespace( "org.apache.test" )
1369 .withProjectId( "samplejar" ).build();
1370 item = myRepoContent.getItem( selector );
1371 assertTrue( item instanceof Project );
1372 myRepoContent.deleteItem( item );
1373 assertFalse( Files.exists(repoRoot.resolve( "org/apache/test/samplejar" )) );
1374 assertTrue( Files.exists(repoRoot.resolve( "org/apache/test/sub/samplejar" )) );
1378 public void deleteVersionItem() throws IOException, URISyntaxException, ItemNotFoundException
1380 ManagedRepository repo = createManagedRepoWithContent( "delete-repository" );
1381 ManagedRepositoryContent myRepoContent = repo.getContent( );
1382 Path repoRoot = repo.getAsset( "" ).getFilePath( );
1383 assertTrue( Files.exists(repoRoot.resolve( "org/apache/maven/A/1.0" )) );
1384 ArchivaItemSelector selector = ArchivaItemSelector.builder( )
1385 .withNamespace( "org.apache.maven" )
1386 .withProjectId( "A" )
1387 .withVersion( "1.0" ).build();
1388 ContentItem item = myRepoContent.getItem( selector );
1389 assertTrue( item instanceof Version );
1390 myRepoContent.deleteItem( item );
1391 assertTrue( Files.exists(repoRoot.resolve( "org/apache/maven/A" )) );
1392 assertTrue( Files.exists( repoRoot.resolve( "org/apache/maven/samplejar/1.0" ) ) );
1393 assertTrue( Files.exists( repoRoot.resolve( "org/apache/maven/samplejar/2.0" ) ) );
1394 assertFalse( Files.exists(repoRoot.resolve( "org/apache/maven/A/1.0" )) );
1396 assertTrue( Files.exists(repoRoot.resolve( "org/apache/test/samplejar" )) );
1397 assertTrue( Files.exists(repoRoot.resolve( "org/apache/test/sub/samplejar" )) );
1398 selector = ArchivaItemSelector.builder( )
1399 .withNamespace( "org.apache.test" )
1400 .withProjectId( "samplejar" )
1401 .withVersion( "2.0" ).build();
1402 item = myRepoContent.getItem( selector );
1403 assertTrue( item instanceof Version );
1404 myRepoContent.deleteItem( item );
1405 assertFalse( Files.exists(repoRoot.resolve( "org/apache/test/samplejar/2.0" )) );
1406 assertTrue( Files.exists(repoRoot.resolve( "org/apache/test/samplejar/1.0" )) );
1407 assertTrue( Files.exists(repoRoot.resolve( "org/apache/test/sub/samplejar/1.0" )) );
1408 assertTrue( Files.exists(repoRoot.resolve( "org/apache/test/sub/samplejar/2.0" )) );
1412 public void deleteArtifactItem() throws IOException, URISyntaxException, ItemNotFoundException
1414 ManagedRepository repo = createManagedRepoWithContent( "delete-repository" );
1415 ManagedRepositoryContent myRepoContent = repo.getContent( );
1416 Path repoRoot = repo.getAsset( "" ).getFilePath( );
1417 assertTrue( Files.exists(repoRoot.resolve( "org/apache/maven/A/1.0/A-1.0.pom" )) );
1418 assertTrue( Files.exists(repoRoot.resolve( "org/apache/maven/A/1.0/A-1.0.war" )) );
1419 ArchivaItemSelector selector = ArchivaItemSelector.builder( )
1420 .withNamespace( "org.apache.maven" )
1421 .withProjectId( "A" )
1422 .withVersion( "1.0" )
1423 .withArtifactId( "A" )
1424 .withArtifactVersion( "1.0" )
1425 .withExtension( "pom" )
1427 ContentItem item = myRepoContent.getItem( selector );
1428 assertTrue( item instanceof Artifact );
1429 myRepoContent.deleteItem( item );
1430 assertTrue( Files.exists( repoRoot.resolve( "org/apache/maven/samplejar/1.0" ) ) );
1431 assertTrue( Files.exists( repoRoot.resolve( "org/apache/maven/samplejar/2.0" ) ) );
1432 assertFalse( Files.exists(repoRoot.resolve( "org/apache/maven/A/1.0/A-1.0.pom" )) );
1433 assertTrue( Files.exists(repoRoot.resolve( "org/apache/maven/A/1.0/A-1.0.war" )) );
1436 assertTrue( Files.exists(repoRoot.resolve( "org/apache/test/samplejar/1.0/samplejar-1.0.jar" )) );
1437 assertTrue( Files.exists(repoRoot.resolve( "org/apache/test/samplejar/1.0/samplejar-1.0.jar.md5" )) );
1438 assertTrue( Files.exists(repoRoot.resolve( "org/apache/test/samplejar/1.0/samplejar-1.0.jar.sha1" )) );
1439 assertTrue( Files.exists(repoRoot.resolve( "org/apache/test/samplejar/1.0/samplejar-1.0.pom" )) );
1440 assertTrue( Files.exists(repoRoot.resolve( "org/apache/test/samplejar/1.0/samplejar-1.0-source.jar" )) );
1441 selector = ArchivaItemSelector.builder( )
1442 .withNamespace( "org.apache.test" )
1443 .withProjectId( "samplejar" )
1444 .withVersion( "1.0" )
1445 .withArtifactId( "samplejar" )
1446 .withArtifactVersion( "1.0" )
1447 .withExtension( "jar" )
1449 item = myRepoContent.getItem( selector );
1450 assertTrue( item instanceof Artifact );
1451 myRepoContent.deleteItem( item );
1452 assertFalse( Files.exists(repoRoot.resolve( "org/apache/test/samplejar/1.0/samplejar-1.0.jar" )) );
1453 assertTrue( Files.exists(repoRoot.resolve( "org/apache/test/samplejar/1.0/samplejar-1.0.jar.md5" )) );
1454 assertTrue( Files.exists(repoRoot.resolve( "org/apache/test/samplejar/1.0/samplejar-1.0.jar.sha1" )) );
1455 assertTrue( Files.exists(repoRoot.resolve( "org/apache/test/samplejar/1.0/samplejar-1.0.pom" )) );
1456 assertTrue( Files.exists(repoRoot.resolve( "org/apache/test/samplejar/1.0/samplejar-1.0-source.jar" )) );
1457 assertTrue( Files.exists(repoRoot.resolve( "org/apache/test/samplejar/2.0" )) );
1458 assertTrue( Files.exists(repoRoot.resolve( "org/apache/test/sub/samplejar/1.0" )) );
1459 assertTrue( Files.exists(repoRoot.resolve( "org/apache/test/sub/samplejar/2.0" )) );
1461 selector = ArchivaItemSelector.builder( )
1462 .withNamespace( "org.apache.test" )
1463 .withProjectId( "samplejar" )
1464 .withVersion( "1.0" )
1465 .withArtifactId( "samplejar" )
1466 .withArtifactVersion( "1.0" )
1467 .withClassifier( "source" )
1468 .withExtension( "jar" )
1470 item = myRepoContent.getItem( selector );
1471 assertTrue( item instanceof Artifact );
1472 myRepoContent.deleteItem( item );
1473 assertFalse( Files.exists(repoRoot.resolve( "org/apache/test/samplejar/1.0/samplejar-1.0.jar" )) );
1474 assertTrue( Files.exists(repoRoot.resolve( "org/apache/test/samplejar/1.0/samplejar-1.0.jar.md5" )) );
1475 assertTrue( Files.exists(repoRoot.resolve( "org/apache/test/samplejar/1.0/samplejar-1.0.jar.sha1" )) );
1476 assertTrue( Files.exists(repoRoot.resolve( "org/apache/test/samplejar/1.0/samplejar-1.0.pom" )) );
1477 assertFalse( Files.exists(repoRoot.resolve( "org/apache/test/samplejar/1.0/samplejar-1.0-source.jar" )) );
1478 assertTrue( Files.exists(repoRoot.resolve( "org/apache/test/samplejar/1.0/samplejar-1.0-source.jar.sha1" )) );
1479 assertTrue( Files.exists(repoRoot.resolve( "org/apache/test/samplejar/2.0" )) );
1480 assertTrue( Files.exists(repoRoot.resolve( "org/apache/test/sub/samplejar/1.0" )) );
1481 assertTrue( Files.exists(repoRoot.resolve( "org/apache/test/sub/samplejar/2.0" )) );
1483 selector = ArchivaItemSelector.builder( )
1484 .withNamespace( "org.apache.test" )
1485 .withProjectId( "samplejar" )
1486 .withVersion( "1.0" )
1487 .withArtifactId( "samplejar" )
1488 .withArtifactVersion( "1.0" )
1489 .withExtension( "jar.md5" )
1491 item = myRepoContent.getItem( selector );
1492 assertTrue( item instanceof Artifact );
1493 myRepoContent.deleteItem( item );
1494 assertFalse( Files.exists(repoRoot.resolve( "org/apache/test/samplejar/1.0/samplejar-1.0.jar" )) );
1495 assertFalse( Files.exists(repoRoot.resolve( "org/apache/test/samplejar/1.0/samplejar-1.0.jar.md5" )) );
1496 assertTrue( Files.exists(repoRoot.resolve( "org/apache/test/samplejar/1.0/samplejar-1.0.jar.sha1" )) );
1497 assertTrue( Files.exists(repoRoot.resolve( "org/apache/test/samplejar/1.0/samplejar-1.0.pom" )) );
1498 assertFalse( Files.exists(repoRoot.resolve( "org/apache/test/samplejar/1.0/samplejar-1.0-source.jar" )) );
1499 assertTrue( Files.exists(repoRoot.resolve( "org/apache/test/samplejar/1.0/samplejar-1.0-source.jar.sha1" )) );
1500 assertTrue( Files.exists(repoRoot.resolve( "org/apache/test/samplejar/2.0" )) );
1501 assertTrue( Files.exists(repoRoot.resolve( "org/apache/test/sub/samplejar/1.0" )) );
1502 assertTrue( Files.exists(repoRoot.resolve( "org/apache/test/sub/samplejar/2.0" )) );
1508 public void deleteItemNotFound() throws IOException, URISyntaxException, ItemNotFoundException
1510 ManagedRepository repo = createManagedRepoWithContent( "delete-repository" );
1511 ManagedRepositoryContent myRepoContent = repo.getContent( );
1512 Path repoRoot = repo.getAsset( "" ).getFilePath( );
1514 ArchivaItemSelector selector = ArchivaItemSelector.builder( )
1515 .withNamespace( "org.apache.test2" )
1518 ContentItem item = myRepoContent.getItem( selector );
1519 assertTrue( item instanceof Namespace );
1522 myRepoContent.deleteItem( item );
1523 assertTrue( "ItemNotFoundException expected for non existing namespace", false );
1524 } catch ( ItemNotFoundException e) {
1527 selector = ArchivaItemSelector.builder( )
1528 .withNamespace( "org.apache.test" )
1529 .withProjectId( "samplejar2" )
1531 item = myRepoContent.getItem( selector );
1532 assertTrue( item instanceof Project );
1535 myRepoContent.deleteItem( item );
1536 assertTrue( "ItemNotFoundException expected for non existing project", false );
1537 } catch ( ItemNotFoundException e) {
1540 selector = ArchivaItemSelector.builder( )
1541 .withNamespace( "org.apache.test" )
1542 .withProjectId( "samplejar" )
1545 item = myRepoContent.getItem( selector );
1546 assertTrue( item instanceof Version );
1549 myRepoContent.deleteItem( item );
1550 assertTrue( "ItemNotFoundException expected for non existing version", false );
1551 } catch ( ItemNotFoundException e) {
1554 selector = ArchivaItemSelector.builder( )
1555 .withNamespace( "org.apache.test" )
1556 .withProjectId( "samplejar" )
1558 .withArtifactId( "samplejar" )
1559 .withArtifactVersion( "1.0" )
1560 .withExtension( "jax" )
1562 item = myRepoContent.getItem( selector );
1563 assertTrue( item instanceof Artifact );
1566 myRepoContent.deleteItem( item );
1567 assertTrue( "ItemNotFoundException expected for non existing artifact", false );
1568 } catch ( ItemNotFoundException e) {
1575 public void testCopyArtifact() throws IOException, URISyntaxException
1577 ManagedRepository repo = createManagedRepoWithContent( "delete-repository" );
1578 ManagedRepositoryContent myRepoContent = repo.getContent( );
1579 Path repoRoot = repo.getAsset( "" ).getFilePath( );
1581 Path tmpFile = Files.createTempFile( "archiva-mvn-repotest", "jar" );
1582 try( OutputStream outputStream = Files.newOutputStream( tmpFile ))
1584 for ( int i = 0; i < 255; i++ )
1586 outputStream.write( "test.test.test\n".getBytes( Charset.forName( "UTF-8" ) ) );
1590 Path file = repoRoot.resolve( "org/apache/maven/samplejar/2.0/samplejar-2.0.jar" );
1591 FileTime lmt = Files.getLastModifiedTime( file );
1592 ArchivaItemSelector selector = ArchivaItemSelector.builder( )
1593 .withNamespace( "org.apache.maven" )
1594 .withProjectId( "samplejar" )
1595 .withVersion( "2.0" )
1596 .withArtifactId( "samplejar" )
1597 .withArtifactVersion( "2.0" )
1598 .withExtension( "jar" )
1600 Artifact artifact = myRepoContent.getArtifact( selector );
1601 myRepoContent.addArtifact( tmpFile, artifact );
1602 FileTime lmtAfter = Files.getLastModifiedTime( file );
1603 assertNotEquals( lmtAfter, lmt );
1604 Reader ln = Files.newBufferedReader( file, Charset.forName( "UTF-8" ) );
1605 char[] content = new char[50];
1607 assertTrue( new String( content ).startsWith( "test.test.test" ) );
1609 tmpFile = Files.createTempFile( "archiva-mvn-repotest", "jar" );
1610 try( OutputStream outputStream = Files.newOutputStream( tmpFile ))
1612 for ( int i = 0; i < 255; i++ )
1614 outputStream.write( "test.test.test\n".getBytes( Charset.forName( "UTF-8" ) ) );
1617 file = repoRoot.resolve( "org/apache/maven/samplejar/2.0/samplejar-2.0.test" );
1618 assertFalse( Files.exists( file ) );
1619 assertTrue( Files.exists( tmpFile ) );
1620 selector = ArchivaItemSelector.builder( )
1621 .withNamespace( "org.apache.maven" )
1622 .withProjectId( "samplejar" )
1623 .withVersion( "2.0" )
1624 .withArtifactId( "samplejar" )
1625 .withArtifactVersion( "2.0" )
1626 .withExtension( "test" )
1628 artifact = myRepoContent.getArtifact( selector );
1629 myRepoContent.addArtifact( tmpFile, artifact );
1630 ln = Files.newBufferedReader( file, Charset.forName( "UTF-8" ) );
1632 assertTrue( new String( content ).startsWith( "test.test.test" ) );