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.repository.storage.fs.FilesystemStorage;
31 import org.apache.archiva.test.utils.ArchivaSpringJUnit4ClassRunner;
32 import org.apache.archiva.webdav.util.MimeTypes;
33 import org.apache.commons.lang3.StringUtils;
34 import org.apache.jackrabbit.webdav.DavException;
35 import org.apache.jackrabbit.webdav.DavResource;
36 import org.apache.jackrabbit.webdav.DavResourceFactory;
37 import org.apache.jackrabbit.webdav.DavResourceLocator;
38 import org.apache.jackrabbit.webdav.DavServletRequest;
39 import org.apache.jackrabbit.webdav.DavServletResponse;
40 import org.apache.jackrabbit.webdav.DavSession;
41 import org.apache.jackrabbit.webdav.lock.ActiveLock;
42 import org.apache.jackrabbit.webdav.lock.LockInfo;
43 import org.apache.jackrabbit.webdav.lock.LockManager;
44 import org.apache.jackrabbit.webdav.lock.Scope;
45 import org.apache.jackrabbit.webdav.lock.SimpleLockManager;
46 import org.apache.jackrabbit.webdav.lock.Type;
47 import org.junit.After;
48 import org.junit.Before;
49 import org.junit.Test;
50 import org.junit.runner.RunWith;
51 import org.springframework.test.context.ContextConfiguration;
53 import javax.inject.Inject;
54 import javax.servlet.http.HttpServletResponse;
55 import java.nio.file.Files;
56 import java.nio.file.Path;
57 import java.nio.file.Paths;
58 import java.util.Collections;
60 @RunWith( ArchivaSpringJUnit4ClassRunner.class )
61 @ContextConfiguration( locations = { "classpath*:/META-INF/spring-context.xml", "classpath*:/spring-context.xml" } )
62 public class DavResourceTest
65 private DavSession session;
68 private MimeTypes mimeTypes;
71 private FileLockManager fileLockManager;
74 private RepositoryRegistry repositoryRegistry;
76 private ArchivaDavResourceLocator resourceLocator;
78 private DavResourceFactory resourceFactory;
82 private final String REPOPATH = "myresource.jar";
84 private Path myResource;
86 private DavResource resource;
88 private LockManager lockManager;
90 private MavenManagedRepository repository;
97 session = new ArchivaDavSession();
98 baseDir = Paths.get( "target/DavResourceTest" );
99 Files.createDirectories( baseDir );
100 Files.createDirectories( baseDir.resolve( "conf" ) );
101 repository = MavenManagedRepository.newLocalInstance( "repo001", "repo001", baseDir);
102 repositoryRegistry.putRepository( repository );
104 myResource = baseDir.resolve( "myresource.jar" );
105 Files.createFile(myResource);
106 resourceFactory = new RootContextDavResourceFactory();
109 (ArchivaDavResourceLocator) new ArchivaDavLocatorFactory().createResourceLocator( "/", REPOPATH );
110 resource = getDavResource( resourceLocator.getHref( false ), myResource );
111 lockManager = new SimpleLockManager();
112 resource.addLockManager( lockManager );
117 public void tearDown()
121 org.apache.archiva.common.utils.FileUtils.deleteDirectory( baseDir );
122 String appserverBase = System.getProperty( "appserver.base" );
123 if ( StringUtils.isNotEmpty( appserverBase ) )
125 FileUtils.deleteDirectory( Paths.get( appserverBase ) );
129 private DavResource getDavResource( String logicalPath, Path file ) throws LayoutException
131 return new ArchivaDavResource( new FilesystemAsset( (FilesystemStorage) repository.getRoot().getStorage(), logicalPath, file.toAbsolutePath()) , logicalPath, repository, session, resourceLocator,
132 resourceFactory, mimeTypes, Collections.<AuditListener> emptyList(), null);
136 public void testDeleteNonExistantResourceShould404()
139 Path dir = baseDir.resolve( "testdir" );
142 DavResource directoryResource = getDavResource( "/testdir", dir );
143 directoryResource.getCollection().removeMember( directoryResource );
144 fail( "Did not throw DavException" );
146 catch ( DavException e )
148 assertEquals( DavServletResponse.SC_NOT_FOUND, e.getErrorCode() );
153 public void testDeleteCollection()
156 Path dir = baseDir.resolve( "testdir" );
159 assertNotNull( Files.createDirectories(dir) );
160 DavResource directoryResource = getDavResource( "/testdir", dir );
161 directoryResource.getCollection().removeMember( directoryResource );
162 assertFalse( Files.exists(dir) );
166 org.apache.archiva.common.utils.FileUtils.deleteDirectory( dir );
171 public void testDeleteResource()
174 assertTrue( Files.exists(myResource) );
175 resource.getCollection().removeMember( resource );
176 assertFalse( Files.exists(myResource) );
180 public void testIsLockable()
182 assertTrue( resource.isLockable( Type.WRITE, Scope.EXCLUSIVE ) );
183 assertFalse( resource.isLockable( Type.WRITE, Scope.SHARED ) );
187 public void testLock()
190 assertEquals( 0, resource.getLocks().length );
192 LockInfo info = new LockInfo( Scope.EXCLUSIVE, Type.WRITE, "/", 0, false );
193 lockManager.createLock( info, resource );
195 assertEquals( 1, resource.getLocks().length );
199 public void testLockIfResourceUnlockable()
202 assertEquals( 0, resource.getLocks().length );
204 LockInfo info = new LockInfo( Scope.SHARED, Type.WRITE, "/", 0, false );
207 lockManager.createLock( info, resource );
208 fail( "Did not throw dav exception" );
210 catch ( Exception e )
212 // Simple lock manager will die
214 assertEquals( 0, resource.getLocks().length );
218 public void testGetLock()
221 LockInfo info = new LockInfo( Scope.EXCLUSIVE, Type.WRITE, "/", 0, false );
222 lockManager.createLock( info, resource );
224 assertEquals( 1, resource.getLocks().length );
227 assertNotNull( resource.getLock( Type.WRITE, Scope.EXCLUSIVE ) );
229 // Lock should not exist
230 assertNull( resource.getLock( Type.WRITE, Scope.SHARED ) );
234 public void testRefreshLockThrowsExceptionIfNoLockIsPresent()
237 LockInfo info = new LockInfo( Scope.EXCLUSIVE, Type.WRITE, "/", 0, false );
239 assertEquals( 0, resource.getLocks().length );
243 lockManager.refreshLock( info, "notoken", resource );
244 fail( "Did not throw dav exception" );
246 catch ( DavException e )
248 assertEquals( DavServletResponse.SC_PRECONDITION_FAILED, e.getErrorCode() );
251 assertEquals( 0, resource.getLocks().length );
255 public void testRefreshLock()
258 LockInfo info = new LockInfo( Scope.EXCLUSIVE, Type.WRITE, "/", 0, false );
260 assertEquals( 0, resource.getLocks().length );
262 lockManager.createLock( info, resource );
264 assertEquals( 1, resource.getLocks().length );
266 ActiveLock lock = resource.getLocks()[0];
268 lockManager.refreshLock( info, lock.getToken(), resource );
270 assertEquals( 1, resource.getLocks().length );
274 public void testUnlock()
277 LockInfo info = new LockInfo( Scope.EXCLUSIVE, Type.WRITE, "/", 0, false );
279 assertEquals( 0, resource.getLocks().length );
281 lockManager.createLock( info, resource );
283 assertEquals( 1, resource.getLocks().length );
285 ActiveLock lock = resource.getLocks()[0];
287 lockManager.releaseLock( lock.getToken(), resource );
289 assertEquals( 0, resource.getLocks().length );
293 public void testUnlockThrowsDavExceptionIfNotLocked()
296 LockInfo info = new LockInfo( Scope.EXCLUSIVE, Type.WRITE, "/", 0, false );
298 assertEquals( 0, resource.getLocks().length );
300 lockManager.createLock( info, resource );
302 assertEquals( 1, resource.getLocks().length );
306 lockManager.releaseLock( "BLAH", resource );
307 fail( "Did not throw DavException" );
309 catch ( DavException e )
311 assertEquals( DavServletResponse.SC_LOCKED, e.getErrorCode() );
314 assertEquals( 1, resource.getLocks().length );
318 public void testUnlockThrowsDavExceptionIfResourceNotLocked()
321 assertEquals( 0, resource.getLocks().length );
325 lockManager.releaseLock( "BLAH", resource );
326 fail( "Did not throw DavException" );
328 catch ( DavException e )
330 assertEquals( DavServletResponse.SC_PRECONDITION_FAILED, e.getErrorCode() );
333 assertEquals( 0, resource.getLocks().length );
336 private class RootContextDavResourceFactory
337 implements DavResourceFactory
340 public DavResource createResource( DavResourceLocator locator, DavServletRequest request,
341 DavServletResponse response )
344 throw new UnsupportedOperationException( "Not supported yet." );
348 public DavResource createResource( DavResourceLocator locator, DavSession session )
353 return new ArchivaDavResource( new FilesystemAsset( (FilesystemStorage) repository.getRoot().getStorage(), "/" , baseDir.toAbsolutePath()), "/", repository, session, resourceLocator,
354 resourceFactory, mimeTypes, Collections.<AuditListener> emptyList(),
357 catch ( LayoutException e )
359 throw new DavException( HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e );