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.commons.io.FileUtils;
31 import org.apache.archiva.configuration.ArchivaConfiguration;
32 import org.apache.archiva.configuration.Configuration;
33 import org.apache.archiva.configuration.ManagedRepositoryConfiguration;
34 import org.apache.archiva.configuration.RemoteRepositoryConfiguration;
35 import org.junit.After;
36 import org.junit.Before;
37 import org.junit.runner.RunWith;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
40 import org.springframework.context.ApplicationContext;
41 import org.springframework.test.context.ContextConfiguration;
42 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
44 import javax.inject.Inject;
45 import javax.servlet.http.HttpServletResponse;
47 import java.io.IOException;
50 * AbstractRepositoryServletTestCase
54 @RunWith( SpringJUnit4ClassRunner.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 ServletUnitClient sc;
65 protected File repoRootInternal;
67 protected File repoRootLegacy;
69 private ServletRunner sr;
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 //archivaConfiguration = (ArchivaConfiguration) lookup( 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 sr = new ServletRunner( new File( "src/test/resources/WEB-INF/web.xml" ) );
130 sr.registerServlet( "/repository/*", UnauthenticatedRepositoryServlet.class.getName() );
136 public void tearDown()
149 if ( repoRootInternal.exists() )
151 FileUtils.deleteDirectory( repoRootInternal );
154 if ( repoRootLegacy.exists() )
156 FileUtils.deleteDirectory( repoRootLegacy );
163 protected void assertFileContents( String expectedContents, File repoRoot, String path )
166 File actualFile = new File( repoRoot, path );
167 assertTrue( "File <" + actualFile.getAbsolutePath() + "> should exist.", actualFile.exists() );
168 assertTrue( "File <" + actualFile.getAbsolutePath() + "> should be a file (not a dir/link/device/etc).",
169 actualFile.isFile() );
171 String actualContents = FileUtils.readFileToString( actualFile, null );
172 assertEquals( "File Contents of <" + actualFile.getAbsolutePath() + ">", expectedContents, actualContents );
175 protected void assertRepositoryValid( RepositoryServlet servlet, String repoId )
178 ManagedRepository repository = servlet.getRepository( repoId );
179 assertNotNull( "Archiva Managed Repository id:<" + repoId + "> should exist.", repository );
180 File repoRoot = new File( repository.getLocation() );
181 assertTrue( "Archiva Managed Repository id:<" + repoId + "> should have a valid location on disk.",
182 repoRoot.exists() && repoRoot.isDirectory() );
185 protected void assertResponseOK( WebResponse response )
187 assertNotNull( "Should have recieved a response", response );
188 Assert.assertEquals( "Should have been an OK response code", HttpServletResponse.SC_OK,
189 response.getResponseCode() );
192 protected void assertResponseOK( WebResponse response, String path )
194 assertNotNull( "Should have recieved a response", response );
195 Assert.assertEquals( "Should have been an OK response code for path: " + path, HttpServletResponse.SC_OK,
196 response.getResponseCode() );
199 protected void assertResponseNotFound( WebResponse response )
201 assertNotNull( "Should have recieved a response", response );
202 Assert.assertEquals( "Should have been an 404/Not Found response code.", HttpServletResponse.SC_NOT_FOUND,
203 response.getResponseCode() );
206 protected void assertResponseInternalServerError( WebResponse response )
208 assertNotNull( "Should have recieved a response", response );
209 Assert.assertEquals( "Should have been an 500/Internal Server Error response code.",
210 HttpServletResponse.SC_INTERNAL_SERVER_ERROR, response.getResponseCode() );
213 protected void assertResponseConflictError( WebResponse response )
215 assertNotNull( "Should have received a response", response );
216 Assert.assertEquals( "Should have been a 409/Conflict response code.", HttpServletResponse.SC_CONFLICT,
217 response.getResponseCode() );
220 protected ManagedRepositoryConfiguration createManagedRepository( String id, String name, File location,
221 boolean blockRedeployments )
223 ManagedRepositoryConfiguration repo = new ManagedRepositoryConfiguration();
225 repo.setName( name );
226 repo.setLocation( location.getAbsolutePath() );
227 repo.setBlockRedeployments( blockRedeployments );
232 protected ManagedRepositoryConfiguration createManagedRepository( String id, String name, File location,
233 String layout, boolean blockRedeployments )
235 ManagedRepositoryConfiguration repo = createManagedRepository( id, name, location, blockRedeployments );
236 repo.setLayout( layout );
240 protected RemoteRepositoryConfiguration createRemoteRepository( String id, String name, String url )
242 RemoteRepositoryConfiguration repo = new RemoteRepositoryConfiguration();
244 repo.setName( name );
249 protected void saveConfiguration( ArchivaConfiguration archivaConfiguration )
252 archivaConfiguration.save( archivaConfiguration.getConfiguration() );
256 protected void setupCleanRepo( File repoRootDir )
259 FileUtils.deleteDirectory( repoRootDir );
260 if ( !repoRootDir.exists() )
262 repoRootDir.mkdirs();
266 protected void assertManagedFileNotExists( File repoRootInternal, String resourcePath )
268 File repoFile = new File( repoRootInternal, resourcePath );
269 assertFalse( "Managed Repository File <" + repoFile.getAbsolutePath() + "> should not exist.",
273 protected void setupCleanInternalRepo()
276 setupCleanRepo( repoRootInternal );
279 protected File populateRepo( File repoRootManaged, String path, String contents )
282 File destFile = new File( repoRootManaged, path );
283 destFile.getParentFile().mkdirs();
284 FileUtils.writeStringToFile( destFile, contents, null );