1 package org.apache.maven.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
23 import java.util.Collections;
25 import org.apache.commons.io.FileUtils;
26 import org.apache.jackrabbit.webdav.DavException;
27 import org.apache.jackrabbit.webdav.DavResource;
28 import org.apache.jackrabbit.webdav.DavResourceFactory;
29 import org.apache.jackrabbit.webdav.DavResourceLocator;
30 import org.apache.jackrabbit.webdav.DavServletRequest;
31 import org.apache.jackrabbit.webdav.DavServletResponse;
32 import org.apache.jackrabbit.webdav.DavSession;
33 import org.apache.jackrabbit.webdav.lock.ActiveLock;
34 import org.apache.jackrabbit.webdav.lock.LockInfo;
35 import org.apache.jackrabbit.webdav.lock.LockManager;
36 import org.apache.jackrabbit.webdav.lock.Scope;
37 import org.apache.jackrabbit.webdav.lock.SimpleLockManager;
38 import org.apache.jackrabbit.webdav.lock.Type;
39 import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration;
40 import org.apache.maven.archiva.repository.audit.AuditListener;
41 import org.apache.maven.archiva.repository.scanner.RepositoryContentConsumers;
42 import org.apache.maven.archiva.webdav.util.MimeTypes;
43 import org.codehaus.plexus.spring.PlexusInSpringTestCase;
44 import org.codehaus.plexus.spring.PlexusToSpringUtils;
45 import org.codehaus.plexus.taskqueue.execution.TaskQueueExecutor;
47 public class DavResourceTest
48 extends PlexusInSpringTestCase
50 private DavSession session;
52 private MimeTypes mimeTypes;
54 private ArchivaDavResourceLocator resourceLocator;
56 private DavResourceFactory resourceFactory;
60 private final String REPOPATH = "myresource.jar";
62 private File myResource;
64 private DavResource resource;
66 private LockManager lockManager;
68 private ManagedRepositoryConfiguration repository = new ManagedRepositoryConfiguration();
71 protected void setUp()
75 session = new ArchivaDavSession();
76 mimeTypes = (MimeTypes) getApplicationContext().getBean( PlexusToSpringUtils.buildSpringId( MimeTypes.class ) );
77 baseDir = getTestFile( "target/DavResourceTest" );
79 myResource = new File( baseDir, "myresource.jar" );
80 assertTrue( "Could not create " + myResource.getAbsolutePath(), myResource.createNewFile() );
81 resourceFactory = new RootContextDavResourceFactory();
83 (ArchivaDavResourceLocator) new ArchivaDavLocatorFactory().createResourceLocator( "/", REPOPATH );
84 resource = getDavResource( resourceLocator.getHref( false ), myResource );
85 lockManager = new SimpleLockManager();
86 resource.addLockManager( lockManager );
90 protected void tearDown()
95 FileUtils.deleteDirectory( baseDir );
98 private DavResource getDavResource( String logicalPath, File file )
100 return new ArchivaDavResource( file.getAbsolutePath(), logicalPath, repository, session, resourceLocator,
101 resourceFactory, mimeTypes, Collections.<AuditListener> emptyList(), null );
104 public void testDeleteNonExistantResourceShould404()
107 File dir = new File( baseDir, "testdir" );
110 DavResource directoryResource = getDavResource( "/testdir", dir );
111 directoryResource.getCollection().removeMember( directoryResource );
112 fail( "Did not throw DavException" );
114 catch ( DavException e )
116 assertEquals( DavServletResponse.SC_NOT_FOUND, e.getErrorCode() );
120 public void testDeleteCollection()
123 File dir = new File( baseDir, "testdir" );
126 assertTrue( dir.mkdir() );
127 DavResource directoryResource = getDavResource( "/testdir", dir );
128 directoryResource.getCollection().removeMember( directoryResource );
129 assertFalse( dir.exists() );
133 FileUtils.deleteDirectory( dir );
137 public void testDeleteResource()
140 assertTrue( myResource.exists() );
141 resource.getCollection().removeMember( resource );
142 assertFalse( myResource.exists() );
145 public void testIsLockable()
147 assertTrue( resource.isLockable( Type.WRITE, Scope.EXCLUSIVE ) );
148 assertFalse( resource.isLockable( Type.WRITE, Scope.SHARED ) );
151 public void testLock()
154 assertEquals( 0, resource.getLocks().length );
156 LockInfo info = new LockInfo( Scope.EXCLUSIVE, Type.WRITE, "/", 0, false );
157 lockManager.createLock( info, resource );
159 assertEquals( 1, resource.getLocks().length );
162 public void testLockIfResourceUnlockable()
165 assertEquals( 0, resource.getLocks().length );
167 LockInfo info = new LockInfo( Scope.SHARED, Type.WRITE, "/", 0, false );
170 lockManager.createLock( info, resource );
171 fail( "Did not throw dav exception" );
173 catch ( Exception e )
175 // Simple lock manager will die
177 assertEquals( 0, resource.getLocks().length );
180 public void testGetLock()
183 LockInfo info = new LockInfo( Scope.EXCLUSIVE, Type.WRITE, "/", 0, false );
184 lockManager.createLock( info, resource );
186 assertEquals( 1, resource.getLocks().length );
189 assertNotNull( resource.getLock( Type.WRITE, Scope.EXCLUSIVE ) );
191 // Lock should not exist
192 assertNull( resource.getLock( Type.WRITE, Scope.SHARED ) );
195 public void testRefreshLockThrowsExceptionIfNoLockIsPresent()
198 LockInfo info = new LockInfo( Scope.EXCLUSIVE, Type.WRITE, "/", 0, false );
200 assertEquals( 0, resource.getLocks().length );
204 lockManager.refreshLock( info, "notoken", resource );
205 fail( "Did not throw dav exception" );
207 catch ( DavException e )
209 assertEquals( DavServletResponse.SC_PRECONDITION_FAILED, e.getErrorCode() );
212 assertEquals( 0, resource.getLocks().length );
215 public void testRefreshLock()
218 LockInfo info = new LockInfo( Scope.EXCLUSIVE, Type.WRITE, "/", 0, false );
220 assertEquals( 0, resource.getLocks().length );
222 lockManager.createLock( info, resource );
224 assertEquals( 1, resource.getLocks().length );
226 ActiveLock lock = resource.getLocks()[0];
228 lockManager.refreshLock( info, lock.getToken(), resource );
230 assertEquals( 1, resource.getLocks().length );
233 public void testUnlock()
236 LockInfo info = new LockInfo( Scope.EXCLUSIVE, Type.WRITE, "/", 0, false );
238 assertEquals( 0, resource.getLocks().length );
240 lockManager.createLock( info, resource );
242 assertEquals( 1, resource.getLocks().length );
244 ActiveLock lock = resource.getLocks()[0];
246 lockManager.releaseLock( lock.getToken(), resource );
248 assertEquals( 0, resource.getLocks().length );
251 public void testUnlockThrowsDavExceptionIfNotLocked()
254 LockInfo info = new LockInfo( Scope.EXCLUSIVE, Type.WRITE, "/", 0, false );
256 assertEquals( 0, resource.getLocks().length );
258 lockManager.createLock( info, resource );
260 assertEquals( 1, resource.getLocks().length );
264 lockManager.releaseLock( "BLAH", resource );
265 fail( "Did not throw DavException" );
267 catch ( DavException e )
269 assertEquals( DavServletResponse.SC_LOCKED, e.getErrorCode() );
272 assertEquals( 1, resource.getLocks().length );
275 public void testUnlockThrowsDavExceptionIfResourceNotLocked()
278 assertEquals( 0, resource.getLocks().length );
282 lockManager.releaseLock( "BLAH", resource );
283 fail( "Did not throw DavException" );
285 catch ( DavException e )
287 assertEquals( DavServletResponse.SC_PRECONDITION_FAILED, e.getErrorCode() );
290 assertEquals( 0, resource.getLocks().length );
293 private class RootContextDavResourceFactory
294 implements DavResourceFactory
296 public DavResource createResource( DavResourceLocator locator, DavServletRequest request,
297 DavServletResponse response )
300 throw new UnsupportedOperationException( "Not supported yet." );
303 public DavResource createResource( DavResourceLocator locator, DavSession session )
306 return new ArchivaDavResource( baseDir.getAbsolutePath(), "/", repository, session, resourceLocator,
307 resourceFactory, mimeTypes, Collections.<AuditListener> emptyList(),