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.1KB

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