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;
99 StandardContext context;
108 String appserverBase = new File( "target/appserver-base" ).getAbsolutePath();
109 System.setProperty( "appserver.base", appserverBase );
111 File testConf = new File( "src/test/resources/repository-archiva.xml" );
112 File testConfDest = new File( appserverBase, "conf/archiva.xml" );
113 if ( testConfDest.exists() )
115 FileUtils.deleteQuietly( testConfDest );
117 FileUtils.copyFile( testConf, testConfDest );
119 archivaConfiguration = applicationContext.getBean( ArchivaConfiguration.class );
121 repoRootInternal = new File( appserverBase, "data/repositories/internal" );
122 repoRootLegacy = new File( appserverBase, "data/repositories/legacy" );
123 Configuration config = archivaConfiguration.getConfiguration();
125 config.getManagedRepositories().clear();
127 config.addManagedRepository(
128 createManagedRepository( REPOID_INTERNAL, "Internal Test Repo", repoRootInternal, true ) );
130 config.addManagedRepository(
131 createManagedRepository( REPOID_LEGACY, "Legacy Format Test Repo", repoRootLegacy, "legacy", true ) );
133 config.getProxyConnectors().clear();
135 config.getRemoteRepositories().clear();
137 saveConfiguration( archivaConfiguration );
139 CacheManager.getInstance().clearAll();
141 applicationContext.getBean( MavenIndexerCleaner.class ).cleanupIndex();
147 protected void startRepository()
150 tomcat = new Tomcat();
151 tomcat.setBaseDir( System.getProperty( "java.io.tmpdir" ) );
154 context = StandardContext.class.cast( 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 Tomcat.addServlet( context, "repository", new UnauthenticatedRepositoryServlet() );
166 context.addServletMapping( "/repository/*", "repository" );
168 Tomcat.addServlet( context, "reinitservlet", new ReinitServlet() );
169 context.addServletMapping( "/reinit/*", "reinitservlet" );
173 this.port = tomcat.getConnector().getLocalPort();
176 protected Servlet findServlet( String name )
179 Container[] childs = context.findChildren();
180 for ( Container container : childs )
182 if ( StringUtils.equals( container.getName(), name ) )
184 Tomcat.ExistingStandardWrapper esw = Tomcat.ExistingStandardWrapper.class.cast( container );
185 Servlet servlet = esw.loadServlet();
193 protected String getSpringConfigLocation()
195 return "classpath*:/META-INF/spring-context.xml,classpath*:spring-context.xml";
202 protected ServletUnitClient getServletUnitClient()
205 if ( servletUnitClient != null )
207 return servletUnitClient;
209 servletRunner = new ServletRunner( new File( "src/test/resources/WEB-INF/web.xml" ) );
211 servletRunner.registerServlet( "/repository/*", UnauthenticatedRepositoryServlet.class.getName() );
213 servletUnitClient = servletRunner.newClient();
215 return servletUnitClient;
219 protected <P extends Page> P page(final String path) throws IOException {
220 return newClient().getPage(base.toExternalForm() + "repository/" + path);
224 protected static WebClient newClient()
226 final WebClient webClient = new WebClient();
227 webClient.getOptions().setJavaScriptEnabled( false );
228 webClient.getOptions().setCssEnabled( false );
229 webClient.getOptions().setAppletEnabled( false );
230 webClient.getOptions().setThrowExceptionOnFailingStatusCode( false );
231 webClient.setAjaxController( new NicelyResynchronizingAjaxController() );
236 protected static WebResponse getWebResponse( String path )
239 WebClient client = newClient();
240 client.getPage( "http://localhost:" + port + "/reinit/reload" );
241 return client.getPage( "http://localhost:" + port + path ).getWebResponse();
244 public static class GetMethodWebRequest
249 public GetMethodWebRequest( String url )
252 super( new URL( url ) );
258 public static class PutMethodWebRequest
263 public PutMethodWebRequest( String url, InputStream inputStream, String contentType )
266 super( new URL( url ), HttpMethod.PUT );
274 public static class ServletUnitClient
277 public ServletUnitClient()
282 public WebResponse getResponse( WebRequest request )
285 return getWebResponse( request.getUrl().getPath() );
288 public WebResponse getResource( WebRequest request )
291 return getResponse( request );
295 public ServletUnitClient getServletUnitClient()
297 return new ServletUnitClient();
302 public void tearDown()
306 if ( repoRootInternal.exists() )
308 FileUtils.deleteDirectory( repoRootInternal );
311 if ( repoRootLegacy.exists() )
313 FileUtils.deleteDirectory( repoRootLegacy );
316 if ( this.tomcat != null )
324 protected void assertFileContents( String expectedContents, File repoRoot, String path )
327 File actualFile = new File( repoRoot, path );
328 assertTrue( "File <" + actualFile.getAbsolutePath() + "> should exist.", actualFile.exists() );
329 assertTrue( "File <" + actualFile.getAbsolutePath() + "> should be a file (not a dir/link/device/etc).",
330 actualFile.isFile() );
332 String actualContents = FileUtils.readFileToString( actualFile, Charset.defaultCharset() );
333 assertEquals( "File Contents of <" + actualFile.getAbsolutePath() + ">", expectedContents, actualContents );
336 protected void assertRepositoryValid( RepositoryServlet servlet, String repoId )
339 ManagedRepository repository = servlet.getRepository( repoId );
340 assertNotNull( "Archiva Managed Repository id:<" + repoId + "> should exist.", repository );
341 File repoRoot = new File( repository.getLocation() );
342 assertTrue( "Archiva Managed Repository id:<" + repoId + "> should have a valid location on disk.",
343 repoRoot.exists() && repoRoot.isDirectory() );
346 protected void assertResponseOK( WebResponse response )
349 assertNotNull( "Should have recieved a response", response );
350 Assert.assertEquals( "Should have been an OK response code", HttpServletResponse.SC_OK,
351 response.getStatusCode() );
354 protected void assertResponseOK( WebResponse response, String path )
356 assertNotNull( "Should have recieved a response", response );
357 Assert.assertEquals( "Should have been an OK response code for path: " + path, HttpServletResponse.SC_OK,
358 response.getStatusCode() );
361 protected void assertResponseNotFound( WebResponse response )
363 assertNotNull( "Should have recieved a response", response );
364 Assert.assertEquals( "Should have been an 404/Not Found response code.", HttpServletResponse.SC_NOT_FOUND,
365 response.getStatusCode() );
368 protected void assertResponseInternalServerError( WebResponse response )
370 assertNotNull( "Should have recieved a response", response );
371 Assert.assertEquals( "Should have been an 500/Internal Server Error response code.",
372 HttpServletResponse.SC_INTERNAL_SERVER_ERROR, response.getStatusCode() );
375 protected void assertResponseConflictError( WebResponse response )
377 assertNotNull( "Should have received a response", response );
378 Assert.assertEquals( "Should have been a 409/Conflict response code.", HttpServletResponse.SC_CONFLICT,
379 response.getStatusCode() );
382 protected ManagedRepositoryConfiguration createManagedRepository( String id, String name, File location,
383 boolean blockRedeployments )
385 ManagedRepositoryConfiguration repo = new ManagedRepositoryConfiguration();
387 repo.setName( name );
388 repo.setLocation( location.getAbsolutePath() );
389 repo.setBlockRedeployments( blockRedeployments );
394 protected ManagedRepositoryConfiguration createManagedRepository( String id, String name, File location,
395 String layout, boolean blockRedeployments )
397 ManagedRepositoryConfiguration repo = createManagedRepository( id, name, location, blockRedeployments );
398 repo.setLayout( layout );
402 protected RemoteRepositoryConfiguration createRemoteRepository( String id, String name, String url )
404 RemoteRepositoryConfiguration repo = new RemoteRepositoryConfiguration();
406 repo.setName( name );
411 protected void saveConfiguration( ArchivaConfiguration archivaConfiguration )
414 archivaConfiguration.save( archivaConfiguration.getConfiguration() );
418 protected void setupCleanRepo( File repoRootDir )
421 FileUtils.deleteDirectory( repoRootDir );
422 if ( !repoRootDir.exists() )
424 repoRootDir.mkdirs();
428 protected void assertManagedFileNotExists( File repoRootInternal, String resourcePath )
430 File repoFile = new File( repoRootInternal, resourcePath );
431 assertFalse( "Managed Repository File <" + repoFile.getAbsolutePath() + "> should not exist.",
435 protected void setupCleanInternalRepo()
438 setupCleanRepo( repoRootInternal );
441 protected File populateRepo( File repoRootManaged, String path, String contents )
444 File destFile = new File( repoRootManaged, path );
445 destFile.getParentFile().mkdirs();
446 FileUtils.writeStringToFile( destFile, contents, Charset.defaultCharset() );