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,
91 response.getResponseCode() );
94 protected void assertResponseOK( WebResponse response, String path )
96 assertNotNull( "Should have recieved a response", response );
97 Assert.assertEquals( "Should have been an OK response code for path: " + path, HttpServletResponse.SC_OK,
98 response.getResponseCode() );
101 protected void assertResponseNotFound( WebResponse response )
103 assertNotNull( "Should have recieved a response", response );
104 Assert.assertEquals( "Should have been an 404/Not Found response code.", HttpServletResponse.SC_NOT_FOUND, response
105 .getResponseCode() );
108 protected void assertResponseInternalServerError( WebResponse response )
110 assertNotNull( "Should have recieved a response", response );
111 Assert.assertEquals( "Should have been an 500/Internal Server Error response code.", HttpServletResponse.SC_INTERNAL_SERVER_ERROR, response
112 .getResponseCode() );
115 protected void assertResponseConflictError( WebResponse response )
117 assertNotNull( "Should have received a response", response );
118 Assert.assertEquals( "Should have been a 409/Conflict response code.", HttpServletResponse.SC_CONFLICT,
119 response.getResponseCode() );
122 protected ManagedRepositoryConfiguration createManagedRepository( String id, String name, File location, boolean blockRedeployments )
124 ManagedRepositoryConfiguration repo = new ManagedRepositoryConfiguration();
126 repo.setName( name );
127 repo.setLocation( location.getAbsolutePath() );
128 repo.setBlockRedeployments( blockRedeployments );
133 protected RemoteRepositoryConfiguration createRemoteRepository( String id, String name, String url )
135 RemoteRepositoryConfiguration repo = new RemoteRepositoryConfiguration();
137 repo.setName( name );
142 protected void dumpResponse( WebResponse response )
144 System.out.println( "---(response)---" );
145 System.out.println( "" + response.getResponseCode() + " " + response.getResponseMessage() );
147 String headerNames[] = response.getHeaderFieldNames();
148 for ( String headerName : headerNames )
150 System.out.println( "[header] " + headerName + ": " + response.getHeaderField( headerName ) );
153 System.out.println( "---(text)---" );
156 System.out.println( response.getText() );
158 catch ( IOException e )
160 System.err.print( "[Exception] : " );
161 e.printStackTrace( System.err );
165 protected void saveConfiguration( ArchivaConfiguration archivaConfiguration )
168 archivaConfiguration.save( archivaConfiguration.getConfiguration() );
171 protected void setUp()
176 String appserverBase = getTestFile( "target/appserver-base" ).getAbsolutePath();
177 System.setProperty( "appserver.base", appserverBase );
179 File testConf = getTestFile( "src/test/resources/repository-archiva.xml" );
180 File testConfDest = new File( appserverBase, "conf/archiva.xml" );
181 FileUtils.copyFile( testConf, testConfDest );
183 archivaConfiguration = (ArchivaConfiguration) lookup( ArchivaConfiguration.class );
184 repoRootInternal = new File( appserverBase, "data/repositories/internal" );
185 Configuration config = archivaConfiguration.getConfiguration();
187 config.addManagedRepository( createManagedRepository( REPOID_INTERNAL, "Internal Test Repo", repoRootInternal, true ) );
188 saveConfiguration( archivaConfiguration );
190 CacheManager.getInstance().removeCache( "url-failures-cache" );
192 HttpUnitOptions.setExceptionsThrownOnErrorStatus( false );
194 sr = new ServletRunner( getTestFile( "src/test/resources/WEB-INF/web.xml" ) );
195 sr.registerServlet( "/repository/*", UnauthenticatedRepositoryServlet.class.getName() );
200 protected String getPlexusConfigLocation()
202 return "org/apache/maven/archiva/webdav/RepositoryServletTest.xml";
206 protected void tearDown()
219 if (repoRootInternal.exists())
221 FileUtils.deleteDirectory(repoRootInternal);
224 release( archivaConfiguration );
229 protected void setupCleanRepo( File repoRootDir )
232 FileUtils.deleteDirectory( repoRootDir );
233 if ( !repoRootDir.exists() )
235 repoRootDir.mkdirs();
239 protected void assertManagedFileNotExists( File repoRootInternal, String resourcePath )
241 File repoFile = new File( repoRootInternal, resourcePath );
242 assertFalse( "Managed Repository File <" + repoFile.getAbsolutePath() + "> should not exist.", repoFile
246 protected void setupCleanInternalRepo()
249 setupCleanRepo( repoRootInternal );
252 protected File populateRepo( File repoRootManaged, String path, String contents )
255 File destFile = new File( repoRootManaged, path );
256 destFile.getParentFile().mkdirs();
257 FileUtils.writeStringToFile( destFile, contents, null );