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.

RepositoryServletProxiedReleasePolicyTest.java 8.2KB

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