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 public void testV4PomConvert()
101 throws IOException, RepositoryConversionException
103 // test that it is copied as is
105 Artifact artifact = createArtifact( "test", "v4artifact", "1.0.0" );
106 ArtifactMetadata artifactMetadata = new ArtifactRepositoryMetadata( artifact );
107 File artifactMetadataFile = new File( targetRepository.getBasedir(),
108 targetRepository.pathOfRemoteRepositoryMetadata( artifactMetadata ) );
109 artifactMetadataFile.delete();
111 ArtifactMetadata versionMetadata = new SnapshotArtifactRepositoryMetadata( artifact );
112 File versionMetadataFile = new File( targetRepository.getBasedir(),
113 targetRepository.pathOfRemoteRepositoryMetadata( versionMetadata ) );
114 versionMetadataFile.delete();
116 File artifactFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
117 artifactFile.delete();
119 repositoryConverter.convert( artifact, targetRepository, reporter );
122 assertTrue( "Check artifact created", artifactFile.exists() );
123 assertTrue( "Check artifact matches", FileUtils.contentEquals( artifactFile, artifact.getFile() ) );
125 artifact = createPomArtifact( artifact );
126 File pomFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
127 File sourcePomFile = new File( sourceRepository.getBasedir(), sourceRepository.pathOf( artifact ) );
128 assertTrue( "Check POM created", pomFile.exists() );
130 compareFiles( sourcePomFile, pomFile );
132 assertTrue( "Check artifact metadata created", artifactMetadataFile.exists() );
134 File expectedMetadataFile = getTestFile( "src/test/expected-files/v4-artifact-metadata.xml" );
136 compareFiles( expectedMetadataFile, artifactMetadataFile );
138 assertTrue( "Check snapshot metadata created", versionMetadataFile.exists() );
140 expectedMetadataFile = getTestFile( "src/test/expected-files/v4-version-metadata.xml" );
142 compareFiles( expectedMetadataFile, versionMetadataFile );
145 public void testV3PomConvert()
146 throws IOException, RepositoryConversionException
148 // test that the pom is coverted
150 Artifact artifact = createArtifact( "test", "v3artifact", "1.0.0" );
151 ArtifactMetadata artifactMetadata = new ArtifactRepositoryMetadata( artifact );
152 File artifactMetadataFile = new File( targetRepository.getBasedir(),
153 targetRepository.pathOfRemoteRepositoryMetadata( artifactMetadata ) );
154 artifactMetadataFile.delete();
156 ArtifactMetadata versionMetadata = new SnapshotArtifactRepositoryMetadata( artifact );
157 File versionMetadataFile = new File( targetRepository.getBasedir(),
158 targetRepository.pathOfRemoteRepositoryMetadata( versionMetadata ) );
159 versionMetadataFile.delete();
161 repositoryConverter.convert( artifact, targetRepository, reporter );
164 File artifactFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
165 assertTrue( "Check artifact created", artifactFile.exists() );
166 assertTrue( "Check artifact matches", FileUtils.contentEquals( artifactFile, artifact.getFile() ) );
168 artifact = createPomArtifact( artifact );
169 File pomFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
170 File expectedPomFile = getTestFile( "src/test/expected-files/converted-v3.pom" );
171 assertTrue( "Check POM created", pomFile.exists() );
173 compareFiles( expectedPomFile, pomFile );
175 assertTrue( "Check artifact metadata created", artifactMetadataFile.exists() );
177 File expectedMetadataFile = getTestFile( "src/test/expected-files/v3-artifact-metadata.xml" );
179 compareFiles( expectedMetadataFile, artifactMetadataFile );
181 assertTrue( "Check snapshot metadata created", versionMetadataFile.exists() );
183 expectedMetadataFile = getTestFile( "src/test/expected-files/v3-version-metadata.xml" );
185 compareFiles( expectedMetadataFile, versionMetadataFile );
188 public void testV3PomConvertWithRelocation()
189 throws RepositoryConversionException, IOException
191 Artifact artifact = createArtifact( "test", "relocated-v3artifact", "1.0.0" );
192 ArtifactMetadata artifactMetadata = new ArtifactRepositoryMetadata( artifact );
193 File artifactMetadataFile = new File( targetRepository.getBasedir(),
194 targetRepository.pathOfRemoteRepositoryMetadata( artifactMetadata ) );
195 artifactMetadataFile.delete();
197 ArtifactMetadata versionMetadata = new SnapshotArtifactRepositoryMetadata( artifact );
198 File versionMetadataFile = new File( targetRepository.getBasedir(),
199 targetRepository.pathOfRemoteRepositoryMetadata( versionMetadata ) );
200 versionMetadataFile.delete();
202 repositoryConverter.convert( artifact, targetRepository, reporter );
203 //checkSuccess(); --> commented until MNG-2100 is fixed
205 File artifactFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
206 assertTrue( "Check if relocated artifact created", artifactFile.exists() );
207 assertTrue( "Check if relocated artifact matches",
208 FileUtils.contentEquals( artifactFile, artifact.getFile() ) );
209 Artifact pomArtifact = createArtifact( "relocated-test", "relocated-v3artifact", "1.0.0", "1.0.0", "pom" );
210 File pomFile = getTestFile( "src/test/expected-files/" + targetRepository.pathOf( pomArtifact ) );
211 File testFile = getTestFile( "target/test-target-repository/" + targetRepository.pathOf( pomArtifact ) );
212 compareFiles( pomFile, testFile );
214 Artifact orig = createArtifact( "test", "relocated-v3artifact", "1.0.0", "1.0.0", "pom" );
215 artifactFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( orig ) );
216 assertTrue( "Check if relocation artifact pom is created", artifactFile.exists() );
217 testFile = getTestFile( "src/test/expected-files/" + targetRepository.pathOf( orig ) );
218 compareFiles( artifactFile, testFile );
221 public void testV3PomWarningsOnConvert()
222 throws RepositoryConversionException, IOException
224 // test that the pom is converted but that warnings are reported
226 Artifact artifact = createArtifact( "test", "v3-warnings-artifact", "1.0.0" );
227 ArtifactMetadata artifactMetadata = new ArtifactRepositoryMetadata( artifact );
228 File artifactMetadataFile = new File( targetRepository.getBasedir(),
229 targetRepository.pathOfRemoteRepositoryMetadata( artifactMetadata ) );
230 artifactMetadataFile.delete();
232 ArtifactMetadata versionMetadata = new SnapshotArtifactRepositoryMetadata( artifact );
233 File versionMetadataFile = new File( targetRepository.getBasedir(),
234 targetRepository.pathOfRemoteRepositoryMetadata( versionMetadata ) );
235 versionMetadataFile.delete();
237 repositoryConverter.convert( artifact, targetRepository, reporter );
238 assertEquals( "check no errors", 0, reporter.getFailures() );
239 assertEquals( "check number of warnings", 2, reporter.getWarnings() );
240 assertEquals( "check success", 1, reporter.getSuccesses() );
242 File artifactFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
243 assertTrue( "Check artifact created", artifactFile.exists() );
244 assertTrue( "Check artifact matches", FileUtils.contentEquals( artifactFile, artifact.getFile() ) );
246 artifact = createPomArtifact( artifact );
247 File pomFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
248 File expectedPomFile = getTestFile( "src/test/expected-files/converted-v3-warnings.pom" );
249 assertTrue( "Check POM created", pomFile.exists() );
251 compareFiles( expectedPomFile, pomFile );
253 // TODO: check 2 warnings (extend and versions) matched on i18n key
256 public void testV4SnapshotPomConvert()
257 throws IOException, RepositoryConversionException
259 // test that it is copied as is
261 Artifact artifact = createArtifact( "test", "v4artifact", "1.0.0-SNAPSHOT" );
262 ArtifactMetadata artifactMetadata = new ArtifactRepositoryMetadata( artifact );
263 File artifactMetadataFile = new File( targetRepository.getBasedir(),
264 targetRepository.pathOfRemoteRepositoryMetadata( artifactMetadata ) );
265 artifactMetadataFile.delete();
267 ArtifactMetadata snapshotMetadata = new SnapshotArtifactRepositoryMetadata( artifact );
268 File snapshotMetadataFile = new File( targetRepository.getBasedir(),
269 targetRepository.pathOfRemoteRepositoryMetadata( snapshotMetadata ) );
270 snapshotMetadataFile.delete();
272 repositoryConverter.convert( artifact, targetRepository, reporter );
275 File artifactFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
276 assertTrue( "Check artifact created", artifactFile.exists() );
277 assertTrue( "Check artifact matches", FileUtils.contentEquals( artifactFile, artifact.getFile() ) );
279 artifact = createPomArtifact( artifact );
280 File pomFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
281 File sourcePomFile = new File( sourceRepository.getBasedir(), sourceRepository.pathOf( artifact ) );
282 assertTrue( "Check POM created", pomFile.exists() );
284 compareFiles( sourcePomFile, pomFile );
286 assertTrue( "Check artifact metadata created", artifactMetadataFile.exists() );
288 File expectedMetadataFile = getTestFile( "src/test/expected-files/v4-snapshot-artifact-metadata.xml" );
290 compareFiles( expectedMetadataFile, artifactMetadataFile );
292 assertTrue( "Check snapshot metadata created", snapshotMetadataFile.exists() );
294 expectedMetadataFile = getTestFile( "src/test/expected-files/v4-snapshot-metadata.xml" );
296 compareFiles( expectedMetadataFile, snapshotMetadataFile );
299 public void testV3SnapshotPomConvert()
300 throws IOException, RepositoryConversionException
302 // test that the pom is coverted
304 Artifact artifact = createArtifact( "test", "v3artifact", "1.0.0-SNAPSHOT" );
305 ArtifactMetadata artifactMetadata = new ArtifactRepositoryMetadata( artifact );
306 File artifactMetadataFile = new File( targetRepository.getBasedir(),
307 targetRepository.pathOfRemoteRepositoryMetadata( artifactMetadata ) );
308 artifactMetadataFile.delete();
310 ArtifactMetadata snapshotMetadata = new SnapshotArtifactRepositoryMetadata( artifact );
311 File snapshotMetadataFile = new File( targetRepository.getBasedir(),
312 targetRepository.pathOfRemoteRepositoryMetadata( snapshotMetadata ) );
313 snapshotMetadataFile.delete();
315 repositoryConverter.convert( artifact, targetRepository, reporter );
318 File artifactFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
319 assertTrue( "Check artifact created", artifactFile.exists() );
320 assertTrue( "Check artifact matches", FileUtils.contentEquals( artifactFile, artifact.getFile() ) );
322 artifact = createPomArtifact( artifact );
323 File pomFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
324 File expectedPomFile = getTestFile( "src/test/expected-files/converted-v3-snapshot.pom" );
325 assertTrue( "Check POM created", pomFile.exists() );
327 compareFiles( expectedPomFile, pomFile );
329 assertTrue( "Check artifact metadata created", artifactMetadataFile.exists() );
331 File expectedMetadataFile = getTestFile( "src/test/expected-files/v3-snapshot-artifact-metadata.xml" );
333 compareFiles( expectedMetadataFile, artifactMetadataFile );
335 assertTrue( "Check snapshot metadata created", snapshotMetadataFile.exists() );
337 expectedMetadataFile = getTestFile( "src/test/expected-files/v3-snapshot-metadata.xml" );
339 compareFiles( expectedMetadataFile, snapshotMetadataFile );
342 public void testV4TimestampedSnapshotPomConvert()
343 throws IOException, RepositoryConversionException
345 // test that it is copied as is
347 Artifact artifact = createArtifact( "test", "v4artifact", "1.0.0-20060111.120115-1" );
348 ArtifactMetadata artifactMetadata = new ArtifactRepositoryMetadata( artifact );
349 File artifactMetadataFile = new File( targetRepository.getBasedir(),
350 targetRepository.pathOfRemoteRepositoryMetadata( artifactMetadata ) );
351 artifactMetadataFile.delete();
353 ArtifactMetadata snapshotMetadata = new SnapshotArtifactRepositoryMetadata( artifact );
354 File snapshotMetadataFile = new File( targetRepository.getBasedir(),
355 targetRepository.pathOfRemoteRepositoryMetadata( snapshotMetadata ) );
356 snapshotMetadataFile.delete();
358 repositoryConverter.convert( artifact, targetRepository, reporter );
361 File artifactFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
362 assertTrue( "Check artifact created", artifactFile.exists() );
363 assertTrue( "Check artifact matches", FileUtils.contentEquals( artifactFile, artifact.getFile() ) );
365 artifact = createPomArtifact( artifact );
366 File pomFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
367 File sourcePomFile = new File( sourceRepository.getBasedir(), sourceRepository.pathOf( artifact ) );
368 assertTrue( "Check POM created", pomFile.exists() );
370 compareFiles( sourcePomFile, pomFile );
372 assertTrue( "Check artifact metadata created", artifactMetadataFile.exists() );
374 File expectedMetadataFile = getTestFile( "src/test/expected-files/v4-snapshot-artifact-metadata.xml" );
376 compareFiles( expectedMetadataFile, artifactMetadataFile );
378 assertTrue( "Check snapshot metadata created", snapshotMetadataFile.exists() );
380 expectedMetadataFile = getTestFile( "src/test/expected-files/v4-timestamped-snapshot-metadata.xml" );
382 compareFiles( expectedMetadataFile, snapshotMetadataFile );
385 public void testV3TimestampedSnapshotPomConvert()
386 throws IOException, RepositoryConversionException
388 // test that the pom is coverted
390 Artifact artifact = createArtifact( "test", "v3artifact", "1.0.0-20060105.130101-3" );
391 ArtifactMetadata artifactMetadata = new ArtifactRepositoryMetadata( artifact );
392 File artifactMetadataFile = new File( targetRepository.getBasedir(),
393 targetRepository.pathOfRemoteRepositoryMetadata( artifactMetadata ) );
394 artifactMetadataFile.delete();
396 ArtifactMetadata snapshotMetadata = new SnapshotArtifactRepositoryMetadata( artifact );
397 File snapshotMetadataFile = new File( targetRepository.getBasedir(),
398 targetRepository.pathOfRemoteRepositoryMetadata( snapshotMetadata ) );
399 snapshotMetadataFile.delete();
401 repositoryConverter.convert( artifact, targetRepository, reporter );
404 File artifactFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
405 assertTrue( "Check artifact created", artifactFile.exists() );
406 assertTrue( "Check artifact matches", FileUtils.contentEquals( artifactFile, artifact.getFile() ) );
408 artifact = createPomArtifact( artifact );
409 File pomFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
410 File expectedPomFile = getTestFile( "src/test/expected-files/converted-v3-timestamped-snapshot.pom" );
411 assertTrue( "Check POM created", pomFile.exists() );
413 compareFiles( expectedPomFile, pomFile );
415 assertTrue( "Check artifact snapshotMetadata created", artifactMetadataFile.exists() );
417 File expectedMetadataFile = getTestFile( "src/test/expected-files/v3-snapshot-artifact-metadata.xml" );
419 compareFiles( expectedMetadataFile, artifactMetadataFile );
421 assertTrue( "Check snapshot snapshotMetadata created", snapshotMetadataFile.exists() );
423 expectedMetadataFile = getTestFile( "src/test/expected-files/v3-timestamped-snapshot-metadata.xml" );
425 compareFiles( expectedMetadataFile, snapshotMetadataFile );
428 public void testNoPomConvert()
429 throws IOException, RepositoryConversionException
431 // test that a POM is not created when there was none at the source
433 Artifact artifact = createArtifact( "test", "noPomArtifact", "1.0.0" );
434 repositoryConverter.convert( artifact, targetRepository, reporter );
435 assertEquals( "check no errors", 0, reporter.getFailures() );
436 assertEquals( "check no warnings", 1, reporter.getWarnings() );
437 assertEquals( "check success", 1, reporter.getSuccesses() );
438 assertEquals( "check warning message", getI18nString( "warning.missing.pom" ), getWarning().getReason() );
440 File artifactFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
441 assertTrue( "Check artifact created", artifactFile.exists() );
442 assertTrue( "Check artifact matches", FileUtils.contentEquals( artifactFile, artifact.getFile() ) );
444 artifact = createPomArtifact( artifact );
445 File pomFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
446 File sourcePomFile = new File( sourceRepository.getBasedir(), sourceRepository.pathOf( artifact ) );
448 assertFalse( "Check no POM created", pomFile.exists() );
449 assertFalse( "No source POM", sourcePomFile.exists() );
452 public void testIncorrectSourceChecksumMd5()
453 throws RepositoryConversionException
455 // test that it fails when the source md5 is wrong
457 Artifact artifact = createArtifact( "test", "incorrectMd5Artifact", "1.0.0" );
458 File file = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
461 repositoryConverter.convert( artifact, targetRepository, reporter );
463 assertEquals( "check failure message", getI18nString( "failure.incorrect.md5" ), getFailure().getReason() );
465 assertFalse( "Check artifact not created", file.exists() );
467 ArtifactRepositoryMetadata metadata = new ArtifactRepositoryMetadata( artifact );
469 new File( targetRepository.getBasedir(), targetRepository.pathOfRemoteRepositoryMetadata( metadata ) );
470 assertFalse( "Check metadata not created", metadataFile.exists() );
473 public void testIncorrectSourceChecksumSha1()
474 throws RepositoryConversionException
476 // test that it fails when the source sha1 is wrong
478 Artifact artifact = createArtifact( "test", "incorrectSha1Artifact", "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.sha1" ), 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 testUnmodifiedArtifact()
495 throws RepositoryConversionException, IOException, InterruptedException
497 // test the unmodified artifact is untouched
499 Artifact artifact = createArtifact( "test", "unmodified-artifact", "1.0.0" );
500 Artifact pomArtifact = createPomArtifact( artifact );
502 File sourceFile = new File( sourceRepository.getBasedir(), sourceRepository.pathOf( artifact ) );
503 File sourcePomFile = new File( sourceRepository.getBasedir(), sourceRepository.pathOf( pomArtifact ) );
504 File targetFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
505 File targetPomFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( pomArtifact ) );
507 assertTrue( "Check target file exists", targetFile.exists() );
508 assertTrue( "Check target POM exists", targetPomFile.exists() );
510 sourceFile.setLastModified( System.currentTimeMillis() );
511 sourcePomFile.setLastModified( System.currentTimeMillis() );
513 long origTime = targetFile.lastModified();
514 long origPomTime = targetPomFile.lastModified();
516 // Need to guarantee last modified is not equal
517 Thread.sleep( SLEEP_MILLIS );
519 repositoryConverter.convert( artifact, targetRepository, reporter );
522 compareFiles( sourceFile, targetFile );
523 compareFiles( sourcePomFile, targetPomFile );
525 assertEquals( "Check unmodified", origTime, targetFile.lastModified() );
526 assertEquals( "Check unmodified", origPomTime, targetPomFile.lastModified() );
529 public void testModifedArtifactFails()
530 throws InterruptedException, RepositoryConversionException, IOException
532 // test that it fails when the source artifact has changed and is different to the existing artifact in the
535 Artifact artifact = createArtifact( "test", "modified-artifact", "1.0.0" );
536 Artifact pomArtifact = createPomArtifact( artifact );
538 File sourceFile = new File( sourceRepository.getBasedir(), sourceRepository.pathOf( artifact ) );
539 File sourcePomFile = new File( sourceRepository.getBasedir(), sourceRepository.pathOf( pomArtifact ) );
540 File targetFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
541 File targetPomFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( pomArtifact ) );
543 assertTrue( "Check target file exists", targetFile.exists() );
544 assertTrue( "Check target POM exists", targetPomFile.exists() );
546 sourceFile.setLastModified( System.currentTimeMillis() );
547 sourcePomFile.setLastModified( System.currentTimeMillis() );
549 long origTime = targetFile.lastModified();
550 long origPomTime = targetPomFile.lastModified();
552 // Need to guarantee last modified is not equal
553 Thread.sleep( SLEEP_MILLIS );
555 repositoryConverter.convert( artifact, targetRepository, reporter );
557 assertEquals( "Check failure message", getI18nString( "failure.target.already.exists" ),
558 getFailure().getReason() );
560 assertEquals( "Check unmodified", origTime, targetFile.lastModified() );
561 assertEquals( "Check unmodified", origPomTime, targetPomFile.lastModified() );
563 ArtifactRepositoryMetadata metadata = new ArtifactRepositoryMetadata( artifact );
565 new File( targetRepository.getBasedir(), targetRepository.pathOfRemoteRepositoryMetadata( metadata ) );
566 assertFalse( "Check metadata not created", metadataFile.exists() );
569 public void testForcedUnmodifiedArtifact()
570 throws Exception, IOException
572 // test unmodified artifact is still converted when set to force
574 repositoryConverter = (RepositoryConverter) lookup( RepositoryConverter.ROLE, "force-repository-converter" );
576 Artifact artifact = createArtifact( "test", "unmodified-artifact", "1.0.0" );
577 Artifact pomArtifact = createPomArtifact( artifact );
579 File sourceFile = new File( sourceRepository.getBasedir(), sourceRepository.pathOf( artifact ) );
580 File sourcePomFile = new File( sourceRepository.getBasedir(), sourceRepository.pathOf( pomArtifact ) );
581 File targetFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
582 File targetPomFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( pomArtifact ) );
584 SimpleDateFormat dateFormat = new SimpleDateFormat( "yyyy-MM-dd", Locale.getDefault() );
585 long origTime = dateFormat.parse( "2006-03-03" ).getTime();
586 targetFile.setLastModified( origTime );
587 targetPomFile.setLastModified( origTime );
589 sourceFile.setLastModified( dateFormat.parse( "2006-01-01" ).getTime() );
590 sourcePomFile.setLastModified( dateFormat.parse( "2006-02-02" ).getTime() );
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", origTime == 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' ).replaceAll( "<\\?xml .+\\?>", "" );
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 ( !".svn".equals( file.getName() ) )
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() );