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.ArchivaConfiguration;
24 import org.apache.maven.archiva.configuration.FileType;
25 import org.apache.maven.archiva.configuration.FileTypes;
26 import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration;
27 import org.apache.maven.archiva.model.ArtifactReference;
28 import org.apache.maven.archiva.model.ProjectReference;
29 import org.apache.maven.archiva.model.VersionedReference;
30 import org.apache.maven.archiva.repository.ManagedRepositoryContent;
31 import org.apache.maven.archiva.repository.layout.LayoutException;
34 import java.util.ArrayList;
35 import java.util.Arrays;
36 import java.util.Collections;
37 import java.util.List;
41 * ManagedDefaultRepositoryContentTest
45 public class ManagedDefaultRepositoryContentTest
46 extends AbstractDefaultRepositoryContentTestCase
48 private ManagedRepositoryContent repoContent;
50 public void testGetVersionsBadArtifact()
53 assertGetVersions( "bad_artifact", Collections.EMPTY_LIST );
56 public void testGetVersionsMissingMultipleVersions()
59 assertGetVersions( "missing_metadata_b", Arrays.asList( "1.0", "1.0.1", "2.0", "2.0.1", "2.0-20070821-dev" ) );
62 public void testGetVersionsSimple()
65 assertVersions( "proxied_multi", "2.1", new String[] { "2.1" } );
68 public void testGetVersionsSimpleYetIncomplete()
71 assertGetVersions( "incomplete_metadata_a", Collections.singletonList( "1.0" ) );
74 public void testGetVersionsSimpleYetMissing()
77 assertGetVersions( "missing_metadata_a", Collections.singletonList( "1.0" ) );
80 public void testGetVersionsSnapshotA()
83 assertVersions( "snap_shots_a", "1.0-alpha-11-SNAPSHOT", new String[] {
84 "1.0-alpha-11-SNAPSHOT",
85 "1.0-alpha-11-20070221.194724-2",
86 "1.0-alpha-11-20070302.212723-3",
87 "1.0-alpha-11-20070303.152828-4",
88 "1.0-alpha-11-20070305.215149-5",
89 "1.0-alpha-11-20070307.170909-6",
90 "1.0-alpha-11-20070314.211405-9",
91 "1.0-alpha-11-20070316.175232-11" } );
94 public void testToMetadataPathFromProjectReference()
96 ProjectReference reference = new ProjectReference();
97 reference.setGroupId( "com.foo" );
98 reference.setArtifactId( "foo-tool" );
100 assertEquals( "com/foo/foo-tool/maven-metadata.xml", repoContent.toMetadataPath( reference ) );
103 public void testToMetadataPathFromVersionReference()
105 VersionedReference reference = new VersionedReference();
106 reference.setGroupId( "com.foo" );
107 reference.setArtifactId( "foo-tool" );
108 reference.setVersion( "1.0" );
110 assertEquals( "com/foo/foo-tool/1.0/maven-metadata.xml", repoContent.toMetadataPath( reference ) );
113 public void testToPathOnNullArtifactReference()
117 ArtifactReference reference = null;
118 repoContent.toPath( reference );
119 fail( "Should have failed due to null artifact reference." );
121 catch ( IllegalArgumentException e )
127 public void testExcludeMetadataFile()
130 assertVersions( "include_xml", "1.0", new String[] { "1.0" } );
133 private void assertGetVersions( String artifactId, List<String> expectedVersions )
136 ProjectReference reference = new ProjectReference();
137 reference.setGroupId( "org.apache.archiva.metadata.tests" );
138 reference.setArtifactId( artifactId );
140 // Use the test metadata-repository, which is already setup for
141 // These kind of version tests.
142 File repoDir = getTestFile( "src/test/repositories/metadata-repository" );
143 repoContent.getRepository().setLocation( repoDir.getAbsolutePath() );
145 // Request the versions.
146 Set<String> testedVersionSet = repoContent.getVersions( reference );
148 // Sort the list (for asserts)
149 List<String> testedVersions = new ArrayList<String>();
150 testedVersions.addAll( testedVersionSet );
151 Collections.sort( testedVersions, new VersionComparator() );
153 // Test the expected array of versions, to the actual tested versions
154 assertEquals( "available versions", expectedVersions, testedVersions );
157 private void assertVersions( String artifactId, String version, String[] expectedVersions )
160 VersionedReference reference = new VersionedReference();
161 reference.setGroupId( "org.apache.archiva.metadata.tests" );
162 reference.setArtifactId( artifactId );
163 reference.setVersion( version );
165 // Use the test metadata-repository, which is already setup for
166 // These kind of version tests.
167 File repoDir = getTestFile( "src/test/repositories/metadata-repository" );
168 repoContent.getRepository().setLocation( repoDir.getAbsolutePath() );
170 // Request the versions.
171 Set<String> testedVersionSet = repoContent.getVersions( reference );
173 // Sort the list (for asserts later)
174 List<String> testedVersions = new ArrayList<String>();
175 testedVersions.addAll( testedVersionSet );
176 Collections.sort( testedVersions, new VersionComparator() );
178 // Test the expected array of versions, to the actual tested versions
179 assertEquals( "Assert Versions: length/size", expectedVersions.length, testedVersions.size() );
181 for ( int i = 0; i < expectedVersions.length; i++ )
183 String actualVersion = testedVersions.get( i );
184 assertEquals( "Versions[" + i + "]", expectedVersions[i], actualVersion );
189 protected void setUp()
194 File repoDir = getTestFile( "src/test/repositories/default-repository" );
196 ManagedRepositoryConfiguration repository = createRepository( "testRepo", "Unit Test Repo", repoDir );
198 ArchivaConfiguration archivaConfiguration = (ArchivaConfiguration) lookup( ArchivaConfiguration.ROLE );
199 FileType fileType = (FileType) archivaConfiguration.getConfiguration().getRepositoryScanning().getFileTypes().get( 0 );
200 fileType.addPattern( "**/*.xml" );
201 assertEquals( FileTypes.ARTIFACTS, fileType.getId() );
203 FileTypes fileTypes = (FileTypes) lookup( FileTypes.class );
204 fileTypes.afterConfigurationChange( null, "fileType", null );
206 repoContent = (ManagedRepositoryContent) lookup( ManagedRepositoryContent.class, "default" );
207 repoContent.setRepository( repository );
211 protected ArtifactReference toArtifactReference( String path )
212 throws LayoutException
214 return repoContent.toArtifactReference( path );
218 protected String toPath( ArtifactReference reference )
220 return repoContent.toPath( reference );