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.commons.io.FileUtils;
30 import org.apache.maven.archiva.configuration.ArchivaConfiguration;
31 import org.apache.maven.archiva.configuration.Configuration;
32 import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration;
33 import org.apache.maven.archiva.configuration.RemoteRepositoryConfiguration;
34 import org.junit.After;
35 import org.junit.Before;
36 import org.junit.runner.RunWith;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
39 import org.springframework.context.ApplicationContext;
40 import org.springframework.test.context.ContextConfiguration;
41 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
43 import javax.inject.Inject;
44 import javax.servlet.http.HttpServletResponse;
46 import java.io.IOException;
49 * AbstractRepositoryServletTestCase
53 @RunWith( SpringJUnit4ClassRunner.class )
54 //ContextConfiguration( locations = { "classpath*:/META-INF/spring-context.xml", "classpath*:/spring-context.xml" } )
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 )
177 ManagedRepositoryConfiguration repository = servlet.getRepository( repoId );
178 assertNotNull( "Archiva Managed Repository id:<" + repoId + "> should exist.", repository );
179 File repoRoot = new File( repository.getLocation() );
180 assertTrue( "Archiva Managed Repository id:<" + repoId + "> should have a valid location on disk.",
181 repoRoot.exists() && repoRoot.isDirectory() );
184 protected void assertResponseOK( WebResponse response )
186 assertNotNull( "Should have recieved a response", response );
187 Assert.assertEquals( "Should have been an OK response code", HttpServletResponse.SC_OK,
188 response.getResponseCode() );
191 protected void assertResponseOK( WebResponse response, String path )
193 assertNotNull( "Should have recieved a response", response );
194 Assert.assertEquals( "Should have been an OK response code for path: " + path, HttpServletResponse.SC_OK,
195 response.getResponseCode() );
198 protected void assertResponseNotFound( WebResponse response )
200 assertNotNull( "Should have recieved a response", response );
201 Assert.assertEquals( "Should have been an 404/Not Found response code.", HttpServletResponse.SC_NOT_FOUND,
202 response.getResponseCode() );
205 protected void assertResponseInternalServerError( WebResponse response )
207 assertNotNull( "Should have recieved a response", response );
208 Assert.assertEquals( "Should have been an 500/Internal Server Error response code.",
209 HttpServletResponse.SC_INTERNAL_SERVER_ERROR, response.getResponseCode() );
212 protected void assertResponseConflictError( WebResponse response )
214 assertNotNull( "Should have received a response", response );
215 Assert.assertEquals( "Should have been a 409/Conflict response code.", HttpServletResponse.SC_CONFLICT,
216 response.getResponseCode() );
219 protected ManagedRepositoryConfiguration createManagedRepository( String id, String name, File location,
220 boolean blockRedeployments )
222 ManagedRepositoryConfiguration repo = new ManagedRepositoryConfiguration();
224 repo.setName( name );
225 repo.setLocation( location.getAbsolutePath() );
226 repo.setBlockRedeployments( blockRedeployments );
231 protected ManagedRepositoryConfiguration createManagedRepository( String id, String name, File location,
232 String layout, boolean blockRedeployments )
234 ManagedRepositoryConfiguration repo = createManagedRepository( id, name, location, blockRedeployments );
235 repo.setLayout( layout );
239 protected RemoteRepositoryConfiguration createRemoteRepository( String id, String name, String url )
241 RemoteRepositoryConfiguration repo = new RemoteRepositoryConfiguration();
243 repo.setName( name );
248 protected void saveConfiguration( ArchivaConfiguration archivaConfiguration )
251 archivaConfiguration.save( archivaConfiguration.getConfiguration() );
255 protected void setupCleanRepo( File repoRootDir )
258 FileUtils.deleteDirectory( repoRootDir );
259 if ( !repoRootDir.exists() )
261 repoRootDir.mkdirs();
265 protected void assertManagedFileNotExists( File repoRootInternal, String resourcePath )
267 File repoFile = new File( repoRootInternal, resourcePath );
268 assertFalse( "Managed Repository File <" + repoFile.getAbsolutePath() + "> should not exist.",
272 protected void setupCleanInternalRepo()
275 setupCleanRepo( repoRootInternal );
278 protected File populateRepo( File repoRootManaged, String path, String contents )
281 File destFile = new File( repoRootManaged, path );
282 destFile.getParentFile().mkdirs();
283 FileUtils.writeStringToFile( destFile, contents, null );