1 package org.apache.maven.archiva.repository.metadata;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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;
42
43 import javax.xml.parsers.ParserConfigurationException;
44 import java.io.File;
45 import java.io.IOException;
46 import java.util.ArrayList;
47 import java.util.Collections;
48 import java.util.List;
49 import java.util.Set;
50
51
52
53
54
55
56 public class MetadataToolsTest
57 extends AbstractRepositoryLayerTestCase
58 {
59 private MetadataTools tools;
60
61 protected MockConfiguration config;
62
63 public void testGatherSnapshotVersionsA()
64 throws Exception
65 {
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" } );
75 }
76
77 public void testGatherSnapshotVersionsAWithProxies()
78 throws Exception
79 {
80
81
82 createProxyConnector( "test-repo", "apache-snapshots" );
83 createProxyConnector( "test-repo", "internal-snapshots" );
84 createProxyConnector( "test-repo", "snapshots.codehaus.org" );
85
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"
95 "1.0-alpha-11-20070316.175232-11" } );
96 }
97
98 public void testGetRepositorySpecificName()
99 throws Exception
100 {
101 RemoteRepositoryContent repoJavaNet = createRemoteRepositoryContent( "maven2-repository.dev.java.net",
102 "Java.net Repository for Maven 2",
103 "http://download.java.net/maven/2/",
104 "default" );
105 RemoteRepositoryContent repoCentral = createRemoteRepositoryContent( "central", "Central Global Repository",
106 "http://repo1.maven.org/maven2/",
107 "default" );
108
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",
112 convertedName );
113
114 convertedName = tools.getRepositorySpecificName( repoCentral, "commons-lang/commons-lang/maven-metadata.xml" );
115 assertMetadataPath( "commons-lang/commons-lang/maven-metadata-central.xml", convertedName );
116 }
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133 public void testUpdateProjectNonExistingVersion()
134 throws Exception
135 {
136 ManagedRepositoryContent testRepo = createTestRepoContent();
137 ProjectReference reference = new ProjectReference();
138 reference.setGroupId( "org.apache.archiva.metadata.tests" );
139 reference.setArtifactId( "missing_artifact" );
140
141 prepTestRepo( testRepo, reference );
142
143
144 assertProjectMetadata( testRepo, reference, "missing_artifact", new String[] {
145 "1.0-SNAPSHOT",
146 "1.1-SNAPSHOT",
147 "1.2-SNAPSHOT" }, "1.2-SNAPSHOT" , null );
148
149 tools.updateMetadata( testRepo, reference );
150
151
152 assertProjectMetadata( testRepo, reference, "missing_artifact", new String[] {
153 "1.0-SNAPSHOT",
154 "1.2-SNAPSHOT" }, "1.2-SNAPSHOT" , null );
155 }
156
157 public void testUpdateProjectMissingMultipleVersions()
158 throws Exception
159 {
160 assertUpdatedProjectMetadata( "missing_metadata_b", new String[] {
161 "1.0",
162 "1.0.1",
163 "2.0",
164 "2.0.1",
165 "2.0-20070821-dev" }, "2.0-20070821-dev" , "2.0-20070821-dev" );
166 }
167
168 public void testUpdateProjectMissingMultipleVersionsWithProxies()
169 throws Exception
170 {
171
172
173
174 createProxyConnector( "test-repo", "central" );
175 createProxyConnector( "test-repo", "java.net" );
176
177 assertUpdatedProjectMetadata( "proxied_multi", new String[] {
178 "1.0-spec"
179 "1.0"
180 "1.0.1"
181 "1.1"
182 "2.0-proposal-beta"
183 "2.0-spec"
184 "2.0"
185 "2.0.1"
186 "2.1"
187 "3.0"
188 "3.1"
189 }
190
191 public void testUpdateProjectSimpleYetIncomplete()
192 throws Exception
193 {
194 assertUpdatedProjectMetadata( "incomplete_metadata_a", new String[] { "1.0" }, "1.0", "1.0" );
195 }
196
197 public void testUpdateProjectSimpleYetMissing()
198 throws Exception
199 {
200 assertUpdatedProjectMetadata( "missing_metadata_a", new String[] { "1.0" }, "1.0", "1.0" );
201 }
202
203 public void testUpdateVersionSimple10()
204 throws Exception
205 {
206 assertUpdatedReleaseVersionMetadata( "missing_metadata_a", "1.0" );
207 }
208
209 public void testUpdateVersionSimple20()
210 throws Exception
211 {
212 assertUpdatedReleaseVersionMetadata( "missing_metadata_b", "2.0" );
213 }
214
215 public void testUpdateVersionSimple20NotSnapshot()
216 throws Exception
217 {
218 assertUpdatedReleaseVersionMetadata( "missing_metadata_b", "2.0-20070821-dev" );
219 }
220
221 public void testUpdateVersionSnapshotA()
222 throws Exception
223 {
224 assertUpdatedSnapshotVersionMetadata( "snap_shots_a", "1.0-alpha-11-SNAPSHOT", "20070316", "175232", "11" );
225 }
226
227 public void testToPathFromVersionReference()
228 {
229 VersionedReference reference = new VersionedReference();
230 reference.setGroupId( "com.foo" );
231 reference.setArtifactId( "foo-tool" );
232 reference.setVersion( "1.0" );
233
234 assertEquals( "com/foo/foo-tool/1.0/maven-metadata.xml", tools.toPath( reference ) );
235 }
236
237 public void testToPathFromProjectReference()
238 {
239 ProjectReference reference = new ProjectReference();
240 reference.setGroupId( "com.foo" );
241 reference.setArtifactId( "foo-tool" );
242
243 assertEquals( "com/foo/foo-tool/maven-metadata.xml", tools.toPath( reference ) );
244 }
245
246 public void testToProjectReferenceFooTools()
247 throws RepositoryMetadataException
248 {
249 assertProjectReference( "com.foo", "foo-tools", "com/foo/foo-tools/maven-metadata.xml" );
250 }
251
252 public void testToProjectReferenceAReallyLongPath()
253 throws RepositoryMetadataException
254 {
255 String groupId = "net.i.have.a.really.long.path.just.for.the.hell.of.it";
256 String artifactId = "a";
257 String path = "net/i/have/a/really/long/path/just/for/the/hell/of/it/a/maven-metadata.xml";
258
259 assertProjectReference( groupId, artifactId, path );
260 }
261
262 public void testToProjectReferenceCommonsLang()
263 throws RepositoryMetadataException
264 {
265 String groupId = "commons-lang";
266 String artifactId = "commons-lang";
267 String path = "commons-lang/commons-lang/maven-metadata.xml";
268
269 assertProjectReference( groupId, artifactId, path );
270 }
271
272 private void assertProjectReference( String groupId, String artifactId, String path )
273 throws RepositoryMetadataException
274 {
275 ProjectReference reference = tools.toProjectReference( path );
276
277 assertNotNull( "Reference should not be null.", reference );
278 assertEquals( "ProjectReference.groupId", groupId, reference.getGroupId() );
279 assertEquals( "ProjectReference.artifactId", artifactId, reference.getArtifactId() );
280 }
281
282 public void testToVersionedReferenceFooTool()
283 throws RepositoryMetadataException
284 {
285 String groupId = "com.foo";
286 String artifactId = "foo-tool";
287 String version = "1.0";
288 String path = "com/foo/foo-tool/1.0/maven-metadata.xml";
289
290 assertVersionedReference( groupId, artifactId, version, path );
291 }
292
293 public void testToVersionedReferenceAReallyLongPath()
294 throws RepositoryMetadataException
295 {
296 String groupId = "net.i.have.a.really.long.path.just.for.the.hell.of.it";
297 String artifactId = "a";
298 String version = "1.1-alpha-1";
299 String path = "net/i/have/a/really/long/path/just/for/the/hell/of/it/a/1.1-alpha-1/maven-metadata.xml";
300
301 assertVersionedReference( groupId, artifactId, version, path );
302 }
303
304 public void testToVersionedReferenceCommonsLang()
305 throws RepositoryMetadataException
306 {
307 String groupId = "commons-lang";
308 String artifactId = "commons-lang";
309 String version = "2.1";
310 String path = "commons-lang/commons-lang/2.1/maven-metadata.xml";
311
312 assertVersionedReference( groupId, artifactId, version, path );
313 }
314
315 public void testToVersionedReferenceSnapshot()
316 throws RepositoryMetadataException
317 {
318 String groupId = "com.foo";
319 String artifactId = "foo-connector";
320 String version = "2.1-SNAPSHOT";
321 String path = "com/foo/foo-connector/2.1-SNAPSHOT/maven-metadata.xml";
322
323 assertVersionedReference( groupId, artifactId, version, path );
324 }
325
326 private void assertVersionedReference( String groupId, String artifactId, String version, String path )
327 throws RepositoryMetadataException
328 {
329 VersionedReference reference = tools.toVersionedReference( path );
330 assertNotNull( "Reference should not be null.", reference );
331
332 assertEquals( "VersionedReference.groupId", groupId, reference.getGroupId() );
333 assertEquals( "VersionedReference.artifactId", artifactId, reference.getArtifactId() );
334 assertEquals( "VersionedReference.version", version, reference.getVersion() );
335 }
336
337 private void assertSnapshotVersions( String artifactId, String version, String[] expectedVersions )
338 throws Exception
339 {
340 File repoRootDir = new File( "src/test/repositories/metadata-repository" );
341
342 VersionedReference reference = new VersionedReference();
343 reference.setGroupId( "org.apache.archiva.metadata.tests" );
344 reference.setArtifactId( artifactId );
345 reference.setVersion( version );
346
347 ManagedRepositoryConfiguration repo = createRepository( "test-repo", "Test Repository: " + getName(),
348 repoRootDir );
349 ManagedRepositoryContent repoContent = (ManagedRepositoryContent) lookup( ManagedRepositoryContent.class,
350 "default" );
351 repoContent.setRepository( repo );
352
353 Set<String> testedVersionSet = tools.gatherSnapshotVersions( repoContent, reference );
354
355
356 List<String> testedVersions = new ArrayList<String>();
357 testedVersions.addAll( testedVersionSet );
358 Collections.sort( testedVersions, new VersionComparator() );
359
360
361 assertEquals( "Assert Snapshot Versions: length/size", expectedVersions.length, testedVersions.size() );
362
363 for ( int i = 0; i < expectedVersions.length; i++ )
364 {
365 String actualVersion = testedVersions.get( i );
366 assertEquals( "Snapshot Versions[" + i + "]", expectedVersions[i], actualVersion );
367 }
368 }
369
370 private void assertMetadata( String expectedMetadata, ManagedRepositoryContent repository,
371 ProjectReference reference )
372 throws LayoutException, IOException, SAXException, ParserConfigurationException
373 {
374 File metadataFile = new File( repository.getRepoRoot(), tools.toPath( reference ) );
375 String actualMetadata = FileUtils.readFileToString( metadataFile, null );
376
377 DetailedDiff detailedDiff = new DetailedDiff( new Diff( expectedMetadata, actualMetadata ) );
378 if ( !detailedDiff.similar() )
379 {
380
381 assertEquals( expectedMetadata, actualMetadata );
382 }
383 }
384
385 private void assertMetadata( String expectedMetadata, ManagedRepositoryContent repository,
386 VersionedReference reference )
387 throws LayoutException, IOException, SAXException, ParserConfigurationException
388 {
389 File metadataFile = new File( repository.getRepoRoot(), tools.toPath( reference ) );
390 String actualMetadata = FileUtils.readFileToString( metadataFile, null );
391
392 DetailedDiff detailedDiff = new DetailedDiff( new Diff( expectedMetadata, actualMetadata ) );
393 if ( !detailedDiff.similar() )
394 {
395
396 assertEquals( expectedMetadata, actualMetadata );
397 }
398 }
399
400 private void assertMetadataPath( String expected, String actual )
401 {
402 assertEquals( "Repository Specific Metadata Path", expected, actual );
403 }
404
405 private void assertUpdatedProjectMetadata( String artifactId, String[] expectedVersions, String latestVersion,
406 String releaseVersion )
407 throws Exception
408 {
409 ManagedRepositoryContent testRepo = createTestRepoContent();
410 ProjectReference reference = new ProjectReference();
411 reference.setGroupId( "org.apache.archiva.metadata.tests" );
412 reference.setArtifactId( artifactId );
413
414 prepTestRepo( testRepo, reference );
415
416 tools.updateMetadata( testRepo, reference );
417
418 StringBuilder buf = new StringBuilder();
419 buf.append( "<metadata>\n" );
420 buf.append( " <groupId>" ).append( reference.getGroupId() ).append( "</groupId>\n" );
421 buf.append( " <artifactId>" ).append( reference.getArtifactId() ).append( "</artifactId>\n" );
422
423
424 if ( expectedVersions != null )
425 {
426 buf.append( " <versioning>\n" );
427 if ( latestVersion != null )
428 {
429 buf.append( " <latest>" ).append( latestVersion ).append( "</latest>\n" );
430 }
431 if ( releaseVersion != null )
432 {
433 buf.append( " <release>" ).append( releaseVersion ).append( "</release>\n" );
434 }
435
436 buf.append( " <versions>\n" );
437 for ( int i = 0; i < expectedVersions.length; i++ )
438 {
439 buf.append( " <version>" ).append( expectedVersions[i] ).append( "</version>\n" );
440 }
441 buf.append( " </versions>\n" );
442 buf.append( " </versioning>\n" );
443 }
444 buf.append( "</metadata>" );
445
446 assertMetadata( buf.toString(), testRepo, reference );
447 }
448
449 private void assertProjectMetadata( ManagedRepositoryContent testRepo, ProjectReference reference, String artifactId,
450 String[] expectedVersions, String latestVersion, String releaseVersion )
451 throws Exception
452 {
453 StringBuilder buf = new StringBuilder();
454 buf.append( "<metadata>\n" );
455 buf.append( " <groupId>" ).append( reference.getGroupId() ).append( "</groupId>\n" );
456 buf.append( " <artifactId>" ).append( reference.getArtifactId() ).append( "</artifactId>\n" );
457
458 if ( expectedVersions != null )
459 {
460 buf.append( " <versioning>\n" );
461 if ( latestVersion != null )
462 {
463 buf.append( " <latest>" ).append( latestVersion ).append( "</latest>\n" );
464 }
465 if ( releaseVersion != null )
466 {
467 buf.append( " <release>" ).append( releaseVersion ).append( "</release>\n" );
468 }
469
470 buf.append( " <versions>\n" );
471 for ( int i = 0; i < expectedVersions.length; i++ )
472 {
473 buf.append( " <version>" ).append( expectedVersions[i] ).append( "</version>\n" );
474 }
475 buf.append( " </versions>\n" );
476 buf.append( " </versioning>\n" );
477 }
478 buf.append( "</metadata>" );
479
480 assertMetadata( buf.toString(), testRepo, reference );
481 }
482
483 private void assertUpdatedReleaseVersionMetadata( String artifactId, String version )
484 throws Exception
485 {
486 ManagedRepositoryContent testRepo = createTestRepoContent();
487 VersionedReference reference = new VersionedReference();
488 reference.setGroupId( "org.apache.archiva.metadata.tests" );
489 reference.setArtifactId( artifactId );
490 reference.setVersion( version );
491
492 prepTestRepo( testRepo, reference );
493
494 tools.updateMetadata( testRepo, reference );
495
496 StringBuilder buf = new StringBuilder();
497 buf.append( "<metadata>\n" );
498 buf.append( " <groupId>" ).append( reference.getGroupId() ).append( "</groupId>\n" );
499 buf.append( " <artifactId>" ).append( reference.getArtifactId() ).append( "</artifactId>\n" );
500 buf.append( " <version>" ).append( reference.getVersion() ).append( "</version>\n" );
501 buf.append( "</metadata>" );
502
503 assertMetadata( buf.toString(), testRepo, reference );
504 }
505
506 private void assertUpdatedSnapshotVersionMetadata( String artifactId, String version, String expectedDate,
507 String expectedTime, String expectedBuildNumber )
508 throws Exception
509 {
510 ManagedRepositoryContent testRepo = createTestRepoContent();
511 VersionedReference reference = new VersionedReference();
512 reference.setGroupId( "org.apache.archiva.metadata.tests" );
513 reference.setArtifactId( artifactId );
514 reference.setVersion( version );
515
516 prepTestRepo( testRepo, reference );
517
518 tools.updateMetadata( testRepo, reference );
519
520 StringBuilder buf = new StringBuilder();
521 buf.append( "<metadata>\n" );
522 buf.append( " <groupId>" ).append( reference.getGroupId() ).append( "</groupId>\n" );
523 buf.append( " <artifactId>" ).append( reference.getArtifactId() ).append( "</artifactId>\n" );
524 buf.append( " <version>" ).append( reference.getVersion() ).append( "</version>\n" );
525 buf.append( " <versioning>\n" );
526 buf.append( " <snapshot>\n" );
527 buf.append( " <buildNumber>" ).append( expectedBuildNumber ).append( "</buildNumber>\n" );
528 buf.append( " <timestamp>" );
529 buf.append( expectedDate ).append( "." ).append( expectedTime );
530 buf.append( "</timestamp>\n" );
531 buf.append( " </snapshot>\n" );
532 buf.append( " <lastUpdated>" ).append( expectedDate ).append( expectedTime ).append( "</lastUpdated>\n" );
533 buf.append( " </versioning>\n" );
534 buf.append( "</metadata>" );
535
536 assertMetadata( buf.toString(), testRepo, reference );
537 }
538
539 private void createProxyConnector( String sourceRepoId, String targetRepoId )
540 {
541 ProxyConnectorConfiguration connectorConfig = new ProxyConnectorConfiguration();
542 connectorConfig.setSourceRepoId( sourceRepoId );
543 connectorConfig.setTargetRepoId( targetRepoId );
544 connectorConfig.addPolicy( ProxyConnectorConfiguration.POLICY_CHECKSUM, ChecksumPolicy.IGNORE );
545 connectorConfig.addPolicy( ProxyConnectorConfiguration.POLICY_RELEASES, ReleasesPolicy.ALWAYS );
546 connectorConfig.addPolicy( ProxyConnectorConfiguration.POLICY_SNAPSHOTS, SnapshotsPolicy.ALWAYS );
547 connectorConfig.addPolicy( ProxyConnectorConfiguration.POLICY_CACHE_FAILURES, CachedFailuresPolicy.NO );
548
549 int count = config.getConfiguration().getProxyConnectors().size();
550 config.getConfiguration().addProxyConnector( connectorConfig );
551
552
553 String prefix = "proxyConnectors.proxyConnector(" + count + ")";
554 config.triggerChange( prefix + ".sourceRepoId", connectorConfig.getSourceRepoId() );
555 config.triggerChange( prefix + ".targetRepoId", connectorConfig.getTargetRepoId() );
556 config.triggerChange( prefix + ".proxyId", connectorConfig.getProxyId() );
557 config.triggerChange( prefix + ".policies.releases", connectorConfig.getPolicy( "releases", "" ) );
558 config.triggerChange( prefix + ".policies.checksum", connectorConfig.getPolicy( "checksum", "" ) );
559 config.triggerChange( prefix + ".policies.snapshots", connectorConfig.getPolicy( "snapshots", "" ) );
560 config.triggerChange( prefix + ".policies.cache-failures", connectorConfig.getPolicy( "cache-failures", "" ) );
561 }
562
563 private ManagedRepositoryContent createTestRepoContent()
564 throws Exception
565 {
566 File repoRoot = new File( "target/metadata-tests/" + getName() );
567 if ( repoRoot.exists() )
568 {
569 FileUtils.deleteDirectory( repoRoot );
570 }
571
572 repoRoot.mkdirs();
573
574 ManagedRepositoryConfiguration repoConfig = createRepository( "test-repo", "Test Repository: " + getName(),
575 repoRoot );
576
577 ManagedRepositoryContent repoContent = (ManagedRepositoryContent) lookup( ManagedRepositoryContent.class,
578 "default" );
579 repoContent.setRepository( repoConfig );
580 return repoContent;
581 }
582
583 private void prepTestRepo( ManagedRepositoryContent repo, ProjectReference reference )
584 throws IOException
585 {
586 String groupDir = StringUtils.replaceChars( reference.getGroupId(), '.', '/' );
587 String path = groupDir + "/" + reference.getArtifactId();
588
589 File srcRepoDir = new File( "src/test/repositories/metadata-repository" );
590 File srcDir = new File( srcRepoDir, path );
591 File destDir = new File( repo.getRepoRoot(), path );
592
593 assertTrue( "Source Dir exists: " + srcDir, srcDir.exists() );
594 destDir.mkdirs();
595
596 FileUtils.copyDirectory( srcDir, destDir );
597 }
598
599 private void prepTestRepo( ManagedRepositoryContent repo, VersionedReference reference )
600 throws IOException
601 {
602 ProjectReference projectRef = new ProjectReference();
603 projectRef.setGroupId( reference.getGroupId() );
604 projectRef.setArtifactId( reference.getArtifactId() );
605
606 prepTestRepo( repo, projectRef );
607 }
608
609 protected void setUp()
610 throws Exception
611 {
612 super.setUp();
613
614 config = (MockConfiguration) lookup( ArchivaConfiguration.class.getName(), "mock" );
615 tools = (MetadataTools) lookup( MetadataTools.class );
616 }
617 }