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.AsciiFileUtil;
25 import org.apache.maven.archiva.converter.artifact.Messages;
26 import org.apache.maven.artifact.Artifact;
27 import org.apache.maven.artifact.factory.ArtifactFactory;
28 import org.apache.maven.artifact.metadata.ArtifactMetadata;
29 import org.apache.maven.artifact.repository.ArtifactRepository;
30 import org.apache.maven.artifact.repository.ArtifactRepositoryFactory;
31 import org.apache.maven.artifact.repository.layout.ArtifactRepositoryLayout;
32 import org.apache.maven.artifact.repository.metadata.ArtifactRepositoryMetadata;
33 import org.apache.maven.artifact.repository.metadata.SnapshotArtifactRepositoryMetadata;
34 import org.codehaus.plexus.PlexusTestCase;
35 import org.codehaus.plexus.util.FileUtils;
38 import java.io.IOException;
39 import java.text.SimpleDateFormat;
40 import java.util.ArrayList;
41 import java.util.Iterator;
42 import java.util.List;
43 import java.util.Locale;
45 import java.util.Map.Entry;
46 import java.util.regex.Matcher;
49 * LegacyToDefaultConverterTest
51 * @author <a href="mailto:joakime@apache.org">Joakim Erdfelt</a>
54 public class LegacyToDefaultConverterTest
55 extends PlexusTestCase
57 private ArtifactRepository sourceRepository;
59 private ArtifactRepository targetRepository;
61 private ArtifactConverter artifactConverter;
63 private ArtifactFactory artifactFactory;
65 private static final int SLEEP_MILLIS = 100;
67 protected void setUp()
72 ArtifactRepositoryFactory factory = (ArtifactRepositoryFactory) lookup( ArtifactRepositoryFactory.ROLE );
74 ArtifactRepositoryLayout layout = (ArtifactRepositoryLayout) lookup( ArtifactRepositoryLayout.ROLE, "legacy" );
76 File sourceBase = getTestFile( "src/test/source-repository" );
77 sourceRepository = factory.createArtifactRepository( "source", sourceBase.toURL().toString(), layout, null,
80 layout = (ArtifactRepositoryLayout) lookup( ArtifactRepositoryLayout.ROLE, "default" );
82 File targetBase = getTestFile( "target/test-target-repository" );
83 copyDirectoryStructure( getTestFile( "src/test/target-repository" ), targetBase );
85 targetRepository = factory.createArtifactRepository( "target", targetBase.toURL().toString(), layout, null,
88 artifactConverter = (ArtifactConverter) lookup( ArtifactConverter.ROLE, "legacy-to-default" );
90 artifactFactory = (ArtifactFactory) lookup( ArtifactFactory.ROLE );
93 protected void tearDown()
99 private void copyDirectoryStructure( File sourceDirectory, File destinationDirectory )
102 if ( !sourceDirectory.exists() )
104 throw new IOException( "Source directory doesn't exists (" + sourceDirectory.getAbsolutePath() + ")." );
107 File[] files = sourceDirectory.listFiles();
109 String sourcePath = sourceDirectory.getAbsolutePath();
111 for ( int i = 0; i < files.length; i++ )
113 File file = files[i];
115 String dest = file.getAbsolutePath();
117 dest = dest.substring( sourcePath.length() + 1 );
119 File destination = new File( destinationDirectory, dest );
123 destination = destination.getParentFile();
125 FileUtils.copyFileToDirectory( file, destination );
127 else if ( file.isDirectory() )
129 if ( !".svn".equals( file.getName() ) )
131 if ( !destination.exists() && !destination.mkdirs() )
133 throw new IOException( "Could not create destination directory '"
134 + destination.getAbsolutePath() + "'." );
136 copyDirectoryStructure( file, destination );
141 throw new IOException( "Unknown file type: " + file.getAbsolutePath() );
146 public void testV4PomConvert()
149 // test that it is copied as is
151 Artifact artifact = createArtifact( "test", "v4artifact", "1.0.0" );
152 ArtifactMetadata artifactMetadata = new ArtifactRepositoryMetadata( artifact );
153 File artifactMetadataFile = new File( targetRepository.getBasedir(), targetRepository
154 .pathOfRemoteRepositoryMetadata( artifactMetadata ) );
155 artifactMetadataFile.delete();
157 ArtifactMetadata versionMetadata = new SnapshotArtifactRepositoryMetadata( artifact );
158 File versionMetadataFile = new File( targetRepository.getBasedir(), targetRepository
159 .pathOfRemoteRepositoryMetadata( versionMetadata ) );
160 versionMetadataFile.delete();
162 File artifactFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
163 artifactFile.delete();
165 artifactConverter.convert( artifact, targetRepository );
166 checkSuccess( artifactConverter );
168 assertTrue( "Check artifact created", artifactFile.exists() );
169 assertTrue( "Check artifact matches", FileUtils.contentEquals( artifactFile, artifact.getFile() ) );
171 artifact = createPomArtifact( artifact );
172 File pomFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
173 File sourcePomFile = new File( sourceRepository.getBasedir(), sourceRepository.pathOf( artifact ) );
174 assertTrue( "Check POM created", pomFile.exists() );
176 compareFiles( sourcePomFile, pomFile );
178 assertTrue( "Check artifact metadata created", artifactMetadataFile.exists() );
180 File expectedMetadataFile = getTestFile( "src/test/expected-files/v4-artifact-metadata.xml" );
182 compareFiles( expectedMetadataFile, artifactMetadataFile );
184 assertTrue( "Check snapshot metadata created", versionMetadataFile.exists() );
186 expectedMetadataFile = getTestFile( "src/test/expected-files/v4-version-metadata.xml" );
188 compareFiles( expectedMetadataFile, versionMetadataFile );
191 public void testV3PomConvert()
194 // test that the pom is coverted
196 Artifact artifact = createArtifact( "test", "v3artifact", "1.0.0" );
197 ArtifactMetadata artifactMetadata = new ArtifactRepositoryMetadata( artifact );
198 File artifactMetadataFile = new File( targetRepository.getBasedir(), targetRepository
199 .pathOfRemoteRepositoryMetadata( artifactMetadata ) );
200 artifactMetadataFile.delete();
202 ArtifactMetadata versionMetadata = new SnapshotArtifactRepositoryMetadata( artifact );
203 File versionMetadataFile = new File( targetRepository.getBasedir(), targetRepository
204 .pathOfRemoteRepositoryMetadata( versionMetadata ) );
205 versionMetadataFile.delete();
207 artifactConverter.convert( artifact, targetRepository );
208 checkSuccess( artifactConverter );
210 File artifactFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
211 assertTrue( "Check artifact created", artifactFile.exists() );
212 assertTrue( "Check artifact matches", FileUtils.contentEquals( artifactFile, artifact.getFile() ) );
214 artifact = createPomArtifact( artifact );
215 File pomFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
216 File expectedPomFile = getTestFile( "src/test/expected-files/converted-v3.pom" );
217 assertTrue( "Check POM created", pomFile.exists() );
219 compareFiles( expectedPomFile, pomFile );
221 assertTrue( "Check artifact metadata created", artifactMetadataFile.exists() );
223 File expectedMetadataFile = getTestFile( "src/test/expected-files/v3-artifact-metadata.xml" );
225 compareFiles( expectedMetadataFile, artifactMetadataFile );
227 assertTrue( "Check snapshot metadata created", versionMetadataFile.exists() );
229 expectedMetadataFile = getTestFile( "src/test/expected-files/v3-version-metadata.xml" );
231 compareFiles( expectedMetadataFile, versionMetadataFile );
234 public void testV3PomConvertWithRelocation()
237 Artifact artifact = createArtifact( "test", "relocated-v3artifact", "1.0.0" );
238 ArtifactMetadata artifactMetadata = new ArtifactRepositoryMetadata( artifact );
239 File artifactMetadataFile = new File( targetRepository.getBasedir(), targetRepository
240 .pathOfRemoteRepositoryMetadata( artifactMetadata ) );
241 artifactMetadataFile.delete();
243 ArtifactMetadata versionMetadata = new SnapshotArtifactRepositoryMetadata( artifact );
244 File versionMetadataFile = new File( targetRepository.getBasedir(), targetRepository
245 .pathOfRemoteRepositoryMetadata( versionMetadata ) );
246 versionMetadataFile.delete();
248 artifactConverter.convert( artifact, targetRepository );
249 //checkSuccess(); --> commented until MNG-2100 is fixed
251 File artifactFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
252 assertTrue( "Check if relocated artifact created", artifactFile.exists() );
253 assertTrue( "Check if relocated artifact matches", FileUtils.contentEquals( artifactFile, artifact.getFile() ) );
254 Artifact pomArtifact = createArtifact( "relocated-test", "relocated-v3artifact", "1.0.0", "1.0.0", "pom" );
255 File pomFile = getTestFile( "src/test/expected-files/" + targetRepository.pathOf( pomArtifact ) );
256 File testFile = getTestFile( "target/test-target-repository/" + targetRepository.pathOf( pomArtifact ) );
257 compareFiles( pomFile, testFile );
259 Artifact orig = createArtifact( "test", "relocated-v3artifact", "1.0.0", "1.0.0", "pom" );
260 artifactFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( orig ) );
261 assertTrue( "Check if relocation artifact pom is created", artifactFile.exists() );
262 testFile = getTestFile( "src/test/expected-files/" + targetRepository.pathOf( orig ) );
263 compareFiles( artifactFile, testFile );
266 public void testV3PomWarningsOnConvert()
269 // test that the pom is converted but that warnings are reported
271 Artifact artifact = createArtifact( "test", "v3-warnings-artifact", "1.0.0" );
272 ArtifactMetadata artifactMetadata = new ArtifactRepositoryMetadata( artifact );
273 File artifactMetadataFile = new File( targetRepository.getBasedir(), targetRepository
274 .pathOfRemoteRepositoryMetadata( artifactMetadata ) );
275 artifactMetadataFile.delete();
277 ArtifactMetadata versionMetadata = new SnapshotArtifactRepositoryMetadata( artifact );
278 File versionMetadataFile = new File( targetRepository.getBasedir(), targetRepository
279 .pathOfRemoteRepositoryMetadata( versionMetadata ) );
280 versionMetadataFile.delete();
282 artifactConverter.convert( artifact, targetRepository );
283 checkWarnings( artifactConverter, 2 );
285 File artifactFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
286 assertTrue( "Check artifact created", artifactFile.exists() );
287 assertTrue( "Check artifact matches", FileUtils.contentEquals( artifactFile, artifact.getFile() ) );
289 artifact = createPomArtifact( artifact );
290 File pomFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
291 File expectedPomFile = getTestFile( "src/test/expected-files/converted-v3-warnings.pom" );
292 assertTrue( "Check POM created", pomFile.exists() );
294 compareFiles( expectedPomFile, pomFile );
296 // TODO: check 2 warnings (extend and versions) matched on i18n key
299 private void doTestV4SnapshotPomConvert( String version, String expectedMetadataFileName )
302 // test that it is copied as is
304 Artifact artifact = createArtifact( "test", "v4artifact", version );
305 ArtifactMetadata artifactMetadata = new ArtifactRepositoryMetadata( artifact );
306 File artifactMetadataFile = new File( targetRepository.getBasedir(), targetRepository
307 .pathOfRemoteRepositoryMetadata( artifactMetadata ) );
308 artifactMetadataFile.delete();
310 ArtifactMetadata snapshotMetadata = new SnapshotArtifactRepositoryMetadata( artifact );
311 File snapshotMetadataFile = new File( targetRepository.getBasedir(), targetRepository
312 .pathOfRemoteRepositoryMetadata( snapshotMetadata ) );
313 snapshotMetadataFile.delete();
315 artifactConverter.convert( artifact, targetRepository );
316 checkSuccess( artifactConverter );
318 File artifactFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
319 assertTrue( "Check artifact created", artifactFile.exists() );
320 assertTrue( "Check artifact matches", FileUtils.contentEquals( artifactFile, artifact.getFile() ) );
322 artifact = createPomArtifact( artifact );
323 File pomFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
324 File sourcePomFile = new File( sourceRepository.getBasedir(), sourceRepository.pathOf( artifact ) );
325 assertTrue( "Check POM created", pomFile.exists() );
327 compareFiles( sourcePomFile, pomFile );
329 assertTrue( "Check artifact metadata created", artifactMetadataFile.exists() );
331 File expectedMetadataFile = getTestFile( "src/test/expected-files/v4-snapshot-artifact-metadata.xml" );
333 compareFiles( expectedMetadataFile, artifactMetadataFile );
335 assertTrue( "Check snapshot metadata created", snapshotMetadataFile.exists() );
337 expectedMetadataFile = getTestFile( expectedMetadataFileName );
339 compareFiles( expectedMetadataFile, snapshotMetadataFile );
342 public void testV3SnapshotPomConvert()
345 // test that the pom is coverted
347 Artifact artifact = createArtifact( "test", "v3artifact", "1.0.0-SNAPSHOT" );
348 ArtifactMetadata artifactMetadata = new ArtifactRepositoryMetadata( artifact );
349 File artifactMetadataFile = new File( targetRepository.getBasedir(), targetRepository
350 .pathOfRemoteRepositoryMetadata( artifactMetadata ) );
351 artifactMetadataFile.delete();
353 ArtifactMetadata snapshotMetadata = new SnapshotArtifactRepositoryMetadata( artifact );
354 File snapshotMetadataFile = new File( targetRepository.getBasedir(), targetRepository
355 .pathOfRemoteRepositoryMetadata( snapshotMetadata ) );
356 snapshotMetadataFile.delete();
358 artifactConverter.convert( artifact, targetRepository );
359 checkSuccess( artifactConverter );
361 File artifactFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
362 assertTrue( "Check artifact created", artifactFile.exists() );
363 assertTrue( "Check artifact matches", FileUtils.contentEquals( artifactFile, artifact.getFile() ) );
365 artifact = createPomArtifact( artifact );
366 File pomFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
367 File expectedPomFile = getTestFile( "src/test/expected-files/converted-v3-snapshot.pom" );
368 assertTrue( "Check POM created", pomFile.exists() );
370 compareFiles( expectedPomFile, pomFile );
372 assertTrue( "Check artifact metadata created", artifactMetadataFile.exists() );
374 File expectedMetadataFile = getTestFile( "src/test/expected-files/v3-snapshot-artifact-metadata.xml" );
376 compareFiles( expectedMetadataFile, artifactMetadataFile );
378 assertTrue( "Check snapshot metadata created", snapshotMetadataFile.exists() );
380 expectedMetadataFile = getTestFile( "src/test/expected-files/v3-snapshot-metadata.xml" );
382 compareFiles( expectedMetadataFile, snapshotMetadataFile );
385 public void testV4SnapshotPomConvert()
388 doTestV4SnapshotPomConvert( "1.0.0-SNAPSHOT", "src/test/expected-files/v4-snapshot-metadata.xml" );
393 public void testV4TimestampedSnapshotPomConvert()
396 doTestV4SnapshotPomConvert( "1.0.0-20060111.120115-1",
397 "src/test/expected-files/v4-timestamped-snapshot-metadata.xml" );
402 public void testMavenOnePluginConversion()
405 Artifact artifact = createArtifact( "org.apache.maven.plugins", "maven-foo-plugin", "1.0", "1.0",
407 artifact.setFile( new File( getBasedir(), "src/test/source-repository/test/plugins/maven-foo-plugin-1.0.jar" ) );
408 artifactConverter.convert( artifact, targetRepository );
409 // There is a warning but I can't figure out how to look at it. Eyeballing the results it appears
410 // the plugin is being coverted correctly.
413 File artifactFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
414 assertTrue( "Check artifact created", artifactFile.exists() );
415 assertTrue( "Check artifact matches", FileUtils.contentEquals( artifactFile, artifact.getFile() ) );
418 The POM isn't needed for Maven 1.x plugins but the raw conversion for
420 artifact = createPomArtifact( artifact );
421 File pomFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
422 File expectedPomFile = getTestFile( "src/test/expected-files/maven-foo-plugin-1.0.pom" );
423 assertTrue( "Check POM created", pomFile.exists() );
424 compareFiles( expectedPomFile, pomFile );
428 public void testV3TimestampedSnapshotPomConvert()
431 // test that the pom is coverted
433 Artifact artifact = createArtifact( "test", "v3artifact", "1.0.0-20060105.130101-3" );
434 ArtifactMetadata artifactMetadata = new ArtifactRepositoryMetadata( artifact );
435 File artifactMetadataFile = new File( targetRepository.getBasedir(), targetRepository
436 .pathOfRemoteRepositoryMetadata( artifactMetadata ) );
437 artifactMetadataFile.delete();
439 ArtifactMetadata snapshotMetadata = new SnapshotArtifactRepositoryMetadata( artifact );
440 File snapshotMetadataFile = new File( targetRepository.getBasedir(), targetRepository
441 .pathOfRemoteRepositoryMetadata( snapshotMetadata ) );
442 snapshotMetadataFile.delete();
444 artifactConverter.convert( artifact, targetRepository );
445 checkSuccess( artifactConverter );
447 File artifactFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
448 assertTrue( "Check artifact created", artifactFile.exists() );
449 assertTrue( "Check artifact matches", FileUtils.contentEquals( artifactFile, artifact.getFile() ) );
451 artifact = createPomArtifact( artifact );
452 File pomFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
453 File expectedPomFile = getTestFile( "src/test/expected-files/converted-v3-timestamped-snapshot.pom" );
454 assertTrue( "Check POM created", pomFile.exists() );
456 compareFiles( expectedPomFile, pomFile );
458 assertTrue( "Check artifact snapshotMetadata created", artifactMetadataFile.exists() );
460 File expectedMetadataFile = getTestFile( "src/test/expected-files/v3-snapshot-artifact-metadata.xml" );
462 compareFiles( expectedMetadataFile, artifactMetadataFile );
464 assertTrue( "Check snapshot snapshotMetadata created", snapshotMetadataFile.exists() );
466 expectedMetadataFile = getTestFile( "src/test/expected-files/v3-timestamped-snapshot-metadata.xml" );
468 compareFiles( expectedMetadataFile, snapshotMetadataFile );
471 public void testNoPomConvert()
474 // test that a POM is not created when there was none at the source
476 Artifact artifact = createArtifact( "test", "noPomArtifact", "1.0.0" );
477 artifactConverter.convert( artifact, targetRepository );
478 checkWarnings( artifactConverter, 1 );
480 assertHasWarningReason( artifactConverter, Messages.getString( "warning.missing.pom" ) );
482 File artifactFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
483 assertTrue( "Check artifact created", artifactFile.exists() );
484 assertTrue( "Check artifact matches", FileUtils.contentEquals( artifactFile, artifact.getFile() ) );
486 artifact = createPomArtifact( artifact );
487 File pomFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
488 File sourcePomFile = new File( sourceRepository.getBasedir(), sourceRepository.pathOf( artifact ) );
490 assertFalse( "Check no POM created", pomFile.exists() );
491 assertFalse( "No source POM", sourcePomFile.exists() );
494 public void testIncorrectSourceChecksumMd5()
497 // test that it fails when the source md5 is wrong
499 Artifact artifact = createArtifact( "test", "incorrectMd5Artifact", "1.0.0" );
500 File file = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
503 artifactConverter.convert( artifact, targetRepository );
504 checkWarnings( artifactConverter, 2 );
506 assertHasWarningReason( artifactConverter, Messages.getString( "failure.incorrect.md5" ) );
508 assertFalse( "Check artifact not created", file.exists() );
510 ArtifactRepositoryMetadata metadata = new ArtifactRepositoryMetadata( artifact );
511 File metadataFile = new File( targetRepository.getBasedir(), targetRepository
512 .pathOfRemoteRepositoryMetadata( metadata ) );
513 assertFalse( "Check metadata not created", metadataFile.exists() );
516 public void testIncorrectSourceChecksumSha1()
519 // test that it fails when the source sha1 is wrong
521 Artifact artifact = createArtifact( "test", "incorrectSha1Artifact", "1.0.0" );
522 File file = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
525 artifactConverter.convert( artifact, targetRepository );
526 checkWarnings( artifactConverter, 2 );
528 assertHasWarningReason( artifactConverter, Messages.getString( "failure.incorrect.sha1" ) );
530 assertFalse( "Check artifact not created", file.exists() );
532 ArtifactRepositoryMetadata metadata = new ArtifactRepositoryMetadata( artifact );
533 File metadataFile = new File( targetRepository.getBasedir(), targetRepository
534 .pathOfRemoteRepositoryMetadata( metadata ) );
535 assertFalse( "Check metadata not created", metadataFile.exists() );
538 public void testUnmodifiedArtifact()
539 throws Exception, InterruptedException
541 // test the unmodified artifact is untouched
543 Artifact artifact = createArtifact( "test", "unmodified-artifact", "1.0.0" );
544 Artifact pomArtifact = createPomArtifact( artifact );
546 File sourceFile = new File( sourceRepository.getBasedir(), sourceRepository.pathOf( artifact ) );
547 File sourcePomFile = new File( sourceRepository.getBasedir(), sourceRepository.pathOf( pomArtifact ) );
548 File targetFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
549 File targetPomFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( pomArtifact ) );
551 assertTrue( "Check target file exists", targetFile.exists() );
552 assertTrue( "Check target POM exists", targetPomFile.exists() );
554 sourceFile.setLastModified( System.currentTimeMillis() );
555 sourcePomFile.setLastModified( System.currentTimeMillis() );
557 long origTime = targetFile.lastModified();
558 long origPomTime = targetPomFile.lastModified();
560 // Need to guarantee last modified is not equal
561 Thread.sleep( SLEEP_MILLIS );
563 artifactConverter.convert( artifact, targetRepository );
564 checkSuccess( artifactConverter );
566 compareFiles( sourceFile, targetFile );
567 compareFiles( sourcePomFile, targetPomFile );
569 assertEquals( "Check artifact unmodified", origTime, targetFile.lastModified() );
570 assertEquals( "Check POM unmodified", origPomTime, targetPomFile.lastModified() );
573 public void testModifedArtifactFails()
576 // test that it fails when the source artifact has changed and is different to the existing artifact in the
579 Artifact artifact = createArtifact( "test", "modified-artifact", "1.0.0" );
580 Artifact pomArtifact = createPomArtifact( artifact );
582 File sourceFile = new File( sourceRepository.getBasedir(), sourceRepository.pathOf( artifact ) );
583 File sourcePomFile = new File( sourceRepository.getBasedir(), sourceRepository.pathOf( pomArtifact ) );
584 File targetFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
585 File targetPomFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( pomArtifact ) );
587 assertTrue( "Check target file exists", targetFile.exists() );
588 assertTrue( "Check target POM exists", targetPomFile.exists() );
590 sourceFile.setLastModified( System.currentTimeMillis() );
591 sourcePomFile.setLastModified( System.currentTimeMillis() );
593 long origTime = targetFile.lastModified();
594 long origPomTime = targetPomFile.lastModified();
596 // Need to guarantee last modified is not equal
597 Thread.sleep( SLEEP_MILLIS );
599 artifactConverter.convert( artifact, targetRepository );
600 checkWarnings( artifactConverter, 2 );
602 assertHasWarningReason( artifactConverter, Messages.getString( "failure.target.already.exists" ) );
604 assertEquals( "Check unmodified", origTime, targetFile.lastModified() );
605 assertEquals( "Check unmodified", origPomTime, targetPomFile.lastModified() );
607 ArtifactRepositoryMetadata metadata = new ArtifactRepositoryMetadata( artifact );
608 File metadataFile = new File( targetRepository.getBasedir(), targetRepository
609 .pathOfRemoteRepositoryMetadata( metadata ) );
610 assertFalse( "Check metadata not created", metadataFile.exists() );
613 public void testForcedUnmodifiedArtifact()
616 // test unmodified artifact is still converted when set to force
618 artifactConverter = (ArtifactConverter) lookup( ArtifactConverter.ROLE, "force-repository-converter" );
620 Artifact artifact = createArtifact( "test", "unmodified-artifact", "1.0.0" );
621 Artifact pomArtifact = createPomArtifact( artifact );
623 File sourceFile = new File( sourceRepository.getBasedir(), sourceRepository.pathOf( artifact ) );
624 File sourcePomFile = new File( sourceRepository.getBasedir(), sourceRepository.pathOf( pomArtifact ) );
625 File targetFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
626 File targetPomFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( pomArtifact ) );
628 SimpleDateFormat dateFormat = new SimpleDateFormat( "yyyy-MM-dd", Locale.getDefault() );
629 long origTime = dateFormat.parse( "2006-03-03" ).getTime();
630 targetFile.setLastModified( origTime );
631 targetPomFile.setLastModified( origTime );
633 sourceFile.setLastModified( dateFormat.parse( "2006-01-01" ).getTime() );
634 sourcePomFile.setLastModified( dateFormat.parse( "2006-02-02" ).getTime() );
636 artifactConverter.convert( artifact, targetRepository );
637 checkSuccess( artifactConverter );
639 compareFiles( sourceFile, targetFile );
640 compareFiles( sourcePomFile, targetPomFile );
642 assertFalse( "Check modified", origTime == targetFile.lastModified() );
643 assertFalse( "Check modified", origTime == targetPomFile.lastModified() );
645 ArtifactRepositoryMetadata metadata = new ArtifactRepositoryMetadata( artifact );
646 File metadataFile = new File( targetRepository.getBasedir(), targetRepository
647 .pathOfRemoteRepositoryMetadata( metadata ) );
648 assertTrue( "Check metadata created", metadataFile.exists() );
651 public void testDryRunSuccess()
654 // test dry run does nothing on a run that will be successful, and returns success
656 artifactConverter = (ArtifactConverter) lookup( ArtifactConverter.ROLE, "dryrun-repository-converter" );
658 Artifact artifact = createArtifact( "test", "dryrun-artifact", "1.0.0" );
659 Artifact pomArtifact = createPomArtifact( artifact );
661 File sourceFile = new File( sourceRepository.getBasedir(), sourceRepository.pathOf( artifact ) );
662 File sourcePomFile = new File( sourceRepository.getBasedir(), sourceRepository.pathOf( pomArtifact ) );
663 File targetFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
664 File targetPomFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( pomArtifact ) );
666 artifactConverter.convert( artifact, targetRepository );
667 checkSuccess( artifactConverter );
669 assertTrue( "Check source file exists", sourceFile.exists() );
670 assertTrue( "Check source POM exists", sourcePomFile.exists() );
672 assertFalse( "Check target file doesn't exist", targetFile.exists() );
673 assertFalse( "Check target POM doesn't exist", targetPomFile.exists() );
675 ArtifactRepositoryMetadata metadata = new ArtifactRepositoryMetadata( artifact );
676 File metadataFile = new File( targetRepository.getBasedir(), targetRepository
677 .pathOfRemoteRepositoryMetadata( metadata ) );
678 assertFalse( "Check metadata not created", metadataFile.exists() );
681 public void testDryRunFailure()
684 // test dry run does nothing on a run that will fail, and returns failure
686 artifactConverter = (ArtifactConverter) lookup( ArtifactConverter.ROLE, "dryrun-repository-converter" );
688 Artifact artifact = createArtifact( "test", "modified-artifact", "1.0.0" );
689 Artifact pomArtifact = createPomArtifact( artifact );
691 File sourceFile = new File( sourceRepository.getBasedir(), sourceRepository.pathOf( artifact ) );
692 File sourcePomFile = new File( sourceRepository.getBasedir(), sourceRepository.pathOf( pomArtifact ) );
693 File targetFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
694 File targetPomFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( pomArtifact ) );
696 assertTrue( "Check target file exists", targetFile.exists() );
697 assertTrue( "Check target POM exists", targetPomFile.exists() );
699 sourceFile.setLastModified( System.currentTimeMillis() );
700 sourcePomFile.setLastModified( System.currentTimeMillis() );
702 long origTime = targetFile.lastModified();
703 long origPomTime = targetPomFile.lastModified();
705 // Need to guarantee last modified is not equal
706 Thread.sleep( SLEEP_MILLIS );
708 artifactConverter.convert( artifact, targetRepository );
709 checkWarnings( artifactConverter, 2 );
711 assertHasWarningReason( artifactConverter, Messages.getString( "failure.target.already.exists" ) );
713 assertEquals( "Check unmodified", origTime, targetFile.lastModified() );
714 assertEquals( "Check unmodified", origPomTime, targetPomFile.lastModified() );
716 ArtifactRepositoryMetadata metadata = new ArtifactRepositoryMetadata( artifact );
717 File metadataFile = new File( targetRepository.getBasedir(), targetRepository
718 .pathOfRemoteRepositoryMetadata( metadata ) );
719 assertFalse( "Check metadata not created", metadataFile.exists() );
722 public void testRollbackArtifactCreated()
725 // test rollback can remove a created artifact, including checksums
727 Artifact artifact = createArtifact( "test", "rollback-created-artifact", "1.0.0" );
728 ArtifactMetadata artifactMetadata = new ArtifactRepositoryMetadata( artifact );
729 File artifactMetadataFile = new File( targetRepository.getBasedir(), targetRepository
730 .pathOfRemoteRepositoryMetadata( artifactMetadata ) );
731 FileUtils.deleteDirectory( artifactMetadataFile.getParentFile() );
733 ArtifactMetadata versionMetadata = new SnapshotArtifactRepositoryMetadata( artifact );
734 File versionMetadataFile = new File( targetRepository.getBasedir(), targetRepository
735 .pathOfRemoteRepositoryMetadata( versionMetadata ) );
737 File artifactFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
739 artifactConverter.convert( artifact, targetRepository );
740 checkWarnings( artifactConverter, 2 );
742 boolean found = false;
743 String pattern = "^" + Messages.getString( "invalid.source.pom" ).replaceFirst( "\\{0\\}", ".*" ) + "$";
744 for ( Iterator it = artifactConverter.getWarnings().values().iterator(); it.hasNext() && !found; )
746 List messages = (List) it.next();
748 for ( Iterator itmsgs = messages.iterator(); itmsgs.hasNext(); )
750 String message = (String) itmsgs.next();
751 if ( message.matches( pattern ) )
759 assertTrue( "Check failure message.", found );
761 assertFalse( "check artifact rolled back", artifactFile.exists() );
762 assertFalse( "check metadata rolled back", artifactMetadataFile.exists() );
763 assertFalse( "check metadata rolled back", versionMetadataFile.exists() );
766 public void testMultipleArtifacts()
769 // test multiple artifacts are converted
771 List artifacts = new ArrayList();
772 artifacts.add( createArtifact( "test", "artifact-one", "1.0.0" ) );
773 artifacts.add( createArtifact( "test", "artifact-two", "1.0.0" ) );
774 artifacts.add( createArtifact( "test", "artifact-three", "1.0.0" ) );
776 for ( Iterator it = artifacts.iterator(); it.hasNext(); )
778 Artifact arti = (Artifact) it.next();
779 artifactConverter.convert( arti, targetRepository );
780 checkSuccess( artifactConverter );
783 for ( Iterator i = artifacts.iterator(); i.hasNext(); )
785 Artifact artifact = (Artifact) i.next();
787 File artifactFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
788 assertTrue( "Check artifact created", artifactFile.exists() );
789 assertTrue( "Check artifact matches", FileUtils.contentEquals( artifactFile, artifact.getFile() ) );
791 artifact = createPomArtifact( artifact );
792 File pomFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
793 File expectedPomFile = getTestFile( "src/test/expected-files/converted-" + artifact.getArtifactId()
795 assertTrue( "Check POM created", pomFile.exists() );
797 compareFiles( expectedPomFile, pomFile );
801 public void testInvalidSourceArtifactMetadata()
804 // test artifact is not converted when source metadata is invalid, and returns failure
806 createModernSourceRepository();
808 Artifact artifact = createArtifact( "test", "incorrectArtifactMetadata", "1.0.0" );
809 File file = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
812 artifactConverter.convert( artifact, targetRepository );
813 checkWarnings( artifactConverter, 2 );
815 assertHasWarningReason( artifactConverter, Messages.getString( "failure.incorrect.artifactMetadata.versions" ) );
817 assertFalse( "Check artifact not created", file.exists() );
819 ArtifactRepositoryMetadata metadata = new ArtifactRepositoryMetadata( artifact );
820 File metadataFile = new File( targetRepository.getBasedir(), targetRepository
821 .pathOfRemoteRepositoryMetadata( metadata ) );
822 assertFalse( "Check metadata not created", metadataFile.exists() );
825 public void testInvalidSourceSnapshotMetadata()
828 // test artifact is not converted when source snapshot metadata is invalid and returns failure
830 createModernSourceRepository();
832 Artifact artifact = createArtifact( "test", "incorrectSnapshotMetadata", "1.0.0-20060102.030405-6" );
833 File file = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
836 artifactConverter.convert( artifact, targetRepository );
837 checkWarnings( artifactConverter, 2 );
839 assertHasWarningReason( artifactConverter, Messages.getString( "failure.incorrect.snapshotMetadata.snapshot" ) );
841 assertFalse( "Check artifact not created", file.exists() );
843 ArtifactRepositoryMetadata metadata = new ArtifactRepositoryMetadata( artifact );
844 File metadataFile = new File( targetRepository.getBasedir(), targetRepository
845 .pathOfRemoteRepositoryMetadata( metadata ) );
846 assertFalse( "Check metadata not created", metadataFile.exists() );
849 public void testMergeArtifactMetadata()
852 // test artifact level metadata is merged when it already exists on successful conversion
854 Artifact artifact = createArtifact( "test", "newversion-artifact", "1.0.1" );
855 artifactConverter.convert( artifact, targetRepository );
856 checkSuccess( artifactConverter );
858 File artifactFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
859 assertTrue( "Check artifact created", artifactFile.exists() );
860 assertTrue( "Check artifact matches", FileUtils.contentEquals( artifactFile, artifact.getFile() ) );
862 artifact = createPomArtifact( artifact );
863 File pomFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
864 File sourcePomFile = new File( sourceRepository.getBasedir(), sourceRepository.pathOf( artifact ) );
865 assertTrue( "Check POM created", pomFile.exists() );
867 compareFiles( sourcePomFile, pomFile );
869 ArtifactMetadata artifactMetadata = new ArtifactRepositoryMetadata( artifact );
870 File artifactMetadataFile = new File( targetRepository.getBasedir(), targetRepository
871 .pathOfRemoteRepositoryMetadata( artifactMetadata ) );
872 assertTrue( "Check artifact metadata created", artifactMetadataFile.exists() );
874 File expectedMetadataFile = getTestFile( "src/test/expected-files/newversion-artifact-metadata.xml" );
876 compareFiles( expectedMetadataFile, artifactMetadataFile );
879 public void testSourceAndTargetRepositoriesMatch()
882 // test that it fails if the same
884 ArtifactRepositoryFactory factory = (ArtifactRepositoryFactory) lookup( ArtifactRepositoryFactory.ROLE );
886 sourceRepository = factory.createArtifactRepository( "source", targetRepository.getUrl(), targetRepository
887 .getLayout(), null, null );
889 Artifact artifact = createArtifact( "test", "repository-artifact", "1.0" );
893 artifactConverter.convert( artifact, targetRepository );
894 fail( "Should have failed trying to convert within the same repository" );
896 catch ( ArtifactConversionException e )
899 assertEquals( "check message", Messages.getString( "exception.repositories.match" ), e.getMessage() );
900 assertNull( "Check no additional cause", e.getCause() );
904 private Artifact createArtifact( String groupId, String artifactId, String version )
906 Matcher matcher = Artifact.VERSION_FILE_PATTERN.matcher( version );
908 if ( matcher.matches() )
910 baseVersion = matcher.group( 1 ) + "-SNAPSHOT";
914 baseVersion = version;
916 return createArtifact( groupId, artifactId, baseVersion, version, "jar" );
919 private Artifact createArtifact( String groupId, String artifactId, String baseVersion, String version, String type )
921 Artifact artifact = artifactFactory.createArtifact( groupId, artifactId, version, null, type );
922 artifact.setBaseVersion( baseVersion );
923 artifact.setRepository( sourceRepository );
924 artifact.setFile( new File( sourceRepository.getBasedir(), sourceRepository.pathOf( artifact ) ) );
928 private Artifact createPomArtifact( Artifact artifact )
930 return createArtifact( artifact.getGroupId(), artifact.getArtifactId(), artifact.getBaseVersion(), artifact
931 .getVersion(), "pom" );
934 private static void compareFiles( File expectedPomFile, File pomFile )
937 String expectedContent = normalizeString( AsciiFileUtil.readFile( expectedPomFile ) );
938 String targetContent = normalizeString( AsciiFileUtil.readFile( pomFile ) );
939 assertEquals( "Check file match between " + expectedPomFile + " and " + pomFile, expectedContent, targetContent );
942 private static String normalizeString( String path )
944 return path.trim().replaceAll( "\r\n", "\n" ).replace( '\r', '\n' ).replaceAll( "<\\?xml .+\\?>", "" );
947 private void checkSuccess( ArtifactConverter converter )
949 assertNotNull( "Warnings should never be null.", converter.getWarnings() );
950 assertEquals( "Should have no warnings.", 0, countWarningMessages( converter ) );
953 private void checkWarnings( ArtifactConverter converter, int count )
955 assertNotNull( "Warnings should never be null.", converter.getWarnings() );
956 assertEquals( "Should have some warnings.", count, countWarningMessages( converter ) );
959 private int countWarningMessages( ArtifactConverter converter )
962 for ( Iterator it = converter.getWarnings().values().iterator(); it.hasNext(); )
964 List values = (List) it.next();
965 count += values.size();
970 private void assertHasWarningReason( ArtifactConverter converter, String reason )
972 assertNotNull( "Warnings should never be null.", converter.getWarnings() );
973 assertTrue( "Expecting 1 or more Warnings", countWarningMessages( converter ) > 0 );
975 for ( Iterator it = converter.getWarnings().values().iterator(); it.hasNext(); )
977 List messages = (List) it.next();
978 if ( messages.contains( reason ) )
980 /* No need to check any furthor */
985 /* didn't find it. */
987 for ( Iterator it = converter.getWarnings().entrySet().iterator(); it.hasNext(); )
989 Map.Entry entry = (Entry) it.next();
990 Artifact artifact = (Artifact) entry.getKey();
991 System.out.println( "-Artifact: " + artifact.getGroupId() + ":" + artifact.getArtifactId() + ":"
992 + artifact.getVersion() );
993 List messages = (List) entry.getValue();
994 for ( Iterator itmsgs = messages.iterator(); itmsgs.hasNext(); )
996 String message = (String) itmsgs.next();
997 System.out.println( " " + message );
1000 fail( "Unable to find message <" + reason + "> in warnings." );
1003 private void createModernSourceRepository()
1006 ArtifactRepositoryFactory factory = (ArtifactRepositoryFactory) lookup( ArtifactRepositoryFactory.ROLE );
1008 ArtifactRepositoryLayout layout = (ArtifactRepositoryLayout) lookup( ArtifactRepositoryLayout.ROLE, "default" );
1010 File sourceBase = getTestFile( "src/test/source-modern-repository" );
1011 sourceRepository = factory.createArtifactRepository( "source", sourceBase.toURL().toString(), layout, null,