1 package org.apache.archiva.webdav;
4 * Licensed to the Apache Software Foundation (ASF) under one
5 * or more contributor license agreements. See the NOTICE file
6 * distributed with this work for additional information
7 * regarding copyright ownership. The ASF licenses this file
8 * to you under the Apache License, Version 2.0 (the
9 * "License"); you may not use this file except in compliance
10 * with the License. You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 * KIND, either express or implied. See the License for the
18 * specific language governing permissions and limitations
22 import com.gargoylesoftware.htmlunit.HttpMethod;
23 import com.gargoylesoftware.htmlunit.NicelyResynchronizingAjaxController;
24 import com.gargoylesoftware.htmlunit.WebClient;
25 import com.gargoylesoftware.htmlunit.WebRequest;
26 import com.gargoylesoftware.htmlunit.WebResponse;
27 import junit.framework.Assert;
28 import junit.framework.TestCase;
29 import net.sf.ehcache.CacheManager;
30 import org.apache.archiva.admin.model.beans.ManagedRepository;
31 import org.apache.archiva.configuration.ArchivaConfiguration;
32 import org.apache.archiva.configuration.Configuration;
33 import org.apache.archiva.configuration.ManagedRepositoryConfiguration;
34 import org.apache.archiva.configuration.RemoteRepositoryConfiguration;
35 import org.apache.archiva.test.utils.ArchivaSpringJUnit4ClassRunner;
36 import org.apache.archiva.webdav.util.MavenIndexerCleaner;
37 import org.apache.archiva.webdav.util.ReinitServlet;
38 import org.apache.catalina.Container;
39 import org.apache.catalina.Context;
40 import org.apache.catalina.core.StandardContext;
41 import org.apache.catalina.deploy.ApplicationParameter;
42 import org.apache.catalina.startup.Tomcat;
43 import org.apache.commons.io.FileUtils;
44 import org.apache.commons.lang.StringUtils;
45 import org.junit.After;
46 import org.junit.Before;
47 import org.junit.runner.RunWith;
48 import org.slf4j.Logger;
49 import org.slf4j.LoggerFactory;
50 import org.springframework.context.ApplicationContext;
51 import org.springframework.test.context.ContextConfiguration;
52 import org.springframework.web.context.ContextLoaderListener;
54 import javax.inject.Inject;
55 import javax.servlet.Servlet;
56 import javax.servlet.http.HttpServletResponse;
58 import java.io.IOException;
59 import java.io.InputStream;
61 import java.nio.charset.Charset;
64 * AbstractRepositoryServletTestCase
66 @RunWith(ArchivaSpringJUnit4ClassRunner.class)
67 @ContextConfiguration(locations = { "classpath*:/repository-servlet-simple.xml" })
68 public abstract class AbstractRepositoryServletTestCase
71 protected static final String REPOID_INTERNAL = "internal";
73 protected static final String REPOID_LEGACY = "legacy";
75 protected File repoRootInternal;
77 protected File repoRootLegacy;
80 protected ArchivaConfiguration archivaConfiguration;
83 protected ApplicationContext applicationContext;
85 protected Logger log = LoggerFactory.getLogger( getClass() );
88 protected void saveConfiguration()
91 saveConfiguration( archivaConfiguration );
94 protected Tomcat tomcat;
96 protected static int port;
105 String appserverBase = new File( "target/appserver-base" ).getAbsolutePath();
106 System.setProperty( "appserver.base", appserverBase );
108 File testConf = new File( "src/test/resources/repository-archiva.xml" );
109 File testConfDest = new File( appserverBase, "conf/archiva.xml" );
110 if ( testConfDest.exists() )
112 FileUtils.deleteQuietly( testConfDest );
114 FileUtils.copyFile( testConf, testConfDest );
116 archivaConfiguration = applicationContext.getBean( ArchivaConfiguration.class );
118 repoRootInternal = new File( appserverBase, "data/repositories/internal" );
119 repoRootLegacy = new File( appserverBase, "data/repositories/legacy" );
120 Configuration config = archivaConfiguration.getConfiguration();
122 config.getManagedRepositories().clear();
124 config.addManagedRepository(
125 createManagedRepository( REPOID_INTERNAL, "Internal Test Repo", repoRootInternal, true ) );
127 config.addManagedRepository(
128 createManagedRepository( REPOID_LEGACY, "Legacy Format Test Repo", repoRootLegacy, "legacy", true ) );
130 config.getProxyConnectors().clear();
132 config.getRemoteRepositories().clear();
134 saveConfiguration( archivaConfiguration );
136 CacheManager.getInstance().clearAll();
138 applicationContext.getBean( MavenIndexerCleaner.class ).cleanupIndex();
143 StandardContext context;
145 UnauthenticatedRepositoryServlet servlet;
147 protected void startRepository()
150 tomcat = new Tomcat();
151 tomcat.setBaseDir( System.getProperty( "java.io.tmpdir" ) );
154 context = (StandardContext) tomcat.addContext( "", System.getProperty( "java.io.tmpdir" ) );
156 ApplicationParameter applicationParameter = new ApplicationParameter();
157 applicationParameter.setName( "contextConfigLocation" );
158 applicationParameter.setValue( getSpringConfigLocation() );
159 context.addApplicationParameter( applicationParameter );
161 context.addApplicationListener( ContextLoaderListener.class.getName() );
163 context.addApplicationListener( MavenIndexerCleaner.class.getName() );
165 servlet = new UnauthenticatedRepositoryServlet();
167 Tomcat.addServlet( context, "repository", servlet );
168 context.addServletMapping( "/repository/*", "repository" );
170 Tomcat.addServlet( context, "reinitservlet", new ReinitServlet() );
171 context.addServletMapping( "/reinit/*", "reinitservlet" );
175 this.port = tomcat.getConnector().getLocalPort();
178 protected Servlet findServlet( String name )
181 Container[] childs = context.findChildren();
182 for ( Container container : childs )
184 if ( StringUtils.equals( container.getName(), name ) )
186 Tomcat.ExistingStandardWrapper esw = Tomcat.ExistingStandardWrapper.class.cast( container );
187 Servlet servlet = esw.loadServlet();
195 protected String getSpringConfigLocation()
197 return "classpath*:/META-INF/spring-context.xml,classpath*:spring-context.xml";
204 protected ServletUnitClient getServletUnitClient()
207 if ( servletUnitClient != null )
209 return servletUnitClient;
211 servletRunner = new ServletRunner( new File( "src/test/resources/WEB-INF/web.xml" ) );
213 servletRunner.registerServlet( "/repository/*", UnauthenticatedRepositoryServlet.class.getName() );
215 servletUnitClient = servletRunner.newClient();
217 return servletUnitClient;
221 protected <P extends Page> P page(final String path) throws IOException {
222 return newClient().getPage(base.toExternalForm() + "repository/" + path);
226 protected static WebClient newClient()
228 final WebClient webClient = new WebClient();
229 webClient.getOptions().setJavaScriptEnabled( false );
230 webClient.getOptions().setCssEnabled( false );
231 webClient.getOptions().setAppletEnabled( false );
232 webClient.getOptions().setThrowExceptionOnFailingStatusCode( false );
233 webClient.setAjaxController( new NicelyResynchronizingAjaxController() );
238 protected static WebResponse getWebResponse( String path )
241 WebClient client = newClient();
242 client.getPage( "http://localhost:" + port + "/reinit/reload" );
243 return client.getPage( "http://localhost:" + port + path ).getWebResponse();
246 public static class GetMethodWebRequest
251 public GetMethodWebRequest( String url )
254 super( new URL( url ) );
260 public static class PutMethodWebRequest
265 public PutMethodWebRequest( String url, InputStream inputStream, String contentType )
268 super( new URL( url ), HttpMethod.PUT );
276 public static class ServletUnitClient
279 public ServletUnitClient()
284 public WebResponse getResponse( WebRequest request )
287 return getWebResponse( request.getUrl().getPath() );
290 public WebResponse getResource( WebRequest request )
293 return getResponse( request );
297 public ServletUnitClient getServletUnitClient()
299 return new ServletUnitClient();
304 public void tearDown()
308 if ( repoRootInternal.exists() )
310 FileUtils.deleteDirectory( repoRootInternal );
313 if ( repoRootLegacy.exists() )
315 FileUtils.deleteDirectory( repoRootLegacy );
318 if ( this.tomcat != null )
326 protected void assertFileContents( String expectedContents, File repoRoot, String path )
329 File actualFile = new File( repoRoot, path );
330 assertTrue( "File <" + actualFile.getAbsolutePath() + "> should exist.", actualFile.exists() );
331 assertTrue( "File <" + actualFile.getAbsolutePath() + "> should be a file (not a dir/link/device/etc).",
332 actualFile.isFile() );
334 String actualContents = FileUtils.readFileToString( actualFile, Charset.defaultCharset() );
335 assertEquals( "File Contents of <" + actualFile.getAbsolutePath() + ">", expectedContents, actualContents );
338 protected void assertRepositoryValid( RepositoryServlet servlet, String repoId )
341 ManagedRepository repository = servlet.getRepository( repoId );
342 assertNotNull( "Archiva Managed Repository id:<" + repoId + "> should exist.", repository );
343 File repoRoot = new File( repository.getLocation() );
344 assertTrue( "Archiva Managed Repository id:<" + repoId + "> should have a valid location on disk.",
345 repoRoot.exists() && repoRoot.isDirectory() );
348 protected void assertResponseOK( WebResponse response )
351 assertNotNull( "Should have recieved a response", response );
352 Assert.assertEquals( "Should have been an OK response code", HttpServletResponse.SC_OK,
353 response.getStatusCode() );
356 protected void assertResponseOK( WebResponse response, String path )
358 assertNotNull( "Should have recieved a response", response );
359 Assert.assertEquals( "Should have been an OK response code for path: " + path, HttpServletResponse.SC_OK,
360 response.getStatusCode() );
363 protected void assertResponseNotFound( WebResponse response )
365 assertNotNull( "Should have recieved a response", response );
366 Assert.assertEquals( "Should have been an 404/Not Found response code.", HttpServletResponse.SC_NOT_FOUND,
367 response.getStatusCode() );
370 protected void assertResponseInternalServerError( WebResponse response )
372 assertNotNull( "Should have recieved a response", response );
373 Assert.assertEquals( "Should have been an 500/Internal Server Error response code.",
374 HttpServletResponse.SC_INTERNAL_SERVER_ERROR, response.getStatusCode() );
377 protected void assertResponseConflictError( WebResponse response )
379 assertNotNull( "Should have received a response", response );
380 Assert.assertEquals( "Should have been a 409/Conflict response code.", HttpServletResponse.SC_CONFLICT,
381 response.getStatusCode() );
384 protected ManagedRepositoryConfiguration createManagedRepository( String id, String name, File location,
385 boolean blockRedeployments )
387 ManagedRepositoryConfiguration repo = new ManagedRepositoryConfiguration();
389 repo.setName( name );
390 repo.setLocation( location.getAbsolutePath() );
391 repo.setBlockRedeployments( blockRedeployments );
396 protected ManagedRepositoryConfiguration createManagedRepository( String id, String name, File location,
397 String layout, boolean blockRedeployments )
399 ManagedRepositoryConfiguration repo = createManagedRepository( id, name, location, blockRedeployments );
400 repo.setLayout( layout );
404 protected RemoteRepositoryConfiguration createRemoteRepository( String id, String name, String url )
406 RemoteRepositoryConfiguration repo = new RemoteRepositoryConfiguration();
408 repo.setName( name );
413 protected void saveConfiguration( ArchivaConfiguration archivaConfiguration )
416 archivaConfiguration.save( archivaConfiguration.getConfiguration() );
420 protected void setupCleanRepo( File repoRootDir )
423 FileUtils.deleteDirectory( repoRootDir );
424 if ( !repoRootDir.exists() )
426 repoRootDir.mkdirs();
430 protected void assertManagedFileNotExists( File repoRootInternal, String resourcePath )
432 File repoFile = new File( repoRootInternal, resourcePath );
433 assertFalse( "Managed Repository File <" + repoFile.getAbsolutePath() + "> should not exist.",
437 protected void setupCleanInternalRepo()
440 setupCleanRepo( repoRootInternal );
443 protected File populateRepo( File repoRootManaged, String path, String contents )
446 File destFile = new File( repoRootManaged, path );
447 destFile.getParentFile().mkdirs();
448 FileUtils.writeStringToFile( destFile, contents, Charset.defaultCharset() );