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.

RepositoryServletProxiedPluginSnapshotPolicyTest.java 8.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. package org.apache.archiva.webdav;
  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 com.gargoylesoftware.htmlunit.WebRequest;
  21. import com.gargoylesoftware.htmlunit.WebResponse;
  22. import org.apache.archiva.configuration.ProxyConnectorConfiguration;
  23. import org.apache.archiva.policies.SnapshotsPolicy;
  24. import org.junit.Test;
  25. import java.nio.file.Files;
  26. import java.nio.file.Path;
  27. import java.nio.file.attribute.FileTime;
  28. import java.util.ArrayList;
  29. /**
  30. * RepositoryServlet Tests, Proxied, Get of Timestamped Snapshot Artifacts, with varying policy settings.
  31. *
  32. *
  33. */
  34. public class RepositoryServletProxiedPluginSnapshotPolicyTest
  35. extends AbstractRepositoryServletProxiedTestCase
  36. {
  37. @Test
  38. public void testGetProxiedSnapshotsArtifactPolicyAlwaysManagedNewer()
  39. throws Exception
  40. {
  41. assertGetProxiedSnapshotsArtifactWithPolicy( EXPECT_MANAGED_CONTENTS, SnapshotsPolicy.ALWAYS,
  42. HAS_MANAGED_COPY, ( NEWER * OVER_ONE_DAY ) );
  43. }
  44. @Test
  45. public void testGetProxiedSnapshotsArtifactPolicyAlwaysManagedOlder()
  46. throws Exception
  47. {
  48. assertGetProxiedSnapshotsArtifactWithPolicy( EXPECT_REMOTE_CONTENTS, SnapshotsPolicy.ALWAYS, HAS_MANAGED_COPY,
  49. ( OLDER * OVER_ONE_DAY ) );
  50. }
  51. @Test
  52. public void testGetProxiedSnapshotsArtifactPolicyAlwaysNoManagedContent()
  53. throws Exception
  54. {
  55. assertGetProxiedSnapshotsArtifactWithPolicy( EXPECT_REMOTE_CONTENTS, SnapshotsPolicy.ALWAYS, NO_MANAGED_COPY );
  56. }
  57. @Test
  58. public void testGetProxiedSnapshotsArtifactPolicyDailyFail()
  59. throws Exception
  60. {
  61. assertGetProxiedSnapshotsArtifactWithPolicy( EXPECT_MANAGED_CONTENTS, SnapshotsPolicy.DAILY, HAS_MANAGED_COPY,
  62. ( NEWER * ONE_MINUTE ) );
  63. }
  64. @Test
  65. public void testGetProxiedSnapshotsArtifactPolicyDailyNoManagedContent()
  66. throws Exception
  67. {
  68. assertGetProxiedSnapshotsArtifactWithPolicy( EXPECT_REMOTE_CONTENTS, SnapshotsPolicy.DAILY, NO_MANAGED_COPY );
  69. }
  70. @Test
  71. public void testGetProxiedSnapshotsArtifactPolicyDailyPass()
  72. throws Exception
  73. {
  74. assertGetProxiedSnapshotsArtifactWithPolicy( EXPECT_REMOTE_CONTENTS, SnapshotsPolicy.DAILY, HAS_MANAGED_COPY,
  75. ( OLDER * OVER_ONE_DAY ) );
  76. }
  77. @Test
  78. public void testGetProxiedSnapshotsArtifactPolicyRejectFail()
  79. throws Exception
  80. {
  81. assertGetProxiedSnapshotsArtifactWithPolicy( EXPECT_MANAGED_CONTENTS, SnapshotsPolicy.NEVER,
  82. HAS_MANAGED_COPY );
  83. }
  84. @Test
  85. public void testGetProxiedSnapshotsArtifactPolicyRejectNoManagedContentFail()
  86. throws Exception
  87. {
  88. assertGetProxiedSnapshotsArtifactWithPolicy( EXPECT_NOT_FOUND, SnapshotsPolicy.NEVER, NO_MANAGED_COPY );
  89. }
  90. @Test
  91. public void testGetProxiedSnapshotsArtifactPolicyRejectPass()
  92. throws Exception
  93. {
  94. assertGetProxiedSnapshotsArtifactWithPolicy( EXPECT_MANAGED_CONTENTS, SnapshotsPolicy.NEVER,
  95. HAS_MANAGED_COPY );
  96. }
  97. @Test
  98. public void testGetProxiedSnapshotsArtifactPolicyHourlyFail()
  99. throws Exception
  100. {
  101. assertGetProxiedSnapshotsArtifactWithPolicy( EXPECT_MANAGED_CONTENTS, SnapshotsPolicy.HOURLY, HAS_MANAGED_COPY,
  102. ( NEWER * ONE_MINUTE ) );
  103. }
  104. @Test
  105. public void testGetProxiedSnapshotsArtifactPolicyHourlyNoManagedContent()
  106. throws Exception
  107. {
  108. assertGetProxiedSnapshotsArtifactWithPolicy( EXPECT_REMOTE_CONTENTS, SnapshotsPolicy.HOURLY, NO_MANAGED_COPY );
  109. }
  110. @Test
  111. public void testGetProxiedSnapshotsArtifactPolicyHourlyPass()
  112. throws Exception
  113. {
  114. assertGetProxiedSnapshotsArtifactWithPolicy( EXPECT_REMOTE_CONTENTS, SnapshotsPolicy.HOURLY, HAS_MANAGED_COPY,
  115. ( OLDER * OVER_ONE_HOUR ) );
  116. }
  117. @Test
  118. public void testGetProxiedSnapshotsArtifactPolicyOnceFail()
  119. throws Exception
  120. {
  121. assertGetProxiedSnapshotsArtifactWithPolicy( EXPECT_MANAGED_CONTENTS, SnapshotsPolicy.ONCE, HAS_MANAGED_COPY );
  122. }
  123. @Test
  124. public void testGetProxiedSnapshotsArtifactPolicyOnceNoManagedContent()
  125. throws Exception
  126. {
  127. assertGetProxiedSnapshotsArtifactWithPolicy( EXPECT_REMOTE_CONTENTS, SnapshotsPolicy.ONCE, NO_MANAGED_COPY );
  128. }
  129. @Test
  130. public void testGetProxiedSnapshotsArtifactPolicyOncePass()
  131. throws Exception
  132. {
  133. assertGetProxiedSnapshotsArtifactWithPolicy( EXPECT_REMOTE_CONTENTS, SnapshotsPolicy.ONCE, NO_MANAGED_COPY );
  134. }
  135. private void assertGetProxiedSnapshotsArtifactWithPolicy( int expectation, String snapshotsPolicy,
  136. boolean hasManagedCopy )
  137. throws Exception
  138. {
  139. assertGetProxiedSnapshotsArtifactWithPolicy( expectation, snapshotsPolicy, hasManagedCopy, 0 );
  140. }
  141. private void assertGetProxiedSnapshotsArtifactWithPolicy( int expectation, String snapshotsPolicy,
  142. boolean hasManagedCopy, long deltaManagedToRemoteTimestamp )
  143. throws Exception
  144. {
  145. // --- Setup
  146. setupSnapshotsRemoteRepo();
  147. setupCleanInternalRepo();
  148. String resourcePath = "org/apache/archiva/archivatest-maven-plugin/4.0-alpha-1-SNAPSHOT/archivatest-maven-plugin-4.0-alpha-1-20070822.033400-42.jar";
  149. String expectedRemoteContents = "archivatest-maven-plugin-4.0-alpha-1-20070822.033400-42|jar-remote-contents";
  150. String expectedManagedContents = null;
  151. Path remoteFile = populateRepo( remoteSnapshots, resourcePath, expectedRemoteContents );
  152. if ( hasManagedCopy )
  153. {
  154. expectedManagedContents = "archivatest-maven-plugin-4.0-alpha-1-20070822.033400-42|jar-managed-contents";
  155. Path managedFile = populateRepo( repoRootInternal, resourcePath, expectedManagedContents );
  156. Files.setLastModifiedTime( managedFile, FileTime.fromMillis( Files.getLastModifiedTime( remoteFile).toMillis() + deltaManagedToRemoteTimestamp ));
  157. }
  158. archivaConfiguration.getConfiguration().setProxyConnectors( new ArrayList<ProxyConnectorConfiguration>( 0 ) );
  159. setupSnapshotConnector( REPOID_INTERNAL, remoteSnapshots, snapshotsPolicy );
  160. saveConfiguration();
  161. // --- Execution
  162. // process the response code later, not via an exception.
  163. //HttpUnitOptions.setExceptionsThrownOnErrorStatus( false );
  164. WebRequest request = new GetMethodWebRequest( "http://machine.com/repository/internal/" + resourcePath );
  165. WebResponse response = getServletUnitClient().getResponse( request );
  166. // --- Verification
  167. switch ( expectation )
  168. {
  169. case EXPECT_MANAGED_CONTENTS:
  170. assertResponseOK( response );
  171. assertTrue( "Invalid Test Case: Can't expect managed contents with "
  172. + "test that doesn't have a managed copy in the first place.", hasManagedCopy );
  173. assertEquals( "Expected managed file contents", expectedManagedContents, response.getContentAsString() );
  174. break;
  175. case EXPECT_REMOTE_CONTENTS:
  176. assertResponseOK( response );
  177. assertEquals( "Expected remote file contents", expectedRemoteContents, response.getContentAsString() );
  178. break;
  179. case EXPECT_NOT_FOUND:
  180. assertResponseNotFound( response );
  181. assertManagedFileNotExists( repoRootInternal, resourcePath );
  182. break;
  183. }
  184. }
  185. }