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 19KB

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