Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

MetadataTransferTest.java 65KB

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