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.commons.io.FileUtils;
23 import org.apache.maven.artifact.Artifact;
24 import org.apache.maven.artifact.factory.ArtifactFactory;
25 import org.apache.maven.artifact.metadata.ArtifactMetadata;
26 import org.apache.maven.artifact.repository.ArtifactRepository;
27 import org.apache.maven.artifact.repository.ArtifactRepositoryFactory;
28 import org.apache.maven.artifact.repository.layout.ArtifactRepositoryLayout;
29 import org.apache.maven.artifact.repository.metadata.ArtifactRepositoryMetadata;
30 import org.apache.maven.artifact.repository.metadata.SnapshotArtifactRepositoryMetadata;
31 import org.codehaus.plexus.spring.PlexusInSpringTestCase;
34 import java.io.IOException;
35 import java.text.SimpleDateFormat;
36 import java.util.ArrayList;
37 import java.util.Iterator;
38 import java.util.List;
39 import java.util.Locale;
41 import java.util.Map.Entry;
42 import java.util.regex.Matcher;
45 * LegacyToDefaultConverterTest
49 public class LegacyToDefaultConverterTest
50 extends PlexusInSpringTestCase
52 private ArtifactRepository sourceRepository;
54 private ArtifactRepository targetRepository;
56 private ArtifactConverter artifactConverter;
58 private ArtifactFactory artifactFactory;
60 private static final int SLEEP_MILLIS = 100;
62 protected void setUp()
67 ArtifactRepositoryFactory factory = (ArtifactRepositoryFactory) lookup( ArtifactRepositoryFactory.ROLE );
69 ArtifactRepositoryLayout layout = (ArtifactRepositoryLayout) lookup( ArtifactRepositoryLayout.ROLE, "legacy" );
71 File sourceBase = getTestFile( "src/test/source-repository" );
72 sourceRepository = factory.createArtifactRepository( "source", sourceBase.toURL().toString(), layout, null,
75 layout = (ArtifactRepositoryLayout) lookup( ArtifactRepositoryLayout.ROLE, "default" );
77 File targetBase = getTestFile( "target/test-target-repository" );
78 copyDirectoryStructure( getTestFile( "src/test/target-repository" ), targetBase );
80 targetRepository = factory.createArtifactRepository( "target", targetBase.toURL().toString(), layout, null,
83 artifactConverter = (ArtifactConverter) lookup( ArtifactConverter.ROLE, "legacy-to-default" );
85 artifactFactory = (ArtifactFactory) lookup( ArtifactFactory.ROLE );
88 protected void tearDown()
94 private void copyDirectoryStructure( File sourceDirectory, File destinationDirectory )
97 if ( !sourceDirectory.exists() )
99 throw new IOException( "Source directory doesn't exists (" + sourceDirectory.getAbsolutePath() + ")." );
102 File[] files = sourceDirectory.listFiles();
104 String sourcePath = sourceDirectory.getAbsolutePath();
106 for ( int i = 0; i < files.length; i++ )
108 File file = files[i];
110 String dest = file.getAbsolutePath();
112 dest = dest.substring( sourcePath.length() + 1 );
114 File destination = new File( destinationDirectory, dest );
118 destination = destination.getParentFile();
120 FileUtils.copyFileToDirectory( file, destination );
122 else if ( file.isDirectory() )
124 if ( !".svn".equals( file.getName() ) )
126 if ( !destination.exists() && !destination.mkdirs() )
128 throw new IOException( "Could not create destination directory '"
129 + destination.getAbsolutePath() + "'." );
131 copyDirectoryStructure( file, destination );
136 throw new IOException( "Unknown file type: " + file.getAbsolutePath() );
141 public void testV4PomConvert()
144 // test that it is copied as is
146 Artifact artifact = createArtifact( "test", "v4artifact", "1.0.0" );
147 ArtifactMetadata artifactMetadata = new ArtifactRepositoryMetadata( artifact );
148 File artifactMetadataFile = new File( targetRepository.getBasedir(), targetRepository
149 .pathOfRemoteRepositoryMetadata( artifactMetadata ) );
150 artifactMetadataFile.delete();
152 ArtifactMetadata versionMetadata = new SnapshotArtifactRepositoryMetadata( artifact );
153 File versionMetadataFile = new File( targetRepository.getBasedir(), targetRepository
154 .pathOfRemoteRepositoryMetadata( versionMetadata ) );
155 versionMetadataFile.delete();
157 File artifactFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
158 artifactFile.delete();
160 artifactConverter.convert( artifact, targetRepository );
161 checkSuccess( artifactConverter );
163 assertTrue( "Check artifact created", artifactFile.exists() );
164 assertTrue( "Check artifact matches", FileUtils.contentEquals( artifactFile, artifact.getFile() ) );
166 artifact = createPomArtifact( artifact );
167 File pomFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
168 File sourcePomFile = new File( sourceRepository.getBasedir(), sourceRepository.pathOf( artifact ) );
169 assertTrue( "Check POM created", pomFile.exists() );
171 compareFiles( sourcePomFile, pomFile );
173 assertTrue( "Check artifact metadata created", artifactMetadataFile.exists() );
175 File expectedMetadataFile = getTestFile( "src/test/expected-files/v4-artifact-metadata.xml" );
177 compareFiles( expectedMetadataFile, artifactMetadataFile );
179 assertTrue( "Check snapshot metadata created", versionMetadataFile.exists() );
181 expectedMetadataFile = getTestFile( "src/test/expected-files/v4-version-metadata.xml" );
183 compareFiles( expectedMetadataFile, versionMetadataFile );
186 public void testV3PomConvert()
189 // test that the pom is coverted
191 Artifact artifact = createArtifact( "test", "v3artifact", "1.0.0" );
192 ArtifactMetadata artifactMetadata = new ArtifactRepositoryMetadata( artifact );
193 File artifactMetadataFile = new File( targetRepository.getBasedir(), targetRepository
194 .pathOfRemoteRepositoryMetadata( artifactMetadata ) );
195 artifactMetadataFile.delete();
197 ArtifactMetadata versionMetadata = new SnapshotArtifactRepositoryMetadata( artifact );
198 File versionMetadataFile = new File( targetRepository.getBasedir(), targetRepository
199 .pathOfRemoteRepositoryMetadata( versionMetadata ) );
200 versionMetadataFile.delete();
202 artifactConverter.convert( artifact, targetRepository );
203 checkSuccess( artifactConverter );
205 File artifactFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
206 assertTrue( "Check artifact created", artifactFile.exists() );
207 assertTrue( "Check artifact matches", FileUtils.contentEquals( artifactFile, artifact.getFile() ) );
209 artifact = createPomArtifact( artifact );
210 File pomFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
211 File expectedPomFile = getTestFile( "src/test/expected-files/converted-v3.pom" );
212 assertTrue( "Check POM created", pomFile.exists() );
214 compareFiles( expectedPomFile, pomFile );
216 assertTrue( "Check artifact metadata created", artifactMetadataFile.exists() );
218 File expectedMetadataFile = getTestFile( "src/test/expected-files/v3-artifact-metadata.xml" );
220 compareFiles( expectedMetadataFile, artifactMetadataFile );
222 assertTrue( "Check snapshot metadata created", versionMetadataFile.exists() );
224 expectedMetadataFile = getTestFile( "src/test/expected-files/v3-version-metadata.xml" );
226 compareFiles( expectedMetadataFile, versionMetadataFile );
229 public void testV3PomConvertWithRelocation()
232 Artifact artifact = createArtifact( "test", "relocated-v3artifact", "1.0.0" );
233 ArtifactMetadata artifactMetadata = new ArtifactRepositoryMetadata( artifact );
234 File artifactMetadataFile = new File( targetRepository.getBasedir(), targetRepository
235 .pathOfRemoteRepositoryMetadata( artifactMetadata ) );
236 artifactMetadataFile.delete();
238 ArtifactMetadata versionMetadata = new SnapshotArtifactRepositoryMetadata( artifact );
239 File versionMetadataFile = new File( targetRepository.getBasedir(), targetRepository
240 .pathOfRemoteRepositoryMetadata( versionMetadata ) );
241 versionMetadataFile.delete();
243 artifactConverter.convert( artifact, targetRepository );
244 //checkSuccess(); --> commented until MNG-2100 is fixed
246 File artifactFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
247 assertTrue( "Check if relocated artifact created", artifactFile.exists() );
248 assertTrue( "Check if relocated artifact matches", FileUtils.contentEquals( artifactFile, artifact.getFile() ) );
249 Artifact pomArtifact = createArtifact( "relocated-test", "relocated-v3artifact", "1.0.0", "1.0.0", "pom" );
250 File pomFile = getTestFile( "src/test/expected-files/" + targetRepository.pathOf( pomArtifact ) );
251 File testFile = getTestFile( "target/test-target-repository/" + targetRepository.pathOf( pomArtifact ) );
252 compareFiles( pomFile, testFile );
254 Artifact orig = createArtifact( "test", "relocated-v3artifact", "1.0.0", "1.0.0", "pom" );
255 artifactFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( orig ) );
256 assertTrue( "Check if relocation artifact pom is created", artifactFile.exists() );
257 testFile = getTestFile( "src/test/expected-files/" + targetRepository.pathOf( orig ) );
258 compareFiles( artifactFile, testFile );
261 public void testV3PomWarningsOnConvert()
264 // test that the pom is converted but that warnings are reported
266 Artifact artifact = createArtifact( "test", "v3-warnings-artifact", "1.0.0" );
267 ArtifactMetadata artifactMetadata = new ArtifactRepositoryMetadata( artifact );
268 File artifactMetadataFile = new File( targetRepository.getBasedir(), targetRepository
269 .pathOfRemoteRepositoryMetadata( artifactMetadata ) );
270 artifactMetadataFile.delete();
272 ArtifactMetadata versionMetadata = new SnapshotArtifactRepositoryMetadata( artifact );
273 File versionMetadataFile = new File( targetRepository.getBasedir(), targetRepository
274 .pathOfRemoteRepositoryMetadata( versionMetadata ) );
275 versionMetadataFile.delete();
277 artifactConverter.convert( artifact, targetRepository );
278 checkWarnings( artifactConverter, 2 );
280 File artifactFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
281 assertTrue( "Check artifact created", artifactFile.exists() );
282 assertTrue( "Check artifact matches", FileUtils.contentEquals( artifactFile, artifact.getFile() ) );
284 artifact = createPomArtifact( artifact );
285 File pomFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
286 File expectedPomFile = getTestFile( "src/test/expected-files/converted-v3-warnings.pom" );
287 assertTrue( "Check POM created", pomFile.exists() );
289 compareFiles( expectedPomFile, pomFile );
291 // TODO: check 2 warnings (extend and versions) matched on i18n key
294 private void doTestV4SnapshotPomConvert( String version, String expectedMetadataFileName )
297 // test that it is copied as is
299 Artifact artifact = createArtifact( "test", "v4artifact", version );
300 ArtifactMetadata artifactMetadata = new ArtifactRepositoryMetadata( artifact );
301 File artifactMetadataFile = new File( targetRepository.getBasedir(), targetRepository
302 .pathOfRemoteRepositoryMetadata( artifactMetadata ) );
303 artifactMetadataFile.delete();
305 ArtifactMetadata snapshotMetadata = new SnapshotArtifactRepositoryMetadata( artifact );
306 File snapshotMetadataFile = new File( targetRepository.getBasedir(), targetRepository
307 .pathOfRemoteRepositoryMetadata( snapshotMetadata ) );
308 snapshotMetadataFile.delete();
310 artifactConverter.convert( artifact, targetRepository );
311 checkSuccess( artifactConverter );
313 File artifactFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
314 assertTrue( "Check artifact created", artifactFile.exists() );
315 assertTrue( "Check artifact matches", FileUtils.contentEquals( artifactFile, artifact.getFile() ) );
317 artifact = createPomArtifact( artifact );
318 File pomFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
319 File sourcePomFile = new File( sourceRepository.getBasedir(), sourceRepository.pathOf( artifact ) );
320 assertTrue( "Check POM created", pomFile.exists() );
322 compareFiles( sourcePomFile, pomFile );
324 assertTrue( "Check artifact metadata created", artifactMetadataFile.exists() );
326 File expectedMetadataFile = getTestFile( "src/test/expected-files/v4-snapshot-artifact-metadata.xml" );
328 compareFiles( expectedMetadataFile, artifactMetadataFile );
330 assertTrue( "Check snapshot metadata created", snapshotMetadataFile.exists() );
332 expectedMetadataFile = getTestFile( expectedMetadataFileName );
334 compareFiles( expectedMetadataFile, snapshotMetadataFile );
337 public void testV3SnapshotPomConvert()
340 // test that the pom is coverted
342 Artifact artifact = createArtifact( "test", "v3artifact", "1.0.0-SNAPSHOT" );
343 ArtifactMetadata artifactMetadata = new ArtifactRepositoryMetadata( artifact );
344 File artifactMetadataFile = new File( targetRepository.getBasedir(), targetRepository
345 .pathOfRemoteRepositoryMetadata( artifactMetadata ) );
346 artifactMetadataFile.delete();
348 ArtifactMetadata snapshotMetadata = new SnapshotArtifactRepositoryMetadata( artifact );
349 File snapshotMetadataFile = new File( targetRepository.getBasedir(), targetRepository
350 .pathOfRemoteRepositoryMetadata( snapshotMetadata ) );
351 snapshotMetadataFile.delete();
353 artifactConverter.convert( artifact, targetRepository );
354 checkSuccess( artifactConverter );
356 File artifactFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
357 assertTrue( "Check artifact created", artifactFile.exists() );
358 assertTrue( "Check artifact matches", FileUtils.contentEquals( artifactFile, artifact.getFile() ) );
360 artifact = createPomArtifact( artifact );
361 File pomFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
362 File expectedPomFile = getTestFile( "src/test/expected-files/converted-v3-snapshot.pom" );
363 assertTrue( "Check POM created", pomFile.exists() );
365 compareFiles( expectedPomFile, pomFile );
367 assertTrue( "Check artifact metadata created", artifactMetadataFile.exists() );
369 File expectedMetadataFile = getTestFile( "src/test/expected-files/v3-snapshot-artifact-metadata.xml" );
371 compareFiles( expectedMetadataFile, artifactMetadataFile );
373 assertTrue( "Check snapshot metadata created", snapshotMetadataFile.exists() );
375 expectedMetadataFile = getTestFile( "src/test/expected-files/v3-snapshot-metadata.xml" );
377 compareFiles( expectedMetadataFile, snapshotMetadataFile );
380 public void testV4SnapshotPomConvert()
383 doTestV4SnapshotPomConvert( "1.0.0-SNAPSHOT", "src/test/expected-files/v4-snapshot-metadata.xml" );
388 public void testV4TimestampedSnapshotPomConvert()
391 doTestV4SnapshotPomConvert( "1.0.0-20060111.120115-1",
392 "src/test/expected-files/v4-timestamped-snapshot-metadata.xml" );
397 public void testMavenOnePluginConversion()
400 Artifact artifact = createArtifact( "org.apache.maven.plugins", "maven-foo-plugin", "1.0", "1.0",
402 artifact.setFile( new File( getBasedir(), "src/test/source-repository/test/plugins/maven-foo-plugin-1.0.jar" ) );
403 artifactConverter.convert( artifact, targetRepository );
404 // There is a warning but I can't figure out how to look at it. Eyeballing the results it appears
405 // the plugin is being coverted correctly.
408 File artifactFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
409 assertTrue( "Check artifact created", artifactFile.exists() );
410 assertTrue( "Check artifact matches", FileUtils.contentEquals( artifactFile, artifact.getFile() ) );
413 The POM isn't needed for Maven 1.x plugins but the raw conversion for
415 artifact = createPomArtifact( artifact );
416 File pomFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
417 File expectedPomFile = getTestFile( "src/test/expected-files/maven-foo-plugin-1.0.pom" );
418 assertTrue( "Check POM created", pomFile.exists() );
419 compareFiles( expectedPomFile, pomFile );
423 public void testV3TimestampedSnapshotPomConvert()
426 // test that the pom is coverted
428 Artifact artifact = createArtifact( "test", "v3artifact", "1.0.0-20060105.130101-3" );
429 ArtifactMetadata artifactMetadata = new ArtifactRepositoryMetadata( artifact );
430 File artifactMetadataFile = new File( targetRepository.getBasedir(), targetRepository
431 .pathOfRemoteRepositoryMetadata( artifactMetadata ) );
432 artifactMetadataFile.delete();
434 ArtifactMetadata snapshotMetadata = new SnapshotArtifactRepositoryMetadata( artifact );
435 File snapshotMetadataFile = new File( targetRepository.getBasedir(), targetRepository
436 .pathOfRemoteRepositoryMetadata( snapshotMetadata ) );
437 snapshotMetadataFile.delete();
439 artifactConverter.convert( artifact, targetRepository );
440 checkSuccess( artifactConverter );
442 File artifactFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
443 assertTrue( "Check artifact created", artifactFile.exists() );
444 assertTrue( "Check artifact matches", FileUtils.contentEquals( artifactFile, artifact.getFile() ) );
446 artifact = createPomArtifact( artifact );
447 File pomFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
448 File expectedPomFile = getTestFile( "src/test/expected-files/converted-v3-timestamped-snapshot.pom" );
449 assertTrue( "Check POM created", pomFile.exists() );
451 compareFiles( expectedPomFile, pomFile );
453 assertTrue( "Check artifact snapshotMetadata created", artifactMetadataFile.exists() );
455 File expectedMetadataFile = getTestFile( "src/test/expected-files/v3-snapshot-artifact-metadata.xml" );
457 compareFiles( expectedMetadataFile, artifactMetadataFile );
459 assertTrue( "Check snapshot snapshotMetadata created", snapshotMetadataFile.exists() );
461 expectedMetadataFile = getTestFile( "src/test/expected-files/v3-timestamped-snapshot-metadata.xml" );
463 compareFiles( expectedMetadataFile, snapshotMetadataFile );
466 public void testNoPomConvert()
469 // test that a POM is not created when there was none at the source
471 Artifact artifact = createArtifact( "test", "noPomArtifact", "1.0.0" );
472 artifactConverter.convert( artifact, targetRepository );
473 checkWarnings( artifactConverter, 1 );
475 assertHasWarningReason( artifactConverter, Messages.getString( "warning.missing.pom" ) );
477 File artifactFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
478 assertTrue( "Check artifact created", artifactFile.exists() );
479 assertTrue( "Check artifact matches", FileUtils.contentEquals( artifactFile, artifact.getFile() ) );
481 artifact = createPomArtifact( artifact );
482 File pomFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
483 File sourcePomFile = new File( sourceRepository.getBasedir(), sourceRepository.pathOf( artifact ) );
485 assertFalse( "Check no POM created", pomFile.exists() );
486 assertFalse( "No source POM", sourcePomFile.exists() );
489 public void testIncorrectSourceChecksumMd5()
492 // test that it fails when the source md5 is wrong
494 Artifact artifact = createArtifact( "test", "incorrectMd5Artifact", "1.0.0" );
495 File file = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
498 artifactConverter.convert( artifact, targetRepository );
499 checkWarnings( artifactConverter, 2 );
501 assertHasWarningReason( artifactConverter, Messages.getString( "failure.incorrect.md5" ) );
503 assertFalse( "Check artifact not created", file.exists() );
505 ArtifactRepositoryMetadata metadata = new ArtifactRepositoryMetadata( artifact );
506 File metadataFile = new File( targetRepository.getBasedir(), targetRepository
507 .pathOfRemoteRepositoryMetadata( metadata ) );
508 assertFalse( "Check metadata not created", metadataFile.exists() );
511 public void testIncorrectSourceChecksumSha1()
514 // test that it fails when the source sha1 is wrong
516 Artifact artifact = createArtifact( "test", "incorrectSha1Artifact", "1.0.0" );
517 File file = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
520 artifactConverter.convert( artifact, targetRepository );
521 checkWarnings( artifactConverter, 2 );
523 assertHasWarningReason( artifactConverter, Messages.getString( "failure.incorrect.sha1" ) );
525 assertFalse( "Check artifact not created", file.exists() );
527 ArtifactRepositoryMetadata metadata = new ArtifactRepositoryMetadata( artifact );
528 File metadataFile = new File( targetRepository.getBasedir(), targetRepository
529 .pathOfRemoteRepositoryMetadata( metadata ) );
530 assertFalse( "Check metadata not created", metadataFile.exists() );
533 public void testUnmodifiedArtifact()
534 throws Exception, InterruptedException
536 // test the unmodified artifact is untouched
538 Artifact artifact = createArtifact( "test", "unmodified-artifact", "1.0.0" );
539 Artifact pomArtifact = createPomArtifact( artifact );
541 File sourceFile = new File( sourceRepository.getBasedir(), sourceRepository.pathOf( artifact ) );
542 File sourcePomFile = new File( sourceRepository.getBasedir(), sourceRepository.pathOf( pomArtifact ) );
543 File targetFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
544 File targetPomFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( pomArtifact ) );
546 assertTrue( "Check target file exists", targetFile.exists() );
547 assertTrue( "Check target POM exists", targetPomFile.exists() );
549 sourceFile.setLastModified( System.currentTimeMillis() );
550 sourcePomFile.setLastModified( System.currentTimeMillis() );
552 long origTime = targetFile.lastModified();
553 long origPomTime = targetPomFile.lastModified();
555 // Need to guarantee last modified is not equal
556 Thread.sleep( SLEEP_MILLIS );
558 artifactConverter.convert( artifact, targetRepository );
559 checkSuccess( artifactConverter );
561 compareFiles( sourceFile, targetFile );
562 compareFiles( sourcePomFile, targetPomFile );
564 assertEquals( "Check artifact unmodified", origTime, targetFile.lastModified() );
565 assertEquals( "Check POM unmodified", origPomTime, targetPomFile.lastModified() );
568 public void testModifedArtifactFails()
571 // test that it fails when the source artifact has changed and is different to the existing artifact in the
574 Artifact artifact = createArtifact( "test", "modified-artifact", "1.0.0" );
575 Artifact pomArtifact = createPomArtifact( artifact );
577 File sourceFile = new File( sourceRepository.getBasedir(), sourceRepository.pathOf( artifact ) );
578 File sourcePomFile = new File( sourceRepository.getBasedir(), sourceRepository.pathOf( pomArtifact ) );
579 File targetFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
580 File targetPomFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( pomArtifact ) );
582 assertTrue( "Check target file exists", targetFile.exists() );
583 assertTrue( "Check target POM exists", targetPomFile.exists() );
585 sourceFile.setLastModified( System.currentTimeMillis() );
586 sourcePomFile.setLastModified( System.currentTimeMillis() );
588 long origTime = targetFile.lastModified();
589 long origPomTime = targetPomFile.lastModified();
591 // Need to guarantee last modified is not equal
592 Thread.sleep( SLEEP_MILLIS );
594 artifactConverter.convert( artifact, targetRepository );
595 checkWarnings( artifactConverter, 2 );
597 assertHasWarningReason( artifactConverter, Messages.getString( "failure.target.already.exists" ) );
599 assertEquals( "Check unmodified", origTime, targetFile.lastModified() );
600 assertEquals( "Check unmodified", origPomTime, targetPomFile.lastModified() );
602 ArtifactRepositoryMetadata metadata = new ArtifactRepositoryMetadata( artifact );
603 File metadataFile = new File( targetRepository.getBasedir(), targetRepository
604 .pathOfRemoteRepositoryMetadata( metadata ) );
605 assertFalse( "Check metadata not created", metadataFile.exists() );
608 public void testForcedUnmodifiedArtifact()
611 // test unmodified artifact is still converted when set to force
613 artifactConverter = (ArtifactConverter) lookup( ArtifactConverter.ROLE, "force-repository-converter" );
615 Artifact artifact = createArtifact( "test", "unmodified-artifact", "1.0.0" );
616 Artifact pomArtifact = createPomArtifact( artifact );
618 File sourceFile = new File( sourceRepository.getBasedir(), sourceRepository.pathOf( artifact ) );
619 File sourcePomFile = new File( sourceRepository.getBasedir(), sourceRepository.pathOf( pomArtifact ) );
620 File targetFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
621 File targetPomFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( pomArtifact ) );
623 SimpleDateFormat dateFormat = new SimpleDateFormat( "yyyy-MM-dd", Locale.getDefault() );
624 long origTime = dateFormat.parse( "2006-03-03" ).getTime();
625 targetFile.setLastModified( origTime );
626 targetPomFile.setLastModified( origTime );
628 sourceFile.setLastModified( dateFormat.parse( "2006-01-01" ).getTime() );
629 sourcePomFile.setLastModified( dateFormat.parse( "2006-02-02" ).getTime() );
631 artifactConverter.convert( artifact, targetRepository );
632 checkSuccess( artifactConverter );
634 compareFiles( sourceFile, targetFile );
635 compareFiles( sourcePomFile, targetPomFile );
637 assertFalse( "Check modified", origTime == targetFile.lastModified() );
638 assertFalse( "Check modified", origTime == targetPomFile.lastModified() );
640 ArtifactRepositoryMetadata metadata = new ArtifactRepositoryMetadata( artifact );
641 File metadataFile = new File( targetRepository.getBasedir(), targetRepository
642 .pathOfRemoteRepositoryMetadata( metadata ) );
643 assertTrue( "Check metadata created", metadataFile.exists() );
646 public void testDryRunSuccess()
649 // test dry run does nothing on a run that will be successful, and returns success
651 artifactConverter = (ArtifactConverter) lookup( ArtifactConverter.ROLE, "dryrun-repository-converter" );
653 Artifact artifact = createArtifact( "test", "dryrun-artifact", "1.0.0" );
654 Artifact pomArtifact = createPomArtifact( artifact );
656 File sourceFile = new File( sourceRepository.getBasedir(), sourceRepository.pathOf( artifact ) );
657 File sourcePomFile = new File( sourceRepository.getBasedir(), sourceRepository.pathOf( pomArtifact ) );
658 File targetFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
659 File targetPomFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( pomArtifact ) );
661 artifactConverter.convert( artifact, targetRepository );
662 checkSuccess( artifactConverter );
664 assertTrue( "Check source file exists", sourceFile.exists() );
665 assertTrue( "Check source POM exists", sourcePomFile.exists() );
667 assertFalse( "Check target file doesn't exist", targetFile.exists() );
668 assertFalse( "Check target POM doesn't exist", targetPomFile.exists() );
670 ArtifactRepositoryMetadata metadata = new ArtifactRepositoryMetadata( artifact );
671 File metadataFile = new File( targetRepository.getBasedir(), targetRepository
672 .pathOfRemoteRepositoryMetadata( metadata ) );
673 assertFalse( "Check metadata not created", metadataFile.exists() );
676 public void testDryRunFailure()
679 // test dry run does nothing on a run that will fail, and returns failure
681 artifactConverter = (ArtifactConverter) lookup( ArtifactConverter.ROLE, "dryrun-repository-converter" );
683 Artifact artifact = createArtifact( "test", "modified-artifact", "1.0.0" );
684 Artifact pomArtifact = createPomArtifact( artifact );
686 File sourceFile = new File( sourceRepository.getBasedir(), sourceRepository.pathOf( artifact ) );
687 File sourcePomFile = new File( sourceRepository.getBasedir(), sourceRepository.pathOf( pomArtifact ) );
688 File targetFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
689 File targetPomFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( pomArtifact ) );
691 assertTrue( "Check target file exists", targetFile.exists() );
692 assertTrue( "Check target POM exists", targetPomFile.exists() );
694 sourceFile.setLastModified( System.currentTimeMillis() );
695 sourcePomFile.setLastModified( System.currentTimeMillis() );
697 long origTime = targetFile.lastModified();
698 long origPomTime = targetPomFile.lastModified();
700 // Need to guarantee last modified is not equal
701 Thread.sleep( SLEEP_MILLIS );
703 artifactConverter.convert( artifact, targetRepository );
704 checkWarnings( artifactConverter, 2 );
706 assertHasWarningReason( artifactConverter, Messages.getString( "failure.target.already.exists" ) );
708 assertEquals( "Check unmodified", origTime, targetFile.lastModified() );
709 assertEquals( "Check unmodified", origPomTime, targetPomFile.lastModified() );
711 ArtifactRepositoryMetadata metadata = new ArtifactRepositoryMetadata( artifact );
712 File metadataFile = new File( targetRepository.getBasedir(), targetRepository
713 .pathOfRemoteRepositoryMetadata( metadata ) );
714 assertFalse( "Check metadata not created", metadataFile.exists() );
717 public void testRollbackArtifactCreated()
720 // test rollback can remove a created artifact, including checksums
722 Artifact artifact = createArtifact( "test", "rollback-created-artifact", "1.0.0" );
723 ArtifactMetadata artifactMetadata = new ArtifactRepositoryMetadata( artifact );
724 File artifactMetadataFile = new File( targetRepository.getBasedir(), targetRepository
725 .pathOfRemoteRepositoryMetadata( artifactMetadata ) );
726 FileUtils.deleteDirectory( artifactMetadataFile.getParentFile() );
728 ArtifactMetadata versionMetadata = new SnapshotArtifactRepositoryMetadata( artifact );
729 File versionMetadataFile = new File( targetRepository.getBasedir(), targetRepository
730 .pathOfRemoteRepositoryMetadata( versionMetadata ) );
732 File artifactFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
734 artifactConverter.convert( artifact, targetRepository );
735 checkWarnings( artifactConverter, 2 );
737 boolean found = false;
738 String pattern = "^" + Messages.getString( "invalid.source.pom" ).replaceFirst( "\\{0\\}", ".*" ) + "$";
739 for ( Iterator it = artifactConverter.getWarnings().values().iterator(); it.hasNext() && !found; )
741 List messages = (List) it.next();
743 for ( Iterator itmsgs = messages.iterator(); itmsgs.hasNext(); )
745 String message = (String) itmsgs.next();
746 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 artifacts = new ArrayList();
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 ( Iterator it = artifacts.iterator(); it.hasNext(); )
773 Artifact arti = (Artifact) it.next();
774 artifactConverter.convert( arti, targetRepository );
775 checkSuccess( artifactConverter );
778 for ( Iterator i = artifacts.iterator(); i.hasNext(); )
780 Artifact artifact = (Artifact) i.next();
782 File artifactFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
783 assertTrue( "Check artifact created", artifactFile.exists() );
784 assertTrue( "Check artifact matches", FileUtils.contentEquals( artifactFile, artifact.getFile() ) );
786 artifact = createPomArtifact( artifact );
787 File pomFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
788 File expectedPomFile = getTestFile( "src/test/expected-files/converted-" + artifact.getArtifactId()
790 assertTrue( "Check POM created", pomFile.exists() );
792 compareFiles( expectedPomFile, pomFile );
796 public void testInvalidSourceArtifactMetadata()
799 // test artifact is not converted when source metadata is invalid, and returns failure
801 createModernSourceRepository();
803 Artifact artifact = createArtifact( "test", "incorrectArtifactMetadata", "1.0.0" );
804 File file = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
807 artifactConverter.convert( artifact, targetRepository );
808 checkWarnings( artifactConverter, 2 );
810 assertHasWarningReason( artifactConverter, Messages.getString( "failure.incorrect.artifactMetadata.versions" ) );
812 assertFalse( "Check artifact not created", file.exists() );
814 ArtifactRepositoryMetadata metadata = new ArtifactRepositoryMetadata( artifact );
815 File metadataFile = new File( targetRepository.getBasedir(), targetRepository
816 .pathOfRemoteRepositoryMetadata( metadata ) );
817 assertFalse( "Check metadata not created", metadataFile.exists() );
820 public void testInvalidSourceSnapshotMetadata()
823 // test artifact is not converted when source snapshot metadata is invalid and returns failure
825 createModernSourceRepository();
827 Artifact artifact = createArtifact( "test", "incorrectSnapshotMetadata", "1.0.0-20060102.030405-6" );
828 File file = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
831 artifactConverter.convert( artifact, targetRepository );
832 checkWarnings( artifactConverter, 2 );
834 assertHasWarningReason( artifactConverter, Messages.getString( "failure.incorrect.snapshotMetadata.snapshot" ) );
836 assertFalse( "Check artifact not created", file.exists() );
838 ArtifactRepositoryMetadata metadata = new ArtifactRepositoryMetadata( artifact );
839 File metadataFile = new File( targetRepository.getBasedir(), targetRepository
840 .pathOfRemoteRepositoryMetadata( metadata ) );
841 assertFalse( "Check metadata not created", metadataFile.exists() );
844 public void testMergeArtifactMetadata()
847 // test artifact level metadata is merged when it already exists on successful conversion
849 Artifact artifact = createArtifact( "test", "newversion-artifact", "1.0.1" );
850 artifactConverter.convert( artifact, targetRepository );
851 checkSuccess( artifactConverter );
853 File artifactFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
854 assertTrue( "Check artifact created", artifactFile.exists() );
855 assertTrue( "Check artifact matches", FileUtils.contentEquals( artifactFile, artifact.getFile() ) );
857 artifact = createPomArtifact( artifact );
858 File pomFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
859 File sourcePomFile = new File( sourceRepository.getBasedir(), sourceRepository.pathOf( artifact ) );
860 assertTrue( "Check POM created", pomFile.exists() );
862 compareFiles( sourcePomFile, pomFile );
864 ArtifactMetadata artifactMetadata = new ArtifactRepositoryMetadata( artifact );
865 File artifactMetadataFile = new File( targetRepository.getBasedir(), targetRepository
866 .pathOfRemoteRepositoryMetadata( artifactMetadata ) );
867 assertTrue( "Check artifact metadata created", artifactMetadataFile.exists() );
869 File expectedMetadataFile = getTestFile( "src/test/expected-files/newversion-artifact-metadata.xml" );
871 compareFiles( expectedMetadataFile, artifactMetadataFile );
874 public void testSourceAndTargetRepositoriesMatch()
877 // test that it fails if the same
879 ArtifactRepositoryFactory factory = (ArtifactRepositoryFactory) lookup( ArtifactRepositoryFactory.ROLE );
881 sourceRepository = factory.createArtifactRepository( "source", targetRepository.getUrl(), targetRepository
882 .getLayout(), null, null );
884 Artifact artifact = createArtifact( "test", "repository-artifact", "1.0" );
888 artifactConverter.convert( artifact, targetRepository );
889 fail( "Should have failed trying to convert within the same repository" );
891 catch ( ArtifactConversionException e )
894 assertEquals( "check message", Messages.getString( "exception.repositories.match" ), e.getMessage() );
895 assertNull( "Check no additional cause", e.getCause() );
899 private Artifact createArtifact( String groupId, String artifactId, String version )
901 Matcher matcher = Artifact.VERSION_FILE_PATTERN.matcher( version );
903 if ( matcher.matches() )
905 baseVersion = matcher.group( 1 ) + "-SNAPSHOT";
909 baseVersion = version;
911 return createArtifact( groupId, artifactId, baseVersion, version, "jar" );
914 private Artifact createArtifact( String groupId, String artifactId, String baseVersion, String version, String type )
916 Artifact artifact = artifactFactory.createArtifact( groupId, artifactId, version, null, type );
917 artifact.setBaseVersion( baseVersion );
918 artifact.setRepository( sourceRepository );
919 artifact.setFile( new File( sourceRepository.getBasedir(), sourceRepository.pathOf( artifact ) ) );
923 private Artifact createPomArtifact( Artifact artifact )
925 return createArtifact( artifact.getGroupId(), artifact.getArtifactId(), artifact.getBaseVersion(), artifact
926 .getVersion(), "pom" );
929 private static void compareFiles( File expectedPomFile, File pomFile )
932 String expectedContent = normalizeString( org.apache.commons.io.FileUtils.readFileToString( expectedPomFile, null ) );
933 String targetContent = normalizeString( org.apache.commons.io.FileUtils.readFileToString( pomFile, null ) );
934 assertEquals( "Check file match between " + expectedPomFile + " and " + pomFile, expectedContent, targetContent );
937 private static String normalizeString( String path )
939 return path.trim().replaceAll( "\r\n", "\n" ).replace( '\r', '\n' ).replaceAll( "<\\?xml .+\\?>", "" );
942 private void checkSuccess( ArtifactConverter converter )
944 assertNotNull( "Warnings should never be null.", converter.getWarnings() );
945 assertEquals( "Should have no warnings.", 0, countWarningMessages( converter ) );
948 private void checkWarnings( ArtifactConverter converter, int count )
950 assertNotNull( "Warnings should never be null.", converter.getWarnings() );
951 assertEquals( "Should have some warnings.", count, countWarningMessages( converter ) );
954 private int countWarningMessages( ArtifactConverter converter )
957 for ( Iterator it = converter.getWarnings().values().iterator(); it.hasNext(); )
959 List values = (List) it.next();
960 count += values.size();
965 private void assertHasWarningReason( ArtifactConverter converter, String reason )
967 assertNotNull( "Warnings should never be null.", converter.getWarnings() );
968 assertTrue( "Expecting 1 or more Warnings", countWarningMessages( converter ) > 0 );
970 for ( Iterator it = converter.getWarnings().values().iterator(); it.hasNext(); )
972 List messages = (List) it.next();
973 if ( messages.contains( reason ) )
975 /* No need to check any furthor */
980 /* didn't find it. */
982 for ( Iterator it = converter.getWarnings().entrySet().iterator(); it.hasNext(); )
984 Map.Entry entry = (Entry) it.next();
985 Artifact artifact = (Artifact) entry.getKey();
986 System.out.println( "-Artifact: " + artifact.getGroupId() + ":" + artifact.getArtifactId() + ":"
987 + artifact.getVersion() );
988 List messages = (List) entry.getValue();
989 for ( Iterator itmsgs = messages.iterator(); itmsgs.hasNext(); )
991 String message = (String) itmsgs.next();
992 System.out.println( " " + message );
995 fail( "Unable to find message <" + reason + "> in warnings." );
998 private void createModernSourceRepository()
1001 ArtifactRepositoryFactory factory = (ArtifactRepositoryFactory) lookup( ArtifactRepositoryFactory.ROLE );
1003 ArtifactRepositoryLayout layout = (ArtifactRepositoryLayout) lookup( ArtifactRepositoryLayout.ROLE, "default" );
1005 File sourceBase = getTestFile( "src/test/source-modern-repository" );
1006 sourceRepository = factory.createArtifactRepository( "source", sourceBase.toURL().toString(), layout, null,