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.junit.Before;
30 import org.junit.Test;
32 import javax.inject.Inject;
33 import javax.inject.Named;
35 import java.util.ArrayList;
36 import java.util.Collections;
37 import java.util.List;
40 import static org.junit.Assert.*;
43 * ManagedLegacyRepositoryContentTest
47 public class ManagedLegacyRepositoryContentTest
48 extends AbstractLegacyRepositoryContentTestCase
51 @Named( "managedRepositoryContent#legacy" )
52 private ManagedRepositoryContent repoContent;
58 File repoDir = new File( "src/test/repositories/legacy-repository" );
60 ManagedRepository repository = createRepository( "testRepo", "Unit Test Repo", repoDir );
61 repository.setLayout( "legacy" );
63 //repoContent = (ManagedRepositoryContent) lookup( ManagedRepositoryContent.class, "legacy" );
64 repoContent.setRepository( repository );
68 public void testGetVersionsFromProjectReference()
71 assertVersions( "org.apache.maven", "testing", new String[]{ "UNKNOWN",
74 "1.0", "1.0-20050611.112233-1" } );
78 public void testGetVersionsFromVersionedReference()
81 assertVersions( "org.apache.maven", "testing", "1.0", new String[]{
84 "1.0", "1.0-20050611.112233-1" } );
87 private void assertVersions( String groupId, String artifactId, String[] expectedVersions )
90 ProjectReference reference = new ProjectReference();
91 reference.setGroupId( groupId );
92 reference.setArtifactId( artifactId );
94 // Request the versions.
95 Set<String> testedVersionSet = repoContent.getVersions( reference );
97 // Sort the list (for asserts later)
98 List<String> testedVersions = new ArrayList<>();
99 testedVersions.addAll( testedVersionSet );
100 Collections.sort( testedVersions, new VersionComparator() );
102 // Test the expected array of versions, to the actual tested versions
103 assertEquals( "Assert (Project) Versions: length/size", expectedVersions.length, testedVersions.size() );
105 for ( int i = 0; i < expectedVersions.length; i++ )
107 String actualVersion = testedVersions.get( i );
108 assertEquals( "(Project) Versions[" + i + "]", expectedVersions[i], actualVersion );
112 private void assertVersions( String groupId, String artifactId, String version, String[] expectedVersions )
115 VersionedReference reference = new VersionedReference();
116 reference.setGroupId( groupId );
117 reference.setArtifactId( artifactId );
118 reference.setVersion( version );
120 // Request the versions.
121 Set<String> testedVersionSet = repoContent.getVersions( reference );
123 // Sort the list (for asserts later)
124 List<String> testedVersions = new ArrayList<>();
125 testedVersions.addAll( testedVersionSet );
126 Collections.sort( testedVersions, new VersionComparator() );
128 // Test the expected array of versions, to the actual tested versions
129 assertEquals( "Assert (Project) Versions: length/size", expectedVersions.length, testedVersions.size() );
131 for ( int i = 0; i < expectedVersions.length; i++ )
133 String actualVersion = testedVersions.get( i );
134 assertEquals( "(Project) Versions[" + i + "]", expectedVersions[i], actualVersion );
139 public void testGetRelatedArtifacts()
142 ArtifactReference reference = createArtifact( "org.apache.maven", "testing", "1.0", null, "jar" );
144 Set<ArtifactReference> related = repoContent.getRelatedArtifacts( reference );
145 assertNotNull( related );
147 String expected[] = new String[]{ "org.apache.maven/jars/testing-1.0.jar",
148 "org.apache.maven/java-sources/testing-1.0-sources.jar",
149 "org.apache.maven/jars/testing-1.0-20050611.112233-1.jar", "org.apache.maven/poms/testing-1.0.pom",
150 "org.apache.maven/distributions/testing-1.0.tar.gz", "org.apache.maven/distributions/testing-1.0.zip",
151 "org.apache.maven/javadoc.jars/testing-1.0-javadoc.jar" };
153 StringBuilder relatedDebugString = new StringBuilder();
154 relatedDebugString.append( "[" );
155 for ( ArtifactReference ref : related )
157 String actualPath = repoContent.toPath( ref );
158 relatedDebugString.append( actualPath ).append( ":" );
160 relatedDebugString.append( "]" );
162 for ( String expectedPath : expected )
164 boolean found = false;
165 for ( ArtifactReference actualRef : related )
167 String actualPath = repoContent.toPath( actualRef );
168 if ( actualPath.endsWith( expectedPath ) )
176 fail( "Unable to find expected artifact [" + expectedPath + "] in list of related artifacts. "
177 + "Related <" + relatedDebugString + ">" );
180 assertEquals( "Related <" + relatedDebugString + ">:", expected.length, related.size() );
185 protected ArtifactReference toArtifactReference( String path )
186 throws LayoutException
188 return repoContent.toArtifactReference( path );
192 protected String toPath( ArtifactReference reference )
194 return repoContent.toPath( reference );