]> source.dussan.org Git - archiva.git/blob
92dbaefddb5123fbb6b997325540f958112c51db
[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 )
382         throws Exception
383     {
384         assertUpdatedProjectMetadata( artifactId, expectedVersions, null, null );
385     }
386
387     private void assertUpdatedProjectMetadata( String artifactId, String[] expectedVersions, String latestVersion,
388                                                String releaseVersion )
389         throws Exception
390     {
391         ManagedRepositoryContent testRepo = createTestRepoContent();
392         ProjectReference reference = new ProjectReference();
393         reference.setGroupId( "org.apache.archiva.metadata.tests" );
394         reference.setArtifactId( artifactId );
395
396         prepTestRepo( testRepo, reference );
397
398         tools.updateMetadata( testRepo, reference );
399
400         StringBuilder buf = new StringBuilder();
401         buf.append( "<metadata>\n" );
402         buf.append( "  <groupId>" ).append( reference.getGroupId() ).append( "</groupId>\n" );
403         buf.append( "  <artifactId>" ).append( reference.getArtifactId() ).append( "</artifactId>\n" );
404         // buf.append( "  <version>1.0</version>\n" );
405
406         if ( expectedVersions != null )
407         {
408             buf.append( "  <versioning>\n" );
409             if ( latestVersion != null )
410             {
411                 buf.append( "    <latest>" ).append( latestVersion ).append( "</latest>\n" );
412             }
413             if ( releaseVersion != null )
414             {
415                 buf.append( "    <release>" ).append( releaseVersion ).append( "</release>\n" );
416             }
417
418             buf.append( "    <versions>\n" );
419             for ( int i = 0; i < expectedVersions.length; i++ )
420             {
421                 buf.append( "      <version>" ).append( expectedVersions[i] ).append( "</version>\n" );
422             }
423             buf.append( "    </versions>\n" );
424             buf.append( "  </versioning>\n" );
425         }
426         buf.append( "</metadata>" );
427
428         assertMetadata( buf.toString(), testRepo, reference );
429     }
430
431     private void assertUpdatedReleaseVersionMetadata( String artifactId, String version )
432         throws Exception
433     {
434         ManagedRepositoryContent testRepo = createTestRepoContent();
435         VersionedReference reference = new VersionedReference();
436         reference.setGroupId( "org.apache.archiva.metadata.tests" );
437         reference.setArtifactId( artifactId );
438         reference.setVersion( version );
439
440         prepTestRepo( testRepo, reference );
441
442         tools.updateMetadata( testRepo, reference );
443
444         StringBuilder buf = new StringBuilder();
445         buf.append( "<metadata>\n" );
446         buf.append( "  <groupId>" ).append( reference.getGroupId() ).append( "</groupId>\n" );
447         buf.append( "  <artifactId>" ).append( reference.getArtifactId() ).append( "</artifactId>\n" );
448         buf.append( "  <version>" ).append( reference.getVersion() ).append( "</version>\n" );
449         buf.append( "</metadata>" );
450
451         assertMetadata( buf.toString(), testRepo, reference );
452     }
453
454     private void assertUpdatedSnapshotVersionMetadata( String artifactId, String version, String expectedDate,
455                                                        String expectedTime, String expectedBuildNumber )
456         throws Exception
457     {
458         ManagedRepositoryContent testRepo = createTestRepoContent();
459         VersionedReference reference = new VersionedReference();
460         reference.setGroupId( "org.apache.archiva.metadata.tests" );
461         reference.setArtifactId( artifactId );
462         reference.setVersion( version );
463
464         prepTestRepo( testRepo, reference );
465
466         tools.updateMetadata( testRepo, reference );
467
468         StringBuilder buf = new StringBuilder();
469         buf.append( "<metadata>\n" );
470         buf.append( "  <groupId>" ).append( reference.getGroupId() ).append( "</groupId>\n" );
471         buf.append( "  <artifactId>" ).append( reference.getArtifactId() ).append( "</artifactId>\n" );
472         buf.append( "  <version>" ).append( reference.getVersion() ).append( "</version>\n" );
473         buf.append( "  <versioning>\n" );
474         buf.append( "    <snapshot>\n" );
475         buf.append( "      <buildNumber>" ).append( expectedBuildNumber ).append( "</buildNumber>\n" );
476         buf.append( "      <timestamp>" );
477         buf.append( expectedDate ).append( "." ).append( expectedTime );
478         buf.append( "</timestamp>\n" );
479         buf.append( "    </snapshot>\n" );
480         buf.append( "    <lastUpdated>" ).append( expectedDate ).append( expectedTime ).append( "</lastUpdated>\n" );
481         buf.append( "  </versioning>\n" );
482         buf.append( "</metadata>" );
483
484         assertMetadata( buf.toString(), testRepo, reference );
485     }
486
487     private void createProxyConnector( String sourceRepoId, String targetRepoId )
488     {
489         ProxyConnectorConfiguration connectorConfig = new ProxyConnectorConfiguration();
490         connectorConfig.setSourceRepoId( sourceRepoId );
491         connectorConfig.setTargetRepoId( targetRepoId );
492         connectorConfig.addPolicy( ProxyConnectorConfiguration.POLICY_CHECKSUM, ChecksumPolicy.IGNORE );
493         connectorConfig.addPolicy( ProxyConnectorConfiguration.POLICY_RELEASES, ReleasesPolicy.ALWAYS );
494         connectorConfig.addPolicy( ProxyConnectorConfiguration.POLICY_SNAPSHOTS, SnapshotsPolicy.ALWAYS );
495         connectorConfig.addPolicy( ProxyConnectorConfiguration.POLICY_CACHE_FAILURES, CachedFailuresPolicy.NO );
496
497         int count = config.getConfiguration().getProxyConnectors().size();
498         config.getConfiguration().addProxyConnector( connectorConfig );
499
500         // Proper Triggering ...
501         String prefix = "proxyConnectors.proxyConnector(" + count + ")";
502         config.triggerChange( prefix + ".sourceRepoId", connectorConfig.getSourceRepoId() );
503         config.triggerChange( prefix + ".targetRepoId", connectorConfig.getTargetRepoId() );
504         config.triggerChange( prefix + ".proxyId", connectorConfig.getProxyId() );
505         config.triggerChange( prefix + ".policies.releases", connectorConfig.getPolicy( "releases", "" ) );
506         config.triggerChange( prefix + ".policies.checksum", connectorConfig.getPolicy( "checksum", "" ) );
507         config.triggerChange( prefix + ".policies.snapshots", connectorConfig.getPolicy( "snapshots", "" ) );
508         config.triggerChange( prefix + ".policies.cache-failures", connectorConfig.getPolicy( "cache-failures", "" ) );
509     }
510
511     private ManagedRepositoryContent createTestRepoContent()
512         throws Exception
513     {
514         File repoRoot = new File( "target/metadata-tests/" + getName() );
515         if ( repoRoot.exists() )
516         {
517             FileUtils.deleteDirectory( repoRoot );
518         }
519
520         repoRoot.mkdirs();
521
522         ManagedRepositoryConfiguration repoConfig = createRepository( "test-repo", "Test Repository: " + getName(),
523                                                                       repoRoot );
524
525         ManagedRepositoryContent repoContent = (ManagedRepositoryContent) lookup( ManagedRepositoryContent.class,
526                                                                                   "default" );
527         repoContent.setRepository( repoConfig );
528         return repoContent;
529     }
530
531     private void prepTestRepo( ManagedRepositoryContent repo, ProjectReference reference )
532         throws IOException
533     {
534         String groupDir = StringUtils.replaceChars( reference.getGroupId(), '.', '/' );
535         String path = groupDir + "/" + reference.getArtifactId();
536
537         File srcRepoDir = new File( "src/test/repositories/metadata-repository" );
538         File srcDir = new File( srcRepoDir, path );
539         File destDir = new File( repo.getRepoRoot(), path );
540
541         assertTrue( "Source Dir exists: " + srcDir, srcDir.exists() );
542         destDir.mkdirs();
543
544         FileUtils.copyDirectory( srcDir, destDir );
545     }
546
547     private void prepTestRepo( ManagedRepositoryContent repo, VersionedReference reference )
548         throws IOException
549     {
550         ProjectReference projectRef = new ProjectReference();
551         projectRef.setGroupId( reference.getGroupId() );
552         projectRef.setArtifactId( reference.getArtifactId() );
553
554         prepTestRepo( repo, projectRef );
555     }
556
557     protected void setUp()
558         throws Exception
559     {
560         super.setUp();
561
562         config = (MockConfiguration) lookup( ArchivaConfiguration.class.getName(), "mock" );
563         tools = (MetadataTools) lookup( MetadataTools.class );
564     }
565 }