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.common.utils.VersionComparator;
23 import org.apache.archiva.configuration.ArchivaConfiguration;
24 import org.apache.archiva.configuration.FileType;
25 import org.apache.archiva.configuration.FileTypes;
26 import org.apache.archiva.model.ArtifactReference;
27 import org.apache.archiva.model.ProjectReference;
28 import org.apache.archiva.model.VersionedReference;
29 import org.apache.archiva.repository.EditableManagedRepository;
30 import org.apache.archiva.repository.ManagedRepositoryContent;
31 import org.apache.archiva.repository.layout.LayoutException;
32 import org.apache.archiva.repository.maven2.MavenManagedRepository;
33 import org.junit.Before;
34 import org.junit.Test;
36 import javax.inject.Inject;
37 import javax.inject.Named;
38 import java.nio.file.Path;
39 import java.nio.file.Paths;
40 import java.util.ArrayList;
41 import java.util.Arrays;
42 import java.util.Collections;
43 import java.util.List;
46 import static org.junit.Assert.assertEquals;
47 import static org.junit.Assert.fail;
50 * ManagedDefaultRepositoryContentTest
52 public class ManagedDefaultRepositoryContentTest
53 extends AbstractDefaultRepositoryContentTestCase
56 @Named ( "managedRepositoryContent#default" )
57 private ManagedRepositoryContent repoContent;
63 @Named ( "archivaConfiguration#default" )
64 ArchivaConfiguration archivaConfiguration;
70 Path repoDir = Paths.get( "src/test/repositories/default-repository" );
72 MavenManagedRepository repository = createRepository( "testRepo", "Unit Test Repo", repoDir );
74 FileType 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 ) );
153 public void testToPathOnNullArtifactReference()
157 ArtifactReference reference = null;
158 repoContent.toPath( reference );
159 fail( "Should have failed due to null artifact reference." );
161 catch ( IllegalArgumentException e )
168 public void testExcludeMetadataFile()
171 assertVersions( "include_xml", "1.0", new String[]{ "1.0" } );
174 private void assertGetVersions( String artifactId, List<String> expectedVersions )
177 ProjectReference reference = new ProjectReference();
178 reference.setGroupId( "org.apache.archiva.metadata.tests" );
179 reference.setArtifactId( artifactId );
181 // Use the test metadata-repository, which is already setup for
182 // These kind of version tests.
183 Path repoDir = Paths.get( "src/test/repositories/metadata-repository" );
184 (( EditableManagedRepository)repoContent.getRepository()).setLocation( repoDir.toAbsolutePath().toUri() );
186 // Request the versions.
187 Set<String> testedVersionSet = repoContent.getVersions( reference );
189 // Sort the list (for asserts)
190 List<String> testedVersions = new ArrayList<>();
191 testedVersions.addAll( testedVersionSet );
192 Collections.sort( testedVersions, new VersionComparator() );
194 // Test the expected array of versions, to the actual tested versions
195 assertEquals( "available versions", expectedVersions, testedVersions );
198 private void assertVersions( String artifactId, String version, String[] expectedVersions )
201 VersionedReference reference = new VersionedReference();
202 reference.setGroupId( "org.apache.archiva.metadata.tests" );
203 reference.setArtifactId( artifactId );
204 reference.setVersion( version );
206 // Use the test metadata-repository, which is already setup for
207 // These kind of version tests.
208 Path repoDir = Paths.get( "src/test/repositories/metadata-repository" );
209 ((EditableManagedRepository)repoContent.getRepository()).setLocation( repoDir.toAbsolutePath().toUri() );
211 // Request the versions.
212 Set<String> testedVersionSet = repoContent.getVersions( reference );
214 // Sort the list (for asserts later)
215 List<String> testedVersions = new ArrayList<>();
216 testedVersions.addAll( testedVersionSet );
217 Collections.sort( testedVersions, new VersionComparator() );
219 // Test the expected array of versions, to the actual tested versions
220 assertEquals( "Assert Versions: length/size", expectedVersions.length, testedVersions.size() );
222 for ( int i = 0; i < expectedVersions.length; i++ )
224 String actualVersion = testedVersions.get( i );
225 assertEquals( "Versions[" + i + "]", expectedVersions[i], actualVersion );
231 protected ArtifactReference toArtifactReference( String path )
232 throws LayoutException
234 return repoContent.toArtifactReference( path );
238 protected String toPath( ArtifactReference reference )
240 return repoContent.toPath( reference );