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.audit.AuditListener;
25 import org.apache.archiva.common.filelock.FileLockManager;
26 import org.apache.commons.io.FileUtils;
27 import org.apache.jackrabbit.webdav.DavException;
28 import org.apache.jackrabbit.webdav.DavResource;
29 import org.apache.jackrabbit.webdav.DavResourceFactory;
30 import org.apache.jackrabbit.webdav.DavResourceLocator;
31 import org.apache.jackrabbit.webdav.DavServletRequest;
32 import org.apache.jackrabbit.webdav.DavServletResponse;
33 import org.apache.jackrabbit.webdav.DavSession;
34 import org.apache.jackrabbit.webdav.lock.ActiveLock;
35 import org.apache.jackrabbit.webdav.lock.LockInfo;
36 import org.apache.jackrabbit.webdav.lock.LockManager;
37 import org.apache.jackrabbit.webdav.lock.Scope;
38 import org.apache.jackrabbit.webdav.lock.SimpleLockManager;
39 import org.apache.jackrabbit.webdav.lock.Type;
40 import org.apache.archiva.webdav.util.MimeTypes;
41 import org.junit.After;
42 import org.junit.Before;
43 import org.junit.Test;
44 import org.junit.runner.RunWith;
45 import org.springframework.test.context.ContextConfiguration;
47 import javax.inject.Inject;
49 import java.util.Collections;
50 import org.apache.archiva.test.utils.ArchivaSpringJUnit4ClassRunner;
52 @RunWith( ArchivaSpringJUnit4ClassRunner.class )
53 @ContextConfiguration( locations = { "classpath*:/META-INF/spring-context.xml", "classpath*:/spring-context.xml" } )
54 public class DavResourceTest
57 private DavSession session;
60 private MimeTypes mimeTypes;
63 private FileLockManager fileLockManager;
65 private ArchivaDavResourceLocator resourceLocator;
67 private DavResourceFactory resourceFactory;
71 private final String REPOPATH = "myresource.jar";
73 private File myResource;
75 private DavResource resource;
77 private LockManager lockManager;
79 private ManagedRepository repository = new ManagedRepository();
87 session = new ArchivaDavSession();
88 baseDir = new File( "target/DavResourceTest" );
90 myResource = new File( baseDir, "myresource.jar" );
91 assertTrue( "Could not create " + myResource.getAbsolutePath(), myResource.createNewFile() );
92 resourceFactory = new RootContextDavResourceFactory();
95 (ArchivaDavResourceLocator) new ArchivaDavLocatorFactory().createResourceLocator( "/", REPOPATH );
96 resource = getDavResource( resourceLocator.getHref( false ), myResource );
97 lockManager = new SimpleLockManager();
98 resource.addLockManager( lockManager );
103 public void tearDown()
107 FileUtils.deleteDirectory( baseDir );
110 private DavResource getDavResource( String logicalPath, File file )
112 return new ArchivaDavResource( file.getAbsolutePath(), logicalPath, repository, session, resourceLocator,
113 resourceFactory, mimeTypes, Collections.<AuditListener> emptyList(), null, fileLockManager );
117 public void testDeleteNonExistantResourceShould404()
120 File dir = new File( baseDir, "testdir" );
123 DavResource directoryResource = getDavResource( "/testdir", dir );
124 directoryResource.getCollection().removeMember( directoryResource );
125 fail( "Did not throw DavException" );
127 catch ( DavException e )
129 assertEquals( DavServletResponse.SC_NOT_FOUND, e.getErrorCode() );
134 public void testDeleteCollection()
137 File dir = new File( baseDir, "testdir" );
140 assertTrue( dir.mkdir() );
141 DavResource directoryResource = getDavResource( "/testdir", dir );
142 directoryResource.getCollection().removeMember( directoryResource );
143 assertFalse( dir.exists() );
147 FileUtils.deleteDirectory( dir );
152 public void testDeleteResource()
155 assertTrue( myResource.exists() );
156 resource.getCollection().removeMember( resource );
157 assertFalse( myResource.exists() );
161 public void testIsLockable()
163 assertTrue( resource.isLockable( Type.WRITE, Scope.EXCLUSIVE ) );
164 assertFalse( resource.isLockable( Type.WRITE, Scope.SHARED ) );
168 public void testLock()
171 assertEquals( 0, resource.getLocks().length );
173 LockInfo info = new LockInfo( Scope.EXCLUSIVE, Type.WRITE, "/", 0, false );
174 lockManager.createLock( info, resource );
176 assertEquals( 1, resource.getLocks().length );
180 public void testLockIfResourceUnlockable()
183 assertEquals( 0, resource.getLocks().length );
185 LockInfo info = new LockInfo( Scope.SHARED, Type.WRITE, "/", 0, false );
188 lockManager.createLock( info, resource );
189 fail( "Did not throw dav exception" );
191 catch ( Exception e )
193 // Simple lock manager will die
195 assertEquals( 0, resource.getLocks().length );
199 public void testGetLock()
202 LockInfo info = new LockInfo( Scope.EXCLUSIVE, Type.WRITE, "/", 0, false );
203 lockManager.createLock( info, resource );
205 assertEquals( 1, resource.getLocks().length );
208 assertNotNull( resource.getLock( Type.WRITE, Scope.EXCLUSIVE ) );
210 // Lock should not exist
211 assertNull( resource.getLock( Type.WRITE, Scope.SHARED ) );
215 public void testRefreshLockThrowsExceptionIfNoLockIsPresent()
218 LockInfo info = new LockInfo( Scope.EXCLUSIVE, Type.WRITE, "/", 0, false );
220 assertEquals( 0, resource.getLocks().length );
224 lockManager.refreshLock( info, "notoken", resource );
225 fail( "Did not throw dav exception" );
227 catch ( DavException e )
229 assertEquals( DavServletResponse.SC_PRECONDITION_FAILED, e.getErrorCode() );
232 assertEquals( 0, resource.getLocks().length );
236 public void testRefreshLock()
239 LockInfo info = new LockInfo( Scope.EXCLUSIVE, Type.WRITE, "/", 0, false );
241 assertEquals( 0, resource.getLocks().length );
243 lockManager.createLock( info, resource );
245 assertEquals( 1, resource.getLocks().length );
247 ActiveLock lock = resource.getLocks()[0];
249 lockManager.refreshLock( info, lock.getToken(), resource );
251 assertEquals( 1, resource.getLocks().length );
255 public void testUnlock()
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.releaseLock( lock.getToken(), resource );
270 assertEquals( 0, resource.getLocks().length );
274 public void testUnlockThrowsDavExceptionIfNotLocked()
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 );
287 lockManager.releaseLock( "BLAH", resource );
288 fail( "Did not throw DavException" );
290 catch ( DavException e )
292 assertEquals( DavServletResponse.SC_LOCKED, e.getErrorCode() );
295 assertEquals( 1, resource.getLocks().length );
299 public void testUnlockThrowsDavExceptionIfResourceNotLocked()
302 assertEquals( 0, resource.getLocks().length );
306 lockManager.releaseLock( "BLAH", resource );
307 fail( "Did not throw DavException" );
309 catch ( DavException e )
311 assertEquals( DavServletResponse.SC_PRECONDITION_FAILED, e.getErrorCode() );
314 assertEquals( 0, resource.getLocks().length );
317 private class RootContextDavResourceFactory
318 implements DavResourceFactory
321 public DavResource createResource( DavResourceLocator locator, DavServletRequest request,
322 DavServletResponse response )
325 throw new UnsupportedOperationException( "Not supported yet." );
329 public DavResource createResource( DavResourceLocator locator, DavSession session )
332 return new ArchivaDavResource( baseDir.getAbsolutePath(), "/", repository, session, resourceLocator,
333 resourceFactory, mimeTypes, Collections.<AuditListener> emptyList(),
334 null, fileLockManager );