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

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