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