1 package org.apache.archiva.metadata.repository.storage.maven2;
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
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 * KIND, either express or implied. See the License for the
18 * specific language governing permissions and limitations
22 import org.apache.archiva.common.filelock.FileLockManager;
23 import org.apache.archiva.common.utils.VersionComparator;
24 import org.apache.archiva.configuration.ArchivaConfiguration;
25 import org.apache.archiva.configuration.FileType;
26 import org.apache.archiva.configuration.FileTypes;
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.LayoutException;
32 import org.apache.archiva.repository.content.maven2.ManagedDefaultRepositoryContent;
33 import org.apache.archiva.repository.maven2.MavenManagedRepository;
34 import org.apache.commons.io.FileUtils;
35 import org.junit.Before;
36 import org.junit.Test;
38 import javax.inject.Inject;
39 import javax.inject.Named;
40 import java.io.IOException;
41 import java.net.URISyntaxException;
42 import java.nio.file.Files;
43 import java.nio.file.Path;
44 import java.nio.file.Paths;
45 import java.util.ArrayList;
46 import java.util.Arrays;
47 import java.util.Collections;
48 import java.util.List;
51 import static org.junit.Assert.*;
54 * ManagedDefaultRepositoryContentTest
56 public class ManagedDefaultRepositoryContentTest
57 extends AbstractDefaultRepositoryContentTestCase
59 private ManagedDefaultRepositoryContent repoContent;
65 @Named ( "archivaConfiguration#default" )
66 ArchivaConfiguration archivaConfiguration;
69 List<? extends ArtifactMappingProvider> artifactMappingProviders;
72 FileLockManager fileLockManager;
74 private Path getRepositoryPath(String repoName) {
77 return Paths.get( Thread.currentThread( ).getContextClassLoader( ).getResource( "repositories/" + repoName ).toURI( ) );
79 catch ( URISyntaxException e )
81 throw new RuntimeException( "Could not resolve repository path " + e.getMessage( ), e );
89 Path repoDir = getRepositoryPath( "default-repository" );
91 MavenManagedRepository repository = createRepository( "testRepo", "Unit Test Repo", repoDir );
93 FileType fileType = archivaConfiguration.getConfiguration().getRepositoryScanning().getFileTypes().get( 0 );
94 fileType.addPattern( "**/*.xml" );
95 assertEquals( FileTypes.ARTIFACTS, fileType.getId() );
97 fileTypes.afterConfigurationChange( null, "fileType", null );
99 repoContent = new ManagedDefaultRepositoryContent(repository, artifactMappingProviders, fileTypes, fileLockManager);
100 //repoContent = (ManagedRepositoryContent) lookup( ManagedRepositoryContent.class, "default" );
104 public void testGetVersionsBadArtifact()
107 assertGetVersions( "bad_artifact", Collections.emptyList() );
111 public void testGetVersionsMissingMultipleVersions()
114 assertGetVersions( "missing_metadata_b", Arrays.asList( "1.0", "1.0.1", "2.0", "2.0.1", "2.0-20070821-dev" ) );
118 public void testGetVersionsSimple()
121 assertVersions( "proxied_multi", "2.1", new String[]{ "2.1" } );
125 public void testGetVersionsSimpleYetIncomplete()
128 assertGetVersions( "incomplete_metadata_a", Collections.singletonList( "1.0" ) );
132 public void testGetVersionsSimpleYetMissing()
135 assertGetVersions( "missing_metadata_a", Collections.singletonList( "1.0" ) );
139 public void testGetVersionsSnapshotA()
142 assertVersions( "snap_shots_a", "1.0-alpha-11-SNAPSHOT",
143 new String[]{ "1.0-alpha-11-SNAPSHOT", "1.0-alpha-11-20070221.194724-2",
144 "1.0-alpha-11-20070302.212723-3", "1.0-alpha-11-20070303.152828-4",
145 "1.0-alpha-11-20070305.215149-5", "1.0-alpha-11-20070307.170909-6",
146 "1.0-alpha-11-20070314.211405-9", "1.0-alpha-11-20070316.175232-11" } );
150 public void testToMetadataPathFromProjectReference()
152 ProjectReference reference = new ProjectReference();
153 reference.setGroupId( "com.foo" );
154 reference.setArtifactId( "foo-tool" );
156 assertEquals( "com/foo/foo-tool/maven-metadata.xml", repoContent.toMetadataPath( reference ) );
160 public void testToMetadataPathFromVersionReference()
162 VersionedReference reference = new VersionedReference();
163 reference.setGroupId( "com.foo" );
164 reference.setArtifactId( "foo-tool" );
165 reference.setVersion( "1.0" );
167 assertEquals( "com/foo/foo-tool/1.0/maven-metadata.xml", repoContent.toMetadataPath( reference ) );
172 public void testToPathOnNullArtifactReference()
176 ArtifactReference reference = null;
177 repoContent.toPath( reference );
178 fail( "Should have failed due to null artifact reference." );
180 catch ( IllegalArgumentException e )
187 public void testExcludeMetadataFile()
190 assertVersions( "include_xml", "1.0", new String[]{ "1.0" } );
193 private void assertGetVersions( String artifactId, List<String> expectedVersions )
196 ProjectReference reference = new ProjectReference();
197 reference.setGroupId( "org.apache.archiva.metadata.tests" );
198 reference.setArtifactId( artifactId );
200 // Use the test metadata-repository, which is already setup for
201 // These kind of version tests.
202 Path repoDir = getRepositoryPath( "metadata-repository" );
203 (( EditableManagedRepository)repoContent.getRepository()).setLocation( repoDir.toAbsolutePath().toUri() );
205 // Request the versions.
206 Set<String> testedVersionSet = repoContent.getVersions( reference );
208 // Sort the list (for asserts)
209 List<String> testedVersions = new ArrayList<>();
210 testedVersions.addAll( testedVersionSet );
211 Collections.sort( testedVersions, new VersionComparator() );
213 // Test the expected array of versions, to the actual tested versions
214 assertEquals( "available versions", expectedVersions, testedVersions );
217 private void assertVersions( String artifactId, String version, String[] expectedVersions )
220 VersionedReference reference = new VersionedReference();
221 reference.setGroupId( "org.apache.archiva.metadata.tests" );
222 reference.setArtifactId( artifactId );
223 reference.setVersion( version );
225 // Use the test metadata-repository, which is already setup for
226 // These kind of version tests.
227 Path repoDir = getRepositoryPath( "metadata-repository" );
228 ((EditableManagedRepository)repoContent.getRepository()).setLocation( repoDir.toAbsolutePath().toUri() );
230 // Request the versions.
231 Set<String> testedVersionSet = repoContent.getVersions( reference );
233 // Sort the list (for asserts later)
234 List<String> testedVersions = new ArrayList<>();
235 testedVersions.addAll( testedVersionSet );
236 Collections.sort( testedVersions, new VersionComparator() );
238 // Test the expected array of versions, to the actual tested versions
239 assertEquals( "Assert Versions: length/size", expectedVersions.length, testedVersions.size() );
241 for ( int i = 0; i < expectedVersions.length; i++ )
243 String actualVersion = testedVersions.get( i );
244 assertEquals( "Versions[" + i + "]", expectedVersions[i], actualVersion );
250 protected ArtifactReference toArtifactReference( String path )
251 throws LayoutException
253 return repoContent.toArtifactReference( path );
257 protected String toPath( ArtifactReference reference )
259 return repoContent.toPath( reference );
262 private Path setupRepoCopy( String source, String target) throws IOException
264 Path defaultRepo = getRepositoryPath( source );
265 Path newRepo = defaultRepo.getParent( ).resolve( target );
266 FileUtils.copyDirectory( defaultRepo.toFile( ), newRepo.toFile( ) );
268 MavenManagedRepository repository = createRepository( "testRepo", "Unit Test Repo", newRepo );
270 FileType fileType = archivaConfiguration.getConfiguration().getRepositoryScanning().getFileTypes().get( 0 );
271 fileType.addPattern( "**/*.xml" );
272 assertEquals( FileTypes.ARTIFACTS, fileType.getId() );
274 fileTypes.afterConfigurationChange( null, "fileType", null );
276 repoContent = new ManagedDefaultRepositoryContent(repository, artifactMappingProviders, fileTypes, fileLockManager);
282 public void testDeleteArtifactWithType() throws IOException
284 Path deleteRepo = setupRepoCopy( "delete-repository", "delete-repository-2" );
285 assertTrue( Files.exists( deleteRepo.resolve( "org/apache/maven/samplejar/1.0/samplejar-1.0-source.jar" ) ) );
286 assertTrue( Files.exists( deleteRepo.resolve( "org/apache/maven/samplejar/1.0/samplejar-1.0.jar" ) ) );
287 assertTrue( Files.exists( deleteRepo.resolve( "org/apache/maven/samplejar/1.0/samplejar-1.0.jar.md5" ) ) );
288 assertTrue( Files.exists( deleteRepo.resolve( "org/apache/maven/samplejar/1.0/samplejar-1.0.jar.sha1" ) ) );
289 assertTrue( Files.exists( deleteRepo.resolve( "org/apache/maven/samplejar/1.0/samplejar-1.0.pom" ) ) );
291 ArtifactReference ref = new ArtifactReference( );
292 ref.setGroupId( "org.apache.maven" );
293 ref.setArtifactId( "samplejar" );
294 ref.setVersion( "1.0" );
295 ref.setType( "jar" );
297 repoContent.deleteArtifact( ref );
299 assertTrue( Files.exists( deleteRepo.resolve( "org/apache/maven/samplejar/1.0/samplejar-1.0-source.jar" ) ) );
300 assertFalse( Files.exists( deleteRepo.resolve( "org/apache/maven/samplejar/1.0/samplejar-1.0.jar" ) ) );
301 assertFalse( Files.exists( deleteRepo.resolve( "org/apache/maven/samplejar/1.0/samplejar-1.0.jar.md5" ) ) );
302 assertFalse( Files.exists( deleteRepo.resolve( "org/apache/maven/samplejar/1.0/samplejar-1.0.jar.sha1" ) ) );
303 assertTrue( Files.exists( deleteRepo.resolve( "org/apache/maven/samplejar/1.0/samplejar-1.0.pom" ) ) );
310 public void testDeleteArtifactWithClassifier() throws IOException
312 Path deleteRepo = setupRepoCopy( "default-repository", "default-repository-2" );
313 assertTrue( Files.exists( deleteRepo.resolve( "org/apache/maven/samplejar/1.0/samplejar-1.0-source.jar" ) ) );
314 assertTrue( Files.exists( deleteRepo.resolve( "org/apache/maven/samplejar/1.0/samplejar-1.0-source.jar.sha1" ) ) );
315 assertTrue( Files.exists( deleteRepo.resolve( "org/apache/maven/samplejar/1.0/samplejar-1.0.jar" ) ) );
316 assertTrue( Files.exists( deleteRepo.resolve( "org/apache/maven/samplejar/1.0/samplejar-1.0.jar.md5" ) ) );
317 assertTrue( Files.exists( deleteRepo.resolve( "org/apache/maven/samplejar/1.0/samplejar-1.0.jar.sha1" ) ) );
318 assertTrue( Files.exists( deleteRepo.resolve( "org/apache/maven/samplejar/1.0/samplejar-1.0.pom" ) ) );
320 ArtifactReference ref = new ArtifactReference( );
321 ref.setGroupId( "org.apache.maven" );
322 ref.setArtifactId( "samplejar" );
323 ref.setVersion( "1.0" );
324 ref.setClassifier( "source" );
325 ref.setType( "jar" );
327 repoContent.deleteArtifact( ref );
329 assertFalse( Files.exists( deleteRepo.resolve( "org/apache/maven/samplejar/1.0/samplejar-1.0-source.jar" ) ) );
330 assertFalse( Files.exists( deleteRepo.resolve( "org/apache/maven/samplejar/1.0/samplejar-1.0-source.jar.sha1" ) ) );
331 assertTrue( Files.exists( deleteRepo.resolve( "org/apache/maven/samplejar/1.0/samplejar-1.0.jar" ) ) );
332 assertTrue( Files.exists( deleteRepo.resolve( "org/apache/maven/samplejar/1.0/samplejar-1.0.jar.md5" ) ) );
333 assertTrue( Files.exists( deleteRepo.resolve( "org/apache/maven/samplejar/1.0/samplejar-1.0.jar.sha1" ) ) );
334 assertTrue( Files.exists( deleteRepo.resolve( "org/apache/maven/samplejar/1.0/samplejar-1.0.pom" ) ) );
339 public void testDeleteArtifactWithoutType() throws IOException
341 Path deleteRepo = setupRepoCopy( "default-repository", "default-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-source.jar.sha1" ) ) );
344 assertTrue( Files.exists( deleteRepo.resolve( "org/apache/maven/samplejar/1.0/samplejar-1.0.jar" ) ) );
345 assertTrue( Files.exists( deleteRepo.resolve( "org/apache/maven/samplejar/1.0/samplejar-1.0.jar.md5" ) ) );
346 assertTrue( Files.exists( deleteRepo.resolve( "org/apache/maven/samplejar/1.0/samplejar-1.0.jar.sha1" ) ) );
347 assertTrue( Files.exists( deleteRepo.resolve( "org/apache/maven/samplejar/1.0/samplejar-1.0.pom" ) ) );
349 ArtifactReference ref = new ArtifactReference( );
350 ref.setGroupId( "org.apache.maven" );
351 ref.setArtifactId( "samplejar" );
352 ref.setVersion( "1.0" );
354 repoContent.deleteArtifact( ref );
356 assertFalse( Files.exists( deleteRepo.resolve( "org/apache/maven/samplejar/1.0" ) ) );
357 assertTrue( Files.exists( deleteRepo.resolve( "org/apache/maven/samplejar" ) ) );