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 junit.framework.TestCase;
23 import org.apache.archiva.common.filelock.FileLockManager;
24 import org.apache.archiva.common.utils.FileUtils;
25 import org.apache.archiva.repository.content.LayoutException;
26 import org.apache.archiva.repository.RepositoryRegistry;
27 import org.apache.archiva.repository.storage.fs.FilesystemAsset;
28 import org.apache.archiva.metadata.audit.AuditListener;
29 import org.apache.archiva.repository.maven.MavenManagedRepository;
30 import org.apache.archiva.test.utils.ArchivaSpringJUnit4ClassRunner;
31 import org.apache.archiva.webdav.util.MimeTypes;
32 import org.apache.commons.lang3.StringUtils;
33 import org.apache.jackrabbit.webdav.DavException;
34 import org.apache.jackrabbit.webdav.DavResource;
35 import org.apache.jackrabbit.webdav.DavResourceFactory;
36 import org.apache.jackrabbit.webdav.DavResourceLocator;
37 import org.apache.jackrabbit.webdav.DavServletRequest;
38 import org.apache.jackrabbit.webdav.DavServletResponse;
39 import org.apache.jackrabbit.webdav.DavSession;
40 import org.apache.jackrabbit.webdav.lock.ActiveLock;
41 import org.apache.jackrabbit.webdav.lock.LockInfo;
42 import org.apache.jackrabbit.webdav.lock.LockManager;
43 import org.apache.jackrabbit.webdav.lock.Scope;
44 import org.apache.jackrabbit.webdav.lock.SimpleLockManager;
45 import org.apache.jackrabbit.webdav.lock.Type;
46 import org.junit.After;
47 import org.junit.Before;
48 import org.junit.Test;
49 import org.junit.runner.RunWith;
50 import org.springframework.test.context.ContextConfiguration;
52 import javax.inject.Inject;
53 import javax.servlet.http.HttpServletResponse;
54 import java.nio.file.Files;
55 import java.nio.file.Path;
56 import java.nio.file.Paths;
57 import java.util.Collections;
59 @RunWith( ArchivaSpringJUnit4ClassRunner.class )
60 @ContextConfiguration( locations = { "classpath*:/META-INF/spring-context.xml", "classpath*:/spring-context.xml" } )
61 public class DavResourceTest
64 private DavSession session;
67 private MimeTypes mimeTypes;
70 private FileLockManager fileLockManager;
73 private RepositoryRegistry repositoryRegistry;
75 private ArchivaDavResourceLocator resourceLocator;
77 private DavResourceFactory resourceFactory;
81 private final String REPOPATH = "myresource.jar";
83 private Path myResource;
85 private DavResource resource;
87 private LockManager lockManager;
89 private MavenManagedRepository repository;
96 session = new ArchivaDavSession();
97 baseDir = Paths.get( "target/DavResourceTest" );
98 Files.createDirectories( baseDir );
99 Files.createDirectories( baseDir.resolve( "conf" ) );
100 repository = MavenManagedRepository.newLocalInstance( "repo001", "repo001", baseDir);
101 repositoryRegistry.putRepository( repository );
103 myResource = baseDir.resolve( "myresource.jar" );
104 Files.createFile(myResource);
105 resourceFactory = new RootContextDavResourceFactory();
108 (ArchivaDavResourceLocator) new ArchivaDavLocatorFactory().createResourceLocator( "/", REPOPATH );
109 resource = getDavResource( resourceLocator.getHref( false ), myResource );
110 lockManager = new SimpleLockManager();
111 resource.addLockManager( lockManager );
116 public void tearDown()
120 org.apache.archiva.common.utils.FileUtils.deleteDirectory( baseDir );
121 String appserverBase = System.getProperty( "appserver.base" );
122 if ( StringUtils.isNotEmpty( appserverBase ) )
124 FileUtils.deleteDirectory( Paths.get( appserverBase ) );
128 private DavResource getDavResource( String logicalPath, Path file ) throws LayoutException
130 return new ArchivaDavResource( new FilesystemAsset( repository, logicalPath, file.toAbsolutePath()) , logicalPath, repository, session, resourceLocator,
131 resourceFactory, mimeTypes, Collections.<AuditListener> emptyList(), null);
135 public void testDeleteNonExistantResourceShould404()
138 Path dir = baseDir.resolve( "testdir" );
141 DavResource directoryResource = getDavResource( "/testdir", dir );
142 directoryResource.getCollection().removeMember( directoryResource );
143 fail( "Did not throw DavException" );
145 catch ( DavException e )
147 assertEquals( DavServletResponse.SC_NOT_FOUND, e.getErrorCode() );
152 public void testDeleteCollection()
155 Path dir = baseDir.resolve( "testdir" );
158 assertNotNull( Files.createDirectories(dir) );
159 DavResource directoryResource = getDavResource( "/testdir", dir );
160 directoryResource.getCollection().removeMember( directoryResource );
161 assertFalse( Files.exists(dir) );
165 org.apache.archiva.common.utils.FileUtils.deleteDirectory( dir );
170 public void testDeleteResource()
173 assertTrue( Files.exists(myResource) );
174 resource.getCollection().removeMember( resource );
175 assertFalse( Files.exists(myResource) );
179 public void testIsLockable()
181 assertTrue( resource.isLockable( Type.WRITE, Scope.EXCLUSIVE ) );
182 assertFalse( resource.isLockable( Type.WRITE, Scope.SHARED ) );
186 public void testLock()
189 assertEquals( 0, resource.getLocks().length );
191 LockInfo info = new LockInfo( Scope.EXCLUSIVE, Type.WRITE, "/", 0, false );
192 lockManager.createLock( info, resource );
194 assertEquals( 1, resource.getLocks().length );
198 public void testLockIfResourceUnlockable()
201 assertEquals( 0, resource.getLocks().length );
203 LockInfo info = new LockInfo( Scope.SHARED, Type.WRITE, "/", 0, false );
206 lockManager.createLock( info, resource );
207 fail( "Did not throw dav exception" );
209 catch ( Exception e )
211 // Simple lock manager will die
213 assertEquals( 0, resource.getLocks().length );
217 public void testGetLock()
220 LockInfo info = new LockInfo( Scope.EXCLUSIVE, Type.WRITE, "/", 0, false );
221 lockManager.createLock( info, resource );
223 assertEquals( 1, resource.getLocks().length );
226 assertNotNull( resource.getLock( Type.WRITE, Scope.EXCLUSIVE ) );
228 // Lock should not exist
229 assertNull( resource.getLock( Type.WRITE, Scope.SHARED ) );
233 public void testRefreshLockThrowsExceptionIfNoLockIsPresent()
236 LockInfo info = new LockInfo( Scope.EXCLUSIVE, Type.WRITE, "/", 0, false );
238 assertEquals( 0, resource.getLocks().length );
242 lockManager.refreshLock( info, "notoken", resource );
243 fail( "Did not throw dav exception" );
245 catch ( DavException e )
247 assertEquals( DavServletResponse.SC_PRECONDITION_FAILED, e.getErrorCode() );
250 assertEquals( 0, resource.getLocks().length );
254 public void testRefreshLock()
257 LockInfo info = new LockInfo( Scope.EXCLUSIVE, Type.WRITE, "/", 0, false );
259 assertEquals( 0, resource.getLocks().length );
261 lockManager.createLock( info, resource );
263 assertEquals( 1, resource.getLocks().length );
265 ActiveLock lock = resource.getLocks()[0];
267 lockManager.refreshLock( info, lock.getToken(), resource );
269 assertEquals( 1, resource.getLocks().length );
273 public void testUnlock()
276 LockInfo info = new LockInfo( Scope.EXCLUSIVE, Type.WRITE, "/", 0, false );
278 assertEquals( 0, resource.getLocks().length );
280 lockManager.createLock( info, resource );
282 assertEquals( 1, resource.getLocks().length );
284 ActiveLock lock = resource.getLocks()[0];
286 lockManager.releaseLock( lock.getToken(), resource );
288 assertEquals( 0, resource.getLocks().length );
292 public void testUnlockThrowsDavExceptionIfNotLocked()
295 LockInfo info = new LockInfo( Scope.EXCLUSIVE, Type.WRITE, "/", 0, false );
297 assertEquals( 0, resource.getLocks().length );
299 lockManager.createLock( info, resource );
301 assertEquals( 1, resource.getLocks().length );
305 lockManager.releaseLock( "BLAH", resource );
306 fail( "Did not throw DavException" );
308 catch ( DavException e )
310 assertEquals( DavServletResponse.SC_LOCKED, e.getErrorCode() );
313 assertEquals( 1, resource.getLocks().length );
317 public void testUnlockThrowsDavExceptionIfResourceNotLocked()
320 assertEquals( 0, resource.getLocks().length );
324 lockManager.releaseLock( "BLAH", resource );
325 fail( "Did not throw DavException" );
327 catch ( DavException e )
329 assertEquals( DavServletResponse.SC_PRECONDITION_FAILED, e.getErrorCode() );
332 assertEquals( 0, resource.getLocks().length );
335 private class RootContextDavResourceFactory
336 implements DavResourceFactory
339 public DavResource createResource( DavResourceLocator locator, DavServletRequest request,
340 DavServletResponse response )
343 throw new UnsupportedOperationException( "Not supported yet." );
347 public DavResource createResource( DavResourceLocator locator, DavSession session )
352 return new ArchivaDavResource( new FilesystemAsset(repository, "/" , baseDir.toAbsolutePath()), "/", repository, session, resourceLocator,
353 resourceFactory, mimeTypes, Collections.<AuditListener> emptyList(),
356 catch ( LayoutException e )
358 throw new DavException( HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e );