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.admin.model.beans.ManagedRepository;
24 import org.apache.archiva.common.filelock.FileLockManager;
25 import org.apache.archiva.repository.events.AuditListener;
26 import org.apache.archiva.test.utils.ArchivaSpringJUnit4ClassRunner;
27 import org.apache.archiva.webdav.util.MimeTypes;
28 import org.apache.jackrabbit.webdav.*;
29 import org.apache.jackrabbit.webdav.lock.*;
30 import org.junit.After;
31 import org.junit.Before;
32 import org.junit.Test;
33 import org.junit.runner.RunWith;
34 import org.springframework.test.context.ContextConfiguration;
36 import javax.inject.Inject;
37 import java.nio.file.Files;
38 import java.nio.file.Path;
39 import java.nio.file.Paths;
40 import java.util.Collections;
42 @RunWith( ArchivaSpringJUnit4ClassRunner.class )
43 @ContextConfiguration( locations = { "classpath*:/META-INF/spring-context.xml", "classpath*:/spring-context.xml" } )
44 public class DavResourceTest
47 private DavSession session;
50 private MimeTypes mimeTypes;
53 private FileLockManager fileLockManager;
55 private ArchivaDavResourceLocator resourceLocator;
57 private DavResourceFactory resourceFactory;
61 private final String REPOPATH = "myresource.jar";
63 private Path myResource;
65 private DavResource resource;
67 private LockManager lockManager;
69 private ManagedRepository repository = new ManagedRepository();
77 session = new ArchivaDavSession();
78 baseDir = Paths.get( "target/DavResourceTest" );
79 Files.createDirectories( baseDir );
80 myResource = baseDir.resolve( "myresource.jar" );
81 Files.createFile(myResource);
82 resourceFactory = new RootContextDavResourceFactory();
85 (ArchivaDavResourceLocator) new ArchivaDavLocatorFactory().createResourceLocator( "/", REPOPATH );
86 resource = getDavResource( resourceLocator.getHref( false ), myResource );
87 lockManager = new SimpleLockManager();
88 resource.addLockManager( lockManager );
93 public void tearDown()
97 org.apache.archiva.common.utils.FileUtils.deleteDirectory( baseDir );
100 private DavResource getDavResource( String logicalPath, Path file )
102 return new ArchivaDavResource( file.toAbsolutePath().toString(), logicalPath, repository, session, resourceLocator,
103 resourceFactory, mimeTypes, Collections.<AuditListener> emptyList(), null, fileLockManager );
107 public void testDeleteNonExistantResourceShould404()
110 Path dir = baseDir.resolve( "testdir" );
113 DavResource directoryResource = getDavResource( "/testdir", dir );
114 directoryResource.getCollection().removeMember( directoryResource );
115 fail( "Did not throw DavException" );
117 catch ( DavException e )
119 assertEquals( DavServletResponse.SC_NOT_FOUND, e.getErrorCode() );
124 public void testDeleteCollection()
127 Path dir = baseDir.resolve( "testdir" );
130 assertNotNull( Files.createDirectories(dir) );
131 DavResource directoryResource = getDavResource( "/testdir", dir );
132 directoryResource.getCollection().removeMember( directoryResource );
133 assertFalse( Files.exists(dir) );
137 org.apache.archiva.common.utils.FileUtils.deleteDirectory( dir );
142 public void testDeleteResource()
145 assertTrue( Files.exists(myResource) );
146 resource.getCollection().removeMember( resource );
147 assertFalse( Files.exists(myResource) );
151 public void testIsLockable()
153 assertTrue( resource.isLockable( Type.WRITE, Scope.EXCLUSIVE ) );
154 assertFalse( resource.isLockable( Type.WRITE, Scope.SHARED ) );
158 public void testLock()
161 assertEquals( 0, resource.getLocks().length );
163 LockInfo info = new LockInfo( Scope.EXCLUSIVE, Type.WRITE, "/", 0, false );
164 lockManager.createLock( info, resource );
166 assertEquals( 1, resource.getLocks().length );
170 public void testLockIfResourceUnlockable()
173 assertEquals( 0, resource.getLocks().length );
175 LockInfo info = new LockInfo( Scope.SHARED, Type.WRITE, "/", 0, false );
178 lockManager.createLock( info, resource );
179 fail( "Did not throw dav exception" );
181 catch ( Exception e )
183 // Simple lock manager will die
185 assertEquals( 0, resource.getLocks().length );
189 public void testGetLock()
192 LockInfo info = new LockInfo( Scope.EXCLUSIVE, Type.WRITE, "/", 0, false );
193 lockManager.createLock( info, resource );
195 assertEquals( 1, resource.getLocks().length );
198 assertNotNull( resource.getLock( Type.WRITE, Scope.EXCLUSIVE ) );
200 // Lock should not exist
201 assertNull( resource.getLock( Type.WRITE, Scope.SHARED ) );
205 public void testRefreshLockThrowsExceptionIfNoLockIsPresent()
208 LockInfo info = new LockInfo( Scope.EXCLUSIVE, Type.WRITE, "/", 0, false );
210 assertEquals( 0, resource.getLocks().length );
214 lockManager.refreshLock( info, "notoken", resource );
215 fail( "Did not throw dav exception" );
217 catch ( DavException e )
219 assertEquals( DavServletResponse.SC_PRECONDITION_FAILED, e.getErrorCode() );
222 assertEquals( 0, resource.getLocks().length );
226 public void testRefreshLock()
229 LockInfo info = new LockInfo( Scope.EXCLUSIVE, Type.WRITE, "/", 0, false );
231 assertEquals( 0, resource.getLocks().length );
233 lockManager.createLock( info, resource );
235 assertEquals( 1, resource.getLocks().length );
237 ActiveLock lock = resource.getLocks()[0];
239 lockManager.refreshLock( info, lock.getToken(), resource );
241 assertEquals( 1, resource.getLocks().length );
245 public void testUnlock()
248 LockInfo info = new LockInfo( Scope.EXCLUSIVE, Type.WRITE, "/", 0, false );
250 assertEquals( 0, resource.getLocks().length );
252 lockManager.createLock( info, resource );
254 assertEquals( 1, resource.getLocks().length );
256 ActiveLock lock = resource.getLocks()[0];
258 lockManager.releaseLock( lock.getToken(), resource );
260 assertEquals( 0, resource.getLocks().length );
264 public void testUnlockThrowsDavExceptionIfNotLocked()
267 LockInfo info = new LockInfo( Scope.EXCLUSIVE, Type.WRITE, "/", 0, false );
269 assertEquals( 0, resource.getLocks().length );
271 lockManager.createLock( info, resource );
273 assertEquals( 1, resource.getLocks().length );
277 lockManager.releaseLock( "BLAH", resource );
278 fail( "Did not throw DavException" );
280 catch ( DavException e )
282 assertEquals( DavServletResponse.SC_LOCKED, e.getErrorCode() );
285 assertEquals( 1, resource.getLocks().length );
289 public void testUnlockThrowsDavExceptionIfResourceNotLocked()
292 assertEquals( 0, resource.getLocks().length );
296 lockManager.releaseLock( "BLAH", resource );
297 fail( "Did not throw DavException" );
299 catch ( DavException e )
301 assertEquals( DavServletResponse.SC_PRECONDITION_FAILED, e.getErrorCode() );
304 assertEquals( 0, resource.getLocks().length );
307 private class RootContextDavResourceFactory
308 implements DavResourceFactory
311 public DavResource createResource( DavResourceLocator locator, DavServletRequest request,
312 DavServletResponse response )
315 throw new UnsupportedOperationException( "Not supported yet." );
319 public DavResource createResource( DavResourceLocator locator, DavSession session )
322 return new ArchivaDavResource( baseDir.toAbsolutePath().toString(), "/", repository, session, resourceLocator,
323 resourceFactory, mimeTypes, Collections.<AuditListener> emptyList(),
324 null, fileLockManager );