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.apache.lucene.store.Lock;
37 import org.apache.lucene.store.LockReleaseFailedException;
38 import org.apache.lucene.store.NativeFSLockFactory;
39 import org.junit.After;
40 import org.junit.Before;
41 import org.junit.BeforeClass;
42 import org.junit.runner.RunWith;
43 import org.slf4j.Logger;
44 import org.slf4j.LoggerFactory;
45 import org.springframework.context.ApplicationContext;
46 import org.springframework.test.context.ContextConfiguration;
47 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
49 import javax.inject.Inject;
50 import javax.servlet.http.HttpServletResponse;
52 import java.io.IOException;
53 import java.lang.reflect.Field;
54 import java.lang.reflect.Method;
57 * AbstractRepositoryServletTestCase
61 @RunWith( SpringJUnit4ClassRunner.class )
62 @ContextConfiguration( locations = { "classpath*:/repository-servlet-simple.xml" } )
63 public abstract class AbstractRepositoryServletTestCase
66 protected static final String REPOID_INTERNAL = "internal";
68 protected static final String REPOID_LEGACY = "legacy";
70 protected ServletUnitClient sc;
72 protected File repoRootInternal;
74 protected File repoRootLegacy;
76 private ServletRunner sr;
78 protected ArchivaConfiguration archivaConfiguration;
81 protected ApplicationContext applicationContext;
83 protected Logger log = LoggerFactory.getLogger( getClass() );
86 protected void saveConfiguration()
89 saveConfiguration( archivaConfiguration );
93 public static void lockCleanup()
102 Field locks = NativeFSLockFactory.class.getDeclaredClasses()[0].getField( "LOCK_HELD" );
103 locks.setAccessible( true );
104 Method clear = locks.getClass().getMethod( "clear" );
106 clear.invoke( field, null );
110 NativeFSLockFactory nativeFSLockFactory =
111 new NativeFSLockFactory( new File( "target/appserver-base/data/repositories/internal/.indexer" ) );
113 Lock lock = nativeFSLockFactory.makeLock( "write.lock" );
115 LoggerFactory.getLogger( AbstractRepositoryServletTestCase.class ).info( "cleanup lock" );
119 catch ( LockReleaseFailedException e )
135 String appserverBase = new File( "target/appserver-base" ).getAbsolutePath();
136 System.setProperty( "appserver.base", appserverBase );
138 File testConf = new File( "src/test/resources/repository-archiva.xml" );
139 File testConfDest = new File( appserverBase, "conf/archiva.xml" );
140 if ( testConfDest.exists() )
142 FileUtils.deleteQuietly( testConfDest );
144 FileUtils.copyFile( testConf, testConfDest );
146 archivaConfiguration = applicationContext.getBean( ArchivaConfiguration.class );
148 repoRootInternal = new File( appserverBase, "data/repositories/internal" );
149 repoRootLegacy = new File( appserverBase, "data/repositories/legacy" );
150 Configuration config = archivaConfiguration.getConfiguration();
152 config.getManagedRepositories().clear();
154 config.addManagedRepository(
155 createManagedRepository( REPOID_INTERNAL, "Internal Test Repo", repoRootInternal, true ) );
157 config.addManagedRepository(
158 createManagedRepository( REPOID_LEGACY, "Legacy Format Test Repo", repoRootLegacy, "legacy", true ) );
160 config.getProxyConnectors().clear();
162 config.getRemoteRepositories().clear();
164 saveConfiguration( archivaConfiguration );
166 CacheManager.getInstance().clearAll();
168 HttpUnitOptions.setExceptionsThrownOnErrorStatus( false );
170 applicationContext.getBean( MavenIndexerCleaner.class ).cleanupIndex();
172 sr = new ServletRunner( new File( "src/test/resources/WEB-INF/web.xml" ) );
174 sr.registerServlet( "/repository/*", UnauthenticatedRepositoryServlet.class.getName() );
180 public void tearDown()
194 if ( repoRootInternal.exists() )
196 FileUtils.deleteDirectory( repoRootInternal );
199 if ( repoRootLegacy.exists() )
201 FileUtils.deleteDirectory( repoRootLegacy );
208 protected void assertFileContents( String expectedContents, File repoRoot, String path )
211 File actualFile = new File( repoRoot, path );
212 assertTrue( "File <" + actualFile.getAbsolutePath() + "> should exist.", actualFile.exists() );
213 assertTrue( "File <" + actualFile.getAbsolutePath() + "> should be a file (not a dir/link/device/etc).",
214 actualFile.isFile() );
216 String actualContents = FileUtils.readFileToString( actualFile, null );
217 assertEquals( "File Contents of <" + actualFile.getAbsolutePath() + ">", expectedContents, actualContents );
220 protected void assertRepositoryValid( RepositoryServlet servlet, String repoId )
223 ManagedRepository repository = servlet.getRepository( repoId );
224 assertNotNull( "Archiva Managed Repository id:<" + repoId + "> should exist.", repository );
225 File repoRoot = new File( repository.getLocation() );
226 assertTrue( "Archiva Managed Repository id:<" + repoId + "> should have a valid location on disk.",
227 repoRoot.exists() && repoRoot.isDirectory() );
230 protected void assertResponseOK( WebResponse response )
232 assertNotNull( "Should have recieved a response", response );
233 Assert.assertEquals( "Should have been an OK response code", HttpServletResponse.SC_OK,
234 response.getResponseCode() );
237 protected void assertResponseOK( WebResponse response, String path )
239 assertNotNull( "Should have recieved a response", response );
240 Assert.assertEquals( "Should have been an OK response code for path: " + path, HttpServletResponse.SC_OK,
241 response.getResponseCode() );
244 protected void assertResponseNotFound( WebResponse response )
246 assertNotNull( "Should have recieved a response", response );
247 Assert.assertEquals( "Should have been an 404/Not Found response code.", HttpServletResponse.SC_NOT_FOUND,
248 response.getResponseCode() );
251 protected void assertResponseInternalServerError( WebResponse response )
253 assertNotNull( "Should have recieved a response", response );
254 Assert.assertEquals( "Should have been an 500/Internal Server Error response code.",
255 HttpServletResponse.SC_INTERNAL_SERVER_ERROR, response.getResponseCode() );
258 protected void assertResponseConflictError( WebResponse response )
260 assertNotNull( "Should have received a response", response );
261 Assert.assertEquals( "Should have been a 409/Conflict response code.", HttpServletResponse.SC_CONFLICT,
262 response.getResponseCode() );
265 protected ManagedRepositoryConfiguration createManagedRepository( String id, String name, File location,
266 boolean blockRedeployments )
268 ManagedRepositoryConfiguration repo = new ManagedRepositoryConfiguration();
270 repo.setName( name );
271 repo.setLocation( location.getAbsolutePath() );
272 repo.setBlockRedeployments( blockRedeployments );
277 protected ManagedRepositoryConfiguration createManagedRepository( String id, String name, File location,
278 String layout, boolean blockRedeployments )
280 ManagedRepositoryConfiguration repo = createManagedRepository( id, name, location, blockRedeployments );
281 repo.setLayout( layout );
285 protected RemoteRepositoryConfiguration createRemoteRepository( String id, String name, String url )
287 RemoteRepositoryConfiguration repo = new RemoteRepositoryConfiguration();
289 repo.setName( name );
294 protected void saveConfiguration( ArchivaConfiguration archivaConfiguration )
297 archivaConfiguration.save( archivaConfiguration.getConfiguration() );
301 protected void setupCleanRepo( File repoRootDir )
304 FileUtils.deleteDirectory( repoRootDir );
305 if ( !repoRootDir.exists() )
307 repoRootDir.mkdirs();
311 protected void assertManagedFileNotExists( File repoRootInternal, String resourcePath )
313 File repoFile = new File( repoRootInternal, resourcePath );
314 assertFalse( "Managed Repository File <" + repoFile.getAbsolutePath() + "> should not exist.",
318 protected void setupCleanInternalRepo()
321 setupCleanRepo( repoRootInternal );
324 protected File populateRepo( File repoRootManaged, String path, String contents )
327 File destFile = new File( repoRootManaged, path );
328 destFile.getParentFile().mkdirs();
329 FileUtils.writeStringToFile( destFile, contents, null );