1 package org.apache.archiva.repository.maven.content;
4 * Licensed to the Apache Software Foundation (ASF) under one
5 * or more contributor license agreements. See the NOTICE file
6 * distributed with this work for additional information
7 * regarding copyright ownership. The ASF licenses this file
8 * to you under the Apache License, Version 2.0 (the
9 * "License"); you may not use this file except in compliance
10 * with the License. You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing,
14 * software distributed under the License is distributed on an
15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16 * KIND, either express or implied. See the License for the
17 * specific language governing permissions and limitations
21 import org.apache.archiva.common.filelock.FileLockManager;
22 import org.apache.archiva.common.utils.VersionComparator;
23 import org.apache.archiva.configuration.ArchivaConfiguration;
24 import org.apache.archiva.configuration.FileType;
25 import org.apache.archiva.configuration.FileTypes;
26 import org.apache.archiva.model.ArtifactReference;
27 import org.apache.archiva.model.ProjectReference;
28 import org.apache.archiva.model.VersionedReference;
29 import org.apache.archiva.repository.EditableManagedRepository;
30 import org.apache.archiva.repository.LayoutException;
31 import org.apache.archiva.repository.ManagedRepositoryContent;
32 import org.apache.archiva.repository.RepositoryContent;
33 import org.apache.archiva.repository.content.ItemSelector;
34 import org.apache.archiva.repository.content.Project;
35 import org.apache.archiva.repository.content.Version;
36 import org.apache.archiva.repository.content.base.ArchivaItemSelector;
37 import org.apache.archiva.repository.maven.MavenManagedRepository;
38 import org.apache.archiva.repository.maven.metadata.storage.ArtifactMappingProvider;
39 import org.apache.commons.io.FileUtils;
40 import org.junit.Before;
41 import org.junit.Test;
43 import javax.inject.Inject;
44 import javax.inject.Named;
45 import java.io.IOException;
46 import java.net.URISyntaxException;
47 import java.nio.file.Files;
48 import java.nio.file.Path;
49 import java.nio.file.Paths;
50 import java.util.ArrayList;
51 import java.util.Arrays;
52 import java.util.Collections;
53 import java.util.List;
55 import java.util.stream.Collectors;
57 import static org.junit.Assert.*;
60 * ManagedDefaultRepositoryContentTest
62 public class ManagedDefaultRepositoryContentTest
63 extends AbstractManagedRepositoryContentTest
65 private ManagedDefaultRepositoryContent repoContent;
71 @Named ( "archivaConfiguration#default" )
72 ArchivaConfiguration archivaConfiguration;
75 List<? extends ArtifactMappingProvider> artifactMappingProviders;
78 MavenContentHelper contentHelper;
81 FileLockManager fileLockManager;
83 private Path getRepositoryPath(String repoName) {
86 return Paths.get( Thread.currentThread( ).getContextClassLoader( ).getResource( "repositories/" + repoName ).toURI( ) );
88 catch ( URISyntaxException e )
90 throw new RuntimeException( "Could not resolve repository path " + e.getMessage( ), e );
98 Path repoDir = getRepositoryPath( "default-repository" );
100 MavenManagedRepository repository = createRepository( "testRepo", "Unit Test Repo", repoDir );
102 FileType fileType = archivaConfiguration.getConfiguration().getRepositoryScanning().getFileTypes().get( 0 );
103 fileType.addPattern( "**/*.xml" );
104 assertEquals( FileTypes.ARTIFACTS, fileType.getId() );
106 fileTypes.afterConfigurationChange( null, "fileType", null );
108 repoContent = new ManagedDefaultRepositoryContent(repository, artifactMappingProviders, fileTypes, fileLockManager);
109 repoContent.setMavenContentHelper( contentHelper );
111 //repoContent = (ManagedRepositoryContent) lookup( ManagedRepositoryContent.class, "default" );
115 public void testGetVersionsBadArtifact()
118 assertGetVersions( "bad_artifact", Collections.emptyList() );
122 public void testGetVersionsMissingMultipleVersions()
125 assertGetVersions( "missing_metadata_b", Arrays.asList( "1.0", "1.0.1", "2.0", "2.0.1", "2.0-20070821-dev" ) );
129 public void testGetVersionsSimple()
132 assertVersions( "proxied_multi", "2.1", new String[]{ "2.1" } );
136 public void testGetVersionsSimpleYetIncomplete()
139 assertGetVersions( "incomplete_metadata_a", Collections.singletonList( "1.0" ) );
143 public void testGetVersionsSimpleYetMissing()
146 assertGetVersions( "missing_metadata_a", Collections.singletonList( "1.0" ) );
150 public void testGetVersionsSnapshotA()
153 assertVersions( "snap_shots_a", "1.0-alpha-11-SNAPSHOT",
154 new String[]{ "1.0-alpha-11-SNAPSHOT", "1.0-alpha-11-20070221.194724-2",
155 "1.0-alpha-11-20070302.212723-3", "1.0-alpha-11-20070303.152828-4",
156 "1.0-alpha-11-20070305.215149-5", "1.0-alpha-11-20070307.170909-6",
157 "1.0-alpha-11-20070314.211405-9", "1.0-alpha-11-20070316.175232-11" } );
161 public void testToMetadataPathFromProjectReference()
163 ProjectReference reference = new ProjectReference();
164 reference.setGroupId( "com.foo" );
165 reference.setArtifactId( "foo-tool" );
167 assertEquals( "com/foo/foo-tool/maven-metadata.xml", repoContent.toMetadataPath( reference ) );
171 public void testToMetadataPathFromVersionReference()
173 VersionedReference reference = new VersionedReference();
174 reference.setGroupId( "com.foo" );
175 reference.setArtifactId( "foo-tool" );
176 reference.setVersion( "1.0" );
178 assertEquals( "com/foo/foo-tool/1.0/maven-metadata.xml", repoContent.toMetadataPath( reference ) );
183 public void testToPathOnNullArtifactReference()
187 ArtifactReference reference = null;
188 repoContent.toPath( reference );
189 fail( "Should have failed due to null artifact reference." );
191 catch ( IllegalArgumentException e )
198 public void testExcludeMetadataFile()
201 assertVersions( "include_xml", "1.0", new String[]{ "1.0" } );
204 private void assertGetVersions( String artifactId, List<String> expectedVersions )
207 ProjectReference reference = new ProjectReference();
208 reference.setGroupId( "org.apache.archiva.metadata.tests" );
209 reference.setArtifactId( artifactId );
211 // Use the test metadata-repository, which is already setup for
212 // These kind of version tests.
213 Path repoDir = getRepositoryPath( "metadata-repository" );
214 (( EditableManagedRepository)repoContent.getRepository()).setLocation( repoDir.toAbsolutePath().toUri() );
216 // Request the versions.
217 Set<String> testedVersionSet = repoContent.getVersions( reference );
219 // Sort the list (for asserts)
220 VersionComparator comparator = new VersionComparator( );
221 List<String> testedVersions = new ArrayList<>();
222 testedVersions.addAll( testedVersionSet );
223 Collections.sort( testedVersions, comparator );
225 // Test the expected array of versions, to the actual tested versions
226 assertEquals( "available versions", expectedVersions, testedVersions );
228 ItemSelector selector = ArchivaItemSelector.builder( )
229 .withNamespace( "org.apache.archiva.metadata.tests" )
230 .withProjectId( artifactId )
232 Project project = repoContent.getProject( selector );
233 assertNotNull( project );
234 List<String> versions = repoContent.getVersions( project ).stream().map(v -> v.getVersion()).sorted( comparator ).collect( Collectors.toList());
235 assertArrayEquals( expectedVersions.toArray(), versions.toArray( ) );
238 private void assertVersions( String artifactId, String version, String[] expectedVersions )
241 VersionedReference reference = new VersionedReference();
242 reference.setGroupId( "org.apache.archiva.metadata.tests" );
243 reference.setArtifactId( artifactId );
244 reference.setVersion( version );
246 // Use the test metadata-repository, which is already setup for
247 // These kind of version tests.
248 Path repoDir = getRepositoryPath( "metadata-repository" );
249 ((EditableManagedRepository)repoContent.getRepository()).setLocation( repoDir.toAbsolutePath().toUri() );
251 // Request the versions.
252 Set<String> testedVersionSet = repoContent.getVersions( reference );
254 // Sort the list (for asserts later)
255 final VersionComparator comparator = new VersionComparator( );
256 List<String> testedVersions = new ArrayList<>();
257 testedVersions.addAll( testedVersionSet );
258 Collections.sort( testedVersions, comparator );
260 // Test the expected array of versions, to the actual tested versions
261 assertEquals( "Assert Versions: length/size", expectedVersions.length, testedVersions.size() );
263 for ( int i = 0; i < expectedVersions.length; i++ )
265 String actualVersion = testedVersions.get( i );
266 assertEquals( "Versions[" + i + "]", expectedVersions[i], actualVersion );
270 ItemSelector selector = ArchivaItemSelector.builder( )
271 .withNamespace( "org.apache.archiva.metadata.tests" )
272 .withProjectId( artifactId )
273 .withVersion( version )
275 List<String> versions = repoContent.getVersions( selector ).stream()
276 .map(v -> v.getVersion()).sorted( comparator ).collect( Collectors.toList());
277 assertArrayEquals( expectedVersions, versions.toArray( ) );
284 protected ArtifactReference toArtifactReference( String path )
285 throws LayoutException
287 return repoContent.toArtifactReference( path );
291 protected ItemSelector toItemSelector( String path ) throws LayoutException
293 return repoContent.toItemSelector( path );
297 protected String toPath( ArtifactReference reference )
299 return repoContent.toPath( reference );
303 protected String toPath( ItemSelector selector ) {
304 return repoContent.toPath( selector );
308 protected ManagedRepositoryContent getManaged( )
314 protected RepositoryContent getContent( )
319 private Path setupRepoCopy( String source, String target) throws IOException
321 Path defaultRepo = getRepositoryPath( source );
322 Path newRepo = defaultRepo.getParent( ).resolve( target );
323 FileUtils.copyDirectory( defaultRepo.toFile( ), newRepo.toFile( ) );
325 MavenManagedRepository repository = createRepository( "testRepo", "Unit Test Repo", newRepo );
327 FileType fileType = archivaConfiguration.getConfiguration().getRepositoryScanning().getFileTypes().get( 0 );
328 fileType.addPattern( "**/*.xml" );
329 assertEquals( FileTypes.ARTIFACTS, fileType.getId() );
331 fileTypes.afterConfigurationChange( null, "fileType", null );
333 repoContent = new ManagedDefaultRepositoryContent(repository, artifactMappingProviders, fileTypes, fileLockManager);
339 public void testDeleteArtifactWithType() throws IOException, org.apache.archiva.repository.ContentNotFoundException, org.apache.archiva.repository.ContentAccessException
341 Path deleteRepo = setupRepoCopy( "delete-repository", "delete-repository-2" );
342 assertTrue( Files.exists( deleteRepo.resolve( "org/apache/maven/samplejar/1.0/samplejar-1.0-source.jar" ) ) );
343 assertTrue( Files.exists( deleteRepo.resolve( "org/apache/maven/samplejar/1.0/samplejar-1.0.jar" ) ) );
344 assertTrue( Files.exists( deleteRepo.resolve( "org/apache/maven/samplejar/1.0/samplejar-1.0.jar.md5" ) ) );
345 assertTrue( Files.exists( deleteRepo.resolve( "org/apache/maven/samplejar/1.0/samplejar-1.0.jar.sha1" ) ) );
346 assertTrue( Files.exists( deleteRepo.resolve( "org/apache/maven/samplejar/1.0/samplejar-1.0.pom" ) ) );
348 ArtifactReference ref = new ArtifactReference( );
349 ref.setGroupId( "org.apache.maven" );
350 ref.setArtifactId( "samplejar" );
351 ref.setVersion( "1.0" );
352 ref.setType( "jar" );
354 repoContent.deleteArtifact( ref );
356 assertTrue( Files.exists( deleteRepo.resolve( "org/apache/maven/samplejar/1.0/samplejar-1.0-source.jar" ) ) );
357 assertFalse( Files.exists( deleteRepo.resolve( "org/apache/maven/samplejar/1.0/samplejar-1.0.jar" ) ) );
358 assertTrue( Files.exists( deleteRepo.resolve( "org/apache/maven/samplejar/1.0/samplejar-1.0.jar.md5" ) ) );
359 assertTrue( Files.exists( deleteRepo.resolve( "org/apache/maven/samplejar/1.0/samplejar-1.0.jar.sha1" ) ) );
360 assertTrue( Files.exists( deleteRepo.resolve( "org/apache/maven/samplejar/1.0/samplejar-1.0.pom" ) ) );
367 public void testDeleteArtifactWithClassifier() throws IOException, org.apache.archiva.repository.ContentNotFoundException, org.apache.archiva.repository.ContentAccessException
369 Path deleteRepo = setupRepoCopy( "default-repository", "default-repository-2" );
370 assertTrue( Files.exists( deleteRepo.resolve( "org/apache/maven/samplejar/1.0/samplejar-1.0-source.jar" ) ) );
371 assertTrue( Files.exists( deleteRepo.resolve( "org/apache/maven/samplejar/1.0/samplejar-1.0-source.jar.sha1" ) ) );
372 assertTrue( Files.exists( deleteRepo.resolve( "org/apache/maven/samplejar/1.0/samplejar-1.0.jar" ) ) );
373 assertTrue( Files.exists( deleteRepo.resolve( "org/apache/maven/samplejar/1.0/samplejar-1.0.jar.md5" ) ) );
374 assertTrue( Files.exists( deleteRepo.resolve( "org/apache/maven/samplejar/1.0/samplejar-1.0.jar.sha1" ) ) );
375 assertTrue( Files.exists( deleteRepo.resolve( "org/apache/maven/samplejar/1.0/samplejar-1.0.pom" ) ) );
377 ArtifactReference ref = new ArtifactReference( );
378 ref.setGroupId( "org.apache.maven" );
379 ref.setArtifactId( "samplejar" );
380 ref.setVersion( "1.0" );
381 ref.setClassifier( "source" );
382 ref.setType( "jar" );
384 repoContent.deleteArtifact( ref );
386 assertFalse( Files.exists( deleteRepo.resolve( "org/apache/maven/samplejar/1.0/samplejar-1.0-source.jar" ) ) );
387 assertTrue( Files.exists( deleteRepo.resolve( "org/apache/maven/samplejar/1.0/samplejar-1.0-source.jar.sha1" ) ) );
388 assertTrue( Files.exists( deleteRepo.resolve( "org/apache/maven/samplejar/1.0/samplejar-1.0.jar" ) ) );
389 assertTrue( Files.exists( deleteRepo.resolve( "org/apache/maven/samplejar/1.0/samplejar-1.0.jar.md5" ) ) );
390 assertTrue( Files.exists( deleteRepo.resolve( "org/apache/maven/samplejar/1.0/samplejar-1.0.jar.sha1" ) ) );
391 assertTrue( Files.exists( deleteRepo.resolve( "org/apache/maven/samplejar/1.0/samplejar-1.0.pom" ) ) );
396 public void testDeleteArtifactWithoutType() throws IOException, org.apache.archiva.repository.ContentNotFoundException, org.apache.archiva.repository.ContentAccessException
398 Path deleteRepo = setupRepoCopy( "default-repository", "default-repository-2" );
399 assertTrue( Files.exists( deleteRepo.resolve( "org/apache/maven/samplejar/1.0/samplejar-1.0-source.jar" ) ) );
400 assertTrue( Files.exists( deleteRepo.resolve( "org/apache/maven/samplejar/1.0/samplejar-1.0-source.jar.sha1" ) ) );
401 assertTrue( Files.exists( deleteRepo.resolve( "org/apache/maven/samplejar/1.0/samplejar-1.0.jar" ) ) );
402 assertTrue( Files.exists( deleteRepo.resolve( "org/apache/maven/samplejar/1.0/samplejar-1.0.jar.md5" ) ) );
403 assertTrue( Files.exists( deleteRepo.resolve( "org/apache/maven/samplejar/1.0/samplejar-1.0.jar.sha1" ) ) );
404 assertTrue( Files.exists( deleteRepo.resolve( "org/apache/maven/samplejar/1.0/samplejar-1.0.pom" ) ) );
406 ArtifactReference ref = new ArtifactReference( );
407 ref.setGroupId( "org.apache.maven" );
408 ref.setArtifactId( "samplejar" );
409 ref.setVersion( "1.0" );
411 repoContent.deleteArtifact( ref );
413 assertFalse( Files.exists( deleteRepo.resolve( "org/apache/maven/samplejar/1.0" ) ) );
414 assertTrue( Files.exists( deleteRepo.resolve( "org/apache/maven/samplejar" ) ) );
420 public void testDeleteVersion() throws IOException, org.apache.archiva.repository.ContentNotFoundException, org.apache.archiva.repository.ContentAccessException
422 Path deleteRepo = setupRepoCopy( "delete-repository", "delete-repository-2" );
423 assertTrue( Files.exists( deleteRepo.resolve( "org/apache/maven/samplejar/1.0/samplejar-1.0-source.jar" ) ) );
424 assertTrue( Files.exists( deleteRepo.resolve( "org/apache/maven/samplejar/1.0/samplejar-1.0.jar" ) ) );
425 assertTrue( Files.exists( deleteRepo.resolve( "org/apache/maven/samplejar/1.0/samplejar-1.0.jar.md5" ) ) );
426 assertTrue( Files.exists( deleteRepo.resolve( "org/apache/maven/samplejar/1.0/samplejar-1.0.jar.sha1" ) ) );
427 assertTrue( Files.exists( deleteRepo.resolve( "org/apache/maven/samplejar/1.0/samplejar-1.0.pom" ) ) );
429 VersionedReference ref = new VersionedReference( ).groupId( "org.apache.maven" ).artifactId( "samplejar" ).version( "1.0" );
431 repoContent.deleteVersion( ref );
433 assertFalse( Files.exists( deleteRepo.resolve( "org/apache/maven/samplejar/1.0" ) ) );
438 public void testDeleteProject() throws IOException, org.apache.archiva.repository.ContentNotFoundException, org.apache.archiva.repository.ContentAccessException
440 Path deleteRepo = setupRepoCopy( "delete-repository", "delete-repository-2" );
441 assertTrue( Files.exists( deleteRepo.resolve( "org/apache/maven/samplejar/1.0/samplejar-1.0-source.jar" ) ) );
442 assertTrue( Files.exists( deleteRepo.resolve( "org/apache/maven/samplejar/1.0/samplejar-1.0.jar" ) ) );
443 assertTrue( Files.exists( deleteRepo.resolve( "org/apache/maven/samplejar/1.0/samplejar-1.0.jar.md5" ) ) );
444 assertTrue( Files.exists( deleteRepo.resolve( "org/apache/maven/samplejar/1.0/samplejar-1.0.jar.sha1" ) ) );
445 assertTrue( Files.exists( deleteRepo.resolve( "org/apache/maven/samplejar/1.0/samplejar-1.0.pom" ) ) );
447 ProjectReference ref = new ProjectReference( ).groupId( "org.apache.maven" ).artifactId( "samplejar" );
449 repoContent.deleteProject( ref );
451 assertFalse( Files.exists( deleteRepo.resolve( "org/apache/maven/samplejar/1.0" ) ) );