1 package org.apache.maven.archiva.repository.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
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.maven.archiva.common.utils.VersionComparator;
23 import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration;
24 import org.apache.maven.archiva.model.ArtifactReference;
25 import org.apache.maven.archiva.model.ProjectReference;
26 import org.apache.maven.archiva.model.VersionedReference;
27 import org.apache.maven.archiva.repository.ManagedRepositoryContent;
28 import org.apache.maven.archiva.repository.layout.LayoutException;
31 import java.util.ArrayList;
32 import java.util.Arrays;
33 import java.util.Collections;
34 import java.util.List;
38 * ManagedDefaultRepositoryContentTest
40 * @author <a href="mailto:joakime@apache.org">Joakim Erdfelt</a>
43 public class ManagedDefaultRepositoryContentTest
44 extends AbstractDefaultRepositoryContentTestCase
46 private ManagedRepositoryContent repoContent;
48 public void testGetVersionsBadArtifact()
51 assertGetVersions( "bad_artifact", Collections.EMPTY_LIST );
54 public void testGetVersionsMissingMultipleVersions()
57 assertGetVersions( "missing_metadata_b", Arrays.asList( "1.0", "1.0.1", "2.0", "2.0.1", "2.0-20070821-dev" ) );
60 public void testGetVersionsSimple()
63 assertVersions( "proxied_multi", "2.1", new String[] { "2.1" } );
66 public void testGetVersionsSimpleYetIncomplete()
69 assertGetVersions( "incomplete_metadata_a", Collections.singletonList( "1.0" ) );
72 public void testGetVersionsSimpleYetMissing()
75 assertGetVersions( "missing_metadata_a", Collections.singletonList( "1.0" ) );
78 public void testGetVersionsSnapshotA()
81 assertVersions( "snap_shots_a", "1.0-alpha-11-SNAPSHOT", new String[] {
82 "1.0-alpha-11-SNAPSHOT",
83 "1.0-alpha-11-20070221.194724-2",
84 "1.0-alpha-11-20070302.212723-3",
85 "1.0-alpha-11-20070303.152828-4",
86 "1.0-alpha-11-20070305.215149-5",
87 "1.0-alpha-11-20070307.170909-6",
88 "1.0-alpha-11-20070314.211405-9",
89 "1.0-alpha-11-20070316.175232-11" } );
92 public void testToMetadataPathFromProjectReference()
94 ProjectReference reference = new ProjectReference();
95 reference.setGroupId( "com.foo" );
96 reference.setArtifactId( "foo-tool" );
98 assertEquals( "com/foo/foo-tool/maven-metadata.xml", repoContent.toMetadataPath( reference ) );
101 public void testToMetadataPathFromVersionReference()
103 VersionedReference reference = new VersionedReference();
104 reference.setGroupId( "com.foo" );
105 reference.setArtifactId( "foo-tool" );
106 reference.setVersion( "1.0" );
108 assertEquals( "com/foo/foo-tool/1.0/maven-metadata.xml", repoContent.toMetadataPath( reference ) );
111 public void testToPathOnNullArtifactReference()
115 ArtifactReference reference = null;
116 repoContent.toPath( reference );
117 fail( "Should have failed due to null artifact reference." );
119 catch ( IllegalArgumentException e )
125 private void assertGetVersions( String artifactId, List<String> expectedVersions )
128 ProjectReference reference = new ProjectReference();
129 reference.setGroupId( "org.apache.archiva.metadata.tests" );
130 reference.setArtifactId( artifactId );
132 // Use the test metadata-repository, which is already setup for
133 // These kind of version tests.
134 File repoDir = getTestFile( "src/test/repositories/metadata-repository" );
135 repoContent.getRepository().setLocation( repoDir.getAbsolutePath() );
137 // Request the versions.
138 Set<String> testedVersionSet = repoContent.getVersions( reference );
140 // Sort the list (for asserts)
141 List<String> testedVersions = new ArrayList<String>();
142 testedVersions.addAll( testedVersionSet );
143 Collections.sort( testedVersions, new VersionComparator() );
145 // Test the expected array of versions, to the actual tested versions
146 assertEquals( "available versions", expectedVersions, testedVersions );
149 private void assertVersions( String artifactId, String version, String[] expectedVersions )
152 VersionedReference reference = new VersionedReference();
153 reference.setGroupId( "org.apache.archiva.metadata.tests" );
154 reference.setArtifactId( artifactId );
155 reference.setVersion( version );
157 // Use the test metadata-repository, which is already setup for
158 // These kind of version tests.
159 File repoDir = getTestFile( "src/test/repositories/metadata-repository" );
160 repoContent.getRepository().setLocation( repoDir.getAbsolutePath() );
162 // Request the versions.
163 Set<String> testedVersionSet = repoContent.getVersions( reference );
165 // Sort the list (for asserts later)
166 List<String> testedVersions = new ArrayList<String>();
167 testedVersions.addAll( testedVersionSet );
168 Collections.sort( testedVersions, new VersionComparator() );
170 // Test the expected array of versions, to the actual tested versions
171 assertEquals( "Assert Versions: length/size", expectedVersions.length, testedVersions.size() );
173 for ( int i = 0; i < expectedVersions.length; i++ )
175 String actualVersion = testedVersions.get( i );
176 assertEquals( "Versions[" + i + "]", expectedVersions[i], actualVersion );
181 protected void setUp()
186 File repoDir = getTestFile( "src/test/repositories/default-repository" );
188 ManagedRepositoryConfiguration repository = createRepository( "testRepo", "Unit Test Repo", repoDir );
190 repoContent = (ManagedRepositoryContent) lookup( ManagedRepositoryContent.class, "default" );
191 repoContent.setRepository( repository );
195 protected ArtifactReference toArtifactReference( String path )
196 throws LayoutException
198 return repoContent.toArtifactReference( path );
202 protected String toPath( ArtifactReference reference )
204 return repoContent.toPath( reference );