You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

LegacyToDefaultConverterTest.java 48KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050
  1. package org.apache.archiva.converter.artifact;
  2. /*
  3. * Licensed to the Apache Software Foundation (ASF) under one
  4. * or more contributor license agreements. See the NOTICE file
  5. * distributed with this work for additional information
  6. * regarding copyright ownership. The ASF licenses this file
  7. * to you under the Apache License, Version 2.0 (the
  8. * "License"); you may not use this file except in compliance
  9. * with the License. You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing,
  14. * software distributed under the License is distributed on an
  15. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  16. * KIND, either express or implied. See the License for the
  17. * specific language governing permissions and limitations
  18. * under the License.
  19. */
  20. import junit.framework.TestCase;
  21. import org.apache.archiva.common.plexusbridge.PlexusSisuBridge;
  22. import org.apache.archiva.test.utils.ArchivaSpringJUnit4ClassRunner;
  23. import org.apache.commons.io.FileUtils;
  24. import org.apache.maven.artifact.Artifact;
  25. import org.apache.maven.artifact.factory.ArtifactFactory;
  26. import org.apache.maven.artifact.metadata.ArtifactMetadata;
  27. import org.apache.maven.artifact.repository.ArtifactRepository;
  28. import org.apache.maven.artifact.repository.MavenArtifactRepository;
  29. import org.apache.maven.artifact.repository.layout.ArtifactRepositoryLayout;
  30. import org.apache.maven.artifact.repository.metadata.ArtifactRepositoryMetadata;
  31. import org.junit.Before;
  32. import org.junit.Test;
  33. import org.junit.runner.RunWith;
  34. import org.springframework.context.ApplicationContext;
  35. import org.springframework.test.context.ContextConfiguration;
  36. import javax.inject.Inject;
  37. import java.io.IOException;
  38. import java.nio.charset.Charset;
  39. import java.nio.file.Files;
  40. import java.nio.file.Path;
  41. import java.nio.file.Paths;
  42. import java.nio.file.attribute.FileTime;
  43. import java.text.SimpleDateFormat;
  44. import java.util.ArrayList;
  45. import java.util.List;
  46. import java.util.Locale;
  47. import java.util.Map;
  48. import java.util.concurrent.TimeUnit;
  49. import java.util.regex.Matcher;
  50. /**
  51. * LegacyToDefaultConverterTest
  52. */
  53. @RunWith (ArchivaSpringJUnit4ClassRunner.class)
  54. @ContextConfiguration (locations = { "classpath*:/META-INF/spring-context.xml", "classpath:/spring-context.xml" })
  55. public class LegacyToDefaultConverterTest
  56. extends TestCase
  57. {
  58. private ArtifactRepository sourceRepository;
  59. private ArtifactRepository targetRepository;
  60. private ArtifactConverter artifactConverter;
  61. private ArtifactFactory artifactFactory;
  62. @Inject
  63. private PlexusSisuBridge plexusSisuBridge;
  64. @Inject
  65. private ApplicationContext applicationContext;
  66. private static final int SLEEP_MILLIS = 100;
  67. @Before
  68. public void init()
  69. throws Exception
  70. {
  71. super.setUp();
  72. // ArtifactRepositoryFactory factory = plexusSisuBridge.lookup( ArtifactRepositoryFactory.class );
  73. ArtifactRepositoryLayout layout = new LegacyRepositoryLayout();
  74. Path sourceBase = getTestFile( "src/test/source-repository" );
  75. sourceRepository =
  76. new MavenArtifactRepository( "source", sourceBase.toUri().toURL().toString(), layout, null, null );
  77. layout = plexusSisuBridge.lookup( ArtifactRepositoryLayout.class, "default" );
  78. Path targetBase = getTestFile( "target/test-target-repository" );
  79. copyDirectoryStructure( getTestFile( "src/test/target-repository" ), targetBase );
  80. targetRepository =
  81. new MavenArtifactRepository( "target", targetBase.toUri().toURL().toString(), layout, null, null );
  82. artifactConverter =
  83. applicationContext.getBean( "artifactConverter#legacy-to-default", ArtifactConverter.class );
  84. artifactConverter.clearWarnings();
  85. artifactFactory = (ArtifactFactory) plexusSisuBridge.lookup( ArtifactFactory.class );
  86. }
  87. public static Path getTestFile( String path )
  88. {
  89. return Paths.get( org.apache.archiva.common.utils.FileUtils.getBasedir(), path );
  90. }
  91. private void copyDirectoryStructure( Path sourceDirectory, Path destinationDirectory )
  92. throws IOException
  93. {
  94. if ( !Files.exists(sourceDirectory) )
  95. {
  96. throw new IOException( "Source directory doesn't exists (" + sourceDirectory.toAbsolutePath()+ ")." );
  97. }
  98. Path[] files = Files.list( sourceDirectory ).toArray( Path[]::new );
  99. String sourcePath = sourceDirectory.toAbsolutePath().toString();
  100. for ( int i = 0; i < files.length; i++ )
  101. {
  102. Path file = files[i];
  103. String dest = file.toAbsolutePath().toString();
  104. dest = dest.substring( sourcePath.length() + 1 );
  105. Path destination = destinationDirectory.resolve( dest );
  106. if ( Files.isRegularFile( file ) )
  107. {
  108. destination = destination.getParent();
  109. FileUtils.copyFileToDirectory( file.toFile(), destination.toFile() );
  110. }
  111. else if ( Files.isDirectory( file ) )
  112. {
  113. if ( !".svn".equals( file.getFileName().toString() ) )
  114. {
  115. if ( !Files.exists(destination))
  116. {
  117. Files.createDirectories( destination );
  118. }
  119. copyDirectoryStructure( file, destination );
  120. }
  121. }
  122. else
  123. {
  124. throw new IOException( "Unknown file type: " + file.toAbsolutePath() );
  125. }
  126. }
  127. }
  128. @Test
  129. public void testV4PomConvert()
  130. throws Exception
  131. {
  132. // test that it is copied as is
  133. Artifact artifact = createArtifact( "test", "v4artifact", "1.0.0" );
  134. ArtifactMetadata artifactMetadata = new ArtifactRepositoryMetadata( artifact );
  135. Path artifactMetadataFile = Paths.get( targetRepository.getBasedir(),
  136. targetRepository.pathOfRemoteRepositoryMetadata( artifactMetadata ) );
  137. Files.deleteIfExists( artifactMetadataFile);
  138. ArtifactMetadata versionMetadata = new SnapshotArtifactRepositoryMetadata( artifact );
  139. Path versionMetadataFile = Paths.get( targetRepository.getBasedir(),
  140. targetRepository.pathOfRemoteRepositoryMetadata( versionMetadata ) );
  141. Files.deleteIfExists(versionMetadataFile);
  142. Path artifactFile = Paths.get( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
  143. Files.deleteIfExists(artifactFile);
  144. artifactConverter.convert( artifact, targetRepository );
  145. checkSuccess( artifactConverter );
  146. assertTrue( "Check artifact created", Files.exists(artifactFile) );
  147. assertTrue( "Check artifact matches", FileUtils.contentEquals( artifactFile.toFile(), artifact.getFile() ) );
  148. artifact = createPomArtifact( artifact );
  149. Path pomFile = Paths.get( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
  150. Path sourcePomFile = Paths.get( sourceRepository.getBasedir(), sourceRepository.pathOf( artifact ) );
  151. assertTrue( "Check POM created", Files.exists(pomFile) );
  152. compareFiles( sourcePomFile, pomFile );
  153. assertTrue( "Check artifact metadata created", Files.exists(artifactMetadataFile) );
  154. Path expectedMetadataFile = getTestFile( "src/test/expected-files/v4-artifact-metadata.xml" );
  155. compareFiles( expectedMetadataFile, artifactMetadataFile );
  156. assertTrue( "Check snapshot metadata created", Files.exists(versionMetadataFile) );
  157. expectedMetadataFile = getTestFile( "src/test/expected-files/v4-version-metadata.xml" );
  158. compareFiles( expectedMetadataFile, versionMetadataFile );
  159. }
  160. @Test
  161. public void testV3PomConvert()
  162. throws Exception
  163. {
  164. // test that the pom is coverted
  165. Artifact artifact = createArtifact( "test", "v3artifact", "1.0.0" );
  166. ArtifactMetadata artifactMetadata = new ArtifactRepositoryMetadata( artifact );
  167. Path artifactMetadataFile = Paths.get( targetRepository.getBasedir(),
  168. targetRepository.pathOfRemoteRepositoryMetadata( artifactMetadata ) );
  169. Files.deleteIfExists(artifactMetadataFile);
  170. ArtifactMetadata versionMetadata = new SnapshotArtifactRepositoryMetadata( artifact );
  171. Path versionMetadataFile = Paths.get( targetRepository.getBasedir(),
  172. targetRepository.pathOfRemoteRepositoryMetadata( versionMetadata ) );
  173. Files.deleteIfExists(versionMetadataFile);
  174. artifactConverter.convert( artifact, targetRepository );
  175. checkSuccess( artifactConverter );
  176. Path artifactFile = Paths.get( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
  177. assertTrue( "Check artifact created", Files.exists(artifactFile) );
  178. assertTrue( "Check artifact matches", FileUtils.contentEquals( artifactFile.toFile(), artifact.getFile() ) );
  179. artifact = createPomArtifact( artifact );
  180. Path pomFile = Paths.get( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
  181. Path expectedPomFile = getTestFile( "src/test/expected-files/converted-v3.pom" );
  182. assertTrue( "Check POM created", Files.exists(pomFile) );
  183. compareFiles( expectedPomFile, pomFile );
  184. assertTrue( "Check artifact metadata created", Files.exists(artifactMetadataFile) );
  185. Path expectedMetadataFile = getTestFile( "src/test/expected-files/v3-artifact-metadata.xml" );
  186. compareFiles( expectedMetadataFile, artifactMetadataFile );
  187. assertTrue( "Check snapshot metadata created", Files.exists(versionMetadataFile) );
  188. expectedMetadataFile = getTestFile( "src/test/expected-files/v3-version-metadata.xml" );
  189. compareFiles( expectedMetadataFile, versionMetadataFile );
  190. }
  191. @Test
  192. public void testV3PomConvertWithRelocation()
  193. throws Exception
  194. {
  195. Artifact artifact = createArtifact( "test", "relocated-v3artifact", "1.0.0" );
  196. ArtifactMetadata artifactMetadata = new ArtifactRepositoryMetadata( artifact );
  197. Path artifactMetadataFile = Paths.get( targetRepository.getBasedir(),
  198. targetRepository.pathOfRemoteRepositoryMetadata( artifactMetadata ) );
  199. Files.deleteIfExists(artifactMetadataFile);
  200. ArtifactMetadata versionMetadata = new SnapshotArtifactRepositoryMetadata( artifact );
  201. Path versionMetadataFile = Paths.get( targetRepository.getBasedir(),
  202. targetRepository.pathOfRemoteRepositoryMetadata( versionMetadata ) );
  203. Files.deleteIfExists(versionMetadataFile);
  204. artifactConverter.convert( artifact, targetRepository );
  205. //checkSuccess(); --> commented until MNG-2100 is fixed
  206. Path artifactFile = Paths.get( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
  207. assertTrue( "Check if relocated artifact created", Files.exists(artifactFile) );
  208. assertTrue( "Check if relocated artifact matches",
  209. FileUtils.contentEquals( artifactFile.toFile(), artifact.getFile() ) );
  210. Artifact pomArtifact = createArtifact( "relocated-test", "relocated-v3artifact", "1.0.0", "1.0.0", "pom" );
  211. Path pomFile = getTestFile( "src/test/expected-files/" + targetRepository.pathOf( pomArtifact ) );
  212. Path testFile = getTestFile( "target/test-target-repository/" + targetRepository.pathOf( pomArtifact ) );
  213. compareFiles( pomFile, testFile );
  214. Artifact orig = createArtifact( "test", "relocated-v3artifact", "1.0.0", "1.0.0", "pom" );
  215. artifactFile = Paths.get( targetRepository.getBasedir(), targetRepository.pathOf( orig ) );
  216. assertTrue( "Check if relocation artifact pom is created", Files.exists(artifactFile) );
  217. testFile = getTestFile( "src/test/expected-files/" + targetRepository.pathOf( orig ) );
  218. compareFiles( artifactFile, testFile );
  219. }
  220. @Test
  221. public void testV3PomWarningsOnConvert()
  222. throws Exception
  223. {
  224. // 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. Path artifactMetadataFile = Paths.get( targetRepository.getBasedir(),
  228. targetRepository.pathOfRemoteRepositoryMetadata( artifactMetadata ) );
  229. Files.deleteIfExists(artifactMetadataFile);
  230. ArtifactMetadata versionMetadata = new SnapshotArtifactRepositoryMetadata( artifact );
  231. Path versionMetadataFile = Paths.get( targetRepository.getBasedir(),
  232. targetRepository.pathOfRemoteRepositoryMetadata( versionMetadata ) );
  233. Files.deleteIfExists(versionMetadataFile);
  234. artifactConverter.convert( artifact, targetRepository );
  235. checkWarnings( artifactConverter, 2 );
  236. Path artifactFile = Paths.get( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
  237. assertTrue( "Check artifact created", Files.exists(artifactFile) );
  238. assertTrue( "Check artifact matches", FileUtils.contentEquals( artifactFile.toFile(), artifact.getFile() ) );
  239. artifact = createPomArtifact( artifact );
  240. Path pomFile = Paths.get( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
  241. Path expectedPomFile = getTestFile( "src/test/expected-files/converted-v3-warnings.pom" );
  242. assertTrue( "Check POM created", Files.exists(pomFile) );
  243. compareFiles( expectedPomFile, pomFile );
  244. // TODO: check 2 warnings (extend and versions) matched on i18n key
  245. }
  246. private void doTestV4SnapshotPomConvert( String version, String expectedMetadataFileName )
  247. throws Exception
  248. {
  249. // test that it is copied as is
  250. Artifact artifact = createArtifact( "test", "v4artifact", version );
  251. ArtifactMetadata artifactMetadata = new ArtifactRepositoryMetadata( artifact );
  252. Path artifactMetadataFile = Paths.get( targetRepository.getBasedir(),
  253. targetRepository.pathOfRemoteRepositoryMetadata( artifactMetadata ) );
  254. Files.deleteIfExists(artifactMetadataFile);
  255. ArtifactMetadata snapshotMetadata = new SnapshotArtifactRepositoryMetadata( artifact );
  256. Path snapshotMetadataFile = Paths.get( targetRepository.getBasedir(),
  257. targetRepository.pathOfRemoteRepositoryMetadata( snapshotMetadata ) );
  258. Files.deleteIfExists(snapshotMetadataFile);
  259. artifactConverter.convert( artifact, targetRepository );
  260. checkSuccess( artifactConverter );
  261. Path artifactFile = Paths.get( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
  262. assertTrue( "Check artifact created", Files.exists(artifactFile) );
  263. assertTrue( "Check artifact matches", FileUtils.contentEquals( artifactFile.toFile(), artifact.getFile() ) );
  264. artifact = createPomArtifact( artifact );
  265. Path pomFile = Paths.get( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
  266. Path sourcePomFile = Paths.get( sourceRepository.getBasedir(), sourceRepository.pathOf( artifact ) );
  267. assertTrue( "Check POM created", Files.exists(pomFile) );
  268. compareFiles( sourcePomFile, pomFile );
  269. assertTrue( "Check artifact metadata created", Files.exists(artifactMetadataFile) );
  270. Path expectedMetadataFile = getTestFile( "src/test/expected-files/v4-snapshot-artifact-metadata.xml" );
  271. compareFiles( expectedMetadataFile, artifactMetadataFile );
  272. assertTrue( "Check snapshot metadata created", Files.exists(snapshotMetadataFile) );
  273. expectedMetadataFile = getTestFile( expectedMetadataFileName );
  274. compareFiles( expectedMetadataFile, snapshotMetadataFile );
  275. }
  276. @Test
  277. public void testV3SnapshotPomConvert()
  278. throws Exception
  279. {
  280. // test that the pom is coverted
  281. Artifact artifact = createArtifact( "test", "v3artifact", "1.0.0-SNAPSHOT" );
  282. ArtifactMetadata artifactMetadata = new ArtifactRepositoryMetadata( artifact );
  283. Path artifactMetadataFile = Paths.get( targetRepository.getBasedir(),
  284. targetRepository.pathOfRemoteRepositoryMetadata( artifactMetadata ) );
  285. Files.deleteIfExists(artifactMetadataFile);
  286. ArtifactMetadata snapshotMetadata = new SnapshotArtifactRepositoryMetadata( artifact );
  287. Path snapshotMetadataFile = Paths.get( targetRepository.getBasedir(),
  288. targetRepository.pathOfRemoteRepositoryMetadata( snapshotMetadata ) );
  289. Files.deleteIfExists(snapshotMetadataFile);
  290. artifactConverter.convert( artifact, targetRepository );
  291. checkSuccess( artifactConverter );
  292. Path artifactFile = Paths.get( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
  293. assertTrue( "Check artifact created", Files.exists(artifactFile) );
  294. assertTrue( "Check artifact matches", FileUtils.contentEquals( artifactFile.toFile(), artifact.getFile() ) );
  295. artifact = createPomArtifact( artifact );
  296. Path pomFile = Paths.get( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
  297. Path expectedPomFile = getTestFile( "src/test/expected-files/converted-v3-snapshot.pom" );
  298. assertTrue( "Check POM created", Files.exists(pomFile) );
  299. compareFiles( expectedPomFile, pomFile );
  300. assertTrue( "Check artifact metadata created", Files.exists(artifactMetadataFile) );
  301. Path expectedMetadataFile = getTestFile( "src/test/expected-files/v3-snapshot-artifact-metadata.xml" );
  302. compareFiles( expectedMetadataFile, artifactMetadataFile );
  303. assertTrue( "Check snapshot metadata created", Files.exists(snapshotMetadataFile) );
  304. expectedMetadataFile = getTestFile( "src/test/expected-files/v3-snapshot-metadata.xml" );
  305. compareFiles( expectedMetadataFile, snapshotMetadataFile );
  306. }
  307. @Test
  308. public void testV4SnapshotPomConvert()
  309. throws Exception
  310. {
  311. doTestV4SnapshotPomConvert( "1.0.0-SNAPSHOT", "src/test/expected-files/v4-snapshot-metadata.xml" );
  312. assertTrue( true );
  313. }
  314. @Test
  315. public void testV4TimestampedSnapshotPomConvert()
  316. throws Exception
  317. {
  318. doTestV4SnapshotPomConvert( "1.0.0-20060111.120115-1",
  319. "src/test/expected-files/v4-timestamped-snapshot-metadata.xml" );
  320. assertTrue( true );
  321. }
  322. @Test
  323. public void testMavenOnePluginConversion()
  324. throws Exception
  325. {
  326. Artifact artifact =
  327. createArtifact( "org.apache.maven.plugins", "maven-foo-plugin", "1.0", "1.0", "maven-plugin" );
  328. artifact.setFile(
  329. Paths.get( org.apache.archiva.common.utils.FileUtils.getBasedir(), "src/test/source-repository/test/plugins/maven-foo-plugin-1.0.jar" ).toFile() );
  330. artifactConverter.convert( artifact, targetRepository );
  331. // There is a warning but I can't figure out how to look at it. Eyeballing the results it appears
  332. // the plugin is being coverted correctly.
  333. //checkSuccess();
  334. Path artifactFile = Paths.get( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
  335. assertTrue( "Check artifact created", Files.exists(artifactFile) );
  336. assertTrue( "Check artifact matches", FileUtils.contentEquals( artifactFile.toFile(), artifact.getFile() ) );
  337. /*
  338. The POM isn't needed for Maven 1.x plugins but the raw conversion for
  339. artifact = createPomArtifact( artifact );
  340. Path pomFile = Paths.get( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
  341. File expectedPomFile = getTestFile( "src/test/expected-files/maven-foo-plugin-1.0.pom" );
  342. assertTrue( "Check POM created", Files.exists(pomFile) );
  343. compareFiles( expectedPomFile, pomFile );
  344. */
  345. }
  346. @Test
  347. public void testV3TimestampedSnapshotPomConvert()
  348. throws Exception
  349. {
  350. // test that the pom is coverted
  351. Artifact artifact = createArtifact( "test", "v3artifact", "1.0.0-20060105.130101-3" );
  352. ArtifactMetadata artifactMetadata = new ArtifactRepositoryMetadata( artifact );
  353. Path artifactMetadataFile = Paths.get( targetRepository.getBasedir(),
  354. targetRepository.pathOfRemoteRepositoryMetadata( artifactMetadata ) );
  355. Files.deleteIfExists(artifactMetadataFile);
  356. ArtifactMetadata snapshotMetadata = new SnapshotArtifactRepositoryMetadata( artifact );
  357. Path snapshotMetadataFile = Paths.get( targetRepository.getBasedir(),
  358. targetRepository.pathOfRemoteRepositoryMetadata( snapshotMetadata ) );
  359. Files.deleteIfExists(snapshotMetadataFile);
  360. artifactConverter.convert( artifact, targetRepository );
  361. checkSuccess( artifactConverter );
  362. Path artifactFile = Paths.get( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
  363. assertTrue( "Check artifact created", Files.exists(artifactFile) );
  364. assertTrue( "Check artifact matches", FileUtils.contentEquals( artifactFile.toFile(), artifact.getFile() ) );
  365. artifact = createPomArtifact( artifact );
  366. Path pomFile = Paths.get( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
  367. Path expectedPomFile = getTestFile( "src/test/expected-files/converted-v3-timestamped-snapshot.pom" );
  368. assertTrue( "Check POM created", Files.exists(pomFile) );
  369. compareFiles( expectedPomFile, pomFile );
  370. assertTrue( "Check artifact snapshotMetadata created", Files.exists(artifactMetadataFile) );
  371. Path expectedMetadataFile = getTestFile( "src/test/expected-files/v3-snapshot-artifact-metadata.xml" );
  372. compareFiles( expectedMetadataFile, artifactMetadataFile );
  373. assertTrue( "Check snapshot snapshotMetadata created", Files.exists(snapshotMetadataFile) );
  374. expectedMetadataFile = getTestFile( "src/test/expected-files/v3-timestamped-snapshot-metadata.xml" );
  375. compareFiles( expectedMetadataFile, snapshotMetadataFile );
  376. }
  377. @Test
  378. public void testNoPomConvert()
  379. throws Exception
  380. {
  381. // test that a POM is not created when there was none at the source
  382. Artifact artifact = createArtifact( "test", "noPomArtifact", "1.0.0" );
  383. artifactConverter.convert( artifact, targetRepository );
  384. checkWarnings( artifactConverter, 1 );
  385. assertHasWarningReason( artifactConverter, Messages.getString( "warning.missing.pom" ) );
  386. Path artifactFile = Paths.get( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
  387. assertTrue( "Check artifact created", Files.exists(artifactFile) );
  388. assertTrue( "Check artifact matches", FileUtils.contentEquals( artifactFile.toFile(), artifact.getFile() ) );
  389. artifact = createPomArtifact( artifact );
  390. Path pomFile = Paths.get( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
  391. Path sourcePomFile = Paths.get( sourceRepository.getBasedir(), sourceRepository.pathOf( artifact ) );
  392. assertFalse( "Check no POM created", Files.exists(pomFile) );
  393. assertFalse( "No source POM", Files.exists(sourcePomFile) );
  394. }
  395. @Test
  396. public void testIncorrectSourceChecksumMd5()
  397. throws Exception
  398. {
  399. // test that it fails when the source md5 is wrong
  400. Artifact artifact = createArtifact( "test", "incorrectMd5Artifact", "1.0.0" );
  401. Path file = Paths.get( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
  402. Files.deleteIfExists(file);
  403. artifactConverter.convert( artifact, targetRepository );
  404. checkWarnings( artifactConverter, 2 );
  405. assertHasWarningReason( artifactConverter, Messages.getString( "failure.incorrect.md5" ) );
  406. assertFalse( "Check artifact not created", Files.exists(file) );
  407. ArtifactRepositoryMetadata metadata = new ArtifactRepositoryMetadata( artifact );
  408. Path metadataFile = Paths.get( targetRepository.getBasedir(), targetRepository.pathOfRemoteRepositoryMetadata( metadata ) );
  409. assertFalse( "Check metadata not created", Files.exists(metadataFile) );
  410. }
  411. @Test
  412. public void testIncorrectSourceChecksumSha1()
  413. throws Exception
  414. {
  415. // test that it fails when the source sha1 is wrong
  416. Artifact artifact = createArtifact( "test", "incorrectSha1Artifact", "1.0.0" );
  417. Path file = Paths.get( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
  418. Files.deleteIfExists(file);
  419. artifactConverter.convert( artifact, targetRepository );
  420. checkWarnings( artifactConverter, 2 );
  421. assertHasWarningReason( artifactConverter, Messages.getString( "failure.incorrect.sha1" ) );
  422. assertFalse( "Check artifact not created", Files.exists(file) );
  423. ArtifactRepositoryMetadata metadata = new ArtifactRepositoryMetadata( artifact );
  424. Path metadataFile = Paths.get( targetRepository.getBasedir(), targetRepository.pathOfRemoteRepositoryMetadata( metadata ) );
  425. assertFalse( "Check metadata not created", Files.exists(metadataFile) );
  426. }
  427. @Test
  428. public void testUnmodifiedArtifact()
  429. throws Exception, InterruptedException
  430. {
  431. // test the unmodified artifact is untouched
  432. Artifact artifact = createArtifact( "test", "unmodified-artifact", "1.0.0" );
  433. Artifact pomArtifact = createPomArtifact( artifact );
  434. Path sourceFile = Paths.get( sourceRepository.getBasedir(), sourceRepository.pathOf( artifact ) );
  435. Path sourcePomFile = Paths.get( sourceRepository.getBasedir(), sourceRepository.pathOf( pomArtifact ) );
  436. Path targetFile = Paths.get( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
  437. Path targetPomFile = Paths.get( targetRepository.getBasedir(), targetRepository.pathOf( pomArtifact ) );
  438. assertTrue( "Check target file exists", Files.exists(targetFile) );
  439. assertTrue( "Check target POM exists", Files.exists(targetPomFile) );
  440. Files.setLastModifiedTime( sourceFile, FileTime.from(System.currentTimeMillis(), TimeUnit.MILLISECONDS) );
  441. Files.setLastModifiedTime( sourcePomFile, FileTime.from(System.currentTimeMillis(), TimeUnit.MILLISECONDS) );
  442. long origTime = Files.getLastModifiedTime( targetFile ).toMillis();
  443. long origPomTime = Files.getLastModifiedTime( targetPomFile ).toMillis();
  444. // Need to guarantee last modified is not equal
  445. Thread.sleep( SLEEP_MILLIS );
  446. artifactConverter.convert( artifact, targetRepository );
  447. checkSuccess( artifactConverter );
  448. compareFiles( sourceFile, targetFile );
  449. compareFiles( sourcePomFile, targetPomFile );
  450. assertEquals( "Check artifact unmodified", origTime, Files.getLastModifiedTime( targetFile ).toMillis() );
  451. assertEquals( "Check POM unmodified", origPomTime, Files.getLastModifiedTime( targetPomFile ).toMillis() );
  452. }
  453. @Test
  454. public void testModifedArtifactFails()
  455. throws Exception
  456. {
  457. // test that it fails when the source artifact has changed and is different to the existing artifact in the
  458. // target repository
  459. Artifact artifact = createArtifact( "test", "modified-artifact", "1.0.0" );
  460. Artifact pomArtifact = createPomArtifact( artifact );
  461. Path sourceFile = Paths.get( sourceRepository.getBasedir(), sourceRepository.pathOf( artifact ) );
  462. Path sourcePomFile = Paths.get( sourceRepository.getBasedir(), sourceRepository.pathOf( pomArtifact ) );
  463. Path targetFile = Paths.get( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
  464. Path targetPomFile = Paths.get( targetRepository.getBasedir(), targetRepository.pathOf( pomArtifact ) );
  465. assertTrue( "Check target file exists", Files.exists(targetFile) );
  466. assertTrue( "Check target POM exists", Files.exists(targetPomFile) );
  467. Files.setLastModifiedTime(sourceFile, FileTime.from(System.currentTimeMillis() , TimeUnit.MILLISECONDS));
  468. Files.setLastModifiedTime(sourcePomFile, FileTime.from(System.currentTimeMillis() , TimeUnit.MILLISECONDS));
  469. long origTime = Files.getLastModifiedTime(targetFile).toMillis();
  470. long origPomTime = Files.getLastModifiedTime(targetPomFile).toMillis();
  471. // Need to guarantee last modified is not equal
  472. Thread.sleep( SLEEP_MILLIS );
  473. artifactConverter.convert( artifact, targetRepository );
  474. checkWarnings( artifactConverter, 2 );
  475. assertHasWarningReason( artifactConverter, Messages.getString( "failure.target.already.exists" ) );
  476. assertEquals( "Check unmodified", origTime, Files.getLastModifiedTime(targetFile).toMillis() );
  477. assertEquals( "Check unmodified", origPomTime, Files.getLastModifiedTime(targetPomFile).toMillis() );
  478. ArtifactRepositoryMetadata metadata = new ArtifactRepositoryMetadata( artifact );
  479. Path metadataFile = Paths.get( targetRepository.getBasedir(), targetRepository.pathOfRemoteRepositoryMetadata( metadata ) );
  480. assertFalse( "Check metadata not created", Files.exists(metadataFile) );
  481. }
  482. @Test
  483. public void testForcedUnmodifiedArtifact()
  484. throws Exception
  485. {
  486. // test unmodified artifact is still converted when set to force
  487. artifactConverter =
  488. applicationContext.getBean( "artifactConverter#force-repository-converter", ArtifactConverter.class );
  489. Artifact artifact = createArtifact( "test", "unmodified-artifact", "1.0.0" );
  490. Artifact pomArtifact = createPomArtifact( artifact );
  491. Path sourceFile = Paths.get( sourceRepository.getBasedir(), sourceRepository.pathOf( artifact ) );
  492. Path sourcePomFile = Paths.get( sourceRepository.getBasedir(), sourceRepository.pathOf( pomArtifact ) );
  493. Path targetFile = Paths.get( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
  494. Path targetPomFile = Paths.get( targetRepository.getBasedir(), targetRepository.pathOf( pomArtifact ) );
  495. SimpleDateFormat dateFormat = new SimpleDateFormat( "yyyy-MM-dd", Locale.getDefault() );
  496. long origTime = dateFormat.parse( "2006-03-03" ).getTime();
  497. Files.setLastModifiedTime(targetFile, FileTime.from(origTime , TimeUnit.MILLISECONDS));
  498. Files.setLastModifiedTime(targetPomFile, FileTime.from(origTime , TimeUnit.MILLISECONDS));
  499. Files.setLastModifiedTime(sourceFile, FileTime.from(dateFormat.parse( "2006-01-01" ).getTime() , TimeUnit.MILLISECONDS));
  500. Files.setLastModifiedTime(sourcePomFile, FileTime.from(dateFormat.parse( "2006-02-02" ).getTime() , TimeUnit.MILLISECONDS));
  501. artifactConverter.convert( artifact, targetRepository );
  502. checkSuccess( artifactConverter );
  503. compareFiles( sourceFile, targetFile );
  504. compareFiles( sourcePomFile, targetPomFile );
  505. assertFalse( "Check modified", origTime == Files.getLastModifiedTime(targetFile).toMillis() );
  506. assertFalse( "Check modified", origTime == Files.getLastModifiedTime(targetPomFile).toMillis() );
  507. ArtifactRepositoryMetadata metadata = new ArtifactRepositoryMetadata( artifact );
  508. Path metadataFile = Paths.get( targetRepository.getBasedir(), targetRepository.pathOfRemoteRepositoryMetadata( metadata ) );
  509. assertTrue( "Check metadata created", Files.exists(metadataFile) );
  510. }
  511. @Test
  512. public void testDryRunSuccess()
  513. throws Exception
  514. {
  515. // test dry run does nothing on a run that will be successful, and returns success
  516. artifactConverter =
  517. applicationContext.getBean( "artifactConverter#dryrun-repository-converter", ArtifactConverter.class );
  518. Artifact artifact = createArtifact( "test", "dryrun-artifact", "1.0.0" );
  519. Artifact pomArtifact = createPomArtifact( artifact );
  520. Path sourceFile = Paths.get( sourceRepository.getBasedir(), sourceRepository.pathOf( artifact ) );
  521. Path sourcePomFile = Paths.get( sourceRepository.getBasedir(), sourceRepository.pathOf( pomArtifact ) );
  522. Path targetFile = Paths.get( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
  523. Path targetPomFile = Paths.get( targetRepository.getBasedir(), targetRepository.pathOf( pomArtifact ) );
  524. // clear warning before test related to MRM-1638
  525. artifactConverter.clearWarnings();
  526. artifactConverter.convert( artifact, targetRepository );
  527. checkSuccess( artifactConverter );
  528. assertTrue( "Check source file exists", Files.exists(sourceFile) );
  529. assertTrue( "Check source POM exists", Files.exists(sourcePomFile) );
  530. assertFalse( "Check target file doesn't exist", Files.exists(targetFile) );
  531. assertFalse( "Check target POM doesn't exist", Files.exists(targetPomFile) );
  532. ArtifactRepositoryMetadata metadata = new ArtifactRepositoryMetadata( artifact );
  533. Path metadataFile = Paths.get( targetRepository.getBasedir(), targetRepository.pathOfRemoteRepositoryMetadata( metadata ) );
  534. assertFalse( "Check metadata not created", Files.exists(metadataFile) );
  535. }
  536. @Test
  537. public void testDryRunFailure()
  538. throws Exception
  539. {
  540. // test dry run does nothing on a run that will fail, and returns failure
  541. artifactConverter =
  542. applicationContext.getBean( "artifactConverter#dryrun-repository-converter", ArtifactConverter.class );
  543. Artifact artifact = createArtifact( "test", "modified-artifact", "1.0.0" );
  544. Artifact pomArtifact = createPomArtifact( artifact );
  545. Path sourceFile = Paths.get( sourceRepository.getBasedir(), sourceRepository.pathOf( artifact ) );
  546. Path sourcePomFile = Paths.get( sourceRepository.getBasedir(), sourceRepository.pathOf( pomArtifact ) );
  547. Path targetFile = Paths.get( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
  548. Path targetPomFile = Paths.get( targetRepository.getBasedir(), targetRepository.pathOf( pomArtifact ) );
  549. assertTrue( "Check target file exists", Files.exists(targetFile) );
  550. assertTrue( "Check target POM exists", Files.exists(targetPomFile) );
  551. Files.setLastModifiedTime(sourceFile, FileTime.from(System.currentTimeMillis() , TimeUnit.MILLISECONDS));
  552. Files.setLastModifiedTime(sourcePomFile, FileTime.from(System.currentTimeMillis() , TimeUnit.MILLISECONDS));
  553. long origTime = Files.getLastModifiedTime(targetFile).toMillis();
  554. long origPomTime = Files.getLastModifiedTime(targetPomFile).toMillis();
  555. // Need to guarantee last modified is not equal
  556. Thread.sleep( SLEEP_MILLIS );
  557. // clear warning before test related to MRM-1638
  558. artifactConverter.clearWarnings();
  559. artifactConverter.convert( artifact, targetRepository );
  560. checkWarnings( artifactConverter, 2 );
  561. assertHasWarningReason( artifactConverter, Messages.getString( "failure.target.already.exists" ) );
  562. assertEquals( "Check unmodified", origTime, Files.getLastModifiedTime(targetFile).toMillis() );
  563. assertEquals( "Check unmodified", origPomTime, Files.getLastModifiedTime(targetPomFile).toMillis() );
  564. ArtifactRepositoryMetadata metadata = new ArtifactRepositoryMetadata( artifact );
  565. Path metadataFile = Paths.get( targetRepository.getBasedir(), targetRepository.pathOfRemoteRepositoryMetadata( metadata ) );
  566. assertFalse( "Check metadata not created", Files.exists(metadataFile) );
  567. }
  568. @Test
  569. public void testRollbackArtifactCreated()
  570. throws Exception
  571. {
  572. // test rollback can remove a created artifact, including checksums
  573. Artifact artifact = createArtifact( "test", "rollback-created-artifact", "1.0.0" );
  574. ArtifactMetadata artifactMetadata = new ArtifactRepositoryMetadata( artifact );
  575. Path artifactMetadataFile = Paths.get( targetRepository.getBasedir(),
  576. targetRepository.pathOfRemoteRepositoryMetadata( artifactMetadata ) );
  577. org.apache.archiva.common.utils.FileUtils.deleteDirectory( artifactMetadataFile.getParent() );
  578. ArtifactMetadata versionMetadata = new SnapshotArtifactRepositoryMetadata( artifact );
  579. Path versionMetadataFile = Paths.get( targetRepository.getBasedir(),
  580. targetRepository.pathOfRemoteRepositoryMetadata( versionMetadata ) );
  581. Path artifactFile = Paths.get( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
  582. artifactConverter.convert( artifact, targetRepository );
  583. checkWarnings( artifactConverter, 2 );
  584. boolean found = false;
  585. String pattern = "^" + Messages.getString( "invalid.source.pom" ).replaceFirst( "\\{0\\}", ".*" ) + "$";
  586. for ( List<String> messages : artifactConverter.getWarnings().values() )
  587. {
  588. for ( String message : messages )
  589. {
  590. if ( message.matches( pattern ) )
  591. {
  592. found = true;
  593. break;
  594. }
  595. }
  596. if ( found )
  597. {
  598. break;
  599. }
  600. }
  601. assertTrue( "Check failure message.", found );
  602. assertFalse( "check artifact rolled back", Files.exists(artifactFile) );
  603. assertFalse( "check metadata rolled back", Files.exists(artifactMetadataFile) );
  604. assertFalse( "check metadata rolled back", Files.exists(versionMetadataFile) );
  605. }
  606. @Test
  607. public void testMultipleArtifacts()
  608. throws Exception
  609. {
  610. // test multiple artifacts are converted
  611. List<Artifact> artifacts = new ArrayList<>();
  612. artifacts.add( createArtifact( "test", "artifact-one", "1.0.0" ) );
  613. artifacts.add( createArtifact( "test", "artifact-two", "1.0.0" ) );
  614. artifacts.add( createArtifact( "test", "artifact-three", "1.0.0" ) );
  615. for ( Artifact artifact : artifacts )
  616. {
  617. artifactConverter.convert( artifact, targetRepository );
  618. checkSuccess( artifactConverter );
  619. }
  620. for ( Artifact artifact : artifacts )
  621. {
  622. Path artifactFile = Paths.get( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
  623. assertTrue( "Check artifact created", Files.exists(artifactFile) );
  624. assertTrue( "Check artifact matches", FileUtils.contentEquals( artifactFile.toFile(), artifact.getFile() ) );
  625. artifact = createPomArtifact( artifact );
  626. Path pomFile = Paths.get( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
  627. Path expectedPomFile =
  628. getTestFile( "src/test/expected-files/converted-" + artifact.getArtifactId() + ".pom" );
  629. assertTrue( "Check POM created", Files.exists(pomFile) );
  630. compareFiles( expectedPomFile, pomFile );
  631. }
  632. }
  633. @Test
  634. public void testInvalidSourceArtifactMetadata()
  635. throws Exception
  636. {
  637. // test artifact is not converted when source metadata is invalid, and returns failure
  638. createModernSourceRepository();
  639. Artifact artifact = createArtifact( "test", "incorrectArtifactMetadata", "1.0.0" );
  640. Path file = Paths.get( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
  641. Files.deleteIfExists(file);
  642. artifactConverter.convert( artifact, targetRepository );
  643. checkWarnings( artifactConverter, 2 );
  644. assertHasWarningReason( artifactConverter,
  645. Messages.getString( "failure.incorrect.artifactMetadata.versions" ) );
  646. assertFalse( "Check artifact not created", Files.exists(file) );
  647. ArtifactRepositoryMetadata metadata = new ArtifactRepositoryMetadata( artifact );
  648. Path metadataFile = Paths.get( targetRepository.getBasedir(), targetRepository.pathOfRemoteRepositoryMetadata( metadata ) );
  649. assertFalse( "Check metadata not created", Files.exists(metadataFile) );
  650. }
  651. @Test
  652. public void testInvalidSourceSnapshotMetadata()
  653. throws Exception
  654. {
  655. // test artifact is not converted when source snapshot metadata is invalid and returns failure
  656. createModernSourceRepository();
  657. Artifact artifact = createArtifact( "test", "incorrectSnapshotMetadata", "1.0.0-20060102.030405-6" );
  658. Path file = Paths.get( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
  659. Files.deleteIfExists(file);
  660. artifactConverter.convert( artifact, targetRepository );
  661. checkWarnings( artifactConverter, 2 );
  662. assertHasWarningReason( artifactConverter,
  663. Messages.getString( "failure.incorrect.snapshotMetadata.snapshot" ) );
  664. assertFalse( "Check artifact not created", Files.exists(file) );
  665. ArtifactRepositoryMetadata metadata = new ArtifactRepositoryMetadata( artifact );
  666. Path metadataFile = Paths.get( targetRepository.getBasedir(), targetRepository.pathOfRemoteRepositoryMetadata( metadata ) );
  667. assertFalse( "Check metadata not created", Files.exists(metadataFile) );
  668. }
  669. @Test
  670. public void testMergeArtifactMetadata()
  671. throws Exception
  672. {
  673. // test artifact level metadata is merged when it already exists on successful conversion
  674. Artifact artifact = createArtifact( "test", "newversion-artifact", "1.0.1" );
  675. artifactConverter.convert( artifact, targetRepository );
  676. checkSuccess( artifactConverter );
  677. Path artifactFile = Paths.get( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
  678. assertTrue( "Check artifact created", Files.exists(artifactFile) );
  679. assertTrue( "Check artifact matches", FileUtils.contentEquals( artifactFile.toFile(), artifact.getFile() ) );
  680. artifact = createPomArtifact( artifact );
  681. Path pomFile = Paths.get( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
  682. Path sourcePomFile = Paths.get( sourceRepository.getBasedir(), sourceRepository.pathOf( artifact ) );
  683. assertTrue( "Check POM created", Files.exists(pomFile) );
  684. compareFiles( sourcePomFile, pomFile );
  685. ArtifactMetadata artifactMetadata = new ArtifactRepositoryMetadata( artifact );
  686. Path artifactMetadataFile = Paths.get( targetRepository.getBasedir(),
  687. targetRepository.pathOfRemoteRepositoryMetadata( artifactMetadata ) );
  688. assertTrue( "Check artifact metadata created", Files.exists(artifactMetadataFile) );
  689. Path expectedMetadataFile = getTestFile( "src/test/expected-files/newversion-artifact-metadata.xml" );
  690. compareFiles( expectedMetadataFile, artifactMetadataFile );
  691. }
  692. @Test
  693. public void testSourceAndTargetRepositoriesMatch()
  694. throws Exception
  695. {
  696. // test that it fails if the same
  697. sourceRepository =
  698. new MavenArtifactRepository( "source", targetRepository.getUrl(), targetRepository.getLayout(), null,
  699. null );
  700. Artifact artifact = createArtifact( "test", "repository-artifact", "1.0" );
  701. try
  702. {
  703. artifactConverter.convert( artifact, targetRepository );
  704. fail( "Should have failed trying to convert within the same repository" );
  705. }
  706. catch ( ArtifactConversionException e )
  707. {
  708. // expected
  709. assertEquals( "check message", Messages.getString( "exception.repositories.match" ), e.getMessage() );
  710. assertNull( "Check no additional cause", e.getCause() );
  711. }
  712. }
  713. private Artifact createArtifact( String groupId, String artifactId, String version )
  714. {
  715. Matcher matcher = Artifact.VERSION_FILE_PATTERN.matcher( version );
  716. String baseVersion;
  717. if ( matcher.matches() )
  718. {
  719. baseVersion = matcher.group( 1 ) + "-SNAPSHOT";
  720. }
  721. else
  722. {
  723. baseVersion = version;
  724. }
  725. return createArtifact( groupId, artifactId, baseVersion, version, "jar" );
  726. }
  727. private Artifact createArtifact( String groupId, String artifactId, String baseVersion, String version,
  728. String type )
  729. {
  730. Artifact artifact = artifactFactory.createArtifact( groupId, artifactId, version, null, type );
  731. artifact.setBaseVersion( baseVersion );
  732. artifact.setRepository( sourceRepository );
  733. artifact.setFile( Paths.get( sourceRepository.getBasedir(), sourceRepository.pathOf( artifact ) ).toFile() );
  734. return artifact;
  735. }
  736. private Artifact createPomArtifact( Artifact artifact )
  737. {
  738. return createArtifact( artifact.getGroupId(), artifact.getArtifactId(), artifact.getBaseVersion(),
  739. artifact.getVersion(), "pom" );
  740. }
  741. private static void compareFiles( Path expectedPomFile, Path pomFile )
  742. throws IOException
  743. {
  744. String expectedContent = normalizeString(
  745. org.apache.archiva.common.utils.FileUtils.readFileToString( expectedPomFile, Charset.defaultCharset() ) );
  746. String targetContent =
  747. normalizeString( org.apache.archiva.common.utils.FileUtils.readFileToString( pomFile, Charset.defaultCharset() ) );
  748. assertEquals( "Check file match between " + expectedPomFile + " and " + pomFile, expectedContent,
  749. targetContent );
  750. }
  751. private static String normalizeString( String path )
  752. {
  753. return path.trim().replaceAll( "\r\n", "\n" ).replace( '\r', '\n' ).replaceAll( "<\\?xml .+\\?>",
  754. "" ).replaceAll( "^\\s+", "" );
  755. }
  756. private void checkSuccess( ArtifactConverter converter )
  757. {
  758. assertNotNull( "Warnings should never be null.", converter.getWarnings() );
  759. assertEquals( "Should have no warnings. " + converter.getWarnings(), 0, countWarningMessages( converter ) );
  760. }
  761. private void checkWarnings( ArtifactConverter converter, int count )
  762. {
  763. assertNotNull( "Warnings should never be null.", converter.getWarnings() );
  764. assertEquals( "Should have some warnings.", count, countWarningMessages( converter ) );
  765. }
  766. private int countWarningMessages( ArtifactConverter converter )
  767. {
  768. int count = 0;
  769. for ( List<String> values : converter.getWarnings().values() )
  770. {
  771. count += values.size();
  772. }
  773. return count;
  774. }
  775. private void assertHasWarningReason( ArtifactConverter converter, String reason )
  776. {
  777. assertNotNull( "Warnings should never be null.", converter.getWarnings() );
  778. assertTrue( "Expecting 1 or more Warnings", countWarningMessages( converter ) > 0 );
  779. for ( List<String> messages : converter.getWarnings().values() )
  780. {
  781. if ( messages.contains( reason ) )
  782. {
  783. /* No need to check any further */
  784. return;
  785. }
  786. }
  787. /* didn't find it. */
  788. for ( Map.Entry<Artifact, List<String>> entry : converter.getWarnings().entrySet() )
  789. {
  790. Artifact artifact = (Artifact) entry.getKey();
  791. System.out.println(
  792. "-Artifact: " + artifact.getGroupId() + ":" + artifact.getArtifactId() + ":" + artifact.getVersion() );
  793. List<String> messages = entry.getValue();
  794. for ( String message : messages )
  795. {
  796. System.out.println( " " + message );
  797. }
  798. }
  799. fail( "Unable to find message <" + reason + "> in warnings." );
  800. }
  801. private void createModernSourceRepository()
  802. throws Exception
  803. {
  804. ArtifactRepositoryLayout layout = plexusSisuBridge.lookup( ArtifactRepositoryLayout.class, "default" );
  805. Path sourceBase = getTestFile( "src/test/source-modern-repository" );
  806. sourceRepository =
  807. new MavenArtifactRepository( "source", sourceBase.toUri().toURL().toString(), layout, null, null );
  808. }
  809. }