1 package org.apache.maven.archiva.repository.metadata;
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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
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.ManagedRepositoryConfiguration;
26 import org.apache.maven.archiva.configuration.ProxyConnectorConfiguration;
27 import org.apache.maven.archiva.model.ProjectReference;
28 import org.apache.maven.archiva.model.VersionedReference;
29 import org.apache.maven.archiva.policies.CachedFailuresPolicy;
30 import org.apache.maven.archiva.policies.ChecksumPolicy;
31 import org.apache.maven.archiva.policies.ReleasesPolicy;
32 import org.apache.maven.archiva.policies.SnapshotsPolicy;
33 import org.apache.maven.archiva.repository.AbstractRepositoryLayerTestCase;
34 import org.apache.maven.archiva.repository.ManagedRepositoryContent;
35 import org.apache.maven.archiva.repository.MockConfiguration;
36 import org.apache.maven.archiva.repository.RemoteRepositoryContent;
37 import org.apache.maven.archiva.repository.layout.LayoutException;
38 import org.custommonkey.xmlunit.DetailedDiff;
39 import org.custommonkey.xmlunit.Diff;
40 import org.junit.Test;
41 import org.springframework.test.context.ContextConfiguration;
42 import org.xml.sax.SAXException;
45 import java.io.IOException;
46 import java.util.ArrayList;
47 import java.util.Collections;
48 import java.util.List;
50 import javax.inject.Inject;
51 import javax.inject.Named;
52 import javax.xml.parsers.ParserConfigurationException;
54 import static org.junit.Assert.*;
61 @ContextConfiguration( locations = { "classpath*:/META-INF/spring-context.xml", "classpath:/spring-context-metadata-tools-test.xml" } )
62 public class MetadataToolsTest
63 extends AbstractRepositoryLayerTestCase
65 @Inject @Named(value = "metadataTools#test")
66 private MetadataTools tools;
69 @Named( value = "archivaConfiguration#mock" )
70 protected MockConfiguration config;
73 public void testGatherSnapshotVersionsA()
76 assertSnapshotVersions( "snap_shots_a", "1.0-alpha-11-SNAPSHOT",
77 new String[]{ "1.0-alpha-11-SNAPSHOT", "1.0-alpha-11-20070221.194724-2",
78 "1.0-alpha-11-20070302.212723-3", "1.0-alpha-11-20070303.152828-4",
79 "1.0-alpha-11-20070305.215149-5", "1.0-alpha-11-20070307.170909-6",
80 "1.0-alpha-11-20070314.211405-9", "1.0-alpha-11-20070316.175232-11" } );
84 public void testGatherSnapshotVersionsAWithProxies()
87 // These proxied repositories do not need to exist for the purposes of this unit test,
88 // just the repository ids are important.
89 createProxyConnector( "test-repo", "apache-snapshots" );
90 createProxyConnector( "test-repo", "internal-snapshots" );
91 createProxyConnector( "test-repo", "snapshots.codehaus.org" );
93 assertSnapshotVersions( "snap_shots_a", "1.0-alpha-11-SNAPSHOT",
94 new String[]{ "1.0-alpha-11-SNAPSHOT", "1.0-alpha-11-20070221.194724-2",
95 "1.0-alpha-11-20070302.212723-3", "1.0-alpha-11-20070303.152828-4",
96 "1.0-alpha-11-20070305.215149-5", "1.0-alpha-11-20070307.170909-6",
97 "1.0-alpha-11-20070314.211405-9", "1.0-alpha-11-20070315.033030-10"
98 /* Arrives in via snapshots.codehaus.org proxy */,
99 "1.0-alpha-11-20070316.175232-11" } );
103 public void testGetRepositorySpecificName()
106 RemoteRepositoryContent repoJavaNet =
107 createRemoteRepositoryContent( "maven2-repository.dev.java.net", "Java.net Repository for Maven 2",
108 "http://download.java.net/maven/2/", "default" );
109 RemoteRepositoryContent repoCentral =
110 createRemoteRepositoryContent( "central", "Central Global Repository", "http://repo1.maven.org/maven2/",
113 String convertedName =
114 tools.getRepositorySpecificName( repoJavaNet, "commons-lang/commons-lang/maven-metadata.xml" );
115 assertMetadataPath( "commons-lang/commons-lang/maven-metadata-maven2-repository.dev.java.net.xml",
118 convertedName = tools.getRepositorySpecificName( repoCentral, "commons-lang/commons-lang/maven-metadata.xml" );
119 assertMetadataPath( "commons-lang/commons-lang/maven-metadata-central.xml", convertedName );
122 // TODO: replace with group tests
123 // public void testUpdateProjectBadArtifact()
128 // assertUpdatedProjectMetadata( "bad_artifact", null );
129 // fail( "Should have thrown an IOException on a bad artifact." );
131 // catch ( IOException e )
138 public void testUpdateProjectNonExistingVersion()
141 ManagedRepositoryContent testRepo = createTestRepoContent();
142 ProjectReference reference = new ProjectReference();
143 reference.setGroupId( "org.apache.archiva.metadata.tests" );
144 reference.setArtifactId( "missing_artifact" );
146 prepTestRepo( testRepo, reference );
148 // check metadata prior to update -- should contain the non-existing artifact version
149 assertProjectMetadata( testRepo, reference, "missing_artifact",
150 new String[]{ "1.0-SNAPSHOT", "1.1-SNAPSHOT", "1.2-SNAPSHOT" }, "1.2-SNAPSHOT", null );
152 tools.updateMetadata( testRepo, reference );
154 // metadata should not contain the non-existing artifact version -- 1.1-SNAPSHOT
155 assertProjectMetadata( testRepo, reference, "missing_artifact", new String[]{ "1.0-SNAPSHOT", "1.2-SNAPSHOT" },
156 "1.2-SNAPSHOT", null );
160 public void testUpdateProjectMissingMultipleVersions()
163 assertUpdatedProjectMetadata( "missing_metadata_b",
164 new String[]{ "1.0", "1.0.1", "2.0", "2.0.1", "2.0-20070821-dev" },
165 "2.0-20070821-dev", "2.0-20070821-dev" );
169 public void testUpdateProjectMissingMultipleVersionsWithProxies()
172 // Attach the (bogus) proxies to the managed repo.
173 // These proxied repositories do not need to exist for the purposes of this unit test,
174 // just the repository ids are important.
175 createProxyConnector( "test-repo", "central" );
176 createProxyConnector( "test-repo", "java.net" );
178 assertUpdatedProjectMetadata( "proxied_multi",
179 new String[]{ "1.0-spec" /* in java.net */, "1.0" /* in managed, and central */,
180 "1.0.1" /* in central */, "1.1" /* in managed */, "2.0-proposal-beta"
181 /* in java.net */, "2.0-spec" /* in java.net */, "2.0"
182 /* in central, and java.net */, "2.0.1" /* in java.net */, "2.1"
183 /* in managed */, "3.0" /* in central */, "3.1" /* in central */ }, "3.1",
188 public void testUpdateProjectSimpleYetIncomplete()
191 assertUpdatedProjectMetadata( "incomplete_metadata_a", new String[]{ "1.0" }, "1.0", "1.0" );
195 public void testUpdateProjectSimpleYetMissing()
198 assertUpdatedProjectMetadata( "missing_metadata_a", new String[]{ "1.0" }, "1.0", "1.0" );
202 public void testUpdateVersionSimple10()
205 assertUpdatedReleaseVersionMetadata( "missing_metadata_a", "1.0" );
209 public void testUpdateVersionSimple20()
212 assertUpdatedReleaseVersionMetadata( "missing_metadata_b", "2.0" );
216 public void testUpdateVersionSimple20NotSnapshot()
219 assertUpdatedReleaseVersionMetadata( "missing_metadata_b", "2.0-20070821-dev" );
223 public void testUpdateVersionSnapshotA()
226 assertUpdatedSnapshotVersionMetadata( "snap_shots_a", "1.0-alpha-11-SNAPSHOT", "20070316", "175232", "11" );
230 public void testToPathFromVersionReference()
232 VersionedReference reference = new VersionedReference();
233 reference.setGroupId( "com.foo" );
234 reference.setArtifactId( "foo-tool" );
235 reference.setVersion( "1.0" );
237 assertEquals( "com/foo/foo-tool/1.0/maven-metadata.xml", tools.toPath( reference ) );
241 public void testToPathFromProjectReference()
243 ProjectReference reference = new ProjectReference();
244 reference.setGroupId( "com.foo" );
245 reference.setArtifactId( "foo-tool" );
247 assertEquals( "com/foo/foo-tool/maven-metadata.xml", tools.toPath( reference ) );
251 public void testToProjectReferenceFooTools()
252 throws RepositoryMetadataException
254 assertProjectReference( "com.foo", "foo-tools", "com/foo/foo-tools/maven-metadata.xml" );
258 public void testToProjectReferenceAReallyLongPath()
259 throws RepositoryMetadataException
261 String groupId = "net.i.have.a.really.long.path.just.for.the.hell.of.it";
262 String artifactId = "a";
263 String path = "net/i/have/a/really/long/path/just/for/the/hell/of/it/a/maven-metadata.xml";
265 assertProjectReference( groupId, artifactId, path );
269 public void testToProjectReferenceCommonsLang()
270 throws RepositoryMetadataException
272 String groupId = "commons-lang";
273 String artifactId = "commons-lang";
274 String path = "commons-lang/commons-lang/maven-metadata.xml";
276 assertProjectReference( groupId, artifactId, path );
279 private void assertProjectReference( String groupId, String artifactId, String path )
280 throws RepositoryMetadataException
282 ProjectReference reference = tools.toProjectReference( path );
284 assertNotNull( "Reference should not be null.", reference );
285 assertEquals( "ProjectReference.groupId", groupId, reference.getGroupId() );
286 assertEquals( "ProjectReference.artifactId", artifactId, reference.getArtifactId() );
290 public void testToVersionedReferenceFooTool()
291 throws RepositoryMetadataException
293 String groupId = "com.foo";
294 String artifactId = "foo-tool";
295 String version = "1.0";
296 String path = "com/foo/foo-tool/1.0/maven-metadata.xml";
298 assertVersionedReference( groupId, artifactId, version, path );
302 public void testToVersionedReferenceAReallyLongPath()
303 throws RepositoryMetadataException
305 String groupId = "net.i.have.a.really.long.path.just.for.the.hell.of.it";
306 String artifactId = "a";
307 String version = "1.1-alpha-1";
308 String path = "net/i/have/a/really/long/path/just/for/the/hell/of/it/a/1.1-alpha-1/maven-metadata.xml";
310 assertVersionedReference( groupId, artifactId, version, path );
314 public void testToVersionedReferenceCommonsLang()
315 throws RepositoryMetadataException
317 String groupId = "commons-lang";
318 String artifactId = "commons-lang";
319 String version = "2.1";
320 String path = "commons-lang/commons-lang/2.1/maven-metadata.xml";
322 assertVersionedReference( groupId, artifactId, version, path );
326 public void testToVersionedReferenceSnapshot()
327 throws RepositoryMetadataException
329 String groupId = "com.foo";
330 String artifactId = "foo-connector";
331 String version = "2.1-SNAPSHOT";
332 String path = "com/foo/foo-connector/2.1-SNAPSHOT/maven-metadata.xml";
334 assertVersionedReference( groupId, artifactId, version, path );
337 private void assertVersionedReference( String groupId, String artifactId, String version, String path )
338 throws RepositoryMetadataException
340 VersionedReference reference = tools.toVersionedReference( path );
341 assertNotNull( "Reference should not be null.", reference );
343 assertEquals( "VersionedReference.groupId", groupId, reference.getGroupId() );
344 assertEquals( "VersionedReference.artifactId", artifactId, reference.getArtifactId() );
345 assertEquals( "VersionedReference.version", version, reference.getVersion() );
348 private void assertSnapshotVersions( String artifactId, String version, String[] expectedVersions )
351 File repoRootDir = new File( "src/test/repositories/metadata-repository" );
353 VersionedReference reference = new VersionedReference();
354 reference.setGroupId( "org.apache.archiva.metadata.tests" );
355 reference.setArtifactId( artifactId );
356 reference.setVersion( version );
358 ManagedRepositoryConfiguration repo =
359 createRepository( "test-repo", "Test Repository: " + name.getMethodName(), repoRootDir );
360 ManagedRepositoryContent repoContent =
361 applicationContext.getBean( "managedRepositoryContent#default", ManagedRepositoryContent.class );
362 repoContent.setRepository( repo );
364 Set<String> testedVersionSet = tools.gatherSnapshotVersions( repoContent, reference );
366 // Sort the list (for asserts)
367 List<String> testedVersions = new ArrayList<String>();
368 testedVersions.addAll( testedVersionSet );
369 Collections.sort( testedVersions, new VersionComparator() );
371 // Test the expected array of versions, to the actual tested versions
372 assertEquals( "Assert Snapshot Versions: length/size", expectedVersions.length, testedVersions.size() );
374 for ( int i = 0; i < expectedVersions.length; i++ )
376 String actualVersion = testedVersions.get( i );
377 assertEquals( "Snapshot Versions[" + i + "]", expectedVersions[i], actualVersion );
381 private void assertMetadata( String expectedMetadata, ManagedRepositoryContent repository,
382 ProjectReference reference )
383 throws LayoutException, IOException, SAXException, ParserConfigurationException
385 File metadataFile = new File( repository.getRepoRoot(), tools.toPath( reference ) );
386 String actualMetadata = FileUtils.readFileToString( metadataFile, null );
388 DetailedDiff detailedDiff = new DetailedDiff( new Diff( expectedMetadata, actualMetadata ) );
389 if ( !detailedDiff.similar() )
391 // If it isn't similar, dump the difference.
392 assertEquals( expectedMetadata, actualMetadata );
396 private void assertMetadata( String expectedMetadata, ManagedRepositoryContent repository,
397 VersionedReference reference )
398 throws LayoutException, IOException, SAXException, ParserConfigurationException
400 File metadataFile = new File( repository.getRepoRoot(), tools.toPath( reference ) );
401 String actualMetadata = FileUtils.readFileToString( metadataFile, null );
403 DetailedDiff detailedDiff = new DetailedDiff( new Diff( expectedMetadata, actualMetadata ) );
404 if ( !detailedDiff.similar() )
406 // If it isn't similar, dump the difference.
407 assertEquals( expectedMetadata, actualMetadata );
411 private void assertMetadataPath( String expected, String actual )
413 assertEquals( "Repository Specific Metadata Path", expected, actual );
416 private void assertUpdatedProjectMetadata( String artifactId, String[] expectedVersions, String latestVersion,
417 String releaseVersion )
420 ManagedRepositoryContent testRepo = createTestRepoContent();
421 ProjectReference reference = new ProjectReference();
422 reference.setGroupId( "org.apache.archiva.metadata.tests" );
423 reference.setArtifactId( artifactId );
425 prepTestRepo( testRepo, reference );
427 tools.updateMetadata( testRepo, reference );
429 StringBuilder buf = new StringBuilder();
430 buf.append( "<metadata>\n" );
431 buf.append( " <groupId>" ).append( reference.getGroupId() ).append( "</groupId>\n" );
432 buf.append( " <artifactId>" ).append( reference.getArtifactId() ).append( "</artifactId>\n" );
433 // buf.append( " <version>1.0</version>\n" );
435 if ( expectedVersions != null )
437 buf.append( " <versioning>\n" );
438 if ( latestVersion != null )
440 buf.append( " <latest>" ).append( latestVersion ).append( "</latest>\n" );
442 if ( releaseVersion != null )
444 buf.append( " <release>" ).append( releaseVersion ).append( "</release>\n" );
447 buf.append( " <versions>\n" );
448 for ( int i = 0; i < expectedVersions.length; i++ )
450 buf.append( " <version>" ).append( expectedVersions[i] ).append( "</version>\n" );
452 buf.append( " </versions>\n" );
453 buf.append( " </versioning>\n" );
455 buf.append( "</metadata>" );
457 assertMetadata( buf.toString(), testRepo, reference );
460 private void assertProjectMetadata( ManagedRepositoryContent testRepo, ProjectReference reference,
461 String artifactId, String[] expectedVersions, String latestVersion,
462 String releaseVersion )
465 StringBuilder buf = new StringBuilder();
466 buf.append( "<metadata>\n" );
467 buf.append( " <groupId>" ).append( reference.getGroupId() ).append( "</groupId>\n" );
468 buf.append( " <artifactId>" ).append( reference.getArtifactId() ).append( "</artifactId>\n" );
470 if ( expectedVersions != null )
472 buf.append( " <versioning>\n" );
473 if ( latestVersion != null )
475 buf.append( " <latest>" ).append( latestVersion ).append( "</latest>\n" );
477 if ( releaseVersion != null )
479 buf.append( " <release>" ).append( releaseVersion ).append( "</release>\n" );
482 buf.append( " <versions>\n" );
483 for ( int i = 0; i < expectedVersions.length; i++ )
485 buf.append( " <version>" ).append( expectedVersions[i] ).append( "</version>\n" );
487 buf.append( " </versions>\n" );
488 buf.append( " </versioning>\n" );
490 buf.append( "</metadata>" );
492 assertMetadata( buf.toString(), testRepo, reference );
495 private void assertUpdatedReleaseVersionMetadata( String artifactId, String version )
498 ManagedRepositoryContent testRepo = createTestRepoContent();
499 VersionedReference reference = new VersionedReference();
500 reference.setGroupId( "org.apache.archiva.metadata.tests" );
501 reference.setArtifactId( artifactId );
502 reference.setVersion( version );
504 prepTestRepo( testRepo, reference );
506 tools.updateMetadata( testRepo, reference );
508 StringBuilder buf = new StringBuilder();
509 buf.append( "<metadata>\n" );
510 buf.append( " <groupId>" ).append( reference.getGroupId() ).append( "</groupId>\n" );
511 buf.append( " <artifactId>" ).append( reference.getArtifactId() ).append( "</artifactId>\n" );
512 buf.append( " <version>" ).append( reference.getVersion() ).append( "</version>\n" );
513 buf.append( "</metadata>" );
515 assertMetadata( buf.toString(), testRepo, reference );
518 private void assertUpdatedSnapshotVersionMetadata( String artifactId, String version, String expectedDate,
519 String expectedTime, String expectedBuildNumber )
522 ManagedRepositoryContent testRepo = createTestRepoContent();
523 VersionedReference reference = new VersionedReference();
524 reference.setGroupId( "org.apache.archiva.metadata.tests" );
525 reference.setArtifactId( artifactId );
526 reference.setVersion( version );
528 prepTestRepo( testRepo, reference );
530 tools.updateMetadata( testRepo, reference );
532 StringBuilder buf = new StringBuilder();
533 buf.append( "<metadata>\n" );
534 buf.append( " <groupId>" ).append( reference.getGroupId() ).append( "</groupId>\n" );
535 buf.append( " <artifactId>" ).append( reference.getArtifactId() ).append( "</artifactId>\n" );
536 buf.append( " <version>" ).append( reference.getVersion() ).append( "</version>\n" );
537 buf.append( " <versioning>\n" );
538 buf.append( " <snapshot>\n" );
539 buf.append( " <buildNumber>" ).append( expectedBuildNumber ).append( "</buildNumber>\n" );
540 buf.append( " <timestamp>" );
541 buf.append( expectedDate ).append( "." ).append( expectedTime );
542 buf.append( "</timestamp>\n" );
543 buf.append( " </snapshot>\n" );
544 buf.append( " <lastUpdated>" ).append( expectedDate ).append( expectedTime ).append( "</lastUpdated>\n" );
545 buf.append( " </versioning>\n" );
546 buf.append( "</metadata>" );
548 assertMetadata( buf.toString(), testRepo, reference );
551 private void createProxyConnector( String sourceRepoId, String targetRepoId )
553 ProxyConnectorConfiguration connectorConfig = new ProxyConnectorConfiguration();
554 connectorConfig.setSourceRepoId( sourceRepoId );
555 connectorConfig.setTargetRepoId( targetRepoId );
556 connectorConfig.addPolicy( ProxyConnectorConfiguration.POLICY_CHECKSUM, ChecksumPolicy.IGNORE );
557 connectorConfig.addPolicy( ProxyConnectorConfiguration.POLICY_RELEASES, ReleasesPolicy.ALWAYS );
558 connectorConfig.addPolicy( ProxyConnectorConfiguration.POLICY_SNAPSHOTS, SnapshotsPolicy.ALWAYS );
559 connectorConfig.addPolicy( ProxyConnectorConfiguration.POLICY_CACHE_FAILURES, CachedFailuresPolicy.NO );
561 int count = config.getConfiguration().getProxyConnectors().size();
562 config.getConfiguration().addProxyConnector( connectorConfig );
564 // Proper Triggering ...
565 String prefix = "proxyConnectors.proxyConnector(" + count + ")";
566 config.triggerChange( prefix + ".sourceRepoId", connectorConfig.getSourceRepoId() );
567 config.triggerChange( prefix + ".targetRepoId", connectorConfig.getTargetRepoId() );
568 config.triggerChange( prefix + ".proxyId", connectorConfig.getProxyId() );
569 config.triggerChange( prefix + ".policies.releases", connectorConfig.getPolicy( "releases", "" ) );
570 config.triggerChange( prefix + ".policies.checksum", connectorConfig.getPolicy( "checksum", "" ) );
571 config.triggerChange( prefix + ".policies.snapshots", connectorConfig.getPolicy( "snapshots", "" ) );
572 config.triggerChange( prefix + ".policies.cache-failures", connectorConfig.getPolicy( "cache-failures", "" ) );
575 private ManagedRepositoryContent createTestRepoContent()
578 File repoRoot = new File( "target/metadata-tests/" + name.getMethodName() );
579 if ( repoRoot.exists() )
581 FileUtils.deleteDirectory( repoRoot );
586 ManagedRepositoryConfiguration repoConfig =
587 createRepository( "test-repo", "Test Repository: " + name.getMethodName(), repoRoot );
589 ManagedRepositoryContent repoContent =
590 applicationContext.getBean( "managedRepositoryContent#default", ManagedRepositoryContent.class );
591 repoContent.setRepository( repoConfig );
595 private void prepTestRepo( ManagedRepositoryContent repo, ProjectReference reference )
598 String groupDir = StringUtils.replaceChars( reference.getGroupId(), '.', '/' );
599 String path = groupDir + "/" + reference.getArtifactId();
601 File srcRepoDir = new File( "src/test/repositories/metadata-repository" );
602 File srcDir = new File( srcRepoDir, path );
603 File destDir = new File( repo.getRepoRoot(), path );
605 assertTrue( "Source Dir exists: " + srcDir, srcDir.exists() );
608 FileUtils.copyDirectory( srcDir, destDir );
611 private void prepTestRepo( ManagedRepositoryContent repo, VersionedReference reference )
614 ProjectReference projectRef = new ProjectReference();
615 projectRef.setGroupId( reference.getGroupId() );
616 projectRef.setArtifactId( reference.getArtifactId() );
618 prepTestRepo( repo, projectRef );