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.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;
43 import javax.xml.parsers.ParserConfigurationException;
45 import java.io.IOException;
46 import java.util.ArrayList;
47 import java.util.Collections;
48 import java.util.List;
56 public class MetadataToolsTest
57 extends AbstractRepositoryLayerTestCase
59 private MetadataTools tools;
61 protected MockConfiguration config;
63 public void testGatherSnapshotVersionsA()
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" } );
77 public void testGatherSnapshotVersionsAWithProxies()
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" );
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" } );
98 public void testGetRepositorySpecificName()
101 RemoteRepositoryContent repoJavaNet = createRemoteRepositoryContent( "maven2-repository.dev.java.net",
102 "Java.net Repository for Maven 2",
103 "http://download.java.net/maven/2/",
105 RemoteRepositoryContent repoCentral = createRemoteRepositoryContent( "central", "Central Global Repository",
106 "http://repo1.maven.org/maven2/",
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",
114 convertedName = tools.getRepositorySpecificName( repoCentral, "commons-lang/commons-lang/maven-metadata.xml" );
115 assertMetadataPath( "commons-lang/commons-lang/maven-metadata-central.xml", convertedName );
118 // TODO: replace with group tests
119 // public void testUpdateProjectBadArtifact()
124 // assertUpdatedProjectMetadata( "bad_artifact", null );
125 // fail( "Should have thrown an IOException on a bad artifact." );
127 // catch ( IOException e )
133 public void testUpdateProjectMissingMultipleVersions()
136 assertUpdatedProjectMetadata( "missing_metadata_b", new String[] {
141 "2.0-20070821-dev" }, "2.0-20070821-dev" , "2.0-20070821-dev" );
144 public void testUpdateProjectMissingMultipleVersionsWithProxies()
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" );
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" );
167 public void testUpdateProjectSimpleYetIncomplete()
170 assertUpdatedProjectMetadata( "incomplete_metadata_a", new String[] { "1.0" }, "1.0", "1.0" );
173 public void testUpdateProjectSimpleYetMissing()
176 assertUpdatedProjectMetadata( "missing_metadata_a", new String[] { "1.0" }, "1.0", "1.0" );
179 public void testUpdateVersionSimple10()
182 assertUpdatedReleaseVersionMetadata( "missing_metadata_a", "1.0" );
185 public void testUpdateVersionSimple20()
188 assertUpdatedReleaseVersionMetadata( "missing_metadata_b", "2.0" );
191 public void testUpdateVersionSimple20NotSnapshot()
194 assertUpdatedReleaseVersionMetadata( "missing_metadata_b", "2.0-20070821-dev" );
197 public void testUpdateVersionSnapshotA()
200 assertUpdatedSnapshotVersionMetadata( "snap_shots_a", "1.0-alpha-11-SNAPSHOT", "20070316", "175232", "11" );
203 public void testToPathFromVersionReference()
205 VersionedReference reference = new VersionedReference();
206 reference.setGroupId( "com.foo" );
207 reference.setArtifactId( "foo-tool" );
208 reference.setVersion( "1.0" );
210 assertEquals( "com/foo/foo-tool/1.0/maven-metadata.xml", tools.toPath( reference ) );
213 public void testToPathFromProjectReference()
215 ProjectReference reference = new ProjectReference();
216 reference.setGroupId( "com.foo" );
217 reference.setArtifactId( "foo-tool" );
219 assertEquals( "com/foo/foo-tool/maven-metadata.xml", tools.toPath( reference ) );
222 public void testToProjectReferenceFooTools()
223 throws RepositoryMetadataException
225 assertProjectReference( "com.foo", "foo-tools", "com/foo/foo-tools/maven-metadata.xml" );
228 public void testToProjectReferenceAReallyLongPath()
229 throws RepositoryMetadataException
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";
235 assertProjectReference( groupId, artifactId, path );
238 public void testToProjectReferenceCommonsLang()
239 throws RepositoryMetadataException
241 String groupId = "commons-lang";
242 String artifactId = "commons-lang";
243 String path = "commons-lang/commons-lang/maven-metadata.xml";
245 assertProjectReference( groupId, artifactId, path );
248 private void assertProjectReference( String groupId, String artifactId, String path )
249 throws RepositoryMetadataException
251 ProjectReference reference = tools.toProjectReference( path );
253 assertNotNull( "Reference should not be null.", reference );
254 assertEquals( "ProjectReference.groupId", groupId, reference.getGroupId() );
255 assertEquals( "ProjectReference.artifactId", artifactId, reference.getArtifactId() );
258 public void testToVersionedReferenceFooTool()
259 throws RepositoryMetadataException
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";
266 assertVersionedReference( groupId, artifactId, version, path );
269 public void testToVersionedReferenceAReallyLongPath()
270 throws RepositoryMetadataException
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";
277 assertVersionedReference( groupId, artifactId, version, path );
280 public void testToVersionedReferenceCommonsLang()
281 throws RepositoryMetadataException
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";
288 assertVersionedReference( groupId, artifactId, version, path );
291 public void testToVersionedReferenceSnapshot()
292 throws RepositoryMetadataException
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";
299 assertVersionedReference( groupId, artifactId, version, path );
302 private void assertVersionedReference( String groupId, String artifactId, String version, String path )
303 throws RepositoryMetadataException
305 VersionedReference reference = tools.toVersionedReference( path );
306 assertNotNull( "Reference should not be null.", reference );
308 assertEquals( "VersionedReference.groupId", groupId, reference.getGroupId() );
309 assertEquals( "VersionedReference.artifactId", artifactId, reference.getArtifactId() );
310 assertEquals( "VersionedReference.version", version, reference.getVersion() );
313 private void assertSnapshotVersions( String artifactId, String version, String[] expectedVersions )
316 File repoRootDir = new File( "src/test/repositories/metadata-repository" );
318 VersionedReference reference = new VersionedReference();
319 reference.setGroupId( "org.apache.archiva.metadata.tests" );
320 reference.setArtifactId( artifactId );
321 reference.setVersion( version );
323 ManagedRepositoryConfiguration repo = createRepository( "test-repo", "Test Repository: " + getName(),
325 ManagedRepositoryContent repoContent = (ManagedRepositoryContent) lookup( ManagedRepositoryContent.class,
327 repoContent.setRepository( repo );
329 Set<String> testedVersionSet = tools.gatherSnapshotVersions( repoContent, reference );
331 // Sort the list (for asserts)
332 List<String> testedVersions = new ArrayList<String>();
333 testedVersions.addAll( testedVersionSet );
334 Collections.sort( testedVersions, new VersionComparator() );
336 // Test the expected array of versions, to the actual tested versions
337 assertEquals( "Assert Snapshot Versions: length/size", expectedVersions.length, testedVersions.size() );
339 for ( int i = 0; i < expectedVersions.length; i++ )
341 String actualVersion = testedVersions.get( i );
342 assertEquals( "Snapshot Versions[" + i + "]", expectedVersions[i], actualVersion );
346 private void assertMetadata( String expectedMetadata, ManagedRepositoryContent repository,
347 ProjectReference reference )
348 throws LayoutException, IOException, SAXException, ParserConfigurationException
350 File metadataFile = new File( repository.getRepoRoot(), tools.toPath( reference ) );
351 String actualMetadata = FileUtils.readFileToString( metadataFile, null );
353 DetailedDiff detailedDiff = new DetailedDiff( new Diff( expectedMetadata, actualMetadata ) );
354 if ( !detailedDiff.similar() )
356 // If it isn't similar, dump the difference.
357 assertEquals( expectedMetadata, actualMetadata );
361 private void assertMetadata( String expectedMetadata, ManagedRepositoryContent repository,
362 VersionedReference reference )
363 throws LayoutException, IOException, SAXException, ParserConfigurationException
365 File metadataFile = new File( repository.getRepoRoot(), tools.toPath( reference ) );
366 String actualMetadata = FileUtils.readFileToString( metadataFile, null );
368 DetailedDiff detailedDiff = new DetailedDiff( new Diff( expectedMetadata, actualMetadata ) );
369 if ( !detailedDiff.similar() )
371 // If it isn't similar, dump the difference.
372 assertEquals( expectedMetadata, actualMetadata );
376 private void assertMetadataPath( String expected, String actual )
378 assertEquals( "Repository Specific Metadata Path", expected, actual );
381 private void assertUpdatedProjectMetadata( String artifactId, String[] expectedVersions, String latestVersion,
382 String releaseVersion )
385 ManagedRepositoryContent testRepo = createTestRepoContent();
386 ProjectReference reference = new ProjectReference();
387 reference.setGroupId( "org.apache.archiva.metadata.tests" );
388 reference.setArtifactId( artifactId );
390 prepTestRepo( testRepo, reference );
392 tools.updateMetadata( testRepo, reference );
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" );
400 if ( expectedVersions != null )
402 buf.append( " <versioning>\n" );
403 if ( latestVersion != null )
405 buf.append( " <latest>" ).append( latestVersion ).append( "</latest>\n" );
407 if ( releaseVersion != null )
409 buf.append( " <release>" ).append( releaseVersion ).append( "</release>\n" );
412 buf.append( " <versions>\n" );
413 for ( int i = 0; i < expectedVersions.length; i++ )
415 buf.append( " <version>" ).append( expectedVersions[i] ).append( "</version>\n" );
417 buf.append( " </versions>\n" );
418 buf.append( " </versioning>\n" );
420 buf.append( "</metadata>" );
422 assertMetadata( buf.toString(), testRepo, reference );
425 private void assertUpdatedReleaseVersionMetadata( String artifactId, String version )
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 );
434 prepTestRepo( testRepo, reference );
436 tools.updateMetadata( testRepo, reference );
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>" );
445 assertMetadata( buf.toString(), testRepo, reference );
448 private void assertUpdatedSnapshotVersionMetadata( String artifactId, String version, String expectedDate,
449 String expectedTime, String expectedBuildNumber )
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 );
458 prepTestRepo( testRepo, reference );
460 tools.updateMetadata( testRepo, reference );
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>" );
478 assertMetadata( buf.toString(), testRepo, reference );
481 private void createProxyConnector( String sourceRepoId, String targetRepoId )
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 );
491 int count = config.getConfiguration().getProxyConnectors().size();
492 config.getConfiguration().addProxyConnector( connectorConfig );
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", "" ) );
505 private ManagedRepositoryContent createTestRepoContent()
508 File repoRoot = new File( "target/metadata-tests/" + getName() );
509 if ( repoRoot.exists() )
511 FileUtils.deleteDirectory( repoRoot );
516 ManagedRepositoryConfiguration repoConfig = createRepository( "test-repo", "Test Repository: " + getName(),
519 ManagedRepositoryContent repoContent = (ManagedRepositoryContent) lookup( ManagedRepositoryContent.class,
521 repoContent.setRepository( repoConfig );
525 private void prepTestRepo( ManagedRepositoryContent repo, ProjectReference reference )
528 String groupDir = StringUtils.replaceChars( reference.getGroupId(), '.', '/' );
529 String path = groupDir + "/" + reference.getArtifactId();
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 );
535 assertTrue( "Source Dir exists: " + srcDir, srcDir.exists() );
538 FileUtils.copyDirectory( srcDir, destDir );
541 private void prepTestRepo( ManagedRepositoryContent repo, VersionedReference reference )
544 ProjectReference projectRef = new ProjectReference();
545 projectRef.setGroupId( reference.getGroupId() );
546 projectRef.setArtifactId( reference.getArtifactId() );
548 prepTestRepo( repo, projectRef );
551 protected void setUp()
556 config = (MockConfiguration) lookup( ArchivaConfiguration.class.getName(), "mock" );
557 tools = (MetadataTools) lookup( MetadataTools.class );