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.admin.model.beans.ManagedRepository;
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.ManagedRepositoryContent;
31 import org.apache.archiva.repository.layout.LayoutException;
32 import org.apache.archiva.test.utils.ArchivaSpringJUnit4ClassRunner;
33 import org.junit.Before;
34 import org.junit.Test;
35 import org.junit.runner.RunWith;
36 import org.springframework.test.context.ContextConfiguration;
38 import javax.inject.Inject;
39 import javax.inject.Named;
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 @RunWith ( ArchivaSpringJUnit4ClassRunner.class )
54 @ContextConfiguration ( locations = { "classpath*:/META-INF/spring-context.xml", "classpath:/spring-context.xml" } )
55 public class ManagedDefaultRepositoryContentTest
56 extends AbstractDefaultRepositoryContentTestCase
59 @Named ( value = "managedRepositoryContent#default" )
60 private ManagedRepositoryContent repoContent;
66 @Named ( value = "archivaConfiguration#default" )
67 ArchivaConfiguration archivaConfiguration;
73 File repoDir = new File( "src/test/repositories/default-repository" );
75 ManagedRepository repository = createRepository( "testRepo", "Unit Test Repo", repoDir );
77 FileType fileType = archivaConfiguration.getConfiguration().getRepositoryScanning().getFileTypes().get( 0 );
78 fileType.addPattern( "**/*.xml" );
79 assertEquals( FileTypes.ARTIFACTS, fileType.getId() );
81 fileTypes.afterConfigurationChange( null, "fileType", null );
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 ) );
155 public void testToPathOnNullArtifactReference()
159 ArtifactReference reference = null;
160 repoContent.toPath( reference );
161 fail( "Should have failed due to null artifact reference." );
163 catch ( IllegalArgumentException e )
170 public void testExcludeMetadataFile()
173 assertVersions( "include_xml", "1.0", new String[]{ "1.0" } );
176 private void assertGetVersions( String artifactId, List<String> expectedVersions )
179 ProjectReference reference = new ProjectReference();
180 reference.setGroupId( "org.apache.archiva.metadata.tests" );
181 reference.setArtifactId( artifactId );
183 // Use the test metadata-repository, which is already setup for
184 // These kind of version tests.
185 File repoDir = new File( "src/test/repositories/metadata-repository" );
186 repoContent.getRepository().setLocation( repoDir.getAbsolutePath() );
188 // Request the versions.
189 Set<String> testedVersionSet = repoContent.getVersions( reference );
191 // Sort the list (for asserts)
192 List<String> testedVersions = new ArrayList<String>();
193 testedVersions.addAll( testedVersionSet );
194 Collections.sort( testedVersions, new VersionComparator() );
196 // Test the expected array of versions, to the actual tested versions
197 assertEquals( "available versions", expectedVersions, testedVersions );
200 private void assertVersions( String artifactId, String version, String[] expectedVersions )
203 VersionedReference reference = new VersionedReference();
204 reference.setGroupId( "org.apache.archiva.metadata.tests" );
205 reference.setArtifactId( artifactId );
206 reference.setVersion( version );
208 // Use the test metadata-repository, which is already setup for
209 // These kind of version tests.
210 File repoDir = new File( "src/test/repositories/metadata-repository" );
211 repoContent.getRepository().setLocation( repoDir.getAbsolutePath() );
213 // Request the versions.
214 Set<String> testedVersionSet = repoContent.getVersions( reference );
216 // Sort the list (for asserts later)
217 List<String> testedVersions = new ArrayList<String>();
218 testedVersions.addAll( testedVersionSet );
219 Collections.sort( testedVersions, new VersionComparator() );
221 // Test the expected array of versions, to the actual tested versions
222 assertEquals( "Assert Versions: length/size", expectedVersions.length, testedVersions.size() );
224 for ( int i = 0; i < expectedVersions.length; i++ )
226 String actualVersion = testedVersions.get( i );
227 assertEquals( "Versions[" + i + "]", expectedVersions[i], actualVersion );
233 protected ArtifactReference toArtifactReference( String path )
234 throws LayoutException
236 return repoContent.toArtifactReference( path );
240 protected String toPath( ArtifactReference reference )
242 return repoContent.toPath( reference );