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

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