1 package org.apache.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.archiva.common.utils.FileUtil;
25 import org.apache.archiva.test.utils.ArchivaSpringJUnit4ClassRunner;
26 import org.apache.commons.io.FileUtils;
27 import org.apache.maven.artifact.Artifact;
28 import org.apache.maven.artifact.factory.ArtifactFactory;
29 import org.apache.maven.artifact.metadata.ArtifactMetadata;
30 import org.apache.maven.artifact.repository.ArtifactRepository;
31 import org.apache.maven.artifact.repository.ArtifactRepositoryFactory;
32 import org.apache.maven.artifact.repository.layout.ArtifactRepositoryLayout;
33 import org.apache.maven.artifact.repository.metadata.ArtifactRepositoryMetadata;
34 import org.apache.maven.artifact.repository.metadata.SnapshotArtifactRepositoryMetadata;
35 import org.junit.Before;
36 import org.junit.Test;
37 import org.junit.runner.RunWith;
38 import org.springframework.context.ApplicationContext;
39 import org.springframework.test.context.ContextConfiguration;
41 import javax.inject.Inject;
43 import java.io.IOException;
44 import java.nio.charset.Charset;
45 import java.text.SimpleDateFormat;
46 import java.util.ArrayList;
47 import java.util.List;
48 import java.util.Locale;
50 import java.util.regex.Matcher;
53 * LegacyToDefaultConverterTest
55 @RunWith (ArchivaSpringJUnit4ClassRunner.class)
56 @ContextConfiguration (locations = { "classpath*:/META-INF/spring-context.xml", "classpath:/spring-context.xml" })
57 public class LegacyToDefaultConverterTest
60 private ArtifactRepository sourceRepository;
62 private ArtifactRepository targetRepository;
64 private ArtifactConverter artifactConverter;
66 private ArtifactFactory artifactFactory;
69 private PlexusSisuBridge plexusSisuBridge;
72 private ApplicationContext applicationContext;
74 private static final int SLEEP_MILLIS = 100;
82 ArtifactRepositoryFactory factory = plexusSisuBridge.lookup( ArtifactRepositoryFactory.class );
84 Map<String, ArtifactRepositoryLayout> layoutsMap = plexusSisuBridge.lookupMap( ArtifactRepositoryLayout.class );
86 System.out.println( "hints " + layoutsMap.keySet().toString() );
88 ArtifactRepositoryLayout layout = plexusSisuBridge.lookup( ArtifactRepositoryLayout.class, "legacy" );
90 File sourceBase = getTestFile( "src/test/source-repository" );
92 factory.createArtifactRepository( "source", sourceBase.toURL().toString(), layout, null, null );
94 layout = plexusSisuBridge.lookup( ArtifactRepositoryLayout.class, "default" );
96 File targetBase = getTestFile( "target/test-target-repository" );
97 copyDirectoryStructure( getTestFile( "src/test/target-repository" ), targetBase );
100 factory.createArtifactRepository( "target", targetBase.toURL().toString(), layout, null, null );
103 applicationContext.getBean( "artifactConverter#legacy-to-default", ArtifactConverter.class );
105 artifactConverter.clearWarnings();
106 artifactFactory = (ArtifactFactory) plexusSisuBridge.lookup( ArtifactFactory.class );
109 public static File getTestFile( String path )
111 return new File( FileUtil.getBasedir(), path );
114 private void copyDirectoryStructure( File sourceDirectory, File destinationDirectory )
117 if ( !sourceDirectory.exists() )
119 throw new IOException( "Source directory doesn't exists (" + sourceDirectory.getAbsolutePath() + ")." );
122 File[] files = sourceDirectory.listFiles();
124 String sourcePath = sourceDirectory.getAbsolutePath();
126 for ( int i = 0; i < files.length; i++ )
128 File file = files[i];
130 String dest = file.getAbsolutePath();
132 dest = dest.substring( sourcePath.length() + 1 );
134 File destination = new File( destinationDirectory, dest );
138 destination = destination.getParentFile();
140 FileUtils.copyFileToDirectory( file, destination );
142 else if ( file.isDirectory() )
144 if ( !".svn".equals( file.getName() ) )
146 if ( !destination.exists() && !destination.mkdirs() )
148 throw new IOException(
149 "Could not create destination directory '" + destination.getAbsolutePath() + "'." );
151 copyDirectoryStructure( file, destination );
156 throw new IOException( "Unknown file type: " + file.getAbsolutePath() );
162 public void testV4PomConvert()
165 // test that it is copied as is
167 Artifact artifact = createArtifact( "test", "v4artifact", "1.0.0" );
168 ArtifactMetadata artifactMetadata = new ArtifactRepositoryMetadata( artifact );
169 File artifactMetadataFile = new File( targetRepository.getBasedir(),
170 targetRepository.pathOfRemoteRepositoryMetadata( artifactMetadata ) );
171 artifactMetadataFile.delete();
173 ArtifactMetadata versionMetadata = new SnapshotArtifactRepositoryMetadata( artifact );
174 File versionMetadataFile = new File( targetRepository.getBasedir(),
175 targetRepository.pathOfRemoteRepositoryMetadata( versionMetadata ) );
176 versionMetadataFile.delete();
178 File artifactFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
179 artifactFile.delete();
181 artifactConverter.convert( artifact, targetRepository );
182 checkSuccess( artifactConverter );
184 assertTrue( "Check artifact created", artifactFile.exists() );
185 assertTrue( "Check artifact matches", FileUtils.contentEquals( artifactFile, artifact.getFile() ) );
187 artifact = createPomArtifact( artifact );
188 File pomFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
189 File sourcePomFile = new File( sourceRepository.getBasedir(), sourceRepository.pathOf( artifact ) );
190 assertTrue( "Check POM created", pomFile.exists() );
192 compareFiles( sourcePomFile, pomFile );
194 assertTrue( "Check artifact metadata created", artifactMetadataFile.exists() );
196 File expectedMetadataFile = getTestFile( "src/test/expected-files/v4-artifact-metadata.xml" );
198 compareFiles( expectedMetadataFile, artifactMetadataFile );
200 assertTrue( "Check snapshot metadata created", versionMetadataFile.exists() );
202 expectedMetadataFile = getTestFile( "src/test/expected-files/v4-version-metadata.xml" );
204 compareFiles( expectedMetadataFile, versionMetadataFile );
208 public void testV3PomConvert()
211 // test that the pom is coverted
213 Artifact artifact = createArtifact( "test", "v3artifact", "1.0.0" );
214 ArtifactMetadata artifactMetadata = new ArtifactRepositoryMetadata( artifact );
215 File artifactMetadataFile = new File( targetRepository.getBasedir(),
216 targetRepository.pathOfRemoteRepositoryMetadata( artifactMetadata ) );
217 artifactMetadataFile.delete();
219 ArtifactMetadata versionMetadata = new SnapshotArtifactRepositoryMetadata( artifact );
220 File versionMetadataFile = new File( targetRepository.getBasedir(),
221 targetRepository.pathOfRemoteRepositoryMetadata( versionMetadata ) );
222 versionMetadataFile.delete();
224 artifactConverter.convert( artifact, targetRepository );
225 checkSuccess( artifactConverter );
227 File artifactFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
228 assertTrue( "Check artifact created", artifactFile.exists() );
229 assertTrue( "Check artifact matches", FileUtils.contentEquals( artifactFile, artifact.getFile() ) );
231 artifact = createPomArtifact( artifact );
232 File pomFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
233 File expectedPomFile = getTestFile( "src/test/expected-files/converted-v3.pom" );
234 assertTrue( "Check POM created", pomFile.exists() );
236 compareFiles( expectedPomFile, pomFile );
238 assertTrue( "Check artifact metadata created", artifactMetadataFile.exists() );
240 File expectedMetadataFile = getTestFile( "src/test/expected-files/v3-artifact-metadata.xml" );
242 compareFiles( expectedMetadataFile, artifactMetadataFile );
244 assertTrue( "Check snapshot metadata created", versionMetadataFile.exists() );
246 expectedMetadataFile = getTestFile( "src/test/expected-files/v3-version-metadata.xml" );
248 compareFiles( expectedMetadataFile, versionMetadataFile );
252 public void testV3PomConvertWithRelocation()
255 Artifact artifact = createArtifact( "test", "relocated-v3artifact", "1.0.0" );
256 ArtifactMetadata artifactMetadata = new ArtifactRepositoryMetadata( artifact );
257 File artifactMetadataFile = new File( targetRepository.getBasedir(),
258 targetRepository.pathOfRemoteRepositoryMetadata( artifactMetadata ) );
259 artifactMetadataFile.delete();
261 ArtifactMetadata versionMetadata = new SnapshotArtifactRepositoryMetadata( artifact );
262 File versionMetadataFile = new File( targetRepository.getBasedir(),
263 targetRepository.pathOfRemoteRepositoryMetadata( versionMetadata ) );
264 versionMetadataFile.delete();
266 artifactConverter.convert( artifact, targetRepository );
267 //checkSuccess(); --> commented until MNG-2100 is fixed
269 File artifactFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
270 assertTrue( "Check if relocated artifact created", artifactFile.exists() );
271 assertTrue( "Check if relocated artifact matches",
272 FileUtils.contentEquals( artifactFile, artifact.getFile() ) );
273 Artifact pomArtifact = createArtifact( "relocated-test", "relocated-v3artifact", "1.0.0", "1.0.0", "pom" );
274 File pomFile = getTestFile( "src/test/expected-files/" + targetRepository.pathOf( pomArtifact ) );
275 File testFile = getTestFile( "target/test-target-repository/" + targetRepository.pathOf( pomArtifact ) );
276 compareFiles( pomFile, testFile );
278 Artifact orig = createArtifact( "test", "relocated-v3artifact", "1.0.0", "1.0.0", "pom" );
279 artifactFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( orig ) );
280 assertTrue( "Check if relocation artifact pom is created", artifactFile.exists() );
281 testFile = getTestFile( "src/test/expected-files/" + targetRepository.pathOf( orig ) );
282 compareFiles( artifactFile, testFile );
286 public void testV3PomWarningsOnConvert()
289 // test that the pom is converted but that warnings are reported
291 Artifact artifact = createArtifact( "test", "v3-warnings-artifact", "1.0.0" );
292 ArtifactMetadata artifactMetadata = new ArtifactRepositoryMetadata( artifact );
293 File artifactMetadataFile = new File( targetRepository.getBasedir(),
294 targetRepository.pathOfRemoteRepositoryMetadata( artifactMetadata ) );
295 artifactMetadataFile.delete();
297 ArtifactMetadata versionMetadata = new SnapshotArtifactRepositoryMetadata( artifact );
298 File versionMetadataFile = new File( targetRepository.getBasedir(),
299 targetRepository.pathOfRemoteRepositoryMetadata( versionMetadata ) );
300 versionMetadataFile.delete();
302 artifactConverter.convert( artifact, targetRepository );
303 checkWarnings( artifactConverter, 2 );
305 File artifactFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
306 assertTrue( "Check artifact created", artifactFile.exists() );
307 assertTrue( "Check artifact matches", FileUtils.contentEquals( artifactFile, artifact.getFile() ) );
309 artifact = createPomArtifact( artifact );
310 File pomFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
311 File expectedPomFile = getTestFile( "src/test/expected-files/converted-v3-warnings.pom" );
312 assertTrue( "Check POM created", pomFile.exists() );
314 compareFiles( expectedPomFile, pomFile );
316 // TODO: check 2 warnings (extend and versions) matched on i18n key
319 private void doTestV4SnapshotPomConvert( String version, String expectedMetadataFileName )
322 // test that it is copied as is
324 Artifact artifact = createArtifact( "test", "v4artifact", version );
325 ArtifactMetadata artifactMetadata = new ArtifactRepositoryMetadata( artifact );
326 File artifactMetadataFile = new File( targetRepository.getBasedir(),
327 targetRepository.pathOfRemoteRepositoryMetadata( artifactMetadata ) );
328 artifactMetadataFile.delete();
330 ArtifactMetadata snapshotMetadata = new SnapshotArtifactRepositoryMetadata( artifact );
331 File snapshotMetadataFile = new File( targetRepository.getBasedir(),
332 targetRepository.pathOfRemoteRepositoryMetadata( snapshotMetadata ) );
333 snapshotMetadataFile.delete();
335 artifactConverter.convert( artifact, targetRepository );
336 checkSuccess( artifactConverter );
338 File artifactFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
339 assertTrue( "Check artifact created", artifactFile.exists() );
340 assertTrue( "Check artifact matches", FileUtils.contentEquals( artifactFile, artifact.getFile() ) );
342 artifact = createPomArtifact( artifact );
343 File pomFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
344 File sourcePomFile = new File( sourceRepository.getBasedir(), sourceRepository.pathOf( artifact ) );
345 assertTrue( "Check POM created", pomFile.exists() );
347 compareFiles( sourcePomFile, pomFile );
349 assertTrue( "Check artifact metadata created", artifactMetadataFile.exists() );
351 File expectedMetadataFile = getTestFile( "src/test/expected-files/v4-snapshot-artifact-metadata.xml" );
353 compareFiles( expectedMetadataFile, artifactMetadataFile );
355 assertTrue( "Check snapshot metadata created", snapshotMetadataFile.exists() );
357 expectedMetadataFile = getTestFile( expectedMetadataFileName );
359 compareFiles( expectedMetadataFile, snapshotMetadataFile );
363 public void testV3SnapshotPomConvert()
366 // test that the pom is coverted
368 Artifact artifact = createArtifact( "test", "v3artifact", "1.0.0-SNAPSHOT" );
369 ArtifactMetadata artifactMetadata = new ArtifactRepositoryMetadata( artifact );
370 File artifactMetadataFile = new File( targetRepository.getBasedir(),
371 targetRepository.pathOfRemoteRepositoryMetadata( artifactMetadata ) );
372 artifactMetadataFile.delete();
374 ArtifactMetadata snapshotMetadata = new SnapshotArtifactRepositoryMetadata( artifact );
375 File snapshotMetadataFile = new File( targetRepository.getBasedir(),
376 targetRepository.pathOfRemoteRepositoryMetadata( snapshotMetadata ) );
377 snapshotMetadataFile.delete();
379 artifactConverter.convert( artifact, targetRepository );
380 checkSuccess( artifactConverter );
382 File artifactFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
383 assertTrue( "Check artifact created", artifactFile.exists() );
384 assertTrue( "Check artifact matches", FileUtils.contentEquals( artifactFile, artifact.getFile() ) );
386 artifact = createPomArtifact( artifact );
387 File pomFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
388 File expectedPomFile = getTestFile( "src/test/expected-files/converted-v3-snapshot.pom" );
389 assertTrue( "Check POM created", pomFile.exists() );
391 compareFiles( expectedPomFile, pomFile );
393 assertTrue( "Check artifact metadata created", artifactMetadataFile.exists() );
395 File expectedMetadataFile = getTestFile( "src/test/expected-files/v3-snapshot-artifact-metadata.xml" );
397 compareFiles( expectedMetadataFile, artifactMetadataFile );
399 assertTrue( "Check snapshot metadata created", snapshotMetadataFile.exists() );
401 expectedMetadataFile = getTestFile( "src/test/expected-files/v3-snapshot-metadata.xml" );
403 compareFiles( expectedMetadataFile, snapshotMetadataFile );
407 public void testV4SnapshotPomConvert()
410 doTestV4SnapshotPomConvert( "1.0.0-SNAPSHOT", "src/test/expected-files/v4-snapshot-metadata.xml" );
416 public void testV4TimestampedSnapshotPomConvert()
419 doTestV4SnapshotPomConvert( "1.0.0-20060111.120115-1",
420 "src/test/expected-files/v4-timestamped-snapshot-metadata.xml" );
426 public void testMavenOnePluginConversion()
430 createArtifact( "org.apache.maven.plugins", "maven-foo-plugin", "1.0", "1.0", "maven-plugin" );
432 new File( FileUtil.getBasedir(), "src/test/source-repository/test/plugins/maven-foo-plugin-1.0.jar" ) );
433 artifactConverter.convert( artifact, targetRepository );
434 // There is a warning but I can't figure out how to look at it. Eyeballing the results it appears
435 // the plugin is being coverted correctly.
438 File artifactFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
439 assertTrue( "Check artifact created", artifactFile.exists() );
440 assertTrue( "Check artifact matches", FileUtils.contentEquals( artifactFile, artifact.getFile() ) );
443 The POM isn't needed for Maven 1.x plugins but the raw conversion for
445 artifact = createPomArtifact( artifact );
446 File pomFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
447 File expectedPomFile = getTestFile( "src/test/expected-files/maven-foo-plugin-1.0.pom" );
448 assertTrue( "Check POM created", pomFile.exists() );
449 compareFiles( expectedPomFile, pomFile );
454 public void testV3TimestampedSnapshotPomConvert()
457 // test that the pom is coverted
459 Artifact artifact = createArtifact( "test", "v3artifact", "1.0.0-20060105.130101-3" );
460 ArtifactMetadata artifactMetadata = new ArtifactRepositoryMetadata( artifact );
461 File artifactMetadataFile = new File( targetRepository.getBasedir(),
462 targetRepository.pathOfRemoteRepositoryMetadata( artifactMetadata ) );
463 artifactMetadataFile.delete();
465 ArtifactMetadata snapshotMetadata = new SnapshotArtifactRepositoryMetadata( artifact );
466 File snapshotMetadataFile = new File( targetRepository.getBasedir(),
467 targetRepository.pathOfRemoteRepositoryMetadata( snapshotMetadata ) );
468 snapshotMetadataFile.delete();
470 artifactConverter.convert( artifact, targetRepository );
471 checkSuccess( artifactConverter );
473 File artifactFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
474 assertTrue( "Check artifact created", artifactFile.exists() );
475 assertTrue( "Check artifact matches", FileUtils.contentEquals( artifactFile, artifact.getFile() ) );
477 artifact = createPomArtifact( artifact );
478 File pomFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
479 File expectedPomFile = getTestFile( "src/test/expected-files/converted-v3-timestamped-snapshot.pom" );
480 assertTrue( "Check POM created", pomFile.exists() );
482 compareFiles( expectedPomFile, pomFile );
484 assertTrue( "Check artifact snapshotMetadata created", artifactMetadataFile.exists() );
486 File expectedMetadataFile = getTestFile( "src/test/expected-files/v3-snapshot-artifact-metadata.xml" );
488 compareFiles( expectedMetadataFile, artifactMetadataFile );
490 assertTrue( "Check snapshot snapshotMetadata created", snapshotMetadataFile.exists() );
492 expectedMetadataFile = getTestFile( "src/test/expected-files/v3-timestamped-snapshot-metadata.xml" );
494 compareFiles( expectedMetadataFile, snapshotMetadataFile );
498 public void testNoPomConvert()
501 // test that a POM is not created when there was none at the source
503 Artifact artifact = createArtifact( "test", "noPomArtifact", "1.0.0" );
504 artifactConverter.convert( artifact, targetRepository );
505 checkWarnings( artifactConverter, 1 );
507 assertHasWarningReason( artifactConverter, Messages.getString( "warning.missing.pom" ) );
509 File artifactFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
510 assertTrue( "Check artifact created", artifactFile.exists() );
511 assertTrue( "Check artifact matches", FileUtils.contentEquals( artifactFile, artifact.getFile() ) );
513 artifact = createPomArtifact( artifact );
514 File pomFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
515 File sourcePomFile = new File( sourceRepository.getBasedir(), sourceRepository.pathOf( artifact ) );
517 assertFalse( "Check no POM created", pomFile.exists() );
518 assertFalse( "No source POM", sourcePomFile.exists() );
522 public void testIncorrectSourceChecksumMd5()
525 // test that it fails when the source md5 is wrong
527 Artifact artifact = createArtifact( "test", "incorrectMd5Artifact", "1.0.0" );
528 File file = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
531 artifactConverter.convert( artifact, targetRepository );
532 checkWarnings( artifactConverter, 2 );
534 assertHasWarningReason( artifactConverter, Messages.getString( "failure.incorrect.md5" ) );
536 assertFalse( "Check artifact not created", file.exists() );
538 ArtifactRepositoryMetadata metadata = new ArtifactRepositoryMetadata( artifact );
540 new File( targetRepository.getBasedir(), targetRepository.pathOfRemoteRepositoryMetadata( metadata ) );
541 assertFalse( "Check metadata not created", metadataFile.exists() );
545 public void testIncorrectSourceChecksumSha1()
548 // test that it fails when the source sha1 is wrong
550 Artifact artifact = createArtifact( "test", "incorrectSha1Artifact", "1.0.0" );
551 File file = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
554 artifactConverter.convert( artifact, targetRepository );
555 checkWarnings( artifactConverter, 2 );
557 assertHasWarningReason( artifactConverter, Messages.getString( "failure.incorrect.sha1" ) );
559 assertFalse( "Check artifact not created", file.exists() );
561 ArtifactRepositoryMetadata metadata = new ArtifactRepositoryMetadata( artifact );
563 new File( targetRepository.getBasedir(), targetRepository.pathOfRemoteRepositoryMetadata( metadata ) );
564 assertFalse( "Check metadata not created", metadataFile.exists() );
568 public void testUnmodifiedArtifact()
569 throws Exception, InterruptedException
571 // test the unmodified artifact is untouched
573 Artifact artifact = createArtifact( "test", "unmodified-artifact", "1.0.0" );
574 Artifact pomArtifact = createPomArtifact( artifact );
576 File sourceFile = new File( sourceRepository.getBasedir(), sourceRepository.pathOf( artifact ) );
577 File sourcePomFile = new File( sourceRepository.getBasedir(), sourceRepository.pathOf( pomArtifact ) );
578 File targetFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
579 File targetPomFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( pomArtifact ) );
581 assertTrue( "Check target file exists", targetFile.exists() );
582 assertTrue( "Check target POM exists", targetPomFile.exists() );
584 sourceFile.setLastModified( System.currentTimeMillis() );
585 sourcePomFile.setLastModified( System.currentTimeMillis() );
587 long origTime = targetFile.lastModified();
588 long origPomTime = targetPomFile.lastModified();
590 // Need to guarantee last modified is not equal
591 Thread.sleep( SLEEP_MILLIS );
593 artifactConverter.convert( artifact, targetRepository );
594 checkSuccess( artifactConverter );
596 compareFiles( sourceFile, targetFile );
597 compareFiles( sourcePomFile, targetPomFile );
599 assertEquals( "Check artifact unmodified", origTime, targetFile.lastModified() );
600 assertEquals( "Check POM unmodified", origPomTime, targetPomFile.lastModified() );
604 public void testModifedArtifactFails()
607 // test that it fails when the source artifact has changed and is different to the existing artifact in the
610 Artifact artifact = createArtifact( "test", "modified-artifact", "1.0.0" );
611 Artifact pomArtifact = createPomArtifact( artifact );
613 File sourceFile = new File( sourceRepository.getBasedir(), sourceRepository.pathOf( artifact ) );
614 File sourcePomFile = new File( sourceRepository.getBasedir(), sourceRepository.pathOf( pomArtifact ) );
615 File targetFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
616 File targetPomFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( pomArtifact ) );
618 assertTrue( "Check target file exists", targetFile.exists() );
619 assertTrue( "Check target POM exists", targetPomFile.exists() );
621 sourceFile.setLastModified( System.currentTimeMillis() );
622 sourcePomFile.setLastModified( System.currentTimeMillis() );
624 long origTime = targetFile.lastModified();
625 long origPomTime = targetPomFile.lastModified();
627 // Need to guarantee last modified is not equal
628 Thread.sleep( SLEEP_MILLIS );
630 artifactConverter.convert( artifact, targetRepository );
631 checkWarnings( artifactConverter, 2 );
633 assertHasWarningReason( artifactConverter, Messages.getString( "failure.target.already.exists" ) );
635 assertEquals( "Check unmodified", origTime, targetFile.lastModified() );
636 assertEquals( "Check unmodified", origPomTime, targetPomFile.lastModified() );
638 ArtifactRepositoryMetadata metadata = new ArtifactRepositoryMetadata( artifact );
640 new File( targetRepository.getBasedir(), targetRepository.pathOfRemoteRepositoryMetadata( metadata ) );
641 assertFalse( "Check metadata not created", metadataFile.exists() );
645 public void testForcedUnmodifiedArtifact()
648 // test unmodified artifact is still converted when set to force
651 applicationContext.getBean( "artifactConverter#force-repository-converter", ArtifactConverter.class );
653 Artifact artifact = createArtifact( "test", "unmodified-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 SimpleDateFormat dateFormat = new SimpleDateFormat( "yyyy-MM-dd", Locale.getDefault() );
662 long origTime = dateFormat.parse( "2006-03-03" ).getTime();
663 targetFile.setLastModified( origTime );
664 targetPomFile.setLastModified( origTime );
666 sourceFile.setLastModified( dateFormat.parse( "2006-01-01" ).getTime() );
667 sourcePomFile.setLastModified( dateFormat.parse( "2006-02-02" ).getTime() );
669 artifactConverter.convert( artifact, targetRepository );
670 checkSuccess( artifactConverter );
672 compareFiles( sourceFile, targetFile );
673 compareFiles( sourcePomFile, targetPomFile );
675 assertFalse( "Check modified", origTime == targetFile.lastModified() );
676 assertFalse( "Check modified", origTime == targetPomFile.lastModified() );
678 ArtifactRepositoryMetadata metadata = new ArtifactRepositoryMetadata( artifact );
680 new File( targetRepository.getBasedir(), targetRepository.pathOfRemoteRepositoryMetadata( metadata ) );
681 assertTrue( "Check metadata created", metadataFile.exists() );
685 public void testDryRunSuccess()
688 // test dry run does nothing on a run that will be successful, and returns success
691 applicationContext.getBean( "artifactConverter#dryrun-repository-converter", ArtifactConverter.class );
693 Artifact artifact = createArtifact( "test", "dryrun-artifact", "1.0.0" );
694 Artifact pomArtifact = createPomArtifact( artifact );
696 File sourceFile = new File( sourceRepository.getBasedir(), sourceRepository.pathOf( artifact ) );
697 File sourcePomFile = new File( sourceRepository.getBasedir(), sourceRepository.pathOf( pomArtifact ) );
698 File targetFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
699 File targetPomFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( pomArtifact ) );
701 // clear warning before test related to MRM-1638
702 artifactConverter.clearWarnings();
703 artifactConverter.convert( artifact, targetRepository );
704 checkSuccess( artifactConverter );
706 assertTrue( "Check source file exists", sourceFile.exists() );
707 assertTrue( "Check source POM exists", sourcePomFile.exists() );
709 assertFalse( "Check target file doesn't exist", targetFile.exists() );
710 assertFalse( "Check target POM doesn't exist", targetPomFile.exists() );
712 ArtifactRepositoryMetadata metadata = new ArtifactRepositoryMetadata( artifact );
714 new File( targetRepository.getBasedir(), targetRepository.pathOfRemoteRepositoryMetadata( metadata ) );
715 assertFalse( "Check metadata not created", metadataFile.exists() );
719 public void testDryRunFailure()
722 // test dry run does nothing on a run that will fail, and returns failure
725 applicationContext.getBean( "artifactConverter#dryrun-repository-converter", ArtifactConverter.class );
727 Artifact artifact = createArtifact( "test", "modified-artifact", "1.0.0" );
728 Artifact pomArtifact = createPomArtifact( artifact );
730 File sourceFile = new File( sourceRepository.getBasedir(), sourceRepository.pathOf( artifact ) );
731 File sourcePomFile = new File( sourceRepository.getBasedir(), sourceRepository.pathOf( pomArtifact ) );
732 File targetFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
733 File targetPomFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( pomArtifact ) );
735 assertTrue( "Check target file exists", targetFile.exists() );
736 assertTrue( "Check target POM exists", targetPomFile.exists() );
738 sourceFile.setLastModified( System.currentTimeMillis() );
739 sourcePomFile.setLastModified( System.currentTimeMillis() );
741 long origTime = targetFile.lastModified();
742 long origPomTime = targetPomFile.lastModified();
744 // Need to guarantee last modified is not equal
745 Thread.sleep( SLEEP_MILLIS );
747 // clear warning before test related to MRM-1638
748 artifactConverter.clearWarnings();
749 artifactConverter.convert( artifact, targetRepository );
750 checkWarnings( artifactConverter, 2 );
752 assertHasWarningReason( artifactConverter, Messages.getString( "failure.target.already.exists" ) );
754 assertEquals( "Check unmodified", origTime, targetFile.lastModified() );
755 assertEquals( "Check unmodified", origPomTime, targetPomFile.lastModified() );
757 ArtifactRepositoryMetadata metadata = new ArtifactRepositoryMetadata( artifact );
759 new File( targetRepository.getBasedir(), targetRepository.pathOfRemoteRepositoryMetadata( metadata ) );
760 assertFalse( "Check metadata not created", metadataFile.exists() );
764 public void testRollbackArtifactCreated()
767 // test rollback can remove a created artifact, including checksums
769 Artifact artifact = createArtifact( "test", "rollback-created-artifact", "1.0.0" );
770 ArtifactMetadata artifactMetadata = new ArtifactRepositoryMetadata( artifact );
771 File artifactMetadataFile = new File( targetRepository.getBasedir(),
772 targetRepository.pathOfRemoteRepositoryMetadata( artifactMetadata ) );
773 FileUtils.deleteDirectory( artifactMetadataFile.getParentFile() );
775 ArtifactMetadata versionMetadata = new SnapshotArtifactRepositoryMetadata( artifact );
776 File versionMetadataFile = new File( targetRepository.getBasedir(),
777 targetRepository.pathOfRemoteRepositoryMetadata( versionMetadata ) );
779 File artifactFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
781 artifactConverter.convert( artifact, targetRepository );
782 checkWarnings( artifactConverter, 2 );
784 boolean found = false;
785 String pattern = "^" + Messages.getString( "invalid.source.pom" ).replaceFirst( "\\{0\\}", ".*" ) + "$";
786 for ( List<String> messages : artifactConverter.getWarnings().values() )
788 for ( String message : messages )
790 if ( message.matches( pattern ) )
803 assertTrue( "Check failure message.", found );
805 assertFalse( "check artifact rolled back", artifactFile.exists() );
806 assertFalse( "check metadata rolled back", artifactMetadataFile.exists() );
807 assertFalse( "check metadata rolled back", versionMetadataFile.exists() );
811 public void testMultipleArtifacts()
814 // test multiple artifacts are converted
816 List<Artifact> artifacts = new ArrayList<>();
817 artifacts.add( createArtifact( "test", "artifact-one", "1.0.0" ) );
818 artifacts.add( createArtifact( "test", "artifact-two", "1.0.0" ) );
819 artifacts.add( createArtifact( "test", "artifact-three", "1.0.0" ) );
821 for ( Artifact artifact : artifacts )
823 artifactConverter.convert( artifact, targetRepository );
824 checkSuccess( artifactConverter );
827 for ( Artifact artifact : artifacts )
829 File artifactFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
830 assertTrue( "Check artifact created", artifactFile.exists() );
831 assertTrue( "Check artifact matches", FileUtils.contentEquals( artifactFile, artifact.getFile() ) );
833 artifact = createPomArtifact( artifact );
834 File pomFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
835 File expectedPomFile =
836 getTestFile( "src/test/expected-files/converted-" + artifact.getArtifactId() + ".pom" );
837 assertTrue( "Check POM created", pomFile.exists() );
839 compareFiles( expectedPomFile, pomFile );
844 public void testInvalidSourceArtifactMetadata()
847 // test artifact is not converted when source metadata is invalid, and returns failure
849 createModernSourceRepository();
851 Artifact artifact = createArtifact( "test", "incorrectArtifactMetadata", "1.0.0" );
852 File file = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
855 artifactConverter.convert( artifact, targetRepository );
856 checkWarnings( artifactConverter, 2 );
858 assertHasWarningReason( artifactConverter,
859 Messages.getString( "failure.incorrect.artifactMetadata.versions" ) );
861 assertFalse( "Check artifact not created", file.exists() );
863 ArtifactRepositoryMetadata metadata = new ArtifactRepositoryMetadata( artifact );
865 new File( targetRepository.getBasedir(), targetRepository.pathOfRemoteRepositoryMetadata( metadata ) );
866 assertFalse( "Check metadata not created", metadataFile.exists() );
870 public void testInvalidSourceSnapshotMetadata()
873 // test artifact is not converted when source snapshot metadata is invalid and returns failure
875 createModernSourceRepository();
877 Artifact artifact = createArtifact( "test", "incorrectSnapshotMetadata", "1.0.0-20060102.030405-6" );
878 File file = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
881 artifactConverter.convert( artifact, targetRepository );
882 checkWarnings( artifactConverter, 2 );
884 assertHasWarningReason( artifactConverter,
885 Messages.getString( "failure.incorrect.snapshotMetadata.snapshot" ) );
887 assertFalse( "Check artifact not created", file.exists() );
889 ArtifactRepositoryMetadata metadata = new ArtifactRepositoryMetadata( artifact );
891 new File( targetRepository.getBasedir(), targetRepository.pathOfRemoteRepositoryMetadata( metadata ) );
892 assertFalse( "Check metadata not created", metadataFile.exists() );
896 public void testMergeArtifactMetadata()
899 // test artifact level metadata is merged when it already exists on successful conversion
901 Artifact artifact = createArtifact( "test", "newversion-artifact", "1.0.1" );
902 artifactConverter.convert( artifact, targetRepository );
903 checkSuccess( artifactConverter );
905 File artifactFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
906 assertTrue( "Check artifact created", artifactFile.exists() );
907 assertTrue( "Check artifact matches", FileUtils.contentEquals( artifactFile, artifact.getFile() ) );
909 artifact = createPomArtifact( artifact );
910 File pomFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
911 File sourcePomFile = new File( sourceRepository.getBasedir(), sourceRepository.pathOf( artifact ) );
912 assertTrue( "Check POM created", pomFile.exists() );
914 compareFiles( sourcePomFile, pomFile );
916 ArtifactMetadata artifactMetadata = new ArtifactRepositoryMetadata( artifact );
917 File artifactMetadataFile = new File( targetRepository.getBasedir(),
918 targetRepository.pathOfRemoteRepositoryMetadata( artifactMetadata ) );
919 assertTrue( "Check artifact metadata created", artifactMetadataFile.exists() );
921 File expectedMetadataFile = getTestFile( "src/test/expected-files/newversion-artifact-metadata.xml" );
923 compareFiles( expectedMetadataFile, artifactMetadataFile );
927 public void testSourceAndTargetRepositoriesMatch()
930 // test that it fails if the same
932 ArtifactRepositoryFactory factory = plexusSisuBridge.lookup( ArtifactRepositoryFactory.class );
935 factory.createArtifactRepository( "source", targetRepository.getUrl(), targetRepository.getLayout(), null,
938 Artifact artifact = createArtifact( "test", "repository-artifact", "1.0" );
942 artifactConverter.convert( artifact, targetRepository );
943 fail( "Should have failed trying to convert within the same repository" );
945 catch ( ArtifactConversionException e )
948 assertEquals( "check message", Messages.getString( "exception.repositories.match" ), e.getMessage() );
949 assertNull( "Check no additional cause", e.getCause() );
953 private Artifact createArtifact( String groupId, String artifactId, String version )
955 Matcher matcher = Artifact.VERSION_FILE_PATTERN.matcher( version );
957 if ( matcher.matches() )
959 baseVersion = matcher.group( 1 ) + "-SNAPSHOT";
963 baseVersion = version;
965 return createArtifact( groupId, artifactId, baseVersion, version, "jar" );
968 private Artifact createArtifact( String groupId, String artifactId, String baseVersion, String version,
971 Artifact artifact = artifactFactory.createArtifact( groupId, artifactId, version, null, type );
972 artifact.setBaseVersion( baseVersion );
973 artifact.setRepository( sourceRepository );
974 artifact.setFile( new File( sourceRepository.getBasedir(), sourceRepository.pathOf( artifact ) ) );
978 private Artifact createPomArtifact( Artifact artifact )
980 return createArtifact( artifact.getGroupId(), artifact.getArtifactId(), artifact.getBaseVersion(),
981 artifact.getVersion(), "pom" );
984 private static void compareFiles( File expectedPomFile, File pomFile )
987 String expectedContent = normalizeString(
988 org.apache.commons.io.FileUtils.readFileToString( expectedPomFile, Charset.defaultCharset() ) );
989 String targetContent =
990 normalizeString( org.apache.commons.io.FileUtils.readFileToString( pomFile, Charset.defaultCharset() ) );
991 assertEquals( "Check file match between " + expectedPomFile + " and " + pomFile, expectedContent,
995 private static String normalizeString( String path )
997 return path.trim().replaceAll( "\r\n", "\n" ).replace( '\r', '\n' ).replaceAll( "<\\?xml .+\\?>",
998 "" ).replaceAll( "^\\s+", "" );
1001 private void checkSuccess( ArtifactConverter converter )
1003 assertNotNull( "Warnings should never be null.", converter.getWarnings() );
1004 assertEquals( "Should have no warnings.", 0, countWarningMessages( converter ) );
1007 private void checkWarnings( ArtifactConverter converter, int count )
1009 assertNotNull( "Warnings should never be null.", converter.getWarnings() );
1010 assertEquals( "Should have some warnings.", count, countWarningMessages( converter ) );
1013 private int countWarningMessages( ArtifactConverter converter )
1016 for ( List<String> values : converter.getWarnings().values() )
1018 count += values.size();
1023 private void assertHasWarningReason( ArtifactConverter converter, String reason )
1025 assertNotNull( "Warnings should never be null.", converter.getWarnings() );
1026 assertTrue( "Expecting 1 or more Warnings", countWarningMessages( converter ) > 0 );
1028 for ( List<String> messages : converter.getWarnings().values() )
1030 if ( messages.contains( reason ) )
1032 /* No need to check any further */
1037 /* didn't find it. */
1039 for ( Map.Entry<Artifact, List<String>> entry : converter.getWarnings().entrySet() )
1041 Artifact artifact = (Artifact) entry.getKey();
1043 "-Artifact: " + artifact.getGroupId() + ":" + artifact.getArtifactId() + ":" + artifact.getVersion() );
1044 List<String> messages = entry.getValue();
1045 for ( String message : messages )
1047 System.out.println( " " + message );
1050 fail( "Unable to find message <" + reason + "> in warnings." );
1053 private void createModernSourceRepository()
1056 ArtifactRepositoryFactory factory = plexusSisuBridge.lookup( ArtifactRepositoryFactory.class );
1058 ArtifactRepositoryLayout layout = plexusSisuBridge.lookup( ArtifactRepositoryLayout.class, "default" );
1060 File sourceBase = getTestFile( "src/test/source-modern-repository" );
1062 factory.createArtifactRepository( "source", sourceBase.toURL().toString(), layout, null, null );