1 package org.apache.maven.archiva.repository.metadata;
4 * Copyright 2001-2007 The Apache Software Foundation.
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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
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;
38 import java.io.IOException;
39 import java.util.ArrayList;
40 import java.util.Collections;
41 import java.util.List;
44 import javax.xml.parsers.ParserConfigurationException;
49 * @author <a href="mailto:joakime@apache.org">Joakim Erdfelt</a>
52 public class MetadataToolsTest
53 extends AbstractRepositoryLayerTestCase
55 private MetadataTools tools;
57 protected MockConfiguration config;
59 public void testGatherSnapshotVersionsA()
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" } );
73 public void testGatherSnapshotVersionsAWithProxies()
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" );
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" } );
94 public void testGetRepositorySpecificName()
97 RemoteRepositoryContent repoJavaNet = createRemoteRepositoryContent( "maven2-repository.dev.java.net",
98 "Java.net Repository for Maven 2",
99 "http://download.java.net/maven/2/",
101 RemoteRepositoryContent repoCentral = createRemoteRepositoryContent( "central", "Central Global Repository",
102 "http://repo1.maven.org/maven2/",
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",
110 convertedName = tools.getRepositorySpecificName( repoCentral, "commons-lang/commons-lang/maven-metadata.xml" );
111 assertMetadataPath( "commons-lang/commons-lang/maven-metadata-central.xml", convertedName );
114 public void testUpdateProjectBadArtifact()
119 assertUpdatedProjectMetadata( "bad_artifact", null );
120 fail( "Should have thrown an IOException on a bad artifact." );
122 catch ( IOException e )
128 public void testUpdateProjectMissingMultipleVersions()
131 assertUpdatedProjectMetadata( "missing_metadata_b", new String[] {
136 "2.0-20070821-dev" }, "2.0-20070821-dev" , "2.0-20070821-dev" );
139 public void testUpdateProjectMissingMultipleVersionsWithProxies()
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" );
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" );
162 public void testUpdateProjectSimpleYetIncomplete()
165 assertUpdatedProjectMetadata( "incomplete_metadata_a", new String[] { "1.0" }, "1.0", "1.0" );
168 public void testUpdateProjectSimpleYetMissing()
171 assertUpdatedProjectMetadata( "missing_metadata_a", new String[] { "1.0" }, "1.0", "1.0" );
174 public void testUpdateVersionSimple10()
177 assertUpdatedReleaseVersionMetadata( "missing_metadata_a", "1.0" );
180 public void testUpdateVersionSimple20()
183 assertUpdatedReleaseVersionMetadata( "missing_metadata_b", "2.0" );
186 public void testUpdateVersionSimple20NotSnapshot()
189 assertUpdatedReleaseVersionMetadata( "missing_metadata_b", "2.0-20070821-dev" );
192 public void testUpdateVersionSnapshotA()
195 assertUpdatedSnapshotVersionMetadata( "snap_shots_a", "1.0-alpha-11-SNAPSHOT", "20070316", "175232", "11" );
198 public void testToPathFromVersionReference()
200 VersionedReference reference = new VersionedReference();
201 reference.setGroupId( "com.foo" );
202 reference.setArtifactId( "foo-tool" );
203 reference.setVersion( "1.0" );
205 assertEquals( "com/foo/foo-tool/1.0/maven-metadata.xml", tools.toPath( reference ) );
208 public void testToPathFromProjectReference()
210 ProjectReference reference = new ProjectReference();
211 reference.setGroupId( "com.foo" );
212 reference.setArtifactId( "foo-tool" );
214 assertEquals( "com/foo/foo-tool/maven-metadata.xml", tools.toPath( reference ) );
217 public void testToProjectReferenceFooTools()
218 throws RepositoryMetadataException
220 assertProjectReference( "com.foo", "foo-tools", "com/foo/foo-tools/maven-metadata.xml" );
223 public void testToProjectReferenceAReallyLongPath()
224 throws RepositoryMetadataException
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";
230 assertProjectReference( groupId, artifactId, path );
233 public void testToProjectReferenceCommonsLang()
234 throws RepositoryMetadataException
236 String groupId = "commons-lang";
237 String artifactId = "commons-lang";
238 String path = "commons-lang/commons-lang/maven-metadata.xml";
240 assertProjectReference( groupId, artifactId, path );
243 private void assertProjectReference( String groupId, String artifactId, String path )
244 throws RepositoryMetadataException
246 ProjectReference reference = tools.toProjectReference( path );
248 assertNotNull( "Reference should not be null.", reference );
249 assertEquals( "ProjectReference.groupId", groupId, reference.getGroupId() );
250 assertEquals( "ProjectReference.artifactId", artifactId, reference.getArtifactId() );
253 public void testToVersionedReferenceFooTool()
254 throws RepositoryMetadataException
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";
261 assertVersionedReference( groupId, artifactId, version, path );
264 public void testToVersionedReferenceAReallyLongPath()
265 throws RepositoryMetadataException
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";
272 assertVersionedReference( groupId, artifactId, version, path );
275 public void testToVersionedReferenceCommonsLang()
276 throws RepositoryMetadataException
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";
283 assertVersionedReference( groupId, artifactId, version, path );
286 public void testToVersionedReferenceSnapshot()
287 throws RepositoryMetadataException
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";
294 assertVersionedReference( groupId, artifactId, version, path );
297 private void assertVersionedReference( String groupId, String artifactId, String version, String path )
298 throws RepositoryMetadataException
300 VersionedReference reference = tools.toVersionedReference( path );
301 assertNotNull( "Reference should not be null.", reference );
303 assertEquals( "VersionedReference.groupId", groupId, reference.getGroupId() );
304 assertEquals( "VersionedReference.artifactId", artifactId, reference.getArtifactId() );
305 assertEquals( "VersionedReference.version", version, reference.getVersion() );
308 private void assertSnapshotVersions( String artifactId, String version, String[] expectedVersions )
311 File repoRootDir = new File( "src/test/repositories/metadata-repository" );
313 VersionedReference reference = new VersionedReference();
314 reference.setGroupId( "org.apache.archiva.metadata.tests" );
315 reference.setArtifactId( artifactId );
316 reference.setVersion( version );
318 ManagedRepositoryConfiguration repo = createRepository( "test-repo", "Test Repository: " + getName(),
320 ManagedRepositoryContent repoContent = (ManagedRepositoryContent) lookup( ManagedRepositoryContent.class,
322 repoContent.setRepository( repo );
324 Set<String> testedVersionSet = tools.gatherSnapshotVersions( repoContent, reference );
326 // Sort the list (for asserts)
327 List<String> testedVersions = new ArrayList<String>();
328 testedVersions.addAll( testedVersionSet );
329 Collections.sort( testedVersions, new VersionComparator() );
331 // Test the expected array of versions, to the actual tested versions
332 assertEquals( "Assert Snapshot Versions: length/size", expectedVersions.length, testedVersions.size() );
334 for ( int i = 0; i < expectedVersions.length; i++ )
336 String actualVersion = testedVersions.get( i );
337 assertEquals( "Snapshot Versions[" + i + "]", expectedVersions[i], actualVersion );
341 private void assertMetadata( String expectedMetadata, ManagedRepositoryContent repository,
342 ProjectReference reference )
343 throws LayoutException, IOException, SAXException, ParserConfigurationException
345 File metadataFile = new File( repository.getRepoRoot(), tools.toPath( reference ) );
346 String actualMetadata = FileUtils.readFileToString( metadataFile, null );
348 DetailedDiff detailedDiff = new DetailedDiff( new Diff( expectedMetadata, actualMetadata ) );
349 if ( !detailedDiff.similar() )
351 // If it isn't similar, dump the difference.
352 assertEquals( expectedMetadata, actualMetadata );
356 private void assertMetadata( String expectedMetadata, ManagedRepositoryContent repository,
357 VersionedReference reference )
358 throws LayoutException, IOException, SAXException, ParserConfigurationException
360 File metadataFile = new File( repository.getRepoRoot(), tools.toPath( reference ) );
361 String actualMetadata = FileUtils.readFileToString( metadataFile, null );
363 DetailedDiff detailedDiff = new DetailedDiff( new Diff( expectedMetadata, actualMetadata ) );
364 if ( !detailedDiff.similar() )
366 // If it isn't similar, dump the difference.
367 assertEquals( expectedMetadata, actualMetadata );
371 private void assertMetadataPath( String expected, String actual )
373 assertEquals( "Repository Specific Metadata Path", expected, actual );
376 private void assertUpdatedProjectMetadata( String artifactId, String[] expectedVersions )
379 assertUpdatedProjectMetadata( artifactId, expectedVersions, null, null );
382 private void assertUpdatedProjectMetadata( String artifactId, String[] expectedVersions, String latestVersion,
383 String releaseVersion )
386 ManagedRepositoryContent testRepo = createTestRepoContent();
387 ProjectReference reference = new ProjectReference();
388 reference.setGroupId( "org.apache.archiva.metadata.tests" );
389 reference.setArtifactId( artifactId );
391 prepTestRepo( testRepo, reference );
393 tools.updateMetadata( testRepo, reference );
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" );
401 if ( expectedVersions != null )
403 buf.append( " <versioning>\n" );
404 if ( latestVersion != null )
406 buf.append( " <latest>" ).append( latestVersion ).append( "</latest>\n" );
408 if ( releaseVersion != null )
410 buf.append( " <release>" ).append( releaseVersion ).append( "</release>\n" );
413 buf.append( " <versions>\n" );
414 for ( int i = 0; i < expectedVersions.length; i++ )
416 buf.append( " <version>" ).append( expectedVersions[i] ).append( "</version>\n" );
418 buf.append( " </versions>\n" );
419 buf.append( " </versioning>\n" );
421 buf.append( "</metadata>" );
423 assertMetadata( buf.toString(), testRepo, reference );
426 private void assertUpdatedReleaseVersionMetadata( String artifactId, String version )
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 );
435 prepTestRepo( testRepo, reference );
437 tools.updateMetadata( testRepo, reference );
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>" );
446 assertMetadata( buf.toString(), testRepo, reference );
449 private void assertUpdatedSnapshotVersionMetadata( String artifactId, String version, String expectedDate,
450 String expectedTime, String expectedBuildNumber )
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 );
459 prepTestRepo( testRepo, reference );
461 tools.updateMetadata( testRepo, reference );
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>" );
479 assertMetadata( buf.toString(), testRepo, reference );
482 private void createProxyConnector( String sourceRepoId, String targetRepoId )
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 );
492 int count = config.getConfiguration().getProxyConnectors().size();
493 config.getConfiguration().addProxyConnector( connectorConfig );
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", "" ) );
506 private ManagedRepositoryContent createTestRepoContent()
509 File repoRoot = new File( "target/metadata-tests/" + getName() );
510 if ( repoRoot.exists() )
512 FileUtils.deleteDirectory( repoRoot );
517 ManagedRepositoryConfiguration repoConfig = createRepository( "test-repo", "Test Repository: " + getName(),
520 ManagedRepositoryContent repoContent = (ManagedRepositoryContent) lookup( ManagedRepositoryContent.class,
522 repoContent.setRepository( repoConfig );
526 private void prepTestRepo( ManagedRepositoryContent repo, ProjectReference reference )
529 String groupDir = StringUtils.replaceChars( reference.getGroupId(), '.', '/' );
530 String path = groupDir + "/" + reference.getArtifactId();
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 );
536 assertTrue( "Source Dir exists: " + srcDir, srcDir.exists() );
539 FileUtils.copyDirectory( srcDir, destDir );
542 private void prepTestRepo( ManagedRepositoryContent repo, VersionedReference reference )
545 ProjectReference projectRef = new ProjectReference();
546 projectRef.setGroupId( reference.getGroupId() );
547 projectRef.setArtifactId( reference.getArtifactId() );
549 prepTestRepo( repo, projectRef );
552 protected void setUp()
557 config = (MockConfiguration) lookup( ArchivaConfiguration.class.getName(), "mock" );
558 tools = (MetadataTools) lookup( MetadataTools.class );