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.

AbstractRepositoryServletTestCase.java 8.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. package org.apache.maven.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.meterware.httpunit.WebResponse;
  21. import com.meterware.httpunit.HttpUnitOptions;
  22. import com.meterware.servletunit.ServletRunner;
  23. import com.meterware.servletunit.ServletUnitClient;
  24. import net.sf.ehcache.CacheManager;
  25. import org.apache.commons.io.FileUtils;
  26. import org.apache.maven.archiva.configuration.ArchivaConfiguration;
  27. import org.apache.maven.archiva.configuration.Configuration;
  28. import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration;
  29. import org.apache.maven.archiva.configuration.RemoteRepositoryConfiguration;
  30. import org.apache.maven.archiva.webdav.RepositoryServlet;
  31. import org.codehaus.plexus.spring.PlexusInSpringTestCase;
  32. import javax.servlet.http.HttpServletResponse;
  33. import java.io.File;
  34. import java.io.IOException;
  35. import junit.framework.Assert;
  36. /**
  37. * AbstractRepositoryServletTestCase
  38. *
  39. * @author <a href="mailto:joakime@apache.org">Joakim Erdfelt</a>
  40. * @version $Id$
  41. */
  42. public abstract class AbstractRepositoryServletTestCase
  43. extends PlexusInSpringTestCase
  44. {
  45. protected static final String REPOID_INTERNAL = "internal";
  46. protected ServletUnitClient sc;
  47. protected File repoRootInternal;
  48. private ServletRunner sr;
  49. protected ArchivaConfiguration archivaConfiguration;
  50. protected void saveConfiguration()
  51. throws Exception
  52. {
  53. saveConfiguration( archivaConfiguration );
  54. }
  55. protected void assertFileContents( String expectedContents, File repoRoot, String path )
  56. throws IOException
  57. {
  58. File actualFile = new File( repoRoot, path );
  59. assertTrue( "File <" + actualFile.getAbsolutePath() + "> should exist.", actualFile.exists() );
  60. assertTrue( "File <" + actualFile.getAbsolutePath() + "> should be a file (not a dir/link/device/etc).",
  61. actualFile.isFile() );
  62. String actualContents = FileUtils.readFileToString( actualFile, null );
  63. assertEquals( "File Contents of <" + actualFile.getAbsolutePath() + ">", expectedContents, actualContents );
  64. }
  65. protected void assertRepositoryValid( RepositoryServlet servlet, String repoId )
  66. {
  67. ManagedRepositoryConfiguration repository = servlet.getRepository( repoId );
  68. assertNotNull( "Archiva Managed Repository id:<" + repoId + "> should exist.", repository );
  69. File repoRoot = new File( repository.getLocation() );
  70. assertTrue( "Archiva Managed Repository id:<" + repoId + "> should have a valid location on disk.", repoRoot
  71. .exists()
  72. && repoRoot.isDirectory() );
  73. }
  74. protected void assertResponseOK( WebResponse response )
  75. {
  76. assertNotNull( "Should have recieved a response", response );
  77. Assert.assertEquals( "Should have been an OK response code.", HttpServletResponse.SC_OK, response.getResponseCode() );
  78. }
  79. protected void assertResponseNotFound( WebResponse response )
  80. {
  81. assertNotNull( "Should have recieved a response", response );
  82. Assert.assertEquals( "Should have been an 404/Not Found response code.", HttpServletResponse.SC_NOT_FOUND, response
  83. .getResponseCode() );
  84. }
  85. protected ManagedRepositoryConfiguration createManagedRepository( String id, String name, File location )
  86. {
  87. ManagedRepositoryConfiguration repo = new ManagedRepositoryConfiguration();
  88. repo.setId( id );
  89. repo.setName( name );
  90. repo.setLocation( location.getAbsolutePath() );
  91. return repo;
  92. }
  93. protected RemoteRepositoryConfiguration createRemoteRepository( String id, String name, String url )
  94. {
  95. RemoteRepositoryConfiguration repo = new RemoteRepositoryConfiguration();
  96. repo.setId( id );
  97. repo.setName( name );
  98. repo.setUrl( url );
  99. return repo;
  100. }
  101. protected void dumpResponse( WebResponse response )
  102. {
  103. System.out.println( "---(response)---" );
  104. System.out.println( "" + response.getResponseCode() + " " + response.getResponseMessage() );
  105. String headerNames[] = response.getHeaderFieldNames();
  106. for ( String headerName : headerNames )
  107. {
  108. System.out.println( "[header] " + headerName + ": " + response.getHeaderField( headerName ) );
  109. }
  110. System.out.println( "---(text)---" );
  111. try
  112. {
  113. System.out.println( response.getText() );
  114. }
  115. catch ( IOException e )
  116. {
  117. System.err.print( "[Exception] : " );
  118. e.printStackTrace( System.err );
  119. }
  120. }
  121. protected void saveConfiguration( ArchivaConfiguration archivaConfiguration )
  122. throws Exception
  123. {
  124. archivaConfiguration.save( archivaConfiguration.getConfiguration() );
  125. }
  126. protected void setUp()
  127. throws Exception
  128. {
  129. super.setUp();
  130. String appserverBase = getTestFile( "target/appserver-base" ).getAbsolutePath();
  131. System.setProperty( "appserver.base", appserverBase );
  132. File testConf = getTestFile( "src/test/resources/repository-archiva.xml" );
  133. File testConfDest = new File( appserverBase, "conf/archiva.xml" );
  134. FileUtils.copyFile( testConf, testConfDest );
  135. archivaConfiguration = (ArchivaConfiguration) lookup( ArchivaConfiguration.class );
  136. repoRootInternal = new File( appserverBase, "data/repositories/internal" );
  137. Configuration config = archivaConfiguration.getConfiguration();
  138. config.addManagedRepository( createManagedRepository( REPOID_INTERNAL, "Internal Test Repo", repoRootInternal ) );
  139. saveConfiguration( archivaConfiguration );
  140. CacheManager.getInstance().removeCache( "url-failures-cache" );
  141. HttpUnitOptions.setExceptionsThrownOnErrorStatus( false );
  142. sr = new ServletRunner( getTestFile( "src/test/resources/WEB-INF/web.xml" ) );
  143. sr.registerServlet( "/repository/*", UnauthenticatedRepositoryServlet.class.getName() );
  144. sc = sr.newClient();
  145. }
  146. @Override
  147. protected String getPlexusConfigLocation()
  148. {
  149. return "org/apache/maven/archiva/webdav/RepositoryServletTest.xml";
  150. }
  151. @Override
  152. protected void tearDown()
  153. throws Exception
  154. {
  155. if ( sc != null )
  156. {
  157. sc.clearContents();
  158. }
  159. if ( sr != null )
  160. {
  161. sr.shutDown();
  162. }
  163. if (repoRootInternal.exists())
  164. {
  165. FileUtils.deleteDirectory(repoRootInternal);
  166. }
  167. super.tearDown();
  168. }
  169. protected void setupCleanRepo( File repoRootDir )
  170. throws IOException
  171. {
  172. FileUtils.deleteDirectory( repoRootDir );
  173. if ( !repoRootDir.exists() )
  174. {
  175. repoRootDir.mkdirs();
  176. }
  177. }
  178. protected void assertManagedFileNotExists( File repoRootInternal, String resourcePath )
  179. {
  180. File repoFile = new File( repoRootInternal, resourcePath );
  181. assertFalse( "Managed Repository File <" + repoFile.getAbsolutePath() + "> should not exist.", repoFile
  182. .exists() );
  183. }
  184. protected void setupCleanInternalRepo()
  185. throws Exception
  186. {
  187. setupCleanRepo( repoRootInternal );
  188. }
  189. protected File populateRepo( File repoRootManaged, String path, String contents )
  190. throws Exception
  191. {
  192. File destFile = new File( repoRootManaged, path );
  193. destFile.getParentFile().mkdirs();
  194. FileUtils.writeStringToFile( destFile, contents, null );
  195. return destFile;
  196. }
  197. }