1 package org.apache.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.archiva.admin.model.beans.ManagedRepository;
23 import org.apache.archiva.common.utils.VersionComparator;
24 import org.apache.archiva.configuration.ArchivaConfiguration;
25 import org.apache.archiva.configuration.FileType;
26 import org.apache.archiva.configuration.FileTypes;
27 import org.apache.archiva.model.ArtifactReference;
28 import org.apache.archiva.model.ProjectReference;
29 import org.apache.archiva.model.VersionedReference;
30 import org.apache.archiva.repository.ManagedRepositoryContent;
31 import org.apache.archiva.repository.layout.LayoutException;
32 import org.junit.Before;
33 import org.junit.Test;
36 import java.util.ArrayList;
37 import java.util.Arrays;
38 import java.util.Collections;
39 import java.util.List;
41 import javax.inject.Inject;
42 import javax.inject.Named;
44 import static org.junit.Assert.*;
47 * ManagedDefaultRepositoryContentTest
51 public class ManagedDefaultRepositoryContentTest
52 extends AbstractDefaultRepositoryContentTestCase
55 @Named( value = "managedRepositoryContent#default" )
56 private ManagedRepositoryContent repoContent;
61 @Inject @Named(value = "archivaConfiguration#default")
62 ArchivaConfiguration archivaConfiguration;
68 File repoDir = new File( "src/test/repositories/default-repository" );
70 ManagedRepository repository = createRepository( "testRepo", "Unit Test Repo", repoDir );
74 (FileType) archivaConfiguration.getConfiguration().getRepositoryScanning().getFileTypes().get( 0 );
75 fileType.addPattern( "**/*.xml" );
76 assertEquals( FileTypes.ARTIFACTS, fileType.getId() );
78 fileTypes.afterConfigurationChange( null, "fileType", null );
80 //repoContent = (ManagedRepositoryContent) lookup( ManagedRepositoryContent.class, "default" );
81 repoContent.setRepository( repository );
85 public void testGetVersionsBadArtifact()
88 assertGetVersions( "bad_artifact", Collections.<String>emptyList() );
92 public void testGetVersionsMissingMultipleVersions()
95 assertGetVersions( "missing_metadata_b", Arrays.asList( "1.0", "1.0.1", "2.0", "2.0.1", "2.0-20070821-dev" ) );
99 public void testGetVersionsSimple()
102 assertVersions( "proxied_multi", "2.1", new String[]{ "2.1" } );
106 public void testGetVersionsSimpleYetIncomplete()
109 assertGetVersions( "incomplete_metadata_a", Collections.singletonList( "1.0" ) );
113 public void testGetVersionsSimpleYetMissing()
116 assertGetVersions( "missing_metadata_a", Collections.singletonList( "1.0" ) );
120 public void testGetVersionsSnapshotA()
123 assertVersions( "snap_shots_a", "1.0-alpha-11-SNAPSHOT",
124 new String[]{ "1.0-alpha-11-SNAPSHOT", "1.0-alpha-11-20070221.194724-2",
125 "1.0-alpha-11-20070302.212723-3", "1.0-alpha-11-20070303.152828-4",
126 "1.0-alpha-11-20070305.215149-5", "1.0-alpha-11-20070307.170909-6",
127 "1.0-alpha-11-20070314.211405-9", "1.0-alpha-11-20070316.175232-11" } );
131 public void testToMetadataPathFromProjectReference()
133 ProjectReference reference = new ProjectReference();
134 reference.setGroupId( "com.foo" );
135 reference.setArtifactId( "foo-tool" );
137 assertEquals( "com/foo/foo-tool/maven-metadata.xml", repoContent.toMetadataPath( reference ) );
141 public void testToMetadataPathFromVersionReference()
143 VersionedReference reference = new VersionedReference();
144 reference.setGroupId( "com.foo" );
145 reference.setArtifactId( "foo-tool" );
146 reference.setVersion( "1.0" );
148 assertEquals( "com/foo/foo-tool/1.0/maven-metadata.xml", repoContent.toMetadataPath( reference ) );
152 public void testToPathOnNullArtifactReference()
156 ArtifactReference reference = null;
157 repoContent.toPath( reference );
158 fail( "Should have failed due to null artifact reference." );
160 catch ( IllegalArgumentException e )
167 public void testExcludeMetadataFile()
170 assertVersions( "include_xml", "1.0", new String[]{ "1.0" } );
173 private void assertGetVersions( String artifactId, List<String> expectedVersions )
176 ProjectReference reference = new ProjectReference();
177 reference.setGroupId( "org.apache.archiva.metadata.tests" );
178 reference.setArtifactId( artifactId );
180 // Use the test metadata-repository, which is already setup for
181 // These kind of version tests.
182 File repoDir = new File( "src/test/repositories/metadata-repository" );
183 repoContent.getRepository().setLocation( repoDir.getAbsolutePath() );
185 // Request the versions.
186 Set<String> testedVersionSet = repoContent.getVersions( reference );
188 // Sort the list (for asserts)
189 List<String> testedVersions = new ArrayList<String>();
190 testedVersions.addAll( testedVersionSet );
191 Collections.sort( testedVersions, new VersionComparator() );
193 // Test the expected array of versions, to the actual tested versions
194 assertEquals( "available versions", expectedVersions, testedVersions );
197 private void assertVersions( String artifactId, String version, String[] expectedVersions )
200 VersionedReference reference = new VersionedReference();
201 reference.setGroupId( "org.apache.archiva.metadata.tests" );
202 reference.setArtifactId( artifactId );
203 reference.setVersion( version );
205 // Use the test metadata-repository, which is already setup for
206 // These kind of version tests.
207 File repoDir = new File( "src/test/repositories/metadata-repository" );
208 repoContent.getRepository().setLocation( repoDir.getAbsolutePath() );
210 // Request the versions.
211 Set<String> testedVersionSet = repoContent.getVersions( reference );
213 // Sort the list (for asserts later)
214 List<String> testedVersions = new ArrayList<String>();
215 testedVersions.addAll( testedVersionSet );
216 Collections.sort( testedVersions, new VersionComparator() );
218 // Test the expected array of versions, to the actual tested versions
219 assertEquals( "Assert Versions: length/size", expectedVersions.length, testedVersions.size() );
221 for ( int i = 0; i < expectedVersions.length; i++ )
223 String actualVersion = testedVersions.get( i );
224 assertEquals( "Versions[" + i + "]", expectedVersions[i], actualVersion );
230 protected ArtifactReference toArtifactReference( String path )
231 throws LayoutException
233 return repoContent.toArtifactReference( path );
237 protected String toPath( ArtifactReference reference )
239 return repoContent.toPath( reference );