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.

ManagedDefaultTransferTest.java 20KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501
  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.policies.CachedFailuresPolicy;
  21. import org.apache.archiva.policies.ChecksumPolicy;
  22. import org.apache.archiva.policies.ReleasesPolicy;
  23. import org.apache.archiva.policies.SnapshotsPolicy;
  24. import org.apache.archiva.repository.content.BaseRepositoryContentLayout;
  25. import org.apache.archiva.repository.content.Artifact;
  26. import org.apache.archiva.repository.storage.StorageAsset;
  27. import org.apache.commons.io.FileUtils;
  28. import org.apache.commons.lang3.StringUtils;
  29. import org.apache.maven.wagon.ResourceDoesNotExistException;
  30. import org.easymock.EasyMock;
  31. import org.junit.Test;
  32. import java.io.File;
  33. import java.nio.charset.Charset;
  34. import java.nio.file.Files;
  35. import java.nio.file.Path;
  36. import java.nio.file.Paths;
  37. import java.nio.file.attribute.FileTime;
  38. import java.util.concurrent.TimeUnit;
  39. import static org.junit.Assert.*;
  40. /**
  41. * ManagedDefaultTransferTest
  42. */
  43. public class ManagedDefaultTransferTest
  44. extends AbstractProxyTestCase
  45. {
  46. @Test
  47. public void testGetDefaultLayoutNotPresentConnectorOffline()
  48. throws Exception
  49. {
  50. String path = "org/apache/maven/test/get-default-layout/1.0/get-default-layout-1.0.jar";
  51. setupTestableManagedRepository( path );
  52. Path expectedFile = managedDefaultDir.resolve(path);
  53. BaseRepositoryContentLayout layout = managedDefaultRepository.getLayout( BaseRepositoryContentLayout.class );
  54. Artifact artifact = layout.getArtifact( path );
  55. // Ensure file isn't present first.
  56. assertNotExistsInManagedDefaultRepo( expectedFile );
  57. // Configure Connector (usually done within archiva.xml configuration)
  58. saveConnector( ID_DEFAULT_MANAGED, ID_PROXIED1, ChecksumPolicy.FIX, ReleasesPolicy.ONCE, SnapshotsPolicy.ONCE,
  59. CachedFailuresPolicy.NO, true );
  60. // Attempt the proxy fetch.
  61. StorageAsset downloadedFile = proxyHandler.fetchFromProxies( managedDefaultRepository.getRepository(), artifact );
  62. assertNull( "File should not have been downloaded", downloadedFile );
  63. }
  64. @Test
  65. public void testGetDefaultLayoutNotPresent()
  66. throws Exception
  67. {
  68. String path = "org/apache/maven/test/get-default-layout/1.0/get-default-layout-1.0.jar";
  69. setupTestableManagedRepository( path );
  70. Path expectedFile = managedDefaultDir.resolve(path);
  71. BaseRepositoryContentLayout layout = managedDefaultRepository.getLayout( BaseRepositoryContentLayout.class );
  72. Artifact artifact = layout.getArtifact( path );
  73. // Ensure file isn't present first.
  74. assertNotExistsInManagedDefaultRepo( expectedFile );
  75. // Configure Connector (usually done within archiva.xml configuration)
  76. saveConnector( ID_DEFAULT_MANAGED, ID_PROXIED1, ChecksumPolicy.FIX, ReleasesPolicy.ONCE, SnapshotsPolicy.ONCE,
  77. CachedFailuresPolicy.NO, false );
  78. // Attempt the proxy fetch.
  79. StorageAsset downloadedFile = proxyHandler.fetchFromProxies( managedDefaultRepository.getRepository(), artifact );
  80. Path sourceFile = Paths.get(REPOPATH_PROXIED1, path);
  81. assertFileEquals( expectedFile, downloadedFile.getFilePath(), sourceFile );
  82. assertNoTempFiles( expectedFile );
  83. }
  84. @Test
  85. public void testGetDefaultLayoutNotPresentPassthrough()
  86. throws Exception
  87. {
  88. String path = "org/apache/maven/test/get-default-layout/1.0/get-default-layout-1.0.jar.asc";
  89. setupTestableManagedRepository( path );
  90. Path expectedFile = managedDefaultDir.resolve(path);
  91. // Ensure file isn't present first.
  92. assertNotExistsInManagedDefaultRepo( expectedFile );
  93. // Configure Connector (usually done within archiva.xml configuration)
  94. saveConnector( ID_DEFAULT_MANAGED, ID_PROXIED1, ChecksumPolicy.FIX, ReleasesPolicy.ONCE, SnapshotsPolicy.ONCE,
  95. CachedFailuresPolicy.NO, false );
  96. // Attempt the proxy fetch.
  97. StorageAsset downloadedFile = proxyHandler.fetchFromProxies( managedDefaultRepository.getRepository(), path );
  98. Path sourceFile = Paths.get(REPOPATH_PROXIED1, path);
  99. assertNotNull(downloadedFile);
  100. assertFileEquals( expectedFile, downloadedFile.getFilePath(), sourceFile );
  101. assertFalse( Files.exists( downloadedFile.getParent().getFilePath().resolve(downloadedFile.getName() + ".sha1" )) );
  102. assertFalse( Files.exists(downloadedFile.getParent().getFilePath().resolve(downloadedFile.getName() + ".md5" ) ));
  103. assertFalse( Files.exists( downloadedFile.getParent().getFilePath().resolve(downloadedFile.getName() + ".sha256" ) ));
  104. assertNoTempFiles( expectedFile );
  105. }
  106. /**
  107. * The attempt here should result in no file being transferred.
  108. * <p/>
  109. * The file exists locally, and the policy is ONCE.
  110. *
  111. * @throws Exception
  112. */
  113. @Test
  114. public void testGetDefaultLayoutAlreadyPresentPolicyOnce()
  115. throws Exception
  116. {
  117. String path = "org/apache/maven/test/get-default-layout-present/1.0/get-default-layout-present-1.0.jar";
  118. setupTestableManagedRepository( path );
  119. Path expectedFile = managedDefaultDir.resolve(path);
  120. BaseRepositoryContentLayout layout = managedDefaultRepository.getLayout( BaseRepositoryContentLayout.class );
  121. Artifact artifact = layout.getArtifact( path );
  122. assertTrue( Files.exists(expectedFile) );
  123. // Configure Connector (usually done within archiva.xml configuration)
  124. saveConnector( ID_DEFAULT_MANAGED, ID_PROXIED1, ChecksumPolicy.FIX, ReleasesPolicy.ONCE, SnapshotsPolicy.ONCE,
  125. CachedFailuresPolicy.NO, false );
  126. // Attempt the proxy fetch.
  127. StorageAsset downloadedFile = proxyHandler.fetchFromProxies( managedDefaultRepository.getRepository(), artifact );
  128. assertFileEquals( expectedFile, downloadedFile.getFilePath(), expectedFile );
  129. assertNoTempFiles( expectedFile );
  130. }
  131. /**
  132. * The attempt here should result in no file being transferred.
  133. * <p/>
  134. * The file exists locally, and the policy is ONCE.
  135. *
  136. * @throws Exception
  137. */
  138. @Test
  139. public void testGetDefaultLayoutAlreadyPresentPassthrough()
  140. throws Exception
  141. {
  142. String path = "org/apache/maven/test/get-default-layout-present/1.0/get-default-layout-present-1.0.jar.asc";
  143. setupTestableManagedRepository( path );
  144. Path expectedFile = managedDefaultDir.resolve(path);
  145. Path remoteFile = Paths.get(REPOPATH_PROXIED1, path);
  146. assertTrue( Files.exists(expectedFile) );
  147. // Set the managed File to be newer than local.
  148. setManagedOlderThanRemote( expectedFile, remoteFile );
  149. long originalModificationTime = Files.getLastModifiedTime(expectedFile).toMillis();
  150. // Configure Connector (usually done within archiva.xml configuration)
  151. saveConnector( ID_DEFAULT_MANAGED, ID_PROXIED1, ChecksumPolicy.FIX, ReleasesPolicy.ONCE, SnapshotsPolicy.ONCE,
  152. CachedFailuresPolicy.NO, false );
  153. // Attempt the proxy fetch.
  154. StorageAsset downloadedFile = proxyHandler.fetchFromProxies( managedDefaultRepository.getRepository(), path );
  155. assertNotDownloaded( downloadedFile );
  156. assertNotModified( expectedFile, originalModificationTime );
  157. assertNoTempFiles( expectedFile );
  158. }
  159. /**
  160. * <p>
  161. * Request a file, that exists locally, and remotely.
  162. * </p>
  163. * <p>
  164. * All policies are set to IGNORE.
  165. * </p>
  166. * <p>
  167. * Managed file is newer than remote file.
  168. * </p>
  169. * <p>
  170. * Transfer should not have occured, as managed file is newer.
  171. * </p>
  172. *
  173. * @throws Exception
  174. */
  175. @Test
  176. public void testGetDefaultLayoutAlreadyPresentNewerThanRemotePolicyIgnored()
  177. throws Exception
  178. {
  179. String path = "org/apache/maven/test/get-default-layout-present/1.0/get-default-layout-present-1.0.jar";
  180. setupTestableManagedRepository( path );
  181. Path expectedFile = managedDefaultDir.resolve(path);
  182. Path remoteFile = Paths.get(REPOPATH_PROXIED1, path);
  183. // Set the managed File to be newer than local.
  184. setManagedNewerThanRemote( expectedFile, remoteFile );
  185. long originalModificationTime = Files.getLastModifiedTime( expectedFile).toMillis();
  186. BaseRepositoryContentLayout layout = managedDefaultRepository.getLayout( BaseRepositoryContentLayout.class );
  187. Artifact artifact = layout.getArtifact( path );
  188. assertTrue( Files.exists(expectedFile) );
  189. // Configure Connector (usually done within archiva.xml configuration)
  190. saveConnector( ID_DEFAULT_MANAGED, ID_PROXIED1, ChecksumPolicy.FIX, ReleasesPolicy.ALWAYS,
  191. SnapshotsPolicy.ALWAYS, CachedFailuresPolicy.NO, false );
  192. // Attempt the proxy fetch.
  193. StorageAsset downloadedFile = proxyHandler.fetchFromProxies( managedDefaultRepository.getRepository(), artifact );
  194. assertNotDownloaded( downloadedFile );
  195. assertNotModified( expectedFile, originalModificationTime );
  196. assertNoTempFiles( expectedFile );
  197. }
  198. /**
  199. * <p>
  200. * Request a file, that exists locally, and remotely.
  201. * </p>
  202. * <p>
  203. * All policies are set to IGNORE.
  204. * </p>
  205. * <p>
  206. * Managed file is older than Remote file.
  207. * </p>
  208. * <p>
  209. * Transfer should have occured, as managed file is older than remote.
  210. * </p>
  211. *
  212. * @throws Exception
  213. */
  214. @Test
  215. public void testGetDefaultLayoutAlreadyPresentOlderThanRemotePolicyIgnored()
  216. throws Exception
  217. {
  218. String path = "org/apache/maven/test/get-default-layout-present/1.0/get-default-layout-present-1.0.jar";
  219. setupTestableManagedRepository( path );
  220. Path expectedFile = managedDefaultDir.resolve(path);
  221. Path remoteFile = Paths.get(REPOPATH_PROXIED1, path);
  222. // Set the managed file to be newer than remote file.
  223. setManagedOlderThanRemote( expectedFile, remoteFile );
  224. BaseRepositoryContentLayout layout = managedDefaultRepository.getLayout( BaseRepositoryContentLayout.class );
  225. Artifact artifact = layout.getArtifact( path );
  226. assertTrue( Files.exists(expectedFile) );
  227. // Configure Connector (usually done within archiva.xml configuration)
  228. saveConnector( ID_DEFAULT_MANAGED, ID_PROXIED1, ChecksumPolicy.FIX, ReleasesPolicy.ALWAYS,
  229. SnapshotsPolicy.ALWAYS, CachedFailuresPolicy.NO, false );
  230. // Attempt the proxy fetch.
  231. StorageAsset downloadedFile = proxyHandler.fetchFromProxies( managedDefaultRepository.getRepository(), artifact );
  232. Path proxiedFile = Paths.get(REPOPATH_PROXIED1, path);
  233. assertFileEquals( expectedFile, downloadedFile.getFilePath(), proxiedFile );
  234. assertNoTempFiles( expectedFile );
  235. }
  236. /**
  237. * The attempt here should result in file being transferred.
  238. * <p/>
  239. * The file exists locally, is over 6 years old, and the policy is DAILY.
  240. *
  241. * @throws Exception
  242. */
  243. @Test
  244. public void testGetDefaultLayoutRemoteUpdate()
  245. throws Exception
  246. {
  247. String path = "org/apache/maven/test/get-default-layout-present/1.0/get-default-layout-present-1.0.jar";
  248. setupTestableManagedRepository( path );
  249. Path expectedFile = managedDefaultDir.resolve(path);
  250. BaseRepositoryContentLayout layout = managedDefaultRepository.getLayout( BaseRepositoryContentLayout.class );
  251. Artifact artifact = layout.getArtifact( path );
  252. assertTrue( Files.exists(expectedFile) );
  253. Files.setLastModifiedTime( expectedFile, FileTime.from(getPastDate().getTime(), TimeUnit.MILLISECONDS ));
  254. // Configure Connector (usually done within archiva.xml configuration)
  255. saveConnector( ID_DEFAULT_MANAGED, ID_PROXIED1, ChecksumPolicy.FIX, ReleasesPolicy.DAILY, SnapshotsPolicy.DAILY,
  256. CachedFailuresPolicy.NO, false );
  257. // Attempt the proxy fetch.
  258. StorageAsset downloadedFile = proxyHandler.fetchFromProxies( managedDefaultRepository.getRepository(), artifact );
  259. Path proxiedFile = Paths.get(REPOPATH_PROXIED1, path);
  260. assertFileEquals( expectedFile, downloadedFile.getFilePath(), proxiedFile );
  261. assertNoTempFiles( expectedFile );
  262. }
  263. @Test
  264. public void testGetWhenInBothProxiedRepos()
  265. throws Exception
  266. {
  267. String path = "org/apache/maven/test/get-in-both-proxies/1.0/get-in-both-proxies-1.0.jar";
  268. setupTestableManagedRepository( path );
  269. Path expectedFile = managedDefaultDir.resolve(path);
  270. BaseRepositoryContentLayout layout = managedDefaultRepository.getLayout( BaseRepositoryContentLayout.class );
  271. Artifact artifact = layout.getArtifact( path );
  272. assertNotExistsInManagedDefaultRepo( expectedFile );
  273. // Configure Connector (usually done within archiva.xml configuration)
  274. saveConnector( ID_DEFAULT_MANAGED, ID_PROXIED1, false );
  275. saveConnector( ID_DEFAULT_MANAGED, ID_PROXIED2, false );
  276. // Attempt the proxy fetch.
  277. StorageAsset downloadedFile = proxyHandler.fetchFromProxies( managedDefaultRepository.getRepository(), artifact );
  278. Path proxied1File = Paths.get(REPOPATH_PROXIED1, path);
  279. Path proxied2File = Paths.get(REPOPATH_PROXIED2, path);
  280. assertFileEquals( expectedFile, downloadedFile.getFilePath(), proxied1File );
  281. assertNoTempFiles( expectedFile );
  282. // TODO: is this check even needed if it passes above?
  283. String actualContents = FileUtils.readFileToString( downloadedFile.getFilePath().toFile(), Charset.defaultCharset() );
  284. String badContents = FileUtils.readFileToString( proxied2File.toFile(), Charset.defaultCharset() );
  285. assertFalse( "Downloaded file contents should not be that of proxy 2",
  286. StringUtils.equals( actualContents, badContents ) );
  287. }
  288. @Test
  289. public void testGetInSecondProxiedRepo()
  290. throws Exception
  291. {
  292. String path = "org/apache/maven/test/get-in-second-proxy/1.0/get-in-second-proxy-1.0.jar";
  293. setupTestableManagedRepository( path );
  294. Path expectedFile = managedDefaultDir.resolve(path);
  295. BaseRepositoryContentLayout layout = managedDefaultRepository.getLayout( BaseRepositoryContentLayout.class );
  296. Artifact artifact = layout.getArtifact( path );
  297. assertNotExistsInManagedDefaultRepo( expectedFile );
  298. // Configure Connector (usually done within archiva.xml configuration)
  299. saveConnector( ID_DEFAULT_MANAGED, ID_PROXIED1, false );
  300. saveConnector( ID_DEFAULT_MANAGED, ID_PROXIED2, false );
  301. // Attempt the proxy fetch.
  302. StorageAsset downloadedFile = proxyHandler.fetchFromProxies( managedDefaultRepository.getRepository(), artifact );
  303. Path proxied2File = Paths.get(REPOPATH_PROXIED2, path);
  304. assertFileEquals( expectedFile, downloadedFile.getFilePath(), proxied2File );
  305. assertNoTempFiles( expectedFile );
  306. }
  307. @Test
  308. public void testNotFoundInAnyProxies()
  309. throws Exception
  310. {
  311. String path = "org/apache/maven/test/does-not-exist/1.0/does-not-exist-1.0.jar";
  312. setupTestableManagedRepository( path );
  313. Path expectedFile = managedDefaultDir.resolve(path);
  314. BaseRepositoryContentLayout layout = managedDefaultRepository.getLayout( BaseRepositoryContentLayout.class );
  315. Artifact artifact = layout.getArtifact( path );
  316. assertNotExistsInManagedDefaultRepo( expectedFile );
  317. // Configure Connector (usually done within archiva.xml configuration)
  318. saveConnector( ID_DEFAULT_MANAGED, ID_PROXIED1, false );
  319. saveConnector( ID_DEFAULT_MANAGED, ID_PROXIED2, false );
  320. // Attempt the proxy fetch.
  321. StorageAsset downloadedFile = proxyHandler.fetchFromProxies( managedDefaultRepository.getRepository(), artifact );
  322. assertNull( "File returned was: " + downloadedFile + "; should have got a not found exception",
  323. downloadedFile );
  324. assertNoTempFiles( expectedFile );
  325. }
  326. @Test
  327. public void testGetInSecondProxiedRepoFirstFails()
  328. throws Exception
  329. {
  330. String path = "org/apache/maven/test/get-in-second-proxy/1.0/get-in-second-proxy-1.0.jar";
  331. setupTestableManagedRepository( path );
  332. Path expectedFile = managedDefaultDir.resolve(path);
  333. BaseRepositoryContentLayout layout = managedDefaultRepository.getLayout( BaseRepositoryContentLayout.class );
  334. Artifact artifact = layout.getArtifact( path );
  335. assertNotExistsInManagedDefaultRepo( expectedFile );
  336. // Configure Repository (usually done within archiva.xml configuration)
  337. saveRemoteRepositoryConfig( "badproxied", "Bad Proxied", "" +
  338. "http://bad.machine.com/repo/", "default" );
  339. wagonMock.get( EasyMock.eq( path), EasyMock.anyObject( File.class ) );
  340. EasyMock.expectLastCall().andThrow( new ResourceDoesNotExistException( "transfer failed" ) );
  341. wagonMockControl.replay();
  342. // Configure Connector (usually done within archiva.xml configuration)
  343. saveConnector( ID_DEFAULT_MANAGED, "badproxied", false );
  344. saveConnector( ID_DEFAULT_MANAGED, ID_PROXIED2, false );
  345. // Attempt the proxy fetch.
  346. StorageAsset downloadedFile = proxyHandler.fetchFromProxies( managedDefaultRepository.getRepository(), artifact );
  347. wagonMockControl.verify();
  348. Path proxied2File = Paths.get(REPOPATH_PROXIED2, path);
  349. assertFileEquals( expectedFile, downloadedFile.getFilePath(), proxied2File );
  350. assertNoTempFiles( expectedFile );
  351. }
  352. @Test
  353. public void testGetAllRepositoriesFail()
  354. throws Exception
  355. {
  356. String path = "org/apache/maven/test/get-in-second-proxy/1.0/get-in-second-proxy-1.0.jar";
  357. setupTestableManagedRepository( path );
  358. Path expectedFile = managedDefaultDir.resolve( path );
  359. BaseRepositoryContentLayout layout = managedDefaultRepository.getLayout( BaseRepositoryContentLayout.class );
  360. Artifact artifact = layout.getArtifact( path );
  361. assertNotExistsInManagedDefaultRepo( expectedFile );
  362. // Configure Repository (usually done within archiva.xml configuration)
  363. saveRemoteRepositoryConfig( "badproxied1", "Bad Proxied 1", "http://bad.machine.com/repo/", "default" );
  364. saveRemoteRepositoryConfig( "badproxied2", "Bad Proxied 2", "http://dead.machine.com/repo/", "default" );
  365. // Configure Connector (usually done within archiva.xml configuration)
  366. saveConnector( ID_DEFAULT_MANAGED, "badproxied1", false );
  367. saveConnector( ID_DEFAULT_MANAGED, "badproxied2", false );
  368. Path tmpFile = expectedFile.getParent().resolve(expectedFile.getFileName() + ".tmp" );
  369. wagonMock.get( EasyMock.eq( path ), EasyMock.anyObject( File.class ) );
  370. EasyMock.expectLastCall().andThrow( new ResourceDoesNotExistException( "Can't find resource." ) );
  371. wagonMock.get( EasyMock.eq( path ), EasyMock.anyObject( File.class ) );
  372. EasyMock.expectLastCall().andThrow( new ResourceDoesNotExistException( "Can't find resource." ) );
  373. wagonMockControl.replay();
  374. StorageAsset downloadedFile = proxyHandler.fetchFromProxies( managedDefaultRepository.getRepository(), artifact );
  375. assertNotDownloaded( downloadedFile );
  376. wagonMockControl.verify();
  377. assertNoTempFiles( expectedFile );
  378. // TODO: do not want failures to present as a not found [MRM-492]
  379. // TODO: How much information on each failure should we pass back to the user vs. logging in the proxy?
  380. }
  381. }