]> source.dussan.org Git - archiva.git/blob
c921adf0fbeeaa0e44adfaef4f6b6d8dd4449ee8
[archiva.git] /
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.common.filelock.FileLockManager;
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.EditableManagedRepository;
31 import org.apache.archiva.repository.LayoutException;
32 import org.apache.archiva.repository.content.maven2.ManagedDefaultRepositoryContent;
33 import org.apache.archiva.repository.maven2.MavenManagedRepository;
34 import org.apache.commons.io.FileUtils;
35 import org.junit.Before;
36 import org.junit.Test;
37
38 import javax.inject.Inject;
39 import javax.inject.Named;
40 import java.io.IOException;
41 import java.net.URISyntaxException;
42 import java.nio.file.Files;
43 import java.nio.file.Path;
44 import java.nio.file.Paths;
45 import java.util.ArrayList;
46 import java.util.Arrays;
47 import java.util.Collections;
48 import java.util.List;
49 import java.util.Set;
50
51 import static org.junit.Assert.*;
52
53 /**
54  * ManagedDefaultRepositoryContentTest
55  */
56 public class ManagedDefaultRepositoryContentTest
57     extends AbstractDefaultRepositoryContentTestCase
58 {
59     private ManagedDefaultRepositoryContent repoContent;
60
61     @Inject
62     FileTypes fileTypes;
63
64     @Inject
65     @Named ( "archivaConfiguration#default" )
66     ArchivaConfiguration archivaConfiguration;
67
68     @Inject
69     List<? extends ArtifactMappingProvider> artifactMappingProviders;
70
71     @Inject
72     FileLockManager fileLockManager;
73
74     private Path getRepositoryPath(String repoName) {
75         try
76         {
77             return Paths.get( Thread.currentThread( ).getContextClassLoader( ).getResource( "repositories/" + repoName ).toURI( ) );
78         }
79         catch ( URISyntaxException e )
80         {
81             throw new RuntimeException( "Could not resolve repository path " + e.getMessage( ), e );
82         }
83     }
84
85     @Before
86     public void setUp()
87         throws Exception
88     {
89         Path repoDir = getRepositoryPath( "default-repository" );
90
91         MavenManagedRepository repository = createRepository( "testRepo", "Unit Test Repo", repoDir );
92
93         FileType fileType = archivaConfiguration.getConfiguration().getRepositoryScanning().getFileTypes().get( 0 );
94         fileType.addPattern( "**/*.xml" );
95         assertEquals( FileTypes.ARTIFACTS, fileType.getId() );
96
97         fileTypes.afterConfigurationChange( null, "fileType", null );
98
99         repoContent = new ManagedDefaultRepositoryContent(repository, artifactMappingProviders, fileTypes, fileLockManager);
100         //repoContent = (ManagedRepositoryContent) lookup( ManagedRepositoryContent.class, "default" );
101     }
102
103     @Test
104     public void testGetVersionsBadArtifact()
105         throws Exception
106     {
107         assertGetVersions( "bad_artifact", Collections.emptyList() );
108     }
109
110     @Test
111     public void testGetVersionsMissingMultipleVersions()
112         throws Exception
113     {
114         assertGetVersions( "missing_metadata_b", Arrays.asList( "1.0", "1.0.1", "2.0", "2.0.1", "2.0-20070821-dev" ) );
115     }
116
117     @Test
118     public void testGetVersionsSimple()
119         throws Exception
120     {
121         assertVersions( "proxied_multi", "2.1", new String[]{ "2.1" } );
122     }
123
124     @Test
125     public void testGetVersionsSimpleYetIncomplete()
126         throws Exception
127     {
128         assertGetVersions( "incomplete_metadata_a", Collections.singletonList( "1.0" ) );
129     }
130
131     @Test
132     public void testGetVersionsSimpleYetMissing()
133         throws Exception
134     {
135         assertGetVersions( "missing_metadata_a", Collections.singletonList( "1.0" ) );
136     }
137
138     @Test
139     public void testGetVersionsSnapshotA()
140         throws Exception
141     {
142         assertVersions( "snap_shots_a", "1.0-alpha-11-SNAPSHOT",
143                         new String[]{ "1.0-alpha-11-SNAPSHOT", "1.0-alpha-11-20070221.194724-2",
144                             "1.0-alpha-11-20070302.212723-3", "1.0-alpha-11-20070303.152828-4",
145                             "1.0-alpha-11-20070305.215149-5", "1.0-alpha-11-20070307.170909-6",
146                             "1.0-alpha-11-20070314.211405-9", "1.0-alpha-11-20070316.175232-11" } );
147     }
148
149     @Test
150     public void testToMetadataPathFromProjectReference()
151     {
152         ProjectReference reference = new ProjectReference();
153         reference.setGroupId( "com.foo" );
154         reference.setArtifactId( "foo-tool" );
155
156         assertEquals( "com/foo/foo-tool/maven-metadata.xml", repoContent.toMetadataPath( reference ) );
157     }
158
159     @Test
160     public void testToMetadataPathFromVersionReference()
161     {
162         VersionedReference reference = new VersionedReference();
163         reference.setGroupId( "com.foo" );
164         reference.setArtifactId( "foo-tool" );
165         reference.setVersion( "1.0" );
166
167         assertEquals( "com/foo/foo-tool/1.0/maven-metadata.xml", repoContent.toMetadataPath( reference ) );
168     }
169
170     @Test
171     @Override
172     public void testToPathOnNullArtifactReference()
173     {
174         try
175         {
176             ArtifactReference reference = null;
177             repoContent.toPath( reference );
178             fail( "Should have failed due to null artifact reference." );
179         }
180         catch ( IllegalArgumentException e )
181         {
182             /* expected path */
183         }
184     }
185
186     @Test
187     public void testExcludeMetadataFile()
188         throws Exception
189     {
190         assertVersions( "include_xml", "1.0", new String[]{ "1.0" } );
191     }
192
193     private void assertGetVersions( String artifactId, List<String> expectedVersions )
194         throws Exception
195     {
196         ProjectReference reference = new ProjectReference();
197         reference.setGroupId( "org.apache.archiva.metadata.tests" );
198         reference.setArtifactId( artifactId );
199
200         // Use the test metadata-repository, which is already setup for
201         // These kind of version tests.
202         Path repoDir = getRepositoryPath( "metadata-repository" );
203         (( EditableManagedRepository)repoContent.getRepository()).setLocation( repoDir.toAbsolutePath().toUri() );
204
205         // Request the versions.
206         Set<String> testedVersionSet = repoContent.getVersions( reference );
207
208         // Sort the list (for asserts)
209         List<String> testedVersions = new ArrayList<>();
210         testedVersions.addAll( testedVersionSet );
211         Collections.sort( testedVersions, new VersionComparator() );
212
213         // Test the expected array of versions, to the actual tested versions
214         assertEquals( "available versions", expectedVersions, testedVersions );
215     }
216
217     private void assertVersions( String artifactId, String version, String[] expectedVersions )
218         throws Exception
219     {
220         VersionedReference reference = new VersionedReference();
221         reference.setGroupId( "org.apache.archiva.metadata.tests" );
222         reference.setArtifactId( artifactId );
223         reference.setVersion( version );
224
225         // Use the test metadata-repository, which is already setup for
226         // These kind of version tests.
227         Path repoDir = getRepositoryPath( "metadata-repository" );
228         ((EditableManagedRepository)repoContent.getRepository()).setLocation( repoDir.toAbsolutePath().toUri() );
229
230         // Request the versions.
231         Set<String> testedVersionSet = repoContent.getVersions( reference );
232
233         // Sort the list (for asserts later)
234         List<String> testedVersions = new ArrayList<>();
235         testedVersions.addAll( testedVersionSet );
236         Collections.sort( testedVersions, new VersionComparator() );
237
238         // Test the expected array of versions, to the actual tested versions
239         assertEquals( "Assert Versions: length/size", expectedVersions.length, testedVersions.size() );
240
241         for ( int i = 0; i < expectedVersions.length; i++ )
242         {
243             String actualVersion = testedVersions.get( i );
244             assertEquals( "Versions[" + i + "]", expectedVersions[i], actualVersion );
245         }
246     }
247
248
249     @Override
250     protected ArtifactReference toArtifactReference( String path )
251         throws LayoutException
252     {
253         return repoContent.toArtifactReference( path );
254     }
255
256     @Override
257     protected String toPath( ArtifactReference reference )
258     {
259         return repoContent.toPath( reference );
260     }
261
262     private Path setupRepoCopy( String source, String target) throws IOException
263     {
264         Path defaultRepo = getRepositoryPath( source );
265         Path newRepo = defaultRepo.getParent( ).resolve( target );
266         FileUtils.copyDirectory( defaultRepo.toFile( ), newRepo.toFile( ) );
267
268         MavenManagedRepository repository = createRepository( "testRepo", "Unit Test Repo", newRepo );
269
270         FileType fileType = archivaConfiguration.getConfiguration().getRepositoryScanning().getFileTypes().get( 0 );
271         fileType.addPattern( "**/*.xml" );
272         assertEquals( FileTypes.ARTIFACTS, fileType.getId() );
273
274         fileTypes.afterConfigurationChange( null, "fileType", null );
275
276         repoContent = new ManagedDefaultRepositoryContent(repository, artifactMappingProviders, fileTypes, fileLockManager);
277         return newRepo;
278
279     }
280
281     @Test
282     public void testDeleteArtifactWithType() throws IOException
283     {
284         Path deleteRepo = setupRepoCopy( "delete-repository", "delete-repository-2" );
285         assertTrue( Files.exists( deleteRepo.resolve( "org/apache/maven/samplejar/1.0/samplejar-1.0-source.jar" ) ) );
286         assertTrue( Files.exists( deleteRepo.resolve( "org/apache/maven/samplejar/1.0/samplejar-1.0.jar" ) ) );
287         assertTrue( Files.exists( deleteRepo.resolve( "org/apache/maven/samplejar/1.0/samplejar-1.0.jar.md5" ) ) );
288         assertTrue( Files.exists( deleteRepo.resolve( "org/apache/maven/samplejar/1.0/samplejar-1.0.jar.sha1" ) ) );
289         assertTrue( Files.exists( deleteRepo.resolve( "org/apache/maven/samplejar/1.0/samplejar-1.0.pom" ) ) );
290
291         ArtifactReference ref = new ArtifactReference( );
292         ref.setGroupId( "org.apache.maven" );
293         ref.setArtifactId( "samplejar" );
294         ref.setVersion( "1.0" );
295         ref.setType( "jar" );
296
297         repoContent.deleteArtifact( ref );
298
299         assertTrue( Files.exists( deleteRepo.resolve( "org/apache/maven/samplejar/1.0/samplejar-1.0-source.jar" ) ) );
300         assertFalse( Files.exists( deleteRepo.resolve( "org/apache/maven/samplejar/1.0/samplejar-1.0.jar" ) ) );
301         assertFalse( Files.exists( deleteRepo.resolve( "org/apache/maven/samplejar/1.0/samplejar-1.0.jar.md5" ) ) );
302         assertFalse( Files.exists( deleteRepo.resolve( "org/apache/maven/samplejar/1.0/samplejar-1.0.jar.sha1" ) ) );
303         assertTrue( Files.exists( deleteRepo.resolve( "org/apache/maven/samplejar/1.0/samplejar-1.0.pom" ) ) );
304
305
306     }
307
308
309     @Test
310     public void testDeleteArtifactWithClassifier() throws IOException
311     {
312         Path deleteRepo = setupRepoCopy( "default-repository", "default-repository-2" );
313         assertTrue( Files.exists( deleteRepo.resolve( "org/apache/maven/samplejar/1.0/samplejar-1.0-source.jar" ) ) );
314         assertTrue( Files.exists( deleteRepo.resolve( "org/apache/maven/samplejar/1.0/samplejar-1.0-source.jar.sha1" ) ) );
315         assertTrue( Files.exists( deleteRepo.resolve( "org/apache/maven/samplejar/1.0/samplejar-1.0.jar" ) ) );
316         assertTrue( Files.exists( deleteRepo.resolve( "org/apache/maven/samplejar/1.0/samplejar-1.0.jar.md5" ) ) );
317         assertTrue( Files.exists( deleteRepo.resolve( "org/apache/maven/samplejar/1.0/samplejar-1.0.jar.sha1" ) ) );
318         assertTrue( Files.exists( deleteRepo.resolve( "org/apache/maven/samplejar/1.0/samplejar-1.0.pom" ) ) );
319
320         ArtifactReference ref = new ArtifactReference( );
321         ref.setGroupId( "org.apache.maven" );
322         ref.setArtifactId( "samplejar" );
323         ref.setVersion( "1.0" );
324         ref.setClassifier( "source" );
325         ref.setType( "jar" );
326
327         repoContent.deleteArtifact( ref );
328
329         assertFalse( Files.exists( deleteRepo.resolve( "org/apache/maven/samplejar/1.0/samplejar-1.0-source.jar" ) ) );
330         assertFalse( Files.exists( deleteRepo.resolve( "org/apache/maven/samplejar/1.0/samplejar-1.0-source.jar.sha1" ) ) );
331         assertTrue( Files.exists( deleteRepo.resolve( "org/apache/maven/samplejar/1.0/samplejar-1.0.jar" ) ) );
332         assertTrue( Files.exists( deleteRepo.resolve( "org/apache/maven/samplejar/1.0/samplejar-1.0.jar.md5" ) ) );
333         assertTrue( Files.exists( deleteRepo.resolve( "org/apache/maven/samplejar/1.0/samplejar-1.0.jar.sha1" ) ) );
334         assertTrue( Files.exists( deleteRepo.resolve( "org/apache/maven/samplejar/1.0/samplejar-1.0.pom" ) ) );
335
336     }
337
338     @Test
339     public void testDeleteArtifactWithoutType() throws IOException
340     {
341         Path deleteRepo = setupRepoCopy( "default-repository", "default-repository-2" );
342         assertTrue( Files.exists( deleteRepo.resolve( "org/apache/maven/samplejar/1.0/samplejar-1.0-source.jar" ) ) );
343         assertTrue( Files.exists( deleteRepo.resolve( "org/apache/maven/samplejar/1.0/samplejar-1.0-source.jar.sha1" ) ) );
344         assertTrue( Files.exists( deleteRepo.resolve( "org/apache/maven/samplejar/1.0/samplejar-1.0.jar" ) ) );
345         assertTrue( Files.exists( deleteRepo.resolve( "org/apache/maven/samplejar/1.0/samplejar-1.0.jar.md5" ) ) );
346         assertTrue( Files.exists( deleteRepo.resolve( "org/apache/maven/samplejar/1.0/samplejar-1.0.jar.sha1" ) ) );
347         assertTrue( Files.exists( deleteRepo.resolve( "org/apache/maven/samplejar/1.0/samplejar-1.0.pom" ) ) );
348
349         ArtifactReference ref = new ArtifactReference( );
350         ref.setGroupId( "org.apache.maven" );
351         ref.setArtifactId( "samplejar" );
352         ref.setVersion( "1.0" );
353
354         repoContent.deleteArtifact( ref );
355
356         assertFalse( Files.exists( deleteRepo.resolve( "org/apache/maven/samplejar/1.0" ) ) );
357         assertTrue( Files.exists( deleteRepo.resolve( "org/apache/maven/samplejar" ) ) );
358
359     }
360 }