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.test.utils.ArchivaSpringJUnit4ClassRunner;
25 import org.apache.commons.io.FileUtils;
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;
40 import javax.inject.Inject;
41 import java.io.IOException;
42 import java.nio.charset.Charset;
43 import java.nio.file.Files;
44 import java.nio.file.Path;
45 import java.nio.file.Paths;
46 import java.nio.file.attribute.FileTime;
47 import java.text.SimpleDateFormat;
48 import java.util.ArrayList;
49 import java.util.List;
50 import java.util.Locale;
52 import java.util.concurrent.TimeUnit;
53 import java.util.regex.Matcher;
56 * LegacyToDefaultConverterTest
58 @RunWith (ArchivaSpringJUnit4ClassRunner.class)
59 @ContextConfiguration (locations = { "classpath*:/META-INF/spring-context.xml", "classpath:/spring-context.xml" })
60 public class LegacyToDefaultConverterTest
63 private ArtifactRepository sourceRepository;
65 private ArtifactRepository targetRepository;
67 private ArtifactConverter artifactConverter;
69 private ArtifactFactory artifactFactory;
72 private PlexusSisuBridge plexusSisuBridge;
75 private ApplicationContext applicationContext;
77 private static final int SLEEP_MILLIS = 100;
85 ArtifactRepositoryFactory factory = plexusSisuBridge.lookup( ArtifactRepositoryFactory.class );
87 Map<String, ArtifactRepositoryLayout> layoutsMap = plexusSisuBridge.lookupMap( ArtifactRepositoryLayout.class );
89 System.out.println( "hints " + layoutsMap.keySet().toString() );
91 ArtifactRepositoryLayout layout = plexusSisuBridge.lookup( ArtifactRepositoryLayout.class, "legacy" );
93 Path sourceBase = getTestFile( "src/test/source-repository" );
95 factory.createArtifactRepository( "source", sourceBase.toUri().toURL().toString(), layout, null, null );
97 layout = plexusSisuBridge.lookup( ArtifactRepositoryLayout.class, "default" );
99 Path targetBase = getTestFile( "target/test-target-repository" );
100 copyDirectoryStructure( getTestFile( "src/test/target-repository" ), targetBase );
103 factory.createArtifactRepository( "target", targetBase.toUri().toURL().toString(), layout, null, null );
106 applicationContext.getBean( "artifactConverter#legacy-to-default", ArtifactConverter.class );
108 artifactConverter.clearWarnings();
109 artifactFactory = (ArtifactFactory) plexusSisuBridge.lookup( ArtifactFactory.class );
112 public static Path getTestFile( String path )
114 return Paths.get( org.apache.archiva.common.utils.FileUtils.getBasedir(), path );
117 private void copyDirectoryStructure( Path sourceDirectory, Path destinationDirectory )
120 if ( !Files.exists(sourceDirectory) )
122 throw new IOException( "Source directory doesn't exists (" + sourceDirectory.toAbsolutePath()+ ")." );
125 Path[] files = Files.list( sourceDirectory ).toArray( Path[]::new );
127 String sourcePath = sourceDirectory.toAbsolutePath().toString();
129 for ( int i = 0; i < files.length; i++ )
131 Path file = files[i];
133 String dest = file.toAbsolutePath().toString();
135 dest = dest.substring( sourcePath.length() + 1 );
137 Path destination = destinationDirectory.resolve( dest );
139 if ( Files.isRegularFile( file ) )
141 destination = destination.getParent();
143 FileUtils.copyFileToDirectory( file.toFile(), destination.toFile() );
145 else if ( Files.isDirectory( file ) )
147 if ( !".svn".equals( file.getFileName().toString() ) )
149 if ( !Files.exists(destination))
151 Files.createDirectories( destination );
153 copyDirectoryStructure( file, destination );
158 throw new IOException( "Unknown file type: " + file.toAbsolutePath() );
164 public void testV4PomConvert()
167 // test that it is copied as is
169 Artifact artifact = createArtifact( "test", "v4artifact", "1.0.0" );
170 ArtifactMetadata artifactMetadata = new ArtifactRepositoryMetadata( artifact );
171 Path artifactMetadataFile = Paths.get( targetRepository.getBasedir(),
172 targetRepository.pathOfRemoteRepositoryMetadata( artifactMetadata ) );
173 Files.deleteIfExists( artifactMetadataFile);
175 ArtifactMetadata versionMetadata = new SnapshotArtifactRepositoryMetadata( artifact );
176 Path versionMetadataFile = Paths.get( targetRepository.getBasedir(),
177 targetRepository.pathOfRemoteRepositoryMetadata( versionMetadata ) );
178 Files.deleteIfExists(versionMetadataFile);
180 Path artifactFile = Paths.get( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
181 Files.deleteIfExists(artifactFile);
183 artifactConverter.convert( artifact, targetRepository );
184 checkSuccess( artifactConverter );
186 assertTrue( "Check artifact created", Files.exists(artifactFile) );
187 assertTrue( "Check artifact matches", FileUtils.contentEquals( artifactFile.toFile(), artifact.getFile() ) );
189 artifact = createPomArtifact( artifact );
190 Path pomFile = Paths.get( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
191 Path sourcePomFile = Paths.get( sourceRepository.getBasedir(), sourceRepository.pathOf( artifact ) );
192 assertTrue( "Check POM created", Files.exists(pomFile) );
194 compareFiles( sourcePomFile, pomFile );
196 assertTrue( "Check artifact metadata created", Files.exists(artifactMetadataFile) );
198 Path expectedMetadataFile = getTestFile( "src/test/expected-files/v4-artifact-metadata.xml" );
200 compareFiles( expectedMetadataFile, artifactMetadataFile );
202 assertTrue( "Check snapshot metadata created", Files.exists(versionMetadataFile) );
204 expectedMetadataFile = getTestFile( "src/test/expected-files/v4-version-metadata.xml" );
206 compareFiles( expectedMetadataFile, versionMetadataFile );
210 public void testV3PomConvert()
213 // test that the pom is coverted
215 Artifact artifact = createArtifact( "test", "v3artifact", "1.0.0" );
216 ArtifactMetadata artifactMetadata = new ArtifactRepositoryMetadata( artifact );
217 Path artifactMetadataFile = Paths.get( targetRepository.getBasedir(),
218 targetRepository.pathOfRemoteRepositoryMetadata( artifactMetadata ) );
219 Files.deleteIfExists(artifactMetadataFile);
221 ArtifactMetadata versionMetadata = new SnapshotArtifactRepositoryMetadata( artifact );
222 Path versionMetadataFile = Paths.get( targetRepository.getBasedir(),
223 targetRepository.pathOfRemoteRepositoryMetadata( versionMetadata ) );
224 Files.deleteIfExists(versionMetadataFile);
226 artifactConverter.convert( artifact, targetRepository );
227 checkSuccess( artifactConverter );
229 Path artifactFile = Paths.get( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
230 assertTrue( "Check artifact created", Files.exists(artifactFile) );
231 assertTrue( "Check artifact matches", FileUtils.contentEquals( artifactFile.toFile(), artifact.getFile() ) );
233 artifact = createPomArtifact( artifact );
234 Path pomFile = Paths.get( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
235 Path expectedPomFile = getTestFile( "src/test/expected-files/converted-v3.pom" );
236 assertTrue( "Check POM created", Files.exists(pomFile) );
238 compareFiles( expectedPomFile, pomFile );
240 assertTrue( "Check artifact metadata created", Files.exists(artifactMetadataFile) );
242 Path expectedMetadataFile = getTestFile( "src/test/expected-files/v3-artifact-metadata.xml" );
244 compareFiles( expectedMetadataFile, artifactMetadataFile );
246 assertTrue( "Check snapshot metadata created", Files.exists(versionMetadataFile) );
248 expectedMetadataFile = getTestFile( "src/test/expected-files/v3-version-metadata.xml" );
250 compareFiles( expectedMetadataFile, versionMetadataFile );
254 public void testV3PomConvertWithRelocation()
257 Artifact artifact = createArtifact( "test", "relocated-v3artifact", "1.0.0" );
258 ArtifactMetadata artifactMetadata = new ArtifactRepositoryMetadata( artifact );
259 Path artifactMetadataFile = Paths.get( targetRepository.getBasedir(),
260 targetRepository.pathOfRemoteRepositoryMetadata( artifactMetadata ) );
261 Files.deleteIfExists(artifactMetadataFile);
263 ArtifactMetadata versionMetadata = new SnapshotArtifactRepositoryMetadata( artifact );
264 Path versionMetadataFile = Paths.get( targetRepository.getBasedir(),
265 targetRepository.pathOfRemoteRepositoryMetadata( versionMetadata ) );
266 Files.deleteIfExists(versionMetadataFile);
268 artifactConverter.convert( artifact, targetRepository );
269 //checkSuccess(); --> commented until MNG-2100 is fixed
271 Path artifactFile = Paths.get( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
272 assertTrue( "Check if relocated artifact created", Files.exists(artifactFile) );
273 assertTrue( "Check if relocated artifact matches",
274 FileUtils.contentEquals( artifactFile.toFile(), artifact.getFile() ) );
275 Artifact pomArtifact = createArtifact( "relocated-test", "relocated-v3artifact", "1.0.0", "1.0.0", "pom" );
276 Path pomFile = getTestFile( "src/test/expected-files/" + targetRepository.pathOf( pomArtifact ) );
277 Path testFile = getTestFile( "target/test-target-repository/" + targetRepository.pathOf( pomArtifact ) );
278 compareFiles( pomFile, testFile );
280 Artifact orig = createArtifact( "test", "relocated-v3artifact", "1.0.0", "1.0.0", "pom" );
281 artifactFile = Paths.get( targetRepository.getBasedir(), targetRepository.pathOf( orig ) );
282 assertTrue( "Check if relocation artifact pom is created", Files.exists(artifactFile) );
283 testFile = getTestFile( "src/test/expected-files/" + targetRepository.pathOf( orig ) );
284 compareFiles( artifactFile, testFile );
288 public void testV3PomWarningsOnConvert()
291 // test that the pom is converted but that warnings are reported
293 Artifact artifact = createArtifact( "test", "v3-warnings-artifact", "1.0.0" );
294 ArtifactMetadata artifactMetadata = new ArtifactRepositoryMetadata( artifact );
295 Path artifactMetadataFile = Paths.get( targetRepository.getBasedir(),
296 targetRepository.pathOfRemoteRepositoryMetadata( artifactMetadata ) );
297 Files.deleteIfExists(artifactMetadataFile);
299 ArtifactMetadata versionMetadata = new SnapshotArtifactRepositoryMetadata( artifact );
300 Path versionMetadataFile = Paths.get( targetRepository.getBasedir(),
301 targetRepository.pathOfRemoteRepositoryMetadata( versionMetadata ) );
302 Files.deleteIfExists(versionMetadataFile);
304 artifactConverter.convert( artifact, targetRepository );
305 checkWarnings( artifactConverter, 2 );
307 Path artifactFile = Paths.get( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
308 assertTrue( "Check artifact created", Files.exists(artifactFile) );
309 assertTrue( "Check artifact matches", FileUtils.contentEquals( artifactFile.toFile(), artifact.getFile() ) );
311 artifact = createPomArtifact( artifact );
312 Path pomFile = Paths.get( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
313 Path expectedPomFile = getTestFile( "src/test/expected-files/converted-v3-warnings.pom" );
314 assertTrue( "Check POM created", Files.exists(pomFile) );
316 compareFiles( expectedPomFile, pomFile );
318 // TODO: check 2 warnings (extend and versions) matched on i18n key
321 private void doTestV4SnapshotPomConvert( String version, String expectedMetadataFileName )
324 // test that it is copied as is
326 Artifact artifact = createArtifact( "test", "v4artifact", version );
327 ArtifactMetadata artifactMetadata = new ArtifactRepositoryMetadata( artifact );
328 Path artifactMetadataFile = Paths.get( targetRepository.getBasedir(),
329 targetRepository.pathOfRemoteRepositoryMetadata( artifactMetadata ) );
330 Files.deleteIfExists(artifactMetadataFile);
332 ArtifactMetadata snapshotMetadata = new SnapshotArtifactRepositoryMetadata( artifact );
333 Path snapshotMetadataFile = Paths.get( targetRepository.getBasedir(),
334 targetRepository.pathOfRemoteRepositoryMetadata( snapshotMetadata ) );
335 Files.deleteIfExists(snapshotMetadataFile);
337 artifactConverter.convert( artifact, targetRepository );
338 checkSuccess( artifactConverter );
340 Path artifactFile = Paths.get( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
341 assertTrue( "Check artifact created", Files.exists(artifactFile) );
342 assertTrue( "Check artifact matches", FileUtils.contentEquals( artifactFile.toFile(), artifact.getFile() ) );
344 artifact = createPomArtifact( artifact );
345 Path pomFile = Paths.get( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
346 Path sourcePomFile = Paths.get( sourceRepository.getBasedir(), sourceRepository.pathOf( artifact ) );
347 assertTrue( "Check POM created", Files.exists(pomFile) );
349 compareFiles( sourcePomFile, pomFile );
351 assertTrue( "Check artifact metadata created", Files.exists(artifactMetadataFile) );
353 Path expectedMetadataFile = getTestFile( "src/test/expected-files/v4-snapshot-artifact-metadata.xml" );
355 compareFiles( expectedMetadataFile, artifactMetadataFile );
357 assertTrue( "Check snapshot metadata created", Files.exists(snapshotMetadataFile) );
359 expectedMetadataFile = getTestFile( expectedMetadataFileName );
361 compareFiles( expectedMetadataFile, snapshotMetadataFile );
365 public void testV3SnapshotPomConvert()
368 // test that the pom is coverted
370 Artifact artifact = createArtifact( "test", "v3artifact", "1.0.0-SNAPSHOT" );
371 ArtifactMetadata artifactMetadata = new ArtifactRepositoryMetadata( artifact );
372 Path artifactMetadataFile = Paths.get( targetRepository.getBasedir(),
373 targetRepository.pathOfRemoteRepositoryMetadata( artifactMetadata ) );
374 Files.deleteIfExists(artifactMetadataFile);
376 ArtifactMetadata snapshotMetadata = new SnapshotArtifactRepositoryMetadata( artifact );
377 Path snapshotMetadataFile = Paths.get( targetRepository.getBasedir(),
378 targetRepository.pathOfRemoteRepositoryMetadata( snapshotMetadata ) );
379 Files.deleteIfExists(snapshotMetadataFile);
381 artifactConverter.convert( artifact, targetRepository );
382 checkSuccess( artifactConverter );
384 Path artifactFile = Paths.get( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
385 assertTrue( "Check artifact created", Files.exists(artifactFile) );
386 assertTrue( "Check artifact matches", FileUtils.contentEquals( artifactFile.toFile(), artifact.getFile() ) );
388 artifact = createPomArtifact( artifact );
389 Path pomFile = Paths.get( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
390 Path expectedPomFile = getTestFile( "src/test/expected-files/converted-v3-snapshot.pom" );
391 assertTrue( "Check POM created", Files.exists(pomFile) );
393 compareFiles( expectedPomFile, pomFile );
395 assertTrue( "Check artifact metadata created", Files.exists(artifactMetadataFile) );
397 Path expectedMetadataFile = getTestFile( "src/test/expected-files/v3-snapshot-artifact-metadata.xml" );
399 compareFiles( expectedMetadataFile, artifactMetadataFile );
401 assertTrue( "Check snapshot metadata created", Files.exists(snapshotMetadataFile) );
403 expectedMetadataFile = getTestFile( "src/test/expected-files/v3-snapshot-metadata.xml" );
405 compareFiles( expectedMetadataFile, snapshotMetadataFile );
409 public void testV4SnapshotPomConvert()
412 doTestV4SnapshotPomConvert( "1.0.0-SNAPSHOT", "src/test/expected-files/v4-snapshot-metadata.xml" );
418 public void testV4TimestampedSnapshotPomConvert()
421 doTestV4SnapshotPomConvert( "1.0.0-20060111.120115-1",
422 "src/test/expected-files/v4-timestamped-snapshot-metadata.xml" );
428 public void testMavenOnePluginConversion()
432 createArtifact( "org.apache.maven.plugins", "maven-foo-plugin", "1.0", "1.0", "maven-plugin" );
434 Paths.get( org.apache.archiva.common.utils.FileUtils.getBasedir(), "src/test/source-repository/test/plugins/maven-foo-plugin-1.0.jar" ).toFile() );
435 artifactConverter.convert( artifact, targetRepository );
436 // There is a warning but I can't figure out how to look at it. Eyeballing the results it appears
437 // the plugin is being coverted correctly.
440 Path artifactFile = Paths.get( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
441 assertTrue( "Check artifact created", Files.exists(artifactFile) );
442 assertTrue( "Check artifact matches", FileUtils.contentEquals( artifactFile.toFile(), artifact.getFile() ) );
445 The POM isn't needed for Maven 1.x plugins but the raw conversion for
447 artifact = createPomArtifact( artifact );
448 Path pomFile = Paths.get( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
449 File expectedPomFile = getTestFile( "src/test/expected-files/maven-foo-plugin-1.0.pom" );
450 assertTrue( "Check POM created", Files.exists(pomFile) );
451 compareFiles( expectedPomFile, pomFile );
456 public void testV3TimestampedSnapshotPomConvert()
459 // test that the pom is coverted
461 Artifact artifact = createArtifact( "test", "v3artifact", "1.0.0-20060105.130101-3" );
462 ArtifactMetadata artifactMetadata = new ArtifactRepositoryMetadata( artifact );
463 Path artifactMetadataFile = Paths.get( targetRepository.getBasedir(),
464 targetRepository.pathOfRemoteRepositoryMetadata( artifactMetadata ) );
465 Files.deleteIfExists(artifactMetadataFile);
467 ArtifactMetadata snapshotMetadata = new SnapshotArtifactRepositoryMetadata( artifact );
468 Path snapshotMetadataFile = Paths.get( targetRepository.getBasedir(),
469 targetRepository.pathOfRemoteRepositoryMetadata( snapshotMetadata ) );
470 Files.deleteIfExists(snapshotMetadataFile);
472 artifactConverter.convert( artifact, targetRepository );
473 checkSuccess( artifactConverter );
475 Path artifactFile = Paths.get( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
476 assertTrue( "Check artifact created", Files.exists(artifactFile) );
477 assertTrue( "Check artifact matches", FileUtils.contentEquals( artifactFile.toFile(), artifact.getFile() ) );
479 artifact = createPomArtifact( artifact );
480 Path pomFile = Paths.get( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
481 Path expectedPomFile = getTestFile( "src/test/expected-files/converted-v3-timestamped-snapshot.pom" );
482 assertTrue( "Check POM created", Files.exists(pomFile) );
484 compareFiles( expectedPomFile, pomFile );
486 assertTrue( "Check artifact snapshotMetadata created", Files.exists(artifactMetadataFile) );
488 Path expectedMetadataFile = getTestFile( "src/test/expected-files/v3-snapshot-artifact-metadata.xml" );
490 compareFiles( expectedMetadataFile, artifactMetadataFile );
492 assertTrue( "Check snapshot snapshotMetadata created", Files.exists(snapshotMetadataFile) );
494 expectedMetadataFile = getTestFile( "src/test/expected-files/v3-timestamped-snapshot-metadata.xml" );
496 compareFiles( expectedMetadataFile, snapshotMetadataFile );
500 public void testNoPomConvert()
503 // test that a POM is not created when there was none at the source
505 Artifact artifact = createArtifact( "test", "noPomArtifact", "1.0.0" );
506 artifactConverter.convert( artifact, targetRepository );
507 checkWarnings( artifactConverter, 1 );
509 assertHasWarningReason( artifactConverter, Messages.getString( "warning.missing.pom" ) );
511 Path artifactFile = Paths.get( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
512 assertTrue( "Check artifact created", Files.exists(artifactFile) );
513 assertTrue( "Check artifact matches", FileUtils.contentEquals( artifactFile.toFile(), artifact.getFile() ) );
515 artifact = createPomArtifact( artifact );
516 Path pomFile = Paths.get( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
517 Path sourcePomFile = Paths.get( sourceRepository.getBasedir(), sourceRepository.pathOf( artifact ) );
519 assertFalse( "Check no POM created", Files.exists(pomFile) );
520 assertFalse( "No source POM", Files.exists(sourcePomFile) );
524 public void testIncorrectSourceChecksumMd5()
527 // test that it fails when the source md5 is wrong
529 Artifact artifact = createArtifact( "test", "incorrectMd5Artifact", "1.0.0" );
530 Path file = Paths.get( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
531 Files.deleteIfExists(file);
533 artifactConverter.convert( artifact, targetRepository );
534 checkWarnings( artifactConverter, 2 );
536 assertHasWarningReason( artifactConverter, Messages.getString( "failure.incorrect.md5" ) );
538 assertFalse( "Check artifact not created", Files.exists(file) );
540 ArtifactRepositoryMetadata metadata = new ArtifactRepositoryMetadata( artifact );
541 Path metadataFile = Paths.get( targetRepository.getBasedir(), targetRepository.pathOfRemoteRepositoryMetadata( metadata ) );
542 assertFalse( "Check metadata not created", Files.exists(metadataFile) );
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 Path file = Paths.get( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
553 Files.deleteIfExists(file);
555 artifactConverter.convert( artifact, targetRepository );
556 checkWarnings( artifactConverter, 2 );
558 assertHasWarningReason( artifactConverter, Messages.getString( "failure.incorrect.sha1" ) );
560 assertFalse( "Check artifact not created", Files.exists(file) );
562 ArtifactRepositoryMetadata metadata = new ArtifactRepositoryMetadata( artifact );
563 Path metadataFile = Paths.get( targetRepository.getBasedir(), targetRepository.pathOfRemoteRepositoryMetadata( metadata ) );
564 assertFalse( "Check metadata not created", Files.exists(metadataFile) );
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 Path sourceFile = Paths.get( sourceRepository.getBasedir(), sourceRepository.pathOf( artifact ) );
577 Path sourcePomFile = Paths.get( sourceRepository.getBasedir(), sourceRepository.pathOf( pomArtifact ) );
578 Path targetFile = Paths.get( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
579 Path targetPomFile = Paths.get( targetRepository.getBasedir(), targetRepository.pathOf( pomArtifact ) );
581 assertTrue( "Check target file exists", Files.exists(targetFile) );
582 assertTrue( "Check target POM exists", Files.exists(targetPomFile) );
584 Files.setLastModifiedTime( sourceFile, FileTime.from(System.currentTimeMillis(), TimeUnit.MILLISECONDS) );
585 Files.setLastModifiedTime( sourcePomFile, FileTime.from(System.currentTimeMillis(), TimeUnit.MILLISECONDS) );
587 long origTime = Files.getLastModifiedTime( targetFile ).toMillis();
588 long origPomTime = Files.getLastModifiedTime( targetPomFile ).toMillis();
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, Files.getLastModifiedTime( targetFile ).toMillis() );
600 assertEquals( "Check POM unmodified", origPomTime, Files.getLastModifiedTime( targetPomFile ).toMillis() );
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 Path sourceFile = Paths.get( sourceRepository.getBasedir(), sourceRepository.pathOf( artifact ) );
614 Path sourcePomFile = Paths.get( sourceRepository.getBasedir(), sourceRepository.pathOf( pomArtifact ) );
615 Path targetFile = Paths.get( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
616 Path targetPomFile = Paths.get( targetRepository.getBasedir(), targetRepository.pathOf( pomArtifact ) );
618 assertTrue( "Check target file exists", Files.exists(targetFile) );
619 assertTrue( "Check target POM exists", Files.exists(targetPomFile) );
621 Files.setLastModifiedTime(sourceFile, FileTime.from(System.currentTimeMillis() , TimeUnit.MILLISECONDS));
622 Files.setLastModifiedTime(sourcePomFile, FileTime.from(System.currentTimeMillis() , TimeUnit.MILLISECONDS));
624 long origTime = Files.getLastModifiedTime(targetFile).toMillis();
625 long origPomTime = Files.getLastModifiedTime(targetPomFile).toMillis();
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, Files.getLastModifiedTime(targetFile).toMillis() );
636 assertEquals( "Check unmodified", origPomTime, Files.getLastModifiedTime(targetPomFile).toMillis() );
638 ArtifactRepositoryMetadata metadata = new ArtifactRepositoryMetadata( artifact );
639 Path metadataFile = Paths.get( targetRepository.getBasedir(), targetRepository.pathOfRemoteRepositoryMetadata( metadata ) );
640 assertFalse( "Check metadata not created", Files.exists(metadataFile) );
644 public void testForcedUnmodifiedArtifact()
647 // test unmodified artifact is still converted when set to force
650 applicationContext.getBean( "artifactConverter#force-repository-converter", ArtifactConverter.class );
652 Artifact artifact = createArtifact( "test", "unmodified-artifact", "1.0.0" );
653 Artifact pomArtifact = createPomArtifact( artifact );
655 Path sourceFile = Paths.get( sourceRepository.getBasedir(), sourceRepository.pathOf( artifact ) );
656 Path sourcePomFile = Paths.get( sourceRepository.getBasedir(), sourceRepository.pathOf( pomArtifact ) );
657 Path targetFile = Paths.get( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
658 Path targetPomFile = Paths.get( targetRepository.getBasedir(), targetRepository.pathOf( pomArtifact ) );
660 SimpleDateFormat dateFormat = new SimpleDateFormat( "yyyy-MM-dd", Locale.getDefault() );
661 long origTime = dateFormat.parse( "2006-03-03" ).getTime();
662 Files.setLastModifiedTime(targetFile, FileTime.from(origTime , TimeUnit.MILLISECONDS));
663 Files.setLastModifiedTime(targetPomFile, FileTime.from(origTime , TimeUnit.MILLISECONDS));
665 Files.setLastModifiedTime(sourceFile, FileTime.from(dateFormat.parse( "2006-01-01" ).getTime() , TimeUnit.MILLISECONDS));
666 Files.setLastModifiedTime(sourcePomFile, FileTime.from(dateFormat.parse( "2006-02-02" ).getTime() , TimeUnit.MILLISECONDS));
668 artifactConverter.convert( artifact, targetRepository );
669 checkSuccess( artifactConverter );
671 compareFiles( sourceFile, targetFile );
672 compareFiles( sourcePomFile, targetPomFile );
674 assertFalse( "Check modified", origTime == Files.getLastModifiedTime(targetFile).toMillis() );
675 assertFalse( "Check modified", origTime == Files.getLastModifiedTime(targetPomFile).toMillis() );
677 ArtifactRepositoryMetadata metadata = new ArtifactRepositoryMetadata( artifact );
678 Path metadataFile = Paths.get( targetRepository.getBasedir(), targetRepository.pathOfRemoteRepositoryMetadata( metadata ) );
679 assertTrue( "Check metadata created", Files.exists(metadataFile) );
683 public void testDryRunSuccess()
686 // test dry run does nothing on a run that will be successful, and returns success
689 applicationContext.getBean( "artifactConverter#dryrun-repository-converter", ArtifactConverter.class );
691 Artifact artifact = createArtifact( "test", "dryrun-artifact", "1.0.0" );
692 Artifact pomArtifact = createPomArtifact( artifact );
694 Path sourceFile = Paths.get( sourceRepository.getBasedir(), sourceRepository.pathOf( artifact ) );
695 Path sourcePomFile = Paths.get( sourceRepository.getBasedir(), sourceRepository.pathOf( pomArtifact ) );
696 Path targetFile = Paths.get( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
697 Path targetPomFile = Paths.get( targetRepository.getBasedir(), targetRepository.pathOf( pomArtifact ) );
699 // clear warning before test related to MRM-1638
700 artifactConverter.clearWarnings();
701 artifactConverter.convert( artifact, targetRepository );
702 checkSuccess( artifactConverter );
704 assertTrue( "Check source file exists", Files.exists(sourceFile) );
705 assertTrue( "Check source POM exists", Files.exists(sourcePomFile) );
707 assertFalse( "Check target file doesn't exist", Files.exists(targetFile) );
708 assertFalse( "Check target POM doesn't exist", Files.exists(targetPomFile) );
710 ArtifactRepositoryMetadata metadata = new ArtifactRepositoryMetadata( artifact );
711 Path metadataFile = Paths.get( targetRepository.getBasedir(), targetRepository.pathOfRemoteRepositoryMetadata( metadata ) );
712 assertFalse( "Check metadata not created", Files.exists(metadataFile) );
716 public void testDryRunFailure()
719 // test dry run does nothing on a run that will fail, and returns failure
722 applicationContext.getBean( "artifactConverter#dryrun-repository-converter", ArtifactConverter.class );
724 Artifact artifact = createArtifact( "test", "modified-artifact", "1.0.0" );
725 Artifact pomArtifact = createPomArtifact( artifact );
727 Path sourceFile = Paths.get( sourceRepository.getBasedir(), sourceRepository.pathOf( artifact ) );
728 Path sourcePomFile = Paths.get( sourceRepository.getBasedir(), sourceRepository.pathOf( pomArtifact ) );
729 Path targetFile = Paths.get( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
730 Path targetPomFile = Paths.get( targetRepository.getBasedir(), targetRepository.pathOf( pomArtifact ) );
732 assertTrue( "Check target file exists", Files.exists(targetFile) );
733 assertTrue( "Check target POM exists", Files.exists(targetPomFile) );
735 Files.setLastModifiedTime(sourceFile, FileTime.from(System.currentTimeMillis() , TimeUnit.MILLISECONDS));
736 Files.setLastModifiedTime(sourcePomFile, FileTime.from(System.currentTimeMillis() , TimeUnit.MILLISECONDS));
738 long origTime = Files.getLastModifiedTime(targetFile).toMillis();
739 long origPomTime = Files.getLastModifiedTime(targetPomFile).toMillis();
741 // Need to guarantee last modified is not equal
742 Thread.sleep( SLEEP_MILLIS );
744 // clear warning before test related to MRM-1638
745 artifactConverter.clearWarnings();
746 artifactConverter.convert( artifact, targetRepository );
747 checkWarnings( artifactConverter, 2 );
749 assertHasWarningReason( artifactConverter, Messages.getString( "failure.target.already.exists" ) );
751 assertEquals( "Check unmodified", origTime, Files.getLastModifiedTime(targetFile).toMillis() );
752 assertEquals( "Check unmodified", origPomTime, Files.getLastModifiedTime(targetPomFile).toMillis() );
754 ArtifactRepositoryMetadata metadata = new ArtifactRepositoryMetadata( artifact );
755 Path metadataFile = Paths.get( targetRepository.getBasedir(), targetRepository.pathOfRemoteRepositoryMetadata( metadata ) );
756 assertFalse( "Check metadata not created", Files.exists(metadataFile) );
760 public void testRollbackArtifactCreated()
763 // test rollback can remove a created artifact, including checksums
765 Artifact artifact = createArtifact( "test", "rollback-created-artifact", "1.0.0" );
766 ArtifactMetadata artifactMetadata = new ArtifactRepositoryMetadata( artifact );
767 Path artifactMetadataFile = Paths.get( targetRepository.getBasedir(),
768 targetRepository.pathOfRemoteRepositoryMetadata( artifactMetadata ) );
769 org.apache.archiva.common.utils.FileUtils.deleteDirectory( artifactMetadataFile.getParent() );
771 ArtifactMetadata versionMetadata = new SnapshotArtifactRepositoryMetadata( artifact );
772 Path versionMetadataFile = Paths.get( targetRepository.getBasedir(),
773 targetRepository.pathOfRemoteRepositoryMetadata( versionMetadata ) );
775 Path artifactFile = Paths.get( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
777 artifactConverter.convert( artifact, targetRepository );
778 checkWarnings( artifactConverter, 2 );
780 boolean found = false;
781 String pattern = "^" + Messages.getString( "invalid.source.pom" ).replaceFirst( "\\{0\\}", ".*" ) + "$";
782 for ( List<String> messages : artifactConverter.getWarnings().values() )
784 for ( String message : messages )
786 if ( message.matches( pattern ) )
799 assertTrue( "Check failure message.", found );
801 assertFalse( "check artifact rolled back", Files.exists(artifactFile) );
802 assertFalse( "check metadata rolled back", Files.exists(artifactMetadataFile) );
803 assertFalse( "check metadata rolled back", Files.exists(versionMetadataFile) );
807 public void testMultipleArtifacts()
810 // test multiple artifacts are converted
812 List<Artifact> artifacts = new ArrayList<>();
813 artifacts.add( createArtifact( "test", "artifact-one", "1.0.0" ) );
814 artifacts.add( createArtifact( "test", "artifact-two", "1.0.0" ) );
815 artifacts.add( createArtifact( "test", "artifact-three", "1.0.0" ) );
817 for ( Artifact artifact : artifacts )
819 artifactConverter.convert( artifact, targetRepository );
820 checkSuccess( artifactConverter );
823 for ( Artifact artifact : artifacts )
825 Path artifactFile = Paths.get( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
826 assertTrue( "Check artifact created", Files.exists(artifactFile) );
827 assertTrue( "Check artifact matches", FileUtils.contentEquals( artifactFile.toFile(), artifact.getFile() ) );
829 artifact = createPomArtifact( artifact );
830 Path pomFile = Paths.get( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
831 Path expectedPomFile =
832 getTestFile( "src/test/expected-files/converted-" + artifact.getArtifactId() + ".pom" );
833 assertTrue( "Check POM created", Files.exists(pomFile) );
835 compareFiles( expectedPomFile, pomFile );
840 public void testInvalidSourceArtifactMetadata()
843 // test artifact is not converted when source metadata is invalid, and returns failure
845 createModernSourceRepository();
847 Artifact artifact = createArtifact( "test", "incorrectArtifactMetadata", "1.0.0" );
848 Path file = Paths.get( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
849 Files.deleteIfExists(file);
851 artifactConverter.convert( artifact, targetRepository );
852 checkWarnings( artifactConverter, 2 );
854 assertHasWarningReason( artifactConverter,
855 Messages.getString( "failure.incorrect.artifactMetadata.versions" ) );
857 assertFalse( "Check artifact not created", Files.exists(file) );
859 ArtifactRepositoryMetadata metadata = new ArtifactRepositoryMetadata( artifact );
860 Path metadataFile = Paths.get( targetRepository.getBasedir(), targetRepository.pathOfRemoteRepositoryMetadata( metadata ) );
861 assertFalse( "Check metadata not created", Files.exists(metadataFile) );
865 public void testInvalidSourceSnapshotMetadata()
868 // test artifact is not converted when source snapshot metadata is invalid and returns failure
870 createModernSourceRepository();
872 Artifact artifact = createArtifact( "test", "incorrectSnapshotMetadata", "1.0.0-20060102.030405-6" );
873 Path file = Paths.get( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
874 Files.deleteIfExists(file);
876 artifactConverter.convert( artifact, targetRepository );
877 checkWarnings( artifactConverter, 2 );
879 assertHasWarningReason( artifactConverter,
880 Messages.getString( "failure.incorrect.snapshotMetadata.snapshot" ) );
882 assertFalse( "Check artifact not created", Files.exists(file) );
884 ArtifactRepositoryMetadata metadata = new ArtifactRepositoryMetadata( artifact );
885 Path metadataFile = Paths.get( targetRepository.getBasedir(), targetRepository.pathOfRemoteRepositoryMetadata( metadata ) );
886 assertFalse( "Check metadata not created", Files.exists(metadataFile) );
890 public void testMergeArtifactMetadata()
893 // test artifact level metadata is merged when it already exists on successful conversion
895 Artifact artifact = createArtifact( "test", "newversion-artifact", "1.0.1" );
896 artifactConverter.convert( artifact, targetRepository );
897 checkSuccess( artifactConverter );
899 Path artifactFile = Paths.get( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
900 assertTrue( "Check artifact created", Files.exists(artifactFile) );
901 assertTrue( "Check artifact matches", FileUtils.contentEquals( artifactFile.toFile(), artifact.getFile() ) );
903 artifact = createPomArtifact( artifact );
904 Path pomFile = Paths.get( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
905 Path sourcePomFile = Paths.get( sourceRepository.getBasedir(), sourceRepository.pathOf( artifact ) );
906 assertTrue( "Check POM created", Files.exists(pomFile) );
908 compareFiles( sourcePomFile, pomFile );
910 ArtifactMetadata artifactMetadata = new ArtifactRepositoryMetadata( artifact );
911 Path artifactMetadataFile = Paths.get( targetRepository.getBasedir(),
912 targetRepository.pathOfRemoteRepositoryMetadata( artifactMetadata ) );
913 assertTrue( "Check artifact metadata created", Files.exists(artifactMetadataFile) );
915 Path expectedMetadataFile = getTestFile( "src/test/expected-files/newversion-artifact-metadata.xml" );
917 compareFiles( expectedMetadataFile, artifactMetadataFile );
921 public void testSourceAndTargetRepositoriesMatch()
924 // test that it fails if the same
926 ArtifactRepositoryFactory factory = plexusSisuBridge.lookup( ArtifactRepositoryFactory.class );
929 factory.createArtifactRepository( "source", targetRepository.getUrl(), targetRepository.getLayout(), null,
932 Artifact artifact = createArtifact( "test", "repository-artifact", "1.0" );
936 artifactConverter.convert( artifact, targetRepository );
937 fail( "Should have failed trying to convert within the same repository" );
939 catch ( ArtifactConversionException e )
942 assertEquals( "check message", Messages.getString( "exception.repositories.match" ), e.getMessage() );
943 assertNull( "Check no additional cause", e.getCause() );
947 private Artifact createArtifact( String groupId, String artifactId, String version )
949 Matcher matcher = Artifact.VERSION_FILE_PATTERN.matcher( version );
951 if ( matcher.matches() )
953 baseVersion = matcher.group( 1 ) + "-SNAPSHOT";
957 baseVersion = version;
959 return createArtifact( groupId, artifactId, baseVersion, version, "jar" );
962 private Artifact createArtifact( String groupId, String artifactId, String baseVersion, String version,
965 Artifact artifact = artifactFactory.createArtifact( groupId, artifactId, version, null, type );
966 artifact.setBaseVersion( baseVersion );
967 artifact.setRepository( sourceRepository );
968 artifact.setFile( Paths.get( sourceRepository.getBasedir(), sourceRepository.pathOf( artifact ) ).toFile() );
972 private Artifact createPomArtifact( Artifact artifact )
974 return createArtifact( artifact.getGroupId(), artifact.getArtifactId(), artifact.getBaseVersion(),
975 artifact.getVersion(), "pom" );
978 private static void compareFiles( Path expectedPomFile, Path pomFile )
981 String expectedContent = normalizeString(
982 org.apache.archiva.common.utils.FileUtils.readFileToString( expectedPomFile, Charset.defaultCharset() ) );
983 String targetContent =
984 normalizeString( org.apache.archiva.common.utils.FileUtils.readFileToString( pomFile, Charset.defaultCharset() ) );
985 assertEquals( "Check file match between " + expectedPomFile + " and " + pomFile, expectedContent,
989 private static String normalizeString( String path )
991 return path.trim().replaceAll( "\r\n", "\n" ).replace( '\r', '\n' ).replaceAll( "<\\?xml .+\\?>",
992 "" ).replaceAll( "^\\s+", "" );
995 private void checkSuccess( ArtifactConverter converter )
997 assertNotNull( "Warnings should never be null.", converter.getWarnings() );
998 assertEquals( "Should have no warnings. " + converter.getWarnings(), 0, countWarningMessages( converter ) );
1001 private void checkWarnings( ArtifactConverter converter, int count )
1003 assertNotNull( "Warnings should never be null.", converter.getWarnings() );
1004 assertEquals( "Should have some warnings.", count, countWarningMessages( converter ) );
1007 private int countWarningMessages( ArtifactConverter converter )
1010 for ( List<String> values : converter.getWarnings().values() )
1012 count += values.size();
1017 private void assertHasWarningReason( ArtifactConverter converter, String reason )
1019 assertNotNull( "Warnings should never be null.", converter.getWarnings() );
1020 assertTrue( "Expecting 1 or more Warnings", countWarningMessages( converter ) > 0 );
1022 for ( List<String> messages : converter.getWarnings().values() )
1024 if ( messages.contains( reason ) )
1026 /* No need to check any further */
1031 /* didn't find it. */
1033 for ( Map.Entry<Artifact, List<String>> entry : converter.getWarnings().entrySet() )
1035 Artifact artifact = (Artifact) entry.getKey();
1037 "-Artifact: " + artifact.getGroupId() + ":" + artifact.getArtifactId() + ":" + artifact.getVersion() );
1038 List<String> messages = entry.getValue();
1039 for ( String message : messages )
1041 System.out.println( " " + message );
1044 fail( "Unable to find message <" + reason + "> in warnings." );
1047 private void createModernSourceRepository()
1050 ArtifactRepositoryFactory factory = plexusSisuBridge.lookup( ArtifactRepositoryFactory.class );
1052 ArtifactRepositoryLayout layout = plexusSisuBridge.lookup( ArtifactRepositoryLayout.class, "default" );
1054 Path sourceBase = getTestFile( "src/test/source-modern-repository" );
1056 factory.createArtifactRepository( "source", sourceBase.toUri().toURL().toString(), layout, null, null );