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
44 * @author <a href="mailto:joakime@apache.org">Joakim Erdfelt</a>
47 public abstract class AbstractRepositoryServletTestCase
48 extends PlexusInSpringTestCase
50 protected static final String REPOID_INTERNAL = "internal";
52 protected ServletUnitClient sc;
54 protected File repoRootInternal;
56 private ServletRunner sr;
58 protected ArchivaConfiguration archivaConfiguration;
60 protected void saveConfiguration()
63 saveConfiguration( archivaConfiguration );
66 protected void assertFileContents( String expectedContents, File repoRoot, String path )
69 File actualFile = new File( repoRoot, path );
70 assertTrue( "File <" + actualFile.getAbsolutePath() + "> should exist.", actualFile.exists() );
71 assertTrue( "File <" + actualFile.getAbsolutePath() + "> should be a file (not a dir/link/device/etc).",
72 actualFile.isFile() );
74 String actualContents = FileUtils.readFileToString( actualFile, null );
75 assertEquals( "File Contents of <" + actualFile.getAbsolutePath() + ">", expectedContents, actualContents );
78 protected void assertRepositoryValid( RepositoryServlet servlet, String repoId )
80 ManagedRepositoryConfiguration repository = servlet.getRepository( repoId );
81 assertNotNull( "Archiva Managed Repository id:<" + repoId + "> should exist.", repository );
82 File repoRoot = new File( repository.getLocation() );
83 assertTrue( "Archiva Managed Repository id:<" + repoId + "> should have a valid location on disk.", repoRoot
85 && repoRoot.isDirectory() );
88 protected void assertResponseOK( WebResponse response )
90 assertNotNull( "Should have recieved a response", response );
91 Assert.assertEquals( "Should have been an OK response code.", HttpServletResponse.SC_OK, response.getResponseCode() );
94 protected void assertResponseNotFound( WebResponse response )
96 assertNotNull( "Should have recieved a response", response );
97 Assert.assertEquals( "Should have been an 404/Not Found response code.", HttpServletResponse.SC_NOT_FOUND, response
101 protected ManagedRepositoryConfiguration createManagedRepository( String id, String name, File location )
103 ManagedRepositoryConfiguration repo = new ManagedRepositoryConfiguration();
105 repo.setName( name );
106 repo.setLocation( location.getAbsolutePath() );
110 protected RemoteRepositoryConfiguration createRemoteRepository( String id, String name, String url )
112 RemoteRepositoryConfiguration repo = new RemoteRepositoryConfiguration();
114 repo.setName( name );
119 protected void dumpResponse( WebResponse response )
121 System.out.println( "---(response)---" );
122 System.out.println( "" + response.getResponseCode() + " " + response.getResponseMessage() );
124 String headerNames[] = response.getHeaderFieldNames();
125 for ( String headerName : headerNames )
127 System.out.println( "[header] " + headerName + ": " + response.getHeaderField( headerName ) );
130 System.out.println( "---(text)---" );
133 System.out.println( response.getText() );
135 catch ( IOException e )
137 System.err.print( "[Exception] : " );
138 e.printStackTrace( System.err );
142 protected void saveConfiguration( ArchivaConfiguration archivaConfiguration )
145 archivaConfiguration.save( archivaConfiguration.getConfiguration() );
148 protected void setUp()
153 String appserverBase = getTestFile( "target/appserver-base" ).getAbsolutePath();
154 System.setProperty( "appserver.base", appserverBase );
156 File testConf = getTestFile( "src/test/resources/repository-archiva.xml" );
157 File testConfDest = new File( appserverBase, "conf/archiva.xml" );
158 FileUtils.copyFile( testConf, testConfDest );
160 archivaConfiguration = (ArchivaConfiguration) lookup( ArchivaConfiguration.class );
161 repoRootInternal = new File( appserverBase, "data/repositories/internal" );
162 Configuration config = archivaConfiguration.getConfiguration();
164 config.addManagedRepository( createManagedRepository( REPOID_INTERNAL, "Internal Test Repo", repoRootInternal ) );
165 saveConfiguration( archivaConfiguration );
167 CacheManager.getInstance().removeCache( "url-failures-cache" );
169 HttpUnitOptions.setExceptionsThrownOnErrorStatus( false );
171 sr = new ServletRunner( getTestFile( "src/test/resources/WEB-INF/web.xml" ) );
172 sr.registerServlet( "/repository/*", UnauthenticatedRepositoryServlet.class.getName() );
177 protected String getPlexusConfigLocation()
179 return "org/apache/maven/archiva/webdav/RepositoryServletTest.xml";
183 protected void tearDown()
199 protected void setupCleanRepo( File repoRootDir )
202 FileUtils.deleteDirectory( repoRootDir );
203 if ( !repoRootDir.exists() )
205 repoRootDir.mkdirs();
209 protected void assertManagedFileNotExists( File repoRootInternal, String resourcePath )
211 File repoFile = new File( repoRootInternal, resourcePath );
212 assertFalse( "Managed Repository File <" + repoFile.getAbsolutePath() + "> should not exist.", repoFile
216 protected void setupCleanInternalRepo()
219 setupCleanRepo( repoRootInternal );
222 protected File populateRepo( File repoRootManaged, String path, String contents )
225 File destFile = new File( repoRootManaged, path );
226 destFile.getParentFile().mkdirs();
227 FileUtils.writeStringToFile( destFile, contents, null );