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
23 import java.io.IOException;
24 import java.text.SimpleDateFormat;
25 import java.util.ArrayList;
26 import java.util.List;
27 import java.util.Locale;
29 import java.util.regex.Matcher;
31 import org.apache.commons.io.FileUtils;
32 import org.apache.maven.artifact.Artifact;
33 import org.apache.maven.artifact.factory.ArtifactFactory;
34 import org.apache.maven.artifact.metadata.ArtifactMetadata;
35 import org.apache.maven.artifact.repository.ArtifactRepository;
36 import org.apache.maven.artifact.repository.ArtifactRepositoryFactory;
37 import org.apache.maven.artifact.repository.layout.ArtifactRepositoryLayout;
38 import org.apache.maven.artifact.repository.metadata.ArtifactRepositoryMetadata;
39 import org.apache.maven.artifact.repository.metadata.SnapshotArtifactRepositoryMetadata;
40 import org.codehaus.plexus.spring.PlexusInSpringTestCase;
43 * LegacyToDefaultConverterTest
47 public class LegacyToDefaultConverterTest
48 extends PlexusInSpringTestCase
50 private ArtifactRepository sourceRepository;
52 private ArtifactRepository targetRepository;
54 private ArtifactConverter artifactConverter;
56 private ArtifactFactory artifactFactory;
58 private static final int SLEEP_MILLIS = 100;
60 protected void setUp()
65 ArtifactRepositoryFactory factory = (ArtifactRepositoryFactory) lookup( ArtifactRepositoryFactory.ROLE );
67 ArtifactRepositoryLayout layout = (ArtifactRepositoryLayout) lookup( ArtifactRepositoryLayout.ROLE, "legacy" );
69 File sourceBase = getTestFile( "src/test/source-repository" );
70 sourceRepository = factory.createArtifactRepository( "source", sourceBase.toURL().toString(), layout, null,
73 layout = (ArtifactRepositoryLayout) lookup( ArtifactRepositoryLayout.ROLE, "default" );
75 File targetBase = getTestFile( "target/test-target-repository" );
76 copyDirectoryStructure( getTestFile( "src/test/target-repository" ), targetBase );
78 targetRepository = factory.createArtifactRepository( "target", targetBase.toURL().toString(), layout, null,
81 artifactConverter = (ArtifactConverter) lookup( ArtifactConverter.ROLE, "legacy-to-default" );
83 artifactFactory = (ArtifactFactory) lookup( ArtifactFactory.ROLE );
86 protected void tearDown()
92 private void copyDirectoryStructure( File sourceDirectory, File destinationDirectory )
95 if ( !sourceDirectory.exists() )
97 throw new IOException( "Source directory doesn't exists (" + sourceDirectory.getAbsolutePath() + ")." );
100 File[] files = sourceDirectory.listFiles();
102 String sourcePath = sourceDirectory.getAbsolutePath();
104 for ( int i = 0; i < files.length; i++ )
106 File file = files[i];
108 String dest = file.getAbsolutePath();
110 dest = dest.substring( sourcePath.length() + 1 );
112 File destination = new File( destinationDirectory, dest );
116 destination = destination.getParentFile();
118 FileUtils.copyFileToDirectory( file, destination );
120 else if ( file.isDirectory() )
122 if ( !".svn".equals( file.getName() ) )
124 if ( !destination.exists() && !destination.mkdirs() )
126 throw new IOException( "Could not create destination directory '"
127 + destination.getAbsolutePath() + "'." );
129 copyDirectoryStructure( file, destination );
134 throw new IOException( "Unknown file type: " + file.getAbsolutePath() );
139 public void testV4PomConvert()
142 // test that it is copied as is
144 Artifact artifact = createArtifact( "test", "v4artifact", "1.0.0" );
145 ArtifactMetadata artifactMetadata = new ArtifactRepositoryMetadata( artifact );
146 File artifactMetadataFile = new File( targetRepository.getBasedir(), targetRepository
147 .pathOfRemoteRepositoryMetadata( artifactMetadata ) );
148 artifactMetadataFile.delete();
150 ArtifactMetadata versionMetadata = new SnapshotArtifactRepositoryMetadata( artifact );
151 File versionMetadataFile = new File( targetRepository.getBasedir(), targetRepository
152 .pathOfRemoteRepositoryMetadata( versionMetadata ) );
153 versionMetadataFile.delete();
155 File artifactFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
156 artifactFile.delete();
158 artifactConverter.convert( artifact, targetRepository );
159 checkSuccess( artifactConverter );
161 assertTrue( "Check artifact created", artifactFile.exists() );
162 assertTrue( "Check artifact matches", FileUtils.contentEquals( artifactFile, artifact.getFile() ) );
164 artifact = createPomArtifact( artifact );
165 File pomFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
166 File sourcePomFile = new File( sourceRepository.getBasedir(), sourceRepository.pathOf( artifact ) );
167 assertTrue( "Check POM created", pomFile.exists() );
169 compareFiles( sourcePomFile, pomFile );
171 assertTrue( "Check artifact metadata created", artifactMetadataFile.exists() );
173 File expectedMetadataFile = getTestFile( "src/test/expected-files/v4-artifact-metadata.xml" );
175 compareFiles( expectedMetadataFile, artifactMetadataFile );
177 assertTrue( "Check snapshot metadata created", versionMetadataFile.exists() );
179 expectedMetadataFile = getTestFile( "src/test/expected-files/v4-version-metadata.xml" );
181 compareFiles( expectedMetadataFile, versionMetadataFile );
184 public void testV3PomConvert()
187 // test that the pom is coverted
189 Artifact artifact = createArtifact( "test", "v3artifact", "1.0.0" );
190 ArtifactMetadata artifactMetadata = new ArtifactRepositoryMetadata( artifact );
191 File artifactMetadataFile = new File( targetRepository.getBasedir(), targetRepository
192 .pathOfRemoteRepositoryMetadata( artifactMetadata ) );
193 artifactMetadataFile.delete();
195 ArtifactMetadata versionMetadata = new SnapshotArtifactRepositoryMetadata( artifact );
196 File versionMetadataFile = new File( targetRepository.getBasedir(), targetRepository
197 .pathOfRemoteRepositoryMetadata( versionMetadata ) );
198 versionMetadataFile.delete();
200 artifactConverter.convert( artifact, targetRepository );
201 checkSuccess( artifactConverter );
203 File artifactFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
204 assertTrue( "Check artifact created", artifactFile.exists() );
205 assertTrue( "Check artifact matches", FileUtils.contentEquals( artifactFile, artifact.getFile() ) );
207 artifact = createPomArtifact( artifact );
208 File pomFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
209 File expectedPomFile = getTestFile( "src/test/expected-files/converted-v3.pom" );
210 assertTrue( "Check POM created", pomFile.exists() );
212 compareFiles( expectedPomFile, pomFile );
214 assertTrue( "Check artifact metadata created", artifactMetadataFile.exists() );
216 File expectedMetadataFile = getTestFile( "src/test/expected-files/v3-artifact-metadata.xml" );
218 compareFiles( expectedMetadataFile, artifactMetadataFile );
220 assertTrue( "Check snapshot metadata created", versionMetadataFile.exists() );
222 expectedMetadataFile = getTestFile( "src/test/expected-files/v3-version-metadata.xml" );
224 compareFiles( expectedMetadataFile, versionMetadataFile );
227 public void testV3PomConvertWithRelocation()
230 Artifact artifact = createArtifact( "test", "relocated-v3artifact", "1.0.0" );
231 ArtifactMetadata artifactMetadata = new ArtifactRepositoryMetadata( artifact );
232 File artifactMetadataFile = new File( targetRepository.getBasedir(), targetRepository
233 .pathOfRemoteRepositoryMetadata( artifactMetadata ) );
234 artifactMetadataFile.delete();
236 ArtifactMetadata versionMetadata = new SnapshotArtifactRepositoryMetadata( artifact );
237 File versionMetadataFile = new File( targetRepository.getBasedir(), targetRepository
238 .pathOfRemoteRepositoryMetadata( versionMetadata ) );
239 versionMetadataFile.delete();
241 artifactConverter.convert( artifact, targetRepository );
242 //checkSuccess(); --> commented until MNG-2100 is fixed
244 File artifactFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
245 assertTrue( "Check if relocated artifact created", artifactFile.exists() );
246 assertTrue( "Check if relocated artifact matches", FileUtils.contentEquals( artifactFile, artifact.getFile() ) );
247 Artifact pomArtifact = createArtifact( "relocated-test", "relocated-v3artifact", "1.0.0", "1.0.0", "pom" );
248 File pomFile = getTestFile( "src/test/expected-files/" + targetRepository.pathOf( pomArtifact ) );
249 File testFile = getTestFile( "target/test-target-repository/" + targetRepository.pathOf( pomArtifact ) );
250 compareFiles( pomFile, testFile );
252 Artifact orig = createArtifact( "test", "relocated-v3artifact", "1.0.0", "1.0.0", "pom" );
253 artifactFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( orig ) );
254 assertTrue( "Check if relocation artifact pom is created", artifactFile.exists() );
255 testFile = getTestFile( "src/test/expected-files/" + targetRepository.pathOf( orig ) );
256 compareFiles( artifactFile, testFile );
259 public void testV3PomWarningsOnConvert()
262 // test that the pom is converted but that warnings are reported
264 Artifact artifact = createArtifact( "test", "v3-warnings-artifact", "1.0.0" );
265 ArtifactMetadata artifactMetadata = new ArtifactRepositoryMetadata( artifact );
266 File artifactMetadataFile = new File( targetRepository.getBasedir(), targetRepository
267 .pathOfRemoteRepositoryMetadata( artifactMetadata ) );
268 artifactMetadataFile.delete();
270 ArtifactMetadata versionMetadata = new SnapshotArtifactRepositoryMetadata( artifact );
271 File versionMetadataFile = new File( targetRepository.getBasedir(), targetRepository
272 .pathOfRemoteRepositoryMetadata( versionMetadata ) );
273 versionMetadataFile.delete();
275 artifactConverter.convert( artifact, targetRepository );
276 checkWarnings( artifactConverter, 2 );
278 File artifactFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
279 assertTrue( "Check artifact created", artifactFile.exists() );
280 assertTrue( "Check artifact matches", FileUtils.contentEquals( artifactFile, artifact.getFile() ) );
282 artifact = createPomArtifact( artifact );
283 File pomFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
284 File expectedPomFile = getTestFile( "src/test/expected-files/converted-v3-warnings.pom" );
285 assertTrue( "Check POM created", pomFile.exists() );
287 compareFiles( expectedPomFile, pomFile );
289 // TODO: check 2 warnings (extend and versions) matched on i18n key
292 private void doTestV4SnapshotPomConvert( String version, String expectedMetadataFileName )
295 // test that it is copied as is
297 Artifact artifact = createArtifact( "test", "v4artifact", version );
298 ArtifactMetadata artifactMetadata = new ArtifactRepositoryMetadata( artifact );
299 File artifactMetadataFile = new File( targetRepository.getBasedir(), targetRepository
300 .pathOfRemoteRepositoryMetadata( artifactMetadata ) );
301 artifactMetadataFile.delete();
303 ArtifactMetadata snapshotMetadata = new SnapshotArtifactRepositoryMetadata( artifact );
304 File snapshotMetadataFile = new File( targetRepository.getBasedir(), targetRepository
305 .pathOfRemoteRepositoryMetadata( snapshotMetadata ) );
306 snapshotMetadataFile.delete();
308 artifactConverter.convert( artifact, targetRepository );
309 checkSuccess( artifactConverter );
311 File artifactFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
312 assertTrue( "Check artifact created", artifactFile.exists() );
313 assertTrue( "Check artifact matches", FileUtils.contentEquals( artifactFile, artifact.getFile() ) );
315 artifact = createPomArtifact( artifact );
316 File pomFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
317 File sourcePomFile = new File( sourceRepository.getBasedir(), sourceRepository.pathOf( artifact ) );
318 assertTrue( "Check POM created", pomFile.exists() );
320 compareFiles( sourcePomFile, pomFile );
322 assertTrue( "Check artifact metadata created", artifactMetadataFile.exists() );
324 File expectedMetadataFile = getTestFile( "src/test/expected-files/v4-snapshot-artifact-metadata.xml" );
326 compareFiles( expectedMetadataFile, artifactMetadataFile );
328 assertTrue( "Check snapshot metadata created", snapshotMetadataFile.exists() );
330 expectedMetadataFile = getTestFile( expectedMetadataFileName );
332 compareFiles( expectedMetadataFile, snapshotMetadataFile );
335 public void testV3SnapshotPomConvert()
338 // test that the pom is coverted
340 Artifact artifact = createArtifact( "test", "v3artifact", "1.0.0-SNAPSHOT" );
341 ArtifactMetadata artifactMetadata = new ArtifactRepositoryMetadata( artifact );
342 File artifactMetadataFile = new File( targetRepository.getBasedir(), targetRepository
343 .pathOfRemoteRepositoryMetadata( artifactMetadata ) );
344 artifactMetadataFile.delete();
346 ArtifactMetadata snapshotMetadata = new SnapshotArtifactRepositoryMetadata( artifact );
347 File snapshotMetadataFile = new File( targetRepository.getBasedir(), targetRepository
348 .pathOfRemoteRepositoryMetadata( snapshotMetadata ) );
349 snapshotMetadataFile.delete();
351 artifactConverter.convert( artifact, targetRepository );
352 checkSuccess( artifactConverter );
354 File artifactFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
355 assertTrue( "Check artifact created", artifactFile.exists() );
356 assertTrue( "Check artifact matches", FileUtils.contentEquals( artifactFile, artifact.getFile() ) );
358 artifact = createPomArtifact( artifact );
359 File pomFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
360 File expectedPomFile = getTestFile( "src/test/expected-files/converted-v3-snapshot.pom" );
361 assertTrue( "Check POM created", pomFile.exists() );
363 compareFiles( expectedPomFile, pomFile );
365 assertTrue( "Check artifact metadata created", artifactMetadataFile.exists() );
367 File expectedMetadataFile = getTestFile( "src/test/expected-files/v3-snapshot-artifact-metadata.xml" );
369 compareFiles( expectedMetadataFile, artifactMetadataFile );
371 assertTrue( "Check snapshot metadata created", snapshotMetadataFile.exists() );
373 expectedMetadataFile = getTestFile( "src/test/expected-files/v3-snapshot-metadata.xml" );
375 compareFiles( expectedMetadataFile, snapshotMetadataFile );
378 public void testV4SnapshotPomConvert()
381 doTestV4SnapshotPomConvert( "1.0.0-SNAPSHOT", "src/test/expected-files/v4-snapshot-metadata.xml" );
386 public void testV4TimestampedSnapshotPomConvert()
389 doTestV4SnapshotPomConvert( "1.0.0-20060111.120115-1",
390 "src/test/expected-files/v4-timestamped-snapshot-metadata.xml" );
395 public void testMavenOnePluginConversion()
398 Artifact artifact = createArtifact( "org.apache.maven.plugins", "maven-foo-plugin", "1.0", "1.0",
400 artifact.setFile( new File( getBasedir(), "src/test/source-repository/test/plugins/maven-foo-plugin-1.0.jar" ) );
401 artifactConverter.convert( artifact, targetRepository );
402 // There is a warning but I can't figure out how to look at it. Eyeballing the results it appears
403 // the plugin is being coverted correctly.
406 File artifactFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
407 assertTrue( "Check artifact created", artifactFile.exists() );
408 assertTrue( "Check artifact matches", FileUtils.contentEquals( artifactFile, artifact.getFile() ) );
411 The POM isn't needed for Maven 1.x plugins but the raw conversion for
413 artifact = createPomArtifact( artifact );
414 File pomFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
415 File expectedPomFile = getTestFile( "src/test/expected-files/maven-foo-plugin-1.0.pom" );
416 assertTrue( "Check POM created", pomFile.exists() );
417 compareFiles( expectedPomFile, pomFile );
421 public void testV3TimestampedSnapshotPomConvert()
424 // test that the pom is coverted
426 Artifact artifact = createArtifact( "test", "v3artifact", "1.0.0-20060105.130101-3" );
427 ArtifactMetadata artifactMetadata = new ArtifactRepositoryMetadata( artifact );
428 File artifactMetadataFile = new File( targetRepository.getBasedir(), targetRepository
429 .pathOfRemoteRepositoryMetadata( artifactMetadata ) );
430 artifactMetadataFile.delete();
432 ArtifactMetadata snapshotMetadata = new SnapshotArtifactRepositoryMetadata( artifact );
433 File snapshotMetadataFile = new File( targetRepository.getBasedir(), targetRepository
434 .pathOfRemoteRepositoryMetadata( snapshotMetadata ) );
435 snapshotMetadataFile.delete();
437 artifactConverter.convert( artifact, targetRepository );
438 checkSuccess( artifactConverter );
440 File artifactFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
441 assertTrue( "Check artifact created", artifactFile.exists() );
442 assertTrue( "Check artifact matches", FileUtils.contentEquals( artifactFile, artifact.getFile() ) );
444 artifact = createPomArtifact( artifact );
445 File pomFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
446 File expectedPomFile = getTestFile( "src/test/expected-files/converted-v3-timestamped-snapshot.pom" );
447 assertTrue( "Check POM created", pomFile.exists() );
449 compareFiles( expectedPomFile, pomFile );
451 assertTrue( "Check artifact snapshotMetadata created", artifactMetadataFile.exists() );
453 File expectedMetadataFile = getTestFile( "src/test/expected-files/v3-snapshot-artifact-metadata.xml" );
455 compareFiles( expectedMetadataFile, artifactMetadataFile );
457 assertTrue( "Check snapshot snapshotMetadata created", snapshotMetadataFile.exists() );
459 expectedMetadataFile = getTestFile( "src/test/expected-files/v3-timestamped-snapshot-metadata.xml" );
461 compareFiles( expectedMetadataFile, snapshotMetadataFile );
464 public void testNoPomConvert()
467 // test that a POM is not created when there was none at the source
469 Artifact artifact = createArtifact( "test", "noPomArtifact", "1.0.0" );
470 artifactConverter.convert( artifact, targetRepository );
471 checkWarnings( artifactConverter, 1 );
473 assertHasWarningReason( artifactConverter, Messages.getString( "warning.missing.pom" ) );
475 File artifactFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
476 assertTrue( "Check artifact created", artifactFile.exists() );
477 assertTrue( "Check artifact matches", FileUtils.contentEquals( artifactFile, artifact.getFile() ) );
479 artifact = createPomArtifact( artifact );
480 File pomFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
481 File sourcePomFile = new File( sourceRepository.getBasedir(), sourceRepository.pathOf( artifact ) );
483 assertFalse( "Check no POM created", pomFile.exists() );
484 assertFalse( "No source POM", sourcePomFile.exists() );
487 public void testIncorrectSourceChecksumMd5()
490 // test that it fails when the source md5 is wrong
492 Artifact artifact = createArtifact( "test", "incorrectMd5Artifact", "1.0.0" );
493 File file = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
496 artifactConverter.convert( artifact, targetRepository );
497 checkWarnings( artifactConverter, 2 );
499 assertHasWarningReason( artifactConverter, Messages.getString( "failure.incorrect.md5" ) );
501 assertFalse( "Check artifact not created", file.exists() );
503 ArtifactRepositoryMetadata metadata = new ArtifactRepositoryMetadata( artifact );
504 File metadataFile = new File( targetRepository.getBasedir(), targetRepository
505 .pathOfRemoteRepositoryMetadata( metadata ) );
506 assertFalse( "Check metadata not created", metadataFile.exists() );
509 public void testIncorrectSourceChecksumSha1()
512 // test that it fails when the source sha1 is wrong
514 Artifact artifact = createArtifact( "test", "incorrectSha1Artifact", "1.0.0" );
515 File file = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
518 artifactConverter.convert( artifact, targetRepository );
519 checkWarnings( artifactConverter, 2 );
521 assertHasWarningReason( artifactConverter, Messages.getString( "failure.incorrect.sha1" ) );
523 assertFalse( "Check artifact not created", file.exists() );
525 ArtifactRepositoryMetadata metadata = new ArtifactRepositoryMetadata( artifact );
526 File metadataFile = new File( targetRepository.getBasedir(), targetRepository
527 .pathOfRemoteRepositoryMetadata( metadata ) );
528 assertFalse( "Check metadata not created", metadataFile.exists() );
531 public void testUnmodifiedArtifact()
532 throws Exception, InterruptedException
534 // test the unmodified artifact is untouched
536 Artifact artifact = createArtifact( "test", "unmodified-artifact", "1.0.0" );
537 Artifact pomArtifact = createPomArtifact( artifact );
539 File sourceFile = new File( sourceRepository.getBasedir(), sourceRepository.pathOf( artifact ) );
540 File sourcePomFile = new File( sourceRepository.getBasedir(), sourceRepository.pathOf( pomArtifact ) );
541 File targetFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
542 File targetPomFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( pomArtifact ) );
544 assertTrue( "Check target file exists", targetFile.exists() );
545 assertTrue( "Check target POM exists", targetPomFile.exists() );
547 sourceFile.setLastModified( System.currentTimeMillis() );
548 sourcePomFile.setLastModified( System.currentTimeMillis() );
550 long origTime = targetFile.lastModified();
551 long origPomTime = targetPomFile.lastModified();
553 // Need to guarantee last modified is not equal
554 Thread.sleep( SLEEP_MILLIS );
556 artifactConverter.convert( artifact, targetRepository );
557 checkSuccess( artifactConverter );
559 compareFiles( sourceFile, targetFile );
560 compareFiles( sourcePomFile, targetPomFile );
562 assertEquals( "Check artifact unmodified", origTime, targetFile.lastModified() );
563 assertEquals( "Check POM unmodified", origPomTime, targetPomFile.lastModified() );
566 public void testModifedArtifactFails()
569 // test that it fails when the source artifact has changed and is different to the existing artifact in the
572 Artifact artifact = createArtifact( "test", "modified-artifact", "1.0.0" );
573 Artifact pomArtifact = createPomArtifact( artifact );
575 File sourceFile = new File( sourceRepository.getBasedir(), sourceRepository.pathOf( artifact ) );
576 File sourcePomFile = new File( sourceRepository.getBasedir(), sourceRepository.pathOf( pomArtifact ) );
577 File targetFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
578 File targetPomFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( pomArtifact ) );
580 assertTrue( "Check target file exists", targetFile.exists() );
581 assertTrue( "Check target POM exists", targetPomFile.exists() );
583 sourceFile.setLastModified( System.currentTimeMillis() );
584 sourcePomFile.setLastModified( System.currentTimeMillis() );
586 long origTime = targetFile.lastModified();
587 long origPomTime = targetPomFile.lastModified();
589 // Need to guarantee last modified is not equal
590 Thread.sleep( SLEEP_MILLIS );
592 artifactConverter.convert( artifact, targetRepository );
593 checkWarnings( artifactConverter, 2 );
595 assertHasWarningReason( artifactConverter, Messages.getString( "failure.target.already.exists" ) );
597 assertEquals( "Check unmodified", origTime, targetFile.lastModified() );
598 assertEquals( "Check unmodified", origPomTime, targetPomFile.lastModified() );
600 ArtifactRepositoryMetadata metadata = new ArtifactRepositoryMetadata( artifact );
601 File metadataFile = new File( targetRepository.getBasedir(), targetRepository
602 .pathOfRemoteRepositoryMetadata( metadata ) );
603 assertFalse( "Check metadata not created", metadataFile.exists() );
606 public void testForcedUnmodifiedArtifact()
609 // test unmodified artifact is still converted when set to force
611 artifactConverter = (ArtifactConverter) lookup( ArtifactConverter.ROLE, "force-repository-converter" );
613 Artifact artifact = createArtifact( "test", "unmodified-artifact", "1.0.0" );
614 Artifact pomArtifact = createPomArtifact( artifact );
616 File sourceFile = new File( sourceRepository.getBasedir(), sourceRepository.pathOf( artifact ) );
617 File sourcePomFile = new File( sourceRepository.getBasedir(), sourceRepository.pathOf( pomArtifact ) );
618 File targetFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
619 File targetPomFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( pomArtifact ) );
621 SimpleDateFormat dateFormat = new SimpleDateFormat( "yyyy-MM-dd", Locale.getDefault() );
622 long origTime = dateFormat.parse( "2006-03-03" ).getTime();
623 targetFile.setLastModified( origTime );
624 targetPomFile.setLastModified( origTime );
626 sourceFile.setLastModified( dateFormat.parse( "2006-01-01" ).getTime() );
627 sourcePomFile.setLastModified( dateFormat.parse( "2006-02-02" ).getTime() );
629 artifactConverter.convert( artifact, targetRepository );
630 checkSuccess( artifactConverter );
632 compareFiles( sourceFile, targetFile );
633 compareFiles( sourcePomFile, targetPomFile );
635 assertFalse( "Check modified", origTime == targetFile.lastModified() );
636 assertFalse( "Check modified", origTime == targetPomFile.lastModified() );
638 ArtifactRepositoryMetadata metadata = new ArtifactRepositoryMetadata( artifact );
639 File metadataFile = new File( targetRepository.getBasedir(), targetRepository
640 .pathOfRemoteRepositoryMetadata( metadata ) );
641 assertTrue( "Check metadata created", metadataFile.exists() );
644 public void testDryRunSuccess()
647 // test dry run does nothing on a run that will be successful, and returns success
649 artifactConverter = (ArtifactConverter) lookup( ArtifactConverter.ROLE, "dryrun-repository-converter" );
651 Artifact artifact = createArtifact( "test", "dryrun-artifact", "1.0.0" );
652 Artifact pomArtifact = createPomArtifact( artifact );
654 File sourceFile = new File( sourceRepository.getBasedir(), sourceRepository.pathOf( artifact ) );
655 File sourcePomFile = new File( sourceRepository.getBasedir(), sourceRepository.pathOf( pomArtifact ) );
656 File targetFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
657 File targetPomFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( pomArtifact ) );
659 artifactConverter.convert( artifact, targetRepository );
660 checkSuccess( artifactConverter );
662 assertTrue( "Check source file exists", sourceFile.exists() );
663 assertTrue( "Check source POM exists", sourcePomFile.exists() );
665 assertFalse( "Check target file doesn't exist", targetFile.exists() );
666 assertFalse( "Check target POM doesn't exist", targetPomFile.exists() );
668 ArtifactRepositoryMetadata metadata = new ArtifactRepositoryMetadata( artifact );
669 File metadataFile = new File( targetRepository.getBasedir(), targetRepository
670 .pathOfRemoteRepositoryMetadata( metadata ) );
671 assertFalse( "Check metadata not created", metadataFile.exists() );
674 public void testDryRunFailure()
677 // test dry run does nothing on a run that will fail, and returns failure
679 artifactConverter = (ArtifactConverter) lookup( ArtifactConverter.ROLE, "dryrun-repository-converter" );
681 Artifact artifact = createArtifact( "test", "modified-artifact", "1.0.0" );
682 Artifact pomArtifact = createPomArtifact( artifact );
684 File sourceFile = new File( sourceRepository.getBasedir(), sourceRepository.pathOf( artifact ) );
685 File sourcePomFile = new File( sourceRepository.getBasedir(), sourceRepository.pathOf( pomArtifact ) );
686 File targetFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
687 File targetPomFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( pomArtifact ) );
689 assertTrue( "Check target file exists", targetFile.exists() );
690 assertTrue( "Check target POM exists", targetPomFile.exists() );
692 sourceFile.setLastModified( System.currentTimeMillis() );
693 sourcePomFile.setLastModified( System.currentTimeMillis() );
695 long origTime = targetFile.lastModified();
696 long origPomTime = targetPomFile.lastModified();
698 // Need to guarantee last modified is not equal
699 Thread.sleep( SLEEP_MILLIS );
701 artifactConverter.convert( artifact, targetRepository );
702 checkWarnings( artifactConverter, 2 );
704 assertHasWarningReason( artifactConverter, Messages.getString( "failure.target.already.exists" ) );
706 assertEquals( "Check unmodified", origTime, targetFile.lastModified() );
707 assertEquals( "Check unmodified", origPomTime, targetPomFile.lastModified() );
709 ArtifactRepositoryMetadata metadata = new ArtifactRepositoryMetadata( artifact );
710 File metadataFile = new File( targetRepository.getBasedir(), targetRepository
711 .pathOfRemoteRepositoryMetadata( metadata ) );
712 assertFalse( "Check metadata not created", metadataFile.exists() );
715 public void testRollbackArtifactCreated()
718 // test rollback can remove a created artifact, including checksums
720 Artifact artifact = createArtifact( "test", "rollback-created-artifact", "1.0.0" );
721 ArtifactMetadata artifactMetadata = new ArtifactRepositoryMetadata( artifact );
722 File artifactMetadataFile = new File( targetRepository.getBasedir(), targetRepository
723 .pathOfRemoteRepositoryMetadata( artifactMetadata ) );
724 FileUtils.deleteDirectory( artifactMetadataFile.getParentFile() );
726 ArtifactMetadata versionMetadata = new SnapshotArtifactRepositoryMetadata( artifact );
727 File versionMetadataFile = new File( targetRepository.getBasedir(), targetRepository
728 .pathOfRemoteRepositoryMetadata( versionMetadata ) );
730 File artifactFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
732 artifactConverter.convert( artifact, targetRepository );
733 checkWarnings( artifactConverter, 2 );
735 boolean found = false;
736 String pattern = "^" + Messages.getString( "invalid.source.pom" ).replaceFirst( "\\{0\\}", ".*" ) + "$";
737 for ( List<String> messages : artifactConverter.getWarnings().values() )
739 for ( String message : messages )
741 if ( message.matches( pattern ) )
754 assertTrue( "Check failure message.", found );
756 assertFalse( "check artifact rolled back", artifactFile.exists() );
757 assertFalse( "check metadata rolled back", artifactMetadataFile.exists() );
758 assertFalse( "check metadata rolled back", versionMetadataFile.exists() );
761 public void testMultipleArtifacts()
764 // test multiple artifacts are converted
766 List<Artifact> artifacts = new ArrayList<Artifact>();
767 artifacts.add( createArtifact( "test", "artifact-one", "1.0.0" ) );
768 artifacts.add( createArtifact( "test", "artifact-two", "1.0.0" ) );
769 artifacts.add( createArtifact( "test", "artifact-three", "1.0.0" ) );
771 for ( Artifact artifact : artifacts )
773 artifactConverter.convert( artifact, targetRepository );
774 checkSuccess( artifactConverter );
777 for ( Artifact artifact : artifacts )
779 File artifactFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
780 assertTrue( "Check artifact created", artifactFile.exists() );
781 assertTrue( "Check artifact matches", FileUtils.contentEquals( artifactFile, artifact.getFile() ) );
783 artifact = createPomArtifact( artifact );
784 File pomFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
785 File expectedPomFile = getTestFile( "src/test/expected-files/converted-" + artifact.getArtifactId()
787 assertTrue( "Check POM created", pomFile.exists() );
789 compareFiles( expectedPomFile, pomFile );
793 public void testInvalidSourceArtifactMetadata()
796 // test artifact is not converted when source metadata is invalid, and returns failure
798 createModernSourceRepository();
800 Artifact artifact = createArtifact( "test", "incorrectArtifactMetadata", "1.0.0" );
801 File file = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
804 artifactConverter.convert( artifact, targetRepository );
805 checkWarnings( artifactConverter, 2 );
807 assertHasWarningReason( artifactConverter, Messages.getString( "failure.incorrect.artifactMetadata.versions" ) );
809 assertFalse( "Check artifact not created", file.exists() );
811 ArtifactRepositoryMetadata metadata = new ArtifactRepositoryMetadata( artifact );
812 File metadataFile = new File( targetRepository.getBasedir(), targetRepository
813 .pathOfRemoteRepositoryMetadata( metadata ) );
814 assertFalse( "Check metadata not created", metadataFile.exists() );
817 public void testInvalidSourceSnapshotMetadata()
820 // test artifact is not converted when source snapshot metadata is invalid and returns failure
822 createModernSourceRepository();
824 Artifact artifact = createArtifact( "test", "incorrectSnapshotMetadata", "1.0.0-20060102.030405-6" );
825 File file = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
828 artifactConverter.convert( artifact, targetRepository );
829 checkWarnings( artifactConverter, 2 );
831 assertHasWarningReason( artifactConverter, Messages.getString( "failure.incorrect.snapshotMetadata.snapshot" ) );
833 assertFalse( "Check artifact not created", file.exists() );
835 ArtifactRepositoryMetadata metadata = new ArtifactRepositoryMetadata( artifact );
836 File metadataFile = new File( targetRepository.getBasedir(), targetRepository
837 .pathOfRemoteRepositoryMetadata( metadata ) );
838 assertFalse( "Check metadata not created", metadataFile.exists() );
841 public void testMergeArtifactMetadata()
844 // test artifact level metadata is merged when it already exists on successful conversion
846 Artifact artifact = createArtifact( "test", "newversion-artifact", "1.0.1" );
847 artifactConverter.convert( artifact, targetRepository );
848 checkSuccess( artifactConverter );
850 File artifactFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
851 assertTrue( "Check artifact created", artifactFile.exists() );
852 assertTrue( "Check artifact matches", FileUtils.contentEquals( artifactFile, artifact.getFile() ) );
854 artifact = createPomArtifact( artifact );
855 File pomFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
856 File sourcePomFile = new File( sourceRepository.getBasedir(), sourceRepository.pathOf( artifact ) );
857 assertTrue( "Check POM created", pomFile.exists() );
859 compareFiles( sourcePomFile, pomFile );
861 ArtifactMetadata artifactMetadata = new ArtifactRepositoryMetadata( artifact );
862 File artifactMetadataFile = new File( targetRepository.getBasedir(), targetRepository
863 .pathOfRemoteRepositoryMetadata( artifactMetadata ) );
864 assertTrue( "Check artifact metadata created", artifactMetadataFile.exists() );
866 File expectedMetadataFile = getTestFile( "src/test/expected-files/newversion-artifact-metadata.xml" );
868 compareFiles( expectedMetadataFile, artifactMetadataFile );
871 public void testSourceAndTargetRepositoriesMatch()
874 // test that it fails if the same
876 ArtifactRepositoryFactory factory = (ArtifactRepositoryFactory) lookup( ArtifactRepositoryFactory.ROLE );
878 sourceRepository = factory.createArtifactRepository( "source", targetRepository.getUrl(), targetRepository
879 .getLayout(), null, null );
881 Artifact artifact = createArtifact( "test", "repository-artifact", "1.0" );
885 artifactConverter.convert( artifact, targetRepository );
886 fail( "Should have failed trying to convert within the same repository" );
888 catch ( ArtifactConversionException e )
891 assertEquals( "check message", Messages.getString( "exception.repositories.match" ), e.getMessage() );
892 assertNull( "Check no additional cause", e.getCause() );
896 private Artifact createArtifact( String groupId, String artifactId, String version )
898 Matcher matcher = Artifact.VERSION_FILE_PATTERN.matcher( version );
900 if ( matcher.matches() )
902 baseVersion = matcher.group( 1 ) + "-SNAPSHOT";
906 baseVersion = version;
908 return createArtifact( groupId, artifactId, baseVersion, version, "jar" );
911 private Artifact createArtifact( String groupId, String artifactId, String baseVersion, String version, String type )
913 Artifact artifact = artifactFactory.createArtifact( groupId, artifactId, version, null, type );
914 artifact.setBaseVersion( baseVersion );
915 artifact.setRepository( sourceRepository );
916 artifact.setFile( new File( sourceRepository.getBasedir(), sourceRepository.pathOf( artifact ) ) );
920 private Artifact createPomArtifact( Artifact artifact )
922 return createArtifact( artifact.getGroupId(), artifact.getArtifactId(), artifact.getBaseVersion(), artifact
923 .getVersion(), "pom" );
926 private static void compareFiles( File expectedPomFile, File pomFile )
929 String expectedContent = normalizeString( org.apache.commons.io.FileUtils.readFileToString( expectedPomFile, null ) );
930 String targetContent = normalizeString( org.apache.commons.io.FileUtils.readFileToString( pomFile, null ) );
931 assertEquals( "Check file match between " + expectedPomFile + " and " + pomFile, expectedContent, targetContent );
934 private static String normalizeString( String path )
936 return path.trim().replaceAll( "\r\n", "\n" ).replace( '\r', '\n' ).replaceAll( "<\\?xml .+\\?>", "" );
939 private void checkSuccess( ArtifactConverter converter )
941 assertNotNull( "Warnings should never be null.", converter.getWarnings() );
942 assertEquals( "Should have no warnings.", 0, countWarningMessages( converter ) );
945 private void checkWarnings( ArtifactConverter converter, int count )
947 assertNotNull( "Warnings should never be null.", converter.getWarnings() );
948 assertEquals( "Should have some warnings.", count, countWarningMessages( converter ) );
951 private int countWarningMessages( ArtifactConverter converter )
954 for ( List<String> values : converter.getWarnings().values() )
956 count += values.size();
961 private void assertHasWarningReason( ArtifactConverter converter, String reason )
963 assertNotNull( "Warnings should never be null.", converter.getWarnings() );
964 assertTrue( "Expecting 1 or more Warnings", countWarningMessages( converter ) > 0 );
966 for ( List<String> messages : converter.getWarnings().values() )
968 if ( messages.contains( reason ) )
970 /* No need to check any further */
975 /* didn't find it. */
977 for ( Map.Entry<Artifact,List<String>> entry : converter.getWarnings().entrySet() )
979 Artifact artifact = (Artifact) entry.getKey();
980 System.out.println( "-Artifact: " + artifact.getGroupId() + ":" + artifact.getArtifactId() + ":"
981 + artifact.getVersion() );
982 List<String> messages = entry.getValue();
983 for ( String message : messages )
985 System.out.println( " " + message );
988 fail( "Unable to find message <" + reason + "> in warnings." );
991 private void createModernSourceRepository()
994 ArtifactRepositoryFactory factory = (ArtifactRepositoryFactory) lookup( ArtifactRepositoryFactory.ROLE );
996 ArtifactRepositoryLayout layout = (ArtifactRepositoryLayout) lookup( ArtifactRepositoryLayout.ROLE, "default" );
998 File sourceBase = getTestFile( "src/test/source-modern-repository" );
999 sourceRepository = factory.createArtifactRepository( "source", sourceBase.toURL().toString(), layout, null,