您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

AbstractRepositoryServletProxiedTestCase.java 9.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  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.WebClient;
  21. import org.apache.archiva.configuration.ProxyConnectorConfiguration;
  22. import org.apache.archiva.configuration.RemoteRepositoryConfiguration;
  23. import org.apache.archiva.policies.CachedFailuresPolicy;
  24. import org.apache.archiva.policies.ChecksumPolicy;
  25. import org.apache.archiva.policies.ReleasesPolicy;
  26. import org.apache.archiva.policies.SnapshotsPolicy;
  27. import org.apache.commons.io.FileUtils;
  28. import org.eclipse.jetty.server.Server;
  29. import org.eclipse.jetty.server.handler.ContextHandlerCollection;
  30. import org.eclipse.jetty.servlet.DefaultServlet;
  31. import org.eclipse.jetty.servlet.ServletContextHandler;
  32. import org.eclipse.jetty.servlet.ServletHolder;
  33. import org.junit.After;
  34. import org.junit.Before;
  35. import javax.servlet.http.HttpServletResponse;
  36. import java.io.File;
  37. import java.nio.charset.Charset;
  38. import static org.assertj.core.api.Assertions.assertThat;
  39. import org.junit.Rule;
  40. /**
  41. * AbstractRepositoryServletProxiedTestCase
  42. */
  43. public abstract class AbstractRepositoryServletProxiedTestCase
  44. extends AbstractRepositoryServletTestCase
  45. {
  46. class RemoteRepoInfo
  47. {
  48. public String id;
  49. public String url;
  50. public String context;
  51. public Server server;
  52. public File root;
  53. public RemoteRepositoryConfiguration config;
  54. }
  55. protected static final long ONE_SECOND = ( 1000 /* milliseconds */ );
  56. protected static final long ONE_MINUTE = ( ONE_SECOND * 60 );
  57. protected static final long ONE_HOUR = ( ONE_MINUTE * 60 );
  58. protected static final long ONE_DAY = ( ONE_HOUR * 24 );
  59. protected static final long OVER_ONE_HOUR = ( ONE_HOUR + ONE_MINUTE );
  60. protected static final long OVER_ONE_DAY = ( ONE_DAY + ONE_HOUR );
  61. protected static final long OLDER = ( -1 );
  62. protected static final long NEWER = 0;
  63. protected static final int EXPECT_MANAGED_CONTENTS = 1;
  64. protected static final int EXPECT_REMOTE_CONTENTS = 2;
  65. protected static final int EXPECT_NOT_FOUND = 3;
  66. protected static final boolean HAS_MANAGED_COPY = true;
  67. protected static final boolean NO_MANAGED_COPY = false;
  68. protected RemoteRepoInfo remoteCentral;
  69. protected RemoteRepoInfo remoteSnapshots;
  70. @Rule
  71. public ArchivaTemporaryFolderRule repoRootInternali = new ArchivaTemporaryFolderRule();
  72. @Before
  73. @Override
  74. public void setUp()
  75. throws Exception
  76. {
  77. super.setUp();
  78. startRepository();
  79. }
  80. @Override
  81. @After
  82. public void tearDown()
  83. throws Exception
  84. {
  85. shutdownServer( remoteCentral );
  86. shutdownServer( remoteSnapshots );
  87. super.tearDown();
  88. }
  89. protected RemoteRepoInfo createServer( String id )
  90. throws Exception
  91. {
  92. RemoteRepoInfo repo = new RemoteRepoInfo();
  93. repo.id = id;
  94. repo.context = "/" + id;
  95. repo.root = repoRootInternali.getRoot();/*Files.createTempDirectory(
  96. "temp" ).toFile();*/// new File( System.getProperty( "basedir" ) + "target/remote-repos/" + id + "/" );
  97. // Remove exising root contents.
  98. if ( repo.root.exists() )
  99. {
  100. FileUtils.deleteDirectory( repo.root );
  101. }
  102. // Establish root directory.
  103. if ( !repo.root.exists() )
  104. {
  105. repo.root.mkdirs();
  106. }
  107. repo.server = new Server( 0 );
  108. ContextHandlerCollection contexts = new ContextHandlerCollection();
  109. repo.server.setHandler( contexts );
  110. ServletContextHandler context = new ServletContextHandler();
  111. context.setContextPath( repo.context );
  112. context.setResourceBase( repo.root.getAbsolutePath() );
  113. context.setAttribute( "dirAllowed", true );
  114. context.setAttribute( "maxCacheSize", 0 );
  115. ServletHolder sh = new ServletHolder( DefaultServlet.class );
  116. context.addServlet( sh, "/" );
  117. contexts.addHandler( context );
  118. repo.server.start();
  119. int port = repo.server.getConnectors()[0].getLocalPort();
  120. repo.url = "http://localhost:" + port + repo.context;
  121. log.info( "Remote HTTP Server started on {}", repo.url );
  122. repo.config = createRemoteRepository( repo.id, "Testable [" + repo.id + "] Remote Repo", repo.url );
  123. return repo;
  124. }
  125. protected void assertServerSetupCorrectly( RemoteRepoInfo remoteRepo )
  126. throws Exception
  127. {
  128. WebClient client = newClient();
  129. int status = client.getPage( remoteRepo.url ).getWebResponse().getStatusCode();
  130. assertThat( status ).isEqualTo( HttpServletResponse.SC_OK );
  131. }
  132. private void setupConnector( String repoId, RemoteRepoInfo remoteRepo, String releasesPolicy,
  133. String snapshotsPolicy )
  134. {
  135. ProxyConnectorConfiguration connector = new ProxyConnectorConfiguration();
  136. connector.setSourceRepoId( repoId );
  137. connector.setTargetRepoId( remoteRepo.id );
  138. connector.addPolicy( ProxyConnectorConfiguration.POLICY_RELEASES, releasesPolicy );
  139. connector.addPolicy( ProxyConnectorConfiguration.POLICY_SNAPSHOTS, snapshotsPolicy );
  140. connector.addPolicy( ProxyConnectorConfiguration.POLICY_CHECKSUM, ChecksumPolicy.IGNORE );
  141. connector.addPolicy( ProxyConnectorConfiguration.POLICY_CACHE_FAILURES, CachedFailuresPolicy.NO );
  142. archivaConfiguration.getConfiguration().addProxyConnector( connector );
  143. }
  144. protected void shutdownServer( RemoteRepoInfo remoteRepo )
  145. {
  146. if ( remoteRepo != null )
  147. {
  148. if ( remoteRepo.server != null )
  149. {
  150. if ( remoteRepo.server.isRunning() )
  151. {
  152. try
  153. {
  154. remoteRepo.server.stop();
  155. // int graceful = remoteRepo.server.getGracefulShutdown();
  156. // System.out.println( "server set to graceful shutdown: " + graceful );
  157. // remoteRepo = null;
  158. }
  159. catch ( Exception e )
  160. {
  161. e.printStackTrace( System.err );
  162. }
  163. }
  164. }
  165. }
  166. }
  167. protected File populateRepo( RemoteRepoInfo remoteRepo, String path, String contents )
  168. throws Exception
  169. {
  170. File destFile = new File( remoteRepo.root, path );
  171. if ( destFile.exists() )
  172. {
  173. destFile.delete();
  174. }
  175. destFile.getParentFile().mkdirs();
  176. FileUtils.writeStringToFile( destFile, contents, Charset.defaultCharset() );
  177. return destFile;
  178. }
  179. protected void setupCentralRemoteRepo()
  180. throws Exception
  181. {
  182. remoteCentral = createServer( "central" );
  183. assertServerSetupCorrectly( remoteCentral );
  184. RemoteRepositoryConfiguration remoteRepositoryConfiguration =
  185. archivaConfiguration.getConfiguration().getRemoteRepositoriesAsMap().get( remoteCentral.id );
  186. if ( remoteRepositoryConfiguration != null )
  187. {
  188. archivaConfiguration.getConfiguration().removeRemoteRepository( remoteRepositoryConfiguration );
  189. }
  190. archivaConfiguration.getConfiguration().addRemoteRepository( remoteCentral.config );
  191. setupCleanRepo( remoteCentral.root );
  192. }
  193. protected void setupConnector( String repoId, RemoteRepoInfo remoteRepo )
  194. {
  195. setupConnector( repoId, remoteRepo, ReleasesPolicy.ALWAYS, SnapshotsPolicy.ALWAYS );
  196. }
  197. protected void setupReleaseConnector( String managedRepoId, RemoteRepoInfo remoteRepo, String releasePolicy )
  198. {
  199. setupConnector( managedRepoId, remoteRepo, releasePolicy, SnapshotsPolicy.ALWAYS );
  200. }
  201. protected void setupSnapshotConnector( String managedRepoId, RemoteRepoInfo remoteRepo, String snapshotsPolicy )
  202. {
  203. setupConnector( managedRepoId, remoteRepo, ReleasesPolicy.ALWAYS, snapshotsPolicy );
  204. }
  205. protected void setupSnapshotsRemoteRepo()
  206. throws Exception
  207. {
  208. remoteSnapshots = createServer( "snapshots" );
  209. assertServerSetupCorrectly( remoteSnapshots );
  210. RemoteRepositoryConfiguration remoteRepositoryConfiguration =
  211. archivaConfiguration.getConfiguration().getRemoteRepositoriesAsMap().get( remoteSnapshots.id );
  212. if ( remoteRepositoryConfiguration != null )
  213. {
  214. archivaConfiguration.getConfiguration().removeRemoteRepository( remoteRepositoryConfiguration );
  215. }
  216. archivaConfiguration.getConfiguration().addRemoteRepository( remoteSnapshots.config );
  217. setupCleanRepo( remoteSnapshots.root );
  218. }
  219. }