]> source.dussan.org Git - archiva.git/blob
c3fe7085f9f162edb0168a46248903b67a3b9d24
[archiva.git] /
1 package org.apache.archiva.repository.maven.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  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  */
20
21 import org.apache.archiva.common.filelock.FileLockManager;
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.LayoutException;
31 import org.apache.archiva.repository.ManagedRepositoryContent;
32 import org.apache.archiva.repository.RepositoryContent;
33 import org.apache.archiva.repository.content.ItemSelector;
34 import org.apache.archiva.repository.content.Project;
35 import org.apache.archiva.repository.content.Version;
36 import org.apache.archiva.repository.content.base.ArchivaItemSelector;
37 import org.apache.archiva.repository.maven.MavenManagedRepository;
38 import org.apache.archiva.repository.maven.metadata.storage.ArtifactMappingProvider;
39 import org.apache.commons.io.FileUtils;
40 import org.junit.Before;
41 import org.junit.Test;
42
43 import javax.inject.Inject;
44 import javax.inject.Named;
45 import java.io.IOException;
46 import java.net.URISyntaxException;
47 import java.nio.file.Files;
48 import java.nio.file.Path;
49 import java.nio.file.Paths;
50 import java.util.ArrayList;
51 import java.util.Arrays;
52 import java.util.Collections;
53 import java.util.List;
54 import java.util.Set;
55 import java.util.stream.Collectors;
56
57 import static org.junit.Assert.*;
58
59 /**
60  * ManagedDefaultRepositoryContentTest
61  */
62 public class ManagedDefaultRepositoryContentTest
63     extends AbstractManagedRepositoryContentTest
64 {
65     private ManagedDefaultRepositoryContent repoContent;
66
67     @Inject
68     FileTypes fileTypes;
69
70     @Inject
71     @Named ( "archivaConfiguration#default" )
72     ArchivaConfiguration archivaConfiguration;
73
74     @Inject
75     List<? extends ArtifactMappingProvider> artifactMappingProviders;
76
77     @Inject
78     MavenContentHelper contentHelper;
79
80     @Inject
81     FileLockManager fileLockManager;
82
83     private Path getRepositoryPath(String repoName) {
84         try
85         {
86             return Paths.get( Thread.currentThread( ).getContextClassLoader( ).getResource( "repositories/" + repoName ).toURI( ) );
87         }
88         catch ( URISyntaxException e )
89         {
90             throw new RuntimeException( "Could not resolve repository path " + e.getMessage( ), e );
91         }
92     }
93
94     @Before
95     public void setUp()
96         throws Exception
97     {
98         Path repoDir = getRepositoryPath( "default-repository" );
99
100         MavenManagedRepository repository = createRepository( "testRepo", "Unit Test Repo", repoDir );
101
102         FileType fileType = archivaConfiguration.getConfiguration().getRepositoryScanning().getFileTypes().get( 0 );
103         fileType.addPattern( "**/*.xml" );
104         assertEquals( FileTypes.ARTIFACTS, fileType.getId() );
105
106         fileTypes.afterConfigurationChange( null, "fileType", null );
107
108         repoContent = new ManagedDefaultRepositoryContent(repository, artifactMappingProviders, fileTypes, fileLockManager);
109         repoContent.setMavenContentHelper( contentHelper );
110         
111         //repoContent = (ManagedRepositoryContent) lookup( ManagedRepositoryContent.class, "default" );
112     }
113
114     @Test
115     public void testGetVersionsBadArtifact()
116         throws Exception
117     {
118         assertGetVersions( "bad_artifact", Collections.emptyList() );
119     }
120
121     @Test
122     public void testGetVersionsMissingMultipleVersions()
123         throws Exception
124     {
125         assertGetVersions( "missing_metadata_b", Arrays.asList( "1.0", "1.0.1", "2.0", "2.0.1", "2.0-20070821-dev" ) );
126     }
127
128     @Test
129     public void testGetVersionsSimple()
130         throws Exception
131     {
132         assertVersions( "proxied_multi", "2.1", new String[]{ "2.1" } );
133     }
134
135     @Test
136     public void testGetVersionsSimpleYetIncomplete()
137         throws Exception
138     {
139         assertGetVersions( "incomplete_metadata_a", Collections.singletonList( "1.0" ) );
140     }
141
142     @Test
143     public void testGetVersionsSimpleYetMissing()
144         throws Exception
145     {
146         assertGetVersions( "missing_metadata_a", Collections.singletonList( "1.0" ) );
147     }
148
149     @Test
150     public void testGetVersionsSnapshotA()
151         throws Exception
152     {
153         assertVersions( "snap_shots_a", "1.0-alpha-11-SNAPSHOT",
154                         new String[]{ "1.0-alpha-11-SNAPSHOT", "1.0-alpha-11-20070221.194724-2",
155                             "1.0-alpha-11-20070302.212723-3", "1.0-alpha-11-20070303.152828-4",
156                             "1.0-alpha-11-20070305.215149-5", "1.0-alpha-11-20070307.170909-6",
157                             "1.0-alpha-11-20070314.211405-9", "1.0-alpha-11-20070316.175232-11" } );
158     }
159
160     @Test
161     public void testToMetadataPathFromProjectReference()
162     {
163         ProjectReference reference = new ProjectReference();
164         reference.setGroupId( "com.foo" );
165         reference.setArtifactId( "foo-tool" );
166
167         assertEquals( "com/foo/foo-tool/maven-metadata.xml", repoContent.toMetadataPath( reference ) );
168     }
169
170     @Test
171     public void testToMetadataPathFromVersionReference()
172     {
173         VersionedReference reference = new VersionedReference();
174         reference.setGroupId( "com.foo" );
175         reference.setArtifactId( "foo-tool" );
176         reference.setVersion( "1.0" );
177
178         assertEquals( "com/foo/foo-tool/1.0/maven-metadata.xml", repoContent.toMetadataPath( reference ) );
179     }
180
181     @Test
182     @Override
183     public void testToPathOnNullArtifactReference()
184     {
185         try
186         {
187             ArtifactReference reference = null;
188             repoContent.toPath( reference );
189             fail( "Should have failed due to null artifact reference." );
190         }
191         catch ( IllegalArgumentException e )
192         {
193             /* expected path */
194         }
195     }
196
197     @Test
198     public void testExcludeMetadataFile()
199         throws Exception
200     {
201         assertVersions( "include_xml", "1.0", new String[]{ "1.0" } );
202     }
203
204     private void assertGetVersions( String artifactId, List<String> expectedVersions )
205         throws Exception
206     {
207         ProjectReference reference = new ProjectReference();
208         reference.setGroupId( "org.apache.archiva.metadata.tests" );
209         reference.setArtifactId( artifactId );
210
211         // Use the test metadata-repository, which is already setup for
212         // These kind of version tests.
213         Path repoDir = getRepositoryPath( "metadata-repository" );
214         (( EditableManagedRepository)repoContent.getRepository()).setLocation( repoDir.toAbsolutePath().toUri() );
215
216         // Request the versions.
217         Set<String> testedVersionSet = repoContent.getVersions( reference );
218
219         // Sort the list (for asserts)
220         VersionComparator comparator = new VersionComparator( );
221         List<String> testedVersions = new ArrayList<>();
222         testedVersions.addAll( testedVersionSet );
223         Collections.sort( testedVersions, comparator );
224
225         // Test the expected array of versions, to the actual tested versions
226         assertEquals( "available versions", expectedVersions, testedVersions );
227
228         ItemSelector selector = ArchivaItemSelector.builder( )
229             .withNamespace( "org.apache.archiva.metadata.tests" )
230             .withProjectId( artifactId )
231             .build( );
232         Project project = repoContent.getProject( selector );
233         assertNotNull( project );
234         List<String> versions = repoContent.getVersions( project ).stream().map(v -> v.getVersion()).sorted( comparator ).collect( Collectors.toList());
235         assertArrayEquals( expectedVersions.toArray(), versions.toArray( ) );
236     }
237
238     private void assertVersions( String artifactId, String version, String[] expectedVersions )
239         throws Exception
240     {
241         VersionedReference reference = new VersionedReference();
242         reference.setGroupId( "org.apache.archiva.metadata.tests" );
243         reference.setArtifactId( artifactId );
244         reference.setVersion( version );
245
246         // Use the test metadata-repository, which is already setup for
247         // These kind of version tests.
248         Path repoDir = getRepositoryPath( "metadata-repository" );
249         ((EditableManagedRepository)repoContent.getRepository()).setLocation( repoDir.toAbsolutePath().toUri() );
250
251         // Request the versions.
252         Set<String> testedVersionSet = repoContent.getVersions( reference );
253
254         // Sort the list (for asserts later)
255         final VersionComparator comparator = new VersionComparator( );
256         List<String> testedVersions = new ArrayList<>();
257         testedVersions.addAll( testedVersionSet );
258         Collections.sort( testedVersions, comparator );
259
260         // Test the expected array of versions, to the actual tested versions
261         assertEquals( "Assert Versions: length/size", expectedVersions.length, testedVersions.size() );
262
263         for ( int i = 0; i < expectedVersions.length; i++ )
264         {
265             String actualVersion = testedVersions.get( i );
266             assertEquals( "Versions[" + i + "]", expectedVersions[i], actualVersion );
267         }
268
269
270         ItemSelector selector = ArchivaItemSelector.builder( )
271             .withNamespace( "org.apache.archiva.metadata.tests" )
272             .withProjectId( artifactId )
273             .withVersion( version )
274             .build( );
275         List<String> versions = repoContent.getVersions( selector ).stream()
276             .map(v -> v.getVersion()).sorted( comparator ).collect( Collectors.toList());
277         assertArrayEquals( expectedVersions, versions.toArray( ) );
278
279
280     }
281
282
283     @Override
284     protected ArtifactReference toArtifactReference( String path )
285         throws LayoutException
286     {
287         return repoContent.toArtifactReference( path );
288     }
289
290     @Override
291     protected ItemSelector toItemSelector( String path ) throws LayoutException
292     {
293         return repoContent.toItemSelector( path );
294     }
295
296     @Override
297     protected String toPath( ArtifactReference reference )
298     {
299         return repoContent.toPath( reference );
300     }
301
302     @Override
303     protected String toPath( ItemSelector selector ) {
304         return repoContent.toPath( selector );
305     }
306
307     @Override
308     protected ManagedRepositoryContent getManaged( )
309     {
310         return repoContent;
311     }
312
313     @Override
314     protected RepositoryContent getContent( )
315     {
316         return repoContent;
317     }
318
319     private Path setupRepoCopy( String source, String target) throws IOException
320     {
321         Path defaultRepo = getRepositoryPath( source );
322         Path newRepo = defaultRepo.getParent( ).resolve( target );
323         FileUtils.copyDirectory( defaultRepo.toFile( ), newRepo.toFile( ) );
324
325         MavenManagedRepository repository = createRepository( "testRepo", "Unit Test Repo", newRepo );
326
327         FileType fileType = archivaConfiguration.getConfiguration().getRepositoryScanning().getFileTypes().get( 0 );
328         fileType.addPattern( "**/*.xml" );
329         assertEquals( FileTypes.ARTIFACTS, fileType.getId() );
330
331         fileTypes.afterConfigurationChange( null, "fileType", null );
332
333         repoContent = new ManagedDefaultRepositoryContent(repository, artifactMappingProviders, fileTypes, fileLockManager);
334         return newRepo;
335
336     }
337
338     @Test
339     public void testDeleteArtifactWithType() throws IOException, org.apache.archiva.repository.ContentNotFoundException, org.apache.archiva.repository.ContentAccessException
340     {
341         Path deleteRepo = setupRepoCopy( "delete-repository", "delete-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.jar" ) ) );
344         assertTrue( Files.exists( deleteRepo.resolve( "org/apache/maven/samplejar/1.0/samplejar-1.0.jar.md5" ) ) );
345         assertTrue( Files.exists( deleteRepo.resolve( "org/apache/maven/samplejar/1.0/samplejar-1.0.jar.sha1" ) ) );
346         assertTrue( Files.exists( deleteRepo.resolve( "org/apache/maven/samplejar/1.0/samplejar-1.0.pom" ) ) );
347
348         ArtifactReference ref = new ArtifactReference( );
349         ref.setGroupId( "org.apache.maven" );
350         ref.setArtifactId( "samplejar" );
351         ref.setVersion( "1.0" );
352         ref.setType( "jar" );
353
354         repoContent.deleteArtifact( ref );
355
356         assertTrue( Files.exists( deleteRepo.resolve( "org/apache/maven/samplejar/1.0/samplejar-1.0-source.jar" ) ) );
357         assertFalse( Files.exists( deleteRepo.resolve( "org/apache/maven/samplejar/1.0/samplejar-1.0.jar" ) ) );
358         assertTrue( Files.exists( deleteRepo.resolve( "org/apache/maven/samplejar/1.0/samplejar-1.0.jar.md5" ) ) );
359         assertTrue( Files.exists( deleteRepo.resolve( "org/apache/maven/samplejar/1.0/samplejar-1.0.jar.sha1" ) ) );
360         assertTrue( Files.exists( deleteRepo.resolve( "org/apache/maven/samplejar/1.0/samplejar-1.0.pom" ) ) );
361
362
363     }
364
365
366     @Test
367     public void testDeleteArtifactWithClassifier() throws IOException, org.apache.archiva.repository.ContentNotFoundException, org.apache.archiva.repository.ContentAccessException
368     {
369         Path deleteRepo = setupRepoCopy( "default-repository", "default-repository-2" );
370         assertTrue( Files.exists( deleteRepo.resolve( "org/apache/maven/samplejar/1.0/samplejar-1.0-source.jar" ) ) );
371         assertTrue( Files.exists( deleteRepo.resolve( "org/apache/maven/samplejar/1.0/samplejar-1.0-source.jar.sha1" ) ) );
372         assertTrue( Files.exists( deleteRepo.resolve( "org/apache/maven/samplejar/1.0/samplejar-1.0.jar" ) ) );
373         assertTrue( Files.exists( deleteRepo.resolve( "org/apache/maven/samplejar/1.0/samplejar-1.0.jar.md5" ) ) );
374         assertTrue( Files.exists( deleteRepo.resolve( "org/apache/maven/samplejar/1.0/samplejar-1.0.jar.sha1" ) ) );
375         assertTrue( Files.exists( deleteRepo.resolve( "org/apache/maven/samplejar/1.0/samplejar-1.0.pom" ) ) );
376
377         ArtifactReference ref = new ArtifactReference( );
378         ref.setGroupId( "org.apache.maven" );
379         ref.setArtifactId( "samplejar" );
380         ref.setVersion( "1.0" );
381         ref.setClassifier( "source" );
382         ref.setType( "jar" );
383
384         repoContent.deleteArtifact( ref );
385
386         assertFalse( Files.exists( deleteRepo.resolve( "org/apache/maven/samplejar/1.0/samplejar-1.0-source.jar" ) ) );
387         assertTrue( Files.exists( deleteRepo.resolve( "org/apache/maven/samplejar/1.0/samplejar-1.0-source.jar.sha1" ) ) );
388         assertTrue( Files.exists( deleteRepo.resolve( "org/apache/maven/samplejar/1.0/samplejar-1.0.jar" ) ) );
389         assertTrue( Files.exists( deleteRepo.resolve( "org/apache/maven/samplejar/1.0/samplejar-1.0.jar.md5" ) ) );
390         assertTrue( Files.exists( deleteRepo.resolve( "org/apache/maven/samplejar/1.0/samplejar-1.0.jar.sha1" ) ) );
391         assertTrue( Files.exists( deleteRepo.resolve( "org/apache/maven/samplejar/1.0/samplejar-1.0.pom" ) ) );
392
393     }
394
395     @Test
396     public void testDeleteArtifactWithoutType() throws IOException, org.apache.archiva.repository.ContentNotFoundException, org.apache.archiva.repository.ContentAccessException
397     {
398         Path deleteRepo = setupRepoCopy( "default-repository", "default-repository-2" );
399         assertTrue( Files.exists( deleteRepo.resolve( "org/apache/maven/samplejar/1.0/samplejar-1.0-source.jar" ) ) );
400         assertTrue( Files.exists( deleteRepo.resolve( "org/apache/maven/samplejar/1.0/samplejar-1.0-source.jar.sha1" ) ) );
401         assertTrue( Files.exists( deleteRepo.resolve( "org/apache/maven/samplejar/1.0/samplejar-1.0.jar" ) ) );
402         assertTrue( Files.exists( deleteRepo.resolve( "org/apache/maven/samplejar/1.0/samplejar-1.0.jar.md5" ) ) );
403         assertTrue( Files.exists( deleteRepo.resolve( "org/apache/maven/samplejar/1.0/samplejar-1.0.jar.sha1" ) ) );
404         assertTrue( Files.exists( deleteRepo.resolve( "org/apache/maven/samplejar/1.0/samplejar-1.0.pom" ) ) );
405
406         ArtifactReference ref = new ArtifactReference( );
407         ref.setGroupId( "org.apache.maven" );
408         ref.setArtifactId( "samplejar" );
409         ref.setVersion( "1.0" );
410
411         repoContent.deleteArtifact( ref );
412
413         assertFalse( Files.exists( deleteRepo.resolve( "org/apache/maven/samplejar/1.0" ) ) );
414         assertTrue( Files.exists( deleteRepo.resolve( "org/apache/maven/samplejar" ) ) );
415
416     }
417
418
419     @Test
420     public void testDeleteVersion() throws IOException, org.apache.archiva.repository.ContentNotFoundException, org.apache.archiva.repository.ContentAccessException
421     {
422         Path deleteRepo = setupRepoCopy( "delete-repository", "delete-repository-2" );
423         assertTrue( Files.exists( deleteRepo.resolve( "org/apache/maven/samplejar/1.0/samplejar-1.0-source.jar" ) ) );
424         assertTrue( Files.exists( deleteRepo.resolve( "org/apache/maven/samplejar/1.0/samplejar-1.0.jar" ) ) );
425         assertTrue( Files.exists( deleteRepo.resolve( "org/apache/maven/samplejar/1.0/samplejar-1.0.jar.md5" ) ) );
426         assertTrue( Files.exists( deleteRepo.resolve( "org/apache/maven/samplejar/1.0/samplejar-1.0.jar.sha1" ) ) );
427         assertTrue( Files.exists( deleteRepo.resolve( "org/apache/maven/samplejar/1.0/samplejar-1.0.pom" ) ) );
428
429         VersionedReference ref = new VersionedReference( ).groupId( "org.apache.maven" ).artifactId( "samplejar" ).version( "1.0" );
430
431         repoContent.deleteVersion( ref );
432
433         assertFalse( Files.exists( deleteRepo.resolve( "org/apache/maven/samplejar/1.0" ) ) );
434
435     }
436
437     @Test
438     public void testDeleteProject() throws IOException, org.apache.archiva.repository.ContentNotFoundException, org.apache.archiva.repository.ContentAccessException
439     {
440         Path deleteRepo = setupRepoCopy( "delete-repository", "delete-repository-2" );
441         assertTrue( Files.exists( deleteRepo.resolve( "org/apache/maven/samplejar/1.0/samplejar-1.0-source.jar" ) ) );
442         assertTrue( Files.exists( deleteRepo.resolve( "org/apache/maven/samplejar/1.0/samplejar-1.0.jar" ) ) );
443         assertTrue( Files.exists( deleteRepo.resolve( "org/apache/maven/samplejar/1.0/samplejar-1.0.jar.md5" ) ) );
444         assertTrue( Files.exists( deleteRepo.resolve( "org/apache/maven/samplejar/1.0/samplejar-1.0.jar.sha1" ) ) );
445         assertTrue( Files.exists( deleteRepo.resolve( "org/apache/maven/samplejar/1.0/samplejar-1.0.pom" ) ) );
446
447         ProjectReference ref = new ProjectReference( ).groupId( "org.apache.maven" ).artifactId( "samplejar" );
448
449         repoContent.deleteProject( ref );
450
451         assertFalse( Files.exists( deleteRepo.resolve( "org/apache/maven/samplejar/1.0" ) ) );
452
453     }
454 }