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 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 testV3PomConvertWithRelocation()
188 throws RepositoryConversionException, IOException
190 Artifact artifact = createArtifact( "test", "relocated-v3artifact", "1.0.0" );
191 ArtifactMetadata artifactMetadata = new ArtifactRepositoryMetadata( artifact );
192 File artifactMetadataFile = new File( targetRepository.getBasedir(),
193 targetRepository.pathOfRemoteRepositoryMetadata( artifactMetadata ) );
194 artifactMetadataFile.delete();
196 ArtifactMetadata versionMetadata = new SnapshotArtifactRepositoryMetadata( artifact );
197 File versionMetadataFile = new File( targetRepository.getBasedir(),
198 targetRepository.pathOfRemoteRepositoryMetadata( versionMetadata ) );
199 versionMetadataFile.delete();
201 repositoryConverter.convert( artifact, targetRepository, reporter );
202 //checkSuccess(); --> commented until MNG-2100 is fixed
204 File artifactFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
205 assertTrue( "Check if relocated artifact created", artifactFile.exists() );
206 assertTrue( "Check if relocated artifact matches",
207 FileUtils.contentEquals( artifactFile, artifact.getFile() ) );
208 Artifact pomArtifact = createArtifact( "relocated-test", "relocated-v3artifact", "1.0.0", "1.0.0", "pom" );
209 File pomFile = getTestFile( "src/test/expected-files/" + targetRepository.pathOf( pomArtifact ) );
210 File testFile = getTestFile( "target/test-target-repository/" + targetRepository.pathOf( pomArtifact ) );
211 assertTrue( "Check if expected relocated pom matches", FileUtils.contentEquals( pomFile, testFile ) );
213 Artifact orig = createArtifact( "test", "relocated-v3artifact", "1.0.0", "1.0.0", "pom" );
214 artifactFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( orig ) );
215 assertTrue( "Check if relocation artifact pom is created", artifactFile.exists() );
216 testFile = getTestFile( "src/test/expected-files/" + targetRepository.pathOf( orig ) );
217 assertTrue( "Check if expected artifact matches", FileUtils.contentEquals( artifactFile, testFile ) );
220 public void testV3PomWarningsOnConvert()
221 throws RepositoryConversionException, IOException
223 // test that the pom is converted but that warnings are reported
225 Artifact artifact = createArtifact( "test", "v3-warnings-artifact", "1.0.0" );
226 ArtifactMetadata artifactMetadata = new ArtifactRepositoryMetadata( artifact );
227 File artifactMetadataFile = new File( targetRepository.getBasedir(),
228 targetRepository.pathOfRemoteRepositoryMetadata( artifactMetadata ) );
229 artifactMetadataFile.delete();
231 ArtifactMetadata versionMetadata = new SnapshotArtifactRepositoryMetadata( artifact );
232 File versionMetadataFile = new File( targetRepository.getBasedir(),
233 targetRepository.pathOfRemoteRepositoryMetadata( versionMetadata ) );
234 versionMetadataFile.delete();
236 repositoryConverter.convert( artifact, targetRepository, reporter );
237 assertEquals( "check no errors", 0, reporter.getFailures() );
238 assertEquals( "check number of warnings", 2, reporter.getWarnings() );
239 assertEquals( "check success", 1, reporter.getSuccesses() );
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 expectedPomFile = getTestFile( "src/test/expected-files/converted-v3-warnings.pom" );
248 assertTrue( "Check POM created", pomFile.exists() );
250 compareFiles( expectedPomFile, pomFile );
252 // TODO: check 2 warnings (extend and versions) matched on i18n key
255 public void testV4SnapshotPomConvert()
256 throws IOException, RepositoryConversionException
258 // test that it is copied as is
260 Artifact artifact = createArtifact( "test", "v4artifact", "1.0.0-SNAPSHOT" );
261 ArtifactMetadata artifactMetadata = new ArtifactRepositoryMetadata( artifact );
262 File artifactMetadataFile = new File( targetRepository.getBasedir(),
263 targetRepository.pathOfRemoteRepositoryMetadata( artifactMetadata ) );
264 artifactMetadataFile.delete();
266 ArtifactMetadata snapshotMetadata = new SnapshotArtifactRepositoryMetadata( artifact );
267 File snapshotMetadataFile = new File( targetRepository.getBasedir(),
268 targetRepository.pathOfRemoteRepositoryMetadata( snapshotMetadata ) );
269 snapshotMetadataFile.delete();
271 repositoryConverter.convert( artifact, targetRepository, reporter );
274 File artifactFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
275 assertTrue( "Check artifact created", artifactFile.exists() );
276 assertTrue( "Check artifact matches", FileUtils.contentEquals( artifactFile, artifact.getFile() ) );
278 artifact = createPomArtifact( artifact );
279 File pomFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
280 File sourcePomFile = new File( sourceRepository.getBasedir(), sourceRepository.pathOf( artifact ) );
281 assertTrue( "Check POM created", pomFile.exists() );
283 compareFiles( sourcePomFile, pomFile );
285 assertTrue( "Check artifact metadata created", artifactMetadataFile.exists() );
287 File expectedMetadataFile = getTestFile( "src/test/expected-files/v4-snapshot-artifact-metadata.xml" );
289 compareFiles( expectedMetadataFile, artifactMetadataFile );
291 assertTrue( "Check snapshot metadata created", snapshotMetadataFile.exists() );
293 expectedMetadataFile = getTestFile( "src/test/expected-files/v4-snapshot-metadata.xml" );
295 compareFiles( expectedMetadataFile, snapshotMetadataFile );
298 public void testV3SnapshotPomConvert()
299 throws IOException, RepositoryConversionException
301 // test that the pom is coverted
303 Artifact artifact = createArtifact( "test", "v3artifact", "1.0.0-SNAPSHOT" );
304 ArtifactMetadata artifactMetadata = new ArtifactRepositoryMetadata( artifact );
305 File artifactMetadataFile = new File( targetRepository.getBasedir(),
306 targetRepository.pathOfRemoteRepositoryMetadata( artifactMetadata ) );
307 artifactMetadataFile.delete();
309 ArtifactMetadata snapshotMetadata = new SnapshotArtifactRepositoryMetadata( artifact );
310 File snapshotMetadataFile = new File( targetRepository.getBasedir(),
311 targetRepository.pathOfRemoteRepositoryMetadata( snapshotMetadata ) );
312 snapshotMetadataFile.delete();
314 repositoryConverter.convert( artifact, targetRepository, reporter );
317 File artifactFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
318 assertTrue( "Check artifact created", artifactFile.exists() );
319 assertTrue( "Check artifact matches", FileUtils.contentEquals( artifactFile, artifact.getFile() ) );
321 artifact = createPomArtifact( artifact );
322 File pomFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
323 File expectedPomFile = getTestFile( "src/test/expected-files/converted-v3-snapshot.pom" );
324 assertTrue( "Check POM created", pomFile.exists() );
326 compareFiles( expectedPomFile, pomFile );
328 assertTrue( "Check artifact metadata created", artifactMetadataFile.exists() );
330 File expectedMetadataFile = getTestFile( "src/test/expected-files/v3-snapshot-artifact-metadata.xml" );
332 compareFiles( expectedMetadataFile, artifactMetadataFile );
334 assertTrue( "Check snapshot metadata created", snapshotMetadataFile.exists() );
336 expectedMetadataFile = getTestFile( "src/test/expected-files/v3-snapshot-metadata.xml" );
338 compareFiles( expectedMetadataFile, snapshotMetadataFile );
341 public void testV4TimestampedSnapshotPomConvert()
342 throws IOException, RepositoryConversionException
344 // test that it is copied as is
346 Artifact artifact = createArtifact( "test", "v4artifact", "1.0.0-20060111.120115-1" );
347 ArtifactMetadata artifactMetadata = new ArtifactRepositoryMetadata( artifact );
348 File artifactMetadataFile = new File( targetRepository.getBasedir(),
349 targetRepository.pathOfRemoteRepositoryMetadata( artifactMetadata ) );
350 artifactMetadataFile.delete();
352 ArtifactMetadata snapshotMetadata = new SnapshotArtifactRepositoryMetadata( artifact );
353 File snapshotMetadataFile = new File( targetRepository.getBasedir(),
354 targetRepository.pathOfRemoteRepositoryMetadata( snapshotMetadata ) );
355 snapshotMetadataFile.delete();
357 repositoryConverter.convert( artifact, targetRepository, reporter );
360 File artifactFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
361 assertTrue( "Check artifact created", artifactFile.exists() );
362 assertTrue( "Check artifact matches", FileUtils.contentEquals( artifactFile, artifact.getFile() ) );
364 artifact = createPomArtifact( artifact );
365 File pomFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
366 File sourcePomFile = new File( sourceRepository.getBasedir(), sourceRepository.pathOf( artifact ) );
367 assertTrue( "Check POM created", pomFile.exists() );
369 compareFiles( sourcePomFile, pomFile );
371 assertTrue( "Check artifact metadata created", artifactMetadataFile.exists() );
373 File expectedMetadataFile = getTestFile( "src/test/expected-files/v4-snapshot-artifact-metadata.xml" );
375 compareFiles( expectedMetadataFile, artifactMetadataFile );
377 assertTrue( "Check snapshot metadata created", snapshotMetadataFile.exists() );
379 expectedMetadataFile = getTestFile( "src/test/expected-files/v4-timestamped-snapshot-metadata.xml" );
381 compareFiles( expectedMetadataFile, snapshotMetadataFile );
384 public void testV3TimestampedSnapshotPomConvert()
385 throws IOException, RepositoryConversionException
387 // test that the pom is coverted
389 Artifact artifact = createArtifact( "test", "v3artifact", "1.0.0-20060105.130101-3" );
390 ArtifactMetadata artifactMetadata = new ArtifactRepositoryMetadata( artifact );
391 File artifactMetadataFile = new File( targetRepository.getBasedir(),
392 targetRepository.pathOfRemoteRepositoryMetadata( artifactMetadata ) );
393 artifactMetadataFile.delete();
395 ArtifactMetadata snapshotMetadata = new SnapshotArtifactRepositoryMetadata( artifact );
396 File snapshotMetadataFile = new File( targetRepository.getBasedir(),
397 targetRepository.pathOfRemoteRepositoryMetadata( snapshotMetadata ) );
398 snapshotMetadataFile.delete();
400 repositoryConverter.convert( artifact, targetRepository, reporter );
403 File artifactFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
404 assertTrue( "Check artifact created", artifactFile.exists() );
405 assertTrue( "Check artifact matches", FileUtils.contentEquals( artifactFile, artifact.getFile() ) );
407 artifact = createPomArtifact( artifact );
408 File pomFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
409 File expectedPomFile = getTestFile( "src/test/expected-files/converted-v3-timestamped-snapshot.pom" );
410 assertTrue( "Check POM created", pomFile.exists() );
412 compareFiles( expectedPomFile, pomFile );
414 assertTrue( "Check artifact snapshotMetadata created", artifactMetadataFile.exists() );
416 File expectedMetadataFile = getTestFile( "src/test/expected-files/v3-snapshot-artifact-metadata.xml" );
418 compareFiles( expectedMetadataFile, artifactMetadataFile );
420 assertTrue( "Check snapshot snapshotMetadata created", snapshotMetadataFile.exists() );
422 expectedMetadataFile = getTestFile( "src/test/expected-files/v3-timestamped-snapshot-metadata.xml" );
424 compareFiles( expectedMetadataFile, snapshotMetadataFile );
427 public void testNoPomConvert()
428 throws IOException, RepositoryConversionException
430 // test that a POM is not created when there was none at the source
432 Artifact artifact = createArtifact( "test", "noPomArtifact", "1.0.0" );
433 repositoryConverter.convert( artifact, targetRepository, reporter );
434 assertEquals( "check no errors", 0, reporter.getFailures() );
435 assertEquals( "check no warnings", 1, reporter.getWarnings() );
436 assertEquals( "check success", 1, reporter.getSuccesses() );
437 assertEquals( "check warning message", getI18nString( "warning.missing.pom" ), getWarning().getReason() );
439 File artifactFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
440 assertTrue( "Check artifact created", artifactFile.exists() );
441 assertTrue( "Check artifact matches", FileUtils.contentEquals( artifactFile, artifact.getFile() ) );
443 artifact = createPomArtifact( artifact );
444 File pomFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
445 File sourcePomFile = new File( sourceRepository.getBasedir(), sourceRepository.pathOf( artifact ) );
447 assertFalse( "Check no POM created", pomFile.exists() );
448 assertFalse( "No source POM", sourcePomFile.exists() );
451 public void testIncorrectSourceChecksumMd5()
452 throws RepositoryConversionException
454 // test that it fails when the source md5 is wrong
456 Artifact artifact = createArtifact( "test", "incorrectMd5Artifact", "1.0.0" );
457 File file = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
460 repositoryConverter.convert( artifact, targetRepository, reporter );
462 assertEquals( "check failure message", getI18nString( "failure.incorrect.md5" ), getFailure().getReason() );
464 assertFalse( "Check artifact not created", file.exists() );
466 ArtifactRepositoryMetadata metadata = new ArtifactRepositoryMetadata( artifact );
468 new File( targetRepository.getBasedir(), targetRepository.pathOfRemoteRepositoryMetadata( metadata ) );
469 assertFalse( "Check metadata not created", metadataFile.exists() );
472 public void testIncorrectSourceChecksumSha1()
473 throws RepositoryConversionException
475 // test that it fails when the source sha1 is wrong
477 Artifact artifact = createArtifact( "test", "incorrectSha1Artifact", "1.0.0" );
478 File file = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
481 repositoryConverter.convert( artifact, targetRepository, reporter );
483 assertEquals( "check failure message", getI18nString( "failure.incorrect.sha1" ), getFailure().getReason() );
485 assertFalse( "Check artifact not created", file.exists() );
487 ArtifactRepositoryMetadata metadata = new ArtifactRepositoryMetadata( artifact );
489 new File( targetRepository.getBasedir(), targetRepository.pathOfRemoteRepositoryMetadata( metadata ) );
490 assertFalse( "Check metadata not created", metadataFile.exists() );
493 public void testUnmodifiedArtifact()
494 throws RepositoryConversionException, IOException, InterruptedException
496 // test the unmodified artifact is untouched
498 Artifact artifact = createArtifact( "test", "unmodified-artifact", "1.0.0" );
499 Artifact pomArtifact = createPomArtifact( artifact );
501 File sourceFile = new File( sourceRepository.getBasedir(), sourceRepository.pathOf( artifact ) );
502 File sourcePomFile = new File( sourceRepository.getBasedir(), sourceRepository.pathOf( pomArtifact ) );
503 File targetFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
504 File targetPomFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( pomArtifact ) );
506 assertTrue( "Check target file exists", targetFile.exists() );
507 assertTrue( "Check target POM exists", targetPomFile.exists() );
509 sourceFile.setLastModified( System.currentTimeMillis() );
510 sourcePomFile.setLastModified( System.currentTimeMillis() );
512 long origTime = targetFile.lastModified();
513 long origPomTime = targetPomFile.lastModified();
515 // Need to guarantee last modified is not equal
516 Thread.sleep( SLEEP_MILLIS );
518 repositoryConverter.convert( artifact, targetRepository, reporter );
521 compareFiles( sourceFile, targetFile );
522 compareFiles( sourcePomFile, targetPomFile );
524 assertEquals( "Check unmodified", origTime, targetFile.lastModified() );
525 assertEquals( "Check unmodified", origPomTime, targetPomFile.lastModified() );
528 public void testModifedArtifactFails()
529 throws InterruptedException, RepositoryConversionException, IOException
531 // test that it fails when the source artifact has changed and is different to the existing artifact in the
534 Artifact artifact = createArtifact( "test", "modified-artifact", "1.0.0" );
535 Artifact pomArtifact = createPomArtifact( artifact );
537 File sourceFile = new File( sourceRepository.getBasedir(), sourceRepository.pathOf( artifact ) );
538 File sourcePomFile = new File( sourceRepository.getBasedir(), sourceRepository.pathOf( pomArtifact ) );
539 File targetFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
540 File targetPomFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( pomArtifact ) );
542 assertTrue( "Check target file exists", targetFile.exists() );
543 assertTrue( "Check target POM exists", targetPomFile.exists() );
545 sourceFile.setLastModified( System.currentTimeMillis() );
546 sourcePomFile.setLastModified( System.currentTimeMillis() );
548 long origTime = targetFile.lastModified();
549 long origPomTime = targetPomFile.lastModified();
551 // Need to guarantee last modified is not equal
552 Thread.sleep( SLEEP_MILLIS );
554 repositoryConverter.convert( artifact, targetRepository, reporter );
556 assertEquals( "Check failure message", getI18nString( "failure.target.already.exists" ),
557 getFailure().getReason() );
559 assertEquals( "Check unmodified", origTime, targetFile.lastModified() );
560 assertEquals( "Check unmodified", origPomTime, targetPomFile.lastModified() );
562 ArtifactRepositoryMetadata metadata = new ArtifactRepositoryMetadata( artifact );
564 new File( targetRepository.getBasedir(), targetRepository.pathOfRemoteRepositoryMetadata( metadata ) );
565 assertFalse( "Check metadata not created", metadataFile.exists() );
568 public void testForcedUnmodifiedArtifact()
569 throws Exception, IOException
571 // test unmodified artifact is still converted when set to force
573 repositoryConverter = (RepositoryConverter) lookup( RepositoryConverter.ROLE, "force-repository-converter" );
575 Artifact artifact = createArtifact( "test", "unmodified-artifact", "1.0.0" );
576 Artifact pomArtifact = createPomArtifact( artifact );
578 File sourceFile = new File( sourceRepository.getBasedir(), sourceRepository.pathOf( artifact ) );
579 File sourcePomFile = new File( sourceRepository.getBasedir(), sourceRepository.pathOf( pomArtifact ) );
580 File targetFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
581 File targetPomFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( pomArtifact ) );
583 long origTime = targetFile.lastModified();
584 long origPomTime = targetPomFile.lastModified();
586 sourceFile.setLastModified( System.currentTimeMillis() );
587 sourcePomFile.setLastModified( System.currentTimeMillis() );
589 // Need to guarantee last modified is not equal
590 Thread.sleep( SLEEP_MILLIS );
592 repositoryConverter.convert( artifact, targetRepository, reporter );
595 compareFiles( sourceFile, targetFile );
596 compareFiles( sourcePomFile, targetPomFile );
598 assertFalse( "Check modified", origTime == targetFile.lastModified() );
599 assertFalse( "Check modified", origPomTime == targetPomFile.lastModified() );
601 ArtifactRepositoryMetadata metadata = new ArtifactRepositoryMetadata( artifact );
603 new File( targetRepository.getBasedir(), targetRepository.pathOfRemoteRepositoryMetadata( metadata ) );
604 assertTrue( "Check metadata created", metadataFile.exists() );
607 public void testDryRunSuccess()
610 // test dry run does nothing on a run that will be successful, and returns success
612 repositoryConverter = (RepositoryConverter) lookup( RepositoryConverter.ROLE, "dryrun-repository-converter" );
614 Artifact artifact = createArtifact( "test", "dryrun-artifact", "1.0.0" );
615 Artifact pomArtifact = createPomArtifact( artifact );
617 File sourceFile = new File( sourceRepository.getBasedir(), sourceRepository.pathOf( artifact ) );
618 File sourcePomFile = new File( sourceRepository.getBasedir(), sourceRepository.pathOf( pomArtifact ) );
619 File targetFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
620 File targetPomFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( pomArtifact ) );
622 repositoryConverter.convert( artifact, targetRepository, reporter );
625 assertTrue( "Check source file exists", sourceFile.exists() );
626 assertTrue( "Check source POM exists", sourcePomFile.exists() );
628 assertFalse( "Check target file doesn't exist", targetFile.exists() );
629 assertFalse( "Check target POM doesn't exist", targetPomFile.exists() );
631 ArtifactRepositoryMetadata metadata = new ArtifactRepositoryMetadata( artifact );
633 new File( targetRepository.getBasedir(), targetRepository.pathOfRemoteRepositoryMetadata( metadata ) );
634 assertFalse( "Check metadata not created", metadataFile.exists() );
637 public void testDryRunFailure()
640 // test dry run does nothing on a run that will fail, and returns failure
642 repositoryConverter = (RepositoryConverter) lookup( RepositoryConverter.ROLE, "dryrun-repository-converter" );
644 Artifact artifact = createArtifact( "test", "modified-artifact", "1.0.0" );
645 Artifact pomArtifact = createPomArtifact( artifact );
647 File sourceFile = new File( sourceRepository.getBasedir(), sourceRepository.pathOf( artifact ) );
648 File sourcePomFile = new File( sourceRepository.getBasedir(), sourceRepository.pathOf( pomArtifact ) );
649 File targetFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
650 File targetPomFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( pomArtifact ) );
652 assertTrue( "Check target file exists", targetFile.exists() );
653 assertTrue( "Check target POM exists", targetPomFile.exists() );
655 sourceFile.setLastModified( System.currentTimeMillis() );
656 sourcePomFile.setLastModified( System.currentTimeMillis() );
658 long origTime = targetFile.lastModified();
659 long origPomTime = targetPomFile.lastModified();
661 // Need to guarantee last modified is not equal
662 Thread.sleep( SLEEP_MILLIS );
664 repositoryConverter.convert( artifact, targetRepository, reporter );
666 assertEquals( "Check failure message", getI18nString( "failure.target.already.exists" ),
667 getFailure().getReason() );
669 assertEquals( "Check unmodified", origTime, targetFile.lastModified() );
670 assertEquals( "Check unmodified", origPomTime, targetPomFile.lastModified() );
672 ArtifactRepositoryMetadata metadata = new ArtifactRepositoryMetadata( artifact );
674 new File( targetRepository.getBasedir(), targetRepository.pathOfRemoteRepositoryMetadata( metadata ) );
675 assertFalse( "Check metadata not created", metadataFile.exists() );
678 public void testRollbackArtifactCreated()
679 throws RepositoryConversionException, IOException
681 // test rollback can remove a created artifact, including checksums
683 Artifact artifact = createArtifact( "test", "rollback-created-artifact", "1.0.0" );
684 ArtifactMetadata artifactMetadata = new ArtifactRepositoryMetadata( artifact );
685 File artifactMetadataFile = new File( targetRepository.getBasedir(),
686 targetRepository.pathOfRemoteRepositoryMetadata( artifactMetadata ) );
687 FileUtils.deleteDirectory( artifactMetadataFile.getParentFile() );
689 ArtifactMetadata versionMetadata = new SnapshotArtifactRepositoryMetadata( artifact );
690 File versionMetadataFile = new File( targetRepository.getBasedir(),
691 targetRepository.pathOfRemoteRepositoryMetadata( versionMetadata ) );
693 File artifactFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
695 repositoryConverter.convert( artifact, targetRepository, reporter );
697 String pattern = "^" + getI18nString( "failure.invalid.source.pom" ).replaceFirst( "\\{0\\}", ".*" ) + "$";
698 assertTrue( "Check failure message", getFailure().getReason().matches( pattern ) );
700 assertFalse( "check artifact rolled back", artifactFile.exists() );
701 assertFalse( "check metadata rolled back", artifactMetadataFile.exists() );
702 assertFalse( "check metadata rolled back", versionMetadataFile.exists() );
705 public void testMultipleArtifacts()
706 throws RepositoryConversionException, IOException
708 // test multiple artifacts are converted
710 List artifacts = new ArrayList();
711 artifacts.add( createArtifact( "test", "artifact-one", "1.0.0" ) );
712 artifacts.add( createArtifact( "test", "artifact-two", "1.0.0" ) );
713 artifacts.add( createArtifact( "test", "artifact-three", "1.0.0" ) );
714 repositoryConverter.convert( artifacts, targetRepository, reporter );
715 assertEquals( "check no errors", 0, reporter.getFailures() );
716 assertEquals( "check no warnings", 0, reporter.getWarnings() );
717 assertEquals( "check successes", 3, reporter.getSuccesses() );
719 for ( Iterator i = artifacts.iterator(); i.hasNext(); )
721 Artifact artifact = (Artifact) i.next();
723 File artifactFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
724 assertTrue( "Check artifact created", artifactFile.exists() );
725 assertTrue( "Check artifact matches", FileUtils.contentEquals( artifactFile, artifact.getFile() ) );
727 artifact = createPomArtifact( artifact );
728 File pomFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
729 File expectedPomFile =
730 getTestFile( "src/test/expected-files/converted-" + artifact.getArtifactId() + ".pom" );
731 assertTrue( "Check POM created", pomFile.exists() );
733 compareFiles( expectedPomFile, pomFile );
737 public void testInvalidSourceArtifactMetadata()
740 // test artifact is not converted when source metadata is invalid, and returns failure
742 createModernSourceRepository();
744 Artifact artifact = createArtifact( "test", "incorrectArtifactMetadata", "1.0.0" );
745 File file = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
748 repositoryConverter.convert( artifact, targetRepository, reporter );
750 assertEquals( "check failure message", getI18nString( "failure.incorrect.artifactMetadata.versions" ),
751 getFailure().getReason() );
753 assertFalse( "Check artifact not created", file.exists() );
755 ArtifactRepositoryMetadata metadata = new ArtifactRepositoryMetadata( artifact );
757 new File( targetRepository.getBasedir(), targetRepository.pathOfRemoteRepositoryMetadata( metadata ) );
758 assertFalse( "Check metadata not created", metadataFile.exists() );
761 public void testInvalidSourceSnapshotMetadata()
762 throws Exception, MalformedURLException
764 // test artifact is not converted when source snapshot metadata is invalid and returns failure
766 createModernSourceRepository();
768 Artifact artifact = createArtifact( "test", "incorrectSnapshotMetadata", "1.0.0-20060102.030405-6" );
769 File file = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
772 repositoryConverter.convert( artifact, targetRepository, reporter );
774 assertEquals( "check failure message", getI18nString( "failure.incorrect.snapshotMetadata.snapshot" ),
775 getFailure().getReason() );
777 assertFalse( "Check artifact not created", file.exists() );
779 ArtifactRepositoryMetadata metadata = new ArtifactRepositoryMetadata( artifact );
781 new File( targetRepository.getBasedir(), targetRepository.pathOfRemoteRepositoryMetadata( metadata ) );
782 assertFalse( "Check metadata not created", metadataFile.exists() );
785 public void testMergeArtifactMetadata()
786 throws RepositoryConversionException, IOException
788 // test artifact level metadata is merged when it already exists on successful conversion
790 Artifact artifact = createArtifact( "test", "newversion-artifact", "1.0.1" );
792 repositoryConverter.convert( artifact, targetRepository, reporter );
795 File artifactFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
796 assertTrue( "Check artifact created", artifactFile.exists() );
797 assertTrue( "Check artifact matches", FileUtils.contentEquals( artifactFile, artifact.getFile() ) );
799 artifact = createPomArtifact( artifact );
800 File pomFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
801 File sourcePomFile = new File( sourceRepository.getBasedir(), sourceRepository.pathOf( artifact ) );
802 assertTrue( "Check POM created", pomFile.exists() );
804 compareFiles( sourcePomFile, pomFile );
806 ArtifactMetadata artifactMetadata = new ArtifactRepositoryMetadata( artifact );
807 File artifactMetadataFile = new File( targetRepository.getBasedir(),
808 targetRepository.pathOfRemoteRepositoryMetadata( artifactMetadata ) );
809 assertTrue( "Check artifact metadata created", artifactMetadataFile.exists() );
811 File expectedMetadataFile = getTestFile( "src/test/expected-files/newversion-artifact-metadata.xml" );
813 compareFiles( expectedMetadataFile, artifactMetadataFile );
816 public void testSourceAndTargetRepositoriesMatch()
819 // test that it fails if the same
821 ArtifactRepositoryFactory factory = (ArtifactRepositoryFactory) lookup( ArtifactRepositoryFactory.ROLE );
823 sourceRepository = factory.createArtifactRepository( "source", targetRepository.getUrl(),
824 targetRepository.getLayout(), null, null );
826 Artifact artifact = createArtifact( "test", "repository-artifact", "1.0" );
830 repositoryConverter.convert( artifact, targetRepository, reporter );
831 fail( "Should have failed trying to convert within the same repository" );
833 catch ( RepositoryConversionException e )
836 assertEquals( "check message", getI18nString( "exception.repositories.match" ), e.getMessage() );
837 assertNull( "Check no additional cause", e.getCause() );
841 private Artifact createArtifact( String groupId, String artifactId, String version )
843 Matcher matcher = Artifact.VERSION_FILE_PATTERN.matcher( version );
845 if ( matcher.matches() )
847 baseVersion = matcher.group( 1 ) + "-SNAPSHOT";
851 baseVersion = version;
853 return createArtifact( groupId, artifactId, baseVersion, version, "jar" );
856 private Artifact createArtifact( String groupId, String artifactId, String baseVersion, String version,
859 Artifact artifact = artifactFactory.createArtifact( groupId, artifactId, version, null, type );
860 artifact.setBaseVersion( baseVersion );
861 artifact.setRepository( sourceRepository );
862 artifact.setFile( new File( sourceRepository.getBasedir(), sourceRepository.pathOf( artifact ) ) );
866 private Artifact createPomArtifact( Artifact artifact )
868 return createArtifact( artifact.getGroupId(), artifact.getArtifactId(), artifact.getBaseVersion(),
869 artifact.getVersion(), "pom" );
872 private static void compareFiles( File expectedPomFile, File pomFile )
875 String expectedContent = normalizeString( FileUtils.fileRead( expectedPomFile ) );
876 String targetContent = normalizeString( FileUtils.fileRead( pomFile ) );
877 assertEquals( "Check file match between " + expectedPomFile + " and " + pomFile, expectedContent,
881 private static String normalizeString( String path )
883 return path.trim().replaceAll( "\r\n", "\n" ).replace( '\r', '\n' );
886 private void checkSuccess()
888 assertEquals( "check no errors", 0, reporter.getFailures() );
889 assertEquals( "check no warnings", 0, reporter.getWarnings() );
890 assertEquals( "check success", 1, reporter.getSuccesses() );
893 private void checkFailure()
895 assertEquals( "check num errors", 1, reporter.getFailures() );
896 assertEquals( "check no warnings", 0, reporter.getWarnings() );
897 assertEquals( "check no success", 0, reporter.getSuccesses() );
900 private String getI18nString( String key )
902 return i18n.getString( repositoryConverter.getClass().getName(), Locale.getDefault(), key );
905 private ArtifactResult getFailure()
907 return (ArtifactResult) reporter.getArtifactFailureIterator().next();
910 private ArtifactResult getWarning()
912 return (ArtifactResult) reporter.getArtifactWarningIterator().next();
915 private void createModernSourceRepository()
918 ArtifactRepositoryFactory factory = (ArtifactRepositoryFactory) lookup( ArtifactRepositoryFactory.ROLE );
920 ArtifactRepositoryLayout layout = (ArtifactRepositoryLayout) lookup( ArtifactRepositoryLayout.ROLE, "default" );
922 File sourceBase = getTestFile( "src/test/source-modern-repository" );
924 factory.createArtifactRepository( "source", sourceBase.toURL().toString(), layout, null, null );
927 private void copyDirectoryStructure( File sourceDirectory, File destinationDirectory )
930 if ( !sourceDirectory.exists() )
932 throw new IOException( "Source directory doesn't exists (" + sourceDirectory.getAbsolutePath() + ")." );
935 File[] files = sourceDirectory.listFiles();
937 String sourcePath = sourceDirectory.getAbsolutePath();
939 for ( int i = 0; i < files.length; i++ )
941 File file = files[i];
943 String dest = file.getAbsolutePath();
945 dest = dest.substring( sourcePath.length() + 1 );
947 File destination = new File( destinationDirectory, dest );
951 destination = destination.getParentFile();
953 FileUtils.copyFileToDirectory( file, destination );
955 else if ( file.isDirectory() )
957 if ( !file.getName().equals( ".svn" ) )
959 if ( !destination.exists() && !destination.mkdirs() )
961 throw new IOException(
962 "Could not create destination directory '" + destination.getAbsolutePath() + "'." );
965 copyDirectoryStructure( file, destination );
970 throw new IOException( "Unknown file type: " + file.getAbsolutePath() );