1 package org.apache.maven.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.meterware.httpunit.WebResponse;
23 import com.meterware.httpunit.HttpUnitOptions;
24 import com.meterware.servletunit.ServletRunner;
25 import com.meterware.servletunit.ServletUnitClient;
26 import net.sf.ehcache.CacheManager;
27 import org.apache.commons.io.FileUtils;
28 import org.apache.maven.archiva.configuration.ArchivaConfiguration;
29 import org.apache.maven.archiva.configuration.Configuration;
30 import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration;
31 import org.apache.maven.archiva.configuration.RemoteRepositoryConfiguration;
32 import org.apache.maven.archiva.webdav.RepositoryServlet;
33 import org.codehaus.plexus.spring.PlexusInSpringTestCase;
35 import javax.servlet.http.HttpServletResponse;
37 import java.io.IOException;
39 import junit.framework.Assert;
42 * AbstractRepositoryServletTestCase
46 public abstract class AbstractRepositoryServletTestCase
47 extends PlexusInSpringTestCase
49 protected static final String REPOID_INTERNAL = "internal";
51 protected ServletUnitClient sc;
53 protected File repoRootInternal;
55 private ServletRunner sr;
57 protected ArchivaConfiguration archivaConfiguration;
59 protected void saveConfiguration()
62 saveConfiguration( archivaConfiguration );
65 protected void assertFileContents( String expectedContents, File repoRoot, String path )
68 File actualFile = new File( repoRoot, path );
69 assertTrue( "File <" + actualFile.getAbsolutePath() + "> should exist.", actualFile.exists() );
70 assertTrue( "File <" + actualFile.getAbsolutePath() + "> should be a file (not a dir/link/device/etc).",
71 actualFile.isFile() );
73 String actualContents = FileUtils.readFileToString( actualFile, null );
74 assertEquals( "File Contents of <" + actualFile.getAbsolutePath() + ">", expectedContents, actualContents );
77 protected void assertRepositoryValid( RepositoryServlet servlet, String repoId )
79 ManagedRepositoryConfiguration repository = servlet.getRepository( repoId );
80 assertNotNull( "Archiva Managed Repository id:<" + repoId + "> should exist.", repository );
81 File repoRoot = new File( repository.getLocation() );
82 assertTrue( "Archiva Managed Repository id:<" + repoId + "> should have a valid location on disk.", repoRoot
84 && repoRoot.isDirectory() );
87 protected void assertResponseOK( WebResponse response )
89 assertNotNull( "Should have recieved a response", response );
90 Assert.assertEquals( "Should have been an OK response code.", HttpServletResponse.SC_OK, response.getResponseCode() );
93 protected void assertResponseNotFound( WebResponse response )
95 assertNotNull( "Should have recieved a response", response );
96 Assert.assertEquals( "Should have been an 404/Not Found response code.", HttpServletResponse.SC_NOT_FOUND, response
100 protected void assertResponseInternalServerError( WebResponse response )
102 assertNotNull( "Should have recieved a response", response );
103 Assert.assertEquals( "Should have been an 500/Internal Server Error response code.", HttpServletResponse.SC_INTERNAL_SERVER_ERROR, response
104 .getResponseCode() );
107 protected ManagedRepositoryConfiguration createManagedRepository( String id, String name, File location )
109 ManagedRepositoryConfiguration repo = new ManagedRepositoryConfiguration();
111 repo.setName( name );
112 repo.setLocation( location.getAbsolutePath() );
116 protected RemoteRepositoryConfiguration createRemoteRepository( String id, String name, String url )
118 RemoteRepositoryConfiguration repo = new RemoteRepositoryConfiguration();
120 repo.setName( name );
125 protected void dumpResponse( WebResponse response )
127 System.out.println( "---(response)---" );
128 System.out.println( "" + response.getResponseCode() + " " + response.getResponseMessage() );
130 String headerNames[] = response.getHeaderFieldNames();
131 for ( String headerName : headerNames )
133 System.out.println( "[header] " + headerName + ": " + response.getHeaderField( headerName ) );
136 System.out.println( "---(text)---" );
139 System.out.println( response.getText() );
141 catch ( IOException e )
143 System.err.print( "[Exception] : " );
144 e.printStackTrace( System.err );
148 protected void saveConfiguration( ArchivaConfiguration archivaConfiguration )
151 archivaConfiguration.save( archivaConfiguration.getConfiguration() );
154 protected void setUp()
159 String appserverBase = getTestFile( "target/appserver-base" ).getAbsolutePath();
160 System.setProperty( "appserver.base", appserverBase );
162 File testConf = getTestFile( "src/test/resources/repository-archiva.xml" );
163 File testConfDest = new File( appserverBase, "conf/archiva.xml" );
164 FileUtils.copyFile( testConf, testConfDest );
166 archivaConfiguration = (ArchivaConfiguration) lookup( ArchivaConfiguration.class );
167 repoRootInternal = new File( appserverBase, "data/repositories/internal" );
168 Configuration config = archivaConfiguration.getConfiguration();
170 config.addManagedRepository( createManagedRepository( REPOID_INTERNAL, "Internal Test Repo", repoRootInternal ) );
171 saveConfiguration( archivaConfiguration );
173 CacheManager.getInstance().removeCache( "url-failures-cache" );
175 HttpUnitOptions.setExceptionsThrownOnErrorStatus( false );
177 sr = new ServletRunner( getTestFile( "src/test/resources/WEB-INF/web.xml" ) );
178 sr.registerServlet( "/repository/*", UnauthenticatedRepositoryServlet.class.getName() );
183 protected String getPlexusConfigLocation()
185 return "org/apache/maven/archiva/webdav/RepositoryServletTest.xml";
189 protected void tearDown()
202 if (repoRootInternal.exists())
204 FileUtils.deleteDirectory(repoRootInternal);
210 protected void setupCleanRepo( File repoRootDir )
213 FileUtils.deleteDirectory( repoRootDir );
214 if ( !repoRootDir.exists() )
216 repoRootDir.mkdirs();
220 protected void assertManagedFileNotExists( File repoRootInternal, String resourcePath )
222 File repoFile = new File( repoRootInternal, resourcePath );
223 assertFalse( "Managed Repository File <" + repoFile.getAbsolutePath() + "> should not exist.", repoFile
227 protected void setupCleanInternalRepo()
230 setupCleanRepo( repoRootInternal );
233 protected File populateRepo( File repoRootManaged, String path, String contents )
236 File destFile = new File( repoRootManaged, path );
237 destFile.getParentFile().mkdirs();
238 FileUtils.writeStringToFile( destFile, contents, null );