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 public class ManagedLegacyRepositoryContentTest
51 extends AbstractDefaultRepositoryContentTestCase
54 @Named( value = "managedRepositoryContent#legacy" )
55 private ManagedRepositoryContent repoContent;
61 File repoDir = new File( "src/test/repositories/legacy-repository" );
63 ManagedRepository repository = createRepository( "testRepo", "Unit Test Repo", repoDir );
64 repository.setLayout( "legacy" );
66 //repoContent = (ManagedRepositoryContent) lookup( ManagedRepositoryContent.class, "legacy" );
67 repoContent.setRepository( repository );
71 public void testGetVersionsFromProjectReference()
74 assertVersions( "org.apache.maven", "testing", new String[]{ "UNKNOWN",
77 "1.0", "1.0-20050611.112233-1" } );
81 public void testGetVersionsFromVersionedReference()
84 assertVersions( "org.apache.maven", "testing", "1.0", new String[]{
87 "1.0", "1.0-20050611.112233-1" } );
90 private void assertVersions( String groupId, String artifactId, String[] expectedVersions )
93 ProjectReference reference = new ProjectReference();
94 reference.setGroupId( groupId );
95 reference.setArtifactId( artifactId );
97 // Request the versions.
98 Set<String> testedVersionSet = repoContent.getVersions( reference );
100 // Sort the list (for asserts later)
101 List<String> testedVersions = new ArrayList<String>();
102 testedVersions.addAll( testedVersionSet );
103 Collections.sort( testedVersions, new VersionComparator() );
105 // Test the expected array of versions, to the actual tested versions
106 assertEquals( "Assert (Project) Versions: length/size", expectedVersions.length, testedVersions.size() );
108 for ( int i = 0; i < expectedVersions.length; i++ )
110 String actualVersion = testedVersions.get( i );
111 assertEquals( "(Project) Versions[" + i + "]", expectedVersions[i], actualVersion );
115 private void assertVersions( String groupId, String artifactId, String version, String[] expectedVersions )
118 VersionedReference reference = new VersionedReference();
119 reference.setGroupId( groupId );
120 reference.setArtifactId( artifactId );
121 reference.setVersion( version );
123 // Request the versions.
124 Set<String> testedVersionSet = repoContent.getVersions( reference );
126 // Sort the list (for asserts later)
127 List<String> testedVersions = new ArrayList<String>();
128 testedVersions.addAll( testedVersionSet );
129 Collections.sort( testedVersions, new VersionComparator() );
131 // Test the expected array of versions, to the actual tested versions
132 assertEquals( "Assert (Project) Versions: length/size", expectedVersions.length, testedVersions.size() );
134 for ( int i = 0; i < expectedVersions.length; i++ )
136 String actualVersion = testedVersions.get( i );
137 assertEquals( "(Project) Versions[" + i + "]", expectedVersions[i], actualVersion );
142 public void testGetRelatedArtifacts()
145 ArtifactReference reference = createArtifact( "org.apache.maven", "testing", "1.0", null, "jar" );
147 Set<ArtifactReference> related = repoContent.getRelatedArtifacts( reference );
148 assertNotNull( related );
150 String expected[] = new String[]{ "org.apache.maven/jars/testing-1.0.jar",
151 "org.apache.maven/java-sources/testing-1.0-sources.jar",
152 "org.apache.maven/jars/testing-1.0-20050611.112233-1.jar", "org.apache.maven/poms/testing-1.0.pom",
153 "org.apache.maven/distributions/testing-1.0.tar.gz", "org.apache.maven/distributions/testing-1.0.zip",
154 "org.apache.maven/javadoc.jars/testing-1.0-javadoc.jar" };
156 StringBuilder relatedDebugString = new StringBuilder();
157 relatedDebugString.append( "[" );
158 for ( ArtifactReference ref : related )
160 String actualPath = repoContent.toPath( ref );
161 relatedDebugString.append( actualPath ).append( ":" );
163 relatedDebugString.append( "]" );
165 for ( String expectedPath : expected )
167 boolean found = false;
168 for ( ArtifactReference actualRef : related )
170 String actualPath = repoContent.toPath( actualRef );
171 if ( actualPath.endsWith( expectedPath ) )
179 fail( "Unable to find expected artifact [" + expectedPath + "] in list of related artifacts. "
180 + "Related <" + relatedDebugString + ">" );
183 assertEquals( "Related <" + relatedDebugString + ">:", expected.length, related.size() );
188 protected ArtifactReference toArtifactReference( String path )
189 throws LayoutException
191 return repoContent.toArtifactReference( path );
195 protected String toPath( ArtifactReference reference )
197 return repoContent.toPath( reference );