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.

RepositoryServletProxiedSnapshotPolicyTest.java 8.9KB

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