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 )
384 assertUpdatedProjectMetadata( artifactId, expectedVersions, null, null );
387 private void assertUpdatedProjectMetadata( String artifactId, String[] expectedVersions, String latestVersion,
388 String releaseVersion )
391 ManagedRepositoryContent testRepo = createTestRepoContent();
392 ProjectReference reference = new ProjectReference();
393 reference.setGroupId( "org.apache.archiva.metadata.tests" );
394 reference.setArtifactId( artifactId );
396 prepTestRepo( testRepo, reference );
398 tools.updateMetadata( testRepo, reference );
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" );
406 if ( expectedVersions != null )
408 buf.append( " <versioning>\n" );
409 if ( latestVersion != null )
411 buf.append( " <latest>" ).append( latestVersion ).append( "</latest>\n" );
413 if ( releaseVersion != null )
415 buf.append( " <release>" ).append( releaseVersion ).append( "</release>\n" );
418 buf.append( " <versions>\n" );
419 for ( int i = 0; i < expectedVersions.length; i++ )
421 buf.append( " <version>" ).append( expectedVersions[i] ).append( "</version>\n" );
423 buf.append( " </versions>\n" );
424 buf.append( " </versioning>\n" );
426 buf.append( "</metadata>" );
428 assertMetadata( buf.toString(), testRepo, reference );
431 private void assertUpdatedReleaseVersionMetadata( String artifactId, String version )
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 );
440 prepTestRepo( testRepo, reference );
442 tools.updateMetadata( testRepo, reference );
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>" );
451 assertMetadata( buf.toString(), testRepo, reference );
454 private void assertUpdatedSnapshotVersionMetadata( String artifactId, String version, String expectedDate,
455 String expectedTime, String expectedBuildNumber )
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 );
464 prepTestRepo( testRepo, reference );
466 tools.updateMetadata( testRepo, reference );
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>" );
484 assertMetadata( buf.toString(), testRepo, reference );
487 private void createProxyConnector( String sourceRepoId, String targetRepoId )
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 );
497 int count = config.getConfiguration().getProxyConnectors().size();
498 config.getConfiguration().addProxyConnector( connectorConfig );
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", "" ) );
511 private ManagedRepositoryContent createTestRepoContent()
514 File repoRoot = new File( "target/metadata-tests/" + getName() );
515 if ( repoRoot.exists() )
517 FileUtils.deleteDirectory( repoRoot );
522 ManagedRepositoryConfiguration repoConfig = createRepository( "test-repo", "Test Repository: " + getName(),
525 ManagedRepositoryContent repoContent = (ManagedRepositoryContent) lookup( ManagedRepositoryContent.class,
527 repoContent.setRepository( repoConfig );
531 private void prepTestRepo( ManagedRepositoryContent repo, ProjectReference reference )
534 String groupDir = StringUtils.replaceChars( reference.getGroupId(), '.', '/' );
535 String path = groupDir + "/" + reference.getArtifactId();
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 );
541 assertTrue( "Source Dir exists: " + srcDir, srcDir.exists() );
544 FileUtils.copyDirectory( srcDir, destDir );
547 private void prepTestRepo( ManagedRepositoryContent repo, VersionedReference reference )
550 ProjectReference projectRef = new ProjectReference();
551 projectRef.setGroupId( reference.getGroupId() );
552 projectRef.setArtifactId( reference.getArtifactId() );
554 prepTestRepo( repo, projectRef );
557 protected void setUp()
562 config = (MockConfiguration) lookup( ArchivaConfiguration.class.getName(), "mock" );
563 tools = (MetadataTools) lookup( MetadataTools.class );