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.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.repository.managed.ManagedRepository;
30 import org.apache.commons.io.FileUtils;
31 import org.apache.maven.archiva.configuration.ArchivaConfiguration;
32 import org.apache.maven.archiva.configuration.Configuration;
33 import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration;
34 import org.apache.maven.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*:/META-INF/spring-context.xml", "classpath*:/spring-context.xml" } )
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 ServletUnitClient sc;
66 protected File repoRootInternal;
68 protected File repoRootLegacy;
70 private ServletRunner sr;
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 //archivaConfiguration = (ArchivaConfiguration) lookup( ArchivaConfiguration.class );
107 repoRootInternal = new File( appserverBase, "data/repositories/internal" );
108 repoRootLegacy = new File( appserverBase, "data/repositories/legacy" );
109 Configuration config = archivaConfiguration.getConfiguration();
111 config.getManagedRepositories().clear();
113 config.addManagedRepository(
114 createManagedRepository( REPOID_INTERNAL, "Internal Test Repo", repoRootInternal, true ) );
116 config.addManagedRepository(
117 createManagedRepository( REPOID_LEGACY, "Legacy Format Test Repo", repoRootLegacy, "legacy", true ) );
119 config.getProxyConnectors().clear();
121 config.getRemoteRepositories().clear();
123 saveConfiguration( archivaConfiguration );
125 CacheManager.getInstance().clearAll();
127 HttpUnitOptions.setExceptionsThrownOnErrorStatus( false );
129 sr = new ServletRunner( new File( "src/test/resources/WEB-INF/web.xml" ) );
131 sr.registerServlet( "/repository/*", UnauthenticatedRepositoryServlet.class.getName() );
137 public void tearDown()
150 if ( repoRootInternal.exists() )
152 FileUtils.deleteDirectory( repoRootInternal );
155 if ( repoRootLegacy.exists() )
157 FileUtils.deleteDirectory( repoRootLegacy );
164 protected void assertFileContents( String expectedContents, File repoRoot, String path )
167 File actualFile = new File( repoRoot, path );
168 assertTrue( "File <" + actualFile.getAbsolutePath() + "> should exist.", actualFile.exists() );
169 assertTrue( "File <" + actualFile.getAbsolutePath() + "> should be a file (not a dir/link/device/etc).",
170 actualFile.isFile() );
172 String actualContents = FileUtils.readFileToString( actualFile, null );
173 assertEquals( "File Contents of <" + actualFile.getAbsolutePath() + ">", expectedContents, actualContents );
176 protected void assertRepositoryValid( RepositoryServlet servlet, String repoId )
179 ManagedRepository repository = servlet.getRepository( repoId );
180 assertNotNull( "Archiva Managed Repository id:<" + repoId + "> should exist.", repository );
181 File repoRoot = new File( repository.getLocation() );
182 assertTrue( "Archiva Managed Repository id:<" + repoId + "> should have a valid location on disk.",
183 repoRoot.exists() && repoRoot.isDirectory() );
186 protected void assertResponseOK( WebResponse response )
188 assertNotNull( "Should have recieved a response", response );
189 Assert.assertEquals( "Should have been an OK response code", HttpServletResponse.SC_OK,
190 response.getResponseCode() );
193 protected void assertResponseOK( WebResponse response, String path )
195 assertNotNull( "Should have recieved a response", response );
196 Assert.assertEquals( "Should have been an OK response code for path: " + path, HttpServletResponse.SC_OK,
197 response.getResponseCode() );
200 protected void assertResponseNotFound( WebResponse response )
202 assertNotNull( "Should have recieved a response", response );
203 Assert.assertEquals( "Should have been an 404/Not Found response code.", HttpServletResponse.SC_NOT_FOUND,
204 response.getResponseCode() );
207 protected void assertResponseInternalServerError( WebResponse response )
209 assertNotNull( "Should have recieved a response", response );
210 Assert.assertEquals( "Should have been an 500/Internal Server Error response code.",
211 HttpServletResponse.SC_INTERNAL_SERVER_ERROR, response.getResponseCode() );
214 protected void assertResponseConflictError( WebResponse response )
216 assertNotNull( "Should have received a response", response );
217 Assert.assertEquals( "Should have been a 409/Conflict response code.", HttpServletResponse.SC_CONFLICT,
218 response.getResponseCode() );
221 protected ManagedRepositoryConfiguration createManagedRepository( String id, String name, File location,
222 boolean blockRedeployments )
224 ManagedRepositoryConfiguration repo = new ManagedRepositoryConfiguration();
226 repo.setName( name );
227 repo.setLocation( location.getAbsolutePath() );
228 repo.setBlockRedeployments( blockRedeployments );
233 protected ManagedRepositoryConfiguration createManagedRepository( String id, String name, File location,
234 String layout, boolean blockRedeployments )
236 ManagedRepositoryConfiguration repo = createManagedRepository( id, name, location, blockRedeployments );
237 repo.setLayout( layout );
241 protected RemoteRepositoryConfiguration createRemoteRepository( String id, String name, String url )
243 RemoteRepositoryConfiguration repo = new RemoteRepositoryConfiguration();
245 repo.setName( name );
250 protected void saveConfiguration( ArchivaConfiguration archivaConfiguration )
253 archivaConfiguration.save( archivaConfiguration.getConfiguration() );
257 protected void setupCleanRepo( File repoRootDir )
260 FileUtils.deleteDirectory( repoRootDir );
261 if ( !repoRootDir.exists() )
263 repoRootDir.mkdirs();
267 protected void assertManagedFileNotExists( File repoRootInternal, String resourcePath )
269 File repoFile = new File( repoRootInternal, resourcePath );
270 assertFalse( "Managed Repository File <" + repoFile.getAbsolutePath() + "> should not exist.",
274 protected void setupCleanInternalRepo()
277 setupCleanRepo( repoRootInternal );
280 protected File populateRepo( File repoRootManaged, String path, String contents )
283 File destFile = new File( repoRootManaged, path );
284 destFile.getParentFile().mkdirs();
285 FileUtils.writeStringToFile( destFile, contents, null );