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.meterware.httpunit.HttpUnitOptions;
23 import com.meterware.httpunit.WebResponse;
24 import com.meterware.servletunit.ServletRunner;
25 import com.meterware.servletunit.ServletUnitClient;
26 import junit.framework.Assert;
27 import junit.framework.TestCase;
28 import net.sf.ehcache.CacheManager;
29 import org.apache.archiva.admin.model.beans.ManagedRepository;
30 import org.apache.archiva.configuration.ArchivaConfiguration;
31 import org.apache.archiva.configuration.Configuration;
32 import org.apache.archiva.configuration.ManagedRepositoryConfiguration;
33 import org.apache.archiva.configuration.RemoteRepositoryConfiguration;
34 import org.apache.archiva.test.utils.ArchivaSpringJUnit4ClassRunner;
35 import org.apache.archiva.webdav.util.MavenIndexerCleaner;
36 import org.apache.commons.io.FileUtils;
37 import org.junit.After;
38 import org.junit.Before;
39 import org.junit.runner.RunWith;
40 import org.slf4j.Logger;
41 import org.slf4j.LoggerFactory;
42 import org.springframework.context.ApplicationContext;
43 import org.springframework.test.context.ContextConfiguration;
45 import javax.inject.Inject;
46 import javax.servlet.http.HttpServletResponse;
48 import java.io.IOException;
49 import java.nio.charset.Charset;
52 * AbstractRepositoryServletTestCase
54 @RunWith ( ArchivaSpringJUnit4ClassRunner.class )
55 @ContextConfiguration ( locations = { "classpath*:/repository-servlet-simple.xml" } )
56 public abstract class AbstractRepositoryServletTestCase
59 protected static final String REPOID_INTERNAL = "internal";
61 protected static final String REPOID_LEGACY = "legacy";
63 protected File repoRootInternal;
65 protected File repoRootLegacy;
67 protected ServletUnitClient servletUnitClient;
69 private ServletRunner servletRunner;
71 protected ArchivaConfiguration archivaConfiguration;
74 protected ApplicationContext applicationContext;
76 protected Logger log = LoggerFactory.getLogger( getClass() );
79 protected void saveConfiguration()
82 saveConfiguration( archivaConfiguration );
92 String appserverBase = new File( "target/appserver-base" ).getAbsolutePath();
93 System.setProperty( "appserver.base", appserverBase );
95 File testConf = new File( "src/test/resources/repository-archiva.xml" );
96 File testConfDest = new File( appserverBase, "conf/archiva.xml" );
97 if ( testConfDest.exists() )
99 FileUtils.deleteQuietly( testConfDest );
101 FileUtils.copyFile( testConf, testConfDest );
103 archivaConfiguration = applicationContext.getBean( ArchivaConfiguration.class );
105 repoRootInternal = new File( appserverBase, "data/repositories/internal" );
106 repoRootLegacy = new File( appserverBase, "data/repositories/legacy" );
107 Configuration config = archivaConfiguration.getConfiguration();
109 config.getManagedRepositories().clear();
111 config.addManagedRepository(
112 createManagedRepository( REPOID_INTERNAL, "Internal Test Repo", repoRootInternal, true ) );
114 config.addManagedRepository(
115 createManagedRepository( REPOID_LEGACY, "Legacy Format Test Repo", repoRootLegacy, "legacy", true ) );
117 config.getProxyConnectors().clear();
119 config.getRemoteRepositories().clear();
121 saveConfiguration( archivaConfiguration );
123 CacheManager.getInstance().clearAll();
125 HttpUnitOptions.setExceptionsThrownOnErrorStatus( false );
127 applicationContext.getBean( MavenIndexerCleaner.class ).cleanupIndex();
131 protected ServletUnitClient getServletUnitClient()
134 if ( servletUnitClient != null )
136 return servletUnitClient;
138 servletRunner = new ServletRunner( new File( "src/test/resources/WEB-INF/web.xml" ) );
140 servletRunner.registerServlet( "/repository/*", UnauthenticatedRepositoryServlet.class.getName() );
142 servletUnitClient = servletRunner.newClient();
144 return servletUnitClient;
149 public void tearDown()
153 if ( servletUnitClient != null )
155 servletUnitClient.clearContents();
158 if ( servletRunner != null )
160 servletRunner.shutDown();
163 if ( repoRootInternal.exists() )
165 FileUtils.deleteDirectory( repoRootInternal );
168 if ( repoRootLegacy.exists() )
170 FileUtils.deleteDirectory( repoRootLegacy );
177 protected void assertFileContents( String expectedContents, File repoRoot, String path )
180 File actualFile = new File( repoRoot, path );
181 assertTrue( "File <" + actualFile.getAbsolutePath() + "> should exist.", actualFile.exists() );
182 assertTrue( "File <" + actualFile.getAbsolutePath() + "> should be a file (not a dir/link/device/etc).",
183 actualFile.isFile() );
185 String actualContents = FileUtils.readFileToString( actualFile, Charset.defaultCharset() );
186 assertEquals( "File Contents of <" + actualFile.getAbsolutePath() + ">", expectedContents, actualContents );
189 protected void assertRepositoryValid( RepositoryServlet servlet, String repoId )
192 ManagedRepository repository = servlet.getRepository( repoId );
193 assertNotNull( "Archiva Managed Repository id:<" + repoId + "> should exist.", repository );
194 File repoRoot = new File( repository.getLocation() );
195 assertTrue( "Archiva Managed Repository id:<" + repoId + "> should have a valid location on disk.",
196 repoRoot.exists() && repoRoot.isDirectory() );
199 protected void assertResponseOK( WebResponse response )
201 assertNotNull( "Should have recieved a response", response );
202 Assert.assertEquals( "Should have been an OK response code", HttpServletResponse.SC_OK,
203 response.getResponseCode() );
206 protected void assertResponseOK( WebResponse response, String path )
208 assertNotNull( "Should have recieved a response", response );
209 Assert.assertEquals( "Should have been an OK response code for path: " + path, HttpServletResponse.SC_OK,
210 response.getResponseCode() );
213 protected void assertResponseNotFound( WebResponse response )
215 assertNotNull( "Should have recieved a response", response );
216 Assert.assertEquals( "Should have been an 404/Not Found response code.", HttpServletResponse.SC_NOT_FOUND,
217 response.getResponseCode() );
220 protected void assertResponseInternalServerError( WebResponse response )
222 assertNotNull( "Should have recieved a response", response );
223 Assert.assertEquals( "Should have been an 500/Internal Server Error response code.",
224 HttpServletResponse.SC_INTERNAL_SERVER_ERROR, response.getResponseCode() );
227 protected void assertResponseConflictError( WebResponse response )
229 assertNotNull( "Should have received a response", response );
230 Assert.assertEquals( "Should have been a 409/Conflict response code.", HttpServletResponse.SC_CONFLICT,
231 response.getResponseCode() );
234 protected ManagedRepositoryConfiguration createManagedRepository( String id, String name, File location,
235 boolean blockRedeployments )
237 ManagedRepositoryConfiguration repo = new ManagedRepositoryConfiguration();
239 repo.setName( name );
240 repo.setLocation( location.getAbsolutePath() );
241 repo.setBlockRedeployments( blockRedeployments );
246 protected ManagedRepositoryConfiguration createManagedRepository( String id, String name, File location,
247 String layout, boolean blockRedeployments )
249 ManagedRepositoryConfiguration repo = createManagedRepository( id, name, location, blockRedeployments );
250 repo.setLayout( layout );
254 protected RemoteRepositoryConfiguration createRemoteRepository( String id, String name, String url )
256 RemoteRepositoryConfiguration repo = new RemoteRepositoryConfiguration();
258 repo.setName( name );
263 protected void saveConfiguration( ArchivaConfiguration archivaConfiguration )
266 archivaConfiguration.save( archivaConfiguration.getConfiguration() );
270 protected void setupCleanRepo( File repoRootDir )
273 FileUtils.deleteDirectory( repoRootDir );
274 if ( !repoRootDir.exists() )
276 repoRootDir.mkdirs();
280 protected void assertManagedFileNotExists( File repoRootInternal, String resourcePath )
282 File repoFile = new File( repoRootInternal, resourcePath );
283 assertFalse( "Managed Repository File <" + repoFile.getAbsolutePath() + "> should not exist.",
287 protected void setupCleanInternalRepo()
290 setupCleanRepo( repoRootInternal );
293 protected File populateRepo( File repoRootManaged, String path, String contents )
296 File destFile = new File( repoRootManaged, path );
297 destFile.getParentFile().mkdirs();
298 FileUtils.writeStringToFile( destFile, contents, Charset.defaultCharset() );