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.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.ManagedRepositoryContent;
31 import org.apache.archiva.repository.content.maven2.ManagedDefaultRepositoryContent;
32 import org.apache.archiva.repository.layout.LayoutException;
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.nio.file.Path;
40 import java.nio.file.Paths;
41 import java.util.ArrayList;
42 import java.util.Arrays;
43 import java.util.Collections;
44 import java.util.List;
47 import static org.junit.Assert.assertEquals;
48 import static org.junit.Assert.fail;
51 * ManagedDefaultRepositoryContentTest
53 public class ManagedDefaultRepositoryContentTest
54 extends AbstractDefaultRepositoryContentTestCase
56 private ManagedDefaultRepositoryContent repoContent;
62 @Named ( "archivaConfiguration#default" )
63 ArchivaConfiguration archivaConfiguration;
66 List<? extends ArtifactMappingProvider> artifactMappingProviders;
72 Path repoDir = Paths.get( "src/test/repositories/default-repository" );
74 MavenManagedRepository repository = createRepository( "testRepo", "Unit Test Repo", repoDir );
76 FileType fileType = archivaConfiguration.getConfiguration().getRepositoryScanning().getFileTypes().get( 0 );
77 fileType.addPattern( "**/*.xml" );
78 assertEquals( FileTypes.ARTIFACTS, fileType.getId() );
80 fileTypes.afterConfigurationChange( null, "fileType", null );
82 repoContent = new ManagedDefaultRepositoryContent(artifactMappingProviders, fileTypes);
83 //repoContent = (ManagedRepositoryContent) lookup( ManagedRepositoryContent.class, "default" );
84 repoContent.setRepository( repository );
88 public void testGetVersionsBadArtifact()
91 assertGetVersions( "bad_artifact", Collections.<String>emptyList() );
95 public void testGetVersionsMissingMultipleVersions()
98 assertGetVersions( "missing_metadata_b", Arrays.asList( "1.0", "1.0.1", "2.0", "2.0.1", "2.0-20070821-dev" ) );
102 public void testGetVersionsSimple()
105 assertVersions( "proxied_multi", "2.1", new String[]{ "2.1" } );
109 public void testGetVersionsSimpleYetIncomplete()
112 assertGetVersions( "incomplete_metadata_a", Collections.singletonList( "1.0" ) );
116 public void testGetVersionsSimpleYetMissing()
119 assertGetVersions( "missing_metadata_a", Collections.singletonList( "1.0" ) );
123 public void testGetVersionsSnapshotA()
126 assertVersions( "snap_shots_a", "1.0-alpha-11-SNAPSHOT",
127 new String[]{ "1.0-alpha-11-SNAPSHOT", "1.0-alpha-11-20070221.194724-2",
128 "1.0-alpha-11-20070302.212723-3", "1.0-alpha-11-20070303.152828-4",
129 "1.0-alpha-11-20070305.215149-5", "1.0-alpha-11-20070307.170909-6",
130 "1.0-alpha-11-20070314.211405-9", "1.0-alpha-11-20070316.175232-11" } );
134 public void testToMetadataPathFromProjectReference()
136 ProjectReference reference = new ProjectReference();
137 reference.setGroupId( "com.foo" );
138 reference.setArtifactId( "foo-tool" );
140 assertEquals( "com/foo/foo-tool/maven-metadata.xml", repoContent.toMetadataPath( reference ) );
144 public void testToMetadataPathFromVersionReference()
146 VersionedReference reference = new VersionedReference();
147 reference.setGroupId( "com.foo" );
148 reference.setArtifactId( "foo-tool" );
149 reference.setVersion( "1.0" );
151 assertEquals( "com/foo/foo-tool/1.0/maven-metadata.xml", repoContent.toMetadataPath( reference ) );
156 public void testToPathOnNullArtifactReference()
160 ArtifactReference reference = null;
161 repoContent.toPath( reference );
162 fail( "Should have failed due to null artifact reference." );
164 catch ( IllegalArgumentException e )
171 public void testExcludeMetadataFile()
174 assertVersions( "include_xml", "1.0", new String[]{ "1.0" } );
177 private void assertGetVersions( String artifactId, List<String> expectedVersions )
180 ProjectReference reference = new ProjectReference();
181 reference.setGroupId( "org.apache.archiva.metadata.tests" );
182 reference.setArtifactId( artifactId );
184 // Use the test metadata-repository, which is already setup for
185 // These kind of version tests.
186 Path repoDir = Paths.get( "src/test/repositories/metadata-repository" );
187 (( EditableManagedRepository)repoContent.getRepository()).setLocation( repoDir.toAbsolutePath().toUri() );
189 // Request the versions.
190 Set<String> testedVersionSet = repoContent.getVersions( reference );
192 // Sort the list (for asserts)
193 List<String> testedVersions = new ArrayList<>();
194 testedVersions.addAll( testedVersionSet );
195 Collections.sort( testedVersions, new VersionComparator() );
197 // Test the expected array of versions, to the actual tested versions
198 assertEquals( "available versions", expectedVersions, testedVersions );
201 private void assertVersions( String artifactId, String version, String[] expectedVersions )
204 VersionedReference reference = new VersionedReference();
205 reference.setGroupId( "org.apache.archiva.metadata.tests" );
206 reference.setArtifactId( artifactId );
207 reference.setVersion( version );
209 // Use the test metadata-repository, which is already setup for
210 // These kind of version tests.
211 Path repoDir = Paths.get( "src/test/repositories/metadata-repository" );
212 ((EditableManagedRepository)repoContent.getRepository()).setLocation( repoDir.toAbsolutePath().toUri() );
214 // Request the versions.
215 Set<String> testedVersionSet = repoContent.getVersions( reference );
217 // Sort the list (for asserts later)
218 List<String> testedVersions = new ArrayList<>();
219 testedVersions.addAll( testedVersionSet );
220 Collections.sort( testedVersions, new VersionComparator() );
222 // Test the expected array of versions, to the actual tested versions
223 assertEquals( "Assert Versions: length/size", expectedVersions.length, testedVersions.size() );
225 for ( int i = 0; i < expectedVersions.length; i++ )
227 String actualVersion = testedVersions.get( i );
228 assertEquals( "Versions[" + i + "]", expectedVersions[i], actualVersion );
234 protected ArtifactReference toArtifactReference( String path )
235 throws LayoutException
237 return repoContent.toArtifactReference( path );
241 protected String toPath( ArtifactReference reference )
243 return repoContent.toPath( reference );