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);
444 public void testDeleteArtifactWithType() throws IOException, org.apache.archiva.repository.ContentNotFoundException, org.apache.archiva.repository.ContentAccessException
446 Path deleteRepo = setupRepoCopy( "delete-repository", "delete-repository-2" );
447 assertTrue( Files.exists( deleteRepo.resolve( "org/apache/maven/samplejar/1.0/samplejar-1.0-source.jar" ) ) );
448 assertTrue( Files.exists( deleteRepo.resolve( "org/apache/maven/samplejar/1.0/samplejar-1.0.jar" ) ) );
449 assertTrue( Files.exists( deleteRepo.resolve( "org/apache/maven/samplejar/1.0/samplejar-1.0.jar.md5" ) ) );
450 assertTrue( Files.exists( deleteRepo.resolve( "org/apache/maven/samplejar/1.0/samplejar-1.0.jar.sha1" ) ) );
451 assertTrue( Files.exists( deleteRepo.resolve( "org/apache/maven/samplejar/1.0/samplejar-1.0.pom" ) ) );
453 ArtifactReference ref = new ArtifactReference( );
454 ref.setGroupId( "org.apache.maven" );
455 ref.setArtifactId( "samplejar" );
456 ref.setVersion( "1.0" );
457 ref.setType( "jar" );
459 repoContent.deleteArtifact( ref );
461 assertTrue( Files.exists( deleteRepo.resolve( "org/apache/maven/samplejar/1.0/samplejar-1.0-source.jar" ) ) );
462 assertFalse( Files.exists( deleteRepo.resolve( "org/apache/maven/samplejar/1.0/samplejar-1.0.jar" ) ) );
463 assertTrue( Files.exists( deleteRepo.resolve( "org/apache/maven/samplejar/1.0/samplejar-1.0.jar.md5" ) ) );
464 assertTrue( Files.exists( deleteRepo.resolve( "org/apache/maven/samplejar/1.0/samplejar-1.0.jar.sha1" ) ) );
465 assertTrue( Files.exists( deleteRepo.resolve( "org/apache/maven/samplejar/1.0/samplejar-1.0.pom" ) ) );
472 public void testDeleteArtifactWithClassifier() throws IOException, org.apache.archiva.repository.ContentNotFoundException, org.apache.archiva.repository.ContentAccessException
474 Path deleteRepo = setupRepoCopy( "default-repository", "default-repository-2" );
475 assertTrue( Files.exists( deleteRepo.resolve( "org/apache/maven/samplejar/1.0/samplejar-1.0-source.jar" ) ) );
476 assertTrue( Files.exists( deleteRepo.resolve( "org/apache/maven/samplejar/1.0/samplejar-1.0-source.jar.sha1" ) ) );
477 assertTrue( Files.exists( deleteRepo.resolve( "org/apache/maven/samplejar/1.0/samplejar-1.0.jar" ) ) );
478 assertTrue( Files.exists( deleteRepo.resolve( "org/apache/maven/samplejar/1.0/samplejar-1.0.jar.md5" ) ) );
479 assertTrue( Files.exists( deleteRepo.resolve( "org/apache/maven/samplejar/1.0/samplejar-1.0.jar.sha1" ) ) );
480 assertTrue( Files.exists( deleteRepo.resolve( "org/apache/maven/samplejar/1.0/samplejar-1.0.pom" ) ) );
482 ArtifactReference ref = new ArtifactReference( );
483 ref.setGroupId( "org.apache.maven" );
484 ref.setArtifactId( "samplejar" );
485 ref.setVersion( "1.0" );
486 ref.setClassifier( "source" );
487 ref.setType( "jar" );
489 repoContent.deleteArtifact( ref );
491 assertFalse( Files.exists( deleteRepo.resolve( "org/apache/maven/samplejar/1.0/samplejar-1.0-source.jar" ) ) );
492 assertTrue( Files.exists( deleteRepo.resolve( "org/apache/maven/samplejar/1.0/samplejar-1.0-source.jar.sha1" ) ) );
493 assertTrue( Files.exists( deleteRepo.resolve( "org/apache/maven/samplejar/1.0/samplejar-1.0.jar" ) ) );
494 assertTrue( Files.exists( deleteRepo.resolve( "org/apache/maven/samplejar/1.0/samplejar-1.0.jar.md5" ) ) );
495 assertTrue( Files.exists( deleteRepo.resolve( "org/apache/maven/samplejar/1.0/samplejar-1.0.jar.sha1" ) ) );
496 assertTrue( Files.exists( deleteRepo.resolve( "org/apache/maven/samplejar/1.0/samplejar-1.0.pom" ) ) );
501 public void testDeleteArtifactWithoutType() throws IOException, org.apache.archiva.repository.ContentNotFoundException, org.apache.archiva.repository.ContentAccessException
503 Path deleteRepo = setupRepoCopy( "default-repository", "default-repository-2" );
504 assertTrue( Files.exists( deleteRepo.resolve( "org/apache/maven/samplejar/1.0/samplejar-1.0-source.jar" ) ) );
505 assertTrue( Files.exists( deleteRepo.resolve( "org/apache/maven/samplejar/1.0/samplejar-1.0-source.jar.sha1" ) ) );
506 assertTrue( Files.exists( deleteRepo.resolve( "org/apache/maven/samplejar/1.0/samplejar-1.0.jar" ) ) );
507 assertTrue( Files.exists( deleteRepo.resolve( "org/apache/maven/samplejar/1.0/samplejar-1.0.jar.md5" ) ) );
508 assertTrue( Files.exists( deleteRepo.resolve( "org/apache/maven/samplejar/1.0/samplejar-1.0.jar.sha1" ) ) );
509 assertTrue( Files.exists( deleteRepo.resolve( "org/apache/maven/samplejar/1.0/samplejar-1.0.pom" ) ) );
511 ArtifactReference ref = new ArtifactReference( );
512 ref.setGroupId( "org.apache.maven" );
513 ref.setArtifactId( "samplejar" );
514 ref.setVersion( "1.0" );
516 repoContent.deleteArtifact( ref );
518 assertFalse( Files.exists( deleteRepo.resolve( "org/apache/maven/samplejar/1.0" ) ) );
519 assertTrue( Files.exists( deleteRepo.resolve( "org/apache/maven/samplejar" ) ) );
525 public void testDeleteVersion() throws IOException, org.apache.archiva.repository.ContentNotFoundException, org.apache.archiva.repository.ContentAccessException
527 Path deleteRepo = setupRepoCopy( "delete-repository", "delete-repository-2" );
528 assertTrue( Files.exists( deleteRepo.resolve( "org/apache/maven/samplejar/1.0/samplejar-1.0-source.jar" ) ) );
529 assertTrue( Files.exists( deleteRepo.resolve( "org/apache/maven/samplejar/1.0/samplejar-1.0.jar" ) ) );
530 assertTrue( Files.exists( deleteRepo.resolve( "org/apache/maven/samplejar/1.0/samplejar-1.0.jar.md5" ) ) );
531 assertTrue( Files.exists( deleteRepo.resolve( "org/apache/maven/samplejar/1.0/samplejar-1.0.jar.sha1" ) ) );
532 assertTrue( Files.exists( deleteRepo.resolve( "org/apache/maven/samplejar/1.0/samplejar-1.0.pom" ) ) );
534 VersionedReference ref = new VersionedReference( ).groupId( "org.apache.maven" ).artifactId( "samplejar" ).version( "1.0" );
536 repoContent.deleteVersion( ref );
538 assertFalse( Files.exists( deleteRepo.resolve( "org/apache/maven/samplejar/1.0" ) ) );
543 public void testDeleteProject() throws IOException, org.apache.archiva.repository.ContentNotFoundException, org.apache.archiva.repository.ContentAccessException
545 Path deleteRepo = setupRepoCopy( "delete-repository", "delete-repository-2" );
546 assertTrue( Files.exists( deleteRepo.resolve( "org/apache/maven/samplejar/1.0/samplejar-1.0-source.jar" ) ) );
547 assertTrue( Files.exists( deleteRepo.resolve( "org/apache/maven/samplejar/1.0/samplejar-1.0.jar" ) ) );
548 assertTrue( Files.exists( deleteRepo.resolve( "org/apache/maven/samplejar/1.0/samplejar-1.0.jar.md5" ) ) );
549 assertTrue( Files.exists( deleteRepo.resolve( "org/apache/maven/samplejar/1.0/samplejar-1.0.jar.sha1" ) ) );
550 assertTrue( Files.exists( deleteRepo.resolve( "org/apache/maven/samplejar/1.0/samplejar-1.0.pom" ) ) );
552 ProjectReference ref = new ProjectReference( ).groupId( "org.apache.maven" ).artifactId( "samplejar" );
554 repoContent.deleteProject( ref );
556 assertFalse( Files.exists( deleteRepo.resolve( "org/apache/maven/samplejar/1.0" ) ) );
561 public void testGetArtifactStreamWithVersionSelector() {
562 ItemSelector selector = ArchivaItemSelector.builder( )
563 .withNamespace( "javax.sql" )
564 .withProjectId( "jdbc" )
565 .withVersion( "2.0" ).build();
566 try(Stream<? extends Artifact> stream = repoContent.newArtifactStream( selector ))
568 assertNotNull( stream );
569 List<? extends Artifact> results = stream.collect( Collectors.toList( ) );
570 checkArtifactListWithVersionSelector1( results );
575 public void testGetArtifactListWithVersionSelector() {
576 ItemSelector selector = ArchivaItemSelector.builder( )
577 .withNamespace( "javax.sql" )
578 .withProjectId( "jdbc" )
579 .withVersion( "2.0" ).build();
580 List<? extends Artifact> results = repoContent.getArtifacts( selector );
581 checkArtifactListWithVersionSelector1( results );
584 private void checkArtifactListWithVersionSelector1( List<? extends Artifact> results )
586 assertNotNull( results );
587 assertEquals( 2, results.size( ) );
588 Artifact mainArtifact = results.stream( ).filter( a -> a.getFileName( ).equals( "jdbc-2.0.jar" ) ).findFirst( ).get( );
589 assertNotNull( mainArtifact );
590 assertEquals( BaseArtifactTypes.MAIN, mainArtifact.getDataType( ) );
591 Artifact metaArtifact = results.stream( ).filter( a -> a.getFileName( ).equals( "maven-metadata-repository.xml" ) ).findFirst( ).get( );
592 assertNotNull( metaArtifact );
593 assertEquals( MavenTypes.REPOSITORY_METADATA, metaArtifact.getDataType( ) );
597 public void testGetArtifactStreamWithVersionSelector2() {
598 ItemSelector selector = ArchivaItemSelector.builder( )
599 .withNamespace( "org.apache.axis2" )
600 .withProjectId( "axis2" )
601 .withVersion( "1.3-SNAPSHOT" ).build();
602 try(Stream<? extends Artifact> stream = repoContent.newArtifactStream( selector ))
604 assertNotNull( stream );
605 List<? extends Artifact> results = stream.collect( Collectors.toList( ) );
606 checkArtifactListWithVersionSelector2( results );
611 public void testGetArtifactListWithVersionSelector2() {
612 ItemSelector selector = ArchivaItemSelector.builder( )
613 .withNamespace( "org.apache.axis2" )
614 .withProjectId( "axis2" )
615 .withVersion( "1.3-SNAPSHOT" ).build();
616 List<? extends Artifact> results = repoContent.getArtifacts( selector );
617 checkArtifactListWithVersionSelector2( results );
620 private void checkArtifactListWithVersionSelector2( List<? extends Artifact> results )
622 assertNotNull( results );
623 assertEquals( 39, results.size( ) );
625 Artifact artifact = results.stream( ).filter( a -> a.getFileName( ).equals( "axis2-1.3-20070725.210059-1.pom" ) )
626 .findFirst( ).get( );
628 assertNotNull( artifact );
629 assertEquals( "pom", artifact.getExtension( ) );
630 assertEquals( BaseArtifactTypes.MAIN, artifact.getDataType( ) );
631 assertEquals( "1.3-SNAPSHOT", artifact.getVersion( ).getVersion( ) );
632 assertEquals( "1.3-20070725.210059-1", artifact.getArtifactVersion( ) );
633 assertEquals( ".pom", artifact.getRemainder( ) );
634 assertEquals( "axis2", artifact.getId( ) );
635 assertEquals( "axis2", artifact.getVersion( ).getProject( ).getId( ) );
636 assertEquals( "org.apache.axis2", artifact.getVersion( ).getProject( ).getNamespace( ).getNamespace( ) );
637 assertEquals( "", artifact.getClassifier( ) );
638 assertEquals( "pom", artifact.getType( ) );
641 artifact = results.stream( ).filter( a -> a.getFileName( ).equals( "axis2-1.3-20070725.210059-1.pom.md5" ) )
642 .findFirst( ).get( );
644 assertNotNull( artifact );
645 assertEquals( "md5", artifact.getExtension( ) );
646 assertEquals( BaseArtifactTypes.RELATED, artifact.getDataType( ) );
647 assertEquals( "1.3-SNAPSHOT", artifact.getVersion( ).getVersion( ) );
648 assertEquals( "1.3-20070725.210059-1", artifact.getArtifactVersion( ) );
649 assertEquals( ".pom.md5", artifact.getRemainder( ) );
650 assertEquals( "axis2", artifact.getId( ) );
651 assertEquals( "axis2", artifact.getVersion( ).getProject( ).getId( ) );
652 assertEquals( "org.apache.axis2", artifact.getVersion( ).getProject( ).getNamespace( ).getNamespace( ) );
653 assertEquals( "", artifact.getClassifier( ) );
654 assertEquals( "md5", artifact.getType( ) );
658 artifact = results.stream( ).filter( a -> a.getFileName( ).equals( "maven-metadata.xml" ) )
659 .findFirst( ).get( );
660 assertNotNull( artifact );
661 assertEquals( BaseArtifactTypes.METADATA, artifact.getDataType( ) );
662 assertEquals( "1.3-SNAPSHOT", artifact.getVersion( ).getVersion( ) );
663 assertEquals( "xml", artifact.getExtension( ) );
667 public void testGetArtifactListWithArtifactSelector1() {
668 ItemSelector selector = ArchivaItemSelector.builder( )
669 .withNamespace( "org.apache.axis2" )
670 .withProjectId( "axis2" )
671 .withVersion( "1.3-SNAPSHOT" )
672 .withArtifactVersion( "1.3-20070731.113304-21" )
673 .withExtension( "pom" )
675 List<? extends Artifact> results = repoContent.getArtifacts( selector );
676 checkArtifactListWithArtifactSelector1( results );
680 public void testGetArtifactStreamWithArtifactSelector1() {
681 ItemSelector selector = ArchivaItemSelector.builder( )
682 .withNamespace( "org.apache.axis2" )
683 .withProjectId( "axis2" )
684 .withVersion( "1.3-SNAPSHOT" )
685 .withArtifactVersion( "1.3-20070731.113304-21" )
686 .withExtension( "pom" )
688 try(Stream<? extends Artifact> results = repoContent.newArtifactStream( selector ))
690 checkArtifactListWithArtifactSelector1( results.collect( Collectors.toList()) );
694 private void checkArtifactListWithArtifactSelector1( List<? extends Artifact> results )
696 assertNotNull( results );
697 assertEquals( 1, results.size( ) );
698 Artifact artifact = results.get( 0 );
699 assertEquals( "pom", artifact.getExtension( ) );
700 assertEquals( BaseArtifactTypes.MAIN, artifact.getDataType( ) );
704 public void testGetArtifactListWithArtifactSelector2() {
705 ItemSelector selector = ArchivaItemSelector.builder( )
706 .withNamespace( "org.apache.axis2" )
707 .withProjectId( "axis2" )
708 .withVersion( "1.3-SNAPSHOT" )
709 .withArtifactVersion( "1.3-20070731.113304-21" )
710 .withExtension( "pom" )
711 .includeRelatedArtifacts()
713 List<? extends Artifact> results = repoContent.getArtifacts( selector );
715 checkArtifactListWithArtifactSelector2( results );
720 public void testGetArtifactStreamWithArtifactSelector2() {
721 ItemSelector selector = ArchivaItemSelector.builder( )
722 .withNamespace( "org.apache.axis2" )
723 .withProjectId( "axis2" )
724 .withVersion( "1.3-SNAPSHOT" )
725 .withArtifactVersion( "1.3-20070731.113304-21" )
726 .withExtension( "pom" )
727 .includeRelatedArtifacts()
729 try(Stream<? extends Artifact> results = repoContent.newArtifactStream( selector ))
731 checkArtifactListWithArtifactSelector2( results.collect( Collectors.toList()) );
735 private void checkArtifactListWithArtifactSelector2( List<? extends Artifact> results )
737 assertNotNull( results );
738 assertEquals( 3, results.size( ) );
739 Artifact artifact = results.stream( ).filter( a -> a.getFileName( ).equalsIgnoreCase( "axis2-1.3-20070731.113304-21.pom" ) )
740 .findFirst( ).get( );
741 assertNotNull( artifact );
742 assertEquals( "pom", artifact.getExtension( ) );
743 assertEquals( BaseArtifactTypes.MAIN, artifact.getDataType( ) );
745 artifact = results.stream( ).filter( a -> a.getFileName( ).equalsIgnoreCase( "axis2-1.3-20070731.113304-21.pom.sha1" ) )
746 .findFirst( ).get( );
747 assertNotNull( artifact );
748 assertEquals( "sha1", artifact.getExtension( ) );
749 assertEquals( BaseArtifactTypes.RELATED, artifact.getDataType( ) );
754 public void testArtifactListWithProjectSelector() {
755 ItemSelector selector = ArchivaItemSelector.builder( )
756 .withNamespace( "org.apache.maven.shared" )
757 .withProjectId( "maven-downloader" )
759 List<? extends Artifact> results = repoContent.getArtifacts( selector );
760 checkArtifactListWithProjectSelector( results );
765 public void testArtifactStreamWithProjectSelector() {
766 ItemSelector selector = ArchivaItemSelector.builder( )
767 .withNamespace( "org.apache.maven.shared" )
768 .withProjectId( "maven-downloader" )
770 Stream<? extends Artifact> results = repoContent.newArtifactStream( selector );
771 checkArtifactListWithProjectSelector( results.collect( Collectors.toList()) );
775 private void checkArtifactListWithProjectSelector( List<? extends Artifact> results )
777 assertNotNull( results );
778 assertEquals( 27, results.size( ) );
780 Artifact artifact = results.stream( ).filter( a -> a.getFileName( ).equalsIgnoreCase( "maven-metadata.xml" ) )
781 .findFirst( ).get( );
782 assertNotNull( artifact );
783 assertEquals( "xml", artifact.getExtension( ) );
784 assertEquals( BaseArtifactTypes.METADATA, artifact.getDataType( ) );
786 artifact = results.stream( ).filter( a -> a.getFileName( ).equalsIgnoreCase( "maven-downloader-1.0-sources.jar" ) )
787 .findFirst( ).get( );
788 assertNotNull( artifact );
789 assertEquals( BaseArtifactTypes.MAIN, artifact.getDataType( ) );
790 assertEquals( "sources", artifact.getClassifier( ) );
791 assertEquals( "java-source", artifact.getType( ) );
793 artifact = results.stream( ).filter( a -> a.getFileName( ).equalsIgnoreCase( "maven-downloader-1.0-sources.jar.sha1" ) )
794 .findFirst( ).get( );
795 assertNotNull( artifact );
796 assertEquals( BaseArtifactTypes.RELATED, artifact.getDataType( ) );
797 assertEquals( "sources", artifact.getClassifier( ) );
798 assertEquals( "sha1", artifact.getType( ) );
799 assertEquals( ".jar.sha1", artifact.getRemainder( ) );
803 public void testArtifactListWithNamespaceSelector() {
804 ItemSelector selector = ArchivaItemSelector.builder( )
805 .withNamespace( "org.multilevel" )
807 List<? extends Artifact> results = repoContent.getArtifacts( selector );
808 assertNotNull( results );
809 assertEquals( 3, results.size( ) );
810 assertTrue( results.get( 0 ).getFileName( ).startsWith( "testproj1" ) );
814 public void testArtifactListWithNamespaceSelectorRecursive() {
815 ItemSelector selector = ArchivaItemSelector.builder( )
816 .withNamespace( "org.multilevel" )
819 List<? extends Artifact> results = repoContent.getArtifacts( selector );
820 checkArtifactListWithNamespaceSelectorRecursive( results );
824 public void testArtifactStreamWithNamespaceSelectorRecursive() {
825 ItemSelector selector = ArchivaItemSelector.builder( )
826 .withNamespace( "org.multilevel" )
829 Stream<? extends Artifact> results = repoContent.newArtifactStream( selector );
830 checkArtifactListWithNamespaceSelectorRecursive( results.collect( Collectors.toList()) );
833 private void checkArtifactListWithNamespaceSelectorRecursive( List<? extends Artifact> results )
835 assertNotNull( results );
836 assertEquals( 6, results.size( ) );
838 Artifact artifact = results.stream( ).filter( a -> a.getFileName( ).equalsIgnoreCase( "testproj2-1.0.pom" ) )
839 .findFirst( ).get( );
840 assertNotNull( artifact );
841 assertEquals( 6, artifact.getAsset( ).getParent( ).getPath( ).split( "/" ).length );
843 artifact = results.stream( ).filter( a -> a.getFileName( ).equalsIgnoreCase( "testproj1-1.0.pom" ) )
844 .findFirst( ).get( );
845 assertNotNull( artifact );
846 assertEquals( 5, artifact.getAsset( ).getParent( ).getPath( ).split( "/" ).length );
851 public void testArtifactListWithArtifactSelector1() {
852 ItemSelector selector = ArchivaItemSelector.builder( )
853 .withNamespace( "org.apache.maven" )
854 .withProjectId( "test" )
855 .withVersion( "1.0-SNAPSHOT" )
856 .withArtifactId( "test" )
857 .withArtifactVersion( "1.0-20050611.112233-1" )
860 List<? extends Artifact> results = repoContent.getArtifacts( selector );
862 assertNotNull( results );
863 assertEquals( 1, results.size( ) );
865 Artifact artifact = results.stream( ).filter( a -> a.getFileName( ).equalsIgnoreCase( "test-1.0-20050611.112233-1.jar" ) )
867 assertNotNull( artifact );
868 assertEquals( "", artifact.getClassifier( ) );
872 public void testArtifactListWithArtifactSelector2() {
873 ItemSelector selector = ArchivaItemSelector.builder( )
874 .withNamespace( "org.apache.maven" )
875 .withProjectId( "test" )
876 .withVersion( "1.0-SNAPSHOT" )
877 .withClassifier( "*" )
878 .withArtifactId( "test" )
879 .withArtifactVersion( "1.0-20050611.112233-1" )
882 List<? extends Artifact> results = repoContent.getArtifacts( selector );
884 assertNotNull( results );
885 assertEquals( 2, results.size( ) );
887 Artifact artifact = results.stream( ).filter( a -> a.getFileName( ).equalsIgnoreCase( "test-1.0-20050611.112233-1-javadoc.jar" ) )
889 assertNotNull( artifact );
890 assertEquals( "javadoc", artifact.getClassifier( ) );
891 assertEquals( "javadoc", artifact.getType( ) );
895 public void testArtifactListWithArtifactSelector3() {
896 ItemSelector selector = ArchivaItemSelector.builder( )
897 .withNamespace( "org.apache.maven" )
898 .withProjectId( "test" )
899 .withVersion( "1.0-SNAPSHOT" )
900 .withClassifier( "*" )
901 .withArtifactVersion( "1.0-20050611.112233-1" )
904 List<? extends Artifact> results = repoContent.getArtifacts( selector );
906 assertNotNull( results );
907 assertEquals( 3, results.size( ) );
909 Artifact artifact = results.stream( ).filter( a -> a.getFileName( ).equalsIgnoreCase( "test-1.0-20050611.112233-1-javadoc.jar" ) )
911 assertNotNull( artifact );
912 assertEquals( "javadoc", artifact.getClassifier( ) );
913 assertEquals( "javadoc", artifact.getType( ) );
915 artifact = results.stream( ).filter( a -> a.getFileName( ).equalsIgnoreCase( "wrong-artifactId-1.0-20050611.112233-1.jar" ) )
917 assertNotNull( artifact );
918 assertEquals( "", artifact.getClassifier( ) );
919 assertEquals( "wrong-artifactId", artifact.getId( ) );
923 public void testArtifactListWithArtifactSelector4() {
924 ItemSelector selector = ArchivaItemSelector.builder( )
925 .withNamespace( "org.apache.maven" )
926 .withProjectId( "test" )
927 .withVersion( "1.0-SNAPSHOT" )
928 .withClassifier( "" )
931 List<? extends Artifact> results = repoContent.getArtifacts( selector );
933 assertNotNull( results );
934 assertEquals( 5, results.size( ) );
936 Artifact artifact = results.stream( ).filter( a -> a.getFileName( ).equalsIgnoreCase( "test-1.0-20050611.112233-1-javadoc.jar" ) )
938 assertNotNull( artifact );
939 assertEquals( "javadoc", artifact.getClassifier( ) );
940 assertEquals( "javadoc", artifact.getType( ) );
942 artifact = results.stream( ).filter( a -> a.getFileName( ).equalsIgnoreCase( "wrong-artifactId-1.0-20050611.112233-1.jar" ) )
944 assertNotNull( artifact );
945 assertEquals( "", artifact.getClassifier( ) );
946 assertEquals( "wrong-artifactId", artifact.getId( ) );
948 artifact = results.stream( ).filter( a -> a.getFileName( ).equalsIgnoreCase( "wrong-artifactId-1.0-20050611.1122x-1.jar" ) )
950 assertNotNull( artifact );
951 assertEquals( "", artifact.getClassifier( ) );
952 assertEquals( "wrong-artifactId", artifact.getId( ) );
953 assertEquals( "", artifact.getArtifactVersion( ) );
955 artifact = results.stream( ).filter( a -> a.getFileName( ).equalsIgnoreCase( "test-1.0-20050611.1122x-1.jar" ) )
957 assertNotNull( artifact );
958 assertEquals( "", artifact.getClassifier( ) );
959 assertEquals( "test", artifact.getId( ) );
960 assertEquals( "1.0-20050611.1122x-1", artifact.getArtifactVersion( ) );
965 public void testArtifactListWithArtifactSelectorWithClassifier() {
966 ItemSelector selector = ArchivaItemSelector.builder( )
967 .withNamespace( "org.apache.maven" )
968 .withProjectId( "test" )
969 .withVersion( "1.0-SNAPSHOT" )
970 .withArtifactId( "test" )
971 .withClassifier( "javadoc" )
972 .withArtifactVersion( "1.0-20050611.112233-1" )
975 List<? extends Artifact> results = repoContent.getArtifacts( selector );
977 assertNotNull( results );
978 assertEquals( 1, results.size( ) );
980 Artifact artifact = results.stream( ).filter( a -> a.getFileName( ).equalsIgnoreCase( "test-1.0-20050611.112233-1-javadoc.jar" ) )
982 assertNotNull( artifact );
983 assertEquals( "javadoc", artifact.getClassifier( ) );
984 assertEquals( "javadoc", artifact.getType( ) );
988 public void testArtifactListWithArtifactSelectorWrongArtifact() {
989 ItemSelector selector = ArchivaItemSelector.builder( )
990 .withNamespace( "org.apache.maven" )
991 .withProjectId( "test" )
992 .withVersion( "1.0-SNAPSHOT" )
993 .withArtifactId( "wrong-artifactId" )
994 .withArtifactVersion( "1.0-20050611.112233-1" )
997 List<? extends Artifact> results = repoContent.getArtifacts( selector );
999 assertNotNull( results );
1000 assertEquals( 1, results.size( ) );
1002 Artifact artifact = results.stream( ).filter( a -> a.getFileName( ).equalsIgnoreCase( "wrong-artifactId-1.0-20050611.112233-1.jar" ) )
1004 assertNotNull( artifact );
1008 public void testArtifactListWithArtifactSelectorVersionPattern() {
1009 ItemSelector selector = ArchivaItemSelector.builder( )
1010 .withNamespace( "org.apache.maven" )
1011 .withProjectId( "test" )
1012 .withVersion( "1.0-SNAPSHOT" )
1013 .withArtifactVersion( "1.0-*" )
1016 List<? extends Artifact> results = repoContent.getArtifacts( selector );
1018 assertNotNull( results );
1019 assertEquals( 5, results.size( ) );
1021 Artifact artifact = results.stream( ).filter( a -> a.getFileName( ).equalsIgnoreCase( "wrong-artifactId-1.0-20050611.112233-1.jar" ) )
1023 assertNotNull( artifact );
1028 public void testNewItemStreamWithNamespace1() {
1029 ItemSelector selector = ArchivaItemSelector.builder( )
1030 .withNamespace( "org.apache.axis2" )
1033 Stream<? extends ContentItem> stream = repoContent.newItemStream( selector, false );
1034 List<? extends ContentItem> result = stream.collect( Collectors.toList( ) );
1035 assertEquals( 41, result.size( ) );
1036 ContentItem item = result.get( 39 );
1037 Version version = item.adapt( Version.class );
1038 assertNotNull( version );
1039 assertEquals( "1.3-SNAPSHOT", version.getVersion( ) );
1040 Project project = result.get( 40 ).adapt( Project.class );
1041 assertNotNull( project );
1042 assertEquals( "axis2", project.getId( ) );
1043 assertTrue( result.stream( ).anyMatch( a -> "axis2-1.3-20070725.210059-1.pom".equals( a.getAsset( ).getName( ) ) ) );
1047 public void testNewItemStreamWithNamespace2() {
1048 ItemSelector selector = ArchivaItemSelector.builder( )
1049 .withNamespace( "org.apache.maven" )
1053 Stream<? extends ContentItem> stream = repoContent.newItemStream( selector, false );
1054 List<? extends ContentItem> result = stream.collect( Collectors.toList( ) );
1055 assertEquals( 170, result.size( ) );
1056 assertEquals( 92, result.stream( ).filter( a -> a instanceof DataItem ).count( ) );
1060 public void testGetArtifactFromContentItem() {
1061 ItemSelector selector = ArchivaItemSelector.builder( )
1062 .withNamespace( "org.apache.maven" ).build();
1063 Namespace ns = repoContent.getNamespace( selector );
1064 List<? extends Artifact> artifacts = repoContent.getArtifacts( ns );
1065 assertNotNull( artifacts );
1066 assertEquals( 39, artifacts.size( ) );
1067 List<? extends Artifact> artifacts2 = repoContent.getArtifacts( (ContentItem)ns );
1068 assertArrayEquals( artifacts.toArray(), artifacts2.toArray() );
1070 selector = ArchivaItemSelector.builder( )
1071 .withNamespace( "org.apache.maven.shared" )
1072 .withProjectId( "maven-downloader" )
1074 Project project = repoContent.getProject( selector );
1075 artifacts = repoContent.getArtifacts( project );
1076 assertNotNull( artifacts );
1077 assertEquals( 27, artifacts.size( ) );
1078 artifacts2 = repoContent.getArtifacts( (ContentItem)project );
1079 assertArrayEquals( artifacts.toArray(), artifacts2.toArray() );
1081 selector = ArchivaItemSelector.builder( )
1082 .withNamespace( "org.apache.maven.shared" )
1083 .withProjectId( "maven-downloader" )
1084 .withVersion( "1.1" )
1086 Version version = repoContent.getVersion( selector );
1087 artifacts = repoContent.getArtifacts( version );
1088 assertNotNull( artifacts );
1089 assertEquals( 12, artifacts.size( ) );
1090 artifacts2 = repoContent.getArtifacts( (ContentItem)version );
1091 assertArrayEquals( artifacts.toArray(), artifacts2.toArray() );
1096 public void testGetRelatedArtifactsFromArtifact() {
1097 ItemSelector selector = ArchivaItemSelector.builder( )
1098 .withNamespace( "org.apache.maven.shared" )
1099 .withProjectId( "maven-downloader" )
1100 .withVersion( "1.1" )
1101 .withExtension( "jar" )
1102 .withArtifactId( "maven-downloader" ).build( );
1104 Artifact artifact = repoContent.getArtifact( selector );
1105 assertNotNull( artifact );
1106 List<? extends Artifact> artifacts = repoContent.getArtifacts( artifact );
1107 assertNotNull( artifacts );
1108 assertEquals( 2, artifacts.size( ) );
1113 public void testToItemFromPath() throws LayoutException
1115 String path = "/org/apache/maven/shared";
1116 ContentItem item = repoContent.toItem( path );
1117 assertNotNull( item );
1118 assertTrue( item instanceof ArchivaContentItem );
1120 path = "/org/apache/maven/shared/maven-downloader";
1121 item = repoContent.toItem( path );
1122 assertNotNull( item );
1123 assertTrue( item instanceof ArchivaContentItem );
1125 path = "/org/apache/maven/shared/maven-downloader/1.1";
1126 item = repoContent.toItem( path );
1127 assertNotNull( item );
1128 assertTrue( item instanceof ArchivaContentItem );
1130 path = "/org/apache/maven/shared/maven-downloader/1.1/maven-downloader-1.1.jar";
1131 item = repoContent.toItem( path );
1132 assertNotNull( item );
1133 assertTrue( item instanceof Artifact );
1138 public void testToItemFromAssetPath() throws LayoutException
1140 StorageAsset path = repoContent.getRepository().getAsset("/org/apache/maven/shared");
1141 ContentItem item = repoContent.toItem( path );
1142 assertNotNull( item );
1143 assertTrue( item instanceof ArchivaContentItem );
1145 path = repoContent.getRepository( ).getAsset( "/org/apache/maven/shared/maven-downloader" );
1146 item = repoContent.toItem( path );
1147 assertNotNull( item );
1148 assertTrue( item instanceof ArchivaContentItem );
1150 path = repoContent.getRepository( ).getAsset( "/org/apache/maven/shared/maven-downloader/1.1" );
1151 item = repoContent.toItem( path );
1152 assertNotNull( item );
1153 assertTrue( item instanceof ArchivaContentItem );
1155 path = repoContent.getRepository( ).getAsset( "/org/apache/maven/shared/maven-downloader/1.1/maven-downloader-1.1.jar" );
1156 item = repoContent.toItem( path );
1157 assertNotNull( item );
1158 assertTrue( item instanceof DataItem );
1163 public void testHasContent() throws LayoutException
1165 ItemSelector selector = ArchivaItemSelector.builder( )
1166 .withNamespace( "org.apache.maven.shared" )
1167 .withProjectId( "maven-downloader" )
1168 .withVersion( "1.1" )
1169 .withArtifactId( "maven-downloader" )
1170 .withExtension( "jar" )
1173 assertTrue( repoContent.hasContent( selector ) );
1175 selector = ArchivaItemSelector.builder( )
1176 .withNamespace( "org.apache.maven.shared" )
1177 .withProjectId( "maven-downloader" )
1178 .withVersion( "1.1" )
1179 .withArtifactId( "maven-downloader" )
1180 .withExtension( "zip" )
1183 assertFalse( repoContent.hasContent( selector ) );
1188 public void testGetItemWithNamespaceSelector() {
1189 ItemSelector selector = ArchivaItemSelector.builder( )
1190 .withNamespace( "org.apache.maven" )
1192 ContentItem item = repoContent.getItem( selector );
1193 assertNotNull( item );
1194 assertTrue( item instanceof Namespace );
1198 public void testGetItemWithProjectSelector() {
1199 ItemSelector selector = ArchivaItemSelector.builder( )
1200 .withNamespace( "org.apache.maven" )
1201 .withProjectId( "shared" )
1203 ContentItem item = repoContent.getItem( selector );
1204 assertNotNull( item );
1205 assertTrue( item instanceof Project );
1209 public void testGetItemWithVersionSelector() {
1210 ItemSelector selector = ArchivaItemSelector.builder( )
1211 .withNamespace( "org.apache.maven" )
1212 .withProjectId( "samplejar" )
1215 ContentItem item = repoContent.getItem( selector );
1216 assertNotNull( item );
1217 assertTrue( item instanceof Version );
1221 public void testGetItemWithArtifactSelector() {
1222 ItemSelector selector = ArchivaItemSelector.builder( )
1223 .withNamespace( "org.apache.maven" )
1224 .withProjectId( "samplejar" )
1226 .withArtifactId( "samplejar" )
1228 ContentItem item = repoContent.getItem( selector );
1229 assertNotNull( item );
1230 assertTrue( item instanceof Artifact );
1234 public void testGetNamespaceFromPath() {
1235 StorageAsset path = repoContent.getRepository( ).getAsset( "/org/apache/axis2" );
1236 Namespace ns = repoContent.getNamespaceFromPath( path );
1237 assertNotNull( ns );
1238 assertEquals( "org.apache.axis2", ns.getNamespace( ) );
1243 public void testArtifactListWithArtifactSelectorAndRelated() {
1244 ItemSelector selector = ArchivaItemSelector.builder( )
1245 .withNamespace( "org.apache.maven" )
1246 .withProjectId( "samplejar" )
1247 .withVersion( "1.0" )
1248 .withArtifactVersion( "1.0" )
1249 .withArtifactId( "samplejar" )
1250 .withExtension( "jar" )
1251 .includeRelatedArtifacts()
1254 List<? extends Artifact> results = repoContent.getArtifacts( selector );
1256 assertNotNull( results );
1257 assertEquals( 3, results.size( ) );
1259 Artifact artifact = results.stream( ).filter( a -> a.getFileName( ).equalsIgnoreCase( "samplejar-1.0.jar" ) )
1261 assertNotNull( artifact );
1262 assertEquals( BaseArtifactTypes.MAIN, artifact.getDataType( ) );
1264 artifact = results.stream( ).filter( a -> a.getFileName( ).equalsIgnoreCase( "samplejar-1.0.jar.md5" ) )
1266 assertNotNull( artifact );
1267 assertEquals( BaseArtifactTypes.RELATED, artifact.getDataType( ) );
1268 assertEquals( "md5", artifact.getExtension( ) );
1270 artifact = results.stream( ).filter( a -> a.getFileName( ).equalsIgnoreCase( "samplejar-1.0.jar.sha1" ) )
1272 assertNotNull( artifact );
1273 assertEquals( BaseArtifactTypes.RELATED, artifact.getDataType( ) );
1274 assertEquals( "sha1", artifact.getExtension( ) );
1278 private Path copyRepository(String repoName) throws IOException, URISyntaxException
1280 Path tempDir = Files.createTempDirectory( "archiva-repocontent" );
1281 Path repoSource = Paths.get( Thread.currentThread( ).getContextClassLoader( ).getResource( "repositories/" + repoName ).toURI( ) );
1282 assertTrue( Files.exists( repoSource ) );
1283 FileUtils.copyDirectory( repoSource.toFile( ), tempDir.toFile() );
1287 private ManagedRepository createManagedRepoWithContent(String sourceRepoName) throws IOException, URISyntaxException
1289 Path repoDir = copyRepository( sourceRepoName );
1290 MavenManagedRepository repo = createRepository( sourceRepoName, sourceRepoName, repoDir );
1291 ManagedDefaultRepositoryContent deleteRepoContent = new ManagedDefaultRepositoryContent( repo, artifactMappingProviders, fileTypes, fileLockManager );
1292 deleteRepoContent.setMavenContentHelper( contentHelper );
1297 public void deleteNamespaceItem() throws IOException, URISyntaxException, ItemNotFoundException
1299 ManagedRepository repo = createManagedRepoWithContent( "delete-repository" );
1300 ManagedRepositoryContent myRepoContent = repo.getContent( );
1301 Path repoRoot = repo.getAsset( "" ).getFilePath( );
1302 assertTrue( Files.exists(repoRoot.resolve( "org/apache/maven" )) );
1303 ArchivaItemSelector selector = ArchivaItemSelector.builder( )
1304 .withNamespace( "org.apache.maven" ).build();
1305 ContentItem item = myRepoContent.getItem( selector );
1306 assertTrue( item instanceof Namespace );
1307 myRepoContent.deleteItem( item );
1308 assertFalse( Files.exists(repoRoot.resolve( "org/apache/maven" )) );
1309 assertTrue( Files.exists(repoRoot.resolve( "org/apache" )) );
1311 // Sub namespaces are deleted too
1312 assertTrue( Files.exists(repoRoot.resolve( "org/apache/test/samplejar" )) );
1313 assertTrue( Files.exists(repoRoot.resolve( "org/apache/test/sub/samplejar" )) );
1314 selector = ArchivaItemSelector.builder( )
1315 .withNamespace( "org.apache.test" ).build();
1316 item = myRepoContent.getItem( selector );
1317 assertTrue( item instanceof Namespace );
1318 myRepoContent.deleteItem( item );
1319 assertFalse( Files.exists(repoRoot.resolve( "org/apache/test/samplejar" )) );
1320 assertFalse( Files.exists(repoRoot.resolve( "org/apache/test/sub/samplejar" )) );
1324 public void deleteProjectItem() throws IOException, URISyntaxException, ItemNotFoundException
1326 ManagedRepository repo = createManagedRepoWithContent( "delete-repository" );
1327 ManagedRepositoryContent myRepoContent = repo.getContent( );
1328 Path repoRoot = repo.getAsset( "" ).getFilePath( );
1329 assertTrue( Files.exists(repoRoot.resolve( "org/apache/maven/A" )) );
1330 ArchivaItemSelector selector = ArchivaItemSelector.builder( )
1331 .withNamespace( "org.apache.maven" )
1332 .withProjectId( "A" ).build();
1333 ContentItem item = myRepoContent.getItem( selector );
1334 assertTrue( item instanceof Project );
1335 myRepoContent.deleteItem( item );
1336 assertTrue( Files.exists(repoRoot.resolve( "org/apache/maven" )) );
1337 assertTrue( Files.exists( repoRoot.resolve( "org/apache/maven/samplejar/1.0" ) ) );
1338 assertTrue( Files.exists( repoRoot.resolve( "org/apache/maven/samplejar/2.0" ) ) );
1339 assertFalse( Files.exists(repoRoot.resolve( "org/apache/maven/A" )) );
1341 assertTrue( Files.exists(repoRoot.resolve( "org/apache/test/samplejar" )) );
1342 assertTrue( Files.exists(repoRoot.resolve( "org/apache/test/sub/samplejar" )) );
1343 selector = ArchivaItemSelector.builder( )
1344 .withNamespace( "org.apache.test" )
1345 .withProjectId( "samplejar" ).build();
1346 item = myRepoContent.getItem( selector );
1347 assertTrue( item instanceof Project );
1348 myRepoContent.deleteItem( item );
1349 assertFalse( Files.exists(repoRoot.resolve( "org/apache/test/samplejar" )) );
1350 assertTrue( Files.exists(repoRoot.resolve( "org/apache/test/sub/samplejar" )) );
1354 public void deleteVersionItem() throws IOException, URISyntaxException, ItemNotFoundException
1356 ManagedRepository repo = createManagedRepoWithContent( "delete-repository" );
1357 ManagedRepositoryContent myRepoContent = repo.getContent( );
1358 Path repoRoot = repo.getAsset( "" ).getFilePath( );
1359 assertTrue( Files.exists(repoRoot.resolve( "org/apache/maven/A/1.0" )) );
1360 ArchivaItemSelector selector = ArchivaItemSelector.builder( )
1361 .withNamespace( "org.apache.maven" )
1362 .withProjectId( "A" )
1363 .withVersion( "1.0" ).build();
1364 ContentItem item = myRepoContent.getItem( selector );
1365 assertTrue( item instanceof Version );
1366 myRepoContent.deleteItem( item );
1367 assertTrue( Files.exists(repoRoot.resolve( "org/apache/maven/A" )) );
1368 assertTrue( Files.exists( repoRoot.resolve( "org/apache/maven/samplejar/1.0" ) ) );
1369 assertTrue( Files.exists( repoRoot.resolve( "org/apache/maven/samplejar/2.0" ) ) );
1370 assertFalse( Files.exists(repoRoot.resolve( "org/apache/maven/A/1.0" )) );
1372 assertTrue( Files.exists(repoRoot.resolve( "org/apache/test/samplejar" )) );
1373 assertTrue( Files.exists(repoRoot.resolve( "org/apache/test/sub/samplejar" )) );
1374 selector = ArchivaItemSelector.builder( )
1375 .withNamespace( "org.apache.test" )
1376 .withProjectId( "samplejar" )
1377 .withVersion( "2.0" ).build();
1378 item = myRepoContent.getItem( selector );
1379 assertTrue( item instanceof Version );
1380 myRepoContent.deleteItem( item );
1381 assertFalse( Files.exists(repoRoot.resolve( "org/apache/test/samplejar/2.0" )) );
1382 assertTrue( Files.exists(repoRoot.resolve( "org/apache/test/samplejar/1.0" )) );
1383 assertTrue( Files.exists(repoRoot.resolve( "org/apache/test/sub/samplejar/1.0" )) );
1384 assertTrue( Files.exists(repoRoot.resolve( "org/apache/test/sub/samplejar/2.0" )) );
1388 public void deleteArtifactItem() throws IOException, URISyntaxException, ItemNotFoundException
1390 ManagedRepository repo = createManagedRepoWithContent( "delete-repository" );
1391 ManagedRepositoryContent myRepoContent = repo.getContent( );
1392 Path repoRoot = repo.getAsset( "" ).getFilePath( );
1393 assertTrue( Files.exists(repoRoot.resolve( "org/apache/maven/A/1.0/A-1.0.pom" )) );
1394 assertTrue( Files.exists(repoRoot.resolve( "org/apache/maven/A/1.0/A-1.0.war" )) );
1395 ArchivaItemSelector selector = ArchivaItemSelector.builder( )
1396 .withNamespace( "org.apache.maven" )
1397 .withProjectId( "A" )
1398 .withVersion( "1.0" )
1399 .withArtifactId( "A" )
1400 .withArtifactVersion( "1.0" )
1401 .withExtension( "pom" )
1403 ContentItem item = myRepoContent.getItem( selector );
1404 assertTrue( item instanceof Artifact );
1405 myRepoContent.deleteItem( item );
1406 assertTrue( Files.exists( repoRoot.resolve( "org/apache/maven/samplejar/1.0" ) ) );
1407 assertTrue( Files.exists( repoRoot.resolve( "org/apache/maven/samplejar/2.0" ) ) );
1408 assertFalse( Files.exists(repoRoot.resolve( "org/apache/maven/A/1.0/A-1.0.pom" )) );
1409 assertTrue( Files.exists(repoRoot.resolve( "org/apache/maven/A/1.0/A-1.0.war" )) );
1412 assertTrue( Files.exists(repoRoot.resolve( "org/apache/test/samplejar/1.0/samplejar-1.0.jar" )) );
1413 assertTrue( Files.exists(repoRoot.resolve( "org/apache/test/samplejar/1.0/samplejar-1.0.jar.md5" )) );
1414 assertTrue( Files.exists(repoRoot.resolve( "org/apache/test/samplejar/1.0/samplejar-1.0.jar.sha1" )) );
1415 assertTrue( Files.exists(repoRoot.resolve( "org/apache/test/samplejar/1.0/samplejar-1.0.pom" )) );
1416 assertTrue( Files.exists(repoRoot.resolve( "org/apache/test/samplejar/1.0/samplejar-1.0-source.jar" )) );
1417 selector = ArchivaItemSelector.builder( )
1418 .withNamespace( "org.apache.test" )
1419 .withProjectId( "samplejar" )
1420 .withVersion( "1.0" )
1421 .withArtifactId( "samplejar" )
1422 .withArtifactVersion( "1.0" )
1423 .withExtension( "jar" )
1425 item = myRepoContent.getItem( selector );
1426 assertTrue( item instanceof Artifact );
1427 myRepoContent.deleteItem( item );
1428 assertFalse( Files.exists(repoRoot.resolve( "org/apache/test/samplejar/1.0/samplejar-1.0.jar" )) );
1429 assertTrue( Files.exists(repoRoot.resolve( "org/apache/test/samplejar/1.0/samplejar-1.0.jar.md5" )) );
1430 assertTrue( Files.exists(repoRoot.resolve( "org/apache/test/samplejar/1.0/samplejar-1.0.jar.sha1" )) );
1431 assertTrue( Files.exists(repoRoot.resolve( "org/apache/test/samplejar/1.0/samplejar-1.0.pom" )) );
1432 assertTrue( Files.exists(repoRoot.resolve( "org/apache/test/samplejar/1.0/samplejar-1.0-source.jar" )) );
1433 assertTrue( Files.exists(repoRoot.resolve( "org/apache/test/samplejar/2.0" )) );
1434 assertTrue( Files.exists(repoRoot.resolve( "org/apache/test/sub/samplejar/1.0" )) );
1435 assertTrue( Files.exists(repoRoot.resolve( "org/apache/test/sub/samplejar/2.0" )) );
1437 selector = ArchivaItemSelector.builder( )
1438 .withNamespace( "org.apache.test" )
1439 .withProjectId( "samplejar" )
1440 .withVersion( "1.0" )
1441 .withArtifactId( "samplejar" )
1442 .withArtifactVersion( "1.0" )
1443 .withClassifier( "source" )
1444 .withExtension( "jar" )
1446 item = myRepoContent.getItem( selector );
1447 assertTrue( item instanceof Artifact );
1448 myRepoContent.deleteItem( item );
1449 assertFalse( Files.exists(repoRoot.resolve( "org/apache/test/samplejar/1.0/samplejar-1.0.jar" )) );
1450 assertTrue( Files.exists(repoRoot.resolve( "org/apache/test/samplejar/1.0/samplejar-1.0.jar.md5" )) );
1451 assertTrue( Files.exists(repoRoot.resolve( "org/apache/test/samplejar/1.0/samplejar-1.0.jar.sha1" )) );
1452 assertTrue( Files.exists(repoRoot.resolve( "org/apache/test/samplejar/1.0/samplejar-1.0.pom" )) );
1453 assertFalse( Files.exists(repoRoot.resolve( "org/apache/test/samplejar/1.0/samplejar-1.0-source.jar" )) );
1454 assertTrue( Files.exists(repoRoot.resolve( "org/apache/test/samplejar/1.0/samplejar-1.0-source.jar.sha1" )) );
1455 assertTrue( Files.exists(repoRoot.resolve( "org/apache/test/samplejar/2.0" )) );
1456 assertTrue( Files.exists(repoRoot.resolve( "org/apache/test/sub/samplejar/1.0" )) );
1457 assertTrue( Files.exists(repoRoot.resolve( "org/apache/test/sub/samplejar/2.0" )) );
1459 selector = ArchivaItemSelector.builder( )
1460 .withNamespace( "org.apache.test" )
1461 .withProjectId( "samplejar" )
1462 .withVersion( "1.0" )
1463 .withArtifactId( "samplejar" )
1464 .withArtifactVersion( "1.0" )
1465 .withExtension( "jar.md5" )
1467 item = myRepoContent.getItem( selector );
1468 assertTrue( item instanceof Artifact );
1469 myRepoContent.deleteItem( item );
1470 assertFalse( Files.exists(repoRoot.resolve( "org/apache/test/samplejar/1.0/samplejar-1.0.jar" )) );
1471 assertFalse( Files.exists(repoRoot.resolve( "org/apache/test/samplejar/1.0/samplejar-1.0.jar.md5" )) );
1472 assertTrue( Files.exists(repoRoot.resolve( "org/apache/test/samplejar/1.0/samplejar-1.0.jar.sha1" )) );
1473 assertTrue( Files.exists(repoRoot.resolve( "org/apache/test/samplejar/1.0/samplejar-1.0.pom" )) );
1474 assertFalse( Files.exists(repoRoot.resolve( "org/apache/test/samplejar/1.0/samplejar-1.0-source.jar" )) );
1475 assertTrue( Files.exists(repoRoot.resolve( "org/apache/test/samplejar/1.0/samplejar-1.0-source.jar.sha1" )) );
1476 assertTrue( Files.exists(repoRoot.resolve( "org/apache/test/samplejar/2.0" )) );
1477 assertTrue( Files.exists(repoRoot.resolve( "org/apache/test/sub/samplejar/1.0" )) );
1478 assertTrue( Files.exists(repoRoot.resolve( "org/apache/test/sub/samplejar/2.0" )) );
1484 public void deleteItemNotFound() throws IOException, URISyntaxException, ItemNotFoundException
1486 ManagedRepository repo = createManagedRepoWithContent( "delete-repository" );
1487 ManagedRepositoryContent myRepoContent = repo.getContent( );
1488 Path repoRoot = repo.getAsset( "" ).getFilePath( );
1490 ArchivaItemSelector selector = ArchivaItemSelector.builder( )
1491 .withNamespace( "org.apache.test2" )
1494 ContentItem item = myRepoContent.getItem( selector );
1495 assertTrue( item instanceof Namespace );
1498 myRepoContent.deleteItem( item );
1499 assertTrue( "ItemNotFoundException expected for non existing namespace", false );
1500 } catch ( ItemNotFoundException e) {
1503 selector = ArchivaItemSelector.builder( )
1504 .withNamespace( "org.apache.test" )
1505 .withProjectId( "samplejar2" )
1507 item = myRepoContent.getItem( selector );
1508 assertTrue( item instanceof Project );
1511 myRepoContent.deleteItem( item );
1512 assertTrue( "ItemNotFoundException expected for non existing project", false );
1513 } catch ( ItemNotFoundException e) {
1516 selector = ArchivaItemSelector.builder( )
1517 .withNamespace( "org.apache.test" )
1518 .withProjectId( "samplejar" )
1521 item = myRepoContent.getItem( selector );
1522 assertTrue( item instanceof Version );
1525 myRepoContent.deleteItem( item );
1526 assertTrue( "ItemNotFoundException expected for non existing version", false );
1527 } catch ( ItemNotFoundException e) {
1530 selector = ArchivaItemSelector.builder( )
1531 .withNamespace( "org.apache.test" )
1532 .withProjectId( "samplejar" )
1534 .withArtifactId( "samplejar" )
1535 .withArtifactVersion( "1.0" )
1536 .withExtension( "jax" )
1538 item = myRepoContent.getItem( selector );
1539 assertTrue( item instanceof Artifact );
1542 myRepoContent.deleteItem( item );
1543 assertTrue( "ItemNotFoundException expected for non existing artifact", false );
1544 } catch ( ItemNotFoundException e) {
1551 public void testAddArtifact() throws IOException, URISyntaxException, LayoutException
1553 ManagedRepository repo = createManagedRepoWithContent( "delete-repository" );
1554 ManagedRepositoryContent myRepoContent = repo.getContent( );
1555 BaseRepositoryContentLayout layout = myRepoContent.getLayout( BaseRepositoryContentLayout.class );
1556 Path repoRoot = repo.getAsset( "" ).getFilePath( );
1558 Path tmpFile = Files.createTempFile( "archiva-mvn-repotest", "jar" );
1559 try( OutputStream outputStream = Files.newOutputStream( tmpFile ))
1561 for ( int i = 0; i < 255; i++ )
1563 outputStream.write( "test.test.test\n".getBytes( Charset.forName( "UTF-8" ) ) );
1567 Path file = repoRoot.resolve( "org/apache/maven/samplejar/2.0/samplejar-2.0.jar" );
1568 FileTime lmt = Files.getLastModifiedTime( file );
1569 ArchivaItemSelector selector = ArchivaItemSelector.builder( )
1570 .withNamespace( "org.apache.maven" )
1571 .withProjectId( "samplejar" )
1572 .withVersion( "2.0" )
1573 .withArtifactId( "samplejar" )
1574 .withArtifactVersion( "2.0" )
1575 .withExtension( "jar" )
1577 Artifact artifact = layout.getArtifact( selector );
1578 layout.addArtifact( tmpFile, artifact );
1579 FileTime lmtAfter = Files.getLastModifiedTime( file );
1580 assertNotEquals( lmtAfter, lmt );
1581 Reader ln = Files.newBufferedReader( file, Charset.forName( "UTF-8" ) );
1582 char[] content = new char[50];
1584 assertTrue( new String( content ).startsWith( "test.test.test" ) );
1586 tmpFile = Files.createTempFile( "archiva-mvn-repotest", "jar" );
1587 try( OutputStream outputStream = Files.newOutputStream( tmpFile ))
1589 for ( int i = 0; i < 255; i++ )
1591 outputStream.write( "test.test.test\n".getBytes( Charset.forName( "UTF-8" ) ) );
1594 file = repoRoot.resolve( "org/apache/maven/samplejar/2.0/samplejar-2.0.test" );
1595 assertFalse( Files.exists( file ) );
1596 assertTrue( Files.exists( tmpFile ) );
1597 selector = ArchivaItemSelector.builder( )
1598 .withNamespace( "org.apache.maven" )
1599 .withProjectId( "samplejar" )
1600 .withVersion( "2.0" )
1601 .withArtifactId( "samplejar" )
1602 .withArtifactVersion( "2.0" )
1603 .withExtension( "test" )
1605 artifact = layout.getArtifact( selector );
1606 layout.addArtifact( tmpFile, artifact );
1607 ln = Files.newBufferedReader( file, Charset.forName( "UTF-8" ) );
1609 assertTrue( new String( content ).startsWith( "test.test.test" ) );
1613 public void getExistingMetadataItem() {
1614 // org/apache/maven/some-ejb/1.0
1615 ArchivaItemSelector versionSelector = ArchivaItemSelector.builder( )
1616 .withNamespace( "org.apache.maven" )
1617 .withProjectId( "some-ejb" )
1618 .withVersion( "1.0" ).build( );
1619 Version version = repoContent.getVersion( versionSelector );
1620 DataItem metaData = repoContent.getMetadataItem( version );
1621 assertTrue( metaData.exists( ) );
1622 assertEquals( "/org/apache/maven/some-ejb/1.0/maven-metadata.xml", metaData.getAsset( ).getPath( ) );
1626 public void getNonExistingMetadataItem() {
1627 // org/apache/maven/some-ejb/1.0
1628 ArchivaItemSelector versionSelector = ArchivaItemSelector.builder( )
1629 .withNamespace( "javax.sql" )
1630 .withProjectId( "jdbc" )
1631 .withVersion( "2.0" ).build( );
1632 Version version = repoContent.getVersion( versionSelector );
1633 DataItem metaData = repoContent.getMetadataItem( version );
1634 assertFalse( metaData.exists( ) );
1635 assertEquals( "/javax/sql/jdbc/2.0/maven-metadata.xml", metaData.getAsset( ).getPath( ) );