1 package org.apache.maven.archiva.converter.artifact;
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.maven.archiva.converter.artifact.ArtifactConversionException;
23 import org.apache.maven.archiva.converter.artifact.ArtifactConverter;
24 import org.apache.maven.archiva.converter.artifact.Messages;
25 import org.apache.maven.artifact.Artifact;
26 import org.apache.maven.artifact.factory.ArtifactFactory;
27 import org.apache.maven.artifact.metadata.ArtifactMetadata;
28 import org.apache.maven.artifact.repository.ArtifactRepository;
29 import org.apache.maven.artifact.repository.ArtifactRepositoryFactory;
30 import org.apache.maven.artifact.repository.layout.ArtifactRepositoryLayout;
31 import org.apache.maven.artifact.repository.metadata.ArtifactRepositoryMetadata;
32 import org.apache.maven.artifact.repository.metadata.SnapshotArtifactRepositoryMetadata;
33 import org.codehaus.plexus.PlexusTestCase;
34 import org.codehaus.plexus.util.FileUtils;
37 import java.io.IOException;
38 import java.text.SimpleDateFormat;
39 import java.util.ArrayList;
40 import java.util.Iterator;
41 import java.util.List;
42 import java.util.Locale;
44 import java.util.Map.Entry;
45 import java.util.regex.Matcher;
48 * LegacyToDefaultConverterTest
50 * @author <a href="mailto:joakime@apache.org">Joakim Erdfelt</a>
53 public class LegacyToDefaultConverterTest
54 extends PlexusTestCase
56 private ArtifactRepository sourceRepository;
58 private ArtifactRepository targetRepository;
60 private ArtifactConverter artifactConverter;
62 private ArtifactFactory artifactFactory;
64 private static final int SLEEP_MILLIS = 100;
66 protected void setUp()
71 ArtifactRepositoryFactory factory = (ArtifactRepositoryFactory) lookup( ArtifactRepositoryFactory.ROLE );
73 ArtifactRepositoryLayout layout = (ArtifactRepositoryLayout) lookup( ArtifactRepositoryLayout.ROLE, "legacy" );
75 File sourceBase = getTestFile( "src/test/source-repository" );
76 sourceRepository = factory.createArtifactRepository( "source", sourceBase.toURL().toString(), layout, null,
79 layout = (ArtifactRepositoryLayout) lookup( ArtifactRepositoryLayout.ROLE, "default" );
81 File targetBase = getTestFile( "target/test-target-repository" );
82 copyDirectoryStructure( getTestFile( "src/test/target-repository" ), targetBase );
84 targetRepository = factory.createArtifactRepository( "target", targetBase.toURL().toString(), layout, null,
87 artifactConverter = (ArtifactConverter) lookup( ArtifactConverter.ROLE, "legacy-to-default" );
89 artifactFactory = (ArtifactFactory) lookup( ArtifactFactory.ROLE );
92 protected void tearDown()
98 private void copyDirectoryStructure( File sourceDirectory, File destinationDirectory )
101 if ( !sourceDirectory.exists() )
103 throw new IOException( "Source directory doesn't exists (" + sourceDirectory.getAbsolutePath() + ")." );
106 File[] files = sourceDirectory.listFiles();
108 String sourcePath = sourceDirectory.getAbsolutePath();
110 for ( int i = 0; i < files.length; i++ )
112 File file = files[i];
114 String dest = file.getAbsolutePath();
116 dest = dest.substring( sourcePath.length() + 1 );
118 File destination = new File( destinationDirectory, dest );
122 destination = destination.getParentFile();
124 FileUtils.copyFileToDirectory( file, destination );
126 else if ( file.isDirectory() )
128 if ( !".svn".equals( file.getName() ) )
130 if ( !destination.exists() && !destination.mkdirs() )
132 throw new IOException( "Could not create destination directory '"
133 + destination.getAbsolutePath() + "'." );
135 copyDirectoryStructure( file, destination );
140 throw new IOException( "Unknown file type: " + file.getAbsolutePath() );
145 public void testV4PomConvert()
148 // test that it is copied as is
150 Artifact artifact = createArtifact( "test", "v4artifact", "1.0.0" );
151 ArtifactMetadata artifactMetadata = new ArtifactRepositoryMetadata( artifact );
152 File artifactMetadataFile = new File( targetRepository.getBasedir(), targetRepository
153 .pathOfRemoteRepositoryMetadata( artifactMetadata ) );
154 artifactMetadataFile.delete();
156 ArtifactMetadata versionMetadata = new SnapshotArtifactRepositoryMetadata( artifact );
157 File versionMetadataFile = new File( targetRepository.getBasedir(), targetRepository
158 .pathOfRemoteRepositoryMetadata( versionMetadata ) );
159 versionMetadataFile.delete();
161 File artifactFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
162 artifactFile.delete();
164 artifactConverter.convert( artifact, targetRepository );
165 checkSuccess( artifactConverter );
167 assertTrue( "Check artifact created", artifactFile.exists() );
168 assertTrue( "Check artifact matches", FileUtils.contentEquals( artifactFile, artifact.getFile() ) );
170 artifact = createPomArtifact( artifact );
171 File pomFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
172 File sourcePomFile = new File( sourceRepository.getBasedir(), sourceRepository.pathOf( artifact ) );
173 assertTrue( "Check POM created", pomFile.exists() );
175 compareFiles( sourcePomFile, pomFile );
177 assertTrue( "Check artifact metadata created", artifactMetadataFile.exists() );
179 File expectedMetadataFile = getTestFile( "src/test/expected-files/v4-artifact-metadata.xml" );
181 compareFiles( expectedMetadataFile, artifactMetadataFile );
183 assertTrue( "Check snapshot metadata created", versionMetadataFile.exists() );
185 expectedMetadataFile = getTestFile( "src/test/expected-files/v4-version-metadata.xml" );
187 compareFiles( expectedMetadataFile, versionMetadataFile );
190 public void testV3PomConvert()
193 // test that the pom is coverted
195 Artifact artifact = createArtifact( "test", "v3artifact", "1.0.0" );
196 ArtifactMetadata artifactMetadata = new ArtifactRepositoryMetadata( artifact );
197 File artifactMetadataFile = new File( targetRepository.getBasedir(), targetRepository
198 .pathOfRemoteRepositoryMetadata( artifactMetadata ) );
199 artifactMetadataFile.delete();
201 ArtifactMetadata versionMetadata = new SnapshotArtifactRepositoryMetadata( artifact );
202 File versionMetadataFile = new File( targetRepository.getBasedir(), targetRepository
203 .pathOfRemoteRepositoryMetadata( versionMetadata ) );
204 versionMetadataFile.delete();
206 artifactConverter.convert( artifact, targetRepository );
207 checkSuccess( artifactConverter );
209 File artifactFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
210 assertTrue( "Check artifact created", artifactFile.exists() );
211 assertTrue( "Check artifact matches", FileUtils.contentEquals( artifactFile, artifact.getFile() ) );
213 artifact = createPomArtifact( artifact );
214 File pomFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
215 File expectedPomFile = getTestFile( "src/test/expected-files/converted-v3.pom" );
216 assertTrue( "Check POM created", pomFile.exists() );
218 compareFiles( expectedPomFile, pomFile );
220 assertTrue( "Check artifact metadata created", artifactMetadataFile.exists() );
222 File expectedMetadataFile = getTestFile( "src/test/expected-files/v3-artifact-metadata.xml" );
224 compareFiles( expectedMetadataFile, artifactMetadataFile );
226 assertTrue( "Check snapshot metadata created", versionMetadataFile.exists() );
228 expectedMetadataFile = getTestFile( "src/test/expected-files/v3-version-metadata.xml" );
230 compareFiles( expectedMetadataFile, versionMetadataFile );
233 public void testV3PomConvertWithRelocation()
236 Artifact artifact = createArtifact( "test", "relocated-v3artifact", "1.0.0" );
237 ArtifactMetadata artifactMetadata = new ArtifactRepositoryMetadata( artifact );
238 File artifactMetadataFile = new File( targetRepository.getBasedir(), targetRepository
239 .pathOfRemoteRepositoryMetadata( artifactMetadata ) );
240 artifactMetadataFile.delete();
242 ArtifactMetadata versionMetadata = new SnapshotArtifactRepositoryMetadata( artifact );
243 File versionMetadataFile = new File( targetRepository.getBasedir(), targetRepository
244 .pathOfRemoteRepositoryMetadata( versionMetadata ) );
245 versionMetadataFile.delete();
247 artifactConverter.convert( artifact, targetRepository );
248 //checkSuccess(); --> commented until MNG-2100 is fixed
250 File artifactFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
251 assertTrue( "Check if relocated artifact created", artifactFile.exists() );
252 assertTrue( "Check if relocated artifact matches", FileUtils.contentEquals( artifactFile, artifact.getFile() ) );
253 Artifact pomArtifact = createArtifact( "relocated-test", "relocated-v3artifact", "1.0.0", "1.0.0", "pom" );
254 File pomFile = getTestFile( "src/test/expected-files/" + targetRepository.pathOf( pomArtifact ) );
255 File testFile = getTestFile( "target/test-target-repository/" + targetRepository.pathOf( pomArtifact ) );
256 compareFiles( pomFile, testFile );
258 Artifact orig = createArtifact( "test", "relocated-v3artifact", "1.0.0", "1.0.0", "pom" );
259 artifactFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( orig ) );
260 assertTrue( "Check if relocation artifact pom is created", artifactFile.exists() );
261 testFile = getTestFile( "src/test/expected-files/" + targetRepository.pathOf( orig ) );
262 compareFiles( artifactFile, testFile );
265 public void testV3PomWarningsOnConvert()
268 // test that the pom is converted but that warnings are reported
270 Artifact artifact = createArtifact( "test", "v3-warnings-artifact", "1.0.0" );
271 ArtifactMetadata artifactMetadata = new ArtifactRepositoryMetadata( artifact );
272 File artifactMetadataFile = new File( targetRepository.getBasedir(), targetRepository
273 .pathOfRemoteRepositoryMetadata( artifactMetadata ) );
274 artifactMetadataFile.delete();
276 ArtifactMetadata versionMetadata = new SnapshotArtifactRepositoryMetadata( artifact );
277 File versionMetadataFile = new File( targetRepository.getBasedir(), targetRepository
278 .pathOfRemoteRepositoryMetadata( versionMetadata ) );
279 versionMetadataFile.delete();
281 artifactConverter.convert( artifact, targetRepository );
282 checkWarnings( artifactConverter, 2 );
284 File artifactFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
285 assertTrue( "Check artifact created", artifactFile.exists() );
286 assertTrue( "Check artifact matches", FileUtils.contentEquals( artifactFile, artifact.getFile() ) );
288 artifact = createPomArtifact( artifact );
289 File pomFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
290 File expectedPomFile = getTestFile( "src/test/expected-files/converted-v3-warnings.pom" );
291 assertTrue( "Check POM created", pomFile.exists() );
293 compareFiles( expectedPomFile, pomFile );
295 // TODO: check 2 warnings (extend and versions) matched on i18n key
298 private void doTestV4SnapshotPomConvert( String version, String expectedMetadataFileName )
301 // test that it is copied as is
303 Artifact artifact = createArtifact( "test", "v4artifact", version );
304 ArtifactMetadata artifactMetadata = new ArtifactRepositoryMetadata( artifact );
305 File artifactMetadataFile = new File( targetRepository.getBasedir(), targetRepository
306 .pathOfRemoteRepositoryMetadata( artifactMetadata ) );
307 artifactMetadataFile.delete();
309 ArtifactMetadata snapshotMetadata = new SnapshotArtifactRepositoryMetadata( artifact );
310 File snapshotMetadataFile = new File( targetRepository.getBasedir(), targetRepository
311 .pathOfRemoteRepositoryMetadata( snapshotMetadata ) );
312 snapshotMetadataFile.delete();
314 artifactConverter.convert( artifact, targetRepository );
315 checkSuccess( artifactConverter );
317 File artifactFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
318 assertTrue( "Check artifact created", artifactFile.exists() );
319 assertTrue( "Check artifact matches", FileUtils.contentEquals( artifactFile, artifact.getFile() ) );
321 artifact = createPomArtifact( artifact );
322 File pomFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
323 File sourcePomFile = new File( sourceRepository.getBasedir(), sourceRepository.pathOf( artifact ) );
324 assertTrue( "Check POM created", pomFile.exists() );
326 compareFiles( sourcePomFile, pomFile );
328 assertTrue( "Check artifact metadata created", artifactMetadataFile.exists() );
330 File expectedMetadataFile = getTestFile( "src/test/expected-files/v4-snapshot-artifact-metadata.xml" );
332 compareFiles( expectedMetadataFile, artifactMetadataFile );
334 assertTrue( "Check snapshot metadata created", snapshotMetadataFile.exists() );
336 expectedMetadataFile = getTestFile( expectedMetadataFileName );
338 compareFiles( expectedMetadataFile, snapshotMetadataFile );
341 public void testV3SnapshotPomConvert()
344 // test that the pom is coverted
346 Artifact artifact = createArtifact( "test", "v3artifact", "1.0.0-SNAPSHOT" );
347 ArtifactMetadata artifactMetadata = new ArtifactRepositoryMetadata( artifact );
348 File artifactMetadataFile = new File( targetRepository.getBasedir(), targetRepository
349 .pathOfRemoteRepositoryMetadata( artifactMetadata ) );
350 artifactMetadataFile.delete();
352 ArtifactMetadata snapshotMetadata = new SnapshotArtifactRepositoryMetadata( artifact );
353 File snapshotMetadataFile = new File( targetRepository.getBasedir(), targetRepository
354 .pathOfRemoteRepositoryMetadata( snapshotMetadata ) );
355 snapshotMetadataFile.delete();
357 artifactConverter.convert( artifact, targetRepository );
358 checkSuccess( artifactConverter );
360 File artifactFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
361 assertTrue( "Check artifact created", artifactFile.exists() );
362 assertTrue( "Check artifact matches", FileUtils.contentEquals( artifactFile, artifact.getFile() ) );
364 artifact = createPomArtifact( artifact );
365 File pomFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
366 File expectedPomFile = getTestFile( "src/test/expected-files/converted-v3-snapshot.pom" );
367 assertTrue( "Check POM created", pomFile.exists() );
369 compareFiles( expectedPomFile, pomFile );
371 assertTrue( "Check artifact metadata created", artifactMetadataFile.exists() );
373 File expectedMetadataFile = getTestFile( "src/test/expected-files/v3-snapshot-artifact-metadata.xml" );
375 compareFiles( expectedMetadataFile, artifactMetadataFile );
377 assertTrue( "Check snapshot metadata created", snapshotMetadataFile.exists() );
379 expectedMetadataFile = getTestFile( "src/test/expected-files/v3-snapshot-metadata.xml" );
381 compareFiles( expectedMetadataFile, snapshotMetadataFile );
384 public void testV4SnapshotPomConvert()
387 doTestV4SnapshotPomConvert( "1.0.0-SNAPSHOT", "src/test/expected-files/v4-snapshot-metadata.xml" );
392 public void testV4TimestampedSnapshotPomConvert()
395 doTestV4SnapshotPomConvert( "1.0.0-20060111.120115-1",
396 "src/test/expected-files/v4-timestamped-snapshot-metadata.xml" );
401 public void testMavenOnePluginConversion()
404 Artifact artifact = createArtifact( "org.apache.maven.plugins", "maven-foo-plugin", "1.0", "1.0",
406 artifact.setFile( new File( getBasedir(), "src/test/source-repository/test/plugins/maven-foo-plugin-1.0.jar" ) );
407 artifactConverter.convert( artifact, targetRepository );
408 // There is a warning but I can't figure out how to look at it. Eyeballing the results it appears
409 // the plugin is being coverted correctly.
412 File artifactFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
413 assertTrue( "Check artifact created", artifactFile.exists() );
414 assertTrue( "Check artifact matches", FileUtils.contentEquals( artifactFile, artifact.getFile() ) );
417 The POM isn't needed for Maven 1.x plugins but the raw conversion for
419 artifact = createPomArtifact( artifact );
420 File pomFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
421 File expectedPomFile = getTestFile( "src/test/expected-files/maven-foo-plugin-1.0.pom" );
422 assertTrue( "Check POM created", pomFile.exists() );
423 compareFiles( expectedPomFile, pomFile );
427 public void testV3TimestampedSnapshotPomConvert()
430 // test that the pom is coverted
432 Artifact artifact = createArtifact( "test", "v3artifact", "1.0.0-20060105.130101-3" );
433 ArtifactMetadata artifactMetadata = new ArtifactRepositoryMetadata( artifact );
434 File artifactMetadataFile = new File( targetRepository.getBasedir(), targetRepository
435 .pathOfRemoteRepositoryMetadata( artifactMetadata ) );
436 artifactMetadataFile.delete();
438 ArtifactMetadata snapshotMetadata = new SnapshotArtifactRepositoryMetadata( artifact );
439 File snapshotMetadataFile = new File( targetRepository.getBasedir(), targetRepository
440 .pathOfRemoteRepositoryMetadata( snapshotMetadata ) );
441 snapshotMetadataFile.delete();
443 artifactConverter.convert( artifact, targetRepository );
444 checkSuccess( artifactConverter );
446 File artifactFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
447 assertTrue( "Check artifact created", artifactFile.exists() );
448 assertTrue( "Check artifact matches", FileUtils.contentEquals( artifactFile, artifact.getFile() ) );
450 artifact = createPomArtifact( artifact );
451 File pomFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
452 File expectedPomFile = getTestFile( "src/test/expected-files/converted-v3-timestamped-snapshot.pom" );
453 assertTrue( "Check POM created", pomFile.exists() );
455 compareFiles( expectedPomFile, pomFile );
457 assertTrue( "Check artifact snapshotMetadata created", artifactMetadataFile.exists() );
459 File expectedMetadataFile = getTestFile( "src/test/expected-files/v3-snapshot-artifact-metadata.xml" );
461 compareFiles( expectedMetadataFile, artifactMetadataFile );
463 assertTrue( "Check snapshot snapshotMetadata created", snapshotMetadataFile.exists() );
465 expectedMetadataFile = getTestFile( "src/test/expected-files/v3-timestamped-snapshot-metadata.xml" );
467 compareFiles( expectedMetadataFile, snapshotMetadataFile );
470 public void testNoPomConvert()
473 // test that a POM is not created when there was none at the source
475 Artifact artifact = createArtifact( "test", "noPomArtifact", "1.0.0" );
476 artifactConverter.convert( artifact, targetRepository );
477 checkWarnings( artifactConverter, 1 );
479 assertHasWarningReason( artifactConverter, Messages.getString( "warning.missing.pom" ) );
481 File artifactFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
482 assertTrue( "Check artifact created", artifactFile.exists() );
483 assertTrue( "Check artifact matches", FileUtils.contentEquals( artifactFile, artifact.getFile() ) );
485 artifact = createPomArtifact( artifact );
486 File pomFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
487 File sourcePomFile = new File( sourceRepository.getBasedir(), sourceRepository.pathOf( artifact ) );
489 assertFalse( "Check no POM created", pomFile.exists() );
490 assertFalse( "No source POM", sourcePomFile.exists() );
493 public void testIncorrectSourceChecksumMd5()
496 // test that it fails when the source md5 is wrong
498 Artifact artifact = createArtifact( "test", "incorrectMd5Artifact", "1.0.0" );
499 File file = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
502 artifactConverter.convert( artifact, targetRepository );
503 checkWarnings( artifactConverter, 2 );
505 assertHasWarningReason( artifactConverter, Messages.getString( "failure.incorrect.md5" ) );
507 assertFalse( "Check artifact not created", file.exists() );
509 ArtifactRepositoryMetadata metadata = new ArtifactRepositoryMetadata( artifact );
510 File metadataFile = new File( targetRepository.getBasedir(), targetRepository
511 .pathOfRemoteRepositoryMetadata( metadata ) );
512 assertFalse( "Check metadata not created", metadataFile.exists() );
515 public void testIncorrectSourceChecksumSha1()
518 // test that it fails when the source sha1 is wrong
520 Artifact artifact = createArtifact( "test", "incorrectSha1Artifact", "1.0.0" );
521 File file = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
524 artifactConverter.convert( artifact, targetRepository );
525 checkWarnings( artifactConverter, 2 );
527 assertHasWarningReason( artifactConverter, Messages.getString( "failure.incorrect.sha1" ) );
529 assertFalse( "Check artifact not created", file.exists() );
531 ArtifactRepositoryMetadata metadata = new ArtifactRepositoryMetadata( artifact );
532 File metadataFile = new File( targetRepository.getBasedir(), targetRepository
533 .pathOfRemoteRepositoryMetadata( metadata ) );
534 assertFalse( "Check metadata not created", metadataFile.exists() );
537 public void testUnmodifiedArtifact()
538 throws Exception, InterruptedException
540 // test the unmodified artifact is untouched
542 Artifact artifact = createArtifact( "test", "unmodified-artifact", "1.0.0" );
543 Artifact pomArtifact = createPomArtifact( artifact );
545 File sourceFile = new File( sourceRepository.getBasedir(), sourceRepository.pathOf( artifact ) );
546 File sourcePomFile = new File( sourceRepository.getBasedir(), sourceRepository.pathOf( pomArtifact ) );
547 File targetFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
548 File targetPomFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( pomArtifact ) );
550 assertTrue( "Check target file exists", targetFile.exists() );
551 assertTrue( "Check target POM exists", targetPomFile.exists() );
553 sourceFile.setLastModified( System.currentTimeMillis() );
554 sourcePomFile.setLastModified( System.currentTimeMillis() );
556 long origTime = targetFile.lastModified();
557 long origPomTime = targetPomFile.lastModified();
559 // Need to guarantee last modified is not equal
560 Thread.sleep( SLEEP_MILLIS );
562 artifactConverter.convert( artifact, targetRepository );
563 checkSuccess( artifactConverter );
565 compareFiles( sourceFile, targetFile );
566 compareFiles( sourcePomFile, targetPomFile );
568 assertEquals( "Check artifact unmodified", origTime, targetFile.lastModified() );
569 assertEquals( "Check POM unmodified", origPomTime, targetPomFile.lastModified() );
572 public void testModifedArtifactFails()
575 // test that it fails when the source artifact has changed and is different to the existing artifact in the
578 Artifact artifact = createArtifact( "test", "modified-artifact", "1.0.0" );
579 Artifact pomArtifact = createPomArtifact( artifact );
581 File sourceFile = new File( sourceRepository.getBasedir(), sourceRepository.pathOf( artifact ) );
582 File sourcePomFile = new File( sourceRepository.getBasedir(), sourceRepository.pathOf( pomArtifact ) );
583 File targetFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
584 File targetPomFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( pomArtifact ) );
586 assertTrue( "Check target file exists", targetFile.exists() );
587 assertTrue( "Check target POM exists", targetPomFile.exists() );
589 sourceFile.setLastModified( System.currentTimeMillis() );
590 sourcePomFile.setLastModified( System.currentTimeMillis() );
592 long origTime = targetFile.lastModified();
593 long origPomTime = targetPomFile.lastModified();
595 // Need to guarantee last modified is not equal
596 Thread.sleep( SLEEP_MILLIS );
598 artifactConverter.convert( artifact, targetRepository );
599 checkWarnings( artifactConverter, 2 );
601 assertHasWarningReason( artifactConverter, Messages.getString( "failure.target.already.exists" ) );
603 assertEquals( "Check unmodified", origTime, targetFile.lastModified() );
604 assertEquals( "Check unmodified", origPomTime, targetPomFile.lastModified() );
606 ArtifactRepositoryMetadata metadata = new ArtifactRepositoryMetadata( artifact );
607 File metadataFile = new File( targetRepository.getBasedir(), targetRepository
608 .pathOfRemoteRepositoryMetadata( metadata ) );
609 assertFalse( "Check metadata not created", metadataFile.exists() );
612 public void testForcedUnmodifiedArtifact()
615 // test unmodified artifact is still converted when set to force
617 artifactConverter = (ArtifactConverter) lookup( ArtifactConverter.ROLE, "force-repository-converter" );
619 Artifact artifact = createArtifact( "test", "unmodified-artifact", "1.0.0" );
620 Artifact pomArtifact = createPomArtifact( artifact );
622 File sourceFile = new File( sourceRepository.getBasedir(), sourceRepository.pathOf( artifact ) );
623 File sourcePomFile = new File( sourceRepository.getBasedir(), sourceRepository.pathOf( pomArtifact ) );
624 File targetFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
625 File targetPomFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( pomArtifact ) );
627 SimpleDateFormat dateFormat = new SimpleDateFormat( "yyyy-MM-dd", Locale.getDefault() );
628 long origTime = dateFormat.parse( "2006-03-03" ).getTime();
629 targetFile.setLastModified( origTime );
630 targetPomFile.setLastModified( origTime );
632 sourceFile.setLastModified( dateFormat.parse( "2006-01-01" ).getTime() );
633 sourcePomFile.setLastModified( dateFormat.parse( "2006-02-02" ).getTime() );
635 artifactConverter.convert( artifact, targetRepository );
636 checkSuccess( artifactConverter );
638 compareFiles( sourceFile, targetFile );
639 compareFiles( sourcePomFile, targetPomFile );
641 assertFalse( "Check modified", origTime == targetFile.lastModified() );
642 assertFalse( "Check modified", origTime == targetPomFile.lastModified() );
644 ArtifactRepositoryMetadata metadata = new ArtifactRepositoryMetadata( artifact );
645 File metadataFile = new File( targetRepository.getBasedir(), targetRepository
646 .pathOfRemoteRepositoryMetadata( metadata ) );
647 assertTrue( "Check metadata created", metadataFile.exists() );
650 public void testDryRunSuccess()
653 // test dry run does nothing on a run that will be successful, and returns success
655 artifactConverter = (ArtifactConverter) lookup( ArtifactConverter.ROLE, "dryrun-repository-converter" );
657 Artifact artifact = createArtifact( "test", "dryrun-artifact", "1.0.0" );
658 Artifact pomArtifact = createPomArtifact( artifact );
660 File sourceFile = new File( sourceRepository.getBasedir(), sourceRepository.pathOf( artifact ) );
661 File sourcePomFile = new File( sourceRepository.getBasedir(), sourceRepository.pathOf( pomArtifact ) );
662 File targetFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
663 File targetPomFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( pomArtifact ) );
665 artifactConverter.convert( artifact, targetRepository );
666 checkSuccess( artifactConverter );
668 assertTrue( "Check source file exists", sourceFile.exists() );
669 assertTrue( "Check source POM exists", sourcePomFile.exists() );
671 assertFalse( "Check target file doesn't exist", targetFile.exists() );
672 assertFalse( "Check target POM doesn't exist", targetPomFile.exists() );
674 ArtifactRepositoryMetadata metadata = new ArtifactRepositoryMetadata( artifact );
675 File metadataFile = new File( targetRepository.getBasedir(), targetRepository
676 .pathOfRemoteRepositoryMetadata( metadata ) );
677 assertFalse( "Check metadata not created", metadataFile.exists() );
680 public void testDryRunFailure()
683 // test dry run does nothing on a run that will fail, and returns failure
685 artifactConverter = (ArtifactConverter) lookup( ArtifactConverter.ROLE, "dryrun-repository-converter" );
687 Artifact artifact = createArtifact( "test", "modified-artifact", "1.0.0" );
688 Artifact pomArtifact = createPomArtifact( artifact );
690 File sourceFile = new File( sourceRepository.getBasedir(), sourceRepository.pathOf( artifact ) );
691 File sourcePomFile = new File( sourceRepository.getBasedir(), sourceRepository.pathOf( pomArtifact ) );
692 File targetFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
693 File targetPomFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( pomArtifact ) );
695 assertTrue( "Check target file exists", targetFile.exists() );
696 assertTrue( "Check target POM exists", targetPomFile.exists() );
698 sourceFile.setLastModified( System.currentTimeMillis() );
699 sourcePomFile.setLastModified( System.currentTimeMillis() );
701 long origTime = targetFile.lastModified();
702 long origPomTime = targetPomFile.lastModified();
704 // Need to guarantee last modified is not equal
705 Thread.sleep( SLEEP_MILLIS );
707 artifactConverter.convert( artifact, targetRepository );
708 checkWarnings( artifactConverter, 2 );
710 assertHasWarningReason( artifactConverter, Messages.getString( "failure.target.already.exists" ) );
712 assertEquals( "Check unmodified", origTime, targetFile.lastModified() );
713 assertEquals( "Check unmodified", origPomTime, targetPomFile.lastModified() );
715 ArtifactRepositoryMetadata metadata = new ArtifactRepositoryMetadata( artifact );
716 File metadataFile = new File( targetRepository.getBasedir(), targetRepository
717 .pathOfRemoteRepositoryMetadata( metadata ) );
718 assertFalse( "Check metadata not created", metadataFile.exists() );
721 public void testRollbackArtifactCreated()
724 // test rollback can remove a created artifact, including checksums
726 Artifact artifact = createArtifact( "test", "rollback-created-artifact", "1.0.0" );
727 ArtifactMetadata artifactMetadata = new ArtifactRepositoryMetadata( artifact );
728 File artifactMetadataFile = new File( targetRepository.getBasedir(), targetRepository
729 .pathOfRemoteRepositoryMetadata( artifactMetadata ) );
730 FileUtils.deleteDirectory( artifactMetadataFile.getParentFile() );
732 ArtifactMetadata versionMetadata = new SnapshotArtifactRepositoryMetadata( artifact );
733 File versionMetadataFile = new File( targetRepository.getBasedir(), targetRepository
734 .pathOfRemoteRepositoryMetadata( versionMetadata ) );
736 File artifactFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
738 artifactConverter.convert( artifact, targetRepository );
739 checkWarnings( artifactConverter, 2 );
741 boolean found = false;
742 String pattern = "^" + Messages.getString( "invalid.source.pom" ).replaceFirst( "\\{0\\}", ".*" ) + "$";
743 for ( Iterator it = artifactConverter.getWarnings().values().iterator(); it.hasNext() && !found; )
745 List messages = (List) it.next();
747 for ( Iterator itmsgs = messages.iterator(); itmsgs.hasNext(); )
749 String message = (String) itmsgs.next();
750 if ( message.matches( pattern ) )
758 assertTrue( "Check failure message.", found );
760 assertFalse( "check artifact rolled back", artifactFile.exists() );
761 assertFalse( "check metadata rolled back", artifactMetadataFile.exists() );
762 assertFalse( "check metadata rolled back", versionMetadataFile.exists() );
765 public void testMultipleArtifacts()
768 // test multiple artifacts are converted
770 List artifacts = new ArrayList();
771 artifacts.add( createArtifact( "test", "artifact-one", "1.0.0" ) );
772 artifacts.add( createArtifact( "test", "artifact-two", "1.0.0" ) );
773 artifacts.add( createArtifact( "test", "artifact-three", "1.0.0" ) );
775 for ( Iterator it = artifacts.iterator(); it.hasNext(); )
777 Artifact arti = (Artifact) it.next();
778 artifactConverter.convert( arti, targetRepository );
779 checkSuccess( artifactConverter );
782 for ( Iterator i = artifacts.iterator(); i.hasNext(); )
784 Artifact artifact = (Artifact) i.next();
786 File artifactFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
787 assertTrue( "Check artifact created", artifactFile.exists() );
788 assertTrue( "Check artifact matches", FileUtils.contentEquals( artifactFile, artifact.getFile() ) );
790 artifact = createPomArtifact( artifact );
791 File pomFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
792 File expectedPomFile = getTestFile( "src/test/expected-files/converted-" + artifact.getArtifactId()
794 assertTrue( "Check POM created", pomFile.exists() );
796 compareFiles( expectedPomFile, pomFile );
800 public void testInvalidSourceArtifactMetadata()
803 // test artifact is not converted when source metadata is invalid, and returns failure
805 createModernSourceRepository();
807 Artifact artifact = createArtifact( "test", "incorrectArtifactMetadata", "1.0.0" );
808 File file = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
811 artifactConverter.convert( artifact, targetRepository );
812 checkWarnings( artifactConverter, 2 );
814 assertHasWarningReason( artifactConverter, Messages.getString( "failure.incorrect.artifactMetadata.versions" ) );
816 assertFalse( "Check artifact not created", file.exists() );
818 ArtifactRepositoryMetadata metadata = new ArtifactRepositoryMetadata( artifact );
819 File metadataFile = new File( targetRepository.getBasedir(), targetRepository
820 .pathOfRemoteRepositoryMetadata( metadata ) );
821 assertFalse( "Check metadata not created", metadataFile.exists() );
824 public void testInvalidSourceSnapshotMetadata()
827 // test artifact is not converted when source snapshot metadata is invalid and returns failure
829 createModernSourceRepository();
831 Artifact artifact = createArtifact( "test", "incorrectSnapshotMetadata", "1.0.0-20060102.030405-6" );
832 File file = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
835 artifactConverter.convert( artifact, targetRepository );
836 checkWarnings( artifactConverter, 2 );
838 assertHasWarningReason( artifactConverter, Messages.getString( "failure.incorrect.snapshotMetadata.snapshot" ) );
840 assertFalse( "Check artifact not created", file.exists() );
842 ArtifactRepositoryMetadata metadata = new ArtifactRepositoryMetadata( artifact );
843 File metadataFile = new File( targetRepository.getBasedir(), targetRepository
844 .pathOfRemoteRepositoryMetadata( metadata ) );
845 assertFalse( "Check metadata not created", metadataFile.exists() );
848 public void testMergeArtifactMetadata()
851 // test artifact level metadata is merged when it already exists on successful conversion
853 Artifact artifact = createArtifact( "test", "newversion-artifact", "1.0.1" );
854 artifactConverter.convert( artifact, targetRepository );
855 checkSuccess( artifactConverter );
857 File artifactFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
858 assertTrue( "Check artifact created", artifactFile.exists() );
859 assertTrue( "Check artifact matches", FileUtils.contentEquals( artifactFile, artifact.getFile() ) );
861 artifact = createPomArtifact( artifact );
862 File pomFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
863 File sourcePomFile = new File( sourceRepository.getBasedir(), sourceRepository.pathOf( artifact ) );
864 assertTrue( "Check POM created", pomFile.exists() );
866 compareFiles( sourcePomFile, pomFile );
868 ArtifactMetadata artifactMetadata = new ArtifactRepositoryMetadata( artifact );
869 File artifactMetadataFile = new File( targetRepository.getBasedir(), targetRepository
870 .pathOfRemoteRepositoryMetadata( artifactMetadata ) );
871 assertTrue( "Check artifact metadata created", artifactMetadataFile.exists() );
873 File expectedMetadataFile = getTestFile( "src/test/expected-files/newversion-artifact-metadata.xml" );
875 compareFiles( expectedMetadataFile, artifactMetadataFile );
878 public void testSourceAndTargetRepositoriesMatch()
881 // test that it fails if the same
883 ArtifactRepositoryFactory factory = (ArtifactRepositoryFactory) lookup( ArtifactRepositoryFactory.ROLE );
885 sourceRepository = factory.createArtifactRepository( "source", targetRepository.getUrl(), targetRepository
886 .getLayout(), null, null );
888 Artifact artifact = createArtifact( "test", "repository-artifact", "1.0" );
892 artifactConverter.convert( artifact, targetRepository );
893 fail( "Should have failed trying to convert within the same repository" );
895 catch ( ArtifactConversionException e )
898 assertEquals( "check message", Messages.getString( "exception.repositories.match" ), e.getMessage() );
899 assertNull( "Check no additional cause", e.getCause() );
903 private Artifact createArtifact( String groupId, String artifactId, String version )
905 Matcher matcher = Artifact.VERSION_FILE_PATTERN.matcher( version );
907 if ( matcher.matches() )
909 baseVersion = matcher.group( 1 ) + "-SNAPSHOT";
913 baseVersion = version;
915 return createArtifact( groupId, artifactId, baseVersion, version, "jar" );
918 private Artifact createArtifact( String groupId, String artifactId, String baseVersion, String version, String type )
920 Artifact artifact = artifactFactory.createArtifact( groupId, artifactId, version, null, type );
921 artifact.setBaseVersion( baseVersion );
922 artifact.setRepository( sourceRepository );
923 artifact.setFile( new File( sourceRepository.getBasedir(), sourceRepository.pathOf( artifact ) ) );
927 private Artifact createPomArtifact( Artifact artifact )
929 return createArtifact( artifact.getGroupId(), artifact.getArtifactId(), artifact.getBaseVersion(), artifact
930 .getVersion(), "pom" );
933 private static void compareFiles( File expectedPomFile, File pomFile )
936 String expectedContent = normalizeString( org.apache.commons.io.FileUtils.readFileToString( expectedPomFile, null ) );
937 String targetContent = normalizeString( org.apache.commons.io.FileUtils.readFileToString( pomFile, null ) );
938 assertEquals( "Check file match between " + expectedPomFile + " and " + pomFile, expectedContent, targetContent );
941 private static String normalizeString( String path )
943 return path.trim().replaceAll( "\r\n", "\n" ).replace( '\r', '\n' ).replaceAll( "<\\?xml .+\\?>", "" );
946 private void checkSuccess( ArtifactConverter converter )
948 assertNotNull( "Warnings should never be null.", converter.getWarnings() );
949 assertEquals( "Should have no warnings.", 0, countWarningMessages( converter ) );
952 private void checkWarnings( ArtifactConverter converter, int count )
954 assertNotNull( "Warnings should never be null.", converter.getWarnings() );
955 assertEquals( "Should have some warnings.", count, countWarningMessages( converter ) );
958 private int countWarningMessages( ArtifactConverter converter )
961 for ( Iterator it = converter.getWarnings().values().iterator(); it.hasNext(); )
963 List values = (List) it.next();
964 count += values.size();
969 private void assertHasWarningReason( ArtifactConverter converter, String reason )
971 assertNotNull( "Warnings should never be null.", converter.getWarnings() );
972 assertTrue( "Expecting 1 or more Warnings", countWarningMessages( converter ) > 0 );
974 for ( Iterator it = converter.getWarnings().values().iterator(); it.hasNext(); )
976 List messages = (List) it.next();
977 if ( messages.contains( reason ) )
979 /* No need to check any furthor */
984 /* didn't find it. */
986 for ( Iterator it = converter.getWarnings().entrySet().iterator(); it.hasNext(); )
988 Map.Entry entry = (Entry) it.next();
989 Artifact artifact = (Artifact) entry.getKey();
990 System.out.println( "-Artifact: " + artifact.getGroupId() + ":" + artifact.getArtifactId() + ":"
991 + artifact.getVersion() );
992 List messages = (List) entry.getValue();
993 for ( Iterator itmsgs = messages.iterator(); itmsgs.hasNext(); )
995 String message = (String) itmsgs.next();
996 System.out.println( " " + message );
999 fail( "Unable to find message <" + reason + "> in warnings." );
1002 private void createModernSourceRepository()
1005 ArtifactRepositoryFactory factory = (ArtifactRepositoryFactory) lookup( ArtifactRepositoryFactory.ROLE );
1007 ArtifactRepositoryLayout layout = (ArtifactRepositoryLayout) lookup( ArtifactRepositoryLayout.ROLE, "default" );
1009 File sourceBase = getTestFile( "src/test/source-modern-repository" );
1010 sourceRepository = factory.createArtifactRepository( "source", sourceBase.toURL().toString(), layout, null,