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

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