1 package org.apache.archiva.maven.repository.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.maven.metadata.MavenMetadataReader;
27 import org.apache.archiva.maven.repository.MavenManagedRepository;
28 import org.apache.archiva.maven.repository.metadata.storage.ArtifactMappingProvider;
29 import org.apache.archiva.metadata.repository.storage.RepositoryPathTranslator;
30 import org.apache.archiva.repository.EditableManagedRepository;
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.BaseRepositoryContentLayout;
37 import org.apache.archiva.repository.content.ContentItem;
38 import org.apache.archiva.repository.content.DataItem;
39 import org.apache.archiva.repository.content.ItemNotFoundException;
40 import org.apache.archiva.repository.content.ItemSelector;
41 import org.apache.archiva.repository.content.LayoutException;
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.storage.StorageAsset;
48 import org.apache.commons.io.FileUtils;
49 import org.junit.Before;
50 import org.junit.Test;
52 import javax.inject.Inject;
53 import javax.inject.Named;
54 import java.io.IOException;
55 import java.io.OutputStream;
56 import java.io.Reader;
57 import java.net.URISyntaxException;
58 import java.nio.charset.Charset;
59 import java.nio.file.Files;
60 import java.nio.file.Path;
61 import java.nio.file.Paths;
62 import java.nio.file.attribute.FileTime;
63 import java.util.List;
64 import java.util.stream.Collectors;
65 import java.util.stream.Stream;
67 import static org.junit.Assert.*;
70 * ManagedDefaultRepositoryContentTest
72 public class ManagedDefaultRepositoryContentTest
73 extends AbstractBaseRepositoryContentLayoutTest
75 private ManagedDefaultRepositoryContent repoContent;
81 @Named ( "archivaConfiguration#default" )
82 ArchivaConfiguration archivaConfiguration;
85 List<? extends ArtifactMappingProvider> artifactMappingProviders;
88 MavenContentHelper contentHelper;
91 @Named( "metadataReader#maven" )
92 MavenMetadataReader metadataReader;
95 FileLockManager fileLockManager;
98 @Named( "repositoryPathTranslator#maven2" )
99 RepositoryPathTranslator pathTranslator;
102 private Path getRepositoryPath(String repoName) {
105 return Paths.get( Thread.currentThread( ).getContextClassLoader( ).getResource( "repositories/" + repoName ).toURI( ) );
107 catch ( URISyntaxException e )
109 throw new RuntimeException( "Could not resolve repository path " + e.getMessage( ), e );
117 Path repoDir = getRepositoryPath( "default-repository" );
119 MavenManagedRepository repository = createRepository( "testRepo", "Unit Test Repo", repoDir );
121 FileType fileType = archivaConfiguration.getConfiguration().getRepositoryScanning().getFileTypes().get( 0 );
122 fileType.addPattern( "**/*.xml" );
123 assertEquals( FileTypes.ARTIFACTS, fileType.getId() );
125 fileTypes.afterConfigurationChange( null, "fileType", null );
127 repoContent = new ManagedDefaultRepositoryContent(repository, fileTypes, fileLockManager);
128 repoContent.setMavenContentHelper( contentHelper );
129 repoContent.setMetadataReader( metadataReader );
130 repoContent.setPathTranslator( pathTranslator );
131 repoContent.setArtifactMappingProviders( artifactMappingProviders );
133 //repoContent = (ManagedRepositoryContent) lookup( ManagedRepositoryContent.class, "default" );
137 public void testGetVersionsSnapshotA()
140 assertArtifactVersions( "snap_shots_a", "1.0-alpha-11-SNAPSHOT",
141 new String[]{ "1.0-alpha-11-SNAPSHOT", "1.0-alpha-11-20070221.194724-2",
142 "1.0-alpha-11-20070302.212723-3", "1.0-alpha-11-20070303.152828-4",
143 "1.0-alpha-11-20070305.215149-5", "1.0-alpha-11-20070307.170909-6",
144 "1.0-alpha-11-20070314.211405-9", "1.0-alpha-11-20070316.175232-11" } );
150 public void testToPathOnNullArtifactReference()
154 ItemSelector reference = null;
155 repoContent.toPath( reference );
156 fail( "Should have failed due to null artifact reference." );
158 catch ( IllegalArgumentException e )
165 protected Artifact createArtifact( String groupId, String artifactId, String version, String classifier, String type ) throws LayoutException
167 ItemSelector selector = createItemSelector( groupId, artifactId, version, classifier, type );
168 return repoContent.getLayout( BaseRepositoryContentLayout.class ).getArtifact( selector );
172 public void testExcludeMetadataFile()
175 assertVersions( "include_xml", "1.0", new String[]{ "1.0" } );
179 private void assertVersions( String artifactId, String version, String[] expectedVersions )
182 // Use the test metadata-repository, which is already setup for
183 // These kind of version tests.
184 Path repoDir = getRepositoryPath( "metadata-repository" );
185 ((EditableManagedRepository)repoContent.getRepository()).setLocation( repoDir.toAbsolutePath().toUri() );
187 // Request the versions.
189 // Sort the list (for asserts later)
190 final VersionComparator comparator = new VersionComparator( );
192 ItemSelector selector = ArchivaItemSelector.builder( )
193 .withNamespace( "org.apache.archiva.metadata.tests" )
194 .withProjectId( artifactId )
195 .withVersion( version )
197 List<String> versions = repoContent.getVersions( selector ).stream()
198 .map(v -> v.getId()).sorted( comparator ).collect( Collectors.toList());
199 assertArrayEquals( expectedVersions, versions.toArray( ) );
204 private void assertArtifactVersions( String artifactId, String version, String[] expectedVersions )
207 // Use the test metadata-repository, which is already setup for
208 // These kind of version tests.
209 Path repoDir = getRepositoryPath( "metadata-repository" );
210 ((EditableManagedRepository)repoContent.getRepository()).setLocation( repoDir.toAbsolutePath().toUri() );
212 // Request the versions.
214 // Sort the list (for asserts later)
215 final VersionComparator comparator = new VersionComparator( );
217 ItemSelector selector = ArchivaItemSelector.builder( )
218 .withNamespace( "org.apache.archiva.metadata.tests" )
219 .withProjectId( artifactId )
220 .withVersion( version )
222 List<String> versions = repoContent.getArtifactVersions( selector ).stream()
223 .sorted( comparator ).collect( Collectors.toList());
224 assertArrayEquals( expectedVersions, versions.toArray( ) );
230 public void getTestGetProjectWithIllegalArgs() {
231 ItemSelector selector = ArchivaItemSelector.builder( )
232 .withNamespace( "org.apache" )
233 .withVersion( "1.0" )
237 repoContent.getProject( selector );
238 assertFalse( "Should throw IllegalArgumentException if no project id is given", true );
239 } catch (IllegalArgumentException e) {
241 assertTrue( e.getMessage( ).contains( "Project id must be set" ) );
246 public void getTestGetVersionWithIllegalArgs() {
247 ItemSelector selector = ArchivaItemSelector.builder( )
248 .withNamespace( "org.apache.maven" )
249 .withVersion( "1.0" )
253 repoContent.getVersion( selector );
254 assertFalse( "Should throw IllegalArgumentException if no project id is given", true );
255 } catch (IllegalArgumentException e) {
257 assertTrue( e.getMessage( ).contains( "Project id must be set" ) );
261 selector = ArchivaItemSelector.builder( )
262 .withNamespace( "org.apache.maven" )
263 .withProjectId( "shared" )
267 repoContent.getVersion( selector );
268 assertFalse( "Should throw IllegalArgumentException if no version is given", true );
269 } catch (IllegalArgumentException e) {
271 assertTrue( e.getMessage( ).contains( "Version must be set" ) );
276 public void getTestGetArtifactWithIllegalArgs() {
277 ItemSelector selector = ArchivaItemSelector.builder( )
278 .withNamespace( "org.apache.maven" )
279 .withVersion( "1.0" )
280 .withArtifactId( "shared" )
281 .withArtifactVersion("1.0")
285 repoContent.getArtifact( selector );
286 assertFalse( "Should throw IllegalArgumentException if no project id is given", true );
287 } catch (IllegalArgumentException e) {
289 assertTrue( e.getMessage( ).contains( "Project id must be set" ) );
293 selector = ArchivaItemSelector.builder( )
294 .withNamespace( "org.apache.maven" )
295 .withProjectId( "shared" )
296 .withArtifactId( "shared" )
300 repoContent.getArtifact( selector );
301 assertFalse( "Should throw IllegalArgumentException if no version is given", true );
302 } catch (IllegalArgumentException e) {
304 assertTrue( e.getMessage( ).contains( "Version must be set" ) );
307 selector = ArchivaItemSelector.builder( )
308 .withNamespace( "org.apache.maven" )
309 .withProjectId( "shared" )
311 .withArtifactVersion("1.0")
315 repoContent.getArtifact( selector );
316 assertFalse( "Should throw IllegalArgumentException if no artifact id is given", true );
317 } catch (IllegalArgumentException e) {
319 assertTrue( e.getMessage( ).contains( "Artifact id must be set" ) );
326 public void testGetProjects() {
327 ItemSelector selector = ArchivaItemSelector.builder( )
328 .withNamespace( "org.apache.maven" ).build();
329 Namespace ns = repoContent.getNamespace( selector );
331 List<? extends Project> projects = repoContent.getProjects( ns );
332 assertEquals( 12, projects.size( ) );
333 String[] expected = new String[]{
334 "A", "B", "C", "archiva", "discovery", "maven-parent", "samplejar", "shared", "some-ejb", "test",
337 Object[] actual = projects.stream( ).map( p -> p.getId( ) ).sorted( ).toArray( );
338 assertArrayEquals( expected, actual);
342 public void testGetProjectsWithSelector() {
343 ItemSelector selector = ArchivaItemSelector.builder( )
344 .withNamespace( "org.apache.maven" ).build();
345 List<? extends Project> projects = repoContent.getProjects( selector );
346 assertEquals( 12, projects.size( ) );
347 String[] expected = new String[]{
348 "A", "B", "C", "archiva", "discovery", "maven-parent", "samplejar", "shared", "some-ejb", "test",
351 Object[] actual = projects.stream( ).map( p -> p.getId( ) ).sorted( ).toArray( );
352 assertArrayEquals( expected, actual);
356 public void testGetVersionsWithIllegalSelector() {
357 ItemSelector selector = ArchivaItemSelector.builder( )
358 .withNamespace( "org.apache.maven" ).build();
361 List<? extends Version> versions = repoContent.getVersions( selector );
362 assertFalse( "IllegalArgumentException expected, when project id not set", true );
363 } catch (IllegalArgumentException e) {
364 assertEquals( "Project id not set, while retrieving versions.", e.getMessage( ) );
369 public void testGetVersionsWithSelector() {
370 ItemSelector selector = ArchivaItemSelector.builder( )
371 .withNamespace( "org.apache.maven" )
372 .withProjectId( "samplejar" ).build();
373 List<? extends Version> versions = repoContent.getVersions( selector );
374 assertNotNull( versions );
375 assertEquals( 2, versions.size( ) );
380 protected ItemSelector toItemSelector( String path ) throws LayoutException
382 return repoContent.toItemSelector( path );
386 protected String toPath( Artifact reference )
388 return repoContent.toPath( reference );
392 protected String toPath( ItemSelector selector ) {
393 return repoContent.toPath( selector );
397 protected ManagedRepositoryContent getManaged( )
403 protected RepositoryContent getContent( )
408 private Path setupRepoCopy( String source, String target) throws IOException
410 Path defaultRepo = getRepositoryPath( source );
411 Path newRepo = defaultRepo.getParent( ).resolve( target );
412 FileUtils.copyDirectory( defaultRepo.toFile( ), newRepo.toFile( ) );
414 MavenManagedRepository repository = createRepository( "testRepo", "Unit Test Repo", newRepo );
416 FileType fileType = archivaConfiguration.getConfiguration().getRepositoryScanning().getFileTypes().get( 0 );
417 fileType.addPattern( "**/*.xml" );
418 assertEquals( FileTypes.ARTIFACTS, fileType.getId() );
420 fileTypes.afterConfigurationChange( null, "fileType", null );
422 repoContent = new ManagedDefaultRepositoryContent(repository, fileTypes, fileLockManager);
429 public void testGetArtifactStreamWithVersionSelector() {
430 ItemSelector selector = ArchivaItemSelector.builder( )
431 .withNamespace( "javax.sql" )
432 .withProjectId( "jdbc" )
433 .withVersion( "2.0" ).build();
434 try(Stream<? extends Artifact> stream = repoContent.newArtifactStream( selector ))
436 assertNotNull( stream );
437 List<? extends Artifact> results = stream.collect( Collectors.toList( ) );
438 checkArtifactListWithVersionSelector1( results );
443 public void testGetArtifactListWithVersionSelector() {
444 ItemSelector selector = ArchivaItemSelector.builder( )
445 .withNamespace( "javax.sql" )
446 .withProjectId( "jdbc" )
447 .withVersion( "2.0" ).build();
448 List<? extends Artifact> results = repoContent.getArtifacts( selector );
449 checkArtifactListWithVersionSelector1( results );
452 private void checkArtifactListWithVersionSelector1( List<? extends Artifact> results )
454 assertNotNull( results );
455 assertEquals( 2, results.size( ) );
456 Artifact mainArtifact = results.stream( ).filter( a -> a.getFileName( ).equals( "jdbc-2.0.jar" ) ).findFirst( ).get( );
457 assertNotNull( mainArtifact );
458 assertEquals( BaseArtifactTypes.MAIN, mainArtifact.getDataType( ) );
459 Artifact metaArtifact = results.stream( ).filter( a -> a.getFileName( ).equals( "maven-metadata-repository.xml" ) ).findFirst( ).get( );
460 assertNotNull( metaArtifact );
461 assertEquals( MavenTypes.REPOSITORY_METADATA, metaArtifact.getDataType( ) );
465 public void testGetArtifactStreamWithVersionSelector2() {
466 ItemSelector selector = ArchivaItemSelector.builder( )
467 .withNamespace( "org.apache.axis2" )
468 .withProjectId( "axis2" )
469 .withVersion( "1.3-SNAPSHOT" ).build();
470 try(Stream<? extends Artifact> stream = repoContent.newArtifactStream( selector ))
472 assertNotNull( stream );
473 List<? extends Artifact> results = stream.collect( Collectors.toList( ) );
474 checkArtifactListWithVersionSelector2( results );
479 public void testGetArtifactListWithVersionSelector2() {
480 ItemSelector selector = ArchivaItemSelector.builder( )
481 .withNamespace( "org.apache.axis2" )
482 .withProjectId( "axis2" )
483 .withVersion( "1.3-SNAPSHOT" ).build();
484 List<? extends Artifact> results = repoContent.getArtifacts( selector );
485 checkArtifactListWithVersionSelector2( results );
488 private void checkArtifactListWithVersionSelector2( List<? extends Artifact> results )
490 assertNotNull( results );
491 assertEquals( 39, results.size( ) );
493 Artifact artifact = results.stream( ).filter( a -> a.getFileName( ).equals( "axis2-1.3-20070725.210059-1.pom" ) )
494 .findFirst( ).get( );
496 assertNotNull( artifact );
497 assertEquals( "pom", artifact.getExtension( ) );
498 assertEquals( BaseArtifactTypes.MAIN, artifact.getDataType( ) );
499 assertEquals( "1.3-SNAPSHOT", artifact.getVersion( ).getId( ) );
500 assertEquals( "1.3-20070725.210059-1", artifact.getArtifactVersion( ) );
501 assertEquals( ".pom", artifact.getRemainder( ) );
502 assertEquals( "axis2", artifact.getId( ) );
503 assertEquals( "axis2", artifact.getVersion( ).getProject( ).getId( ) );
504 assertEquals( "org.apache.axis2", artifact.getVersion( ).getProject( ).getNamespace( ).getId( ) );
505 assertEquals( "", artifact.getClassifier( ) );
506 assertEquals( "pom", artifact.getType( ) );
509 artifact = results.stream( ).filter( a -> a.getFileName( ).equals( "axis2-1.3-20070725.210059-1.pom.md5" ) )
510 .findFirst( ).get( );
512 assertNotNull( artifact );
513 assertEquals( "md5", artifact.getExtension( ) );
514 assertEquals( BaseArtifactTypes.RELATED, artifact.getDataType( ) );
515 assertEquals( "1.3-SNAPSHOT", artifact.getVersion( ).getId( ) );
516 assertEquals( "1.3-20070725.210059-1", artifact.getArtifactVersion( ) );
517 assertEquals( ".pom.md5", artifact.getRemainder( ) );
518 assertEquals( "axis2", artifact.getId( ) );
519 assertEquals( "axis2", artifact.getVersion( ).getProject( ).getId( ) );
520 assertEquals( "org.apache.axis2", artifact.getVersion( ).getProject( ).getNamespace( ).getId( ) );
521 assertEquals( "", artifact.getClassifier( ) );
522 assertEquals( "md5", artifact.getType( ) );
526 artifact = results.stream( ).filter( a -> a.getFileName( ).equals( "maven-metadata.xml" ) )
527 .findFirst( ).get( );
528 assertNotNull( artifact );
529 assertEquals( BaseArtifactTypes.METADATA, artifact.getDataType( ) );
530 assertEquals( "1.3-SNAPSHOT", artifact.getVersion( ).getId( ) );
531 assertEquals( "xml", artifact.getExtension( ) );
535 public void testGetArtifactListWithArtifactSelector1() {
536 ItemSelector selector = ArchivaItemSelector.builder( )
537 .withNamespace( "org.apache.axis2" )
538 .withProjectId( "axis2" )
539 .withVersion( "1.3-SNAPSHOT" )
540 .withArtifactVersion( "1.3-20070731.113304-21" )
541 .withExtension( "pom" )
543 List<? extends Artifact> results = repoContent.getArtifacts( selector );
544 checkArtifactListWithArtifactSelector1( results );
548 public void testGetArtifactStreamWithArtifactSelector1() {
549 ItemSelector selector = ArchivaItemSelector.builder( )
550 .withNamespace( "org.apache.axis2" )
551 .withProjectId( "axis2" )
552 .withVersion( "1.3-SNAPSHOT" )
553 .withArtifactVersion( "1.3-20070731.113304-21" )
554 .withExtension( "pom" )
556 try(Stream<? extends Artifact> results = repoContent.newArtifactStream( selector ))
558 checkArtifactListWithArtifactSelector1( results.collect( Collectors.toList()) );
562 private void checkArtifactListWithArtifactSelector1( List<? extends Artifact> results )
564 assertNotNull( results );
565 assertEquals( 1, results.size( ) );
566 Artifact artifact = results.get( 0 );
567 assertEquals( "pom", artifact.getExtension( ) );
568 assertEquals( BaseArtifactTypes.MAIN, artifact.getDataType( ) );
572 public void testGetArtifactListWithArtifactSelector2() {
573 ItemSelector selector = ArchivaItemSelector.builder( )
574 .withNamespace( "org.apache.axis2" )
575 .withProjectId( "axis2" )
576 .withVersion( "1.3-SNAPSHOT" )
577 .withArtifactVersion( "1.3-20070731.113304-21" )
578 .withExtension( "pom" )
579 .includeRelatedArtifacts()
581 List<? extends Artifact> results = repoContent.getArtifacts( selector );
583 checkArtifactListWithArtifactSelector2( results );
588 public void testGetArtifactStreamWithArtifactSelector2() {
589 ItemSelector selector = ArchivaItemSelector.builder( )
590 .withNamespace( "org.apache.axis2" )
591 .withProjectId( "axis2" )
592 .withVersion( "1.3-SNAPSHOT" )
593 .withArtifactVersion( "1.3-20070731.113304-21" )
594 .withExtension( "pom" )
595 .includeRelatedArtifacts()
597 try(Stream<? extends Artifact> results = repoContent.newArtifactStream( selector ))
599 checkArtifactListWithArtifactSelector2( results.collect( Collectors.toList()) );
603 private void checkArtifactListWithArtifactSelector2( List<? extends Artifact> results )
605 assertNotNull( results );
606 assertEquals( 3, results.size( ) );
607 Artifact artifact = results.stream( ).filter( a -> a.getFileName( ).equalsIgnoreCase( "axis2-1.3-20070731.113304-21.pom" ) )
608 .findFirst( ).get( );
609 assertNotNull( artifact );
610 assertEquals( "pom", artifact.getExtension( ) );
611 assertEquals( BaseArtifactTypes.MAIN, artifact.getDataType( ) );
613 artifact = results.stream( ).filter( a -> a.getFileName( ).equalsIgnoreCase( "axis2-1.3-20070731.113304-21.pom.sha1" ) )
614 .findFirst( ).get( );
615 assertNotNull( artifact );
616 assertEquals( "sha1", artifact.getExtension( ) );
617 assertEquals( BaseArtifactTypes.RELATED, artifact.getDataType( ) );
622 public void testArtifactListWithProjectSelector() {
623 ItemSelector selector = ArchivaItemSelector.builder( )
624 .withNamespace( "org.apache.maven.shared" )
625 .withProjectId( "maven-downloader" )
627 List<? extends Artifact> results = repoContent.getArtifacts( selector );
628 checkArtifactListWithProjectSelector( results );
633 public void testArtifactStreamWithProjectSelector() {
634 ItemSelector selector = ArchivaItemSelector.builder( )
635 .withNamespace( "org.apache.maven.shared" )
636 .withProjectId( "maven-downloader" )
638 Stream<? extends Artifact> results = repoContent.newArtifactStream( selector );
639 checkArtifactListWithProjectSelector( results.collect( Collectors.toList()) );
643 private void checkArtifactListWithProjectSelector( List<? extends Artifact> results )
645 assertNotNull( results );
646 assertEquals( 27, results.size( ) );
648 Artifact artifact = results.stream( ).filter( a -> a.getFileName( ).equalsIgnoreCase( "maven-metadata.xml" ) )
649 .findFirst( ).get( );
650 assertNotNull( artifact );
651 assertEquals( "xml", artifact.getExtension( ) );
652 assertEquals( BaseArtifactTypes.METADATA, artifact.getDataType( ) );
654 artifact = results.stream( ).filter( a -> a.getFileName( ).equalsIgnoreCase( "maven-downloader-1.0-sources.jar" ) )
655 .findFirst( ).get( );
656 assertNotNull( artifact );
657 assertEquals( BaseArtifactTypes.MAIN, artifact.getDataType( ) );
658 assertEquals( "sources", artifact.getClassifier( ) );
659 assertEquals( "java-source", artifact.getType( ) );
661 artifact = results.stream( ).filter( a -> a.getFileName( ).equalsIgnoreCase( "maven-downloader-1.0-sources.jar.sha1" ) )
662 .findFirst( ).get( );
663 assertNotNull( artifact );
664 assertEquals( BaseArtifactTypes.RELATED, artifact.getDataType( ) );
665 assertEquals( "sources", artifact.getClassifier( ) );
666 assertEquals( "sha1", artifact.getType( ) );
667 assertEquals( ".jar.sha1", artifact.getRemainder( ) );
671 public void testArtifactListWithNamespaceSelector() {
672 ItemSelector selector = ArchivaItemSelector.builder( )
673 .withNamespace( "org.multilevel" )
675 List<? extends Artifact> results = repoContent.getArtifacts( selector );
676 assertNotNull( results );
677 assertEquals( 3, results.size( ) );
678 assertTrue( results.get( 0 ).getFileName( ).startsWith( "testproj1" ) );
682 public void testArtifactListWithNamespaceSelectorRecursive() {
683 ItemSelector selector = ArchivaItemSelector.builder( )
684 .withNamespace( "org.multilevel" )
687 List<? extends Artifact> results = repoContent.getArtifacts( selector );
688 checkArtifactListWithNamespaceSelectorRecursive( results );
692 public void testArtifactStreamWithNamespaceSelectorRecursive() {
693 ItemSelector selector = ArchivaItemSelector.builder( )
694 .withNamespace( "org.multilevel" )
697 Stream<? extends Artifact> results = repoContent.newArtifactStream( selector );
698 checkArtifactListWithNamespaceSelectorRecursive( results.collect( Collectors.toList()) );
701 private void checkArtifactListWithNamespaceSelectorRecursive( List<? extends Artifact> results )
703 assertNotNull( results );
704 assertEquals( 6, results.size( ) );
706 Artifact artifact = results.stream( ).filter( a -> a.getFileName( ).equalsIgnoreCase( "testproj2-1.0.pom" ) )
707 .findFirst( ).get( );
708 assertNotNull( artifact );
709 assertEquals( 6, artifact.getAsset( ).getParent( ).getPath( ).split( "/" ).length );
711 artifact = results.stream( ).filter( a -> a.getFileName( ).equalsIgnoreCase( "testproj1-1.0.pom" ) )
712 .findFirst( ).get( );
713 assertNotNull( artifact );
714 assertEquals( 5, artifact.getAsset( ).getParent( ).getPath( ).split( "/" ).length );
719 public void testArtifactListWithArtifactSelector1() {
720 ItemSelector selector = ArchivaItemSelector.builder( )
721 .withNamespace( "org.apache.maven" )
722 .withProjectId( "test" )
723 .withVersion( "1.0-SNAPSHOT" )
724 .withArtifactId( "test" )
725 .withArtifactVersion( "1.0-20050611.112233-1" )
728 List<? extends Artifact> results = repoContent.getArtifacts( selector );
730 assertNotNull( results );
731 assertEquals( 1, results.size( ) );
733 Artifact artifact = results.stream( ).filter( a -> a.getFileName( ).equalsIgnoreCase( "test-1.0-20050611.112233-1.jar" ) )
735 assertNotNull( artifact );
736 assertEquals( "", artifact.getClassifier( ) );
740 public void testArtifactListWithArtifactSelector2() {
741 ItemSelector selector = ArchivaItemSelector.builder( )
742 .withNamespace( "org.apache.maven" )
743 .withProjectId( "test" )
744 .withVersion( "1.0-SNAPSHOT" )
745 .withClassifier( "*" )
746 .withArtifactId( "test" )
747 .withArtifactVersion( "1.0-20050611.112233-1" )
750 List<? extends Artifact> results = repoContent.getArtifacts( selector );
752 assertNotNull( results );
753 assertEquals( 2, results.size( ) );
755 Artifact artifact = results.stream( ).filter( a -> a.getFileName( ).equalsIgnoreCase( "test-1.0-20050611.112233-1-javadoc.jar" ) )
757 assertNotNull( artifact );
758 assertEquals( "javadoc", artifact.getClassifier( ) );
759 assertEquals( "javadoc", artifact.getType( ) );
763 public void testArtifactListWithArtifactSelector3() {
764 ItemSelector selector = ArchivaItemSelector.builder( )
765 .withNamespace( "org.apache.maven" )
766 .withProjectId( "test" )
767 .withVersion( "1.0-SNAPSHOT" )
768 .withClassifier( "*" )
769 .withArtifactVersion( "1.0-20050611.112233-1" )
772 List<? extends Artifact> results = repoContent.getArtifacts( selector );
774 assertNotNull( results );
775 assertEquals( 3, results.size( ) );
777 Artifact artifact = results.stream( ).filter( a -> a.getFileName( ).equalsIgnoreCase( "test-1.0-20050611.112233-1-javadoc.jar" ) )
779 assertNotNull( artifact );
780 assertEquals( "javadoc", artifact.getClassifier( ) );
781 assertEquals( "javadoc", artifact.getType( ) );
783 artifact = results.stream( ).filter( a -> a.getFileName( ).equalsIgnoreCase( "wrong-artifactId-1.0-20050611.112233-1.jar" ) )
785 assertNotNull( artifact );
786 assertEquals( "", artifact.getClassifier( ) );
787 assertEquals( "wrong-artifactId", artifact.getId( ) );
791 public void testArtifactListWithArtifactSelector4() {
792 ItemSelector selector = ArchivaItemSelector.builder( )
793 .withNamespace( "org.apache.maven" )
794 .withProjectId( "test" )
795 .withVersion( "1.0-SNAPSHOT" )
796 .withClassifier( "" )
799 List<? extends Artifact> results = repoContent.getArtifacts( selector );
801 assertNotNull( results );
802 assertEquals( 5, results.size( ) );
804 Artifact artifact = results.stream( ).filter( a -> a.getFileName( ).equalsIgnoreCase( "test-1.0-20050611.112233-1-javadoc.jar" ) )
806 assertNotNull( artifact );
807 assertEquals( "javadoc", artifact.getClassifier( ) );
808 assertEquals( "javadoc", artifact.getType( ) );
810 artifact = results.stream( ).filter( a -> a.getFileName( ).equalsIgnoreCase( "wrong-artifactId-1.0-20050611.112233-1.jar" ) )
812 assertNotNull( artifact );
813 assertEquals( "", artifact.getClassifier( ) );
814 assertEquals( "wrong-artifactId", artifact.getId( ) );
816 artifact = results.stream( ).filter( a -> a.getFileName( ).equalsIgnoreCase( "wrong-artifactId-1.0-20050611.1122x-1.jar" ) )
818 assertNotNull( artifact );
819 assertEquals( "", artifact.getClassifier( ) );
820 assertEquals( "wrong-artifactId", artifact.getId( ) );
821 assertEquals( "", artifact.getArtifactVersion( ) );
823 artifact = results.stream( ).filter( a -> a.getFileName( ).equalsIgnoreCase( "test-1.0-20050611.1122x-1.jar" ) )
825 assertNotNull( artifact );
826 assertEquals( "", artifact.getClassifier( ) );
827 assertEquals( "test", artifact.getId( ) );
828 assertEquals( "1.0-20050611.1122x-1", artifact.getArtifactVersion( ) );
833 public void testArtifactListWithArtifactSelectorWithClassifier() {
834 ItemSelector selector = ArchivaItemSelector.builder( )
835 .withNamespace( "org.apache.maven" )
836 .withProjectId( "test" )
837 .withVersion( "1.0-SNAPSHOT" )
838 .withArtifactId( "test" )
839 .withClassifier( "javadoc" )
840 .withArtifactVersion( "1.0-20050611.112233-1" )
843 List<? extends Artifact> results = repoContent.getArtifacts( selector );
845 assertNotNull( results );
846 assertEquals( 1, results.size( ) );
848 Artifact artifact = results.stream( ).filter( a -> a.getFileName( ).equalsIgnoreCase( "test-1.0-20050611.112233-1-javadoc.jar" ) )
850 assertNotNull( artifact );
851 assertEquals( "javadoc", artifact.getClassifier( ) );
852 assertEquals( "javadoc", artifact.getType( ) );
856 public void testArtifactListWithArtifactSelectorWrongArtifact() {
857 ItemSelector selector = ArchivaItemSelector.builder( )
858 .withNamespace( "org.apache.maven" )
859 .withProjectId( "test" )
860 .withVersion( "1.0-SNAPSHOT" )
861 .withArtifactId( "wrong-artifactId" )
862 .withArtifactVersion( "1.0-20050611.112233-1" )
865 List<? extends Artifact> results = repoContent.getArtifacts( selector );
867 assertNotNull( results );
868 assertEquals( 1, results.size( ) );
870 Artifact artifact = results.stream( ).filter( a -> a.getFileName( ).equalsIgnoreCase( "wrong-artifactId-1.0-20050611.112233-1.jar" ) )
872 assertNotNull( artifact );
876 public void testArtifactListWithArtifactSelectorVersionPattern() {
877 ItemSelector selector = ArchivaItemSelector.builder( )
878 .withNamespace( "org.apache.maven" )
879 .withProjectId( "test" )
880 .withVersion( "1.0-SNAPSHOT" )
881 .withArtifactVersion( "1.0-*" )
884 List<? extends Artifact> results = repoContent.getArtifacts( selector );
886 assertNotNull( results );
887 assertEquals( 5, results.size( ) );
889 Artifact artifact = results.stream( ).filter( a -> a.getFileName( ).equalsIgnoreCase( "wrong-artifactId-1.0-20050611.112233-1.jar" ) )
891 assertNotNull( artifact );
896 public void testNewItemStreamWithNamespace1() {
897 ItemSelector selector = ArchivaItemSelector.builder( )
898 .withNamespace( "org.apache.axis2" )
901 Stream<? extends ContentItem> stream = repoContent.newItemStream( selector, false );
902 List<? extends ContentItem> result = stream.collect( Collectors.toList( ) );
903 assertEquals( 41, result.size( ) );
904 ContentItem item = result.get( 39 );
905 Version version = item.adapt( Version.class );
906 assertNotNull( version );
907 assertEquals( "1.3-SNAPSHOT", version.getId( ) );
908 Project project = result.get( 40 ).adapt( Project.class );
909 assertNotNull( project );
910 assertEquals( "axis2", project.getId( ) );
911 assertTrue( result.stream( ).anyMatch( a -> "axis2-1.3-20070725.210059-1.pom".equals( a.getAsset( ).getName( ) ) ) );
915 public void testNewItemStreamWithNamespace2() {
916 ItemSelector selector = ArchivaItemSelector.builder( )
917 .withNamespace( "org.apache.maven" )
921 Stream<? extends ContentItem> stream = repoContent.newItemStream( selector, false );
922 List<? extends ContentItem> result = stream.collect( Collectors.toList( ) );
923 assertEquals( 170, result.size( ) );
924 assertEquals( 92, result.stream( ).filter( a -> a instanceof DataItem ).count( ) );
928 public void testGetArtifactFromContentItem() {
929 ItemSelector selector = ArchivaItemSelector.builder( )
930 .withNamespace( "org.apache.maven" ).build();
931 Namespace ns = repoContent.getNamespace( selector );
932 List<? extends Artifact> artifacts = repoContent.getArtifacts( ns );
933 assertNotNull( artifacts );
934 assertEquals( 39, artifacts.size( ) );
935 List<? extends Artifact> artifacts2 = repoContent.getArtifacts( (ContentItem)ns );
936 assertArrayEquals( artifacts.toArray(), artifacts2.toArray() );
938 selector = ArchivaItemSelector.builder( )
939 .withNamespace( "org.apache.maven.shared" )
940 .withProjectId( "maven-downloader" )
942 Project project = repoContent.getProject( selector );
943 artifacts = repoContent.getArtifacts( project );
944 assertNotNull( artifacts );
945 assertEquals( 27, artifacts.size( ) );
946 artifacts2 = repoContent.getArtifacts( (ContentItem)project );
947 assertArrayEquals( artifacts.toArray(), artifacts2.toArray() );
949 selector = ArchivaItemSelector.builder( )
950 .withNamespace( "org.apache.maven.shared" )
951 .withProjectId( "maven-downloader" )
952 .withVersion( "1.1" )
954 Version version = repoContent.getVersion( selector );
955 artifacts = repoContent.getArtifacts( version );
956 assertNotNull( artifacts );
957 assertEquals( 12, artifacts.size( ) );
958 artifacts2 = repoContent.getArtifacts( (ContentItem)version );
959 assertArrayEquals( artifacts.toArray(), artifacts2.toArray() );
964 public void testGetRelatedArtifactsFromArtifact() {
965 ItemSelector selector = ArchivaItemSelector.builder( )
966 .withNamespace( "org.apache.maven.shared" )
967 .withProjectId( "maven-downloader" )
968 .withVersion( "1.1" )
969 .withExtension( "jar" )
970 .withArtifactId( "maven-downloader" ).build( );
972 Artifact artifact = repoContent.getArtifact( selector );
973 assertNotNull( artifact );
974 List<? extends Artifact> artifacts = repoContent.getArtifacts( artifact );
975 assertNotNull( artifacts );
976 assertEquals( 2, artifacts.size( ) );
981 public void testToItemFromPath() throws LayoutException
983 String path = "/org/apache/maven/shared";
984 ContentItem item = repoContent.toItem( path );
985 assertNotNull( item );
986 assertTrue( item instanceof ArchivaContentItem );
988 path = "/org/apache/maven/shared/maven-downloader";
989 item = repoContent.toItem( path );
990 assertNotNull( item );
991 assertTrue( item instanceof ArchivaContentItem );
993 path = "/org/apache/maven/shared/maven-downloader/1.1";
994 item = repoContent.toItem( path );
995 assertNotNull( item );
996 assertTrue( item instanceof ArchivaContentItem );
998 path = "/org/apache/maven/shared/maven-downloader/1.1/maven-downloader-1.1.jar";
999 item = repoContent.toItem( path );
1000 assertNotNull( item );
1001 assertTrue( item instanceof DataItem );
1006 public void testToItemFromAssetPath() throws LayoutException
1008 StorageAsset path = repoContent.getRepository().getAsset("/org/apache/maven/shared");
1009 ContentItem item = repoContent.toItem( path );
1010 assertNotNull( item );
1011 assertTrue( item instanceof ArchivaContentItem );
1013 path = repoContent.getRepository( ).getAsset( "/org/apache/maven/shared/maven-downloader" );
1014 item = repoContent.toItem( path );
1015 assertNotNull( item );
1016 assertTrue( item instanceof ArchivaContentItem );
1018 path = repoContent.getRepository( ).getAsset( "/org/apache/maven/shared/maven-downloader/1.1" );
1019 item = repoContent.toItem( path );
1020 assertNotNull( item );
1021 assertTrue( item instanceof ArchivaContentItem );
1023 path = repoContent.getRepository( ).getAsset( "/org/apache/maven/shared/maven-downloader/1.1/maven-downloader-1.1.jar" );
1024 item = repoContent.toItem( path );
1025 assertNotNull( item );
1026 assertTrue( item instanceof DataItem );
1031 public void testHasContent() throws LayoutException
1033 ItemSelector selector = ArchivaItemSelector.builder( )
1034 .withNamespace( "org.apache.maven.shared" )
1035 .withProjectId( "maven-downloader" )
1036 .withVersion( "1.1" )
1037 .withArtifactId( "maven-downloader" )
1038 .withExtension( "jar" )
1041 assertTrue( repoContent.hasContent( selector ) );
1043 selector = ArchivaItemSelector.builder( )
1044 .withNamespace( "org.apache.maven.shared" )
1045 .withProjectId( "maven-downloader" )
1046 .withVersion( "1.1" )
1047 .withArtifactId( "maven-downloader" )
1048 .withExtension( "zip" )
1051 assertFalse( repoContent.hasContent( selector ) );
1056 public void testGetItemWithNamespaceSelector() {
1057 ItemSelector selector = ArchivaItemSelector.builder( )
1058 .withNamespace( "org.apache.maven" )
1060 ContentItem item = repoContent.getItem( selector );
1061 assertNotNull( item );
1062 assertTrue( item instanceof Namespace );
1066 public void testGetItemWithProjectSelector() {
1067 ItemSelector selector = ArchivaItemSelector.builder( )
1068 .withNamespace( "org.apache.maven" )
1069 .withProjectId( "shared" )
1071 ContentItem item = repoContent.getItem( selector );
1072 assertNotNull( item );
1073 assertTrue( item instanceof Project );
1077 public void testGetItemWithVersionSelector() {
1078 ItemSelector selector = ArchivaItemSelector.builder( )
1079 .withNamespace( "org.apache.maven" )
1080 .withProjectId( "samplejar" )
1083 ContentItem item = repoContent.getItem( selector );
1084 assertNotNull( item );
1085 assertTrue( item instanceof Version );
1089 public void testGetItemWithArtifactSelector() {
1090 ItemSelector selector = ArchivaItemSelector.builder( )
1091 .withNamespace( "org.apache.maven" )
1092 .withProjectId( "samplejar" )
1094 .withArtifactId( "samplejar" )
1096 ContentItem item = repoContent.getItem( selector );
1097 assertNotNull( item );
1098 assertTrue( item instanceof Artifact );
1102 public void testGetNamespaceFromPath() throws LayoutException
1104 StorageAsset path = repoContent.getRepository( ).getAsset( "/org/apache/axis2" );
1105 Namespace ns = repoContent.getNamespaceFromPath( path );
1106 assertNotNull( ns );
1107 assertEquals( "org.apache.axis2", ns.getId( ) );
1112 public void testArtifactListWithArtifactSelectorAndRelated() {
1113 ItemSelector selector = ArchivaItemSelector.builder( )
1114 .withNamespace( "org.apache.maven" )
1115 .withProjectId( "samplejar" )
1116 .withVersion( "1.0" )
1117 .withArtifactVersion( "1.0" )
1118 .withArtifactId( "samplejar" )
1119 .withExtension( "jar" )
1120 .includeRelatedArtifacts()
1123 List<? extends Artifact> results = repoContent.getArtifacts( selector );
1125 assertNotNull( results );
1126 assertEquals( 3, results.size( ) );
1128 Artifact artifact = results.stream( ).filter( a -> a.getFileName( ).equalsIgnoreCase( "samplejar-1.0.jar" ) )
1130 assertNotNull( artifact );
1131 assertEquals( BaseArtifactTypes.MAIN, artifact.getDataType( ) );
1133 artifact = results.stream( ).filter( a -> a.getFileName( ).equalsIgnoreCase( "samplejar-1.0.jar.md5" ) )
1135 assertNotNull( artifact );
1136 assertEquals( BaseArtifactTypes.RELATED, artifact.getDataType( ) );
1137 assertEquals( "md5", artifact.getExtension( ) );
1139 artifact = results.stream( ).filter( a -> a.getFileName( ).equalsIgnoreCase( "samplejar-1.0.jar.sha1" ) )
1141 assertNotNull( artifact );
1142 assertEquals( BaseArtifactTypes.RELATED, artifact.getDataType( ) );
1143 assertEquals( "sha1", artifact.getExtension( ) );
1147 private Path copyRepository(String repoName) throws IOException, URISyntaxException
1149 Path tempDir = Files.createTempDirectory( "archiva-repocontent" );
1150 Path repoSource = Paths.get( Thread.currentThread( ).getContextClassLoader( ).getResource( "repositories/" + repoName ).toURI( ) );
1151 assertTrue( Files.exists( repoSource ) );
1152 FileUtils.copyDirectory( repoSource.toFile( ), tempDir.toFile() );
1156 private ManagedRepository createManagedRepoWithContent(String sourceRepoName) throws IOException, URISyntaxException
1158 Path repoDir = copyRepository( sourceRepoName );
1159 MavenManagedRepository repo = createRepository( sourceRepoName, sourceRepoName, repoDir );
1160 ManagedDefaultRepositoryContent deleteRepoContent = new ManagedDefaultRepositoryContent( repo, fileTypes, fileLockManager );
1161 deleteRepoContent.setMavenContentHelper( contentHelper );
1166 public void deleteNamespaceItem() throws IOException, URISyntaxException, ItemNotFoundException
1168 ManagedRepository repo = createManagedRepoWithContent( "delete-repository" );
1169 ManagedRepositoryContent myRepoContent = repo.getContent( );
1170 Path repoRoot = repo.getRoot().getFilePath( );
1171 assertTrue( Files.exists(repoRoot.resolve( "org/apache/maven" )) );
1172 ArchivaItemSelector selector = ArchivaItemSelector.builder( )
1173 .withNamespace( "org.apache.maven" ).build();
1174 ContentItem item = myRepoContent.getItem( selector );
1175 assertTrue( item instanceof Namespace );
1176 myRepoContent.deleteItem( item );
1177 assertFalse( Files.exists(repoRoot.resolve( "org/apache/maven" )) );
1178 assertTrue( Files.exists(repoRoot.resolve( "org/apache" )) );
1180 // Sub namespaces are deleted too
1181 assertTrue( Files.exists(repoRoot.resolve( "org/apache/test/samplejar" )) );
1182 assertTrue( Files.exists(repoRoot.resolve( "org/apache/test/sub/samplejar" )) );
1183 selector = ArchivaItemSelector.builder( )
1184 .withNamespace( "org.apache.test" ).build();
1185 item = myRepoContent.getItem( selector );
1186 assertTrue( item instanceof Namespace );
1187 myRepoContent.deleteItem( item );
1188 assertFalse( Files.exists(repoRoot.resolve( "org/apache/test/samplejar" )) );
1189 assertFalse( Files.exists(repoRoot.resolve( "org/apache/test/sub/samplejar" )) );
1193 public void deleteProjectItem() throws IOException, URISyntaxException, ItemNotFoundException
1195 ManagedRepository repo = createManagedRepoWithContent( "delete-repository" );
1196 ManagedRepositoryContent myRepoContent = repo.getContent( );
1197 Path repoRoot = repo.getRoot().getFilePath( );
1198 assertTrue( Files.exists(repoRoot.resolve( "org/apache/maven/A" )) );
1199 ArchivaItemSelector selector = ArchivaItemSelector.builder( )
1200 .withNamespace( "org.apache.maven" )
1201 .withProjectId( "A" ).build();
1202 ContentItem item = myRepoContent.getItem( selector );
1203 assertTrue( item instanceof Project );
1204 myRepoContent.deleteItem( item );
1205 assertTrue( Files.exists(repoRoot.resolve( "org/apache/maven" )) );
1206 assertTrue( Files.exists( repoRoot.resolve( "org/apache/maven/samplejar/1.0" ) ) );
1207 assertTrue( Files.exists( repoRoot.resolve( "org/apache/maven/samplejar/2.0" ) ) );
1208 assertFalse( Files.exists(repoRoot.resolve( "org/apache/maven/A" )) );
1210 assertTrue( Files.exists(repoRoot.resolve( "org/apache/test/samplejar" )) );
1211 assertTrue( Files.exists(repoRoot.resolve( "org/apache/test/sub/samplejar" )) );
1212 selector = ArchivaItemSelector.builder( )
1213 .withNamespace( "org.apache.test" )
1214 .withProjectId( "samplejar" ).build();
1215 item = myRepoContent.getItem( selector );
1216 assertTrue( item instanceof Project );
1217 myRepoContent.deleteItem( item );
1218 assertFalse( Files.exists(repoRoot.resolve( "org/apache/test/samplejar" )) );
1219 assertTrue( Files.exists(repoRoot.resolve( "org/apache/test/sub/samplejar" )) );
1223 public void deleteVersionItem() throws IOException, URISyntaxException, ItemNotFoundException
1225 ManagedRepository repo = createManagedRepoWithContent( "delete-repository" );
1226 ManagedRepositoryContent myRepoContent = repo.getContent( );
1227 Path repoRoot = repo.getRoot().getFilePath( );
1228 assertTrue( Files.exists(repoRoot.resolve( "org/apache/maven/A/1.0" )) );
1229 ArchivaItemSelector selector = ArchivaItemSelector.builder( )
1230 .withNamespace( "org.apache.maven" )
1231 .withProjectId( "A" )
1232 .withVersion( "1.0" ).build();
1233 ContentItem item = myRepoContent.getItem( selector );
1234 assertTrue( item instanceof Version );
1235 myRepoContent.deleteItem( item );
1236 assertTrue( Files.exists(repoRoot.resolve( "org/apache/maven/A" )) );
1237 assertTrue( Files.exists( repoRoot.resolve( "org/apache/maven/samplejar/1.0" ) ) );
1238 assertTrue( Files.exists( repoRoot.resolve( "org/apache/maven/samplejar/2.0" ) ) );
1239 assertFalse( Files.exists(repoRoot.resolve( "org/apache/maven/A/1.0" )) );
1241 assertTrue( Files.exists(repoRoot.resolve( "org/apache/test/samplejar" )) );
1242 assertTrue( Files.exists(repoRoot.resolve( "org/apache/test/sub/samplejar" )) );
1243 selector = ArchivaItemSelector.builder( )
1244 .withNamespace( "org.apache.test" )
1245 .withProjectId( "samplejar" )
1246 .withVersion( "2.0" ).build();
1247 item = myRepoContent.getItem( selector );
1248 assertTrue( item instanceof Version );
1249 myRepoContent.deleteItem( item );
1250 assertFalse( Files.exists(repoRoot.resolve( "org/apache/test/samplejar/2.0" )) );
1251 assertTrue( Files.exists(repoRoot.resolve( "org/apache/test/samplejar/1.0" )) );
1252 assertTrue( Files.exists(repoRoot.resolve( "org/apache/test/sub/samplejar/1.0" )) );
1253 assertTrue( Files.exists(repoRoot.resolve( "org/apache/test/sub/samplejar/2.0" )) );
1257 public void deleteArtifactItem() throws IOException, URISyntaxException, ItemNotFoundException
1259 ManagedRepository repo = createManagedRepoWithContent( "delete-repository" );
1260 ManagedRepositoryContent myRepoContent = repo.getContent( );
1261 Path repoRoot = repo.getRoot().getFilePath( );
1262 assertTrue( Files.exists(repoRoot.resolve( "org/apache/maven/A/1.0/A-1.0.pom" )) );
1263 assertTrue( Files.exists(repoRoot.resolve( "org/apache/maven/A/1.0/A-1.0.war" )) );
1264 ArchivaItemSelector selector = ArchivaItemSelector.builder( )
1265 .withNamespace( "org.apache.maven" )
1266 .withProjectId( "A" )
1267 .withVersion( "1.0" )
1268 .withArtifactId( "A" )
1269 .withArtifactVersion( "1.0" )
1270 .withExtension( "pom" )
1272 ContentItem item = myRepoContent.getItem( selector );
1273 assertTrue( item instanceof Artifact );
1274 myRepoContent.deleteItem( item );
1275 assertTrue( Files.exists( repoRoot.resolve( "org/apache/maven/samplejar/1.0" ) ) );
1276 assertTrue( Files.exists( repoRoot.resolve( "org/apache/maven/samplejar/2.0" ) ) );
1277 assertFalse( Files.exists(repoRoot.resolve( "org/apache/maven/A/1.0/A-1.0.pom" )) );
1278 assertTrue( Files.exists(repoRoot.resolve( "org/apache/maven/A/1.0/A-1.0.war" )) );
1281 assertTrue( Files.exists(repoRoot.resolve( "org/apache/test/samplejar/1.0/samplejar-1.0.jar" )) );
1282 assertTrue( Files.exists(repoRoot.resolve( "org/apache/test/samplejar/1.0/samplejar-1.0.jar.md5" )) );
1283 assertTrue( Files.exists(repoRoot.resolve( "org/apache/test/samplejar/1.0/samplejar-1.0.jar.sha1" )) );
1284 assertTrue( Files.exists(repoRoot.resolve( "org/apache/test/samplejar/1.0/samplejar-1.0.pom" )) );
1285 assertTrue( Files.exists(repoRoot.resolve( "org/apache/test/samplejar/1.0/samplejar-1.0-source.jar" )) );
1286 selector = ArchivaItemSelector.builder( )
1287 .withNamespace( "org.apache.test" )
1288 .withProjectId( "samplejar" )
1289 .withVersion( "1.0" )
1290 .withArtifactId( "samplejar" )
1291 .withArtifactVersion( "1.0" )
1292 .withExtension( "jar" )
1294 item = myRepoContent.getItem( selector );
1295 assertTrue( item instanceof Artifact );
1296 myRepoContent.deleteItem( item );
1297 assertFalse( Files.exists(repoRoot.resolve( "org/apache/test/samplejar/1.0/samplejar-1.0.jar" )) );
1298 assertTrue( Files.exists(repoRoot.resolve( "org/apache/test/samplejar/1.0/samplejar-1.0.jar.md5" )) );
1299 assertTrue( Files.exists(repoRoot.resolve( "org/apache/test/samplejar/1.0/samplejar-1.0.jar.sha1" )) );
1300 assertTrue( Files.exists(repoRoot.resolve( "org/apache/test/samplejar/1.0/samplejar-1.0.pom" )) );
1301 assertTrue( Files.exists(repoRoot.resolve( "org/apache/test/samplejar/1.0/samplejar-1.0-source.jar" )) );
1302 assertTrue( Files.exists(repoRoot.resolve( "org/apache/test/samplejar/2.0" )) );
1303 assertTrue( Files.exists(repoRoot.resolve( "org/apache/test/sub/samplejar/1.0" )) );
1304 assertTrue( Files.exists(repoRoot.resolve( "org/apache/test/sub/samplejar/2.0" )) );
1306 selector = ArchivaItemSelector.builder( )
1307 .withNamespace( "org.apache.test" )
1308 .withProjectId( "samplejar" )
1309 .withVersion( "1.0" )
1310 .withArtifactId( "samplejar" )
1311 .withArtifactVersion( "1.0" )
1312 .withClassifier( "source" )
1313 .withExtension( "jar" )
1315 item = myRepoContent.getItem( selector );
1316 assertTrue( item instanceof Artifact );
1317 myRepoContent.deleteItem( item );
1318 assertFalse( Files.exists(repoRoot.resolve( "org/apache/test/samplejar/1.0/samplejar-1.0.jar" )) );
1319 assertTrue( Files.exists(repoRoot.resolve( "org/apache/test/samplejar/1.0/samplejar-1.0.jar.md5" )) );
1320 assertTrue( Files.exists(repoRoot.resolve( "org/apache/test/samplejar/1.0/samplejar-1.0.jar.sha1" )) );
1321 assertTrue( Files.exists(repoRoot.resolve( "org/apache/test/samplejar/1.0/samplejar-1.0.pom" )) );
1322 assertFalse( Files.exists(repoRoot.resolve( "org/apache/test/samplejar/1.0/samplejar-1.0-source.jar" )) );
1323 assertTrue( Files.exists(repoRoot.resolve( "org/apache/test/samplejar/1.0/samplejar-1.0-source.jar.sha1" )) );
1324 assertTrue( Files.exists(repoRoot.resolve( "org/apache/test/samplejar/2.0" )) );
1325 assertTrue( Files.exists(repoRoot.resolve( "org/apache/test/sub/samplejar/1.0" )) );
1326 assertTrue( Files.exists(repoRoot.resolve( "org/apache/test/sub/samplejar/2.0" )) );
1328 selector = ArchivaItemSelector.builder( )
1329 .withNamespace( "org.apache.test" )
1330 .withProjectId( "samplejar" )
1331 .withVersion( "1.0" )
1332 .withArtifactId( "samplejar" )
1333 .withArtifactVersion( "1.0" )
1334 .withExtension( "jar.md5" )
1336 item = myRepoContent.getItem( selector );
1337 assertTrue( item instanceof Artifact );
1338 myRepoContent.deleteItem( item );
1339 assertFalse( Files.exists(repoRoot.resolve( "org/apache/test/samplejar/1.0/samplejar-1.0.jar" )) );
1340 assertFalse( Files.exists(repoRoot.resolve( "org/apache/test/samplejar/1.0/samplejar-1.0.jar.md5" )) );
1341 assertTrue( Files.exists(repoRoot.resolve( "org/apache/test/samplejar/1.0/samplejar-1.0.jar.sha1" )) );
1342 assertTrue( Files.exists(repoRoot.resolve( "org/apache/test/samplejar/1.0/samplejar-1.0.pom" )) );
1343 assertFalse( Files.exists(repoRoot.resolve( "org/apache/test/samplejar/1.0/samplejar-1.0-source.jar" )) );
1344 assertTrue( Files.exists(repoRoot.resolve( "org/apache/test/samplejar/1.0/samplejar-1.0-source.jar.sha1" )) );
1345 assertTrue( Files.exists(repoRoot.resolve( "org/apache/test/samplejar/2.0" )) );
1346 assertTrue( Files.exists(repoRoot.resolve( "org/apache/test/sub/samplejar/1.0" )) );
1347 assertTrue( Files.exists(repoRoot.resolve( "org/apache/test/sub/samplejar/2.0" )) );
1353 public void deleteItemNotFound() throws IOException, URISyntaxException, ItemNotFoundException
1355 ManagedRepository repo = createManagedRepoWithContent( "delete-repository" );
1356 ManagedRepositoryContent myRepoContent = repo.getContent( );
1357 Path repoRoot = repo.getRoot().getFilePath( );
1359 ArchivaItemSelector selector = ArchivaItemSelector.builder( )
1360 .withNamespace( "org.apache.test2" )
1363 ContentItem item = myRepoContent.getItem( selector );
1364 assertTrue( item instanceof Namespace );
1367 myRepoContent.deleteItem( item );
1368 assertTrue( "ItemNotFoundException expected for non existing namespace", false );
1369 } catch ( ItemNotFoundException e) {
1372 selector = ArchivaItemSelector.builder( )
1373 .withNamespace( "org.apache.test" )
1374 .withProjectId( "samplejar2" )
1376 item = myRepoContent.getItem( selector );
1377 assertTrue( item instanceof Project );
1380 myRepoContent.deleteItem( item );
1381 assertTrue( "ItemNotFoundException expected for non existing project", false );
1382 } catch ( ItemNotFoundException e) {
1385 selector = ArchivaItemSelector.builder( )
1386 .withNamespace( "org.apache.test" )
1387 .withProjectId( "samplejar" )
1390 item = myRepoContent.getItem( selector );
1391 assertTrue( item instanceof Version );
1394 myRepoContent.deleteItem( item );
1395 assertTrue( "ItemNotFoundException expected for non existing version", false );
1396 } catch ( ItemNotFoundException e) {
1399 selector = ArchivaItemSelector.builder( )
1400 .withNamespace( "org.apache.test" )
1401 .withProjectId( "samplejar" )
1403 .withArtifactId( "samplejar" )
1404 .withArtifactVersion( "1.0" )
1405 .withExtension( "jax" )
1407 item = myRepoContent.getItem( selector );
1408 assertTrue( item instanceof Artifact );
1411 myRepoContent.deleteItem( item );
1412 assertTrue( "ItemNotFoundException expected for non existing artifact", false );
1413 } catch ( ItemNotFoundException e) {
1420 public void testAddArtifact() throws IOException, URISyntaxException, LayoutException
1422 ManagedRepository repo = createManagedRepoWithContent( "delete-repository" );
1423 ManagedRepositoryContent myRepoContent = repo.getContent( );
1424 BaseRepositoryContentLayout layout = myRepoContent.getLayout( BaseRepositoryContentLayout.class );
1425 Path repoRoot = repo.getRoot().getFilePath( );
1427 Path tmpFile = Files.createTempFile( "archiva-mvn-repotest", "jar" );
1428 try( OutputStream outputStream = Files.newOutputStream( tmpFile ))
1430 for ( int i = 0; i < 255; i++ )
1432 outputStream.write( "test.test.test\n".getBytes( Charset.forName( "UTF-8" ) ) );
1436 Path file = repoRoot.resolve( "org/apache/maven/samplejar/2.0/samplejar-2.0.jar" );
1437 FileTime lmt = Files.getLastModifiedTime( file );
1438 ArchivaItemSelector selector = ArchivaItemSelector.builder( )
1439 .withNamespace( "org.apache.maven" )
1440 .withProjectId( "samplejar" )
1441 .withVersion( "2.0" )
1442 .withArtifactId( "samplejar" )
1443 .withArtifactVersion( "2.0" )
1444 .withExtension( "jar" )
1446 Artifact artifact = layout.getArtifact( selector );
1447 layout.addArtifact( tmpFile, artifact );
1448 FileTime lmtAfter = Files.getLastModifiedTime( file );
1449 assertNotEquals( lmtAfter, lmt );
1450 Reader ln = Files.newBufferedReader( file, Charset.forName( "UTF-8" ) );
1451 char[] content = new char[50];
1453 assertTrue( new String( content ).startsWith( "test.test.test" ) );
1455 tmpFile = Files.createTempFile( "archiva-mvn-repotest", "jar" );
1456 try( OutputStream outputStream = Files.newOutputStream( tmpFile ))
1458 for ( int i = 0; i < 255; i++ )
1460 outputStream.write( "test.test.test\n".getBytes( Charset.forName( "UTF-8" ) ) );
1463 file = repoRoot.resolve( "org/apache/maven/samplejar/2.0/samplejar-2.0.test" );
1464 assertFalse( Files.exists( file ) );
1465 assertTrue( Files.exists( tmpFile ) );
1466 selector = ArchivaItemSelector.builder( )
1467 .withNamespace( "org.apache.maven" )
1468 .withProjectId( "samplejar" )
1469 .withVersion( "2.0" )
1470 .withArtifactId( "samplejar" )
1471 .withArtifactVersion( "2.0" )
1472 .withExtension( "test" )
1474 artifact = layout.getArtifact( selector );
1475 layout.addArtifact( tmpFile, artifact );
1476 ln = Files.newBufferedReader( file, Charset.forName( "UTF-8" ) );
1478 assertTrue( new String( content ).startsWith( "test.test.test" ) );
1482 public void getExistingMetadataItem() {
1483 // org/apache/maven/some-ejb/1.0
1484 ArchivaItemSelector versionSelector = ArchivaItemSelector.builder( )
1485 .withNamespace( "org.apache.maven" )
1486 .withProjectId( "some-ejb" )
1487 .withVersion( "1.0" ).build( );
1488 Version version = repoContent.getVersion( versionSelector );
1489 DataItem metaData = repoContent.getMetadataItem( version );
1490 assertTrue( metaData.exists( ) );
1491 assertEquals( "/org/apache/maven/some-ejb/1.0/maven-metadata.xml", metaData.getAsset( ).getPath( ) );
1495 public void getNonExistingMetadataItem() {
1496 // org/apache/maven/some-ejb/1.0
1497 ArchivaItemSelector versionSelector = ArchivaItemSelector.builder( )
1498 .withNamespace( "javax.sql" )
1499 .withProjectId( "jdbc" )
1500 .withVersion( "2.0" ).build( );
1501 Version version = repoContent.getVersion( versionSelector );
1502 DataItem metaData = repoContent.getMetadataItem( version );
1503 assertFalse( metaData.exists( ) );
1504 assertEquals( "/javax/sql/jdbc/2.0/maven-metadata.xml", metaData.getAsset( ).getPath( ) );