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.

SnapshotTransferTest.java 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  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.junit.Test;
  26. import java.nio.file.Files;
  27. import java.nio.file.Path;
  28. import java.nio.file.Paths;
  29. import java.nio.file.attribute.FileTime;
  30. import java.util.concurrent.TimeUnit;
  31. import static org.junit.Assert.assertFalse;
  32. import static org.junit.Assert.assertTrue;
  33. /**
  34. * SnapshotTransferTest
  35. *
  36. *
  37. */
  38. public class SnapshotTransferTest
  39. extends AbstractProxyTestCase
  40. {
  41. @Test
  42. public void testSnapshotNonExistant()
  43. throws Exception
  44. {
  45. String path = "org/apache/maven/test/does-not-exist/1.0-SNAPSHOT/does-not-exist-1.0-SNAPSHOT.jar";
  46. setupTestableManagedRepository( path );
  47. Path expectedFile = managedDefaultDir.resolve(path);
  48. ArtifactReference artifact = managedDefaultRepository.toArtifactReference( path );
  49. Files.deleteIfExists(expectedFile);
  50. assertFalse( Files.exists(expectedFile) );
  51. // Configure Connector (usually done within archiva.xml configuration)
  52. saveConnector( ID_DEFAULT_MANAGED, ID_PROXIED1, false);
  53. Path downloadedFile = proxyHandler.fetchFromProxies( managedDefaultRepository, artifact );
  54. assertNotDownloaded( downloadedFile );
  55. assertNoTempFiles( expectedFile );
  56. }
  57. @Test
  58. public void testTimestampDrivenSnapshotNotPresentAlready()
  59. throws Exception
  60. {
  61. String path = "org/apache/maven/test/get-timestamped-snapshot/1.0-SNAPSHOT/get-timestamped-snapshot-1.0-SNAPSHOT.jar";
  62. setupTestableManagedRepository( path );
  63. Path expectedFile = managedDefaultDir.resolve(path);
  64. ArtifactReference artifact = managedDefaultRepository.toArtifactReference( path );
  65. Files.deleteIfExists(expectedFile);
  66. assertFalse( Files.exists(expectedFile) );
  67. // Configure Connector (usually done within archiva.xml configuration)
  68. saveConnector( ID_DEFAULT_MANAGED, ID_PROXIED1, false);
  69. Path downloadedFile = proxyHandler.fetchFromProxies( managedDefaultRepository, artifact );
  70. Path proxiedFile = Paths.get(REPOPATH_PROXIED1, path);
  71. assertFileEquals( expectedFile, downloadedFile, proxiedFile );
  72. assertNoTempFiles( expectedFile );
  73. }
  74. @Test
  75. public void testNewerTimestampDrivenSnapshotOnFirstRepo()
  76. throws Exception
  77. {
  78. String path = "org/apache/maven/test/get-present-timestamped-snapshot/1.0-SNAPSHOT/get-present-timestamped-snapshot-1.0-SNAPSHOT.jar";
  79. setupTestableManagedRepository( path );
  80. Path expectedFile = managedDefaultDir.resolve(path);
  81. ArtifactReference artifact = managedDefaultRepository.toArtifactReference( path );
  82. assertTrue( Files.exists(expectedFile) );
  83. Files.setLastModifiedTime( expectedFile, FileTime.from( getPastDate().getTime(), TimeUnit.MILLISECONDS ));
  84. // Configure Connector (usually done within archiva.xml configuration)
  85. saveConnector( ID_DEFAULT_MANAGED, ID_PROXIED1, false);
  86. Path downloadedFile = proxyHandler.fetchFromProxies( managedDefaultRepository, artifact );
  87. Path proxiedFile = Paths.get(REPOPATH_PROXIED1, path);
  88. assertFileEquals( expectedFile, downloadedFile, proxiedFile );
  89. assertNoTempFiles( expectedFile );
  90. }
  91. @Test
  92. public void testOlderTimestampDrivenSnapshotOnFirstRepo()
  93. throws Exception
  94. {
  95. String path = "org/apache/maven/test/get-present-timestamped-snapshot/1.0-SNAPSHOT/get-present-timestamped-snapshot-1.0-SNAPSHOT.jar";
  96. setupTestableManagedRepository( path );
  97. Path expectedFile = managedDefaultDir.resolve(path);
  98. Path remoteFile = Paths.get(REPOPATH_PROXIED1, path);
  99. setManagedNewerThanRemote( expectedFile, remoteFile );
  100. ArtifactReference artifact = managedDefaultRepository.toArtifactReference( path );
  101. // Configure Connector (usually done within archiva.xml configuration)
  102. saveConnector( ID_DEFAULT_MANAGED, ID_PROXIED1, false );
  103. // Attempt to download.
  104. Path downloadedFile = proxyHandler.fetchFromProxies( managedDefaultRepository, artifact );
  105. // Should not have downloaded as managed is newer than remote.
  106. assertNotDownloaded( downloadedFile );
  107. assertNoTempFiles( expectedFile );
  108. }
  109. /**
  110. * TODO: Has problems with wagon implementation not preserving timestamp.
  111. */
  112. /*
  113. public void testNewerTimestampDrivenSnapshotOnSecondRepoThanFirstNotPresentAlready()
  114. throws Exception
  115. {
  116. String path = "org/apache/maven/test/get-timestamped-snapshot-in-both/1.0-SNAPSHOT/get-timestamped-snapshot-in-both-1.0-SNAPSHOT.jar";
  117. setupTestableManagedRepository( path );
  118. Path expectedFile = managedDefaultDir.resolve(path);
  119. ArtifactReference artifact = createArtifactReference( "default", path );
  120. Files.delete(expectedFile);
  121. assertFalse( Files.exists(expectedFile) );
  122. // Create customized proxy / target repository
  123. File targetProxyDir = saveTargetedRepositoryConfig( ID_PROXIED1_TARGET, REPOPATH_PROXIED1,
  124. REPOPATH_PROXIED1_TARGET, "default" );
  125. new File( targetProxyDir, path ).setLastModified( getPastDate().getTime() );
  126. // Configure Connector (usually done within archiva.xml configuration)
  127. saveConnector( ID_DEFAULT_MANAGED, ID_PROXIED1_TARGET, ChecksumPolicy.IGNORED, ReleasesPolicy.IGNORED,
  128. SnapshotsPolicy.IGNORED, CachedFailuresPolicy.IGNORED );
  129. saveConnector( ID_DEFAULT_MANAGED, ID_PROXIED2, ChecksumPolicy.IGNORED, ReleasesPolicy.IGNORED,
  130. SnapshotsPolicy.IGNORED, CachedFailuresPolicy.IGNORED );
  131. File downloadedFile = proxyHandler.fetchFromProxies( managedDefaultRepository, artifact );
  132. // Should have downloaded the content from proxy2, as proxy1 has an old (by file.lastModified check) version.
  133. Path proxiedFile = Paths.get(REPOPATH_PROXIED2, path);
  134. assertFileEquals( expectedFile, downloadedFile, proxiedFile );
  135. assertNoTempFiles( expectedFile );
  136. }
  137. public void testOlderTimestampDrivenSnapshotOnSecondRepoThanFirstNotPresentAlready()
  138. throws Exception
  139. {
  140. String path = "org/apache/maven/test/get-timestamped-snapshot-in-both/1.0-SNAPSHOT/get-timestamped-snapshot-in-both-1.0-SNAPSHOT.jar";
  141. setupTestableManagedRepository( path );
  142. Path expectedFile = managedDefaultDir.resolve(path);
  143. ArtifactReference artifact = createArtifactReference( "default", path );
  144. Files.delete(expectedFile);
  145. assertFalse( Files.exists(expectedFile) );
  146. // Create customized proxy / target repository
  147. File targetProxyDir = saveTargetedRepositoryConfig( ID_PROXIED2_TARGET, REPOPATH_PROXIED2,
  148. REPOPATH_PROXIED2_TARGET, "default" );
  149. new File( targetProxyDir, path ).setLastModified( getPastDate().getTime() );
  150. // Configure Connector (usually done within archiva.xml configuration)
  151. saveConnector( ID_DEFAULT_MANAGED, ID_PROXIED1, ChecksumPolicy.IGNORED, ReleasesPolicy.IGNORED,
  152. SnapshotsPolicy.IGNORED, CachedFailuresPolicy.IGNORED );
  153. saveConnector( ID_DEFAULT_MANAGED, ID_PROXIED2_TARGET, ChecksumPolicy.IGNORED, ReleasesPolicy.IGNORED,
  154. SnapshotsPolicy.IGNORED, CachedFailuresPolicy.IGNORED );
  155. File downloadedFile = proxyHandler.fetchFromProxies( managedDefaultRepository, artifact );
  156. File proxiedFile = new File( REPOPATH_PROXIED1_TARGET, path );
  157. assertFileEquals( expectedFile, downloadedFile, proxiedFile );
  158. assertNoTempFiles( expectedFile );
  159. } */
  160. @Test
  161. public void testTimestampDrivenSnapshotNotExpired()
  162. throws Exception
  163. {
  164. String path = "org/apache/maven/test/get-present-timestamped-snapshot/1.0-SNAPSHOT/get-present-timestamped-snapshot-1.0-SNAPSHOT.jar";
  165. setupTestableManagedRepository( path );
  166. Path expectedFile = managedDefaultDir.resolve(path);
  167. ArtifactReference artifact = managedDefaultRepository.toArtifactReference( path );
  168. assertTrue( Files.exists(expectedFile) );
  169. Path proxiedFile = Paths.get(REPOPATH_PROXIED1, path);
  170. Files.setLastModifiedTime( proxiedFile, FileTime.from( getFutureDate().getTime(), TimeUnit.MILLISECONDS ));
  171. // Configure Connector (usually done within archiva.xml configuration)
  172. saveConnector( ID_DEFAULT_MANAGED, ID_PROXIED1, false);
  173. Path downloadedFile = proxyHandler.fetchFromProxies( managedDefaultRepository, artifact );
  174. assertFileEquals( expectedFile, downloadedFile, proxiedFile );
  175. assertNoTempFiles( expectedFile );
  176. }
  177. @Test
  178. public void testTimestampDrivenSnapshotNotUpdated()
  179. throws Exception
  180. {
  181. String path = "org/apache/maven/test/get-present-timestamped-snapshot/1.0-SNAPSHOT/get-present-timestamped-snapshot-1.0-SNAPSHOT.jar";
  182. setupTestableManagedRepository( path );
  183. Path expectedFile = managedDefaultDir.resolve(path);
  184. Path remoteFile = Paths.get(REPOPATH_PROXIED1, path);
  185. setManagedNewerThanRemote( expectedFile, remoteFile, 12000000 );
  186. long expectedTimestamp = Files.getLastModifiedTime( expectedFile ).toMillis();
  187. ArtifactReference artifact = managedDefaultRepository.toArtifactReference( path );
  188. // Configure Connector (usually done within archiva.xml configuration)
  189. saveConnector( ID_DEFAULT_MANAGED, ID_PROXIED1, false);
  190. Path downloadedFile = proxyHandler.fetchFromProxies( managedDefaultRepository, artifact );
  191. assertNotDownloaded( downloadedFile );
  192. assertNotModified( expectedFile, expectedTimestamp );
  193. assertNoTempFiles( expectedFile );
  194. }
  195. @Test
  196. public void testTimestampDrivenSnapshotNotPresentAlreadyExpiredCacheFailure()
  197. throws Exception
  198. {
  199. String path = "org/apache/maven/test/get-timestamped-snapshot/1.0-SNAPSHOT/get-timestamped-snapshot-1.0-SNAPSHOT.jar";
  200. setupTestableManagedRepository( path );
  201. Path expectedFile = managedDefaultDir.resolve(path);
  202. ArtifactReference artifact = managedDefaultRepository.toArtifactReference( path );
  203. Files.deleteIfExists(expectedFile);
  204. assertFalse( Files.exists(expectedFile) );
  205. // Configure Connector (usually done within archiva.xml configuration)
  206. saveConnector( ID_DEFAULT_MANAGED, ID_PROXIED1, ChecksumPolicy.IGNORE, ReleasesPolicy.ALWAYS,
  207. SnapshotsPolicy.ALWAYS, CachedFailuresPolicy.YES , false);
  208. saveConnector( ID_DEFAULT_MANAGED, ID_PROXIED2, ChecksumPolicy.IGNORE, ReleasesPolicy.ALWAYS,
  209. SnapshotsPolicy.ALWAYS, CachedFailuresPolicy.YES , false);
  210. Path downloadedFile = proxyHandler.fetchFromProxies( managedDefaultRepository, artifact );
  211. Path proxiedFile = Paths.get(REPOPATH_PROXIED1, path);
  212. assertFileEquals( expectedFile, downloadedFile, proxiedFile );
  213. assertNoTempFiles( expectedFile );
  214. }
  215. @Test
  216. public void testMetadataDrivenSnapshotNotPresentAlready()
  217. throws Exception
  218. {
  219. String path = "org/apache/maven/test/get-metadata-snapshot/1.0-SNAPSHOT/get-metadata-snapshot-1.0-20050831.101112-1.jar";
  220. setupTestableManagedRepository( path );
  221. Path expectedFile = managedDefaultDir.resolve(path);
  222. ArtifactReference artifact = managedDefaultRepository.toArtifactReference( path );
  223. Files.deleteIfExists(expectedFile);
  224. assertFalse( Files.exists(expectedFile) );
  225. // Configure Connector (usually done within archiva.xml configuration)
  226. saveConnector( ID_DEFAULT_MANAGED, ID_PROXIED1, false);
  227. Path downloadedFile = proxyHandler.fetchFromProxies( managedDefaultRepository, artifact );
  228. Path proxiedFile = Paths.get(REPOPATH_PROXIED1, path);
  229. assertFileEquals( expectedFile, downloadedFile, proxiedFile );
  230. assertNoTempFiles( expectedFile );
  231. }
  232. @Test
  233. public void testGetMetadataDrivenSnapshotRemoteUpdate()
  234. throws Exception
  235. {
  236. // Metadata driven snapshots (using a full timestamp) are treated like a release. It is the timing of the
  237. // updates to the metadata files that triggers which will be downloaded
  238. String path = "org/apache/maven/test/get-present-metadata-snapshot/1.0-SNAPSHOT/get-present-metadata-snapshot-1.0-20050831.101112-1.jar";
  239. setupTestableManagedRepository( path );
  240. Path expectedFile = managedDefaultDir.resolve(path);
  241. ArtifactReference artifact = managedDefaultRepository.toArtifactReference( path );
  242. assertTrue( Files.exists(expectedFile) );
  243. Files.setLastModifiedTime( expectedFile, FileTime.from( getPastDate().getTime(), TimeUnit.MILLISECONDS ));
  244. // Configure Connector (usually done within archiva.xml configuration)
  245. saveConnector( ID_DEFAULT_MANAGED, ID_PROXIED1, false);
  246. Path downloadedFile = proxyHandler.fetchFromProxies( managedDefaultRepository, artifact );
  247. Path proxiedFile = Paths.get(REPOPATH_PROXIED1, path);
  248. assertFileEquals( expectedFile, downloadedFile, proxiedFile );
  249. assertNoTempFiles( expectedFile );
  250. }
  251. }