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.Context;
39 import org.apache.catalina.core.StandardContext;
40 import org.apache.catalina.deploy.ApplicationParameter;
41 import org.apache.catalina.startup.Tomcat;
42 import org.apache.commons.io.FileUtils;
43 import org.junit.After;
44 import org.junit.Before;
45 import org.junit.runner.RunWith;
46 import org.slf4j.Logger;
47 import org.slf4j.LoggerFactory;
48 import org.springframework.context.ApplicationContext;
49 import org.springframework.test.context.ContextConfiguration;
50 import org.springframework.web.context.ContextLoaderListener;
52 import javax.inject.Inject;
53 import javax.servlet.http.HttpServletResponse;
55 import java.io.IOException;
56 import java.io.InputStream;
58 import java.nio.charset.Charset;
61 * AbstractRepositoryServletTestCase
63 @RunWith( ArchivaSpringJUnit4ClassRunner.class )
64 @ContextConfiguration( locations = { "classpath*:/repository-servlet-simple.xml" } )
65 public abstract class AbstractRepositoryServletTestCase
68 protected static final String REPOID_INTERNAL = "internal";
70 protected static final String REPOID_LEGACY = "legacy";
72 protected File repoRootInternal;
74 protected File repoRootLegacy;
77 protected ArchivaConfiguration archivaConfiguration;
80 protected ApplicationContext applicationContext;
82 protected Logger log = LoggerFactory.getLogger( getClass() );
85 protected void saveConfiguration()
88 saveConfiguration( archivaConfiguration );
91 protected Tomcat tomcat;
93 protected static int port;
102 String appserverBase = new File( "target/appserver-base" ).getAbsolutePath();
103 System.setProperty( "appserver.base", appserverBase );
105 File testConf = new File( "src/test/resources/repository-archiva.xml" );
106 File testConfDest = new File( appserverBase, "conf/archiva.xml" );
107 if ( testConfDest.exists() )
109 FileUtils.deleteQuietly( testConfDest );
111 FileUtils.copyFile( testConf, testConfDest );
113 archivaConfiguration = applicationContext.getBean( ArchivaConfiguration.class );
115 repoRootInternal = new File( appserverBase, "data/repositories/internal" );
116 repoRootLegacy = new File( appserverBase, "data/repositories/legacy" );
117 Configuration config = archivaConfiguration.getConfiguration();
119 config.getManagedRepositories().clear();
121 config.addManagedRepository(
122 createManagedRepository( REPOID_INTERNAL, "Internal Test Repo", repoRootInternal, true ) );
124 config.addManagedRepository(
125 createManagedRepository( REPOID_LEGACY, "Legacy Format Test Repo", repoRootLegacy, "legacy", true ) );
127 config.getProxyConnectors().clear();
129 config.getRemoteRepositories().clear();
131 saveConfiguration( archivaConfiguration );
133 CacheManager.getInstance().clearAll();
135 applicationContext.getBean( MavenIndexerCleaner.class ).cleanupIndex();
141 StandardContext context;
143 UnauthenticatedRepositoryServlet servlet;
145 protected void startRepository() throws Exception
147 tomcat = new Tomcat();
148 tomcat.setBaseDir( System.getProperty( "java.io.tmpdir" ) );
151 context = (StandardContext) tomcat.addContext( "", System.getProperty( "java.io.tmpdir" ) );
153 ApplicationParameter applicationParameter = new ApplicationParameter();
154 applicationParameter.setName( "contextConfigLocation" );
155 applicationParameter.setValue( getSpringConfigLocation() );
156 context.addApplicationParameter( applicationParameter );
158 context.addApplicationListener( ContextLoaderListener.class.getName() );
160 context.addApplicationListener( MavenIndexerCleaner.class.getName() );
162 servlet = new UnauthenticatedRepositoryServlet();
164 Tomcat.addServlet( context, "repository", servlet );
165 context.addServletMapping( "/repository/*", "repository" );
168 Tomcat.addServlet( context, "reinitservlet", new ReinitServlet() );
169 context.addServletMapping( "/reinit/*", "reinitservlet" );
173 this.port = tomcat.getConnector().getLocalPort();
176 protected String getSpringConfigLocation()
178 return "classpath*:/META-INF/spring-context.xml,classpath*:spring-context.xml";
185 protected ServletUnitClient getServletUnitClient()
188 if ( servletUnitClient != null )
190 return servletUnitClient;
192 servletRunner = new ServletRunner( new File( "src/test/resources/WEB-INF/web.xml" ) );
194 servletRunner.registerServlet( "/repository/*", UnauthenticatedRepositoryServlet.class.getName() );
196 servletUnitClient = servletRunner.newClient();
198 return servletUnitClient;
202 protected <P extends Page> P page(final String path) throws IOException {
203 return newClient().getPage(base.toExternalForm() + "repository/" + path);
207 protected static WebClient newClient()
209 final WebClient webClient = new WebClient();
210 webClient.getOptions().setJavaScriptEnabled( false );
211 webClient.getOptions().setCssEnabled( false );
212 webClient.getOptions().setAppletEnabled( false );
213 webClient.getOptions().setThrowExceptionOnFailingStatusCode( false );
214 webClient.setAjaxController( new NicelyResynchronizingAjaxController() );
219 protected static WebResponse getWebResponse( String path )
222 WebClient client = newClient();
223 client.getPage( "http://localhost:" + port + "/reinit/reload" );
224 return client.getPage( "http://localhost:" + port + path ).getWebResponse();
227 public static class GetMethodWebRequest
232 public GetMethodWebRequest( String url )
235 super( new URL( url ) );
241 public static class PutMethodWebRequest
246 public PutMethodWebRequest( String url, InputStream inputStream, String contentType )
249 super( new URL( url ), HttpMethod.PUT );
257 public static class ServletUnitClient
260 public ServletUnitClient()
265 public WebResponse getResponse( WebRequest request )
268 return getWebResponse( request.getUrl().getPath() );
271 public WebResponse getResource( WebRequest request )
274 return getResponse( request );
278 public ServletUnitClient getServletUnitClient()
280 return new ServletUnitClient();
285 public void tearDown()
289 if ( repoRootInternal.exists() )
291 FileUtils.deleteDirectory( repoRootInternal );
294 if ( repoRootLegacy.exists() )
296 FileUtils.deleteDirectory( repoRootLegacy );
299 if ( this.tomcat != null )
307 protected void assertFileContents( String expectedContents, File repoRoot, String path )
310 File actualFile = new File( repoRoot, path );
311 assertTrue( "File <" + actualFile.getAbsolutePath() + "> should exist.", actualFile.exists() );
312 assertTrue( "File <" + actualFile.getAbsolutePath() + "> should be a file (not a dir/link/device/etc).",
313 actualFile.isFile() );
315 String actualContents = FileUtils.readFileToString( actualFile, Charset.defaultCharset() );
316 assertEquals( "File Contents of <" + actualFile.getAbsolutePath() + ">", expectedContents, actualContents );
319 protected void assertRepositoryValid( RepositoryServlet servlet, String repoId )
322 ManagedRepository repository = servlet.getRepository( repoId );
323 assertNotNull( "Archiva Managed Repository id:<" + repoId + "> should exist.", repository );
324 File repoRoot = new File( repository.getLocation() );
325 assertTrue( "Archiva Managed Repository id:<" + repoId + "> should have a valid location on disk.",
326 repoRoot.exists() && repoRoot.isDirectory() );
329 protected void assertResponseOK( WebResponse response )
332 assertNotNull( "Should have recieved a response", response );
333 Assert.assertEquals( "Should have been an OK response code", HttpServletResponse.SC_OK,
334 response.getStatusCode() );
337 protected void assertResponseOK( WebResponse response, String path )
339 assertNotNull( "Should have recieved a response", response );
340 Assert.assertEquals( "Should have been an OK response code for path: " + path, HttpServletResponse.SC_OK,
341 response.getStatusCode() );
344 protected void assertResponseNotFound( WebResponse response )
346 assertNotNull( "Should have recieved a response", response );
347 Assert.assertEquals( "Should have been an 404/Not Found response code.", HttpServletResponse.SC_NOT_FOUND,
348 response.getStatusCode() );
351 protected void assertResponseInternalServerError( WebResponse response )
353 assertNotNull( "Should have recieved a response", response );
354 Assert.assertEquals( "Should have been an 500/Internal Server Error response code.",
355 HttpServletResponse.SC_INTERNAL_SERVER_ERROR, response.getStatusCode() );
358 protected void assertResponseConflictError( WebResponse response )
360 assertNotNull( "Should have received a response", response );
361 Assert.assertEquals( "Should have been a 409/Conflict response code.", HttpServletResponse.SC_CONFLICT,
362 response.getStatusCode() );
365 protected ManagedRepositoryConfiguration createManagedRepository( String id, String name, File location,
366 boolean blockRedeployments )
368 ManagedRepositoryConfiguration repo = new ManagedRepositoryConfiguration();
370 repo.setName( name );
371 repo.setLocation( location.getAbsolutePath() );
372 repo.setBlockRedeployments( blockRedeployments );
377 protected ManagedRepositoryConfiguration createManagedRepository( String id, String name, File location,
378 String layout, boolean blockRedeployments )
380 ManagedRepositoryConfiguration repo = createManagedRepository( id, name, location, blockRedeployments );
381 repo.setLayout( layout );
385 protected RemoteRepositoryConfiguration createRemoteRepository( String id, String name, String url )
387 RemoteRepositoryConfiguration repo = new RemoteRepositoryConfiguration();
389 repo.setName( name );
394 protected void saveConfiguration( ArchivaConfiguration archivaConfiguration )
397 archivaConfiguration.save( archivaConfiguration.getConfiguration() );
401 protected void setupCleanRepo( File repoRootDir )
404 FileUtils.deleteDirectory( repoRootDir );
405 if ( !repoRootDir.exists() )
407 repoRootDir.mkdirs();
411 protected void assertManagedFileNotExists( File repoRootInternal, String resourcePath )
413 File repoFile = new File( repoRootInternal, resourcePath );
414 assertFalse( "Managed Repository File <" + repoFile.getAbsolutePath() + "> should not exist.",
418 protected void setupCleanInternalRepo()
421 setupCleanRepo( repoRootInternal );
424 protected File populateRepo( File repoRootManaged, String path, String contents )
427 File destFile = new File( repoRootManaged, path );
428 destFile.getParentFile().mkdirs();
429 FileUtils.writeStringToFile( destFile, contents, Charset.defaultCharset() );