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.

MetadataTransferTest.java 65KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440
  1. package org.apache.archiva.maven.proxy;
  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. * Unless required by applicable law or agreed to in writing,
  13. * software distributed under the License is distributed on an
  14. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  15. * KIND, either express or implied. See the License for the
  16. * specific language governing permissions and limitations
  17. * under the License.
  18. */
  19. import org.apache.archiva.common.filelock.DefaultFileLockManager;
  20. import org.apache.archiva.common.utils.VersionUtil;
  21. import org.apache.archiva.configuration.ProxyConnectorConfiguration;
  22. import org.apache.archiva.model.ArchivaRepositoryMetadata;
  23. import org.apache.archiva.model.Plugin;
  24. import org.apache.archiva.model.SnapshotVersion;
  25. import org.apache.archiva.policies.CachedFailuresPolicy;
  26. import org.apache.archiva.policies.ChecksumPolicy;
  27. import org.apache.archiva.policies.ReleasesPolicy;
  28. import org.apache.archiva.policies.SnapshotsPolicy;
  29. import org.apache.archiva.repository.content.BaseRepositoryContentLayout;
  30. import org.apache.archiva.repository.content.ContentItem;
  31. import org.apache.archiva.repository.content.DataItem;
  32. import org.apache.archiva.repository.content.ItemSelector;
  33. import org.apache.archiva.repository.content.Project;
  34. import org.apache.archiva.repository.content.Version;
  35. import org.apache.archiva.repository.content.base.ArchivaItemSelector;
  36. import org.apache.archiva.repository.metadata.RepositoryMetadataException;
  37. import org.apache.archiva.repository.metadata.base.MetadataTools;
  38. import org.apache.archiva.repository.metadata.base.RepositoryMetadataWriter;
  39. import org.apache.archiva.repository.storage.StorageAsset;
  40. import org.apache.archiva.repository.storage.fs.FilesystemStorage;
  41. import org.apache.commons.lang3.StringUtils;
  42. import org.apache.maven.wagon.TransferFailedException;
  43. import org.easymock.EasyMock;
  44. import org.junit.Test;
  45. import org.xmlunit.builder.DiffBuilder;
  46. import org.xmlunit.diff.Diff;
  47. import org.xmlunit.diff.Difference;
  48. import javax.inject.Inject;
  49. import javax.inject.Named;
  50. import java.io.File;
  51. import java.io.StringWriter;
  52. import java.nio.file.Files;
  53. import java.nio.file.Path;
  54. import java.util.ArrayList;
  55. import java.util.Arrays;
  56. import static org.junit.Assert.*;
  57. /**
  58. * MetadataTransferTest - Tests the various fetching / merging concepts surrounding the maven-metadata.xml files
  59. * present in the repository.
  60. * <p/>
  61. * Test Case Naming is as follows.
  62. * <p/>
  63. * <code>
  64. * public void testGet[Release|Snapshot|Project]Metadata[Not]Proxied[Not|On]Local[Not|On|Multiple]Remote
  65. * </code>
  66. * <p/>
  67. * <pre>
  68. * Which should leave the following matrix of test cases.
  69. *
  70. * Metadata | Proxied | Local | Remote
  71. * ----------+----------+-------+---------
  72. * Release | Not | Not | n/a (1)
  73. * Release | Not | On | n/a (1)
  74. * Release | | Not | Not
  75. * Release | | Not | On
  76. * Release | | Not | Multiple
  77. * Release | | On | Not
  78. * Release | | On | On
  79. * Release | | On | Multiple
  80. * Snapshot | Not | Not | n/a (1)
  81. * Snapshot | Not | On | n/a (1)
  82. * Snapshot | | Not | Not
  83. * Snapshot | | Not | On
  84. * Snapshot | | Not | Multiple
  85. * Snapshot | | On | Not
  86. * Snapshot | | On | On
  87. * Snapshot | | On | Multiple
  88. * Project | Not | Not | n/a (1)
  89. * Project | Not | On | n/a (1)
  90. * Project | | Not | Not
  91. * Project | | Not | On
  92. * Project | | Not | Multiple
  93. * Project | | On | Not
  94. * Project | | On | On
  95. * Project | | On | Multiple
  96. *
  97. * (1) If it isn't proxied, no point in having a remote.
  98. * </pre>
  99. *
  100. *
  101. */
  102. public class MetadataTransferTest
  103. extends AbstractProxyTestCase
  104. {
  105. @Inject
  106. @Named(value = "metadataTools#mocked")
  107. private MetadataTools metadataTools;
  108. @Test
  109. public void testGetProjectMetadataProxiedNotLocalOnRemoteConnectoDisabled()
  110. throws Exception
  111. {
  112. // New project metadata that does not exist locally but exists on remote.
  113. String requestedResource = "org/apache/maven/test/get-found-in-proxy/maven-metadata.xml";
  114. setupTestableManagedRepository( requestedResource );
  115. // Configure Connector (usually done within archiva.xml configuration)
  116. saveConnector( ID_DEFAULT_MANAGED, ID_PROXIED1, ChecksumPolicy.FIX, ReleasesPolicy.ALWAYS,
  117. SnapshotsPolicy.ALWAYS, CachedFailuresPolicy.NO, true );
  118. assertResourceNotFound( requestedResource );
  119. assertNoRepoMetadata( ID_PROXIED1, requestedResource );
  120. Path expectedFile = managedDefaultDir.resolve(requestedResource);
  121. BaseRepositoryContentLayout layout = managedDefaultRepository.getLayout( BaseRepositoryContentLayout.class );
  122. ContentItem metaItem = managedDefaultRepository.toItem( requestedResource );
  123. Project project = layout.adaptItem( Project.class, managedDefaultRepository.getParent( metaItem ) );
  124. assertNotNull( project );
  125. String metaPath = managedDefaultRepository.toPath( layout.getMetadataItem( project ) );
  126. StorageAsset downloadedFile = proxyHandler.fetchMetadataFromProxies( managedDefaultRepository.getRepository(),
  127. metaPath ).getFile();
  128. assertNull( "Should not have downloaded a file.", downloadedFile );
  129. assertNoTempFiles( expectedFile );
  130. }
  131. // TODO: same test for other fetch* methods
  132. @Test
  133. public void testFetchFromTwoProxiesWhenFirstConnectionFails()
  134. throws Exception
  135. {
  136. // Project metadata that does not exist locally, but has multiple versions in remote repos
  137. String requestedResource = "org/apache/maven/test/get-default-layout/maven-metadata.xml";
  138. setupTestableManagedRepository( requestedResource );
  139. saveRemoteRepositoryConfig( "badproxied1", "Bad Proxied 1", "http://bad.machine.com/repo/", "default" );
  140. // Configure Connector (usually done within archiva.xml configuration)
  141. saveConnector( ID_DEFAULT_MANAGED, "badproxied1", ChecksumPolicy.FIX, ReleasesPolicy.ALWAYS,
  142. SnapshotsPolicy.ALWAYS, CachedFailuresPolicy.NO, false );
  143. saveConnector( ID_DEFAULT_MANAGED, ID_PROXIED2, ChecksumPolicy.FIX, ReleasesPolicy.ALWAYS,
  144. SnapshotsPolicy.ALWAYS, CachedFailuresPolicy.NO, false );
  145. assertResourceNotFound( requestedResource );
  146. assertNoRepoMetadata( "badproxied1", requestedResource );
  147. assertNoRepoMetadata( ID_PROXIED2, requestedResource );
  148. // ensure that a hard failure in the first proxy connector is skipped and the second repository checked
  149. Path expectedFile = managedDefaultDir.resolve(
  150. metadataTools.getRepositorySpecificName( "badproxied1", requestedResource ) );
  151. wagonMock.get( EasyMock.eq( requestedResource ), EasyMock.anyObject( File.class ));
  152. EasyMock.expectLastCall().andThrow( new TransferFailedException( "can't connect" ) );
  153. wagonMockControl.replay();
  154. assertFetchProjectOrGroup( requestedResource );
  155. wagonMockControl.verify();
  156. assertProjectMetadataContents( requestedResource, new String[]{ "1.0.1" }, "1.0.1", "1.0.1" );
  157. assertNoRepoMetadata( "badproxied1", requestedResource );
  158. assertRepoProjectMetadata( ID_PROXIED2, requestedResource, new String[]{ "1.0.1" } );
  159. }
  160. /**
  161. * Attempt to get the project metadata for non-existant artifact.
  162. * <p/>
  163. * Expected result: the maven-metadata.xml file is not created on the managed repository, nor returned
  164. * to the requesting client.
  165. */
  166. @Test
  167. public void testGetProjectMetadataNotProxiedNotLocal()
  168. throws Exception
  169. {
  170. // The artifactId "get-default-metadata-nonexistant" does not exist (intentionally).
  171. String requestedResource = "org/apache/maven/test/get-default-metadata-nonexistant/maven-metadata.xml";
  172. setupTestableManagedRepository( requestedResource );
  173. config.getConfiguration().setProxyConnectors( new ArrayList<ProxyConnectorConfiguration>( ) );
  174. assertResourceNotFound( requestedResource );
  175. // No proxy setup, nothing fetched, failure expected.
  176. assertFetchProjectOrGroupFailed( requestedResource );
  177. // No local artifactId, and no fetch, should equal no metadata file downloaded / created / updated.
  178. assertResourceNotFound( requestedResource );
  179. }
  180. @Test
  181. public void testGetProjectMetadataNotProxiedOnLocal()
  182. throws Exception
  183. {
  184. // Project metadata that exists and has multiple versions
  185. String requestedResource = "org/apache/maven/test/get-project-metadata/maven-metadata.xml";
  186. setupTestableManagedRepository( requestedResource );
  187. config.getConfiguration().setProxyConnectors( new ArrayList<ProxyConnectorConfiguration>( ) );
  188. assertResourceExists( requestedResource );
  189. // No proxy setup, nothing fetched from remote, but local exists.
  190. assertFetchProjectOrGroup( requestedResource );
  191. // Nothing fetched. Should only contain contents of what is in the repository.
  192. // A metadata update is not performed in this use case. Local metadata content is only
  193. // updated via the metadata updater consumer.
  194. assertProjectMetadataContents( requestedResource, new String[]{ "1.0" }, null, null );
  195. }
  196. @Test
  197. public void testGetProjectMetadataProxiedNotLocalMultipleRemotes()
  198. throws Exception
  199. {
  200. // Project metadata that does not exist locally, but has multiple versions in remote repos
  201. String requestedResource = "org/apache/maven/test/get-default-layout/maven-metadata.xml";
  202. setupTestableManagedRepository( requestedResource );
  203. // Configure Connector (usually done within archiva.xml configuration)
  204. saveConnector( ID_DEFAULT_MANAGED, ID_PROXIED1, ChecksumPolicy.FIX, ReleasesPolicy.ALWAYS,
  205. SnapshotsPolicy.ALWAYS, CachedFailuresPolicy.NO, false );
  206. saveConnector( ID_DEFAULT_MANAGED, ID_PROXIED2, ChecksumPolicy.FIX, ReleasesPolicy.ALWAYS,
  207. SnapshotsPolicy.ALWAYS, CachedFailuresPolicy.NO, false );
  208. assertResourceNotFound( requestedResource );
  209. assertNoRepoMetadata( ID_PROXIED1, requestedResource );
  210. assertNoRepoMetadata( ID_PROXIED2, requestedResource );
  211. // Two proxies setup, metadata fetched from both remotes.
  212. assertFetchProjectOrGroup( requestedResource );
  213. // Nothing fetched. Should only contain contents of what is in the repository.
  214. assertProjectMetadataContents( requestedResource, new String[]{ "1.0", "1.0.1" }, "1.0.1", "1.0.1" );
  215. assertRepoProjectMetadata( ID_PROXIED1, requestedResource, new String[]{ "1.0" } );
  216. assertRepoProjectMetadata( ID_PROXIED2, requestedResource, new String[]{ "1.0.1" } );
  217. }
  218. @Test
  219. public void testGetProjectMetadataProxiedNotLocalNotRemote()
  220. throws Exception
  221. {
  222. // Non-existant project metadata that does not exist locally and doesn't exist on remotes.
  223. String requestedResource = "org/apache/maven/test/get-bogus-artifact/maven-metadata.xml";
  224. setupTestableManagedRepository( requestedResource );
  225. // Configure Connector (usually done within archiva.xml configuration)
  226. saveConnector( ID_DEFAULT_MANAGED, ID_PROXIED1, ChecksumPolicy.FIX, ReleasesPolicy.ALWAYS,
  227. SnapshotsPolicy.ALWAYS, CachedFailuresPolicy.NO, false );
  228. saveConnector( ID_DEFAULT_MANAGED, ID_PROXIED2, ChecksumPolicy.FIX, ReleasesPolicy.ALWAYS,
  229. SnapshotsPolicy.ALWAYS, CachedFailuresPolicy.NO, false );
  230. assertResourceNotFound( requestedResource );
  231. assertNoRepoMetadata( ID_PROXIED1, requestedResource );
  232. assertNoRepoMetadata( ID_PROXIED2, requestedResource );
  233. // Two proxies setup, nothing fetched from remotes, local does not exist.
  234. assertFetchProjectOrGroupFailed( requestedResource );
  235. // Nothing fetched. Nothing should exist.
  236. assertResourceNotFound( requestedResource );
  237. assertNoRepoMetadata( ID_PROXIED1, requestedResource );
  238. assertNoRepoMetadata( ID_PROXIED2, requestedResource );
  239. }
  240. @Test
  241. public void testGetProjectMetadataProxiedNotLocalOnRemote()
  242. throws Exception
  243. {
  244. // New project metadata that does not exist locally but exists on remote.
  245. String requestedResource = "org/apache/maven/test/get-found-in-proxy/maven-metadata.xml";
  246. setupTestableManagedRepository( requestedResource );
  247. // Configure Connector (usually done within archiva.xml configuration)
  248. saveConnector( ID_DEFAULT_MANAGED, ID_PROXIED1, ChecksumPolicy.FIX, ReleasesPolicy.ALWAYS,
  249. SnapshotsPolicy.ALWAYS, CachedFailuresPolicy.NO, false );
  250. assertResourceNotFound( requestedResource );
  251. assertNoRepoMetadata( ID_PROXIED1, requestedResource );
  252. // One proxy setup, metadata fetched from remote, local does not exist.
  253. assertFetchProjectOrGroup( requestedResource );
  254. // Remote fetched. Local created/updated.
  255. assertProjectMetadataContents( requestedResource, new String[]{ "1.0.5" }, "1.0.5", "1.0.5" );
  256. assertRepoProjectMetadata( ID_PROXIED1, requestedResource, new String[]{ "1.0.5" } );
  257. }
  258. @Test
  259. public void testGetProjectMetadataProxiedOnLocalMultipleRemote()
  260. throws Exception
  261. {
  262. // Project metadata that exist locally, and has multiple versions in remote repos
  263. String requestedResource = "org/apache/maven/test/get-on-multiple-repos/maven-metadata.xml";
  264. setupTestableManagedRepository( requestedResource );
  265. // Configure Connector (usually done within archiva.xml configuration)
  266. saveConnector( ID_DEFAULT_MANAGED, ID_PROXIED1, ChecksumPolicy.FIX, ReleasesPolicy.ALWAYS,
  267. SnapshotsPolicy.ALWAYS, CachedFailuresPolicy.NO, false );
  268. saveConnector( ID_DEFAULT_MANAGED, ID_PROXIED2, ChecksumPolicy.FIX, ReleasesPolicy.ALWAYS,
  269. SnapshotsPolicy.ALWAYS, CachedFailuresPolicy.NO, false );
  270. assertProjectMetadataContents( requestedResource, new String[]{ "1.0" }, null, null );
  271. assertNoRepoMetadata( ID_PROXIED1, requestedResource );
  272. assertNoRepoMetadata( ID_PROXIED2, requestedResource );
  273. // Two proxies setup, metadata fetched from both remotes.
  274. assertFetchProjectOrGroup( requestedResource );
  275. // metadata fetched from both repos, and merged with local version.
  276. assertProjectMetadataContents( requestedResource, new String[]{ "1.0", "1.0.1", "2.0" }, "2.0", "2.0" );
  277. assertRepoProjectMetadata( ID_PROXIED1, requestedResource, new String[]{ "1.0", "2.0" } );
  278. assertRepoProjectMetadata( ID_PROXIED2, requestedResource, new String[]{ "1.0", "1.0.1" } );
  279. }
  280. @Test
  281. public void testGetProjectMetadataProxiedOnLocalNotRemote()
  282. throws Exception
  283. {
  284. // Project metadata that exist locally, and does not exist in remote repos.
  285. String requestedResource = "org/apache/maven/test/get-not-on-remotes/maven-metadata.xml";
  286. setupTestableManagedRepository( requestedResource );
  287. config.getConfiguration().setProxyConnectors( new ArrayList<ProxyConnectorConfiguration>( ) );
  288. // Configure Connector (usually done within archiva.xml configuration)
  289. saveConnector( ID_DEFAULT_MANAGED, ID_PROXIED1, ChecksumPolicy.FIX, ReleasesPolicy.ALWAYS,
  290. SnapshotsPolicy.ALWAYS, CachedFailuresPolicy.NO, false );
  291. saveConnector( ID_DEFAULT_MANAGED, ID_PROXIED2, ChecksumPolicy.FIX, ReleasesPolicy.ALWAYS,
  292. SnapshotsPolicy.ALWAYS, CachedFailuresPolicy.NO, false );
  293. assertProjectMetadataContents( requestedResource, new String[]{ "1.0-beta-2" }, null, null );
  294. assertNoRepoMetadata( ID_PROXIED1, requestedResource );
  295. assertNoRepoMetadata( ID_PROXIED2, requestedResource );
  296. // Two proxies setup, metadata fetch from remotes fail (because they dont exist).
  297. assertFetchProjectOrGroup( requestedResource );
  298. // metadata not fetched from both repos, and local version exists.
  299. // Since there was no updated metadata content from a remote/proxy, a metadata update on
  300. // the local file never ran. Local only updates are performed via the metadata updater consumer.
  301. assertProjectMetadataContents( requestedResource, new String[]{ "1.0-beta-2" }, null, null );
  302. assertNoRepoMetadata( ID_PROXIED1, requestedResource );
  303. assertNoRepoMetadata( ID_PROXIED2, requestedResource );
  304. }
  305. @Test
  306. public void testGetProjectMetadataProxiedOnLocalOnRemote()
  307. throws Exception
  308. {
  309. // Project metadata that exist locally and exists on remote.
  310. String requestedResource = "org/apache/maven/test/get-on-local-on-remote/maven-metadata.xml";
  311. setupTestableManagedRepository( requestedResource );
  312. // Configure Connector (usually done within archiva.xml configuration)
  313. saveConnector( ID_DEFAULT_MANAGED, ID_PROXIED1, ChecksumPolicy.FIX, ReleasesPolicy.ALWAYS,
  314. SnapshotsPolicy.ALWAYS, CachedFailuresPolicy.NO, false );
  315. assertProjectMetadataContents( requestedResource, new String[]{ "1.0.8", "1.0.22" }, null, null );
  316. assertNoRepoMetadata( ID_PROXIED1, requestedResource );
  317. // One proxy setup, metadata fetched from remote, local exists.
  318. assertFetchProjectOrGroup( requestedResource );
  319. // Remote fetched. Local updated.
  320. assertProjectMetadataContents( requestedResource, new String[]{ "1.0.8", "1.0.22", "2.0" }, "2.0", "2.0" );
  321. assertRepoProjectMetadata( ID_PROXIED1, requestedResource, new String[]{ "1.0.22", "2.0" } );
  322. }
  323. /**
  324. * A request for a release maven-metadata.xml file that does not exist locally, and the managed
  325. * repository has no proxied repositories set up.
  326. * <p/>
  327. * Expected result: the maven-metadata.xml file is not created on the managed repository, nor returned
  328. * to the requesting client.
  329. */
  330. @Test
  331. public void testGetReleaseMetadataNotProxiedNotLocal()
  332. throws Exception
  333. {
  334. // The artifactId "get-default-metadata-nonexistant" does not exist (intentionally).
  335. String requestedResource = "org/apache/maven/test/get-default-metadata-nonexistant/1.0/maven-metadata.xml";
  336. setupTestableManagedRepository( requestedResource );
  337. assertNoMetadata( requestedResource );
  338. // No proxy setup, nothing fetched, failure expected.
  339. assertFetchVersionedFailed( requestedResource );
  340. // No local artifactId, and no fetch, should equal no metadata file downloaded / created / updated.
  341. assertNoMetadata( requestedResource );
  342. }
  343. /**
  344. * A request for a maven-metadata.xml file that does exist locally, and the managed
  345. * repository has no proxied repositories set up.
  346. * <p/>
  347. * Expected result: the maven-metadata.xml file is updated locally, based off of the managed repository
  348. * information, and then returned to the client.
  349. */
  350. @Test
  351. public void testGetReleaseMetadataNotProxiedOnLocal()
  352. throws Exception
  353. {
  354. String requestedResource = "org/apache/maven/test/get-default-metadata/1.0/maven-metadata.xml";
  355. setupTestableManagedRepository( requestedResource );
  356. assertResourceExists( requestedResource );
  357. assertFetchVersioned( requestedResource );
  358. assertReleaseMetadataContents( requestedResource );
  359. }
  360. /**
  361. * A request for a release maven-metadata.xml file that does not exist on the managed repository, but
  362. * exists on multiple remote repositories.
  363. * <p/>
  364. * Expected result: the maven-metadata.xml file is downloaded from the remote into the repository specific
  365. * file location on the managed repository, a merge of the contents to the requested
  366. * maven-metadata.xml is performed, and then the merged maven-metadata.xml file is
  367. * returned to the client.
  368. */
  369. @Test
  370. public void testGetReleaseMetadataProxiedNotLocalMultipleRemotes()
  371. throws Exception
  372. {
  373. String requestedResource = "org/apache/maven/test/get-default-layout/1.0/maven-metadata.xml";
  374. setupTestableManagedRepository( requestedResource );
  375. // Configure Connector (usually done within archiva.xml configuration)
  376. saveConnector( ID_DEFAULT_MANAGED, ID_PROXIED1, ChecksumPolicy.FIX, ReleasesPolicy.ALWAYS,
  377. SnapshotsPolicy.ALWAYS, CachedFailuresPolicy.NO, false );
  378. saveConnector( ID_DEFAULT_MANAGED, ID_PROXIED2, ChecksumPolicy.FIX, ReleasesPolicy.ALWAYS,
  379. SnapshotsPolicy.ALWAYS, CachedFailuresPolicy.NO, false );
  380. assertResourceNotFound( requestedResource );
  381. assertNoRepoMetadata( ID_PROXIED1, requestedResource );
  382. assertNoRepoMetadata( ID_PROXIED2, requestedResource );
  383. assertFetchVersioned( requestedResource );
  384. assertReleaseMetadataContents( requestedResource );
  385. assertRepoReleaseMetadataContents( ID_PROXIED1, requestedResource );
  386. assertRepoReleaseMetadataContents( ID_PROXIED2, requestedResource );
  387. }
  388. /**
  389. * A request for a maven-metadata.xml file that does not exist locally, nor does it exist in a remote
  390. * proxied repository.
  391. * <p/>
  392. * Expected result: the maven-metadata.xml file is created locally, based off of managed repository
  393. * information, and then return to the client.
  394. */
  395. @Test
  396. public void testGetReleaseMetadataProxiedNotLocalNotRemote()
  397. throws Exception
  398. {
  399. String requestedResource = "org/apache/maven/test/get-bad-metadata/1.0/maven-metadata.xml";
  400. setupTestableManagedRepository( requestedResource );
  401. // Configure Connector (usually done within archiva.xml configuration)
  402. saveConnector( ID_DEFAULT_MANAGED, ID_PROXIED1, ChecksumPolicy.FIX, ReleasesPolicy.ALWAYS,
  403. SnapshotsPolicy.ALWAYS, CachedFailuresPolicy.NO, false );
  404. assertResourceNotFound( requestedResource );
  405. assertFetchProjectOrGroupFailed( requestedResource );
  406. assertResourceNotFound( requestedResource );
  407. assertNoRepoMetadata( ID_PROXIED1, requestedResource );
  408. }
  409. /**
  410. * A request for a maven-metadata.xml file that does not exist on the managed repository, but
  411. * exists on 1 remote repository.
  412. * <p/>
  413. * Expected result: the maven-metadata.xml file is downloaded from the remote into the repository specific
  414. * file location on the managed repository, a merge of the contents to the requested
  415. * maven-metadata.xml is performed, and then the merged maven-metadata.xml file is
  416. * returned to the client.
  417. */
  418. @Test
  419. public void testGetReleaseMetadataProxiedNotLocalOnRemote()
  420. throws Exception
  421. {
  422. String requestedResource = "org/apache/maven/test/get-default-layout/1.0/maven-metadata.xml";
  423. setupTestableManagedRepository( requestedResource );
  424. // Configure Connector (usually done within archiva.xml configuration)
  425. saveConnector( ID_DEFAULT_MANAGED, ID_PROXIED1, ChecksumPolicy.FIX, ReleasesPolicy.ALWAYS,
  426. SnapshotsPolicy.ALWAYS, CachedFailuresPolicy.NO, false );
  427. assertNoRepoMetadata( ID_PROXIED1, requestedResource );
  428. assertFetchVersioned( requestedResource );
  429. assertReleaseMetadataContents( requestedResource );
  430. assertRepoReleaseMetadataContents( ID_PROXIED1, requestedResource );
  431. }
  432. /**
  433. * A request for a maven-metadata.xml file that exists in the managed repository, but
  434. * not on any remote repository.
  435. * <p/>
  436. * Expected result: the maven-metadata.xml file does not exist on the remote proxied repository and
  437. * is not downloaded. There is no repository specific metadata file on the managed
  438. * repository. The managed repository maven-metadata.xml is returned to the
  439. * client as-is.
  440. */
  441. @Test
  442. public void testGetReleaseMetadataProxiedOnLocalNotRemote()
  443. throws Exception
  444. {
  445. String requestedResource = "org/apache/maven/test/get-not-on-remotes/1.0-beta-2/maven-metadata.xml";
  446. setupTestableManagedRepository( requestedResource );
  447. // Configure Connector (usually done within archiva.xml configuration)
  448. saveConnector( ID_DEFAULT_MANAGED, ID_PROXIED1, ChecksumPolicy.FIX, ReleasesPolicy.ALWAYS,
  449. SnapshotsPolicy.ALWAYS, CachedFailuresPolicy.NO, false );
  450. assertReleaseMetadataContents( requestedResource );
  451. assertFetchVersioned( requestedResource );
  452. assertReleaseMetadataContents( requestedResource );
  453. assertNoRepoMetadata( ID_PROXIED1, requestedResource );
  454. }
  455. /**
  456. * A request for a maven-metadata.xml file that exists in the managed repository, and on multiple
  457. * remote repositories.
  458. * <p/>
  459. * Expected result: the maven-metadata.xml file on the remote proxied repository is downloaded
  460. * and merged into the contents of the existing managed repository copy of
  461. * the maven-metadata.xml file.
  462. */
  463. @Test
  464. public void testGetReleaseMetadataProxiedOnLocalMultipleRemote()
  465. throws Exception
  466. {
  467. String requestedResource = "org/apache/maven/test/get-on-multiple-repos/1.0/maven-metadata.xml";
  468. setupTestableManagedRepository( requestedResource );
  469. // Configure Connector (usually done within archiva.xml configuration)
  470. saveConnector( ID_DEFAULT_MANAGED, ID_PROXIED1, ChecksumPolicy.FIX, ReleasesPolicy.ALWAYS,
  471. SnapshotsPolicy.ALWAYS, CachedFailuresPolicy.NO, false );
  472. saveConnector( ID_DEFAULT_MANAGED, ID_PROXIED2, ChecksumPolicy.FIX, ReleasesPolicy.ALWAYS,
  473. SnapshotsPolicy.ALWAYS, CachedFailuresPolicy.NO, false );
  474. assertReleaseMetadataContents( requestedResource );
  475. assertNoRepoMetadata( ID_PROXIED1, requestedResource );
  476. assertNoRepoMetadata( ID_PROXIED2, requestedResource );
  477. assertFetchVersioned( requestedResource );
  478. assertReleaseMetadataContents( requestedResource );
  479. assertRepoReleaseMetadataContents( ID_PROXIED1, requestedResource );
  480. assertRepoReleaseMetadataContents( ID_PROXIED2, requestedResource );
  481. }
  482. /**
  483. * A request for a maven-metadata.xml file that exists in the managed repository, and on one
  484. * remote repository.
  485. * <p/>
  486. * Expected result: the maven-metadata.xml file on the remote proxied repository is downloaded
  487. * and merged into the contents of the existing managed repository copy of
  488. * the maven-metadata.xml file.
  489. */
  490. @Test
  491. public void testGetReleaseMetadataProxiedOnLocalOnRemote()
  492. throws Exception
  493. {
  494. String requestedResource = "org/apache/maven/test/get-on-local-on-remote/1.0.22/maven-metadata.xml";
  495. setupTestableManagedRepository( requestedResource );
  496. // Configure Connector (usually done within archiva.xml configuration)
  497. saveConnector( ID_DEFAULT_MANAGED, ID_PROXIED1, ChecksumPolicy.FIX, ReleasesPolicy.ALWAYS,
  498. SnapshotsPolicy.ALWAYS, CachedFailuresPolicy.NO, false );
  499. assertReleaseMetadataContents( requestedResource );
  500. assertNoRepoMetadata( ID_PROXIED1, requestedResource );
  501. assertFetchVersioned( requestedResource );
  502. assertReleaseMetadataContents( requestedResource );
  503. assertRepoReleaseMetadataContents( ID_PROXIED1, requestedResource );
  504. }
  505. @Test
  506. public void testGetSnapshotMetadataNotProxiedNotLocal()
  507. throws Exception
  508. {
  509. // The artifactId "get-default-metadata-nonexistant" does not exist (intentionally).
  510. String requestedResource =
  511. "org/apache/maven/test/get-default-metadata-nonexistant/1.0-SNAPSHOT/maven-metadata.xml";
  512. setupTestableManagedRepository( requestedResource );
  513. assertNoMetadata( requestedResource );
  514. // No proxy setup, nothing fetched, no local file, failure expected.
  515. assertFetchVersionedFailed( requestedResource );
  516. // No local artifactId, and no fetch, should equal no metadata file downloaded / created / updated.
  517. assertNoMetadata( requestedResource );
  518. }
  519. @Test
  520. public void testGetSnapshotMetadataNotProxiedOnLocal()
  521. throws Exception
  522. {
  523. // The artifactId exists locally (but not on a remote repo)
  524. String requestedResource =
  525. "org/apache/maven/test/get-snapshot-on-local-not-remote/2.0-alpha-2-SNAPSHOT/maven-metadata.xml";
  526. setupTestableManagedRepository( requestedResource );
  527. assertResourceExists( requestedResource );
  528. // No proxy setup, nothing fetched from remote, local file exists, fetch should succeed.
  529. assertFetchVersioned( requestedResource );
  530. // Local metadata exists, should be updated to reflect the latest release.
  531. assertSnapshotMetadataContents( requestedResource, "20070821", "220304", 2 );
  532. }
  533. @Test
  534. public void testGetSnapshotMetadataProxiedNotLocalMultipleRemotes()
  535. throws Exception
  536. {
  537. String requestedResource =
  538. "org/apache/maven/test/get-timestamped-snapshot-in-both/1.0-SNAPSHOT/maven-metadata.xml";
  539. setupTestableManagedRepository( requestedResource );
  540. // Configure Connector (usually done within archiva.xml configuration)
  541. saveConnector( ID_DEFAULT_MANAGED, ID_PROXIED1, ChecksumPolicy.FIX, ReleasesPolicy.ALWAYS,
  542. SnapshotsPolicy.ALWAYS, CachedFailuresPolicy.NO, false );
  543. saveConnector( ID_DEFAULT_MANAGED, ID_PROXIED2, ChecksumPolicy.FIX, ReleasesPolicy.ALWAYS,
  544. SnapshotsPolicy.ALWAYS, CachedFailuresPolicy.NO, false );
  545. assertResourceNotFound( requestedResource );
  546. assertNoRepoMetadata( ID_PROXIED1, requestedResource );
  547. assertNoRepoMetadata( ID_PROXIED2, requestedResource );
  548. // Proxying 2 repos, both have content, local file updated.
  549. assertFetchVersioned( requestedResource );
  550. assertSnapshotMetadataContents( requestedResource, "20070101", "000103", 2 );
  551. assertRepoSnapshotMetadataContents( ID_PROXIED1, requestedResource, "20061227", "112101", 2 );
  552. assertRepoSnapshotMetadataContents( ID_PROXIED2, requestedResource, "20070101", "000103", 2 );
  553. }
  554. @Test
  555. public void testGetSnapshotMetadataProxiedNotLocalNotRemote()
  556. throws Exception
  557. {
  558. // The artifactId "get-default-metadata-nonexistant" does not exist (intentionally).
  559. String requestedResource =
  560. "org/apache/maven/test/get-default-metadata-nonexistant/1.0-SNAPSHOT/maven-metadata.xml";
  561. setupTestableManagedRepository( requestedResource );
  562. // Configure Connector (usually done within archiva.xml configuration)
  563. saveConnector( ID_DEFAULT_MANAGED, ID_PROXIED1, ChecksumPolicy.FIX, ReleasesPolicy.ALWAYS,
  564. SnapshotsPolicy.ALWAYS, CachedFailuresPolicy.NO, false );
  565. assertNoMetadata( requestedResource );
  566. // One proxy setup, nothing fetched, no local file, failure expected.
  567. assertFetchVersionedFailed( requestedResource );
  568. // No local artifactId, and no fetch, should equal no metadata file downloaded / created / updated.
  569. assertNoMetadata( requestedResource );
  570. assertNoRepoMetadata( ID_PROXIED1, requestedResource );
  571. }
  572. @Test
  573. public void testGetSnapshotMetadataProxiedNotLocalOnRemote()
  574. throws Exception
  575. {
  576. // Artifact exists only in the proxied1 location.
  577. String requestedResource = "org/apache/maven/test/get-metadata-snapshot/1.0-SNAPSHOT/maven-metadata.xml";
  578. setupTestableManagedRepository( requestedResource );
  579. // Configure Connector (usually done within archiva.xml configuration)
  580. saveConnector( ID_DEFAULT_MANAGED, ID_PROXIED1, ChecksumPolicy.FIX, ReleasesPolicy.ALWAYS,
  581. SnapshotsPolicy.ALWAYS, CachedFailuresPolicy.NO, false );
  582. assertResourceNotFound( requestedResource );
  583. // One proxy setup, one metadata fetched, local file created/updated.
  584. assertFetchVersioned( requestedResource );
  585. // Local artifact Id should contain latest (which in this case is from proxied download)
  586. assertSnapshotMetadataContents( requestedResource, "20050831", "101112", 1 );
  587. assertRepoSnapshotMetadataContents( ID_PROXIED1, requestedResource, "20050831", "101112", 1 );
  588. }
  589. @Test
  590. public void testGetSnapshotMetadataProxiedOnLocalMultipleRemote()
  591. throws Exception
  592. {
  593. String requestedResource = "org/apache/maven/test/get-snapshot-popular/2.0-SNAPSHOT/maven-metadata.xml";
  594. setupTestableManagedRepository( requestedResource );
  595. // Configure Connector (usually done within archiva.xml configuration)
  596. saveConnector( ID_DEFAULT_MANAGED, ID_PROXIED1, ChecksumPolicy.FIX, ReleasesPolicy.ALWAYS,
  597. SnapshotsPolicy.ALWAYS, CachedFailuresPolicy.NO, false );
  598. saveConnector( ID_DEFAULT_MANAGED, ID_PROXIED2, ChecksumPolicy.FIX, ReleasesPolicy.ALWAYS,
  599. SnapshotsPolicy.ALWAYS, CachedFailuresPolicy.NO, false );
  600. assertSnapshotMetadataContents( requestedResource, "20070822", "021008", 3 );
  601. assertNoRepoMetadata( ID_PROXIED1, requestedResource );
  602. assertNoRepoMetadata( ID_PROXIED2, requestedResource );
  603. // Proxying 2 repos, both have content, local file updated.
  604. assertFetchVersioned( requestedResource );
  605. assertSnapshotMetadataContents( requestedResource, "20070823", "212711", 6 );
  606. assertRepoSnapshotMetadataContents( ID_PROXIED1, requestedResource, "20070822", "145534", 9 );
  607. assertRepoSnapshotMetadataContents( ID_PROXIED2, requestedResource, "20070823", "212711", 6 );
  608. }
  609. @Test
  610. public void testGetSnapshotMetadataProxiedOnLocalNotRemote()
  611. throws Exception
  612. {
  613. // The artifactId exists locally (but not on a remote repo)
  614. String requestedResource =
  615. "org/apache/maven/test/get-snapshot-on-local-not-remote/2.0-alpha-2-SNAPSHOT/maven-metadata.xml";
  616. setupTestableManagedRepository( requestedResource );
  617. // Configure Connector (usually done within archiva.xml configuration)
  618. saveConnector( ID_DEFAULT_MANAGED, ID_PROXIED1, ChecksumPolicy.FIX, ReleasesPolicy.ALWAYS,
  619. SnapshotsPolicy.ALWAYS, CachedFailuresPolicy.NO, false );
  620. saveConnector( ID_DEFAULT_MANAGED, ID_PROXIED2, ChecksumPolicy.FIX, ReleasesPolicy.ALWAYS,
  621. SnapshotsPolicy.ALWAYS, CachedFailuresPolicy.NO, false );
  622. assertResourceExists( requestedResource );
  623. assertNoRepoMetadata( ID_PROXIED1, requestedResource );
  624. assertNoRepoMetadata( ID_PROXIED2, requestedResource );
  625. // two proxies setup, nothing fetched from either remote, local file exists, fetch should succeed.
  626. assertFetchVersioned( requestedResource );
  627. // Local metadata exists, repo metadatas should not exist, local file updated.
  628. assertSnapshotMetadataContents( requestedResource, "20070821", "220304", 2 );
  629. assertNoRepoMetadata( ID_PROXIED1, requestedResource );
  630. assertNoRepoMetadata( ID_PROXIED2, requestedResource );
  631. }
  632. @Test
  633. public void testGetSnapshotMetadataProxiedOnLocalOnRemote()
  634. throws Exception
  635. {
  636. // The artifactId exists locally (but not on a remote repo)
  637. String requestedResource =
  638. "org/apache/maven/test/get-present-metadata-snapshot/1.0-SNAPSHOT/maven-metadata.xml";
  639. setupTestableManagedRepository( requestedResource );
  640. // Configure Connector (usually done within archiva.xml configuration)
  641. saveConnector( ID_DEFAULT_MANAGED, ID_PROXIED1, ChecksumPolicy.FIX, ReleasesPolicy.ALWAYS,
  642. SnapshotsPolicy.ALWAYS, CachedFailuresPolicy.NO, false );
  643. assertSnapshotMetadataContents( requestedResource, "20050831", "101112", 1 );
  644. assertNoRepoMetadata( ID_PROXIED1, requestedResource );
  645. // two proxies setup, nothing fetched from either remote, local file exists, fetch should succeed.
  646. assertFetchVersioned( requestedResource );
  647. // Local metadata exists, repo metadata exists, local file updated.
  648. assertSnapshotMetadataContents( requestedResource, "20050831", "101112", 1 );
  649. assertRepoSnapshotMetadataContents( ID_PROXIED1, requestedResource, "20050831", "101112", 1 );
  650. }
  651. @Test
  652. public void testGetGroupMetadataNotProxiedNotLocal()
  653. throws Exception
  654. {
  655. // The artifactId "get-default-metadata-nonexistant" does not exist (intentionally).
  656. String requestedResource = "org/apache/maven/test/groups/get-default-metadata-nonexistant/maven-metadata.xml";
  657. setupTestableManagedRepository( requestedResource );
  658. assertResourceNotFound( requestedResource );
  659. // No proxy setup, nothing fetched, failure expected.
  660. assertFetchProjectOrGroupFailed( requestedResource );
  661. // No local artifactId, and no fetch, should equal no metadata file downloaded / created / updated.
  662. assertResourceNotFound( requestedResource );
  663. }
  664. @Test
  665. public void testGetGroupMetadataNotProxiedOnLocal()
  666. throws Exception
  667. {
  668. // Project metadata that exists and has multiple versions
  669. String requestedResource = "org/apache/maven/test/groups/get-project-metadata/maven-metadata.xml";
  670. setupTestableManagedRepository( requestedResource );
  671. assertResourceExists( requestedResource );
  672. // No proxy setup, nothing fetched from remote, but local exists.
  673. assertFetchProjectOrGroup( requestedResource );
  674. // Nothing fetched. Should only contain contents of what is in the repository.
  675. // A metadata update is not performed in this use case. Local metadata content is only
  676. // updated via the metadata updater consumer.
  677. assertGroupMetadataContents( requestedResource, new String[]{ "plugin1" } );
  678. }
  679. @Test
  680. public void testGetGroupMetadataProxiedNotLocalMultipleRemotes()
  681. throws Exception
  682. {
  683. // Project metadata that does not exist locally, but has multiple versions in remote repos
  684. String requestedResource = "org/apache/maven/test/groups/get-default-layout/maven-metadata.xml";
  685. setupTestableManagedRepository( requestedResource );
  686. // Configure Connector (usually done within archiva.xml configuration)
  687. saveConnector( ID_DEFAULT_MANAGED, ID_PROXIED1, ChecksumPolicy.FIX, ReleasesPolicy.ALWAYS,
  688. SnapshotsPolicy.ALWAYS, CachedFailuresPolicy.NO, false );
  689. saveConnector( ID_DEFAULT_MANAGED, ID_PROXIED2, ChecksumPolicy.FIX, ReleasesPolicy.ALWAYS,
  690. SnapshotsPolicy.ALWAYS, CachedFailuresPolicy.NO, false );
  691. assertResourceNotFound( requestedResource );
  692. assertNoRepoMetadata( ID_PROXIED1, requestedResource );
  693. assertNoRepoMetadata( ID_PROXIED2, requestedResource );
  694. // Two proxies setup, metadata fetched from both remotes.
  695. assertFetchProjectOrGroup( requestedResource );
  696. // Nothing fetched. Should only contain contents of what is in the repository.
  697. assertGroupMetadataContents( requestedResource, new String[]{ "plugin2", "plugin1" } );
  698. assertRepoGroupMetadataContents( ID_PROXIED1, requestedResource, new String[]{ "plugin1" } );
  699. assertRepoGroupMetadataContents( ID_PROXIED2, requestedResource, new String[]{ "plugin2" } );
  700. }
  701. @Test
  702. public void testGetGroupsMetadataProxiedNotLocalNotRemote()
  703. throws Exception
  704. {
  705. // Non-existant project metadata that does not exist locally and doesn't exist on remotes.
  706. String requestedResource = "org/apache/maven/test/groups/get-bogus-artifact/maven-metadata.xml";
  707. setupTestableManagedRepository( requestedResource );
  708. // Configure Connector (usually done within archiva.xml configuration)
  709. saveConnector( ID_DEFAULT_MANAGED, ID_PROXIED1, ChecksumPolicy.FIX, ReleasesPolicy.ALWAYS,
  710. SnapshotsPolicy.ALWAYS, CachedFailuresPolicy.NO, false );
  711. saveConnector( ID_DEFAULT_MANAGED, ID_PROXIED2, ChecksumPolicy.FIX, ReleasesPolicy.ALWAYS,
  712. SnapshotsPolicy.ALWAYS, CachedFailuresPolicy.NO, false );
  713. assertResourceNotFound( requestedResource );
  714. assertNoRepoMetadata( ID_PROXIED1, requestedResource );
  715. assertNoRepoMetadata( ID_PROXIED2, requestedResource );
  716. // Two proxies setup, nothing fetched from remotes, local does not exist.
  717. assertFetchProjectOrGroupFailed( requestedResource );
  718. // Nothing fetched. Nothing should exist.
  719. assertResourceNotFound( requestedResource );
  720. assertNoRepoMetadata( ID_PROXIED1, requestedResource );
  721. assertNoRepoMetadata( ID_PROXIED2, requestedResource );
  722. }
  723. @Test
  724. public void testGetGroupMetadataProxiedNotLocalOnRemote()
  725. throws Exception
  726. {
  727. // New project metadata that does not exist locally but exists on remote.
  728. String requestedResource = "org/apache/maven/test/groups/get-found-in-proxy/maven-metadata.xml";
  729. setupTestableManagedRepository( requestedResource );
  730. // Configure Connector (usually done within archiva.xml configuration)
  731. saveConnector( ID_DEFAULT_MANAGED, ID_PROXIED1, ChecksumPolicy.FIX, ReleasesPolicy.ALWAYS,
  732. SnapshotsPolicy.ALWAYS, CachedFailuresPolicy.NO, false );
  733. assertResourceNotFound( requestedResource );
  734. assertNoRepoMetadata( ID_PROXIED1, requestedResource );
  735. // One proxy setup, metadata fetched from remote, local does not exist.
  736. assertFetchProjectOrGroup( requestedResource );
  737. // Remote fetched. Local created/updated.
  738. assertGroupMetadataContents( requestedResource, new String[]{ "plugin3" } );
  739. assertRepoGroupMetadataContents( ID_PROXIED1, requestedResource, new String[]{ "plugin3" } );
  740. }
  741. @Test
  742. public void testGetGroupMetadataProxiedOnLocalMultipleRemote()
  743. throws Exception
  744. {
  745. // Project metadata that exist locally, and has multiple versions in remote repos
  746. String requestedResource = "org/apache/maven/test/groups/get-on-multiple-repos/maven-metadata.xml";
  747. setupTestableManagedRepository( requestedResource );
  748. // Configure Connector (usually done within archiva.xml configuration)
  749. saveConnector( ID_DEFAULT_MANAGED, ID_PROXIED1, ChecksumPolicy.FIX, ReleasesPolicy.ALWAYS,
  750. SnapshotsPolicy.ALWAYS, CachedFailuresPolicy.NO, false );
  751. saveConnector( ID_DEFAULT_MANAGED, ID_PROXIED2, ChecksumPolicy.FIX, ReleasesPolicy.ALWAYS,
  752. SnapshotsPolicy.ALWAYS, CachedFailuresPolicy.NO, false );
  753. assertGroupMetadataContents( requestedResource, new String[]{ "plugin1" } );
  754. assertNoRepoMetadata( ID_PROXIED1, requestedResource );
  755. assertNoRepoMetadata( ID_PROXIED2, requestedResource );
  756. // Two proxies setup, metadata fetched from both remotes.
  757. assertFetchProjectOrGroup( requestedResource );
  758. // metadata fetched from both repos, and merged with local version.
  759. assertGroupMetadataContents( requestedResource, new String[]{ "plugin1", "plugin2", "plugin4" } );
  760. assertRepoGroupMetadataContents( ID_PROXIED1, requestedResource, new String[]{ "plugin1", "plugin4" } );
  761. assertRepoGroupMetadataContents( ID_PROXIED2, requestedResource, new String[]{ "plugin1", "plugin2" } );
  762. }
  763. @Test
  764. public void testGetGroupMetadataProxiedOnLocalNotRemote()
  765. throws Exception
  766. {
  767. // Project metadata that exist locally, and does not exist in remote repos.
  768. String requestedResource = "org/apache/maven/test/groups/get-not-on-remotes/maven-metadata.xml";
  769. setupTestableManagedRepository( requestedResource );
  770. // Configure Connector (usually done within archiva.xml configuration)
  771. saveConnector( ID_DEFAULT_MANAGED, ID_PROXIED1, ChecksumPolicy.FIX, ReleasesPolicy.ALWAYS,
  772. SnapshotsPolicy.ALWAYS, CachedFailuresPolicy.NO, false );
  773. saveConnector( ID_DEFAULT_MANAGED, ID_PROXIED2, ChecksumPolicy.FIX, ReleasesPolicy.ALWAYS,
  774. SnapshotsPolicy.ALWAYS, CachedFailuresPolicy.NO, false );
  775. assertGroupMetadataContents( requestedResource, new String[]{ "plugin5" } );
  776. assertNoRepoMetadata( ID_PROXIED1, requestedResource );
  777. assertNoRepoMetadata( ID_PROXIED2, requestedResource );
  778. // Two proxies setup, metadata fetch from remotes fail (because they dont exist).
  779. assertFetchProjectOrGroup( requestedResource );
  780. // metadata not fetched from both repos, and local version exists.
  781. // Since there was no updated metadata content from a remote/proxy, a metadata update on
  782. // the local file never ran. Local only updates are performed via the metadata updater consumer.
  783. assertGroupMetadataContents( requestedResource, new String[]{ "plugin5" } );
  784. assertNoRepoMetadata( ID_PROXIED1, requestedResource );
  785. assertNoRepoMetadata( ID_PROXIED2, requestedResource );
  786. }
  787. @Test
  788. public void testGetGroupMetadataProxiedOnLocalOnRemote()
  789. throws Exception
  790. {
  791. // Project metadata that exist locally and exists on remote.
  792. String requestedResource = "org/apache/maven/test/groups/get-on-local-on-remote/maven-metadata.xml";
  793. setupTestableManagedRepository( requestedResource );
  794. // Configure Connector (usually done within archiva.xml configuration)
  795. saveConnector( ID_DEFAULT_MANAGED, ID_PROXIED1, ChecksumPolicy.FIX, ReleasesPolicy.ALWAYS,
  796. SnapshotsPolicy.ALWAYS, CachedFailuresPolicy.NO, false );
  797. assertGroupMetadataContents( requestedResource, new String[]{ "plugin6", "plugin7" } );
  798. assertNoRepoMetadata( ID_PROXIED1, requestedResource );
  799. // One proxy setup, metadata fetched from remote, local exists.
  800. assertFetchProjectOrGroup( requestedResource );
  801. // Remote fetched. Local updated.
  802. assertGroupMetadataContents( requestedResource, new String[]{ "plugin6", "plugin7", "plugin4" } );
  803. assertRepoGroupMetadataContents( ID_PROXIED1, requestedResource, new String[]{ "plugin7", "plugin4" } );
  804. }
  805. /**
  806. * Transfer the metadata file.
  807. *
  808. * @param requestedResource the requested resource
  809. * @throws Exception
  810. */
  811. private void assertFetchProjectOrGroup( String requestedResource )
  812. throws Exception
  813. {
  814. Path expectedFile = managedDefaultDir.resolve(requestedResource);
  815. BaseRepositoryContentLayout layout = managedDefaultRepository.getLayout( BaseRepositoryContentLayout.class );
  816. ContentItem metaItem = managedDefaultRepository.toItem( requestedResource );
  817. Project project = layout.adaptItem( Project.class, managedDefaultRepository.getParent( metaItem ) );
  818. assertNotNull( project );
  819. String metaPath = managedDefaultRepository.toPath( layout.getMetadataItem( project ) );
  820. StorageAsset downloadedFile = proxyHandler.fetchMetadataFromProxies( managedDefaultRepository.getRepository(),
  821. metaPath ).getFile();
  822. assertNotNull( "Should have downloaded a file.", downloadedFile );
  823. assertNoTempFiles( expectedFile );
  824. }
  825. /**
  826. * Transfer the metadata file, not expected to succeed.
  827. *
  828. * @param requestedResource the requested resource
  829. * @throws Exception
  830. */
  831. private void assertFetchProjectOrGroupFailed( String requestedResource )
  832. throws Exception
  833. {
  834. Path expectedFile = managedDefaultDir.resolve(requestedResource);
  835. BaseRepositoryContentLayout layout = managedDefaultRepository.getLayout( BaseRepositoryContentLayout.class );
  836. ContentItem metaItem = managedDefaultRepository.toItem( requestedResource );
  837. Project project = layout.adaptItem( Project.class, managedDefaultRepository.getParent( metaItem ) );
  838. assertNotNull( project );
  839. String metaPath = managedDefaultRepository.toPath( layout.getMetadataItem( project ) );
  840. StorageAsset downloadedFile = proxyHandler.fetchMetadataFromProxies( managedDefaultRepository.getRepository(),
  841. metaPath ).getFile();
  842. assertNull( downloadedFile );
  843. assertNoTempFiles( expectedFile );
  844. }
  845. /**
  846. * Transfer the metadata file.
  847. *
  848. * @param requestedResource the requested resource
  849. * @throws Exception
  850. */
  851. private void assertFetchVersioned( String requestedResource )
  852. throws Exception
  853. {
  854. Path expectedFile = managedDefaultDir.resolve(requestedResource);
  855. ContentItem item = managedDefaultRepository.toItem( requestedResource );
  856. if (item instanceof DataItem) {
  857. item = managedDefaultRepository.getParent( item );
  858. }
  859. assertNotNull( item );
  860. BaseRepositoryContentLayout layout = managedDefaultRepository.getLayout( BaseRepositoryContentLayout.class );
  861. Version version = layout.adaptItem( Version.class, item );
  862. assertNotNull( version );
  863. String metaPath = managedDefaultRepository.toPath( layout.getMetadataItem(
  864. version ) );
  865. StorageAsset downloadedFile = proxyHandler.fetchMetadataFromProxies( managedDefaultRepository.getRepository(),
  866. metaPath).getFile();
  867. assertNotNull( "Should have downloaded a file.", downloadedFile );
  868. assertNoTempFiles( expectedFile );
  869. }
  870. /**
  871. * Transfer the metadata file, not expected to succeed.
  872. *
  873. * @param requestedResource the requested resource
  874. * @throws Exception
  875. */
  876. private void assertFetchVersionedFailed( String requestedResource )
  877. throws Exception
  878. {
  879. Path expectedFile = managedDefaultDir.resolve(requestedResource);
  880. ContentItem item = managedDefaultRepository.toItem( requestedResource );
  881. assertNotNull( item );
  882. BaseRepositoryContentLayout layout = managedDefaultRepository.getLayout( BaseRepositoryContentLayout.class );
  883. Version version = layout.adaptItem( Version.class, item );
  884. assertNotNull( version );
  885. String metaPath = managedDefaultRepository.toPath( layout.getMetadataItem(
  886. version ) );
  887. assertNotNull( metaPath );
  888. StorageAsset downloadedFile = proxyHandler.fetchMetadataFromProxies( managedDefaultRepository.getRepository(),
  889. metaPath ).getFile();
  890. assertNull( downloadedFile );
  891. assertNoTempFiles( expectedFile );
  892. }
  893. /**
  894. * Test for the existance of the requestedResource in the default managed repository.
  895. *
  896. * @param requestedResource the requested resource
  897. * @throws Exception
  898. */
  899. private void assertResourceExists( String requestedResource )
  900. throws Exception
  901. {
  902. Path actualFile = managedDefaultDir.resolve(requestedResource);
  903. assertTrue( "Resource should exist: " + requestedResource, Files.exists(actualFile) );
  904. }
  905. private void assertMetadataEquals( String expectedMetadataXml, Path actualFile )
  906. throws Exception
  907. {
  908. assertNotNull( "Actual File should not be null.", actualFile );
  909. assertTrue( "Actual file exists.", Files.exists(actualFile) );
  910. StringWriter actualContents = new StringWriter();
  911. FilesystemStorage fsStorage = new FilesystemStorage(actualFile.getParent(), new DefaultFileLockManager());
  912. StorageAsset actualFileAsset = fsStorage.getAsset(actualFile.getFileName().toString());
  913. ArchivaRepositoryMetadata metadata = metadataTools.getMetadataReader( null ).read( actualFileAsset );
  914. RepositoryMetadataWriter.write( metadata, actualContents );
  915. Diff detailedDiff = DiffBuilder.compare( expectedMetadataXml).withTest( actualContents.toString() ).checkForSimilar().build();
  916. if ( detailedDiff.hasDifferences() )
  917. {
  918. for ( Difference diff : detailedDiff.getDifferences() )
  919. {
  920. System.out.println( diff );
  921. }
  922. assertEquals( expectedMetadataXml, actualContents );
  923. }
  924. // assertEquals( "Check file contents.", expectedMetadataXml, actualContents );
  925. }
  926. /**
  927. * Ensures that the requested resource is not present in the managed repository.
  928. *
  929. * @param requestedResource the requested resource
  930. * @throws Exception
  931. */
  932. private void assertNoMetadata( String requestedResource )
  933. throws Exception
  934. {
  935. Path expectedFile = managedDefaultDir.resolve(requestedResource);
  936. assertFalse( "metadata should not exist: " + expectedFile, Files.exists(expectedFile) );
  937. }
  938. /**
  939. * Ensures that the proxied repository specific maven metadata file does NOT exist in the
  940. * managed repository.
  941. *
  942. * @param proxiedRepoId the proxied repository id to validate with.
  943. * @param requestedResource the resource requested.
  944. */
  945. private void assertNoRepoMetadata( String proxiedRepoId, String requestedResource )
  946. {
  947. String proxiedFile = metadataTools.getRepositorySpecificName( proxiedRepoId, requestedResource );
  948. Path actualFile = managedDefaultDir.resolve(proxiedFile);
  949. assertFalse( "Repo specific metadata should not exist: " + actualFile, Files.exists(actualFile) );
  950. }
  951. private void assertGroupMetadataContents( String requestedResource, String expectedPlugins[] )
  952. throws Exception
  953. {
  954. Path actualFile = managedDefaultDir.resolve(requestedResource);
  955. assertTrue( "Snapshot Metadata should exist: " + requestedResource, Files.exists(actualFile) );
  956. ItemSelector actualMetadata = createGroupSelector( requestedResource );
  957. assertGroupMetadata( actualFile, actualMetadata, expectedPlugins );
  958. }
  959. private ItemSelector createProjectSelector(String path) throws RepositoryMetadataException
  960. {
  961. return metadataTools.toProjectSelector( path );
  962. }
  963. private ItemSelector createVersionedSelector(String path) throws RepositoryMetadataException
  964. {
  965. return metadataTools.toVersionedSelector( path );
  966. }
  967. private ItemSelector createGroupSelector( String requestedResource )
  968. throws RepositoryMetadataException
  969. {
  970. ItemSelector projectSelector = createProjectSelector( requestedResource );
  971. ArchivaItemSelector.Builder projectReference = ArchivaItemSelector.builder( ).withSelector( projectSelector );
  972. projectReference.withNamespace( projectSelector.getNamespace() + "." + projectSelector.getProjectId() );
  973. projectReference.withArtifactId( null );
  974. projectReference.withProjectId( null );
  975. return projectReference.build();
  976. }
  977. private void assertRepoGroupMetadataContents( String proxiedRepoId, String requestedResource,
  978. String expectedPlugins[] )
  979. throws Exception
  980. {
  981. String proxiedFile = metadataTools.getRepositorySpecificName( proxiedRepoId, requestedResource );
  982. Path actualFile = managedDefaultDir.resolve(proxiedFile);
  983. assertTrue( "Repo Specific Group Metadata should exist: " + requestedResource, Files.exists(actualFile) );
  984. ItemSelector actualMetadata = createGroupSelector( requestedResource );
  985. assertGroupMetadata( actualFile, actualMetadata, expectedPlugins );
  986. }
  987. private void assertGroupMetadata( Path actualFile, ItemSelector actualMetadata, String expectedPlugins[] )
  988. throws Exception
  989. {
  990. // Build expected metadata XML
  991. StringWriter expectedMetadataXml = new StringWriter();
  992. ArchivaRepositoryMetadata m = new ArchivaRepositoryMetadata();
  993. m.setGroupId( actualMetadata.getNamespace() );
  994. for ( String pluginId : expectedPlugins )
  995. {
  996. Plugin p = new Plugin();
  997. p.setPrefix( pluginId );
  998. p.setArtifactId( pluginId + "-maven-plugin" );
  999. p.setName( "The " + pluginId + " Plugin" );
  1000. m.getPlugins().add( p );
  1001. }
  1002. RepositoryMetadataWriter.write( m, expectedMetadataXml );
  1003. // Compare the file to the actual contents.
  1004. assertMetadataEquals( expectedMetadataXml.toString(), actualFile );
  1005. }
  1006. /**
  1007. * Test for the existance of the requestedResource in the default managed repository, and if it exists,
  1008. * does it contain the specified list of expected versions?
  1009. *
  1010. * @param requestedResource the requested resource
  1011. * @throws Exception
  1012. */
  1013. private void assertProjectMetadataContents( String requestedResource, String expectedVersions[],
  1014. String latestVersion, String releaseVersion )
  1015. throws Exception
  1016. {
  1017. Path actualFile = managedDefaultDir.resolve(requestedResource);
  1018. assertTrue( Files.exists(actualFile) );
  1019. ItemSelector metadata = createProjectSelector( requestedResource );
  1020. // Build expected metadata XML
  1021. StringWriter expectedMetadataXml = new StringWriter();
  1022. ArchivaRepositoryMetadata m = new ArchivaRepositoryMetadata();
  1023. m.setGroupId( metadata.getNamespace() );
  1024. m.setArtifactId( metadata.getArtifactId() );
  1025. m.setLatestVersion( latestVersion );
  1026. m.setReleasedVersion( releaseVersion );
  1027. if ( expectedVersions != null )
  1028. {
  1029. m.getAvailableVersions().addAll( Arrays.asList( expectedVersions ) );
  1030. }
  1031. RepositoryMetadataWriter.write( m, expectedMetadataXml );
  1032. // Compare the file to the actual contents.
  1033. assertMetadataEquals( expectedMetadataXml.toString(), actualFile );
  1034. }
  1035. /**
  1036. * Test for the existance of the requestedResource in the default managed repository, and if it exists,
  1037. * does it contain the expected release maven-metadata.xml contents?
  1038. *
  1039. * @param requestedResource the requested resource
  1040. * @throws Exception
  1041. */
  1042. private void assertReleaseMetadataContents( String requestedResource )
  1043. throws Exception
  1044. {
  1045. Path actualFile = managedDefaultDir.resolve(requestedResource);
  1046. assertTrue( "Release Metadata should exist: " + requestedResource, Files.exists(actualFile) );
  1047. ItemSelector metadata = createVersionedSelector( requestedResource );
  1048. // Build expected metadata XML
  1049. StringWriter expectedMetadataXml = new StringWriter();
  1050. ArchivaRepositoryMetadata m = new ArchivaRepositoryMetadata();
  1051. m.setGroupId( metadata.getNamespace() );
  1052. m.setArtifactId( metadata.getArtifactId() );
  1053. m.setVersion( metadata.getVersion() );
  1054. RepositoryMetadataWriter.write( m, expectedMetadataXml );
  1055. // Compare the file to the actual contents.
  1056. assertMetadataEquals( expectedMetadataXml.toString(), actualFile );
  1057. }
  1058. /**
  1059. * Test for the existance of the snapshot metadata in the default managed repository, and if it exists,
  1060. * does it contain the expected release maven-metadata.xml contents?
  1061. *
  1062. * @param requestedResource the requested resource
  1063. * @param expectedDate the date in "yyyyMMdd" format
  1064. * @param expectedTime the time in "hhmmss" format
  1065. * @param expectedBuildnumber the build number
  1066. * @throws Exception
  1067. */
  1068. private void assertSnapshotMetadataContents( String requestedResource, String expectedDate, String expectedTime,
  1069. int expectedBuildnumber )
  1070. throws Exception
  1071. {
  1072. Path actualFile = managedDefaultDir.resolve(requestedResource);
  1073. assertTrue( "Snapshot Metadata should exist: " + requestedResource, Files.exists(actualFile) );
  1074. ItemSelector actualMetadata = createVersionedSelector( requestedResource );
  1075. assertSnapshotMetadata( actualFile, actualMetadata, expectedDate, expectedTime, expectedBuildnumber );
  1076. }
  1077. /**
  1078. * Test for the existance of the proxied repository specific snapshot metadata in the default managed
  1079. * repository, and if it exists, does it contain the expected release maven-metadata.xml contents?
  1080. *
  1081. * @param proxiedRepoId the repository id of the proxied repository.
  1082. * @param requestedResource the requested resource
  1083. * @param expectedDate the date in "yyyyMMdd" format
  1084. * @param expectedTime the time in "hhmmss" format
  1085. * @param expectedBuildnumber the build number
  1086. * @throws Exception
  1087. */
  1088. private void assertRepoSnapshotMetadataContents( String proxiedRepoId, String requestedResource,
  1089. String expectedDate, String expectedTime, int expectedBuildnumber )
  1090. throws Exception
  1091. {
  1092. String proxiedFile = metadataTools.getRepositorySpecificName( proxiedRepoId, requestedResource );
  1093. Path actualFile = managedDefaultDir.resolve(proxiedFile);
  1094. assertTrue( "Repo Specific Snapshot Metadata should exist: " + requestedResource, Files.exists(actualFile) );
  1095. ItemSelector actualMetadata = createVersionedSelector( requestedResource );
  1096. assertSnapshotMetadata( actualFile, actualMetadata, expectedDate, expectedTime, expectedBuildnumber );
  1097. }
  1098. private void assertSnapshotMetadata( Path actualFile, ItemSelector actualMetadata, String expectedDate,
  1099. String expectedTime, int expectedBuildnumber )
  1100. throws RepositoryMetadataException, Exception
  1101. {
  1102. // Build expected metadata XML
  1103. StringWriter expectedMetadataXml = new StringWriter();
  1104. ArchivaRepositoryMetadata m = new ArchivaRepositoryMetadata();
  1105. m.setGroupId( actualMetadata.getNamespace() );
  1106. m.setArtifactId( actualMetadata.getArtifactId() );
  1107. m.setVersion( VersionUtil.getBaseVersion( actualMetadata.getVersion() ) );
  1108. m.setSnapshotVersion( new SnapshotVersion() );
  1109. if ( StringUtils.isNotBlank( expectedDate ) && StringUtils.isNotBlank( expectedTime ) )
  1110. {
  1111. m.getSnapshotVersion().setTimestamp( expectedDate + "." + expectedTime );
  1112. }
  1113. m.getSnapshotVersion().setBuildNumber( expectedBuildnumber );
  1114. m.setLastUpdated( expectedDate + expectedTime );
  1115. RepositoryMetadataWriter.write( m, expectedMetadataXml );
  1116. // Compare the file to the actual contents.
  1117. assertMetadataEquals( expectedMetadataXml.toString(), actualFile );
  1118. }
  1119. /**
  1120. * Ensures that the repository specific maven metadata file exists, and contains the appropriate
  1121. * list of expected versions within.
  1122. *
  1123. * @param proxiedRepoId
  1124. * @param requestedResource
  1125. * @param expectedProxyVersions
  1126. */
  1127. private void assertRepoProjectMetadata( String proxiedRepoId, String requestedResource,
  1128. String[] expectedProxyVersions )
  1129. throws Exception
  1130. {
  1131. String proxiedFile = metadataTools.getRepositorySpecificName( proxiedRepoId, requestedResource );
  1132. Path actualFile = managedDefaultDir.resolve(proxiedFile);
  1133. assertTrue( Files.exists(actualFile) );
  1134. ItemSelector metadata = createProjectSelector( requestedResource );
  1135. // Build expected metadata XML
  1136. StringWriter expectedMetadataXml = new StringWriter();
  1137. ArchivaRepositoryMetadata m = new ArchivaRepositoryMetadata();
  1138. m.setGroupId( metadata.getNamespace() );
  1139. m.setArtifactId( metadata.getArtifactId() );
  1140. if ( expectedProxyVersions != null )
  1141. {
  1142. m.getAvailableVersions().addAll( Arrays.asList( expectedProxyVersions ) );
  1143. }
  1144. RepositoryMetadataWriter.write( m, expectedMetadataXml );
  1145. // Compare the file to the actual contents.
  1146. assertMetadataEquals( expectedMetadataXml.toString(), actualFile );
  1147. }
  1148. /**
  1149. * Ensures that the repository specific maven metadata file exists, and contains the appropriate
  1150. * list of expected versions within.
  1151. *
  1152. * @param proxiedRepoId
  1153. * @param requestedResource
  1154. */
  1155. private void assertRepoReleaseMetadataContents( String proxiedRepoId, String requestedResource )
  1156. throws Exception
  1157. {
  1158. String proxiedFile = metadataTools.getRepositorySpecificName( proxiedRepoId, requestedResource );
  1159. Path actualFile = managedDefaultDir.resolve(proxiedFile);
  1160. assertTrue( "Release metadata for repo should exist: " + actualFile, Files.exists(actualFile) );
  1161. ItemSelector metadata = createVersionedSelector( requestedResource );
  1162. // Build expected metadata XML
  1163. StringWriter expectedMetadataXml = new StringWriter();
  1164. ArchivaRepositoryMetadata m = new ArchivaRepositoryMetadata();
  1165. m.setGroupId( metadata.getNamespace() );
  1166. m.setArtifactId( metadata.getArtifactId() );
  1167. m.setVersion( metadata.getVersion() );
  1168. RepositoryMetadataWriter.write( m, expectedMetadataXml );
  1169. // Compare the file to the actual contents.
  1170. assertMetadataEquals( expectedMetadataXml.toString(), actualFile );
  1171. }
  1172. /**
  1173. * Test for the non-existance of the requestedResource in the default managed repository.
  1174. *
  1175. * @param requestedResource the requested resource
  1176. * @throws Exception
  1177. */
  1178. private void assertResourceNotFound( String requestedResource )
  1179. throws Exception
  1180. {
  1181. Path actualFile = managedDefaultDir.resolve(requestedResource);
  1182. assertFalse( "Resource should not exist: " + requestedResource, Files.exists(actualFile) );
  1183. }
  1184. }