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 FileUtils.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 private void doTestV4SnapshotPomConvert( String version, String expectedMetadataFileName )
257 throws RepositoryConversionException, IOException
259 // test that it is copied as is
261 Artifact artifact = createArtifact( "test", "v4artifact", version );
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( expectedMetadataFileName );
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 testV4SnapshotPomConvert()
343 throws IOException, RepositoryConversionException
345 doTestV4SnapshotPomConvert( "1.0.0-SNAPSHOT", "src/test/expected-files/v4-snapshot-metadata.xml" );
350 public void testV4TimestampedSnapshotPomConvert()
351 throws IOException, RepositoryConversionException
353 doTestV4SnapshotPomConvert( "1.0.0-20060111.120115-1",
354 "src/test/expected-files/v4-timestamped-snapshot-metadata.xml" );
359 public void testV3TimestampedSnapshotPomConvert()
360 throws IOException, RepositoryConversionException
362 // test that the pom is coverted
364 Artifact artifact = createArtifact( "test", "v3artifact", "1.0.0-20060105.130101-3" );
365 ArtifactMetadata artifactMetadata = new ArtifactRepositoryMetadata( artifact );
366 File artifactMetadataFile = new File( targetRepository.getBasedir(),
367 targetRepository.pathOfRemoteRepositoryMetadata( artifactMetadata ) );
368 artifactMetadataFile.delete();
370 ArtifactMetadata snapshotMetadata = new SnapshotArtifactRepositoryMetadata( artifact );
371 File snapshotMetadataFile = new File( targetRepository.getBasedir(),
372 targetRepository.pathOfRemoteRepositoryMetadata( snapshotMetadata ) );
373 snapshotMetadataFile.delete();
375 repositoryConverter.convert( artifact, targetRepository, reporter );
378 File artifactFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
379 assertTrue( "Check artifact created", artifactFile.exists() );
380 assertTrue( "Check artifact matches", FileUtils.contentEquals( artifactFile, artifact.getFile() ) );
382 artifact = createPomArtifact( artifact );
383 File pomFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
384 File expectedPomFile = getTestFile( "src/test/expected-files/converted-v3-timestamped-snapshot.pom" );
385 assertTrue( "Check POM created", pomFile.exists() );
387 compareFiles( expectedPomFile, pomFile );
389 assertTrue( "Check artifact snapshotMetadata created", artifactMetadataFile.exists() );
391 File expectedMetadataFile = getTestFile( "src/test/expected-files/v3-snapshot-artifact-metadata.xml" );
393 compareFiles( expectedMetadataFile, artifactMetadataFile );
395 assertTrue( "Check snapshot snapshotMetadata created", snapshotMetadataFile.exists() );
397 expectedMetadataFile = getTestFile( "src/test/expected-files/v3-timestamped-snapshot-metadata.xml" );
399 compareFiles( expectedMetadataFile, snapshotMetadataFile );
402 public void testNoPomConvert()
403 throws IOException, RepositoryConversionException
405 // test that a POM is not created when there was none at the source
407 Artifact artifact = createArtifact( "test", "noPomArtifact", "1.0.0" );
408 repositoryConverter.convert( artifact, targetRepository, reporter );
409 assertEquals( "check no errors", 0, reporter.getFailures() );
410 assertEquals( "check no warnings", 1, reporter.getWarnings() );
411 assertEquals( "check success", 1, reporter.getSuccesses() );
412 assertEquals( "check warning message", getI18nString( "warning.missing.pom" ), getWarning().getReason() );
414 File artifactFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
415 assertTrue( "Check artifact created", artifactFile.exists() );
416 assertTrue( "Check artifact matches", FileUtils.contentEquals( artifactFile, artifact.getFile() ) );
418 artifact = createPomArtifact( artifact );
419 File pomFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
420 File sourcePomFile = new File( sourceRepository.getBasedir(), sourceRepository.pathOf( artifact ) );
422 assertFalse( "Check no POM created", pomFile.exists() );
423 assertFalse( "No source POM", sourcePomFile.exists() );
426 public void testIncorrectSourceChecksumMd5()
427 throws RepositoryConversionException
429 // test that it fails when the source md5 is wrong
431 Artifact artifact = createArtifact( "test", "incorrectMd5Artifact", "1.0.0" );
432 File file = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
435 repositoryConverter.convert( artifact, targetRepository, reporter );
437 assertEquals( "check failure message", getI18nString( "failure.incorrect.md5" ), getFailure().getReason() );
439 assertFalse( "Check artifact not created", file.exists() );
441 ArtifactRepositoryMetadata metadata = new ArtifactRepositoryMetadata( artifact );
443 new File( targetRepository.getBasedir(), targetRepository.pathOfRemoteRepositoryMetadata( metadata ) );
444 assertFalse( "Check metadata not created", metadataFile.exists() );
447 public void testIncorrectSourceChecksumSha1()
448 throws RepositoryConversionException
450 // test that it fails when the source sha1 is wrong
452 Artifact artifact = createArtifact( "test", "incorrectSha1Artifact", "1.0.0" );
453 File file = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
456 repositoryConverter.convert( artifact, targetRepository, reporter );
458 assertEquals( "check failure message", getI18nString( "failure.incorrect.sha1" ), getFailure().getReason() );
460 assertFalse( "Check artifact not created", file.exists() );
462 ArtifactRepositoryMetadata metadata = new ArtifactRepositoryMetadata( artifact );
464 new File( targetRepository.getBasedir(), targetRepository.pathOfRemoteRepositoryMetadata( metadata ) );
465 assertFalse( "Check metadata not created", metadataFile.exists() );
468 public void testUnmodifiedArtifact()
469 throws RepositoryConversionException, IOException, InterruptedException
471 // test the unmodified artifact is untouched
473 Artifact artifact = createArtifact( "test", "unmodified-artifact", "1.0.0" );
474 Artifact pomArtifact = createPomArtifact( artifact );
476 File sourceFile = new File( sourceRepository.getBasedir(), sourceRepository.pathOf( artifact ) );
477 File sourcePomFile = new File( sourceRepository.getBasedir(), sourceRepository.pathOf( pomArtifact ) );
478 File targetFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
479 File targetPomFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( pomArtifact ) );
481 assertTrue( "Check target file exists", targetFile.exists() );
482 assertTrue( "Check target POM exists", targetPomFile.exists() );
484 sourceFile.setLastModified( System.currentTimeMillis() );
485 sourcePomFile.setLastModified( System.currentTimeMillis() );
487 long origTime = targetFile.lastModified();
488 long origPomTime = targetPomFile.lastModified();
490 // Need to guarantee last modified is not equal
491 Thread.sleep( SLEEP_MILLIS );
493 repositoryConverter.convert( artifact, targetRepository, reporter );
496 compareFiles( sourceFile, targetFile );
497 compareFiles( sourcePomFile, targetPomFile );
499 assertEquals( "Check unmodified", origTime, targetFile.lastModified() );
500 assertEquals( "Check unmodified", origPomTime, targetPomFile.lastModified() );
503 public void testModifedArtifactFails()
504 throws InterruptedException, RepositoryConversionException, IOException
506 // test that it fails when the source artifact has changed and is different to the existing artifact in the
509 Artifact artifact = createArtifact( "test", "modified-artifact", "1.0.0" );
510 Artifact pomArtifact = createPomArtifact( artifact );
512 File sourceFile = new File( sourceRepository.getBasedir(), sourceRepository.pathOf( artifact ) );
513 File sourcePomFile = new File( sourceRepository.getBasedir(), sourceRepository.pathOf( pomArtifact ) );
514 File targetFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
515 File targetPomFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( pomArtifact ) );
517 assertTrue( "Check target file exists", targetFile.exists() );
518 assertTrue( "Check target POM exists", targetPomFile.exists() );
520 sourceFile.setLastModified( System.currentTimeMillis() );
521 sourcePomFile.setLastModified( System.currentTimeMillis() );
523 long origTime = targetFile.lastModified();
524 long origPomTime = targetPomFile.lastModified();
526 // Need to guarantee last modified is not equal
527 Thread.sleep( SLEEP_MILLIS );
529 repositoryConverter.convert( artifact, targetRepository, reporter );
531 assertEquals( "Check failure message", getI18nString( "failure.target.already.exists" ),
532 getFailure().getReason() );
534 assertEquals( "Check unmodified", origTime, targetFile.lastModified() );
535 assertEquals( "Check unmodified", origPomTime, targetPomFile.lastModified() );
537 ArtifactRepositoryMetadata metadata = new ArtifactRepositoryMetadata( artifact );
539 new File( targetRepository.getBasedir(), targetRepository.pathOfRemoteRepositoryMetadata( metadata ) );
540 assertFalse( "Check metadata not created", metadataFile.exists() );
543 public void testForcedUnmodifiedArtifact()
544 throws Exception, IOException
546 // test unmodified artifact is still converted when set to force
548 repositoryConverter = (RepositoryConverter) lookup( RepositoryConverter.ROLE, "force-repository-converter" );
550 Artifact artifact = createArtifact( "test", "unmodified-artifact", "1.0.0" );
551 Artifact pomArtifact = createPomArtifact( artifact );
553 File sourceFile = new File( sourceRepository.getBasedir(), sourceRepository.pathOf( artifact ) );
554 File sourcePomFile = new File( sourceRepository.getBasedir(), sourceRepository.pathOf( pomArtifact ) );
555 File targetFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
556 File targetPomFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( pomArtifact ) );
558 SimpleDateFormat dateFormat = new SimpleDateFormat( "yyyy-MM-dd", Locale.getDefault() );
559 long origTime = dateFormat.parse( "2006-03-03" ).getTime();
560 targetFile.setLastModified( origTime );
561 targetPomFile.setLastModified( origTime );
563 sourceFile.setLastModified( dateFormat.parse( "2006-01-01" ).getTime() );
564 sourcePomFile.setLastModified( dateFormat.parse( "2006-02-02" ).getTime() );
566 repositoryConverter.convert( artifact, targetRepository, reporter );
569 compareFiles( sourceFile, targetFile );
570 compareFiles( sourcePomFile, targetPomFile );
572 assertFalse( "Check modified", origTime == targetFile.lastModified() );
573 assertFalse( "Check modified", origTime == targetPomFile.lastModified() );
575 ArtifactRepositoryMetadata metadata = new ArtifactRepositoryMetadata( artifact );
577 new File( targetRepository.getBasedir(), targetRepository.pathOfRemoteRepositoryMetadata( metadata ) );
578 assertTrue( "Check metadata created", metadataFile.exists() );
581 public void testDryRunSuccess()
584 // test dry run does nothing on a run that will be successful, and returns success
586 repositoryConverter = (RepositoryConverter) lookup( RepositoryConverter.ROLE, "dryrun-repository-converter" );
588 Artifact artifact = createArtifact( "test", "dryrun-artifact", "1.0.0" );
589 Artifact pomArtifact = createPomArtifact( artifact );
591 File sourceFile = new File( sourceRepository.getBasedir(), sourceRepository.pathOf( artifact ) );
592 File sourcePomFile = new File( sourceRepository.getBasedir(), sourceRepository.pathOf( pomArtifact ) );
593 File targetFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
594 File targetPomFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( pomArtifact ) );
596 repositoryConverter.convert( artifact, targetRepository, reporter );
599 assertTrue( "Check source file exists", sourceFile.exists() );
600 assertTrue( "Check source POM exists", sourcePomFile.exists() );
602 assertFalse( "Check target file doesn't exist", targetFile.exists() );
603 assertFalse( "Check target POM doesn't exist", targetPomFile.exists() );
605 ArtifactRepositoryMetadata metadata = new ArtifactRepositoryMetadata( artifact );
607 new File( targetRepository.getBasedir(), targetRepository.pathOfRemoteRepositoryMetadata( metadata ) );
608 assertFalse( "Check metadata not created", metadataFile.exists() );
611 public void testDryRunFailure()
614 // test dry run does nothing on a run that will fail, and returns failure
616 repositoryConverter = (RepositoryConverter) lookup( RepositoryConverter.ROLE, "dryrun-repository-converter" );
618 Artifact artifact = createArtifact( "test", "modified-artifact", "1.0.0" );
619 Artifact pomArtifact = createPomArtifact( artifact );
621 File sourceFile = new File( sourceRepository.getBasedir(), sourceRepository.pathOf( artifact ) );
622 File sourcePomFile = new File( sourceRepository.getBasedir(), sourceRepository.pathOf( pomArtifact ) );
623 File targetFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
624 File targetPomFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( pomArtifact ) );
626 assertTrue( "Check target file exists", targetFile.exists() );
627 assertTrue( "Check target POM exists", targetPomFile.exists() );
629 sourceFile.setLastModified( System.currentTimeMillis() );
630 sourcePomFile.setLastModified( System.currentTimeMillis() );
632 long origTime = targetFile.lastModified();
633 long origPomTime = targetPomFile.lastModified();
635 // Need to guarantee last modified is not equal
636 Thread.sleep( SLEEP_MILLIS );
638 repositoryConverter.convert( artifact, targetRepository, reporter );
640 assertEquals( "Check failure message", getI18nString( "failure.target.already.exists" ),
641 getFailure().getReason() );
643 assertEquals( "Check unmodified", origTime, targetFile.lastModified() );
644 assertEquals( "Check unmodified", origPomTime, targetPomFile.lastModified() );
646 ArtifactRepositoryMetadata metadata = new ArtifactRepositoryMetadata( artifact );
648 new File( targetRepository.getBasedir(), targetRepository.pathOfRemoteRepositoryMetadata( metadata ) );
649 assertFalse( "Check metadata not created", metadataFile.exists() );
652 public void testRollbackArtifactCreated()
653 throws RepositoryConversionException, IOException
655 // test rollback can remove a created artifact, including checksums
657 Artifact artifact = createArtifact( "test", "rollback-created-artifact", "1.0.0" );
658 ArtifactMetadata artifactMetadata = new ArtifactRepositoryMetadata( artifact );
659 File artifactMetadataFile = new File( targetRepository.getBasedir(),
660 targetRepository.pathOfRemoteRepositoryMetadata( artifactMetadata ) );
661 FileUtils.deleteDirectory( artifactMetadataFile.getParentFile() );
663 ArtifactMetadata versionMetadata = new SnapshotArtifactRepositoryMetadata( artifact );
664 File versionMetadataFile = new File( targetRepository.getBasedir(),
665 targetRepository.pathOfRemoteRepositoryMetadata( versionMetadata ) );
667 File artifactFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
669 repositoryConverter.convert( artifact, targetRepository, reporter );
671 String pattern = "^" + getI18nString( "failure.invalid.source.pom" ).replaceFirst( "\\{0\\}", ".*" ) + "$";
672 assertTrue( "Check failure message", getFailure().getReason().matches( pattern ) );
674 assertFalse( "check artifact rolled back", artifactFile.exists() );
675 assertFalse( "check metadata rolled back", artifactMetadataFile.exists() );
676 assertFalse( "check metadata rolled back", versionMetadataFile.exists() );
679 public void testMultipleArtifacts()
680 throws RepositoryConversionException, IOException
682 // test multiple artifacts are converted
684 List artifacts = new ArrayList();
685 artifacts.add( createArtifact( "test", "artifact-one", "1.0.0" ) );
686 artifacts.add( createArtifact( "test", "artifact-two", "1.0.0" ) );
687 artifacts.add( createArtifact( "test", "artifact-three", "1.0.0" ) );
688 repositoryConverter.convert( artifacts, targetRepository, reporter );
689 assertEquals( "check no errors", 0, reporter.getFailures() );
690 assertEquals( "check no warnings", 0, reporter.getWarnings() );
691 assertEquals( "check successes", 3, reporter.getSuccesses() );
693 for ( Iterator i = artifacts.iterator(); i.hasNext(); )
695 Artifact artifact = (Artifact) i.next();
697 File artifactFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
698 assertTrue( "Check artifact created", artifactFile.exists() );
699 assertTrue( "Check artifact matches", FileUtils.contentEquals( artifactFile, artifact.getFile() ) );
701 artifact = createPomArtifact( artifact );
702 File pomFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
703 File expectedPomFile =
704 getTestFile( "src/test/expected-files/converted-" + artifact.getArtifactId() + ".pom" );
705 assertTrue( "Check POM created", pomFile.exists() );
707 compareFiles( expectedPomFile, pomFile );
711 public void testInvalidSourceArtifactMetadata()
714 // test artifact is not converted when source metadata is invalid, and returns failure
716 createModernSourceRepository();
718 Artifact artifact = createArtifact( "test", "incorrectArtifactMetadata", "1.0.0" );
719 File file = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
722 repositoryConverter.convert( artifact, targetRepository, reporter );
724 assertEquals( "check failure message", getI18nString( "failure.incorrect.artifactMetadata.versions" ),
725 getFailure().getReason() );
727 assertFalse( "Check artifact not created", file.exists() );
729 ArtifactRepositoryMetadata metadata = new ArtifactRepositoryMetadata( artifact );
731 new File( targetRepository.getBasedir(), targetRepository.pathOfRemoteRepositoryMetadata( metadata ) );
732 assertFalse( "Check metadata not created", metadataFile.exists() );
735 public void testInvalidSourceSnapshotMetadata()
736 throws Exception, MalformedURLException
738 // test artifact is not converted when source snapshot metadata is invalid and returns failure
740 createModernSourceRepository();
742 Artifact artifact = createArtifact( "test", "incorrectSnapshotMetadata", "1.0.0-20060102.030405-6" );
743 File file = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
746 repositoryConverter.convert( artifact, targetRepository, reporter );
748 assertEquals( "check failure message", getI18nString( "failure.incorrect.snapshotMetadata.snapshot" ),
749 getFailure().getReason() );
751 assertFalse( "Check artifact not created", file.exists() );
753 ArtifactRepositoryMetadata metadata = new ArtifactRepositoryMetadata( artifact );
755 new File( targetRepository.getBasedir(), targetRepository.pathOfRemoteRepositoryMetadata( metadata ) );
756 assertFalse( "Check metadata not created", metadataFile.exists() );
759 public void testMergeArtifactMetadata()
760 throws RepositoryConversionException, IOException
762 // test artifact level metadata is merged when it already exists on successful conversion
764 Artifact artifact = createArtifact( "test", "newversion-artifact", "1.0.1" );
766 repositoryConverter.convert( artifact, targetRepository, reporter );
769 File artifactFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
770 assertTrue( "Check artifact created", artifactFile.exists() );
771 assertTrue( "Check artifact matches", FileUtils.contentEquals( artifactFile, artifact.getFile() ) );
773 artifact = createPomArtifact( artifact );
774 File pomFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
775 File sourcePomFile = new File( sourceRepository.getBasedir(), sourceRepository.pathOf( artifact ) );
776 assertTrue( "Check POM created", pomFile.exists() );
778 compareFiles( sourcePomFile, pomFile );
780 ArtifactMetadata artifactMetadata = new ArtifactRepositoryMetadata( artifact );
781 File artifactMetadataFile = new File( targetRepository.getBasedir(),
782 targetRepository.pathOfRemoteRepositoryMetadata( artifactMetadata ) );
783 assertTrue( "Check artifact metadata created", artifactMetadataFile.exists() );
785 File expectedMetadataFile = getTestFile( "src/test/expected-files/newversion-artifact-metadata.xml" );
787 compareFiles( expectedMetadataFile, artifactMetadataFile );
790 public void testSourceAndTargetRepositoriesMatch()
793 // test that it fails if the same
795 ArtifactRepositoryFactory factory = (ArtifactRepositoryFactory) lookup( ArtifactRepositoryFactory.ROLE );
797 sourceRepository = factory.createArtifactRepository( "source", targetRepository.getUrl(),
798 targetRepository.getLayout(), null, null );
800 Artifact artifact = createArtifact( "test", "repository-artifact", "1.0" );
804 repositoryConverter.convert( artifact, targetRepository, reporter );
805 fail( "Should have failed trying to convert within the same repository" );
807 catch ( RepositoryConversionException e )
810 assertEquals( "check message", getI18nString( "exception.repositories.match" ), e.getMessage() );
811 assertNull( "Check no additional cause", e.getCause() );
815 private Artifact createArtifact( String groupId, String artifactId, String version )
817 Matcher matcher = Artifact.VERSION_FILE_PATTERN.matcher( version );
819 if ( matcher.matches() )
821 baseVersion = matcher.group( 1 ) + "-SNAPSHOT";
825 baseVersion = version;
827 return createArtifact( groupId, artifactId, baseVersion, version, "jar" );
830 private Artifact createArtifact( String groupId, String artifactId, String baseVersion, String version,
833 Artifact artifact = artifactFactory.createArtifact( groupId, artifactId, version, null, type );
834 artifact.setBaseVersion( baseVersion );
835 artifact.setRepository( sourceRepository );
836 artifact.setFile( new File( sourceRepository.getBasedir(), sourceRepository.pathOf( artifact ) ) );
840 private Artifact createPomArtifact( Artifact artifact )
842 return createArtifact( artifact.getGroupId(), artifact.getArtifactId(), artifact.getBaseVersion(),
843 artifact.getVersion(), "pom" );
846 private static void compareFiles( File expectedPomFile, File pomFile )
849 String expectedContent = normalizeString( FileUtils.fileRead( expectedPomFile ) );
850 String targetContent = normalizeString( FileUtils.fileRead( pomFile ) );
851 assertEquals( "Check file match between " + expectedPomFile + " and " + pomFile, expectedContent,
855 private static String normalizeString( String path )
857 return path.trim().replaceAll( "\r\n", "\n" ).replace( '\r', '\n' ).replaceAll( "<\\?xml .+\\?>", "" );
860 private void checkSuccess()
862 assertEquals( "check no errors", 0, reporter.getFailures() );
863 assertEquals( "check no warnings", 0, reporter.getWarnings() );
864 assertEquals( "check success", 1, reporter.getSuccesses() );
867 private void checkFailure()
869 assertEquals( "check num errors", 1, reporter.getFailures() );
870 assertEquals( "check no warnings", 0, reporter.getWarnings() );
871 assertEquals( "check no success", 0, reporter.getSuccesses() );
874 private String getI18nString( String key )
876 return i18n.getString( repositoryConverter.getClass().getName(), Locale.getDefault(), key );
879 private ArtifactResult getFailure()
881 return (ArtifactResult) reporter.getArtifactFailureIterator().next();
884 private ArtifactResult getWarning()
886 return (ArtifactResult) reporter.getArtifactWarningIterator().next();
889 private void createModernSourceRepository()
892 ArtifactRepositoryFactory factory = (ArtifactRepositoryFactory) lookup( ArtifactRepositoryFactory.ROLE );
894 ArtifactRepositoryLayout layout = (ArtifactRepositoryLayout) lookup( ArtifactRepositoryLayout.ROLE, "default" );
896 File sourceBase = getTestFile( "src/test/source-modern-repository" );
898 factory.createArtifactRepository( "source", sourceBase.toURL().toString(), layout, null, null );