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.model.ArtifactReference;
25 import org.apache.archiva.model.ProjectReference;
26 import org.apache.archiva.model.VersionedReference;
27 import org.apache.archiva.repository.ManagedRepositoryContent;
28 import org.apache.archiva.repository.layout.LayoutException;
29 import org.apache.archiva.test.utils.ArchivaSpringJUnit4ClassRunner;
30 import org.junit.Before;
31 import org.junit.Test;
32 import org.junit.runner.RunWith;
33 import org.springframework.test.context.ContextConfiguration;
35 import javax.inject.Inject;
36 import javax.inject.Named;
38 import java.util.ArrayList;
39 import java.util.Collections;
40 import java.util.List;
43 import static org.junit.Assert.*;
46 * ManagedLegacyRepositoryContentTest
50 @RunWith ( ArchivaSpringJUnit4ClassRunner.class )
51 @ContextConfiguration ( locations = { "classpath*:/META-INF/spring-context.xml", "classpath:/spring-context.xml" } )
52 public class ManagedLegacyRepositoryContentTest
53 extends AbstractDefaultRepositoryContentTestCase
56 @Named( value = "managedRepositoryContent#legacy" )
57 private ManagedRepositoryContent repoContent;
63 File repoDir = new File( "src/test/repositories/legacy-repository" );
65 ManagedRepository repository = createRepository( "testRepo", "Unit Test Repo", repoDir );
66 repository.setLayout( "legacy" );
68 //repoContent = (ManagedRepositoryContent) lookup( ManagedRepositoryContent.class, "legacy" );
69 repoContent.setRepository( repository );
73 public void testGetVersionsFromProjectReference()
76 assertVersions( "org.apache.maven", "testing", new String[]{ "UNKNOWN",
79 "1.0", "1.0-20050611.112233-1" } );
83 public void testGetVersionsFromVersionedReference()
86 assertVersions( "org.apache.maven", "testing", "1.0", new String[]{
89 "1.0", "1.0-20050611.112233-1" } );
92 private void assertVersions( String groupId, String artifactId, String[] expectedVersions )
95 ProjectReference reference = new ProjectReference();
96 reference.setGroupId( groupId );
97 reference.setArtifactId( artifactId );
99 // Request the versions.
100 Set<String> testedVersionSet = repoContent.getVersions( reference );
102 // Sort the list (for asserts later)
103 List<String> testedVersions = new ArrayList<String>();
104 testedVersions.addAll( testedVersionSet );
105 Collections.sort( testedVersions, new VersionComparator() );
107 // Test the expected array of versions, to the actual tested versions
108 assertEquals( "Assert (Project) Versions: length/size", expectedVersions.length, testedVersions.size() );
110 for ( int i = 0; i < expectedVersions.length; i++ )
112 String actualVersion = testedVersions.get( i );
113 assertEquals( "(Project) Versions[" + i + "]", expectedVersions[i], actualVersion );
117 private void assertVersions( String groupId, String artifactId, String version, String[] expectedVersions )
120 VersionedReference reference = new VersionedReference();
121 reference.setGroupId( groupId );
122 reference.setArtifactId( artifactId );
123 reference.setVersion( version );
125 // Request the versions.
126 Set<String> testedVersionSet = repoContent.getVersions( reference );
128 // Sort the list (for asserts later)
129 List<String> testedVersions = new ArrayList<String>();
130 testedVersions.addAll( testedVersionSet );
131 Collections.sort( testedVersions, new VersionComparator() );
133 // Test the expected array of versions, to the actual tested versions
134 assertEquals( "Assert (Project) Versions: length/size", expectedVersions.length, testedVersions.size() );
136 for ( int i = 0; i < expectedVersions.length; i++ )
138 String actualVersion = testedVersions.get( i );
139 assertEquals( "(Project) Versions[" + i + "]", expectedVersions[i], actualVersion );
144 public void testGetRelatedArtifacts()
147 ArtifactReference reference = createArtifact( "org.apache.maven", "testing", "1.0", null, "jar" );
149 Set<ArtifactReference> related = repoContent.getRelatedArtifacts( reference );
150 assertNotNull( related );
152 String expected[] = new String[]{ "org.apache.maven/jars/testing-1.0.jar",
153 "org.apache.maven/java-sources/testing-1.0-sources.jar",
154 "org.apache.maven/jars/testing-1.0-20050611.112233-1.jar", "org.apache.maven/poms/testing-1.0.pom",
155 "org.apache.maven/distributions/testing-1.0.tar.gz", "org.apache.maven/distributions/testing-1.0.zip",
156 "org.apache.maven/javadoc.jars/testing-1.0-javadoc.jar" };
158 StringBuilder relatedDebugString = new StringBuilder();
159 relatedDebugString.append( "[" );
160 for ( ArtifactReference ref : related )
162 String actualPath = repoContent.toPath( ref );
163 relatedDebugString.append( actualPath ).append( ":" );
165 relatedDebugString.append( "]" );
167 for ( String expectedPath : expected )
169 boolean found = false;
170 for ( ArtifactReference actualRef : related )
172 String actualPath = repoContent.toPath( actualRef );
173 if ( actualPath.endsWith( expectedPath ) )
181 fail( "Unable to find expected artifact [" + expectedPath + "] in list of related artifacts. "
182 + "Related <" + relatedDebugString + ">" );
185 assertEquals( "Related <" + relatedDebugString + ">:", expected.length, related.size() );
190 protected ArtifactReference toArtifactReference( String path )
191 throws LayoutException
193 return repoContent.toArtifactReference( path );
197 protected String toPath( ArtifactReference reference )
199 return repoContent.toPath( reference );