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