View Javadoc

1   package org.apache.maven.archiva.repository.content;
2   
3   /*
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
11   *
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   *
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
19   * under the License.
20   */
21  
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;
32  
33  import java.io.File;
34  import java.util.ArrayList;
35  import java.util.Arrays;
36  import java.util.Collections;
37  import java.util.List;
38  import java.util.Set;
39  
40  /**
41   * ManagedDefaultRepositoryContentTest 
42   *
43   * @version $Id: ManagedDefaultRepositoryContentTest.java 755296 2009-03-17 16:13:38Z brett $
44   */
45  public class ManagedDefaultRepositoryContentTest
46      extends AbstractDefaultRepositoryContentTestCase
47  {
48      private ManagedRepositoryContent repoContent;
49  
50      public void testGetVersionsBadArtifact()
51          throws Exception
52      {
53          assertGetVersions( "bad_artifact", Collections.<String>emptyList() );
54      }
55  
56      public void testGetVersionsMissingMultipleVersions()
57          throws Exception
58      {
59          assertGetVersions( "missing_metadata_b", Arrays.asList( "1.0", "1.0.1", "2.0", "2.0.1", "2.0-20070821-dev" ) );
60      }
61  
62      public void testGetVersionsSimple()
63          throws Exception
64      {
65          assertVersions( "proxied_multi", "2.1", new String[] { "2.1" } );
66      }
67  
68      public void testGetVersionsSimpleYetIncomplete()
69          throws Exception
70      {
71          assertGetVersions( "incomplete_metadata_a", Collections.singletonList( "1.0" ) );
72      }
73  
74      public void testGetVersionsSimpleYetMissing()
75          throws Exception
76      {
77          assertGetVersions( "missing_metadata_a", Collections.singletonList( "1.0" ) );
78      }
79  
80      public void testGetVersionsSnapshotA()
81          throws Exception
82      {
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" } );
92      }
93  
94      public void testToMetadataPathFromProjectReference()
95      {
96          ProjectReference reference = new ProjectReference();
97          reference.setGroupId( "com.foo" );
98          reference.setArtifactId( "foo-tool" );
99  
100         assertEquals( "com/foo/foo-tool/maven-metadata.xml", repoContent.toMetadataPath( reference ) );
101     }
102 
103     public void testToMetadataPathFromVersionReference()
104     {
105         VersionedReference reference = new VersionedReference();
106         reference.setGroupId( "com.foo" );
107         reference.setArtifactId( "foo-tool" );
108         reference.setVersion( "1.0" );
109 
110         assertEquals( "com/foo/foo-tool/1.0/maven-metadata.xml", repoContent.toMetadataPath( reference ) );
111     }
112 
113     public void testToPathOnNullArtifactReference()
114     {
115         try
116         {
117             ArtifactReference reference = null;
118             repoContent.toPath( reference );
119             fail( "Should have failed due to null artifact reference." );
120         }
121         catch ( IllegalArgumentException e )
122         {
123             /* expected path */
124         }
125     }
126 
127     public void testExcludeMetadataFile()
128         throws Exception
129     {
130         assertVersions( "include_xml", "1.0", new String[] { "1.0" } );
131     }
132 
133     private void assertGetVersions( String artifactId, List<String> expectedVersions )
134         throws Exception
135     {
136         ProjectReference reference = new ProjectReference();
137         reference.setGroupId( "org.apache.archiva.metadata.tests" );
138         reference.setArtifactId( artifactId );
139 
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() );
144 
145         // Request the versions.
146         Set<String> testedVersionSet = repoContent.getVersions( reference );
147 
148         // Sort the list (for asserts)
149         List<String> testedVersions = new ArrayList<String>();
150         testedVersions.addAll( testedVersionSet );
151         Collections.sort( testedVersions, new VersionComparator() );
152 
153         // Test the expected array of versions, to the actual tested versions
154         assertEquals( "available versions", expectedVersions, testedVersions );
155     }
156 
157     private void assertVersions( String artifactId, String version, String[] expectedVersions )
158         throws Exception
159     {
160         VersionedReference reference = new VersionedReference();
161         reference.setGroupId( "org.apache.archiva.metadata.tests" );
162         reference.setArtifactId( artifactId );
163         reference.setVersion( version );
164 
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() );
169 
170         // Request the versions.
171         Set<String> testedVersionSet = repoContent.getVersions( reference );
172 
173         // Sort the list (for asserts later)
174         List<String> testedVersions = new ArrayList<String>();
175         testedVersions.addAll( testedVersionSet );
176         Collections.sort( testedVersions, new VersionComparator() );
177 
178         // Test the expected array of versions, to the actual tested versions
179         assertEquals( "Assert Versions: length/size", expectedVersions.length, testedVersions.size() );
180 
181         for ( int i = 0; i < expectedVersions.length; i++ )
182         {
183             String actualVersion = testedVersions.get( i );
184             assertEquals( "Versions[" + i + "]", expectedVersions[i], actualVersion );
185         }
186     }
187 
188     @Override
189     protected void setUp()
190         throws Exception
191     {
192         super.setUp();
193 
194         File repoDir = getTestFile( "src/test/repositories/default-repository" );
195 
196         ManagedRepositoryConfiguration repository = createRepository( "testRepo", "Unit Test Repo", repoDir );
197 
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() );
202 
203         FileTypes fileTypes = (FileTypes) lookup( FileTypes.class );
204         fileTypes.afterConfigurationChange( null, "fileType", null );
205 
206         repoContent = (ManagedRepositoryContent) lookup( ManagedRepositoryContent.class, "default" );
207         repoContent.setRepository( repository );
208     }
209 
210     @Override
211     protected ArtifactReference toArtifactReference( String path )
212         throws LayoutException
213     {
214         return repoContent.toArtifactReference( path );
215     }
216 
217     @Override
218     protected String toPath( ArtifactReference reference )
219     {
220         return repoContent.toPath( reference );
221     }
222 }