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.catalina.Context;
38 import org.apache.catalina.deploy.ApplicationParameter;
39 import org.apache.catalina.startup.Tomcat;
40 import org.apache.commons.io.FileUtils;
41 import org.junit.After;
42 import org.junit.Before;
43 import org.junit.runner.RunWith;
44 import org.slf4j.Logger;
45 import org.slf4j.LoggerFactory;
46 import org.springframework.context.ApplicationContext;
47 import org.springframework.test.context.ContextConfiguration;
48 import org.springframework.web.context.ContextLoaderListener;
50 import javax.inject.Inject;
51 import javax.servlet.http.HttpServletResponse;
53 import java.io.IOException;
54 import java.io.InputStream;
56 import java.nio.charset.Charset;
59 * AbstractRepositoryServletTestCase
61 @RunWith( ArchivaSpringJUnit4ClassRunner.class )
62 @ContextConfiguration( locations = { "classpath*:/repository-servlet-simple.xml" } )
63 public abstract class AbstractRepositoryServletTestCase
66 protected static final String REPOID_INTERNAL = "internal";
68 protected static final String REPOID_LEGACY = "legacy";
70 protected File repoRootInternal;
72 protected File repoRootLegacy;
75 protected ArchivaConfiguration archivaConfiguration;
78 protected ApplicationContext applicationContext;
80 protected Logger log = LoggerFactory.getLogger( getClass() );
83 protected void saveConfiguration()
86 saveConfiguration( archivaConfiguration );
89 protected Tomcat tomcat;
91 protected static int port;
100 String appserverBase = new File( "target/appserver-base" ).getAbsolutePath();
101 System.setProperty( "appserver.base", appserverBase );
103 File testConf = new File( "src/test/resources/repository-archiva.xml" );
104 File testConfDest = new File( appserverBase, "conf/archiva.xml" );
105 if ( testConfDest.exists() )
107 FileUtils.deleteQuietly( testConfDest );
109 FileUtils.copyFile( testConf, testConfDest );
111 archivaConfiguration = applicationContext.getBean( ArchivaConfiguration.class );
113 repoRootInternal = new File( appserverBase, "data/repositories/internal" );
114 repoRootLegacy = new File( appserverBase, "data/repositories/legacy" );
115 Configuration config = archivaConfiguration.getConfiguration();
117 config.getManagedRepositories().clear();
119 config.addManagedRepository(
120 createManagedRepository( REPOID_INTERNAL, "Internal Test Repo", repoRootInternal, true ) );
122 config.addManagedRepository(
123 createManagedRepository( REPOID_LEGACY, "Legacy Format Test Repo", repoRootLegacy, "legacy", true ) );
125 config.getProxyConnectors().clear();
127 config.getRemoteRepositories().clear();
129 saveConfiguration( archivaConfiguration );
131 CacheManager.getInstance().clearAll();
133 applicationContext.getBean( MavenIndexerCleaner.class ).cleanupIndex();
135 tomcat = new Tomcat();
136 tomcat.setBaseDir( System.getProperty( "java.io.tmpdir" ) );
139 Context context = tomcat.addContext( "", System.getProperty( "java.io.tmpdir" ) );
141 ApplicationParameter applicationParameter = new ApplicationParameter();
142 applicationParameter.setName( "contextConfigLocation" );
143 applicationParameter.setValue( getSpringConfigLocation() );
144 context.addApplicationParameter( applicationParameter );
146 context.addApplicationListener( ContextLoaderListener.class.getName() );
148 context.addApplicationListener( MavenIndexerCleaner.class.getName() );
150 Tomcat.addServlet( context, "repository", new UnauthenticatedRepositoryServlet() );
151 context.addServletMapping( "/repository/*", "repository" );
155 this.port = tomcat.getConnector().getLocalPort();
159 protected String getSpringConfigLocation()
161 return "classpath*:/META-INF/spring-context.xml,classpath*:spring-context.xml";
168 protected ServletUnitClient getServletUnitClient()
171 if ( servletUnitClient != null )
173 return servletUnitClient;
175 servletRunner = new ServletRunner( new File( "src/test/resources/WEB-INF/web.xml" ) );
177 servletRunner.registerServlet( "/repository/*", UnauthenticatedRepositoryServlet.class.getName() );
179 servletUnitClient = servletRunner.newClient();
181 return servletUnitClient;
185 protected <P extends Page> P page(final String path) throws IOException {
186 return newClient().getPage(base.toExternalForm() + "repository/" + path);
190 protected static WebClient newClient()
192 final WebClient webClient = new WebClient();
193 webClient.getOptions().setJavaScriptEnabled( false );
194 webClient.getOptions().setCssEnabled( false );
195 webClient.getOptions().setAppletEnabled( false );
196 webClient.setAjaxController( new NicelyResynchronizingAjaxController() );
201 protected static WebResponse getWebResponse( String path )
204 return newClient().getPage( "http://localhost:" + port + path ).getWebResponse();
207 public static class GetMethodWebRequest
212 public GetMethodWebRequest( String url )
215 super( new URL( url ) );
221 public static class PutMethodWebRequest
226 public PutMethodWebRequest( String url, InputStream inputStream, String contentType )
229 super( new URL( url ), HttpMethod.PUT );
237 public static class ServletUnitClient
240 public ServletUnitClient()
245 public WebResponse getResponse( WebRequest request )
248 return getWebResponse( request.getUrl().getPath() );
251 public WebResponse getResource( WebRequest request )
254 return getResponse( request );
258 public ServletUnitClient getServletUnitClient()
260 return new ServletUnitClient();
265 public void tearDown()
269 if ( repoRootInternal.exists() )
271 FileUtils.deleteDirectory( repoRootInternal );
274 if ( repoRootLegacy.exists() )
276 FileUtils.deleteDirectory( repoRootLegacy );
279 if ( this.tomcat != null )
287 protected void assertFileContents( String expectedContents, File repoRoot, String path )
290 File actualFile = new File( repoRoot, path );
291 assertTrue( "File <" + actualFile.getAbsolutePath() + "> should exist.", actualFile.exists() );
292 assertTrue( "File <" + actualFile.getAbsolutePath() + "> should be a file (not a dir/link/device/etc).",
293 actualFile.isFile() );
295 String actualContents = FileUtils.readFileToString( actualFile, Charset.defaultCharset() );
296 assertEquals( "File Contents of <" + actualFile.getAbsolutePath() + ">", expectedContents, actualContents );
299 protected void assertRepositoryValid( RepositoryServlet servlet, String repoId )
302 ManagedRepository repository = servlet.getRepository( repoId );
303 assertNotNull( "Archiva Managed Repository id:<" + repoId + "> should exist.", repository );
304 File repoRoot = new File( repository.getLocation() );
305 assertTrue( "Archiva Managed Repository id:<" + repoId + "> should have a valid location on disk.",
306 repoRoot.exists() && repoRoot.isDirectory() );
309 protected void assertResponseOK( WebResponse response )
312 assertNotNull( "Should have recieved a response", response );
313 Assert.assertEquals( "Should have been an OK response code", HttpServletResponse.SC_OK,
314 response.getStatusCode() );
317 protected void assertResponseOK( WebResponse response, String path )
319 assertNotNull( "Should have recieved a response", response );
320 Assert.assertEquals( "Should have been an OK response code for path: " + path, HttpServletResponse.SC_OK,
321 response.getStatusCode() );
324 protected void assertResponseNotFound( WebResponse response )
326 assertNotNull( "Should have recieved a response", response );
327 Assert.assertEquals( "Should have been an 404/Not Found response code.", HttpServletResponse.SC_NOT_FOUND,
328 response.getStatusCode() );
331 protected void assertResponseInternalServerError( WebResponse response )
333 assertNotNull( "Should have recieved a response", response );
334 Assert.assertEquals( "Should have been an 500/Internal Server Error response code.",
335 HttpServletResponse.SC_INTERNAL_SERVER_ERROR, response.getStatusCode() );
338 protected void assertResponseConflictError( WebResponse response )
340 assertNotNull( "Should have received a response", response );
341 Assert.assertEquals( "Should have been a 409/Conflict response code.", HttpServletResponse.SC_CONFLICT,
342 response.getStatusCode() );
345 protected ManagedRepositoryConfiguration createManagedRepository( String id, String name, File location,
346 boolean blockRedeployments )
348 ManagedRepositoryConfiguration repo = new ManagedRepositoryConfiguration();
350 repo.setName( name );
351 repo.setLocation( location.getAbsolutePath() );
352 repo.setBlockRedeployments( blockRedeployments );
357 protected ManagedRepositoryConfiguration createManagedRepository( String id, String name, File location,
358 String layout, boolean blockRedeployments )
360 ManagedRepositoryConfiguration repo = createManagedRepository( id, name, location, blockRedeployments );
361 repo.setLayout( layout );
365 protected RemoteRepositoryConfiguration createRemoteRepository( String id, String name, String url )
367 RemoteRepositoryConfiguration repo = new RemoteRepositoryConfiguration();
369 repo.setName( name );
374 protected void saveConfiguration( ArchivaConfiguration archivaConfiguration )
377 archivaConfiguration.save( archivaConfiguration.getConfiguration() );
381 protected void setupCleanRepo( File repoRootDir )
384 FileUtils.deleteDirectory( repoRootDir );
385 if ( !repoRootDir.exists() )
387 repoRootDir.mkdirs();
391 protected void assertManagedFileNotExists( File repoRootInternal, String resourcePath )
393 File repoFile = new File( repoRootInternal, resourcePath );
394 assertFalse( "Managed Repository File <" + repoFile.getAbsolutePath() + "> should not exist.",
398 protected void setupCleanInternalRepo()
401 setupCleanRepo( repoRootInternal );
404 protected File populateRepo( File repoRootManaged, String path, String contents )
407 File destFile = new File( repoRootManaged, path );
408 destFile.getParentFile().mkdirs();
409 FileUtils.writeStringToFile( destFile, contents, Charset.defaultCharset() );