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.webdav.util.MavenIndexerCleaner;
35 import org.apache.commons.io.FileUtils;
36 import org.junit.After;
37 import org.junit.Before;
38 import org.junit.runner.RunWith;
39 import org.slf4j.Logger;
40 import org.slf4j.LoggerFactory;
41 import org.springframework.context.ApplicationContext;
42 import org.springframework.test.context.ContextConfiguration;
44 import javax.inject.Inject;
45 import javax.servlet.http.HttpServletResponse;
47 import java.io.IOException;
48 import org.apache.archiva.test.utils.ArchivaSpringJUnit4ClassRunner;
51 * AbstractRepositoryServletTestCase
55 @RunWith( ArchivaSpringJUnit4ClassRunner.class )
56 @ContextConfiguration( locations = { "classpath*:/repository-servlet-simple.xml" } )
57 public abstract class AbstractRepositoryServletTestCase
60 protected static final String REPOID_INTERNAL = "internal";
62 protected static final String REPOID_LEGACY = "legacy";
64 protected File repoRootInternal;
66 protected File repoRootLegacy;
68 protected ServletUnitClient servletUnitClient;
70 private ServletRunner servletRunner;
72 protected ArchivaConfiguration archivaConfiguration;
75 protected ApplicationContext applicationContext;
77 protected Logger log = LoggerFactory.getLogger( getClass() );
80 protected void saveConfiguration()
83 saveConfiguration( archivaConfiguration );
93 String appserverBase = new File( "target/appserver-base" ).getAbsolutePath();
94 System.setProperty( "appserver.base", appserverBase );
96 File testConf = new File( "src/test/resources/repository-archiva.xml" );
97 File testConfDest = new File( appserverBase, "conf/archiva.xml" );
98 if ( testConfDest.exists() )
100 FileUtils.deleteQuietly( testConfDest );
102 FileUtils.copyFile( testConf, testConfDest );
104 archivaConfiguration = applicationContext.getBean( ArchivaConfiguration.class );
106 repoRootInternal = new File( appserverBase, "data/repositories/internal" );
107 repoRootLegacy = new File( appserverBase, "data/repositories/legacy" );
108 Configuration config = archivaConfiguration.getConfiguration();
110 config.getManagedRepositories().clear();
112 config.addManagedRepository(
113 createManagedRepository( REPOID_INTERNAL, "Internal Test Repo", repoRootInternal, true ) );
115 config.addManagedRepository(
116 createManagedRepository( REPOID_LEGACY, "Legacy Format Test Repo", repoRootLegacy, "legacy", true ) );
118 config.getProxyConnectors().clear();
120 config.getRemoteRepositories().clear();
122 saveConfiguration( archivaConfiguration );
124 CacheManager.getInstance().clearAll();
126 HttpUnitOptions.setExceptionsThrownOnErrorStatus( false );
128 applicationContext.getBean( MavenIndexerCleaner.class ).cleanupIndex();
132 protected ServletUnitClient getServletUnitClient()
135 if ( servletUnitClient != null )
137 return servletUnitClient;
139 servletRunner = new ServletRunner( new File( "src/test/resources/WEB-INF/web.xml" ) );
141 servletRunner.registerServlet( "/repository/*", UnauthenticatedRepositoryServlet.class.getName() );
143 servletUnitClient = servletRunner.newClient();
145 return servletUnitClient;
150 public void tearDown()
154 if ( servletUnitClient != null )
156 servletUnitClient.clearContents();
159 if ( servletRunner != null )
161 servletRunner.shutDown();
164 if ( repoRootInternal.exists() )
166 FileUtils.deleteDirectory( repoRootInternal );
169 if ( repoRootLegacy.exists() )
171 FileUtils.deleteDirectory( repoRootLegacy );
178 protected void assertFileContents( String expectedContents, File repoRoot, String path )
181 File actualFile = new File( repoRoot, path );
182 assertTrue( "File <" + actualFile.getAbsolutePath() + "> should exist.", actualFile.exists() );
183 assertTrue( "File <" + actualFile.getAbsolutePath() + "> should be a file (not a dir/link/device/etc).",
184 actualFile.isFile() );
186 String actualContents = FileUtils.readFileToString( actualFile, null );
187 assertEquals( "File Contents of <" + actualFile.getAbsolutePath() + ">", expectedContents, actualContents );
190 protected void assertRepositoryValid( RepositoryServlet servlet, String repoId )
193 ManagedRepository repository = servlet.getRepository( repoId );
194 assertNotNull( "Archiva Managed Repository id:<" + repoId + "> should exist.", repository );
195 File repoRoot = new File( repository.getLocation() );
196 assertTrue( "Archiva Managed Repository id:<" + repoId + "> should have a valid location on disk.",
197 repoRoot.exists() && repoRoot.isDirectory() );
200 protected void assertResponseOK( WebResponse response )
202 assertNotNull( "Should have recieved a response", response );
203 Assert.assertEquals( "Should have been an OK response code", HttpServletResponse.SC_OK,
204 response.getResponseCode() );
207 protected void assertResponseOK( WebResponse response, String path )
209 assertNotNull( "Should have recieved a response", response );
210 Assert.assertEquals( "Should have been an OK response code for path: " + path, HttpServletResponse.SC_OK,
211 response.getResponseCode() );
214 protected void assertResponseNotFound( WebResponse response )
216 assertNotNull( "Should have recieved a response", response );
217 Assert.assertEquals( "Should have been an 404/Not Found response code.", HttpServletResponse.SC_NOT_FOUND,
218 response.getResponseCode() );
221 protected void assertResponseInternalServerError( WebResponse response )
223 assertNotNull( "Should have recieved a response", response );
224 Assert.assertEquals( "Should have been an 500/Internal Server Error response code.",
225 HttpServletResponse.SC_INTERNAL_SERVER_ERROR, response.getResponseCode() );
228 protected void assertResponseConflictError( WebResponse response )
230 assertNotNull( "Should have received a response", response );
231 Assert.assertEquals( "Should have been a 409/Conflict response code.", HttpServletResponse.SC_CONFLICT,
232 response.getResponseCode() );
235 protected ManagedRepositoryConfiguration createManagedRepository( String id, String name, File location,
236 boolean blockRedeployments )
238 ManagedRepositoryConfiguration repo = new ManagedRepositoryConfiguration();
240 repo.setName( name );
241 repo.setLocation( location.getAbsolutePath() );
242 repo.setBlockRedeployments( blockRedeployments );
247 protected ManagedRepositoryConfiguration createManagedRepository( String id, String name, File location,
248 String layout, boolean blockRedeployments )
250 ManagedRepositoryConfiguration repo = createManagedRepository( id, name, location, blockRedeployments );
251 repo.setLayout( layout );
255 protected RemoteRepositoryConfiguration createRemoteRepository( String id, String name, String url )
257 RemoteRepositoryConfiguration repo = new RemoteRepositoryConfiguration();
259 repo.setName( name );
264 protected void saveConfiguration( ArchivaConfiguration archivaConfiguration )
267 archivaConfiguration.save( archivaConfiguration.getConfiguration() );
271 protected void setupCleanRepo( File repoRootDir )
274 FileUtils.deleteDirectory( repoRootDir );
275 if ( !repoRootDir.exists() )
277 repoRootDir.mkdirs();
281 protected void assertManagedFileNotExists( File repoRootInternal, String resourcePath )
283 File repoFile = new File( repoRootInternal, resourcePath );
284 assertFalse( "Managed Repository File <" + repoFile.getAbsolutePath() + "> should not exist.",
288 protected void setupCleanInternalRepo()
291 setupCleanRepo( repoRootInternal );
294 protected File populateRepo( File repoRootManaged, String path, String contents )
297 File destFile = new File( repoRootManaged, path );
298 destFile.getParentFile().mkdirs();
299 FileUtils.writeStringToFile( destFile, contents, null );