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.junit.Before;
35 import org.junit.Test;
37 import javax.inject.Inject;
38 import javax.inject.Named;
39 import java.net.URISyntaxException;
40 import java.nio.file.Path;
41 import java.nio.file.Paths;
42 import java.util.ArrayList;
43 import java.util.Arrays;
44 import java.util.Collections;
45 import java.util.List;
48 import static org.junit.Assert.assertEquals;
49 import static org.junit.Assert.fail;
52 * ManagedDefaultRepositoryContentTest
54 public class ManagedDefaultRepositoryContentTest
55 extends AbstractDefaultRepositoryContentTestCase
57 private ManagedDefaultRepositoryContent repoContent;
63 @Named ( "archivaConfiguration#default" )
64 ArchivaConfiguration archivaConfiguration;
67 List<? extends ArtifactMappingProvider> artifactMappingProviders;
70 FileLockManager fileLockManager;
72 private Path getRepositoryPath(String repoName) {
75 return Paths.get( Thread.currentThread( ).getContextClassLoader( ).getResource( "repositories/" + repoName ).toURI( ) );
77 catch ( URISyntaxException e )
79 throw new RuntimeException( "Could not resolve repository path " + e.getMessage( ), e );
87 Path repoDir = getRepositoryPath( "default-repository" );
89 MavenManagedRepository repository = createRepository( "testRepo", "Unit Test Repo", repoDir );
91 FileType fileType = archivaConfiguration.getConfiguration().getRepositoryScanning().getFileTypes().get( 0 );
92 fileType.addPattern( "**/*.xml" );
93 assertEquals( FileTypes.ARTIFACTS, fileType.getId() );
95 fileTypes.afterConfigurationChange( null, "fileType", null );
97 repoContent = new ManagedDefaultRepositoryContent(repository, artifactMappingProviders, fileTypes, fileLockManager);
98 //repoContent = (ManagedRepositoryContent) lookup( ManagedRepositoryContent.class, "default" );
102 public void testGetVersionsBadArtifact()
105 assertGetVersions( "bad_artifact", Collections.emptyList() );
109 public void testGetVersionsMissingMultipleVersions()
112 assertGetVersions( "missing_metadata_b", Arrays.asList( "1.0", "1.0.1", "2.0", "2.0.1", "2.0-20070821-dev" ) );
116 public void testGetVersionsSimple()
119 assertVersions( "proxied_multi", "2.1", new String[]{ "2.1" } );
123 public void testGetVersionsSimpleYetIncomplete()
126 assertGetVersions( "incomplete_metadata_a", Collections.singletonList( "1.0" ) );
130 public void testGetVersionsSimpleYetMissing()
133 assertGetVersions( "missing_metadata_a", Collections.singletonList( "1.0" ) );
137 public void testGetVersionsSnapshotA()
140 assertVersions( "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" } );
148 public void testToMetadataPathFromProjectReference()
150 ProjectReference reference = new ProjectReference();
151 reference.setGroupId( "com.foo" );
152 reference.setArtifactId( "foo-tool" );
154 assertEquals( "com/foo/foo-tool/maven-metadata.xml", repoContent.toMetadataPath( reference ) );
158 public void testToMetadataPathFromVersionReference()
160 VersionedReference reference = new VersionedReference();
161 reference.setGroupId( "com.foo" );
162 reference.setArtifactId( "foo-tool" );
163 reference.setVersion( "1.0" );
165 assertEquals( "com/foo/foo-tool/1.0/maven-metadata.xml", repoContent.toMetadataPath( reference ) );
170 public void testToPathOnNullArtifactReference()
174 ArtifactReference reference = null;
175 repoContent.toPath( reference );
176 fail( "Should have failed due to null artifact reference." );
178 catch ( IllegalArgumentException e )
185 public void testExcludeMetadataFile()
188 assertVersions( "include_xml", "1.0", new String[]{ "1.0" } );
191 private void assertGetVersions( String artifactId, List<String> expectedVersions )
194 ProjectReference reference = new ProjectReference();
195 reference.setGroupId( "org.apache.archiva.metadata.tests" );
196 reference.setArtifactId( artifactId );
198 // Use the test metadata-repository, which is already setup for
199 // These kind of version tests.
200 Path repoDir = getRepositoryPath( "metadata-repository" );
201 (( EditableManagedRepository)repoContent.getRepository()).setLocation( repoDir.toAbsolutePath().toUri() );
203 // Request the versions.
204 Set<String> testedVersionSet = repoContent.getVersions( reference );
206 // Sort the list (for asserts)
207 List<String> testedVersions = new ArrayList<>();
208 testedVersions.addAll( testedVersionSet );
209 Collections.sort( testedVersions, new VersionComparator() );
211 // Test the expected array of versions, to the actual tested versions
212 assertEquals( "available versions", expectedVersions, testedVersions );
215 private void assertVersions( String artifactId, String version, String[] expectedVersions )
218 VersionedReference reference = new VersionedReference();
219 reference.setGroupId( "org.apache.archiva.metadata.tests" );
220 reference.setArtifactId( artifactId );
221 reference.setVersion( version );
223 // Use the test metadata-repository, which is already setup for
224 // These kind of version tests.
225 Path repoDir = getRepositoryPath( "metadata-repository" );
226 ((EditableManagedRepository)repoContent.getRepository()).setLocation( repoDir.toAbsolutePath().toUri() );
228 // Request the versions.
229 Set<String> testedVersionSet = repoContent.getVersions( reference );
231 // Sort the list (for asserts later)
232 List<String> testedVersions = new ArrayList<>();
233 testedVersions.addAll( testedVersionSet );
234 Collections.sort( testedVersions, new VersionComparator() );
236 // Test the expected array of versions, to the actual tested versions
237 assertEquals( "Assert Versions: length/size", expectedVersions.length, testedVersions.size() );
239 for ( int i = 0; i < expectedVersions.length; i++ )
241 String actualVersion = testedVersions.get( i );
242 assertEquals( "Versions[" + i + "]", expectedVersions[i], actualVersion );
248 protected ArtifactReference toArtifactReference( String path )
249 throws LayoutException
251 return repoContent.toArtifactReference( path );
255 protected String toPath( ArtifactReference reference )
257 return repoContent.toPath( reference );