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.common.plexusbridge.PlexusSisuBridge;
31 import org.apache.commons.io.FileUtils;
32 import org.apache.archiva.configuration.ArchivaConfiguration;
33 import org.apache.archiva.configuration.Configuration;
34 import org.apache.archiva.configuration.ManagedRepositoryConfiguration;
35 import org.apache.archiva.configuration.RemoteRepositoryConfiguration;
36 import org.apache.maven.index.NexusIndexer;
37 import org.apache.maven.index.context.IndexingContext;
38 import org.junit.After;
39 import org.junit.Before;
40 import org.junit.runner.RunWith;
41 import org.slf4j.Logger;
42 import org.slf4j.LoggerFactory;
43 import org.springframework.context.ApplicationContext;
44 import org.springframework.test.context.ContextConfiguration;
45 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
47 import javax.inject.Inject;
48 import javax.servlet.http.HttpServletResponse;
50 import java.io.IOException;
53 * AbstractRepositoryServletTestCase
57 @RunWith( SpringJUnit4ClassRunner.class )
58 @ContextConfiguration( locations = { "classpath*:/repository-servlet-simple.xml" } )
59 public abstract class AbstractRepositoryServletTestCase
62 protected static final String REPOID_INTERNAL = "internal";
64 protected static final String REPOID_LEGACY = "legacy";
66 protected ServletUnitClient sc;
68 protected File repoRootInternal;
70 protected File repoRootLegacy;
72 private ServletRunner sr;
74 protected ArchivaConfiguration archivaConfiguration;
77 protected ApplicationContext applicationContext;
79 protected Logger log = LoggerFactory.getLogger( getClass() );
82 protected void saveConfiguration()
85 saveConfiguration( archivaConfiguration );
95 String appserverBase = new File( "target/appserver-base" ).getAbsolutePath();
96 System.setProperty( "appserver.base", appserverBase );
98 File testConf = new File( "src/test/resources/repository-archiva.xml" );
99 File testConfDest = new File( appserverBase, "conf/archiva.xml" );
100 if ( testConfDest.exists() )
102 FileUtils.deleteQuietly( testConfDest );
104 FileUtils.copyFile( testConf, testConfDest );
106 archivaConfiguration = applicationContext.getBean( ArchivaConfiguration.class );
108 //archivaConfiguration = (ArchivaConfiguration) lookup( ArchivaConfiguration.class );
109 repoRootInternal = new File( appserverBase, "data/repositories/internal" );
110 repoRootLegacy = new File( appserverBase, "data/repositories/legacy" );
111 Configuration config = archivaConfiguration.getConfiguration();
113 config.getManagedRepositories().clear();
115 config.addManagedRepository(
116 createManagedRepository( REPOID_INTERNAL, "Internal Test Repo", repoRootInternal, true ) );
118 config.addManagedRepository(
119 createManagedRepository( REPOID_LEGACY, "Legacy Format Test Repo", repoRootLegacy, "legacy", true ) );
121 config.getProxyConnectors().clear();
123 config.getRemoteRepositories().clear();
125 saveConfiguration( archivaConfiguration );
127 CacheManager.getInstance().clearAll();
129 HttpUnitOptions.setExceptionsThrownOnErrorStatus( false );
131 sr = new ServletRunner( new File( "src/test/resources/WEB-INF/web.xml" ) );
133 sr.registerServlet( "/repository/*", UnauthenticatedRepositoryServlet.class.getName() );
139 public void tearDown()
154 if ( repoRootInternal.exists() )
156 FileUtils.deleteDirectory( repoRootInternal );
159 if ( repoRootLegacy.exists() )
161 FileUtils.deleteDirectory( repoRootLegacy );
168 protected void assertFileContents( String expectedContents, File repoRoot, String path )
171 File actualFile = new File( repoRoot, path );
172 assertTrue( "File <" + actualFile.getAbsolutePath() + "> should exist.", actualFile.exists() );
173 assertTrue( "File <" + actualFile.getAbsolutePath() + "> should be a file (not a dir/link/device/etc).",
174 actualFile.isFile() );
176 String actualContents = FileUtils.readFileToString( actualFile, null );
177 assertEquals( "File Contents of <" + actualFile.getAbsolutePath() + ">", expectedContents, actualContents );
180 protected void assertRepositoryValid( RepositoryServlet servlet, String repoId )
183 ManagedRepository repository = servlet.getRepository( repoId );
184 assertNotNull( "Archiva Managed Repository id:<" + repoId + "> should exist.", repository );
185 File repoRoot = new File( repository.getLocation() );
186 assertTrue( "Archiva Managed Repository id:<" + repoId + "> should have a valid location on disk.",
187 repoRoot.exists() && repoRoot.isDirectory() );
190 protected void assertResponseOK( WebResponse response )
192 assertNotNull( "Should have recieved a response", response );
193 Assert.assertEquals( "Should have been an OK response code", HttpServletResponse.SC_OK,
194 response.getResponseCode() );
197 protected void assertResponseOK( WebResponse response, String path )
199 assertNotNull( "Should have recieved a response", response );
200 Assert.assertEquals( "Should have been an OK response code for path: " + path, HttpServletResponse.SC_OK,
201 response.getResponseCode() );
204 protected void assertResponseNotFound( WebResponse response )
206 assertNotNull( "Should have recieved a response", response );
207 Assert.assertEquals( "Should have been an 404/Not Found response code.", HttpServletResponse.SC_NOT_FOUND,
208 response.getResponseCode() );
211 protected void assertResponseInternalServerError( WebResponse response )
213 assertNotNull( "Should have recieved a response", response );
214 Assert.assertEquals( "Should have been an 500/Internal Server Error response code.",
215 HttpServletResponse.SC_INTERNAL_SERVER_ERROR, response.getResponseCode() );
218 protected void assertResponseConflictError( WebResponse response )
220 assertNotNull( "Should have received a response", response );
221 Assert.assertEquals( "Should have been a 409/Conflict response code.", HttpServletResponse.SC_CONFLICT,
222 response.getResponseCode() );
225 protected ManagedRepositoryConfiguration createManagedRepository( String id, String name, File location,
226 boolean blockRedeployments )
228 ManagedRepositoryConfiguration repo = new ManagedRepositoryConfiguration();
230 repo.setName( name );
231 repo.setLocation( location.getAbsolutePath() );
232 repo.setBlockRedeployments( blockRedeployments );
237 protected ManagedRepositoryConfiguration createManagedRepository( String id, String name, File location,
238 String layout, boolean blockRedeployments )
240 ManagedRepositoryConfiguration repo = createManagedRepository( id, name, location, blockRedeployments );
241 repo.setLayout( layout );
245 protected RemoteRepositoryConfiguration createRemoteRepository( String id, String name, String url )
247 RemoteRepositoryConfiguration repo = new RemoteRepositoryConfiguration();
249 repo.setName( name );
254 protected void saveConfiguration( ArchivaConfiguration archivaConfiguration )
257 archivaConfiguration.save( archivaConfiguration.getConfiguration() );
261 protected void setupCleanRepo( File repoRootDir )
264 FileUtils.deleteDirectory( repoRootDir );
265 if ( !repoRootDir.exists() )
267 repoRootDir.mkdirs();
271 protected void assertManagedFileNotExists( File repoRootInternal, String resourcePath )
273 File repoFile = new File( repoRootInternal, resourcePath );
274 assertFalse( "Managed Repository File <" + repoFile.getAbsolutePath() + "> should not exist.",
278 protected void setupCleanInternalRepo()
281 setupCleanRepo( repoRootInternal );
284 protected File populateRepo( File repoRootManaged, String path, String contents )
287 File destFile = new File( repoRootManaged, path );
288 destFile.getParentFile().mkdirs();
289 FileUtils.writeStringToFile( destFile, contents, null );