]> source.dussan.org Git - archiva.git/blob
223238da2d9564b6feb91422e70785bfddd5ad80
[archiva.git] /
1 package org.apache.maven.archiva.repository.metadata;
2
3 /*
4  * Copyright 2001-2007 The Apache Software Foundation.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */
18
19 import org.apache.commons.io.FileUtils;
20 import org.apache.commons.lang.StringUtils;
21 import org.apache.maven.archiva.common.utils.VersionComparator;
22 import org.apache.maven.archiva.configuration.ArchivaConfiguration;
23 import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration;
24 import org.apache.maven.archiva.configuration.ProxyConnectorConfiguration;
25 import org.apache.maven.archiva.model.ProjectReference;
26 import org.apache.maven.archiva.model.VersionedReference;
27 import org.apache.maven.archiva.policies.CachedFailuresPolicy;
28 import org.apache.maven.archiva.policies.ChecksumPolicy;
29 import org.apache.maven.archiva.policies.ReleasesPolicy;
30 import org.apache.maven.archiva.policies.SnapshotsPolicy;
31 import org.apache.maven.archiva.repository.AbstractRepositoryLayerTestCase;
32 import org.apache.maven.archiva.repository.ManagedRepositoryContent;
33 import org.apache.maven.archiva.repository.MockConfiguration;
34 import org.apache.maven.archiva.repository.RemoteRepositoryContent;
35 import org.apache.maven.archiva.repository.layout.LayoutException;
36 import org.custommonkey.xmlunit.DetailedDiff;
37 import org.custommonkey.xmlunit.Diff;
38 import org.xml.sax.SAXException;
39
40 import java.io.File;
41 import java.io.IOException;
42 import java.util.ArrayList;
43 import java.util.Collections;
44 import java.util.List;
45 import java.util.Set;
46
47 import javax.xml.parsers.ParserConfigurationException;
48
49 /**
50  * MetadataToolsTest
51  *
52  * @author <a href="mailto:joakime@apache.org">Joakim Erdfelt</a>
53  * @version $Id$
54  */
55 public class MetadataToolsTest
56     extends AbstractRepositoryLayerTestCase
57 {
58     private MetadataTools tools;
59
60     protected MockConfiguration config;
61
62     public void testGatherSnapshotVersionsA()
63         throws Exception
64     {
65         assertSnapshotVersions( "snap_shots_a", "1.0-alpha-11-SNAPSHOT", new String[] {
66             "1.0-alpha-11-SNAPSHOT",
67             "1.0-alpha-11-20070221.194724-2",
68             "1.0-alpha-11-20070302.212723-3",
69             "1.0-alpha-11-20070303.152828-4",
70             "1.0-alpha-11-20070305.215149-5",
71             "1.0-alpha-11-20070307.170909-6",
72             "1.0-alpha-11-20070314.211405-9",
73             "1.0-alpha-11-20070316.175232-11" } );
74     }
75
76     public void testGatherSnapshotVersionsAWithProxies()
77         throws Exception
78     {
79         // These proxied repositories do not need to exist for the purposes of this unit test,
80         // just the repository ids are important.
81         createProxyConnector( "test-repo", "apache-snapshots" );
82         createProxyConnector( "test-repo", "internal-snapshots" );
83         createProxyConnector( "test-repo", "snapshots.codehaus.org" );
84
85         assertSnapshotVersions( "snap_shots_a", "1.0-alpha-11-SNAPSHOT", new String[] {
86             "1.0-alpha-11-SNAPSHOT",
87             "1.0-alpha-11-20070221.194724-2",
88             "1.0-alpha-11-20070302.212723-3",
89             "1.0-alpha-11-20070303.152828-4",
90             "1.0-alpha-11-20070305.215149-5",
91             "1.0-alpha-11-20070307.170909-6",
92             "1.0-alpha-11-20070314.211405-9",
93             "1.0-alpha-11-20070315.033030-10" /* Arrives in via snapshots.codehaus.org proxy */,
94             "1.0-alpha-11-20070316.175232-11" } );
95     }
96
97     public void testGetRepositorySpecificName()
98         throws Exception
99     {
100         RemoteRepositoryContent repoJavaNet = createRemoteRepositoryContent( "maven2-repository.dev.java.net",
101                                                                              "Java.net Repository for Maven 2",
102                                                                              "http://download.java.net/maven/2/",
103                                                                              "default" );
104         RemoteRepositoryContent repoCentral = createRemoteRepositoryContent( "central", "Central Global Repository",
105                                                                              "http://repo1.maven.org/maven2/",
106                                                                              "default" );
107
108         String convertedName = tools.getRepositorySpecificName( repoJavaNet,
109                                                                 "commons-lang/commons-lang/maven-metadata.xml" );
110         assertMetadataPath( "commons-lang/commons-lang/maven-metadata-maven2-repository.dev.java.net.xml",
111                             convertedName );
112
113         convertedName = tools.getRepositorySpecificName( repoCentral, "commons-lang/commons-lang/maven-metadata.xml" );
114         assertMetadataPath( "commons-lang/commons-lang/maven-metadata-central.xml", convertedName );
115     }
116
117     public void testUpdateProjectBadArtifact()
118         throws Exception
119     {
120         try
121         {
122             assertUpdatedProjectMetadata( "bad_artifact", null );
123             fail( "Should have thrown an IOException on a bad artifact." );
124         }
125         catch ( IOException e )
126         {
127             // Expected path
128         }
129     }
130
131     public void testUpdateProjectMissingMultipleVersions()
132         throws Exception
133     {
134         assertUpdatedProjectMetadata( "missing_metadata_b", new String[] {
135             "1.0",
136             "1.0.1",
137             "2.0",
138             "2.0.1",
139             "2.0-20070821-dev" }, "2.0-20070821-dev" , "2.0-20070821-dev" );
140     }
141
142     public void testUpdateProjectMissingMultipleVersionsWithProxies()
143         throws Exception
144     {
145         // Attach the (bogus) proxies to the managed repo.
146         // These proxied repositories do not need to exist for the purposes of this unit test,
147         // just the repository ids are important.
148         createProxyConnector( "test-repo", "central" );
149         createProxyConnector( "test-repo", "java.net" );
150
151         assertUpdatedProjectMetadata( "proxied_multi", new String[] {
152             "1.0-spec" /* in java.net */,
153             "1.0" /* in managed, and central */,
154             "1.0.1" /* in central */,
155             "1.1" /* in managed */,
156             "2.0-proposal-beta" /* in java.net */,
157             "2.0-spec" /* in java.net */,
158             "2.0" /* in central, and java.net */,
159             "2.0.1" /* in java.net */,
160             "2.1" /* in managed */,
161             "3.0" /* in central */,
162             "3.1" /* in central */}, "3.1", "3.1" );
163     }
164
165     public void testUpdateProjectSimpleYetIncomplete()
166         throws Exception
167     {
168         assertUpdatedProjectMetadata( "incomplete_metadata_a", new String[] { "1.0" }, "1.0", "1.0" );
169     }
170
171     public void testUpdateProjectSimpleYetMissing()
172         throws Exception
173     {
174         assertUpdatedProjectMetadata( "missing_metadata_a", new String[] { "1.0" }, "1.0", "1.0" );
175     }
176
177     public void testUpdateVersionSimple10()
178         throws Exception
179     {
180         assertUpdatedReleaseVersionMetadata( "missing_metadata_a", "1.0" );
181     }
182
183     public void testUpdateVersionSimple20()
184         throws Exception
185     {
186         assertUpdatedReleaseVersionMetadata( "missing_metadata_b", "2.0" );
187     }
188
189     public void testUpdateVersionSimple20NotSnapshot()
190         throws Exception
191     {
192         assertUpdatedReleaseVersionMetadata( "missing_metadata_b", "2.0-20070821-dev" );
193     }
194
195     public void testUpdateVersionSnapshotA()
196         throws Exception
197     {
198         assertUpdatedSnapshotVersionMetadata( "snap_shots_a", "1.0-alpha-11-SNAPSHOT", "20070316", "175232", "11" );
199     }
200
201     public void testToPathFromVersionReference()
202     {
203         VersionedReference reference = new VersionedReference();
204         reference.setGroupId( "com.foo" );
205         reference.setArtifactId( "foo-tool" );
206         reference.setVersion( "1.0" );
207
208         assertEquals( "com/foo/foo-tool/1.0/maven-metadata.xml", tools.toPath( reference ) );
209     }
210
211     public void testToPathFromProjectReference()
212     {
213         ProjectReference reference = new ProjectReference();
214         reference.setGroupId( "com.foo" );
215         reference.setArtifactId( "foo-tool" );
216
217         assertEquals( "com/foo/foo-tool/maven-metadata.xml", tools.toPath( reference ) );
218     }
219
220     public void testToProjectReferenceFooTools()
221         throws RepositoryMetadataException
222     {
223         assertProjectReference( "com.foo", "foo-tools", "com/foo/foo-tools/maven-metadata.xml" );
224     }
225
226     public void testToProjectReferenceAReallyLongPath()
227         throws RepositoryMetadataException
228     {
229         String groupId = "net.i.have.a.really.long.path.just.for.the.hell.of.it";
230         String artifactId = "a";
231         String path = "net/i/have/a/really/long/path/just/for/the/hell/of/it/a/maven-metadata.xml";
232
233         assertProjectReference( groupId, artifactId, path );
234     }
235
236     public void testToProjectReferenceCommonsLang()
237         throws RepositoryMetadataException
238     {
239         String groupId = "commons-lang";
240         String artifactId = "commons-lang";
241         String path = "commons-lang/commons-lang/maven-metadata.xml";
242
243         assertProjectReference( groupId, artifactId, path );
244     }
245
246     private void assertProjectReference( String groupId, String artifactId, String path )
247         throws RepositoryMetadataException
248     {
249         ProjectReference reference = tools.toProjectReference( path );
250
251         assertNotNull( "Reference should not be null.", reference );
252         assertEquals( "ProjectReference.groupId", groupId, reference.getGroupId() );
253         assertEquals( "ProjectReference.artifactId", artifactId, reference.getArtifactId() );
254     }
255
256     public void testToVersionedReferenceFooTool()
257         throws RepositoryMetadataException
258     {
259         String groupId = "com.foo";
260         String artifactId = "foo-tool";
261         String version = "1.0";
262         String path = "com/foo/foo-tool/1.0/maven-metadata.xml";
263
264         assertVersionedReference( groupId, artifactId, version, path );
265     }
266
267     public void testToVersionedReferenceAReallyLongPath()
268         throws RepositoryMetadataException
269     {
270         String groupId = "net.i.have.a.really.long.path.just.for.the.hell.of.it";
271         String artifactId = "a";
272         String version = "1.1-alpha-1";
273         String path = "net/i/have/a/really/long/path/just/for/the/hell/of/it/a/1.1-alpha-1/maven-metadata.xml";
274
275         assertVersionedReference( groupId, artifactId, version, path );
276     }
277
278     public void testToVersionedReferenceCommonsLang()
279         throws RepositoryMetadataException
280     {
281         String groupId = "commons-lang";
282         String artifactId = "commons-lang";
283         String version = "2.1";
284         String path = "commons-lang/commons-lang/2.1/maven-metadata.xml";
285
286         assertVersionedReference( groupId, artifactId, version, path );
287     }
288
289     public void testToVersionedReferenceSnapshot()
290         throws RepositoryMetadataException
291     {
292         String groupId = "com.foo";
293         String artifactId = "foo-connector";
294         String version = "2.1-SNAPSHOT";
295         String path = "com/foo/foo-connector/2.1-SNAPSHOT/maven-metadata.xml";
296
297         assertVersionedReference( groupId, artifactId, version, path );
298     }
299
300     private void assertVersionedReference( String groupId, String artifactId, String version, String path )
301         throws RepositoryMetadataException
302     {
303         VersionedReference reference = tools.toVersionedReference( path );
304         assertNotNull( "Reference should not be null.", reference );
305
306         assertEquals( "VersionedReference.groupId", groupId, reference.getGroupId() );
307         assertEquals( "VersionedReference.artifactId", artifactId, reference.getArtifactId() );
308         assertEquals( "VersionedReference.version", version, reference.getVersion() );
309     }
310
311     private void assertSnapshotVersions( String artifactId, String version, String[] expectedVersions )
312         throws Exception
313     {
314         File repoRootDir = new File( "src/test/repositories/metadata-repository" );
315
316         VersionedReference reference = new VersionedReference();
317         reference.setGroupId( "org.apache.archiva.metadata.tests" );
318         reference.setArtifactId( artifactId );
319         reference.setVersion( version );
320
321         ManagedRepositoryConfiguration repo = createRepository( "test-repo", "Test Repository: " + getName(),
322                                                                 repoRootDir );
323         ManagedRepositoryContent repoContent = (ManagedRepositoryContent) lookup( ManagedRepositoryContent.class,
324                                                                                   "default" );
325         repoContent.setRepository( repo );
326
327         Set<String> testedVersionSet = tools.gatherSnapshotVersions( repoContent, reference );
328
329         // Sort the list (for asserts)
330         List<String> testedVersions = new ArrayList<String>();
331         testedVersions.addAll( testedVersionSet );
332         Collections.sort( testedVersions, new VersionComparator() );
333
334         // Test the expected array of versions, to the actual tested versions
335         assertEquals( "Assert Snapshot Versions: length/size", expectedVersions.length, testedVersions.size() );
336
337         for ( int i = 0; i < expectedVersions.length; i++ )
338         {
339             String actualVersion = testedVersions.get( i );
340             assertEquals( "Snapshot Versions[" + i + "]", expectedVersions[i], actualVersion );
341         }
342     }
343
344     private void assertMetadata( String expectedMetadata, ManagedRepositoryContent repository,
345                                  ProjectReference reference )
346         throws LayoutException, IOException, SAXException, ParserConfigurationException
347     {
348         File metadataFile = new File( repository.getRepoRoot(), tools.toPath( reference ) );
349         String actualMetadata = FileUtils.readFileToString( metadataFile, null );
350
351         DetailedDiff detailedDiff = new DetailedDiff( new Diff( expectedMetadata, actualMetadata ) );
352         if ( !detailedDiff.similar() )
353         {
354             // If it isn't similar, dump the difference.
355             assertEquals( expectedMetadata, actualMetadata );
356         }
357     }
358
359     private void assertMetadata( String expectedMetadata, ManagedRepositoryContent repository,
360                                  VersionedReference reference )
361         throws LayoutException, IOException, SAXException, ParserConfigurationException
362     {
363         File metadataFile = new File( repository.getRepoRoot(), tools.toPath( reference ) );
364         String actualMetadata = FileUtils.readFileToString( metadataFile, null );
365
366         DetailedDiff detailedDiff = new DetailedDiff( new Diff( expectedMetadata, actualMetadata ) );
367         if ( !detailedDiff.similar() )
368         {
369             // If it isn't similar, dump the difference.
370             assertEquals( expectedMetadata, actualMetadata );
371         }
372     }
373
374     private void assertMetadataPath( String expected, String actual )
375     {
376         assertEquals( "Repository Specific Metadata Path", expected, actual );
377     }
378
379     private void assertUpdatedProjectMetadata( String artifactId, String[] expectedVersions )
380         throws Exception
381     {
382         assertUpdatedProjectMetadata( artifactId, expectedVersions, null, null );
383     }
384
385     private void assertUpdatedProjectMetadata( String artifactId, String[] expectedVersions, String latestVersion,
386                                                String releaseVersion )
387         throws Exception
388     {
389         ManagedRepositoryContent testRepo = createTestRepoContent();
390         ProjectReference reference = new ProjectReference();
391         reference.setGroupId( "org.apache.archiva.metadata.tests" );
392         reference.setArtifactId( artifactId );
393
394         prepTestRepo( testRepo, reference );
395
396         tools.updateMetadata( testRepo, reference );
397
398         StringBuilder buf = new StringBuilder();
399         buf.append( "<metadata>\n" );
400         buf.append( "  <groupId>" ).append( reference.getGroupId() ).append( "</groupId>\n" );
401         buf.append( "  <artifactId>" ).append( reference.getArtifactId() ).append( "</artifactId>\n" );
402         // buf.append( "  <version>1.0</version>\n" );
403
404         if ( expectedVersions != null )
405         {
406             buf.append( "  <versioning>\n" );
407             if ( latestVersion != null )
408             {
409                 buf.append( "    <latest>" ).append( latestVersion ).append( "</latest>\n" );
410             }
411             if ( releaseVersion != null )
412             {
413                 buf.append( "    <release>" ).append( releaseVersion ).append( "</release>\n" );
414             }
415
416             buf.append( "    <versions>\n" );
417             for ( int i = 0; i < expectedVersions.length; i++ )
418             {
419                 buf.append( "      <version>" ).append( expectedVersions[i] ).append( "</version>\n" );
420             }
421             buf.append( "    </versions>\n" );
422             buf.append( "  </versioning>\n" );
423         }
424         buf.append( "</metadata>" );
425
426         assertMetadata( buf.toString(), testRepo, reference );
427     }
428
429     private void assertUpdatedReleaseVersionMetadata( String artifactId, String version )
430         throws Exception
431     {
432         ManagedRepositoryContent testRepo = createTestRepoContent();
433         VersionedReference reference = new VersionedReference();
434         reference.setGroupId( "org.apache.archiva.metadata.tests" );
435         reference.setArtifactId( artifactId );
436         reference.setVersion( version );
437
438         prepTestRepo( testRepo, reference );
439
440         tools.updateMetadata( testRepo, reference );
441
442         StringBuilder buf = new StringBuilder();
443         buf.append( "<metadata>\n" );
444         buf.append( "  <groupId>" ).append( reference.getGroupId() ).append( "</groupId>\n" );
445         buf.append( "  <artifactId>" ).append( reference.getArtifactId() ).append( "</artifactId>\n" );
446         buf.append( "  <version>" ).append( reference.getVersion() ).append( "</version>\n" );
447         buf.append( "</metadata>" );
448
449         assertMetadata( buf.toString(), testRepo, reference );
450     }
451
452     private void assertUpdatedSnapshotVersionMetadata( String artifactId, String version, String expectedDate,
453                                                        String expectedTime, String expectedBuildNumber )
454         throws Exception
455     {
456         ManagedRepositoryContent testRepo = createTestRepoContent();
457         VersionedReference reference = new VersionedReference();
458         reference.setGroupId( "org.apache.archiva.metadata.tests" );
459         reference.setArtifactId( artifactId );
460         reference.setVersion( version );
461
462         prepTestRepo( testRepo, reference );
463
464         tools.updateMetadata( testRepo, reference );
465
466         StringBuilder buf = new StringBuilder();
467         buf.append( "<metadata>\n" );
468         buf.append( "  <groupId>" ).append( reference.getGroupId() ).append( "</groupId>\n" );
469         buf.append( "  <artifactId>" ).append( reference.getArtifactId() ).append( "</artifactId>\n" );
470         buf.append( "  <version>" ).append( reference.getVersion() ).append( "</version>\n" );
471         buf.append( "  <versioning>\n" );
472         buf.append( "    <snapshot>\n" );
473         buf.append( "      <buildNumber>" ).append( expectedBuildNumber ).append( "</buildNumber>\n" );
474         buf.append( "      <timestamp>" );
475         buf.append( expectedDate ).append( "." ).append( expectedTime );
476         buf.append( "</timestamp>\n" );
477         buf.append( "    </snapshot>\n" );
478         buf.append( "    <lastUpdated>" ).append( expectedDate ).append( expectedTime ).append( "</lastUpdated>\n" );
479         buf.append( "  </versioning>\n" );
480         buf.append( "</metadata>" );
481
482         assertMetadata( buf.toString(), testRepo, reference );
483     }
484
485     private void createProxyConnector( String sourceRepoId, String targetRepoId )
486     {
487         ProxyConnectorConfiguration connectorConfig = new ProxyConnectorConfiguration();
488         connectorConfig.setSourceRepoId( sourceRepoId );
489         connectorConfig.setTargetRepoId( targetRepoId );
490         connectorConfig.addPolicy( ProxyConnectorConfiguration.POLICY_CHECKSUM, ChecksumPolicy.IGNORE );
491         connectorConfig.addPolicy( ProxyConnectorConfiguration.POLICY_RELEASES, ReleasesPolicy.ALWAYS );
492         connectorConfig.addPolicy( ProxyConnectorConfiguration.POLICY_SNAPSHOTS, SnapshotsPolicy.ALWAYS );
493         connectorConfig.addPolicy( ProxyConnectorConfiguration.POLICY_CACHE_FAILURES, CachedFailuresPolicy.NO );
494
495         int count = config.getConfiguration().getProxyConnectors().size();
496         config.getConfiguration().addProxyConnector( connectorConfig );
497
498         // Proper Triggering ...
499         String prefix = "proxyConnectors.proxyConnector(" + count + ")";
500         config.triggerChange( prefix + ".sourceRepoId", connectorConfig.getSourceRepoId() );
501         config.triggerChange( prefix + ".targetRepoId", connectorConfig.getTargetRepoId() );
502         config.triggerChange( prefix + ".proxyId", connectorConfig.getProxyId() );
503         config.triggerChange( prefix + ".policies.releases", connectorConfig.getPolicy( "releases", "" ) );
504         config.triggerChange( prefix + ".policies.checksum", connectorConfig.getPolicy( "checksum", "" ) );
505         config.triggerChange( prefix + ".policies.snapshots", connectorConfig.getPolicy( "snapshots", "" ) );
506         config.triggerChange( prefix + ".policies.cache-failures", connectorConfig.getPolicy( "cache-failures", "" ) );
507     }
508
509     private ManagedRepositoryContent createTestRepoContent()
510         throws Exception
511     {
512         File repoRoot = new File( "target/metadata-tests/" + getName() );
513         if ( repoRoot.exists() )
514         {
515             FileUtils.deleteDirectory( repoRoot );
516         }
517
518         repoRoot.mkdirs();
519
520         ManagedRepositoryConfiguration repoConfig = createRepository( "test-repo", "Test Repository: " + getName(),
521                                                                       repoRoot );
522
523         ManagedRepositoryContent repoContent = (ManagedRepositoryContent) lookup( ManagedRepositoryContent.class,
524                                                                                   "default" );
525         repoContent.setRepository( repoConfig );
526         return repoContent;
527     }
528
529     private void prepTestRepo( ManagedRepositoryContent repo, ProjectReference reference )
530         throws IOException
531     {
532         String groupDir = StringUtils.replaceChars( reference.getGroupId(), '.', '/' );
533         String path = groupDir + "/" + reference.getArtifactId();
534
535         File srcRepoDir = new File( "src/test/repositories/metadata-repository" );
536         File srcDir = new File( srcRepoDir, path );
537         File destDir = new File( repo.getRepoRoot(), path );
538
539         assertTrue( "Source Dir exists: " + srcDir, srcDir.exists() );
540         destDir.mkdirs();
541
542         FileUtils.copyDirectory( srcDir, destDir );
543     }
544
545     private void prepTestRepo( ManagedRepositoryContent repo, VersionedReference reference )
546         throws IOException
547     {
548         ProjectReference projectRef = new ProjectReference();
549         projectRef.setGroupId( reference.getGroupId() );
550         projectRef.setArtifactId( reference.getArtifactId() );
551
552         prepTestRepo( repo, projectRef );
553     }
554
555     protected void setUp()
556         throws Exception
557     {
558         super.setUp();
559
560         config = (MockConfiguration) lookup( ArchivaConfiguration.class.getName(), "mock" );
561         tools = (MetadataTools) lookup( MetadataTools.class );
562     }
563 }