1 package org.apache.archiva.web.servlet;
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.archiva.web.servlet.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 ManagedRepositoryConfiguration createManagedRepository( String id, String name, File location )
102 ManagedRepositoryConfiguration repo = new ManagedRepositoryConfiguration();
104 repo.setName( name );
105 repo.setLocation( location.getAbsolutePath() );
109 protected RemoteRepositoryConfiguration createRemoteRepository( String id, String name, String url )
111 RemoteRepositoryConfiguration repo = new RemoteRepositoryConfiguration();
113 repo.setName( name );
118 protected void dumpResponse( WebResponse response )
120 System.out.println( "---(response)---" );
121 System.out.println( "" + response.getResponseCode() + " " + response.getResponseMessage() );
123 String headerNames[] = response.getHeaderFieldNames();
124 for ( String headerName : headerNames )
126 System.out.println( "[header] " + headerName + ": " + response.getHeaderField( headerName ) );
129 System.out.println( "---(text)---" );
132 System.out.println( response.getText() );
134 catch ( IOException e )
136 System.err.print( "[Exception] : " );
137 e.printStackTrace( System.err );
141 protected void saveConfiguration( ArchivaConfiguration archivaConfiguration )
144 archivaConfiguration.save( archivaConfiguration.getConfiguration() );
147 protected void setUp()
152 String appserverBase = getTestFile( "target/appserver-base" ).getAbsolutePath();
153 System.setProperty( "appserver.base", appserverBase );
155 File testConf = getTestFile( "src/test/resources/repository-archiva.xml" );
156 File testConfDest = new File( appserverBase, "conf/archiva.xml" );
157 FileUtils.copyFile( testConf, testConfDest );
159 archivaConfiguration = (ArchivaConfiguration) lookup( ArchivaConfiguration.class );
160 repoRootInternal = new File( appserverBase, "data/repositories/internal" );
161 Configuration config = archivaConfiguration.getConfiguration();
163 config.addManagedRepository( createManagedRepository( REPOID_INTERNAL, "Internal Test Repo", repoRootInternal ) );
164 saveConfiguration( archivaConfiguration );
166 CacheManager.getInstance().removeCache( "url-failures-cache" );
168 HttpUnitOptions.setExceptionsThrownOnErrorStatus( false );
170 sr = new ServletRunner( getTestFile( "src/test/resources/WEB-INF/web.xml" ) );
171 sr.registerServlet( "/repository/*", RepositoryServlet.class.getName() );
176 protected String getPlexusConfigLocation()
178 return "org/apache/maven/archiva/webdav/RepositoryServletTest.xml";
182 protected void tearDown()
195 if (repoRootInternal.exists())
197 FileUtils.deleteDirectory(repoRootInternal);
203 protected void setupCleanRepo( File repoRootDir )
206 FileUtils.deleteDirectory( repoRootDir );
207 if ( !repoRootDir.exists() )
209 repoRootDir.mkdirs();
213 protected void assertManagedFileNotExists( File repoRootInternal, String resourcePath )
215 File repoFile = new File( repoRootInternal, resourcePath );
216 assertFalse( "Managed Repository File <" + repoFile.getAbsolutePath() + "> should not exist.", repoFile
220 protected void setupCleanInternalRepo()
223 setupCleanRepo( repoRootInternal );
226 protected File populateRepo( File repoRootManaged, String path, String contents )
229 File destFile = new File( repoRootManaged, path );
230 destFile.getParentFile().mkdirs();
231 FileUtils.writeStringToFile( destFile, contents, null );