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.util.ArrayList;
38 import java.util.Iterator;
39 import java.util.List;
40 import java.util.Locale;
41 import java.util.regex.Matcher;
44 * Test the repository converter.
46 * @author <a href="mailto:brett@apache.org">Brett Porter</a>
47 * @todo what about deletions from the source repository?
48 * @todo use artifact-test instead
49 * @todo should reject if dependencies are missing - rely on reporting?
50 * @todo group metadata
52 public class RepositoryConverterTest
53 extends PlexusTestCase
55 private ArtifactRepository sourceRepository;
57 private ArtifactRepository targetRepository;
59 private RepositoryConverter repositoryConverter;
61 private ArtifactFactory artifactFactory;
63 private ArtifactReporter reporter;
65 private static final int SLEEP_MILLIS = 100;
69 protected void setUp()
74 ArtifactRepositoryFactory factory = (ArtifactRepositoryFactory) lookup( ArtifactRepositoryFactory.ROLE );
76 ArtifactRepositoryLayout layout = (ArtifactRepositoryLayout) lookup( ArtifactRepositoryLayout.ROLE, "legacy" );
78 File sourceBase = getTestFile( "src/test/source-repository" );
80 factory.createArtifactRepository( "source", sourceBase.toURL().toString(), layout, null, null );
82 layout = (ArtifactRepositoryLayout) lookup( ArtifactRepositoryLayout.ROLE, "default" );
84 File targetBase = getTestFile( "target/test-target-repository" );
85 FileUtils.copyDirectoryStructure( getTestFile( "src/test/target-repository" ), targetBase );
88 factory.createArtifactRepository( "target", targetBase.toURL().toString(), layout, null, null );
90 repositoryConverter = (RepositoryConverter) lookup( RepositoryConverter.ROLE, "default" );
92 artifactFactory = (ArtifactFactory) lookup( ArtifactFactory.ROLE );
94 i18n = (I18N) lookup( I18N.ROLE );
96 reporter = new DefaultArtifactReporter();
99 public void testV4PomConvert()
100 throws IOException, RepositoryConversionException
102 // test that it is copied as is
104 Artifact artifact = createArtifact( "test", "v4artifact", "1.0.0" );
105 ArtifactMetadata artifactMetadata = new ArtifactRepositoryMetadata( artifact );
106 File artifactMetadataFile = new File( targetRepository.getBasedir(),
107 targetRepository.pathOfRemoteRepositoryMetadata( artifactMetadata ) );
108 artifactMetadataFile.delete();
110 ArtifactMetadata versionMetadata = new SnapshotArtifactRepositoryMetadata( artifact );
111 File versionMetadataFile = new File( targetRepository.getBasedir(),
112 targetRepository.pathOfRemoteRepositoryMetadata( versionMetadata ) );
113 versionMetadataFile.delete();
115 File artifactFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
116 artifactFile.delete();
118 repositoryConverter.convert( artifact, targetRepository, reporter );
121 assertTrue( "Check artifact created", artifactFile.exists() );
122 assertTrue( "Check artifact matches", FileUtils.contentEquals( artifactFile, artifact.getFile() ) );
124 artifact = createPomArtifact( artifact );
125 File pomFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
126 File sourcePomFile = new File( sourceRepository.getBasedir(), sourceRepository.pathOf( artifact ) );
127 assertTrue( "Check POM created", pomFile.exists() );
129 compareFiles( sourcePomFile, pomFile );
131 assertTrue( "Check artifact metadata created", artifactMetadataFile.exists() );
133 File expectedMetadataFile = getTestFile( "src/test/expected-files/v4-artifact-metadata.xml" );
135 compareFiles( expectedMetadataFile, artifactMetadataFile );
137 assertTrue( "Check snapshot metadata created", versionMetadataFile.exists() );
139 expectedMetadataFile = getTestFile( "src/test/expected-files/v4-version-metadata.xml" );
141 compareFiles( expectedMetadataFile, versionMetadataFile );
144 public void testV3PomConvert()
145 throws IOException, RepositoryConversionException
147 // test that the pom is coverted
149 Artifact artifact = createArtifact( "test", "v3artifact", "1.0.0" );
150 ArtifactMetadata artifactMetadata = new ArtifactRepositoryMetadata( artifact );
151 File artifactMetadataFile = new File( targetRepository.getBasedir(),
152 targetRepository.pathOfRemoteRepositoryMetadata( artifactMetadata ) );
153 artifactMetadataFile.delete();
155 ArtifactMetadata versionMetadata = new SnapshotArtifactRepositoryMetadata( artifact );
156 File versionMetadataFile = new File( targetRepository.getBasedir(),
157 targetRepository.pathOfRemoteRepositoryMetadata( versionMetadata ) );
158 versionMetadataFile.delete();
160 repositoryConverter.convert( artifact, targetRepository, reporter );
163 File artifactFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
164 assertTrue( "Check artifact created", artifactFile.exists() );
165 assertTrue( "Check artifact matches", FileUtils.contentEquals( artifactFile, artifact.getFile() ) );
167 artifact = createPomArtifact( artifact );
168 File pomFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
169 File expectedPomFile = getTestFile( "src/test/expected-files/converted-v3.pom" );
170 assertTrue( "Check POM created", pomFile.exists() );
172 compareFiles( expectedPomFile, pomFile );
174 assertTrue( "Check artifact metadata created", artifactMetadataFile.exists() );
176 File expectedMetadataFile = getTestFile( "src/test/expected-files/v3-artifact-metadata.xml" );
178 compareFiles( expectedMetadataFile, artifactMetadataFile );
180 assertTrue( "Check snapshot metadata created", versionMetadataFile.exists() );
182 expectedMetadataFile = getTestFile( "src/test/expected-files/v3-version-metadata.xml" );
184 compareFiles( expectedMetadataFile, versionMetadataFile );
187 public void testV3PomWarningsOnConvert()
188 throws RepositoryConversionException, IOException
190 // test that the pom is converted but that warnings are reported
192 Artifact artifact = createArtifact( "test", "v3-warnings-artifact", "1.0.0" );
193 ArtifactMetadata artifactMetadata = new ArtifactRepositoryMetadata( artifact );
194 File artifactMetadataFile = new File( targetRepository.getBasedir(),
195 targetRepository.pathOfRemoteRepositoryMetadata( artifactMetadata ) );
196 artifactMetadataFile.delete();
198 ArtifactMetadata versionMetadata = new SnapshotArtifactRepositoryMetadata( artifact );
199 File versionMetadataFile = new File( targetRepository.getBasedir(),
200 targetRepository.pathOfRemoteRepositoryMetadata( versionMetadata ) );
201 versionMetadataFile.delete();
203 repositoryConverter.convert( artifact, targetRepository, reporter );
204 assertEquals( "check no errors", 0, reporter.getFailures() );
205 assertEquals( "check number of warnings", 2, reporter.getWarnings() );
206 assertEquals( "check success", 1, reporter.getSuccesses() );
208 File artifactFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
209 assertTrue( "Check artifact created", artifactFile.exists() );
210 assertTrue( "Check artifact matches", FileUtils.contentEquals( artifactFile, artifact.getFile() ) );
212 artifact = createPomArtifact( artifact );
213 File pomFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
214 File expectedPomFile = getTestFile( "src/test/expected-files/converted-v3-warnings.pom" );
215 assertTrue( "Check POM created", pomFile.exists() );
217 compareFiles( expectedPomFile, pomFile );
219 // TODO: check 2 warnings (extend and versions) matched on i18n key
222 public void testV4SnapshotPomConvert()
223 throws IOException, RepositoryConversionException
225 // test that it is copied as is
227 Artifact artifact = createArtifact( "test", "v4artifact", "1.0.0-SNAPSHOT" );
228 ArtifactMetadata artifactMetadata = new ArtifactRepositoryMetadata( artifact );
229 File artifactMetadataFile = new File( targetRepository.getBasedir(),
230 targetRepository.pathOfRemoteRepositoryMetadata( artifactMetadata ) );
231 artifactMetadataFile.delete();
233 ArtifactMetadata snapshotMetadata = new SnapshotArtifactRepositoryMetadata( artifact );
234 File snapshotMetadataFile = new File( targetRepository.getBasedir(),
235 targetRepository.pathOfRemoteRepositoryMetadata( snapshotMetadata ) );
236 snapshotMetadataFile.delete();
238 repositoryConverter.convert( artifact, targetRepository, reporter );
241 File artifactFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
242 assertTrue( "Check artifact created", artifactFile.exists() );
243 assertTrue( "Check artifact matches", FileUtils.contentEquals( artifactFile, artifact.getFile() ) );
245 artifact = createPomArtifact( artifact );
246 File pomFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
247 File sourcePomFile = new File( sourceRepository.getBasedir(), sourceRepository.pathOf( artifact ) );
248 assertTrue( "Check POM created", pomFile.exists() );
250 compareFiles( sourcePomFile, pomFile );
252 assertTrue( "Check artifact metadata created", artifactMetadataFile.exists() );
254 File expectedMetadataFile = getTestFile( "src/test/expected-files/v4-snapshot-artifact-metadata.xml" );
256 compareFiles( expectedMetadataFile, artifactMetadataFile );
258 assertTrue( "Check snapshot metadata created", snapshotMetadataFile.exists() );
260 expectedMetadataFile = getTestFile( "src/test/expected-files/v4-snapshot-metadata.xml" );
262 compareFiles( expectedMetadataFile, snapshotMetadataFile );
265 public void testV3SnapshotPomConvert()
266 throws IOException, RepositoryConversionException
268 // test that the pom is coverted
270 Artifact artifact = createArtifact( "test", "v3artifact", "1.0.0-SNAPSHOT" );
271 ArtifactMetadata artifactMetadata = new ArtifactRepositoryMetadata( artifact );
272 File artifactMetadataFile = new File( targetRepository.getBasedir(),
273 targetRepository.pathOfRemoteRepositoryMetadata( artifactMetadata ) );
274 artifactMetadataFile.delete();
276 ArtifactMetadata snapshotMetadata = new SnapshotArtifactRepositoryMetadata( artifact );
277 File snapshotMetadataFile = new File( targetRepository.getBasedir(),
278 targetRepository.pathOfRemoteRepositoryMetadata( snapshotMetadata ) );
279 snapshotMetadataFile.delete();
281 repositoryConverter.convert( artifact, targetRepository, reporter );
284 File artifactFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
285 assertTrue( "Check artifact created", artifactFile.exists() );
286 assertTrue( "Check artifact matches", FileUtils.contentEquals( artifactFile, artifact.getFile() ) );
288 artifact = createPomArtifact( artifact );
289 File pomFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
290 File expectedPomFile = getTestFile( "src/test/expected-files/converted-v3-snapshot.pom" );
291 assertTrue( "Check POM created", pomFile.exists() );
293 compareFiles( expectedPomFile, pomFile );
295 assertTrue( "Check artifact metadata created", artifactMetadataFile.exists() );
297 File expectedMetadataFile = getTestFile( "src/test/expected-files/v3-snapshot-artifact-metadata.xml" );
299 compareFiles( expectedMetadataFile, artifactMetadataFile );
301 assertTrue( "Check snapshot metadata created", snapshotMetadataFile.exists() );
303 expectedMetadataFile = getTestFile( "src/test/expected-files/v3-snapshot-metadata.xml" );
305 compareFiles( expectedMetadataFile, snapshotMetadataFile );
308 public void testV4TimestampedSnapshotPomConvert()
309 throws IOException, RepositoryConversionException
311 // test that it is copied as is
313 Artifact artifact = createArtifact( "test", "v4artifact", "1.0.0-20060111.120115-1" );
314 ArtifactMetadata artifactMetadata = new ArtifactRepositoryMetadata( artifact );
315 File artifactMetadataFile = new File( targetRepository.getBasedir(),
316 targetRepository.pathOfRemoteRepositoryMetadata( artifactMetadata ) );
317 artifactMetadataFile.delete();
319 ArtifactMetadata snapshotMetadata = new SnapshotArtifactRepositoryMetadata( artifact );
320 File snapshotMetadataFile = new File( targetRepository.getBasedir(),
321 targetRepository.pathOfRemoteRepositoryMetadata( snapshotMetadata ) );
322 snapshotMetadataFile.delete();
324 repositoryConverter.convert( artifact, targetRepository, reporter );
327 File artifactFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
328 assertTrue( "Check artifact created", artifactFile.exists() );
329 assertTrue( "Check artifact matches", FileUtils.contentEquals( artifactFile, artifact.getFile() ) );
331 artifact = createPomArtifact( artifact );
332 File pomFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
333 File sourcePomFile = new File( sourceRepository.getBasedir(), sourceRepository.pathOf( artifact ) );
334 assertTrue( "Check POM created", pomFile.exists() );
336 compareFiles( sourcePomFile, pomFile );
338 assertTrue( "Check artifact metadata created", artifactMetadataFile.exists() );
340 File expectedMetadataFile = getTestFile( "src/test/expected-files/v4-snapshot-artifact-metadata.xml" );
342 compareFiles( expectedMetadataFile, artifactMetadataFile );
344 assertTrue( "Check snapshot metadata created", snapshotMetadataFile.exists() );
346 expectedMetadataFile = getTestFile( "src/test/expected-files/v4-timestamped-snapshot-metadata.xml" );
348 compareFiles( expectedMetadataFile, snapshotMetadataFile );
351 public void testV3TimestampedSnapshotPomConvert()
352 throws IOException, RepositoryConversionException
354 // test that the pom is coverted
356 Artifact artifact = createArtifact( "test", "v3artifact", "1.0.0-20060105.130101-3" );
357 ArtifactMetadata artifactMetadata = new ArtifactRepositoryMetadata( artifact );
358 File artifactMetadataFile = new File( targetRepository.getBasedir(),
359 targetRepository.pathOfRemoteRepositoryMetadata( artifactMetadata ) );
360 artifactMetadataFile.delete();
362 ArtifactMetadata snapshotMetadata = new SnapshotArtifactRepositoryMetadata( artifact );
363 File snapshotMetadataFile = new File( targetRepository.getBasedir(),
364 targetRepository.pathOfRemoteRepositoryMetadata( snapshotMetadata ) );
365 snapshotMetadataFile.delete();
367 repositoryConverter.convert( artifact, targetRepository, reporter );
370 File artifactFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
371 assertTrue( "Check artifact created", artifactFile.exists() );
372 assertTrue( "Check artifact matches", FileUtils.contentEquals( artifactFile, artifact.getFile() ) );
374 artifact = createPomArtifact( artifact );
375 File pomFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
376 File expectedPomFile = getTestFile( "src/test/expected-files/converted-v3-timestamped-snapshot.pom" );
377 assertTrue( "Check POM created", pomFile.exists() );
379 compareFiles( expectedPomFile, pomFile );
381 assertTrue( "Check artifact snapshotMetadata created", artifactMetadataFile.exists() );
383 File expectedMetadataFile = getTestFile( "src/test/expected-files/v3-snapshot-artifact-metadata.xml" );
385 compareFiles( expectedMetadataFile, artifactMetadataFile );
387 assertTrue( "Check snapshot snapshotMetadata created", snapshotMetadataFile.exists() );
389 expectedMetadataFile = getTestFile( "src/test/expected-files/v3-timestamped-snapshot-metadata.xml" );
391 compareFiles( expectedMetadataFile, snapshotMetadataFile );
394 public void testNoPomConvert()
395 throws IOException, RepositoryConversionException
397 // test that a POM is not created when there was none at the source
399 Artifact artifact = createArtifact( "test", "noPomArtifact", "1.0.0" );
400 repositoryConverter.convert( artifact, targetRepository, reporter );
401 assertEquals( "check no errors", 0, reporter.getFailures() );
402 assertEquals( "check no warnings", 1, reporter.getWarnings() );
403 assertEquals( "check success", 1, reporter.getSuccesses() );
404 assertEquals( "check warning message", getI18nString( "warning.missing.pom" ), getWarning().getReason() );
406 File artifactFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
407 assertTrue( "Check artifact created", artifactFile.exists() );
408 assertTrue( "Check artifact matches", FileUtils.contentEquals( artifactFile, artifact.getFile() ) );
410 artifact = createPomArtifact( artifact );
411 File pomFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
412 File sourcePomFile = new File( sourceRepository.getBasedir(), sourceRepository.pathOf( artifact ) );
414 assertFalse( "Check no POM created", pomFile.exists() );
415 assertFalse( "No source POM", sourcePomFile.exists() );
418 public void testIncorrectSourceChecksumMd5()
419 throws RepositoryConversionException
421 // test that it fails when the source md5 is wrong
423 Artifact artifact = createArtifact( "test", "incorrectMd5Artifact", "1.0.0" );
424 File file = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
427 repositoryConverter.convert( artifact, targetRepository, reporter );
429 assertEquals( "check failure message", getI18nString( "failure.incorrect.md5" ), getFailure().getReason() );
431 assertFalse( "Check artifact not created", file.exists() );
433 ArtifactRepositoryMetadata metadata = new ArtifactRepositoryMetadata( artifact );
435 new File( targetRepository.getBasedir(), targetRepository.pathOfRemoteRepositoryMetadata( metadata ) );
436 assertFalse( "Check metadata not created", metadataFile.exists() );
439 public void testIncorrectSourceChecksumSha1()
440 throws RepositoryConversionException
442 // test that it fails when the source sha1 is wrong
444 Artifact artifact = createArtifact( "test", "incorrectSha1Artifact", "1.0.0" );
445 File file = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
448 repositoryConverter.convert( artifact, targetRepository, reporter );
450 assertEquals( "check failure message", getI18nString( "failure.incorrect.sha1" ), getFailure().getReason() );
452 assertFalse( "Check artifact not created", file.exists() );
454 ArtifactRepositoryMetadata metadata = new ArtifactRepositoryMetadata( artifact );
456 new File( targetRepository.getBasedir(), targetRepository.pathOfRemoteRepositoryMetadata( metadata ) );
457 assertFalse( "Check metadata not created", metadataFile.exists() );
460 public void testUnmodifiedArtifact()
461 throws RepositoryConversionException, IOException, InterruptedException
463 // test the unmodified artifact is untouched
465 Artifact artifact = createArtifact( "test", "unmodified-artifact", "1.0.0" );
466 Artifact pomArtifact = createPomArtifact( artifact );
468 File sourceFile = new File( sourceRepository.getBasedir(), sourceRepository.pathOf( artifact ) );
469 File sourcePomFile = new File( sourceRepository.getBasedir(), sourceRepository.pathOf( pomArtifact ) );
470 File targetFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
471 File targetPomFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( pomArtifact ) );
473 assertTrue( "Check target file exists", targetFile.exists() );
474 assertTrue( "Check target POM exists", targetPomFile.exists() );
476 sourceFile.setLastModified( System.currentTimeMillis() );
477 sourcePomFile.setLastModified( System.currentTimeMillis() );
479 long origTime = targetFile.lastModified();
480 long origPomTime = targetPomFile.lastModified();
482 // Need to guarantee last modified is not equal
483 Thread.sleep( SLEEP_MILLIS );
485 repositoryConverter.convert( artifact, targetRepository, reporter );
488 compareFiles( sourceFile, targetFile );
489 compareFiles( sourcePomFile, targetPomFile );
491 assertEquals( "Check unmodified", origTime, targetFile.lastModified() );
492 assertEquals( "Check unmodified", origPomTime, targetPomFile.lastModified() );
495 public void testModifedArtifactFails()
496 throws InterruptedException, RepositoryConversionException, IOException
498 // test that it fails when the source artifact has changed and is different to the existing artifact in the
501 Artifact artifact = createArtifact( "test", "modified-artifact", "1.0.0" );
502 Artifact pomArtifact = createPomArtifact( artifact );
504 File sourceFile = new File( sourceRepository.getBasedir(), sourceRepository.pathOf( artifact ) );
505 File sourcePomFile = new File( sourceRepository.getBasedir(), sourceRepository.pathOf( pomArtifact ) );
506 File targetFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
507 File targetPomFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( pomArtifact ) );
509 assertTrue( "Check target file exists", targetFile.exists() );
510 assertTrue( "Check target POM exists", targetPomFile.exists() );
512 sourceFile.setLastModified( System.currentTimeMillis() );
513 sourcePomFile.setLastModified( System.currentTimeMillis() );
515 long origTime = targetFile.lastModified();
516 long origPomTime = targetPomFile.lastModified();
518 // Need to guarantee last modified is not equal
519 Thread.sleep( SLEEP_MILLIS );
521 repositoryConverter.convert( artifact, targetRepository, reporter );
523 assertEquals( "Check failure message", getI18nString( "failure.target.already.exists" ),
524 getFailure().getReason() );
526 assertEquals( "Check unmodified", origTime, targetFile.lastModified() );
527 assertEquals( "Check unmodified", origPomTime, targetPomFile.lastModified() );
529 ArtifactRepositoryMetadata metadata = new ArtifactRepositoryMetadata( artifact );
531 new File( targetRepository.getBasedir(), targetRepository.pathOfRemoteRepositoryMetadata( metadata ) );
532 assertFalse( "Check metadata not created", metadataFile.exists() );
535 public void testForcedUnmodifiedArtifact()
536 throws Exception, IOException
538 // test unmodified artifact is still converted when set to force
540 repositoryConverter = (RepositoryConverter) lookup( RepositoryConverter.ROLE, "force-repository-converter" );
542 Artifact artifact = createArtifact( "test", "unmodified-artifact", "1.0.0" );
543 Artifact pomArtifact = createPomArtifact( artifact );
545 File sourceFile = new File( sourceRepository.getBasedir(), sourceRepository.pathOf( artifact ) );
546 File sourcePomFile = new File( sourceRepository.getBasedir(), sourceRepository.pathOf( pomArtifact ) );
547 File targetFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
548 File targetPomFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( pomArtifact ) );
550 long origTime = targetFile.lastModified();
551 long origPomTime = targetPomFile.lastModified();
553 sourceFile.setLastModified( System.currentTimeMillis() );
554 sourcePomFile.setLastModified( System.currentTimeMillis() );
556 // Need to guarantee last modified is not equal
557 Thread.sleep( SLEEP_MILLIS );
559 repositoryConverter.convert( artifact, targetRepository, reporter );
562 compareFiles( sourceFile, targetFile );
563 compareFiles( sourcePomFile, targetPomFile );
565 assertFalse( "Check modified", origTime == targetFile.lastModified() );
566 assertFalse( "Check modified", origPomTime == targetPomFile.lastModified() );
568 ArtifactRepositoryMetadata metadata = new ArtifactRepositoryMetadata( artifact );
570 new File( targetRepository.getBasedir(), targetRepository.pathOfRemoteRepositoryMetadata( metadata ) );
571 assertTrue( "Check metadata created", metadataFile.exists() );
574 public void testDryRunSuccess()
577 // test dry run does nothing on a run that will be successful, and returns success
579 repositoryConverter = (RepositoryConverter) lookup( RepositoryConverter.ROLE, "dryrun-repository-converter" );
581 Artifact artifact = createArtifact( "test", "dryrun-artifact", "1.0.0" );
582 Artifact pomArtifact = createPomArtifact( artifact );
584 File sourceFile = new File( sourceRepository.getBasedir(), sourceRepository.pathOf( artifact ) );
585 File sourcePomFile = new File( sourceRepository.getBasedir(), sourceRepository.pathOf( pomArtifact ) );
586 File targetFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
587 File targetPomFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( pomArtifact ) );
589 repositoryConverter.convert( artifact, targetRepository, reporter );
592 assertTrue( "Check source file exists", sourceFile.exists() );
593 assertTrue( "Check source POM exists", sourcePomFile.exists() );
595 assertFalse( "Check target file doesn't exist", targetFile.exists() );
596 assertFalse( "Check target POM doesn't exist", targetPomFile.exists() );
598 ArtifactRepositoryMetadata metadata = new ArtifactRepositoryMetadata( artifact );
600 new File( targetRepository.getBasedir(), targetRepository.pathOfRemoteRepositoryMetadata( metadata ) );
601 assertFalse( "Check metadata not created", metadataFile.exists() );
604 public void testDryRunFailure()
607 // test dry run does nothing on a run that will fail, and returns failure
609 repositoryConverter = (RepositoryConverter) lookup( RepositoryConverter.ROLE, "dryrun-repository-converter" );
611 Artifact artifact = createArtifact( "test", "modified-artifact", "1.0.0" );
612 Artifact pomArtifact = createPomArtifact( artifact );
614 File sourceFile = new File( sourceRepository.getBasedir(), sourceRepository.pathOf( artifact ) );
615 File sourcePomFile = new File( sourceRepository.getBasedir(), sourceRepository.pathOf( pomArtifact ) );
616 File targetFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
617 File targetPomFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( pomArtifact ) );
619 assertTrue( "Check target file exists", targetFile.exists() );
620 assertTrue( "Check target POM exists", targetPomFile.exists() );
622 sourceFile.setLastModified( System.currentTimeMillis() );
623 sourcePomFile.setLastModified( System.currentTimeMillis() );
625 long origTime = targetFile.lastModified();
626 long origPomTime = targetPomFile.lastModified();
628 // Need to guarantee last modified is not equal
629 Thread.sleep( SLEEP_MILLIS );
631 repositoryConverter.convert( artifact, targetRepository, reporter );
633 assertEquals( "Check failure message", getI18nString( "failure.target.already.exists" ),
634 getFailure().getReason() );
636 assertEquals( "Check unmodified", origTime, targetFile.lastModified() );
637 assertEquals( "Check unmodified", origPomTime, targetPomFile.lastModified() );
639 ArtifactRepositoryMetadata metadata = new ArtifactRepositoryMetadata( artifact );
641 new File( targetRepository.getBasedir(), targetRepository.pathOfRemoteRepositoryMetadata( metadata ) );
642 assertFalse( "Check metadata not created", metadataFile.exists() );
645 public void testRollbackArtifactCreated()
646 throws RepositoryConversionException, IOException
648 // test rollback can remove a created artifact, including checksums
650 Artifact artifact = createArtifact( "test", "rollback-created-artifact", "1.0.0" );
651 ArtifactMetadata artifactMetadata = new ArtifactRepositoryMetadata( artifact );
652 File artifactMetadataFile = new File( targetRepository.getBasedir(),
653 targetRepository.pathOfRemoteRepositoryMetadata( artifactMetadata ) );
654 FileUtils.deleteDirectory( artifactMetadataFile.getParentFile() );
656 ArtifactMetadata versionMetadata = new SnapshotArtifactRepositoryMetadata( artifact );
657 File versionMetadataFile = new File( targetRepository.getBasedir(),
658 targetRepository.pathOfRemoteRepositoryMetadata( versionMetadata ) );
660 File artifactFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
662 repositoryConverter.convert( artifact, targetRepository, reporter );
664 String pattern = "^" + getI18nString( "failure.invalid.source.pom" ).replace( "{0}", "(.*?)" ) + "$";
665 assertTrue( "Check failure message", getFailure().getReason().matches( pattern ) );
667 assertFalse( "check artifact rolled back", artifactFile.exists() );
668 assertFalse( "check metadata rolled back", artifactMetadataFile.exists() );
669 assertFalse( "check metadata rolled back", versionMetadataFile.exists() );
672 public void testMultipleArtifacts()
673 throws RepositoryConversionException, IOException
675 // test multiple artifacts are converted
677 List artifacts = new ArrayList();
678 artifacts.add( createArtifact( "test", "artifact-one", "1.0.0" ) );
679 artifacts.add( createArtifact( "test", "artifact-two", "1.0.0" ) );
680 artifacts.add( createArtifact( "test", "artifact-three", "1.0.0" ) );
681 repositoryConverter.convert( artifacts, targetRepository, reporter );
682 assertEquals( "check no errors", 0, reporter.getFailures() );
683 assertEquals( "check no warnings", 0, reporter.getWarnings() );
684 assertEquals( "check successes", 3, reporter.getSuccesses() );
686 for ( Iterator i = artifacts.iterator(); i.hasNext(); )
688 Artifact artifact = (Artifact) i.next();
690 File artifactFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
691 assertTrue( "Check artifact created", artifactFile.exists() );
692 assertTrue( "Check artifact matches", FileUtils.contentEquals( artifactFile, artifact.getFile() ) );
694 artifact = createPomArtifact( artifact );
695 File pomFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
696 File expectedPomFile =
697 getTestFile( "src/test/expected-files/converted-" + artifact.getArtifactId() + ".pom" );
698 assertTrue( "Check POM created", pomFile.exists() );
700 compareFiles( expectedPomFile, pomFile );
704 public void testInvalidSourceArtifactMetadata()
707 // test artifact is not converted when source metadata is invalid, and returns failure
709 createModernSourceRepository();
711 Artifact artifact = createArtifact( "test", "incorrectArtifactMetadata", "1.0.0" );
712 File file = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
715 repositoryConverter.convert( artifact, targetRepository, reporter );
717 assertEquals( "check failure message", getI18nString( "failure.incorrect.artifactMetadata.versions" ),
718 getFailure().getReason() );
720 assertFalse( "Check artifact not created", file.exists() );
722 ArtifactRepositoryMetadata metadata = new ArtifactRepositoryMetadata( artifact );
724 new File( targetRepository.getBasedir(), targetRepository.pathOfRemoteRepositoryMetadata( metadata ) );
725 assertFalse( "Check metadata not created", metadataFile.exists() );
728 public void testInvalidSourceSnapshotMetadata()
729 throws Exception, MalformedURLException
731 // test artifact is not converted when source snapshot metadata is invalid and returns failure
733 createModernSourceRepository();
735 Artifact artifact = createArtifact( "test", "incorrectSnapshotMetadata", "1.0.0-20060102.030405-6" );
736 File file = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
739 repositoryConverter.convert( artifact, targetRepository, reporter );
741 assertEquals( "check failure message", getI18nString( "failure.incorrect.snapshotMetadata.snapshot" ),
742 getFailure().getReason() );
744 assertFalse( "Check artifact not created", file.exists() );
746 ArtifactRepositoryMetadata metadata = new ArtifactRepositoryMetadata( artifact );
748 new File( targetRepository.getBasedir(), targetRepository.pathOfRemoteRepositoryMetadata( metadata ) );
749 assertFalse( "Check metadata not created", metadataFile.exists() );
752 public void testMergeArtifactMetadata()
753 throws RepositoryConversionException, IOException
755 // test artifact level metadata is merged when it already exists on successful conversion
757 Artifact artifact = createArtifact( "test", "newversion-artifact", "1.0.1" );
759 repositoryConverter.convert( artifact, targetRepository, reporter );
762 File artifactFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
763 assertTrue( "Check artifact created", artifactFile.exists() );
764 assertTrue( "Check artifact matches", FileUtils.contentEquals( artifactFile, artifact.getFile() ) );
766 artifact = createPomArtifact( artifact );
767 File pomFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
768 File sourcePomFile = new File( sourceRepository.getBasedir(), sourceRepository.pathOf( artifact ) );
769 assertTrue( "Check POM created", pomFile.exists() );
771 compareFiles( sourcePomFile, pomFile );
773 ArtifactMetadata artifactMetadata = new ArtifactRepositoryMetadata( artifact );
774 File artifactMetadataFile = new File( targetRepository.getBasedir(),
775 targetRepository.pathOfRemoteRepositoryMetadata( artifactMetadata ) );
776 assertTrue( "Check artifact metadata created", artifactMetadataFile.exists() );
778 File expectedMetadataFile = getTestFile( "src/test/expected-files/newversion-artifact-metadata.xml" );
780 compareFiles( expectedMetadataFile, artifactMetadataFile );
783 public void testSourceAndTargetRepositoriesMatch()
786 // test that it fails if the same
788 ArtifactRepositoryFactory factory = (ArtifactRepositoryFactory) lookup( ArtifactRepositoryFactory.ROLE );
790 sourceRepository = factory.createArtifactRepository( "source", targetRepository.getUrl(),
791 targetRepository.getLayout(), null, null );
793 Artifact artifact = createArtifact( "test", "repository-artifact", "1.0" );
797 repositoryConverter.convert( artifact, targetRepository, reporter );
798 fail( "Should have failed trying to convert within the same repository" );
800 catch ( RepositoryConversionException e )
803 assertEquals( "check message", getI18nString( "exception.repositories.match" ), e.getMessage() );
804 assertNull( "Check no additional cause", e.getCause() );
808 private Artifact createArtifact( String groupId, String artifactId, String version )
810 Matcher matcher = Artifact.VERSION_FILE_PATTERN.matcher( version );
812 if ( matcher.matches() )
814 baseVersion = matcher.group( 1 ) + "-SNAPSHOT";
818 baseVersion = version;
820 return createArtifact( groupId, artifactId, baseVersion, version, "jar" );
823 private Artifact createArtifact( String groupId, String artifactId, String baseVersion, String version,
826 Artifact artifact = artifactFactory.createArtifact( groupId, artifactId, version, null, type );
827 artifact.setBaseVersion( baseVersion );
828 artifact.setRepository( sourceRepository );
829 artifact.setFile( new File( sourceRepository.getBasedir(), sourceRepository.pathOf( artifact ) ) );
833 private Artifact createPomArtifact( Artifact artifact )
835 return createArtifact( artifact.getGroupId(), artifact.getArtifactId(), artifact.getBaseVersion(),
836 artifact.getVersion(), "pom" );
839 private static void compareFiles( File expectedPomFile, File pomFile )
842 String expectedContent = normalizeString( FileUtils.fileRead( expectedPomFile ) );
843 String targetContent = normalizeString( FileUtils.fileRead( pomFile ) );
844 assertEquals( "Check file match between " + expectedPomFile + " and " + pomFile, expectedContent,
848 private static String normalizeString( String path )
850 return path.trim().replaceAll( "\r\n", "\n" ).replace( '\r', '\n' );
853 private void checkSuccess()
855 assertEquals( "check no errors", 0, reporter.getFailures() );
856 assertEquals( "check no warnings", 0, reporter.getWarnings() );
857 assertEquals( "check success", 1, reporter.getSuccesses() );
860 private void checkFailure()
862 assertEquals( "check num errors", 1, reporter.getFailures() );
863 assertEquals( "check no warnings", 0, reporter.getWarnings() );
864 assertEquals( "check no success", 0, reporter.getSuccesses() );
867 private String getI18nString( String key )
869 return i18n.getString( repositoryConverter.getClass().getName(), Locale.getDefault(), key );
872 private ArtifactResult getFailure()
874 return (ArtifactResult) reporter.getArtifactFailureIterator().next();
877 private ArtifactResult getWarning()
879 return (ArtifactResult) reporter.getArtifactWarningIterator().next();
882 private void createModernSourceRepository()
885 ArtifactRepositoryFactory factory = (ArtifactRepositoryFactory) lookup( ArtifactRepositoryFactory.ROLE );
887 ArtifactRepositoryLayout layout = (ArtifactRepositoryLayout) lookup( ArtifactRepositoryLayout.ROLE, "default" );
889 File sourceBase = getTestFile( "src/test/source-modern-repository" );
891 factory.createArtifactRepository( "source", sourceBase.toURL().toString(), layout, null, null );