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.CachedFailuresPolicy;
28 import org.apache.maven.archiva.policies.ChecksumPolicy;
29 import org.apache.maven.archiva.policies.ReleasesPolicy;
30 import org.apache.maven.archiva.policies.SnapshotsPolicy;
31 import org.apache.maven.archiva.repository.AbstractRepositoryLayerTestCase;
32 import org.apache.maven.archiva.repository.ManagedRepositoryContent;
33 import org.apache.maven.archiva.repository.MockConfiguration;
34 import org.apache.maven.archiva.repository.RemoteRepositoryContent;
35 import org.apache.maven.archiva.repository.layout.LayoutException;
36 import org.custommonkey.xmlunit.DetailedDiff;
37 import org.custommonkey.xmlunit.Diff;
38 import org.xml.sax.SAXException;
41 import java.io.IOException;
42 import java.util.ArrayList;
43 import java.util.Collections;
44 import java.util.List;
47 import javax.xml.parsers.ParserConfigurationException;
52 * @author <a href="mailto:joakime@apache.org">Joakim Erdfelt</a>
55 public class MetadataToolsTest
56 extends AbstractRepositoryLayerTestCase
58 private MetadataTools tools;
60 protected MockConfiguration config;
62 public void testGatherSnapshotVersionsA()
65 assertSnapshotVersions( "snap_shots_a", "1.0-alpha-11-SNAPSHOT", new String[] {
66 "1.0-alpha-11-SNAPSHOT",
67 "1.0-alpha-11-20070221.194724-2",
68 "1.0-alpha-11-20070302.212723-3",
69 "1.0-alpha-11-20070303.152828-4",
70 "1.0-alpha-11-20070305.215149-5",
71 "1.0-alpha-11-20070307.170909-6",
72 "1.0-alpha-11-20070314.211405-9",
73 "1.0-alpha-11-20070316.175232-11" } );
76 public void testGatherSnapshotVersionsAWithProxies()
79 // These proxied repositories do not need to exist for the purposes of this unit test,
80 // just the repository ids are important.
81 createProxyConnector( "test-repo", "apache-snapshots" );
82 createProxyConnector( "test-repo", "internal-snapshots" );
83 createProxyConnector( "test-repo", "snapshots.codehaus.org" );
85 assertSnapshotVersions( "snap_shots_a", "1.0-alpha-11-SNAPSHOT", new String[] {
86 "1.0-alpha-11-SNAPSHOT",
87 "1.0-alpha-11-20070221.194724-2",
88 "1.0-alpha-11-20070302.212723-3",
89 "1.0-alpha-11-20070303.152828-4",
90 "1.0-alpha-11-20070305.215149-5",
91 "1.0-alpha-11-20070307.170909-6",
92 "1.0-alpha-11-20070314.211405-9",
93 "1.0-alpha-11-20070315.033030-10" /* Arrives in via snapshots.codehaus.org proxy */,
94 "1.0-alpha-11-20070316.175232-11" } );
97 public void testGetRepositorySpecificName()
100 RemoteRepositoryContent repoJavaNet = createRemoteRepositoryContent( "maven2-repository.dev.java.net",
101 "Java.net Repository for Maven 2",
102 "http://download.java.net/maven/2/",
104 RemoteRepositoryContent repoCentral = createRemoteRepositoryContent( "central", "Central Global Repository",
105 "http://repo1.maven.org/maven2/",
108 String convertedName = tools.getRepositorySpecificName( repoJavaNet,
109 "commons-lang/commons-lang/maven-metadata.xml" );
110 assertMetadataPath( "commons-lang/commons-lang/maven-metadata-maven2-repository.dev.java.net.xml",
113 convertedName = tools.getRepositorySpecificName( repoCentral, "commons-lang/commons-lang/maven-metadata.xml" );
114 assertMetadataPath( "commons-lang/commons-lang/maven-metadata-central.xml", convertedName );
117 public void testUpdateProjectBadArtifact()
122 assertUpdatedProjectMetadata( "bad_artifact", null );
123 fail( "Should have thrown an IOException on a bad artifact." );
125 catch ( IOException e )
131 public void testUpdateProjectMissingMultipleVersions()
134 assertUpdatedProjectMetadata( "missing_metadata_b", new String[] {
139 "2.0-20070821-dev" }, "2.0-20070821-dev" , "2.0-20070821-dev" );
142 public void testUpdateProjectMissingMultipleVersionsWithProxies()
145 // Attach the (bogus) proxies to the managed repo.
146 // These proxied repositories do not need to exist for the purposes of this unit test,
147 // just the repository ids are important.
148 createProxyConnector( "test-repo", "central" );
149 createProxyConnector( "test-repo", "java.net" );
151 assertUpdatedProjectMetadata( "proxied_multi", new String[] {
152 "1.0-spec" /* in java.net */,
153 "1.0" /* in managed, and central */,
154 "1.0.1" /* in central */,
155 "1.1" /* in managed */,
156 "2.0-proposal-beta" /* in java.net */,
157 "2.0-spec" /* in java.net */,
158 "2.0" /* in central, and java.net */,
159 "2.0.1" /* in java.net */,
160 "2.1" /* in managed */,
161 "3.0" /* in central */,
162 "3.1" /* in central */}, "3.1", "3.1" );
165 public void testUpdateProjectSimpleYetIncomplete()
168 assertUpdatedProjectMetadata( "incomplete_metadata_a", new String[] { "1.0" }, "1.0", "1.0" );
171 public void testUpdateProjectSimpleYetMissing()
174 assertUpdatedProjectMetadata( "missing_metadata_a", new String[] { "1.0" }, "1.0", "1.0" );
177 public void testUpdateVersionSimple10()
180 assertUpdatedReleaseVersionMetadata( "missing_metadata_a", "1.0" );
183 public void testUpdateVersionSimple20()
186 assertUpdatedReleaseVersionMetadata( "missing_metadata_b", "2.0" );
189 public void testUpdateVersionSimple20NotSnapshot()
192 assertUpdatedReleaseVersionMetadata( "missing_metadata_b", "2.0-20070821-dev" );
195 public void testUpdateVersionSnapshotA()
198 assertUpdatedSnapshotVersionMetadata( "snap_shots_a", "1.0-alpha-11-SNAPSHOT", "20070316", "175232", "11" );
201 public void testToPathFromVersionReference()
203 VersionedReference reference = new VersionedReference();
204 reference.setGroupId( "com.foo" );
205 reference.setArtifactId( "foo-tool" );
206 reference.setVersion( "1.0" );
208 assertEquals( "com/foo/foo-tool/1.0/maven-metadata.xml", tools.toPath( reference ) );
211 public void testToPathFromProjectReference()
213 ProjectReference reference = new ProjectReference();
214 reference.setGroupId( "com.foo" );
215 reference.setArtifactId( "foo-tool" );
217 assertEquals( "com/foo/foo-tool/maven-metadata.xml", tools.toPath( reference ) );
220 public void testToProjectReferenceFooTools()
221 throws RepositoryMetadataException
223 assertProjectReference( "com.foo", "foo-tools", "com/foo/foo-tools/maven-metadata.xml" );
226 public void testToProjectReferenceAReallyLongPath()
227 throws RepositoryMetadataException
229 String groupId = "net.i.have.a.really.long.path.just.for.the.hell.of.it";
230 String artifactId = "a";
231 String path = "net/i/have/a/really/long/path/just/for/the/hell/of/it/a/maven-metadata.xml";
233 assertProjectReference( groupId, artifactId, path );
236 public void testToProjectReferenceCommonsLang()
237 throws RepositoryMetadataException
239 String groupId = "commons-lang";
240 String artifactId = "commons-lang";
241 String path = "commons-lang/commons-lang/maven-metadata.xml";
243 assertProjectReference( groupId, artifactId, path );
246 private void assertProjectReference( String groupId, String artifactId, String path )
247 throws RepositoryMetadataException
249 ProjectReference reference = tools.toProjectReference( path );
251 assertNotNull( "Reference should not be null.", reference );
252 assertEquals( "ProjectReference.groupId", groupId, reference.getGroupId() );
253 assertEquals( "ProjectReference.artifactId", artifactId, reference.getArtifactId() );
256 public void testToVersionedReferenceFooTool()
257 throws RepositoryMetadataException
259 String groupId = "com.foo";
260 String artifactId = "foo-tool";
261 String version = "1.0";
262 String path = "com/foo/foo-tool/1.0/maven-metadata.xml";
264 assertVersionedReference( groupId, artifactId, version, path );
267 public void testToVersionedReferenceAReallyLongPath()
268 throws RepositoryMetadataException
270 String groupId = "net.i.have.a.really.long.path.just.for.the.hell.of.it";
271 String artifactId = "a";
272 String version = "1.1-alpha-1";
273 String path = "net/i/have/a/really/long/path/just/for/the/hell/of/it/a/1.1-alpha-1/maven-metadata.xml";
275 assertVersionedReference( groupId, artifactId, version, path );
278 public void testToVersionedReferenceCommonsLang()
279 throws RepositoryMetadataException
281 String groupId = "commons-lang";
282 String artifactId = "commons-lang";
283 String version = "2.1";
284 String path = "commons-lang/commons-lang/2.1/maven-metadata.xml";
286 assertVersionedReference( groupId, artifactId, version, path );
289 public void testToVersionedReferenceSnapshot()
290 throws RepositoryMetadataException
292 String groupId = "com.foo";
293 String artifactId = "foo-connector";
294 String version = "2.1-SNAPSHOT";
295 String path = "com/foo/foo-connector/2.1-SNAPSHOT/maven-metadata.xml";
297 assertVersionedReference( groupId, artifactId, version, path );
300 private void assertVersionedReference( String groupId, String artifactId, String version, String path )
301 throws RepositoryMetadataException
303 VersionedReference reference = tools.toVersionedReference( path );
304 assertNotNull( "Reference should not be null.", reference );
306 assertEquals( "VersionedReference.groupId", groupId, reference.getGroupId() );
307 assertEquals( "VersionedReference.artifactId", artifactId, reference.getArtifactId() );
308 assertEquals( "VersionedReference.version", version, reference.getVersion() );
311 private void assertSnapshotVersions( String artifactId, String version, String[] expectedVersions )
314 File repoRootDir = new File( "src/test/repositories/metadata-repository" );
316 VersionedReference reference = new VersionedReference();
317 reference.setGroupId( "org.apache.archiva.metadata.tests" );
318 reference.setArtifactId( artifactId );
319 reference.setVersion( version );
321 ManagedRepositoryConfiguration repo = createRepository( "test-repo", "Test Repository: " + getName(),
323 ManagedRepositoryContent repoContent = (ManagedRepositoryContent) lookup( ManagedRepositoryContent.class,
325 repoContent.setRepository( repo );
327 Set<String> testedVersionSet = tools.gatherSnapshotVersions( repoContent, reference );
329 // Sort the list (for asserts)
330 List<String> testedVersions = new ArrayList<String>();
331 testedVersions.addAll( testedVersionSet );
332 Collections.sort( testedVersions, new VersionComparator() );
334 // Test the expected array of versions, to the actual tested versions
335 assertEquals( "Assert Snapshot Versions: length/size", expectedVersions.length, testedVersions.size() );
337 for ( int i = 0; i < expectedVersions.length; i++ )
339 String actualVersion = testedVersions.get( i );
340 assertEquals( "Snapshot Versions[" + i + "]", expectedVersions[i], actualVersion );
344 private void assertMetadata( String expectedMetadata, ManagedRepositoryContent repository,
345 ProjectReference reference )
346 throws LayoutException, IOException, SAXException, ParserConfigurationException
348 File metadataFile = new File( repository.getRepoRoot(), tools.toPath( reference ) );
349 String actualMetadata = FileUtils.readFileToString( metadataFile, null );
351 DetailedDiff detailedDiff = new DetailedDiff( new Diff( expectedMetadata, actualMetadata ) );
352 if ( !detailedDiff.similar() )
354 // If it isn't similar, dump the difference.
355 assertEquals( expectedMetadata, actualMetadata );
359 private void assertMetadata( String expectedMetadata, ManagedRepositoryContent repository,
360 VersionedReference reference )
361 throws LayoutException, IOException, SAXException, ParserConfigurationException
363 File metadataFile = new File( repository.getRepoRoot(), tools.toPath( reference ) );
364 String actualMetadata = FileUtils.readFileToString( metadataFile, null );
366 DetailedDiff detailedDiff = new DetailedDiff( new Diff( expectedMetadata, actualMetadata ) );
367 if ( !detailedDiff.similar() )
369 // If it isn't similar, dump the difference.
370 assertEquals( expectedMetadata, actualMetadata );
374 private void assertMetadataPath( String expected, String actual )
376 assertEquals( "Repository Specific Metadata Path", expected, actual );
379 private void assertUpdatedProjectMetadata( String artifactId, String[] expectedVersions )
382 assertUpdatedProjectMetadata( artifactId, expectedVersions, null, null );
385 private void assertUpdatedProjectMetadata( String artifactId, String[] expectedVersions, String latestVersion,
386 String releaseVersion )
389 ManagedRepositoryContent testRepo = createTestRepoContent();
390 ProjectReference reference = new ProjectReference();
391 reference.setGroupId( "org.apache.archiva.metadata.tests" );
392 reference.setArtifactId( artifactId );
394 prepTestRepo( testRepo, reference );
396 tools.updateMetadata( testRepo, reference );
398 StringBuilder buf = new StringBuilder();
399 buf.append( "<metadata>\n" );
400 buf.append( " <groupId>" ).append( reference.getGroupId() ).append( "</groupId>\n" );
401 buf.append( " <artifactId>" ).append( reference.getArtifactId() ).append( "</artifactId>\n" );
402 // buf.append( " <version>1.0</version>\n" );
404 if ( expectedVersions != null )
406 buf.append( " <versioning>\n" );
407 if ( latestVersion != null )
409 buf.append( " <latest>" ).append( latestVersion ).append( "</latest>\n" );
411 if ( releaseVersion != null )
413 buf.append( " <release>" ).append( releaseVersion ).append( "</release>\n" );
416 buf.append( " <versions>\n" );
417 for ( int i = 0; i < expectedVersions.length; i++ )
419 buf.append( " <version>" ).append( expectedVersions[i] ).append( "</version>\n" );
421 buf.append( " </versions>\n" );
422 buf.append( " </versioning>\n" );
424 buf.append( "</metadata>" );
426 assertMetadata( buf.toString(), testRepo, reference );
429 private void assertUpdatedReleaseVersionMetadata( String artifactId, String version )
432 ManagedRepositoryContent testRepo = createTestRepoContent();
433 VersionedReference reference = new VersionedReference();
434 reference.setGroupId( "org.apache.archiva.metadata.tests" );
435 reference.setArtifactId( artifactId );
436 reference.setVersion( version );
438 prepTestRepo( testRepo, reference );
440 tools.updateMetadata( testRepo, reference );
442 StringBuilder buf = new StringBuilder();
443 buf.append( "<metadata>\n" );
444 buf.append( " <groupId>" ).append( reference.getGroupId() ).append( "</groupId>\n" );
445 buf.append( " <artifactId>" ).append( reference.getArtifactId() ).append( "</artifactId>\n" );
446 buf.append( " <version>" ).append( reference.getVersion() ).append( "</version>\n" );
447 buf.append( "</metadata>" );
449 assertMetadata( buf.toString(), testRepo, reference );
452 private void assertUpdatedSnapshotVersionMetadata( String artifactId, String version, String expectedDate,
453 String expectedTime, String expectedBuildNumber )
456 ManagedRepositoryContent testRepo = createTestRepoContent();
457 VersionedReference reference = new VersionedReference();
458 reference.setGroupId( "org.apache.archiva.metadata.tests" );
459 reference.setArtifactId( artifactId );
460 reference.setVersion( version );
462 prepTestRepo( testRepo, reference );
464 tools.updateMetadata( testRepo, reference );
466 StringBuilder buf = new StringBuilder();
467 buf.append( "<metadata>\n" );
468 buf.append( " <groupId>" ).append( reference.getGroupId() ).append( "</groupId>\n" );
469 buf.append( " <artifactId>" ).append( reference.getArtifactId() ).append( "</artifactId>\n" );
470 buf.append( " <version>" ).append( reference.getVersion() ).append( "</version>\n" );
471 buf.append( " <versioning>\n" );
472 buf.append( " <snapshot>\n" );
473 buf.append( " <buildNumber>" ).append( expectedBuildNumber ).append( "</buildNumber>\n" );
474 buf.append( " <timestamp>" );
475 buf.append( expectedDate ).append( "." ).append( expectedTime );
476 buf.append( "</timestamp>\n" );
477 buf.append( " </snapshot>\n" );
478 buf.append( " <lastUpdated>" ).append( expectedDate ).append( expectedTime ).append( "</lastUpdated>\n" );
479 buf.append( " </versioning>\n" );
480 buf.append( "</metadata>" );
482 assertMetadata( buf.toString(), testRepo, reference );
485 private void createProxyConnector( String sourceRepoId, String targetRepoId )
487 ProxyConnectorConfiguration connectorConfig = new ProxyConnectorConfiguration();
488 connectorConfig.setSourceRepoId( sourceRepoId );
489 connectorConfig.setTargetRepoId( targetRepoId );
490 connectorConfig.addPolicy( ProxyConnectorConfiguration.POLICY_CHECKSUM, ChecksumPolicy.IGNORE );
491 connectorConfig.addPolicy( ProxyConnectorConfiguration.POLICY_RELEASES, ReleasesPolicy.ALWAYS );
492 connectorConfig.addPolicy( ProxyConnectorConfiguration.POLICY_SNAPSHOTS, SnapshotsPolicy.ALWAYS );
493 connectorConfig.addPolicy( ProxyConnectorConfiguration.POLICY_CACHE_FAILURES, CachedFailuresPolicy.NO );
495 int count = config.getConfiguration().getProxyConnectors().size();
496 config.getConfiguration().addProxyConnector( connectorConfig );
498 // Proper Triggering ...
499 String prefix = "proxyConnectors.proxyConnector(" + count + ")";
500 config.triggerChange( prefix + ".sourceRepoId", connectorConfig.getSourceRepoId() );
501 config.triggerChange( prefix + ".targetRepoId", connectorConfig.getTargetRepoId() );
502 config.triggerChange( prefix + ".proxyId", connectorConfig.getProxyId() );
503 config.triggerChange( prefix + ".policies.releases", connectorConfig.getPolicy( "releases", "" ) );
504 config.triggerChange( prefix + ".policies.checksum", connectorConfig.getPolicy( "checksum", "" ) );
505 config.triggerChange( prefix + ".policies.snapshots", connectorConfig.getPolicy( "snapshots", "" ) );
506 config.triggerChange( prefix + ".policies.cache-failures", connectorConfig.getPolicy( "cache-failures", "" ) );
509 private ManagedRepositoryContent createTestRepoContent()
512 File repoRoot = new File( "target/metadata-tests/" + getName() );
513 if ( repoRoot.exists() )
515 FileUtils.deleteDirectory( repoRoot );
520 ManagedRepositoryConfiguration repoConfig = createRepository( "test-repo", "Test Repository: " + getName(),
523 ManagedRepositoryContent repoContent = (ManagedRepositoryContent) lookup( ManagedRepositoryContent.class,
525 repoContent.setRepository( repoConfig );
529 private void prepTestRepo( ManagedRepositoryContent repo, ProjectReference reference )
532 String groupDir = StringUtils.replaceChars( reference.getGroupId(), '.', '/' );
533 String path = groupDir + "/" + reference.getArtifactId();
535 File srcRepoDir = new File( "src/test/repositories/metadata-repository" );
536 File srcDir = new File( srcRepoDir, path );
537 File destDir = new File( repo.getRepoRoot(), path );
539 assertTrue( "Source Dir exists: " + srcDir, srcDir.exists() );
542 FileUtils.copyDirectory( srcDir, destDir );
545 private void prepTestRepo( ManagedRepositoryContent repo, VersionedReference reference )
548 ProjectReference projectRef = new ProjectReference();
549 projectRef.setGroupId( reference.getGroupId() );
550 projectRef.setArtifactId( reference.getArtifactId() );
552 prepTestRepo( repo, projectRef );
555 protected void setUp()
560 config = (MockConfiguration) lookup( ArchivaConfiguration.class.getName(), "mock" );
561 tools = (MetadataTools) lookup( MetadataTools.class );