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.Collections;
33 import java.util.List;
37 * ManagedLegacyRepositoryContentTest
39 * @author <a href="mailto:joakime@apache.org">Joakim Erdfelt</a>
42 public class ManagedLegacyRepositoryContentTest
43 extends AbstractLegacyRepositoryContentTestCase
45 private ManagedRepositoryContent repoContent;
47 public void testGetVersionsFromProjectReference()
50 assertVersions( "org.apache.maven", "testing", new String[] {
55 "1.0-20050611.112233-1" } );
58 public void testGetVersionsFromVersionedReference()
61 assertVersions( "org.apache.maven", "testing", "1.0", new String[] {
65 "1.0-20050611.112233-1" } );
68 private void assertVersions( String groupId, String artifactId, String[] expectedVersions )
71 ProjectReference reference = new ProjectReference();
72 reference.setGroupId( groupId );
73 reference.setArtifactId( artifactId );
75 // Request the versions.
76 Set<String> testedVersionSet = repoContent.getVersions( reference );
78 // Sort the list (for asserts later)
79 List<String> testedVersions = new ArrayList<String>();
80 testedVersions.addAll( testedVersionSet );
81 Collections.sort( testedVersions, new VersionComparator() );
83 // Test the expected array of versions, to the actual tested versions
84 assertEquals( "Assert (Project) Versions: length/size", expectedVersions.length, testedVersions.size() );
86 for ( int i = 0; i < expectedVersions.length; i++ )
88 String actualVersion = testedVersions.get( i );
89 assertEquals( "(Project) Versions[" + i + "]", expectedVersions[i], actualVersion );
93 private void assertVersions( String groupId, String artifactId, String version, String[] expectedVersions )
96 VersionedReference reference = new VersionedReference();
97 reference.setGroupId( groupId );
98 reference.setArtifactId( artifactId );
99 reference.setVersion( version );
101 // Request the versions.
102 Set<String> testedVersionSet = repoContent.getVersions( reference );
104 // Sort the list (for asserts later)
105 List<String> testedVersions = new ArrayList<String>();
106 testedVersions.addAll( testedVersionSet );
107 Collections.sort( testedVersions, new VersionComparator() );
109 // Test the expected array of versions, to the actual tested versions
110 assertEquals( "Assert (Project) Versions: length/size", expectedVersions.length, testedVersions.size() );
112 for ( int i = 0; i < expectedVersions.length; i++ )
114 String actualVersion = testedVersions.get( i );
115 assertEquals( "(Project) Versions[" + i + "]", expectedVersions[i], actualVersion );
119 public void testGetRelatedArtifacts()
122 ArtifactReference reference = createArtifact( "org.apache.maven", "testing", "1.0", null, "jar" );
124 Set<ArtifactReference> related = repoContent.getRelatedArtifacts( reference );
125 assertNotNull( related );
127 String expected[] = new String[] {
128 "org.apache.maven/jars/testing-1.0.jar",
129 "org.apache.maven/java-sources/testing-1.0-sources.jar",
130 "org.apache.maven/jars/testing-1.0-20050611.112233-1.jar",
131 "org.apache.maven/poms/testing-1.0.pom",
132 "org.apache.maven/javadoc.jars/testing-1.0-javadoc.jar" };
134 StringBuffer relatedDebugString = new StringBuffer();
135 relatedDebugString.append( "[" );
136 for ( ArtifactReference ref : related )
138 String actualPath = repoContent.toPath( ref );
139 relatedDebugString.append( actualPath ).append( ":" );
141 relatedDebugString.append( "]" );
143 assertEquals( "Related <" + relatedDebugString + ">:", expected.length, related.size() );
145 for ( String expectedPath : expected )
147 boolean found = false;
148 for ( ArtifactReference actualRef : related )
150 String actualPath = repoContent.toPath( actualRef );
151 if ( actualPath.endsWith( expectedPath ) )
159 fail( "Unable to find expected artifact [" + expectedPath + "] in list of related artifacts. "
160 + "Related <" + relatedDebugString + ">" );
166 protected void setUp()
171 File repoDir = getTestFile( "src/test/repositories/legacy-repository" );
173 ManagedRepositoryConfiguration repository = createRepository( "testRepo", "Unit Test Repo", repoDir );
174 repository.setLayout( "legacy" );
176 repoContent = (ManagedRepositoryContent) lookup( ManagedRepositoryContent.class, "legacy" );
177 repoContent.setRepository( repository );
181 protected ArtifactReference toArtifactReference( String path )
182 throws LayoutException
184 return repoContent.toArtifactReference( path );
188 protected String toPath( ArtifactReference reference )
190 return repoContent.toPath( reference );