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 junit.framework.TestCase;
23 import org.apache.archiva.common.plexusbridge.PlexusSisuBridge;
24 import org.apache.commons.io.FileUtils;
25 import org.apache.maven.archiva.common.utils.FileUtil;
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.junit.Before;
35 import org.junit.Test;
36 import org.junit.runner.RunWith;
37 import org.springframework.context.ApplicationContext;
38 import org.springframework.test.context.ContextConfiguration;
39 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
41 import javax.inject.Inject;
43 import java.io.IOException;
44 import java.text.SimpleDateFormat;
45 import java.util.ArrayList;
46 import java.util.List;
47 import java.util.Locale;
49 import java.util.regex.Matcher;
52 * LegacyToDefaultConverterTest
56 @RunWith( SpringJUnit4ClassRunner.class )
57 @ContextConfiguration( locations = { "classpath*:/META-INF/spring-context.xml", "classpath:/spring-context.xml" } )
58 public class LegacyToDefaultConverterTest
61 private ArtifactRepository sourceRepository;
63 private ArtifactRepository targetRepository;
65 private ArtifactConverter artifactConverter;
67 private ArtifactFactory artifactFactory;
70 private PlexusSisuBridge plexusSisuBridge;
73 private ApplicationContext applicationContext;
75 private static final int SLEEP_MILLIS = 100;
83 ArtifactRepositoryFactory factory = plexusSisuBridge.lookup( ArtifactRepositoryFactory.class );
85 Map<String, ArtifactRepositoryLayout> layoutsMap = plexusSisuBridge.lookupMap( ArtifactRepositoryLayout.class );
87 System.out.println("hints " + layoutsMap.keySet().toString() );
89 ArtifactRepositoryLayout layout = plexusSisuBridge.lookup( ArtifactRepositoryLayout.class, "legacy" );
91 File sourceBase = getTestFile( "src/test/source-repository" );
93 factory.createArtifactRepository( "source", sourceBase.toURL().toString(), layout, null, null );
95 layout = plexusSisuBridge.lookup( ArtifactRepositoryLayout.class, "default" );
97 File targetBase = getTestFile( "target/test-target-repository" );
98 copyDirectoryStructure( getTestFile( "src/test/target-repository" ), targetBase );
101 factory.createArtifactRepository( "target", targetBase.toURL().toString(), layout, null, null );
104 applicationContext.getBean( "artifactConverter#legacy-to-default", ArtifactConverter.class );
106 artifactConverter.clearWarnings();
107 artifactFactory = (ArtifactFactory) plexusSisuBridge.lookup( ArtifactFactory.class );
110 public static File getTestFile( String path )
112 return new File( FileUtil.getBasedir(), path );
115 private void copyDirectoryStructure( File sourceDirectory, File destinationDirectory )
118 if ( !sourceDirectory.exists() )
120 throw new IOException( "Source directory doesn't exists (" + sourceDirectory.getAbsolutePath() + ")." );
123 File[] files = sourceDirectory.listFiles();
125 String sourcePath = sourceDirectory.getAbsolutePath();
127 for ( int i = 0; i < files.length; i++ )
129 File file = files[i];
131 String dest = file.getAbsolutePath();
133 dest = dest.substring( sourcePath.length() + 1 );
135 File destination = new File( destinationDirectory, dest );
139 destination = destination.getParentFile();
141 FileUtils.copyFileToDirectory( file, destination );
143 else if ( file.isDirectory() )
145 if ( !".svn".equals( file.getName() ) )
147 if ( !destination.exists() && !destination.mkdirs() )
149 throw new IOException(
150 "Could not create destination directory '" + destination.getAbsolutePath() + "'." );
152 copyDirectoryStructure( file, destination );
157 throw new IOException( "Unknown file type: " + file.getAbsolutePath() );
163 public void testV4PomConvert()
166 // test that it is copied as is
168 Artifact artifact = createArtifact( "test", "v4artifact", "1.0.0" );
169 ArtifactMetadata artifactMetadata = new ArtifactRepositoryMetadata( artifact );
170 File artifactMetadataFile = new File( targetRepository.getBasedir(),
171 targetRepository.pathOfRemoteRepositoryMetadata( artifactMetadata ) );
172 artifactMetadataFile.delete();
174 ArtifactMetadata versionMetadata = new SnapshotArtifactRepositoryMetadata( artifact );
175 File versionMetadataFile = new File( targetRepository.getBasedir(),
176 targetRepository.pathOfRemoteRepositoryMetadata( versionMetadata ) );
177 versionMetadataFile.delete();
179 File artifactFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
180 artifactFile.delete();
182 artifactConverter.convert( artifact, targetRepository );
183 checkSuccess( artifactConverter );
185 assertTrue( "Check artifact created", artifactFile.exists() );
186 assertTrue( "Check artifact matches", FileUtils.contentEquals( artifactFile, artifact.getFile() ) );
188 artifact = createPomArtifact( artifact );
189 File pomFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
190 File sourcePomFile = new File( sourceRepository.getBasedir(), sourceRepository.pathOf( artifact ) );
191 assertTrue( "Check POM created", pomFile.exists() );
193 compareFiles( sourcePomFile, pomFile );
195 assertTrue( "Check artifact metadata created", artifactMetadataFile.exists() );
197 File expectedMetadataFile = getTestFile( "src/test/expected-files/v4-artifact-metadata.xml" );
199 compareFiles( expectedMetadataFile, artifactMetadataFile );
201 assertTrue( "Check snapshot metadata created", versionMetadataFile.exists() );
203 expectedMetadataFile = getTestFile( "src/test/expected-files/v4-version-metadata.xml" );
205 compareFiles( expectedMetadataFile, versionMetadataFile );
209 public void testV3PomConvert()
212 // test that the pom is coverted
214 Artifact artifact = createArtifact( "test", "v3artifact", "1.0.0" );
215 ArtifactMetadata artifactMetadata = new ArtifactRepositoryMetadata( artifact );
216 File artifactMetadataFile = new File( targetRepository.getBasedir(),
217 targetRepository.pathOfRemoteRepositoryMetadata( artifactMetadata ) );
218 artifactMetadataFile.delete();
220 ArtifactMetadata versionMetadata = new SnapshotArtifactRepositoryMetadata( artifact );
221 File versionMetadataFile = new File( targetRepository.getBasedir(),
222 targetRepository.pathOfRemoteRepositoryMetadata( versionMetadata ) );
223 versionMetadataFile.delete();
225 artifactConverter.convert( artifact, targetRepository );
226 checkSuccess( artifactConverter );
228 File artifactFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
229 assertTrue( "Check artifact created", artifactFile.exists() );
230 assertTrue( "Check artifact matches", FileUtils.contentEquals( artifactFile, artifact.getFile() ) );
232 artifact = createPomArtifact( artifact );
233 File pomFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
234 File expectedPomFile = getTestFile( "src/test/expected-files/converted-v3.pom" );
235 assertTrue( "Check POM created", pomFile.exists() );
237 compareFiles( expectedPomFile, pomFile );
239 assertTrue( "Check artifact metadata created", artifactMetadataFile.exists() );
241 File expectedMetadataFile = getTestFile( "src/test/expected-files/v3-artifact-metadata.xml" );
243 compareFiles( expectedMetadataFile, artifactMetadataFile );
245 assertTrue( "Check snapshot metadata created", versionMetadataFile.exists() );
247 expectedMetadataFile = getTestFile( "src/test/expected-files/v3-version-metadata.xml" );
249 compareFiles( expectedMetadataFile, versionMetadataFile );
253 public void testV3PomConvertWithRelocation()
256 Artifact artifact = createArtifact( "test", "relocated-v3artifact", "1.0.0" );
257 ArtifactMetadata artifactMetadata = new ArtifactRepositoryMetadata( artifact );
258 File artifactMetadataFile = new File( targetRepository.getBasedir(),
259 targetRepository.pathOfRemoteRepositoryMetadata( artifactMetadata ) );
260 artifactMetadataFile.delete();
262 ArtifactMetadata versionMetadata = new SnapshotArtifactRepositoryMetadata( artifact );
263 File versionMetadataFile = new File( targetRepository.getBasedir(),
264 targetRepository.pathOfRemoteRepositoryMetadata( versionMetadata ) );
265 versionMetadataFile.delete();
267 artifactConverter.convert( artifact, targetRepository );
268 //checkSuccess(); --> commented until MNG-2100 is fixed
270 File artifactFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
271 assertTrue( "Check if relocated artifact created", artifactFile.exists() );
272 assertTrue( "Check if relocated artifact matches",
273 FileUtils.contentEquals( artifactFile, artifact.getFile() ) );
274 Artifact pomArtifact = createArtifact( "relocated-test", "relocated-v3artifact", "1.0.0", "1.0.0", "pom" );
275 File pomFile = getTestFile( "src/test/expected-files/" + targetRepository.pathOf( pomArtifact ) );
276 File testFile = getTestFile( "target/test-target-repository/" + targetRepository.pathOf( pomArtifact ) );
277 compareFiles( pomFile, testFile );
279 Artifact orig = createArtifact( "test", "relocated-v3artifact", "1.0.0", "1.0.0", "pom" );
280 artifactFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( orig ) );
281 assertTrue( "Check if relocation artifact pom is created", artifactFile.exists() );
282 testFile = getTestFile( "src/test/expected-files/" + targetRepository.pathOf( orig ) );
283 compareFiles( artifactFile, testFile );
287 public void testV3PomWarningsOnConvert()
290 // test that the pom is converted but that warnings are reported
292 Artifact artifact = createArtifact( "test", "v3-warnings-artifact", "1.0.0" );
293 ArtifactMetadata artifactMetadata = new ArtifactRepositoryMetadata( artifact );
294 File artifactMetadataFile = new File( targetRepository.getBasedir(),
295 targetRepository.pathOfRemoteRepositoryMetadata( artifactMetadata ) );
296 artifactMetadataFile.delete();
298 ArtifactMetadata versionMetadata = new SnapshotArtifactRepositoryMetadata( artifact );
299 File versionMetadataFile = new File( targetRepository.getBasedir(),
300 targetRepository.pathOfRemoteRepositoryMetadata( versionMetadata ) );
301 versionMetadataFile.delete();
303 artifactConverter.convert( artifact, targetRepository );
304 checkWarnings( artifactConverter, 2 );
306 File artifactFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
307 assertTrue( "Check artifact created", artifactFile.exists() );
308 assertTrue( "Check artifact matches", FileUtils.contentEquals( artifactFile, artifact.getFile() ) );
310 artifact = createPomArtifact( artifact );
311 File pomFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
312 File expectedPomFile = getTestFile( "src/test/expected-files/converted-v3-warnings.pom" );
313 assertTrue( "Check POM created", pomFile.exists() );
315 compareFiles( expectedPomFile, pomFile );
317 // TODO: check 2 warnings (extend and versions) matched on i18n key
320 private void doTestV4SnapshotPomConvert( String version, String expectedMetadataFileName )
323 // test that it is copied as is
325 Artifact artifact = createArtifact( "test", "v4artifact", version );
326 ArtifactMetadata artifactMetadata = new ArtifactRepositoryMetadata( artifact );
327 File artifactMetadataFile = new File( targetRepository.getBasedir(),
328 targetRepository.pathOfRemoteRepositoryMetadata( artifactMetadata ) );
329 artifactMetadataFile.delete();
331 ArtifactMetadata snapshotMetadata = new SnapshotArtifactRepositoryMetadata( artifact );
332 File snapshotMetadataFile = new File( targetRepository.getBasedir(),
333 targetRepository.pathOfRemoteRepositoryMetadata( snapshotMetadata ) );
334 snapshotMetadataFile.delete();
336 artifactConverter.convert( artifact, targetRepository );
337 checkSuccess( artifactConverter );
339 File artifactFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
340 assertTrue( "Check artifact created", artifactFile.exists() );
341 assertTrue( "Check artifact matches", FileUtils.contentEquals( artifactFile, artifact.getFile() ) );
343 artifact = createPomArtifact( artifact );
344 File pomFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
345 File sourcePomFile = new File( sourceRepository.getBasedir(), sourceRepository.pathOf( artifact ) );
346 assertTrue( "Check POM created", pomFile.exists() );
348 compareFiles( sourcePomFile, pomFile );
350 assertTrue( "Check artifact metadata created", artifactMetadataFile.exists() );
352 File expectedMetadataFile = getTestFile( "src/test/expected-files/v4-snapshot-artifact-metadata.xml" );
354 compareFiles( expectedMetadataFile, artifactMetadataFile );
356 assertTrue( "Check snapshot metadata created", snapshotMetadataFile.exists() );
358 expectedMetadataFile = getTestFile( expectedMetadataFileName );
360 compareFiles( expectedMetadataFile, snapshotMetadataFile );
364 public void testV3SnapshotPomConvert()
367 // test that the pom is coverted
369 Artifact artifact = createArtifact( "test", "v3artifact", "1.0.0-SNAPSHOT" );
370 ArtifactMetadata artifactMetadata = new ArtifactRepositoryMetadata( artifact );
371 File artifactMetadataFile = new File( targetRepository.getBasedir(),
372 targetRepository.pathOfRemoteRepositoryMetadata( artifactMetadata ) );
373 artifactMetadataFile.delete();
375 ArtifactMetadata snapshotMetadata = new SnapshotArtifactRepositoryMetadata( artifact );
376 File snapshotMetadataFile = new File( targetRepository.getBasedir(),
377 targetRepository.pathOfRemoteRepositoryMetadata( snapshotMetadata ) );
378 snapshotMetadataFile.delete();
380 artifactConverter.convert( artifact, targetRepository );
381 checkSuccess( artifactConverter );
383 File artifactFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
384 assertTrue( "Check artifact created", artifactFile.exists() );
385 assertTrue( "Check artifact matches", FileUtils.contentEquals( artifactFile, artifact.getFile() ) );
387 artifact = createPomArtifact( artifact );
388 File pomFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
389 File expectedPomFile = getTestFile( "src/test/expected-files/converted-v3-snapshot.pom" );
390 assertTrue( "Check POM created", pomFile.exists() );
392 compareFiles( expectedPomFile, pomFile );
394 assertTrue( "Check artifact metadata created", artifactMetadataFile.exists() );
396 File expectedMetadataFile = getTestFile( "src/test/expected-files/v3-snapshot-artifact-metadata.xml" );
398 compareFiles( expectedMetadataFile, artifactMetadataFile );
400 assertTrue( "Check snapshot metadata created", snapshotMetadataFile.exists() );
402 expectedMetadataFile = getTestFile( "src/test/expected-files/v3-snapshot-metadata.xml" );
404 compareFiles( expectedMetadataFile, snapshotMetadataFile );
408 public void testV4SnapshotPomConvert()
411 doTestV4SnapshotPomConvert( "1.0.0-SNAPSHOT", "src/test/expected-files/v4-snapshot-metadata.xml" );
417 public void testV4TimestampedSnapshotPomConvert()
420 doTestV4SnapshotPomConvert( "1.0.0-20060111.120115-1",
421 "src/test/expected-files/v4-timestamped-snapshot-metadata.xml" );
427 public void testMavenOnePluginConversion()
431 createArtifact( "org.apache.maven.plugins", "maven-foo-plugin", "1.0", "1.0", "maven-plugin" );
433 new File( FileUtil.getBasedir(), "src/test/source-repository/test/plugins/maven-foo-plugin-1.0.jar" ) );
434 artifactConverter.convert( artifact, targetRepository );
435 // There is a warning but I can't figure out how to look at it. Eyeballing the results it appears
436 // the plugin is being coverted correctly.
439 File artifactFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
440 assertTrue( "Check artifact created", artifactFile.exists() );
441 assertTrue( "Check artifact matches", FileUtils.contentEquals( artifactFile, artifact.getFile() ) );
444 The POM isn't needed for Maven 1.x plugins but the raw conversion for
446 artifact = createPomArtifact( artifact );
447 File pomFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
448 File expectedPomFile = getTestFile( "src/test/expected-files/maven-foo-plugin-1.0.pom" );
449 assertTrue( "Check POM created", pomFile.exists() );
450 compareFiles( expectedPomFile, pomFile );
455 public void testV3TimestampedSnapshotPomConvert()
458 // test that the pom is coverted
460 Artifact artifact = createArtifact( "test", "v3artifact", "1.0.0-20060105.130101-3" );
461 ArtifactMetadata artifactMetadata = new ArtifactRepositoryMetadata( artifact );
462 File artifactMetadataFile = new File( targetRepository.getBasedir(),
463 targetRepository.pathOfRemoteRepositoryMetadata( artifactMetadata ) );
464 artifactMetadataFile.delete();
466 ArtifactMetadata snapshotMetadata = new SnapshotArtifactRepositoryMetadata( artifact );
467 File snapshotMetadataFile = new File( targetRepository.getBasedir(),
468 targetRepository.pathOfRemoteRepositoryMetadata( snapshotMetadata ) );
469 snapshotMetadataFile.delete();
471 artifactConverter.convert( artifact, targetRepository );
472 checkSuccess( artifactConverter );
474 File artifactFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
475 assertTrue( "Check artifact created", artifactFile.exists() );
476 assertTrue( "Check artifact matches", FileUtils.contentEquals( artifactFile, artifact.getFile() ) );
478 artifact = createPomArtifact( artifact );
479 File pomFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
480 File expectedPomFile = getTestFile( "src/test/expected-files/converted-v3-timestamped-snapshot.pom" );
481 assertTrue( "Check POM created", pomFile.exists() );
483 compareFiles( expectedPomFile, pomFile );
485 assertTrue( "Check artifact snapshotMetadata created", artifactMetadataFile.exists() );
487 File expectedMetadataFile = getTestFile( "src/test/expected-files/v3-snapshot-artifact-metadata.xml" );
489 compareFiles( expectedMetadataFile, artifactMetadataFile );
491 assertTrue( "Check snapshot snapshotMetadata created", snapshotMetadataFile.exists() );
493 expectedMetadataFile = getTestFile( "src/test/expected-files/v3-timestamped-snapshot-metadata.xml" );
495 compareFiles( expectedMetadataFile, snapshotMetadataFile );
499 public void testNoPomConvert()
502 // test that a POM is not created when there was none at the source
504 Artifact artifact = createArtifact( "test", "noPomArtifact", "1.0.0" );
505 artifactConverter.convert( artifact, targetRepository );
506 checkWarnings( artifactConverter, 1 );
508 assertHasWarningReason( artifactConverter, Messages.getString( "warning.missing.pom" ) );
510 File artifactFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
511 assertTrue( "Check artifact created", artifactFile.exists() );
512 assertTrue( "Check artifact matches", FileUtils.contentEquals( artifactFile, artifact.getFile() ) );
514 artifact = createPomArtifact( artifact );
515 File pomFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
516 File sourcePomFile = new File( sourceRepository.getBasedir(), sourceRepository.pathOf( artifact ) );
518 assertFalse( "Check no POM created", pomFile.exists() );
519 assertFalse( "No source POM", sourcePomFile.exists() );
523 public void testIncorrectSourceChecksumMd5()
526 // test that it fails when the source md5 is wrong
528 Artifact artifact = createArtifact( "test", "incorrectMd5Artifact", "1.0.0" );
529 File file = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
532 artifactConverter.convert( artifact, targetRepository );
533 checkWarnings( artifactConverter, 2 );
535 assertHasWarningReason( artifactConverter, Messages.getString( "failure.incorrect.md5" ) );
537 assertFalse( "Check artifact not created", file.exists() );
539 ArtifactRepositoryMetadata metadata = new ArtifactRepositoryMetadata( artifact );
541 new File( targetRepository.getBasedir(), targetRepository.pathOfRemoteRepositoryMetadata( metadata ) );
542 assertFalse( "Check metadata not created", metadataFile.exists() );
546 public void testIncorrectSourceChecksumSha1()
549 // test that it fails when the source sha1 is wrong
551 Artifact artifact = createArtifact( "test", "incorrectSha1Artifact", "1.0.0" );
552 File file = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
555 artifactConverter.convert( artifact, targetRepository );
556 checkWarnings( artifactConverter, 2 );
558 assertHasWarningReason( artifactConverter, Messages.getString( "failure.incorrect.sha1" ) );
560 assertFalse( "Check artifact not created", file.exists() );
562 ArtifactRepositoryMetadata metadata = new ArtifactRepositoryMetadata( artifact );
564 new File( targetRepository.getBasedir(), targetRepository.pathOfRemoteRepositoryMetadata( metadata ) );
565 assertFalse( "Check metadata not created", metadataFile.exists() );
569 public void testUnmodifiedArtifact()
570 throws Exception, InterruptedException
572 // test the unmodified artifact is untouched
574 Artifact artifact = createArtifact( "test", "unmodified-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 checkSuccess( artifactConverter );
597 compareFiles( sourceFile, targetFile );
598 compareFiles( sourcePomFile, targetPomFile );
600 assertEquals( "Check artifact unmodified", origTime, targetFile.lastModified() );
601 assertEquals( "Check POM unmodified", origPomTime, targetPomFile.lastModified() );
605 public void testModifedArtifactFails()
608 // test that it fails when the source artifact has changed and is different to the existing artifact in the
611 Artifact artifact = createArtifact( "test", "modified-artifact", "1.0.0" );
612 Artifact pomArtifact = createPomArtifact( artifact );
614 File sourceFile = new File( sourceRepository.getBasedir(), sourceRepository.pathOf( artifact ) );
615 File sourcePomFile = new File( sourceRepository.getBasedir(), sourceRepository.pathOf( pomArtifact ) );
616 File targetFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
617 File targetPomFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( pomArtifact ) );
619 assertTrue( "Check target file exists", targetFile.exists() );
620 assertTrue( "Check target POM exists", targetPomFile.exists() );
622 sourceFile.setLastModified( System.currentTimeMillis() );
623 sourcePomFile.setLastModified( System.currentTimeMillis() );
625 long origTime = targetFile.lastModified();
626 long origPomTime = targetPomFile.lastModified();
628 // Need to guarantee last modified is not equal
629 Thread.sleep( SLEEP_MILLIS );
631 artifactConverter.convert( artifact, targetRepository );
632 checkWarnings( artifactConverter, 2 );
634 assertHasWarningReason( artifactConverter, Messages.getString( "failure.target.already.exists" ) );
636 assertEquals( "Check unmodified", origTime, targetFile.lastModified() );
637 assertEquals( "Check unmodified", origPomTime, targetPomFile.lastModified() );
639 ArtifactRepositoryMetadata metadata = new ArtifactRepositoryMetadata( artifact );
641 new File( targetRepository.getBasedir(), targetRepository.pathOfRemoteRepositoryMetadata( metadata ) );
642 assertFalse( "Check metadata not created", metadataFile.exists() );
646 public void testForcedUnmodifiedArtifact()
649 // test unmodified artifact is still converted when set to force
652 applicationContext.getBean( "artifactConverter#force-repository-converter", ArtifactConverter.class );
654 Artifact artifact = createArtifact( "test", "unmodified-artifact", "1.0.0" );
655 Artifact pomArtifact = createPomArtifact( artifact );
657 File sourceFile = new File( sourceRepository.getBasedir(), sourceRepository.pathOf( artifact ) );
658 File sourcePomFile = new File( sourceRepository.getBasedir(), sourceRepository.pathOf( pomArtifact ) );
659 File targetFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
660 File targetPomFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( pomArtifact ) );
662 SimpleDateFormat dateFormat = new SimpleDateFormat( "yyyy-MM-dd", Locale.getDefault() );
663 long origTime = dateFormat.parse( "2006-03-03" ).getTime();
664 targetFile.setLastModified( origTime );
665 targetPomFile.setLastModified( origTime );
667 sourceFile.setLastModified( dateFormat.parse( "2006-01-01" ).getTime() );
668 sourcePomFile.setLastModified( dateFormat.parse( "2006-02-02" ).getTime() );
670 artifactConverter.convert( artifact, targetRepository );
671 checkSuccess( artifactConverter );
673 compareFiles( sourceFile, targetFile );
674 compareFiles( sourcePomFile, targetPomFile );
676 assertFalse( "Check modified", origTime == targetFile.lastModified() );
677 assertFalse( "Check modified", origTime == targetPomFile.lastModified() );
679 ArtifactRepositoryMetadata metadata = new ArtifactRepositoryMetadata( artifact );
681 new File( targetRepository.getBasedir(), targetRepository.pathOfRemoteRepositoryMetadata( metadata ) );
682 assertTrue( "Check metadata created", metadataFile.exists() );
686 public void testDryRunSuccess()
689 // test dry run does nothing on a run that will be successful, and returns success
692 applicationContext.getBean( "artifactConverter#dryrun-repository-converter", ArtifactConverter.class );
694 Artifact artifact = createArtifact( "test", "dryrun-artifact", "1.0.0" );
695 Artifact pomArtifact = createPomArtifact( artifact );
697 File sourceFile = new File( sourceRepository.getBasedir(), sourceRepository.pathOf( artifact ) );
698 File sourcePomFile = new File( sourceRepository.getBasedir(), sourceRepository.pathOf( pomArtifact ) );
699 File targetFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
700 File targetPomFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( pomArtifact ) );
702 artifactConverter.convert( artifact, targetRepository );
703 checkSuccess( artifactConverter );
705 assertTrue( "Check source file exists", sourceFile.exists() );
706 assertTrue( "Check source POM exists", sourcePomFile.exists() );
708 assertFalse( "Check target file doesn't exist", targetFile.exists() );
709 assertFalse( "Check target POM doesn't exist", targetPomFile.exists() );
711 ArtifactRepositoryMetadata metadata = new ArtifactRepositoryMetadata( artifact );
713 new File( targetRepository.getBasedir(), targetRepository.pathOfRemoteRepositoryMetadata( metadata ) );
714 assertFalse( "Check metadata not created", metadataFile.exists() );
718 public void testDryRunFailure()
721 // test dry run does nothing on a run that will fail, and returns failure
724 applicationContext.getBean( "artifactConverter#dryrun-repository-converter", ArtifactConverter.class );
726 Artifact artifact = createArtifact( "test", "modified-artifact", "1.0.0" );
727 Artifact pomArtifact = createPomArtifact( artifact );
729 File sourceFile = new File( sourceRepository.getBasedir(), sourceRepository.pathOf( artifact ) );
730 File sourcePomFile = new File( sourceRepository.getBasedir(), sourceRepository.pathOf( pomArtifact ) );
731 File targetFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
732 File targetPomFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( pomArtifact ) );
734 assertTrue( "Check target file exists", targetFile.exists() );
735 assertTrue( "Check target POM exists", targetPomFile.exists() );
737 sourceFile.setLastModified( System.currentTimeMillis() );
738 sourcePomFile.setLastModified( System.currentTimeMillis() );
740 long origTime = targetFile.lastModified();
741 long origPomTime = targetPomFile.lastModified();
743 // Need to guarantee last modified is not equal
744 Thread.sleep( SLEEP_MILLIS );
746 artifactConverter.convert( artifact, targetRepository );
747 checkWarnings( artifactConverter, 2 );
749 assertHasWarningReason( artifactConverter, Messages.getString( "failure.target.already.exists" ) );
751 assertEquals( "Check unmodified", origTime, targetFile.lastModified() );
752 assertEquals( "Check unmodified", origPomTime, targetPomFile.lastModified() );
754 ArtifactRepositoryMetadata metadata = new ArtifactRepositoryMetadata( artifact );
756 new File( targetRepository.getBasedir(), targetRepository.pathOfRemoteRepositoryMetadata( metadata ) );
757 assertFalse( "Check metadata not created", metadataFile.exists() );
761 public void testRollbackArtifactCreated()
764 // test rollback can remove a created artifact, including checksums
766 Artifact artifact = createArtifact( "test", "rollback-created-artifact", "1.0.0" );
767 ArtifactMetadata artifactMetadata = new ArtifactRepositoryMetadata( artifact );
768 File artifactMetadataFile = new File( targetRepository.getBasedir(),
769 targetRepository.pathOfRemoteRepositoryMetadata( artifactMetadata ) );
770 FileUtils.deleteDirectory( artifactMetadataFile.getParentFile() );
772 ArtifactMetadata versionMetadata = new SnapshotArtifactRepositoryMetadata( artifact );
773 File versionMetadataFile = new File( targetRepository.getBasedir(),
774 targetRepository.pathOfRemoteRepositoryMetadata( versionMetadata ) );
776 File artifactFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
778 artifactConverter.convert( artifact, targetRepository );
779 checkWarnings( artifactConverter, 2 );
781 boolean found = false;
782 String pattern = "^" + Messages.getString( "invalid.source.pom" ).replaceFirst( "\\{0\\}", ".*" ) + "$";
783 for ( List<String> messages : artifactConverter.getWarnings().values() )
785 for ( String message : messages )
787 if ( message.matches( pattern ) )
800 assertTrue( "Check failure message.", found );
802 assertFalse( "check artifact rolled back", artifactFile.exists() );
803 assertFalse( "check metadata rolled back", artifactMetadataFile.exists() );
804 assertFalse( "check metadata rolled back", versionMetadataFile.exists() );
808 public void testMultipleArtifacts()
811 // test multiple artifacts are converted
813 List<Artifact> artifacts = new ArrayList<Artifact>();
814 artifacts.add( createArtifact( "test", "artifact-one", "1.0.0" ) );
815 artifacts.add( createArtifact( "test", "artifact-two", "1.0.0" ) );
816 artifacts.add( createArtifact( "test", "artifact-three", "1.0.0" ) );
818 for ( Artifact artifact : artifacts )
820 artifactConverter.convert( artifact, targetRepository );
821 checkSuccess( artifactConverter );
824 for ( Artifact artifact : artifacts )
826 File artifactFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
827 assertTrue( "Check artifact created", artifactFile.exists() );
828 assertTrue( "Check artifact matches", FileUtils.contentEquals( artifactFile, artifact.getFile() ) );
830 artifact = createPomArtifact( artifact );
831 File pomFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
832 File expectedPomFile =
833 getTestFile( "src/test/expected-files/converted-" + artifact.getArtifactId() + ".pom" );
834 assertTrue( "Check POM created", pomFile.exists() );
836 compareFiles( expectedPomFile, pomFile );
841 public void testInvalidSourceArtifactMetadata()
844 // test artifact is not converted when source metadata is invalid, and returns failure
846 createModernSourceRepository();
848 Artifact artifact = createArtifact( "test", "incorrectArtifactMetadata", "1.0.0" );
849 File file = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
852 artifactConverter.convert( artifact, targetRepository );
853 checkWarnings( artifactConverter, 2 );
855 assertHasWarningReason( artifactConverter,
856 Messages.getString( "failure.incorrect.artifactMetadata.versions" ) );
858 assertFalse( "Check artifact not created", file.exists() );
860 ArtifactRepositoryMetadata metadata = new ArtifactRepositoryMetadata( artifact );
862 new File( targetRepository.getBasedir(), targetRepository.pathOfRemoteRepositoryMetadata( metadata ) );
863 assertFalse( "Check metadata not created", metadataFile.exists() );
867 public void testInvalidSourceSnapshotMetadata()
870 // test artifact is not converted when source snapshot metadata is invalid and returns failure
872 createModernSourceRepository();
874 Artifact artifact = createArtifact( "test", "incorrectSnapshotMetadata", "1.0.0-20060102.030405-6" );
875 File file = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
878 artifactConverter.convert( artifact, targetRepository );
879 checkWarnings( artifactConverter, 2 );
881 assertHasWarningReason( artifactConverter,
882 Messages.getString( "failure.incorrect.snapshotMetadata.snapshot" ) );
884 assertFalse( "Check artifact not created", file.exists() );
886 ArtifactRepositoryMetadata metadata = new ArtifactRepositoryMetadata( artifact );
888 new File( targetRepository.getBasedir(), targetRepository.pathOfRemoteRepositoryMetadata( metadata ) );
889 assertFalse( "Check metadata not created", metadataFile.exists() );
893 public void testMergeArtifactMetadata()
896 // test artifact level metadata is merged when it already exists on successful conversion
898 Artifact artifact = createArtifact( "test", "newversion-artifact", "1.0.1" );
899 artifactConverter.convert( artifact, targetRepository );
900 checkSuccess( artifactConverter );
902 File artifactFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
903 assertTrue( "Check artifact created", artifactFile.exists() );
904 assertTrue( "Check artifact matches", FileUtils.contentEquals( artifactFile, artifact.getFile() ) );
906 artifact = createPomArtifact( artifact );
907 File pomFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
908 File sourcePomFile = new File( sourceRepository.getBasedir(), sourceRepository.pathOf( artifact ) );
909 assertTrue( "Check POM created", pomFile.exists() );
911 compareFiles( sourcePomFile, pomFile );
913 ArtifactMetadata artifactMetadata = new ArtifactRepositoryMetadata( artifact );
914 File artifactMetadataFile = new File( targetRepository.getBasedir(),
915 targetRepository.pathOfRemoteRepositoryMetadata( artifactMetadata ) );
916 assertTrue( "Check artifact metadata created", artifactMetadataFile.exists() );
918 File expectedMetadataFile = getTestFile( "src/test/expected-files/newversion-artifact-metadata.xml" );
920 compareFiles( expectedMetadataFile, artifactMetadataFile );
924 public void testSourceAndTargetRepositoriesMatch()
927 // test that it fails if the same
929 ArtifactRepositoryFactory factory = plexusSisuBridge.lookup( ArtifactRepositoryFactory.class );
932 factory.createArtifactRepository( "source", targetRepository.getUrl(), targetRepository.getLayout(), null,
935 Artifact artifact = createArtifact( "test", "repository-artifact", "1.0" );
939 artifactConverter.convert( artifact, targetRepository );
940 fail( "Should have failed trying to convert within the same repository" );
942 catch ( ArtifactConversionException e )
945 assertEquals( "check message", Messages.getString( "exception.repositories.match" ), e.getMessage() );
946 assertNull( "Check no additional cause", e.getCause() );
950 private Artifact createArtifact( String groupId, String artifactId, String version )
952 Matcher matcher = Artifact.VERSION_FILE_PATTERN.matcher( version );
954 if ( matcher.matches() )
956 baseVersion = matcher.group( 1 ) + "-SNAPSHOT";
960 baseVersion = version;
962 return createArtifact( groupId, artifactId, baseVersion, version, "jar" );
965 private Artifact createArtifact( String groupId, String artifactId, String baseVersion, String version,
968 Artifact artifact = artifactFactory.createArtifact( groupId, artifactId, version, null, type );
969 artifact.setBaseVersion( baseVersion );
970 artifact.setRepository( sourceRepository );
971 artifact.setFile( new File( sourceRepository.getBasedir(), sourceRepository.pathOf( artifact ) ) );
975 private Artifact createPomArtifact( Artifact artifact )
977 return createArtifact( artifact.getGroupId(), artifact.getArtifactId(), artifact.getBaseVersion(),
978 artifact.getVersion(), "pom" );
981 private static void compareFiles( File expectedPomFile, File pomFile )
984 String expectedContent =
985 normalizeString( org.apache.commons.io.FileUtils.readFileToString( expectedPomFile, null ) );
986 String targetContent = normalizeString( org.apache.commons.io.FileUtils.readFileToString( pomFile, null ) );
987 assertEquals( "Check file match between " + expectedPomFile + " and " + pomFile, expectedContent,
991 private static String normalizeString( String path )
993 return path.trim().replaceAll( "\r\n", "\n" ).replace( '\r', '\n' ).replaceAll( "<\\?xml .+\\?>",
994 "" ).replaceAll( "^\\s+", "" );
997 private void checkSuccess( ArtifactConverter converter )
999 assertNotNull( "Warnings should never be null.", converter.getWarnings() );
1000 assertEquals( "Should have no warnings.", 0, countWarningMessages( converter ) );
1003 private void checkWarnings( ArtifactConverter converter, int count )
1005 assertNotNull( "Warnings should never be null.", converter.getWarnings() );
1006 assertEquals( "Should have some warnings.", count, countWarningMessages( converter ) );
1009 private int countWarningMessages( ArtifactConverter converter )
1012 for ( List<String> values : converter.getWarnings().values() )
1014 count += values.size();
1019 private void assertHasWarningReason( ArtifactConverter converter, String reason )
1021 assertNotNull( "Warnings should never be null.", converter.getWarnings() );
1022 assertTrue( "Expecting 1 or more Warnings", countWarningMessages( converter ) > 0 );
1024 for ( List<String> messages : converter.getWarnings().values() )
1026 if ( messages.contains( reason ) )
1028 /* No need to check any further */
1033 /* didn't find it. */
1035 for ( Map.Entry<Artifact, List<String>> entry : converter.getWarnings().entrySet() )
1037 Artifact artifact = (Artifact) entry.getKey();
1039 "-Artifact: " + artifact.getGroupId() + ":" + artifact.getArtifactId() + ":" + artifact.getVersion() );
1040 List<String> messages = entry.getValue();
1041 for ( String message : messages )
1043 System.out.println( " " + message );
1046 fail( "Unable to find message <" + reason + "> in warnings." );
1049 private void createModernSourceRepository()
1052 ArtifactRepositoryFactory factory = plexusSisuBridge.lookup( ArtifactRepositoryFactory.class );
1054 ArtifactRepositoryLayout layout = plexusSisuBridge.lookup( ArtifactRepositoryLayout.class, "default" );
1056 File sourceBase = getTestFile( "src/test/source-modern-repository" );
1058 factory.createArtifactRepository( "source", sourceBase.toURL().toString(), layout, null, null );