1 package org.apache.maven.repository.converter;
4 * Copyright 2005-2006 The Apache Software Foundation.
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
19 import org.apache.maven.artifact.Artifact;
20 import org.apache.maven.artifact.factory.ArtifactFactory;
21 import org.apache.maven.artifact.metadata.ArtifactMetadata;
22 import org.apache.maven.artifact.repository.ArtifactRepository;
23 import org.apache.maven.artifact.repository.ArtifactRepositoryFactory;
24 import org.apache.maven.artifact.repository.layout.ArtifactRepositoryLayout;
25 import org.apache.maven.artifact.repository.metadata.ArtifactRepositoryMetadata;
26 import org.apache.maven.artifact.repository.metadata.SnapshotArtifactRepositoryMetadata;
27 import org.apache.maven.repository.reporting.ArtifactReporter;
28 import org.apache.maven.repository.reporting.ArtifactResult;
29 import org.apache.maven.repository.reporting.DefaultArtifactReporter;
30 import org.codehaus.plexus.PlexusTestCase;
31 import org.codehaus.plexus.i18n.I18N;
32 import org.codehaus.plexus.util.FileUtils;
35 import java.io.IOException;
36 import java.net.MalformedURLException;
37 import java.text.SimpleDateFormat;
38 import java.util.ArrayList;
39 import java.util.Iterator;
40 import java.util.List;
41 import java.util.Locale;
42 import java.util.regex.Matcher;
45 * Test the repository converter.
47 * @author <a href="mailto:brett@apache.org">Brett Porter</a>
48 * @todo what about deletions from the source repository?
49 * @todo use artifact-test instead
50 * @todo should reject if dependencies are missing - rely on reporting?
51 * @todo group metadata
53 public class RepositoryConverterTest
54 extends PlexusTestCase
56 private ArtifactRepository sourceRepository;
58 private ArtifactRepository targetRepository;
60 private RepositoryConverter repositoryConverter;
62 private ArtifactFactory artifactFactory;
64 private ArtifactReporter reporter;
66 private static final int SLEEP_MILLIS = 100;
70 protected void setUp()
75 ArtifactRepositoryFactory factory = (ArtifactRepositoryFactory) lookup( ArtifactRepositoryFactory.ROLE );
77 ArtifactRepositoryLayout layout = (ArtifactRepositoryLayout) lookup( ArtifactRepositoryLayout.ROLE, "legacy" );
79 File sourceBase = getTestFile( "src/test/source-repository" );
81 factory.createArtifactRepository( "source", sourceBase.toURL().toString(), layout, null, null );
83 layout = (ArtifactRepositoryLayout) lookup( ArtifactRepositoryLayout.ROLE, "default" );
85 File targetBase = getTestFile( "target/test-target-repository" );
86 copyDirectoryStructure( getTestFile( "src/test/target-repository" ), targetBase );
89 factory.createArtifactRepository( "target", targetBase.toURL().toString(), layout, null, null );
91 repositoryConverter = (RepositoryConverter) lookup( RepositoryConverter.ROLE, "default" );
93 artifactFactory = (ArtifactFactory) lookup( ArtifactFactory.ROLE );
95 i18n = (I18N) lookup( I18N.ROLE );
97 reporter = new DefaultArtifactReporter();
100 private void copyDirectoryStructure( File sourceDirectory, File destinationDirectory )
103 if ( !sourceDirectory.exists() )
105 throw new IOException( "Source directory doesn't exists (" + sourceDirectory.getAbsolutePath() + ")." );
108 File[] files = sourceDirectory.listFiles();
110 String sourcePath = sourceDirectory.getAbsolutePath();
112 for ( int i = 0; i < files.length; i++ )
114 File file = files[i];
116 String dest = file.getAbsolutePath();
118 dest = dest.substring( sourcePath.length() + 1 );
120 File destination = new File( destinationDirectory, dest );
124 destination = destination.getParentFile();
126 FileUtils.copyFileToDirectory( file, destination );
128 else if ( file.isDirectory() )
130 if ( !".svn".equals( file.getName() ) )
132 if ( !destination.exists() && !destination.mkdirs() )
134 throw new IOException(
135 "Could not create destination directory '" + destination.getAbsolutePath() + "'." );
137 copyDirectoryStructure( file, destination );
142 throw new IOException( "Unknown file type: " + file.getAbsolutePath() );
147 public void testV4PomConvert()
148 throws IOException, RepositoryConversionException
150 // test that it is copied as is
152 Artifact artifact = createArtifact( "test", "v4artifact", "1.0.0" );
153 ArtifactMetadata artifactMetadata = new ArtifactRepositoryMetadata( artifact );
154 File artifactMetadataFile = new File( targetRepository.getBasedir(),
155 targetRepository.pathOfRemoteRepositoryMetadata( artifactMetadata ) );
156 artifactMetadataFile.delete();
158 ArtifactMetadata versionMetadata = new SnapshotArtifactRepositoryMetadata( artifact );
159 File versionMetadataFile = new File( targetRepository.getBasedir(),
160 targetRepository.pathOfRemoteRepositoryMetadata( versionMetadata ) );
161 versionMetadataFile.delete();
163 File artifactFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
164 artifactFile.delete();
166 repositoryConverter.convert( artifact, targetRepository, reporter );
169 assertTrue( "Check artifact created", artifactFile.exists() );
170 assertTrue( "Check artifact matches", FileUtils.contentEquals( artifactFile, artifact.getFile() ) );
172 artifact = createPomArtifact( artifact );
173 File pomFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
174 File sourcePomFile = new File( sourceRepository.getBasedir(), sourceRepository.pathOf( artifact ) );
175 assertTrue( "Check POM created", pomFile.exists() );
177 compareFiles( sourcePomFile, pomFile );
179 assertTrue( "Check artifact metadata created", artifactMetadataFile.exists() );
181 File expectedMetadataFile = getTestFile( "src/test/expected-files/v4-artifact-metadata.xml" );
183 compareFiles( expectedMetadataFile, artifactMetadataFile );
185 assertTrue( "Check snapshot metadata created", versionMetadataFile.exists() );
187 expectedMetadataFile = getTestFile( "src/test/expected-files/v4-version-metadata.xml" );
189 compareFiles( expectedMetadataFile, versionMetadataFile );
192 public void testV3PomConvert()
193 throws IOException, RepositoryConversionException
195 // test that the pom is coverted
197 Artifact artifact = createArtifact( "test", "v3artifact", "1.0.0" );
198 ArtifactMetadata artifactMetadata = new ArtifactRepositoryMetadata( artifact );
199 File artifactMetadataFile = new File( targetRepository.getBasedir(),
200 targetRepository.pathOfRemoteRepositoryMetadata( artifactMetadata ) );
201 artifactMetadataFile.delete();
203 ArtifactMetadata versionMetadata = new SnapshotArtifactRepositoryMetadata( artifact );
204 File versionMetadataFile = new File( targetRepository.getBasedir(),
205 targetRepository.pathOfRemoteRepositoryMetadata( versionMetadata ) );
206 versionMetadataFile.delete();
208 repositoryConverter.convert( artifact, targetRepository, reporter );
211 File artifactFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
212 assertTrue( "Check artifact created", artifactFile.exists() );
213 assertTrue( "Check artifact matches", FileUtils.contentEquals( artifactFile, artifact.getFile() ) );
215 artifact = createPomArtifact( artifact );
216 File pomFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
217 File expectedPomFile = getTestFile( "src/test/expected-files/converted-v3.pom" );
218 assertTrue( "Check POM created", pomFile.exists() );
220 compareFiles( expectedPomFile, pomFile );
222 assertTrue( "Check artifact metadata created", artifactMetadataFile.exists() );
224 File expectedMetadataFile = getTestFile( "src/test/expected-files/v3-artifact-metadata.xml" );
226 compareFiles( expectedMetadataFile, artifactMetadataFile );
228 assertTrue( "Check snapshot metadata created", versionMetadataFile.exists() );
230 expectedMetadataFile = getTestFile( "src/test/expected-files/v3-version-metadata.xml" );
232 compareFiles( expectedMetadataFile, versionMetadataFile );
235 public void testV3PomConvertWithRelocation()
236 throws RepositoryConversionException, IOException
238 Artifact artifact = createArtifact( "test", "relocated-v3artifact", "1.0.0" );
239 ArtifactMetadata artifactMetadata = new ArtifactRepositoryMetadata( artifact );
240 File artifactMetadataFile = new File( targetRepository.getBasedir(),
241 targetRepository.pathOfRemoteRepositoryMetadata( artifactMetadata ) );
242 artifactMetadataFile.delete();
244 ArtifactMetadata versionMetadata = new SnapshotArtifactRepositoryMetadata( artifact );
245 File versionMetadataFile = new File( targetRepository.getBasedir(),
246 targetRepository.pathOfRemoteRepositoryMetadata( versionMetadata ) );
247 versionMetadataFile.delete();
249 repositoryConverter.convert( artifact, targetRepository, reporter );
250 //checkSuccess(); --> commented until MNG-2100 is fixed
252 File artifactFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
253 assertTrue( "Check if relocated artifact created", artifactFile.exists() );
254 assertTrue( "Check if relocated artifact matches",
255 FileUtils.contentEquals( artifactFile, artifact.getFile() ) );
256 Artifact pomArtifact = createArtifact( "relocated-test", "relocated-v3artifact", "1.0.0", "1.0.0", "pom" );
257 File pomFile = getTestFile( "src/test/expected-files/" + targetRepository.pathOf( pomArtifact ) );
258 File testFile = getTestFile( "target/test-target-repository/" + targetRepository.pathOf( pomArtifact ) );
259 compareFiles( pomFile, testFile );
261 Artifact orig = createArtifact( "test", "relocated-v3artifact", "1.0.0", "1.0.0", "pom" );
262 artifactFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( orig ) );
263 assertTrue( "Check if relocation artifact pom is created", artifactFile.exists() );
264 testFile = getTestFile( "src/test/expected-files/" + targetRepository.pathOf( orig ) );
265 compareFiles( artifactFile, testFile );
268 public void testV3PomWarningsOnConvert()
269 throws RepositoryConversionException, IOException
271 // test that the pom is converted but that warnings are reported
273 Artifact artifact = createArtifact( "test", "v3-warnings-artifact", "1.0.0" );
274 ArtifactMetadata artifactMetadata = new ArtifactRepositoryMetadata( artifact );
275 File artifactMetadataFile = new File( targetRepository.getBasedir(),
276 targetRepository.pathOfRemoteRepositoryMetadata( artifactMetadata ) );
277 artifactMetadataFile.delete();
279 ArtifactMetadata versionMetadata = new SnapshotArtifactRepositoryMetadata( artifact );
280 File versionMetadataFile = new File( targetRepository.getBasedir(),
281 targetRepository.pathOfRemoteRepositoryMetadata( versionMetadata ) );
282 versionMetadataFile.delete();
284 repositoryConverter.convert( artifact, targetRepository, reporter );
285 assertEquals( "check no errors", 0, reporter.getFailures() );
286 assertEquals( "check number of warnings", 2, reporter.getWarnings() );
287 assertEquals( "check success", 1, reporter.getSuccesses() );
289 File artifactFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
290 assertTrue( "Check artifact created", artifactFile.exists() );
291 assertTrue( "Check artifact matches", FileUtils.contentEquals( artifactFile, artifact.getFile() ) );
293 artifact = createPomArtifact( artifact );
294 File pomFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
295 File expectedPomFile = getTestFile( "src/test/expected-files/converted-v3-warnings.pom" );
296 assertTrue( "Check POM created", pomFile.exists() );
298 compareFiles( expectedPomFile, pomFile );
300 // TODO: check 2 warnings (extend and versions) matched on i18n key
303 private void doTestV4SnapshotPomConvert( String version, String expectedMetadataFileName )
304 throws RepositoryConversionException, IOException
306 // test that it is copied as is
308 Artifact artifact = createArtifact( "test", "v4artifact", version );
309 ArtifactMetadata artifactMetadata = new ArtifactRepositoryMetadata( artifact );
310 File artifactMetadataFile = new File( targetRepository.getBasedir(),
311 targetRepository.pathOfRemoteRepositoryMetadata( artifactMetadata ) );
312 artifactMetadataFile.delete();
314 ArtifactMetadata snapshotMetadata = new SnapshotArtifactRepositoryMetadata( artifact );
315 File snapshotMetadataFile = new File( targetRepository.getBasedir(),
316 targetRepository.pathOfRemoteRepositoryMetadata( snapshotMetadata ) );
317 snapshotMetadataFile.delete();
319 repositoryConverter.convert( artifact, targetRepository, reporter );
322 File artifactFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
323 assertTrue( "Check artifact created", artifactFile.exists() );
324 assertTrue( "Check artifact matches", FileUtils.contentEquals( artifactFile, artifact.getFile() ) );
326 artifact = createPomArtifact( artifact );
327 File pomFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
328 File sourcePomFile = new File( sourceRepository.getBasedir(), sourceRepository.pathOf( artifact ) );
329 assertTrue( "Check POM created", pomFile.exists() );
331 compareFiles( sourcePomFile, pomFile );
333 assertTrue( "Check artifact metadata created", artifactMetadataFile.exists() );
335 File expectedMetadataFile = getTestFile( "src/test/expected-files/v4-snapshot-artifact-metadata.xml" );
337 compareFiles( expectedMetadataFile, artifactMetadataFile );
339 assertTrue( "Check snapshot metadata created", snapshotMetadataFile.exists() );
341 expectedMetadataFile = getTestFile( expectedMetadataFileName );
343 compareFiles( expectedMetadataFile, snapshotMetadataFile );
346 public void testV3SnapshotPomConvert()
347 throws IOException, RepositoryConversionException
349 // test that the pom is coverted
351 Artifact artifact = createArtifact( "test", "v3artifact", "1.0.0-SNAPSHOT" );
352 ArtifactMetadata artifactMetadata = new ArtifactRepositoryMetadata( artifact );
353 File artifactMetadataFile = new File( targetRepository.getBasedir(),
354 targetRepository.pathOfRemoteRepositoryMetadata( artifactMetadata ) );
355 artifactMetadataFile.delete();
357 ArtifactMetadata snapshotMetadata = new SnapshotArtifactRepositoryMetadata( artifact );
358 File snapshotMetadataFile = new File( targetRepository.getBasedir(),
359 targetRepository.pathOfRemoteRepositoryMetadata( snapshotMetadata ) );
360 snapshotMetadataFile.delete();
362 repositoryConverter.convert( artifact, targetRepository, reporter );
365 File artifactFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
366 assertTrue( "Check artifact created", artifactFile.exists() );
367 assertTrue( "Check artifact matches", FileUtils.contentEquals( artifactFile, artifact.getFile() ) );
369 artifact = createPomArtifact( artifact );
370 File pomFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
371 File expectedPomFile = getTestFile( "src/test/expected-files/converted-v3-snapshot.pom" );
372 assertTrue( "Check POM created", pomFile.exists() );
374 compareFiles( expectedPomFile, pomFile );
376 assertTrue( "Check artifact metadata created", artifactMetadataFile.exists() );
378 File expectedMetadataFile = getTestFile( "src/test/expected-files/v3-snapshot-artifact-metadata.xml" );
380 compareFiles( expectedMetadataFile, artifactMetadataFile );
382 assertTrue( "Check snapshot metadata created", snapshotMetadataFile.exists() );
384 expectedMetadataFile = getTestFile( "src/test/expected-files/v3-snapshot-metadata.xml" );
386 compareFiles( expectedMetadataFile, snapshotMetadataFile );
389 public void testV4SnapshotPomConvert()
390 throws IOException, RepositoryConversionException
392 doTestV4SnapshotPomConvert( "1.0.0-SNAPSHOT", "src/test/expected-files/v4-snapshot-metadata.xml" );
397 public void testV4TimestampedSnapshotPomConvert()
398 throws IOException, RepositoryConversionException
400 doTestV4SnapshotPomConvert( "1.0.0-20060111.120115-1",
401 "src/test/expected-files/v4-timestamped-snapshot-metadata.xml" );
406 public void testV3TimestampedSnapshotPomConvert()
407 throws IOException, RepositoryConversionException
409 // test that the pom is coverted
411 Artifact artifact = createArtifact( "test", "v3artifact", "1.0.0-20060105.130101-3" );
412 ArtifactMetadata artifactMetadata = new ArtifactRepositoryMetadata( artifact );
413 File artifactMetadataFile = new File( targetRepository.getBasedir(),
414 targetRepository.pathOfRemoteRepositoryMetadata( artifactMetadata ) );
415 artifactMetadataFile.delete();
417 ArtifactMetadata snapshotMetadata = new SnapshotArtifactRepositoryMetadata( artifact );
418 File snapshotMetadataFile = new File( targetRepository.getBasedir(),
419 targetRepository.pathOfRemoteRepositoryMetadata( snapshotMetadata ) );
420 snapshotMetadataFile.delete();
422 repositoryConverter.convert( artifact, targetRepository, reporter );
425 File artifactFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
426 assertTrue( "Check artifact created", artifactFile.exists() );
427 assertTrue( "Check artifact matches", FileUtils.contentEquals( artifactFile, artifact.getFile() ) );
429 artifact = createPomArtifact( artifact );
430 File pomFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
431 File expectedPomFile = getTestFile( "src/test/expected-files/converted-v3-timestamped-snapshot.pom" );
432 assertTrue( "Check POM created", pomFile.exists() );
434 compareFiles( expectedPomFile, pomFile );
436 assertTrue( "Check artifact snapshotMetadata created", artifactMetadataFile.exists() );
438 File expectedMetadataFile = getTestFile( "src/test/expected-files/v3-snapshot-artifact-metadata.xml" );
440 compareFiles( expectedMetadataFile, artifactMetadataFile );
442 assertTrue( "Check snapshot snapshotMetadata created", snapshotMetadataFile.exists() );
444 expectedMetadataFile = getTestFile( "src/test/expected-files/v3-timestamped-snapshot-metadata.xml" );
446 compareFiles( expectedMetadataFile, snapshotMetadataFile );
449 public void testNoPomConvert()
450 throws IOException, RepositoryConversionException
452 // test that a POM is not created when there was none at the source
454 Artifact artifact = createArtifact( "test", "noPomArtifact", "1.0.0" );
455 repositoryConverter.convert( artifact, targetRepository, reporter );
456 assertEquals( "check no errors", 0, reporter.getFailures() );
457 assertEquals( "check no warnings", 1, reporter.getWarnings() );
458 assertEquals( "check success", 1, reporter.getSuccesses() );
459 assertEquals( "check warning message", getI18nString( "warning.missing.pom" ), getWarning().getReason() );
461 File artifactFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
462 assertTrue( "Check artifact created", artifactFile.exists() );
463 assertTrue( "Check artifact matches", FileUtils.contentEquals( artifactFile, artifact.getFile() ) );
465 artifact = createPomArtifact( artifact );
466 File pomFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
467 File sourcePomFile = new File( sourceRepository.getBasedir(), sourceRepository.pathOf( artifact ) );
469 assertFalse( "Check no POM created", pomFile.exists() );
470 assertFalse( "No source POM", sourcePomFile.exists() );
473 public void testIncorrectSourceChecksumMd5()
474 throws RepositoryConversionException
476 // test that it fails when the source md5 is wrong
478 Artifact artifact = createArtifact( "test", "incorrectMd5Artifact", "1.0.0" );
479 File file = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
482 repositoryConverter.convert( artifact, targetRepository, reporter );
484 assertEquals( "check failure message", getI18nString( "failure.incorrect.md5" ), getFailure().getReason() );
486 assertFalse( "Check artifact not created", file.exists() );
488 ArtifactRepositoryMetadata metadata = new ArtifactRepositoryMetadata( artifact );
490 new File( targetRepository.getBasedir(), targetRepository.pathOfRemoteRepositoryMetadata( metadata ) );
491 assertFalse( "Check metadata not created", metadataFile.exists() );
494 public void testIncorrectSourceChecksumSha1()
495 throws RepositoryConversionException
497 // test that it fails when the source sha1 is wrong
499 Artifact artifact = createArtifact( "test", "incorrectSha1Artifact", "1.0.0" );
500 File file = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
503 repositoryConverter.convert( artifact, targetRepository, reporter );
505 assertEquals( "check failure message", getI18nString( "failure.incorrect.sha1" ), getFailure().getReason() );
507 assertFalse( "Check artifact not created", file.exists() );
509 ArtifactRepositoryMetadata metadata = new ArtifactRepositoryMetadata( artifact );
511 new File( targetRepository.getBasedir(), targetRepository.pathOfRemoteRepositoryMetadata( metadata ) );
512 assertFalse( "Check metadata not created", metadataFile.exists() );
515 public void testUnmodifiedArtifact()
516 throws RepositoryConversionException, IOException, InterruptedException
518 // test the unmodified artifact is untouched
520 Artifact artifact = createArtifact( "test", "unmodified-artifact", "1.0.0" );
521 Artifact pomArtifact = createPomArtifact( artifact );
523 File sourceFile = new File( sourceRepository.getBasedir(), sourceRepository.pathOf( artifact ) );
524 File sourcePomFile = new File( sourceRepository.getBasedir(), sourceRepository.pathOf( pomArtifact ) );
525 File targetFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
526 File targetPomFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( pomArtifact ) );
528 assertTrue( "Check target file exists", targetFile.exists() );
529 assertTrue( "Check target POM exists", targetPomFile.exists() );
531 sourceFile.setLastModified( System.currentTimeMillis() );
532 sourcePomFile.setLastModified( System.currentTimeMillis() );
534 long origTime = targetFile.lastModified();
535 long origPomTime = targetPomFile.lastModified();
537 // Need to guarantee last modified is not equal
538 Thread.sleep( SLEEP_MILLIS );
540 repositoryConverter.convert( artifact, targetRepository, reporter );
543 compareFiles( sourceFile, targetFile );
544 compareFiles( sourcePomFile, targetPomFile );
546 assertEquals( "Check unmodified", origTime, targetFile.lastModified() );
547 assertEquals( "Check unmodified", origPomTime, targetPomFile.lastModified() );
550 public void testModifedArtifactFails()
551 throws InterruptedException, RepositoryConversionException, IOException
553 // test that it fails when the source artifact has changed and is different to the existing artifact in the
556 Artifact artifact = createArtifact( "test", "modified-artifact", "1.0.0" );
557 Artifact pomArtifact = createPomArtifact( artifact );
559 File sourceFile = new File( sourceRepository.getBasedir(), sourceRepository.pathOf( artifact ) );
560 File sourcePomFile = new File( sourceRepository.getBasedir(), sourceRepository.pathOf( pomArtifact ) );
561 File targetFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
562 File targetPomFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( pomArtifact ) );
564 assertTrue( "Check target file exists", targetFile.exists() );
565 assertTrue( "Check target POM exists", targetPomFile.exists() );
567 sourceFile.setLastModified( System.currentTimeMillis() );
568 sourcePomFile.setLastModified( System.currentTimeMillis() );
570 long origTime = targetFile.lastModified();
571 long origPomTime = targetPomFile.lastModified();
573 // Need to guarantee last modified is not equal
574 Thread.sleep( SLEEP_MILLIS );
576 repositoryConverter.convert( artifact, targetRepository, reporter );
578 assertEquals( "Check failure message", getI18nString( "failure.target.already.exists" ),
579 getFailure().getReason() );
581 assertEquals( "Check unmodified", origTime, targetFile.lastModified() );
582 assertEquals( "Check unmodified", origPomTime, targetPomFile.lastModified() );
584 ArtifactRepositoryMetadata metadata = new ArtifactRepositoryMetadata( artifact );
586 new File( targetRepository.getBasedir(), targetRepository.pathOfRemoteRepositoryMetadata( metadata ) );
587 assertFalse( "Check metadata not created", metadataFile.exists() );
590 public void testForcedUnmodifiedArtifact()
591 throws Exception, IOException
593 // test unmodified artifact is still converted when set to force
595 repositoryConverter = (RepositoryConverter) lookup( RepositoryConverter.ROLE, "force-repository-converter" );
597 Artifact artifact = createArtifact( "test", "unmodified-artifact", "1.0.0" );
598 Artifact pomArtifact = createPomArtifact( artifact );
600 File sourceFile = new File( sourceRepository.getBasedir(), sourceRepository.pathOf( artifact ) );
601 File sourcePomFile = new File( sourceRepository.getBasedir(), sourceRepository.pathOf( pomArtifact ) );
602 File targetFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
603 File targetPomFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( pomArtifact ) );
605 SimpleDateFormat dateFormat = new SimpleDateFormat( "yyyy-MM-dd", Locale.getDefault() );
606 long origTime = dateFormat.parse( "2006-03-03" ).getTime();
607 targetFile.setLastModified( origTime );
608 targetPomFile.setLastModified( origTime );
610 sourceFile.setLastModified( dateFormat.parse( "2006-01-01" ).getTime() );
611 sourcePomFile.setLastModified( dateFormat.parse( "2006-02-02" ).getTime() );
613 repositoryConverter.convert( artifact, targetRepository, reporter );
616 compareFiles( sourceFile, targetFile );
617 compareFiles( sourcePomFile, targetPomFile );
619 assertFalse( "Check modified", origTime == targetFile.lastModified() );
620 assertFalse( "Check modified", origTime == targetPomFile.lastModified() );
622 ArtifactRepositoryMetadata metadata = new ArtifactRepositoryMetadata( artifact );
624 new File( targetRepository.getBasedir(), targetRepository.pathOfRemoteRepositoryMetadata( metadata ) );
625 assertTrue( "Check metadata created", metadataFile.exists() );
628 public void testDryRunSuccess()
631 // test dry run does nothing on a run that will be successful, and returns success
633 repositoryConverter = (RepositoryConverter) lookup( RepositoryConverter.ROLE, "dryrun-repository-converter" );
635 Artifact artifact = createArtifact( "test", "dryrun-artifact", "1.0.0" );
636 Artifact pomArtifact = createPomArtifact( artifact );
638 File sourceFile = new File( sourceRepository.getBasedir(), sourceRepository.pathOf( artifact ) );
639 File sourcePomFile = new File( sourceRepository.getBasedir(), sourceRepository.pathOf( pomArtifact ) );
640 File targetFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
641 File targetPomFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( pomArtifact ) );
643 repositoryConverter.convert( artifact, targetRepository, reporter );
646 assertTrue( "Check source file exists", sourceFile.exists() );
647 assertTrue( "Check source POM exists", sourcePomFile.exists() );
649 assertFalse( "Check target file doesn't exist", targetFile.exists() );
650 assertFalse( "Check target POM doesn't exist", targetPomFile.exists() );
652 ArtifactRepositoryMetadata metadata = new ArtifactRepositoryMetadata( artifact );
654 new File( targetRepository.getBasedir(), targetRepository.pathOfRemoteRepositoryMetadata( metadata ) );
655 assertFalse( "Check metadata not created", metadataFile.exists() );
658 public void testDryRunFailure()
661 // test dry run does nothing on a run that will fail, and returns failure
663 repositoryConverter = (RepositoryConverter) lookup( RepositoryConverter.ROLE, "dryrun-repository-converter" );
665 Artifact artifact = createArtifact( "test", "modified-artifact", "1.0.0" );
666 Artifact pomArtifact = createPomArtifact( artifact );
668 File sourceFile = new File( sourceRepository.getBasedir(), sourceRepository.pathOf( artifact ) );
669 File sourcePomFile = new File( sourceRepository.getBasedir(), sourceRepository.pathOf( pomArtifact ) );
670 File targetFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
671 File targetPomFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( pomArtifact ) );
673 assertTrue( "Check target file exists", targetFile.exists() );
674 assertTrue( "Check target POM exists", targetPomFile.exists() );
676 sourceFile.setLastModified( System.currentTimeMillis() );
677 sourcePomFile.setLastModified( System.currentTimeMillis() );
679 long origTime = targetFile.lastModified();
680 long origPomTime = targetPomFile.lastModified();
682 // Need to guarantee last modified is not equal
683 Thread.sleep( SLEEP_MILLIS );
685 repositoryConverter.convert( artifact, targetRepository, reporter );
687 assertEquals( "Check failure message", getI18nString( "failure.target.already.exists" ),
688 getFailure().getReason() );
690 assertEquals( "Check unmodified", origTime, targetFile.lastModified() );
691 assertEquals( "Check unmodified", origPomTime, targetPomFile.lastModified() );
693 ArtifactRepositoryMetadata metadata = new ArtifactRepositoryMetadata( artifact );
695 new File( targetRepository.getBasedir(), targetRepository.pathOfRemoteRepositoryMetadata( metadata ) );
696 assertFalse( "Check metadata not created", metadataFile.exists() );
699 public void testRollbackArtifactCreated()
700 throws RepositoryConversionException, IOException
702 // test rollback can remove a created artifact, including checksums
704 Artifact artifact = createArtifact( "test", "rollback-created-artifact", "1.0.0" );
705 ArtifactMetadata artifactMetadata = new ArtifactRepositoryMetadata( artifact );
706 File artifactMetadataFile = new File( targetRepository.getBasedir(),
707 targetRepository.pathOfRemoteRepositoryMetadata( artifactMetadata ) );
708 FileUtils.deleteDirectory( artifactMetadataFile.getParentFile() );
710 ArtifactMetadata versionMetadata = new SnapshotArtifactRepositoryMetadata( artifact );
711 File versionMetadataFile = new File( targetRepository.getBasedir(),
712 targetRepository.pathOfRemoteRepositoryMetadata( versionMetadata ) );
714 File artifactFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
716 repositoryConverter.convert( artifact, targetRepository, reporter );
718 String pattern = "^" + getI18nString( "failure.invalid.source.pom" ).replaceFirst( "\\{0\\}", ".*" ) + "$";
719 assertTrue( "Check failure message", getFailure().getReason().matches( pattern ) );
721 assertFalse( "check artifact rolled back", artifactFile.exists() );
722 assertFalse( "check metadata rolled back", artifactMetadataFile.exists() );
723 assertFalse( "check metadata rolled back", versionMetadataFile.exists() );
726 public void testMultipleArtifacts()
727 throws RepositoryConversionException, IOException
729 // test multiple artifacts are converted
731 List artifacts = new ArrayList();
732 artifacts.add( createArtifact( "test", "artifact-one", "1.0.0" ) );
733 artifacts.add( createArtifact( "test", "artifact-two", "1.0.0" ) );
734 artifacts.add( createArtifact( "test", "artifact-three", "1.0.0" ) );
735 repositoryConverter.convert( artifacts, targetRepository, reporter );
736 assertEquals( "check no errors", 0, reporter.getFailures() );
737 assertEquals( "check no warnings", 0, reporter.getWarnings() );
738 assertEquals( "check successes", 3, reporter.getSuccesses() );
740 for ( Iterator i = artifacts.iterator(); i.hasNext(); )
742 Artifact artifact = (Artifact) i.next();
744 File artifactFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
745 assertTrue( "Check artifact created", artifactFile.exists() );
746 assertTrue( "Check artifact matches", FileUtils.contentEquals( artifactFile, artifact.getFile() ) );
748 artifact = createPomArtifact( artifact );
749 File pomFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
750 File expectedPomFile =
751 getTestFile( "src/test/expected-files/converted-" + artifact.getArtifactId() + ".pom" );
752 assertTrue( "Check POM created", pomFile.exists() );
754 compareFiles( expectedPomFile, pomFile );
758 public void testInvalidSourceArtifactMetadata()
761 // test artifact is not converted when source metadata is invalid, and returns failure
763 createModernSourceRepository();
765 Artifact artifact = createArtifact( "test", "incorrectArtifactMetadata", "1.0.0" );
766 File file = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
769 repositoryConverter.convert( artifact, targetRepository, reporter );
771 assertEquals( "check failure message", getI18nString( "failure.incorrect.artifactMetadata.versions" ),
772 getFailure().getReason() );
774 assertFalse( "Check artifact not created", file.exists() );
776 ArtifactRepositoryMetadata metadata = new ArtifactRepositoryMetadata( artifact );
778 new File( targetRepository.getBasedir(), targetRepository.pathOfRemoteRepositoryMetadata( metadata ) );
779 assertFalse( "Check metadata not created", metadataFile.exists() );
782 public void testInvalidSourceSnapshotMetadata()
783 throws Exception, MalformedURLException
785 // test artifact is not converted when source snapshot metadata is invalid and returns failure
787 createModernSourceRepository();
789 Artifact artifact = createArtifact( "test", "incorrectSnapshotMetadata", "1.0.0-20060102.030405-6" );
790 File file = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
793 repositoryConverter.convert( artifact, targetRepository, reporter );
795 assertEquals( "check failure message", getI18nString( "failure.incorrect.snapshotMetadata.snapshot" ),
796 getFailure().getReason() );
798 assertFalse( "Check artifact not created", file.exists() );
800 ArtifactRepositoryMetadata metadata = new ArtifactRepositoryMetadata( artifact );
802 new File( targetRepository.getBasedir(), targetRepository.pathOfRemoteRepositoryMetadata( metadata ) );
803 assertFalse( "Check metadata not created", metadataFile.exists() );
806 public void testMergeArtifactMetadata()
807 throws RepositoryConversionException, IOException
809 // test artifact level metadata is merged when it already exists on successful conversion
811 Artifact artifact = createArtifact( "test", "newversion-artifact", "1.0.1" );
813 repositoryConverter.convert( artifact, targetRepository, reporter );
816 File artifactFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
817 assertTrue( "Check artifact created", artifactFile.exists() );
818 assertTrue( "Check artifact matches", FileUtils.contentEquals( artifactFile, artifact.getFile() ) );
820 artifact = createPomArtifact( artifact );
821 File pomFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
822 File sourcePomFile = new File( sourceRepository.getBasedir(), sourceRepository.pathOf( artifact ) );
823 assertTrue( "Check POM created", pomFile.exists() );
825 compareFiles( sourcePomFile, pomFile );
827 ArtifactMetadata artifactMetadata = new ArtifactRepositoryMetadata( artifact );
828 File artifactMetadataFile = new File( targetRepository.getBasedir(),
829 targetRepository.pathOfRemoteRepositoryMetadata( artifactMetadata ) );
830 assertTrue( "Check artifact metadata created", artifactMetadataFile.exists() );
832 File expectedMetadataFile = getTestFile( "src/test/expected-files/newversion-artifact-metadata.xml" );
834 compareFiles( expectedMetadataFile, artifactMetadataFile );
837 public void testSourceAndTargetRepositoriesMatch()
840 // test that it fails if the same
842 ArtifactRepositoryFactory factory = (ArtifactRepositoryFactory) lookup( ArtifactRepositoryFactory.ROLE );
844 sourceRepository = factory.createArtifactRepository( "source", targetRepository.getUrl(),
845 targetRepository.getLayout(), null, null );
847 Artifact artifact = createArtifact( "test", "repository-artifact", "1.0" );
851 repositoryConverter.convert( artifact, targetRepository, reporter );
852 fail( "Should have failed trying to convert within the same repository" );
854 catch ( RepositoryConversionException e )
857 assertEquals( "check message", getI18nString( "exception.repositories.match" ), e.getMessage() );
858 assertNull( "Check no additional cause", e.getCause() );
862 private Artifact createArtifact( String groupId, String artifactId, String version )
864 Matcher matcher = Artifact.VERSION_FILE_PATTERN.matcher( version );
866 if ( matcher.matches() )
868 baseVersion = matcher.group( 1 ) + "-SNAPSHOT";
872 baseVersion = version;
874 return createArtifact( groupId, artifactId, baseVersion, version, "jar" );
877 private Artifact createArtifact( String groupId, String artifactId, String baseVersion, String version,
880 Artifact artifact = artifactFactory.createArtifact( groupId, artifactId, version, null, type );
881 artifact.setBaseVersion( baseVersion );
882 artifact.setRepository( sourceRepository );
883 artifact.setFile( new File( sourceRepository.getBasedir(), sourceRepository.pathOf( artifact ) ) );
887 private Artifact createPomArtifact( Artifact artifact )
889 return createArtifact( artifact.getGroupId(), artifact.getArtifactId(), artifact.getBaseVersion(),
890 artifact.getVersion(), "pom" );
893 private static void compareFiles( File expectedPomFile, File pomFile )
896 String expectedContent = normalizeString( FileUtils.fileRead( expectedPomFile ) );
897 String targetContent = normalizeString( FileUtils.fileRead( pomFile ) );
898 assertEquals( "Check file match between " + expectedPomFile + " and " + pomFile, expectedContent,
902 private static String normalizeString( String path )
904 return path.trim().replaceAll( "\r\n", "\n" ).replace( '\r', '\n' ).replaceAll( "<\\?xml .+\\?>", "" );
907 private void checkSuccess()
909 assertEquals( "check no errors", 0, reporter.getFailures() );
910 assertEquals( "check no warnings", 0, reporter.getWarnings() );
911 assertEquals( "check success", 1, reporter.getSuccesses() );
914 private void checkFailure()
916 assertEquals( "check num errors", 1, reporter.getFailures() );
917 assertEquals( "check no warnings", 0, reporter.getWarnings() );
918 assertEquals( "check no success", 0, reporter.getSuccesses() );
921 private String getI18nString( String key )
923 return i18n.getString( repositoryConverter.getClass().getName(), Locale.getDefault(), key );
926 private ArtifactResult getFailure()
928 return (ArtifactResult) reporter.getArtifactFailureIterator().next();
931 private ArtifactResult getWarning()
933 return (ArtifactResult) reporter.getArtifactWarningIterator().next();
936 private void createModernSourceRepository()
939 ArtifactRepositoryFactory factory = (ArtifactRepositoryFactory) lookup( ArtifactRepositoryFactory.ROLE );
941 ArtifactRepositoryLayout layout = (ArtifactRepositoryLayout) lookup( ArtifactRepositoryLayout.ROLE, "default" );
943 File sourceBase = getTestFile( "src/test/source-modern-repository" );
945 factory.createArtifactRepository( "source", sourceBase.toURL().toString(), layout, null, null );