View Javadoc
1   package org.apache.archiva.metadata.repository.storage.maven2;
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.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;
34  
35  import javax.inject.Inject;
36  import javax.inject.Named;
37  import java.io.File;
38  import java.util.ArrayList;
39  import java.util.Arrays;
40  import java.util.Collections;
41  import java.util.List;
42  import java.util.Set;
43  
44  import static org.junit.Assert.assertEquals;
45  import static org.junit.Assert.fail;
46  
47  /**
48   * ManagedDefaultRepositoryContentTest
49   */
50  public class ManagedDefaultRepositoryContentTest
51      extends AbstractDefaultRepositoryContentTestCase
52  {
53      @Inject
54      @Named ( "managedRepositoryContent#default" )
55      private ManagedRepositoryContent repoContent;
56  
57      @Inject
58      FileTypes fileTypes;
59  
60      @Inject
61      @Named ( "archivaConfiguration#default" )
62      ArchivaConfiguration archivaConfiguration;
63  
64      @Before
65      public void setUp()
66          throws Exception
67      {
68          File repoDir = new File( "src/test/repositories/default-repository" );
69  
70          ManagedRepository repository = createRepository( "testRepo", "Unit Test Repo", repoDir );
71  
72          FileType fileType = archivaConfiguration.getConfiguration().getRepositoryScanning().getFileTypes().get( 0 );
73          fileType.addPattern( "**/*.xml" );
74          assertEquals( FileTypes.ARTIFACTS, fileType.getId() );
75  
76          fileTypes.afterConfigurationChange( null, "fileType", null );
77  
78          //repoContent = (ManagedRepositoryContent) lookup( ManagedRepositoryContent.class, "default" );
79          repoContent.setRepository( repository );
80      }
81  
82      @Test
83      public void testGetVersionsBadArtifact()
84          throws Exception
85      {
86          assertGetVersions( "bad_artifact", Collections.<String>emptyList() );
87      }
88  
89      @Test
90      public void testGetVersionsMissingMultipleVersions()
91          throws Exception
92      {
93          assertGetVersions( "missing_metadata_b", Arrays.asList( "1.0", "1.0.1", "2.0", "2.0.1", "2.0-20070821-dev" ) );
94      }
95  
96      @Test
97      public void testGetVersionsSimple()
98          throws Exception
99      {
100         assertVersions( "proxied_multi", "2.1", new String[]{ "2.1" } );
101     }
102 
103     @Test
104     public void testGetVersionsSimpleYetIncomplete()
105         throws Exception
106     {
107         assertGetVersions( "incomplete_metadata_a", Collections.singletonList( "1.0" ) );
108     }
109 
110     @Test
111     public void testGetVersionsSimpleYetMissing()
112         throws Exception
113     {
114         assertGetVersions( "missing_metadata_a", Collections.singletonList( "1.0" ) );
115     }
116 
117     @Test
118     public void testGetVersionsSnapshotA()
119         throws Exception
120     {
121         assertVersions( "snap_shots_a", "1.0-alpha-11-SNAPSHOT",
122                         new String[]{ "1.0-alpha-11-SNAPSHOT", "1.0-alpha-11-20070221.194724-2",
123                             "1.0-alpha-11-20070302.212723-3", "1.0-alpha-11-20070303.152828-4",
124                             "1.0-alpha-11-20070305.215149-5", "1.0-alpha-11-20070307.170909-6",
125                             "1.0-alpha-11-20070314.211405-9", "1.0-alpha-11-20070316.175232-11" } );
126     }
127 
128     @Test
129     public void testToMetadataPathFromProjectReference()
130     {
131         ProjectReference reference = new ProjectReference();
132         reference.setGroupId( "com.foo" );
133         reference.setArtifactId( "foo-tool" );
134 
135         assertEquals( "com/foo/foo-tool/maven-metadata.xml", repoContent.toMetadataPath( reference ) );
136     }
137 
138     @Test
139     public void testToMetadataPathFromVersionReference()
140     {
141         VersionedReference reference = new VersionedReference();
142         reference.setGroupId( "com.foo" );
143         reference.setArtifactId( "foo-tool" );
144         reference.setVersion( "1.0" );
145 
146         assertEquals( "com/foo/foo-tool/1.0/maven-metadata.xml", repoContent.toMetadataPath( reference ) );
147     }
148 
149     @Test
150     @Override
151     public void testToPathOnNullArtifactReference()
152     {
153         try
154         {
155             ArtifactReference reference = null;
156             repoContent.toPath( reference );
157             fail( "Should have failed due to null artifact reference." );
158         }
159         catch ( IllegalArgumentException e )
160         {
161             /* expected path */
162         }
163     }
164 
165     @Test
166     public void testExcludeMetadataFile()
167         throws Exception
168     {
169         assertVersions( "include_xml", "1.0", new String[]{ "1.0" } );
170     }
171 
172     private void assertGetVersions( String artifactId, List<String> expectedVersions )
173         throws Exception
174     {
175         ProjectReference reference = new ProjectReference();
176         reference.setGroupId( "org.apache.archiva.metadata.tests" );
177         reference.setArtifactId( artifactId );
178 
179         // Use the test metadata-repository, which is already setup for
180         // These kind of version tests.
181         File repoDir = new File( "src/test/repositories/metadata-repository" );
182         repoContent.getRepository().setLocation( repoDir.getAbsolutePath() );
183 
184         // Request the versions.
185         Set<String> testedVersionSet = repoContent.getVersions( reference );
186 
187         // Sort the list (for asserts)
188         List<String> testedVersions = new ArrayList<>();
189         testedVersions.addAll( testedVersionSet );
190         Collections.sort( testedVersions, new VersionComparator() );
191 
192         // Test the expected array of versions, to the actual tested versions
193         assertEquals( "available versions", expectedVersions, testedVersions );
194     }
195 
196     private void assertVersions( String artifactId, String version, String[] expectedVersions )
197         throws Exception
198     {
199         VersionedReference reference = new VersionedReference();
200         reference.setGroupId( "org.apache.archiva.metadata.tests" );
201         reference.setArtifactId( artifactId );
202         reference.setVersion( version );
203 
204         // Use the test metadata-repository, which is already setup for
205         // These kind of version tests.
206         File repoDir = new File( "src/test/repositories/metadata-repository" );
207         repoContent.getRepository().setLocation( repoDir.getAbsolutePath() );
208 
209         // Request the versions.
210         Set<String> testedVersionSet = repoContent.getVersions( reference );
211 
212         // Sort the list (for asserts later)
213         List<String> testedVersions = new ArrayList<>();
214         testedVersions.addAll( testedVersionSet );
215         Collections.sort( testedVersions, new VersionComparator() );
216 
217         // Test the expected array of versions, to the actual tested versions
218         assertEquals( "Assert Versions: length/size", expectedVersions.length, testedVersions.size() );
219 
220         for ( int i = 0; i < expectedVersions.length; i++ )
221         {
222             String actualVersion = testedVersions.get( i );
223             assertEquals( "Versions[" + i + "]", expectedVersions[i], actualVersion );
224         }
225     }
226 
227 
228     @Override
229     protected ArtifactReference toArtifactReference( String path )
230         throws LayoutException
231     {
232         return repoContent.toArtifactReference( path );
233     }
234 
235     @Override
236     protected String toPath( ArtifactReference reference )
237     {
238         return repoContent.toPath( reference );
239     }
240 }