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.webdav.util.MimeTypes;
42 import org.codehaus.plexus.spring.PlexusInSpringTestCase;
43 import org.codehaus.plexus.spring.PlexusToSpringUtils;
45 public class DavResourceTest
46 extends PlexusInSpringTestCase
48 private DavSession session;
50 private MimeTypes mimeTypes;
52 private ArchivaDavResourceLocator resourceLocator;
54 private DavResourceFactory resourceFactory;
58 private final String REPOPATH = "myresource.jar";
60 private File myResource;
62 private DavResource resource;
64 private LockManager lockManager;
66 private ManagedRepositoryConfiguration repository = new ManagedRepositoryConfiguration();
69 protected void setUp()
73 session = new ArchivaDavSession();
74 mimeTypes = (MimeTypes) getApplicationContext().getBean( PlexusToSpringUtils.buildSpringId( MimeTypes.class ) );
75 baseDir = getTestFile( "target/DavResourceTest" );
77 myResource = new File( baseDir, "myresource.jar" );
78 assertTrue( "Could not create " + myResource.getAbsolutePath(), myResource.createNewFile() );
79 resourceFactory = new RootContextDavResourceFactory();
82 (ArchivaDavResourceLocator) new ArchivaDavLocatorFactory().createResourceLocator( "/", REPOPATH );
83 resource = getDavResource( resourceLocator.getHref( false ), myResource );
84 lockManager = new SimpleLockManager();
85 resource.addLockManager( lockManager );
89 protected void tearDown()
94 FileUtils.deleteDirectory( baseDir );
97 private DavResource getDavResource( String logicalPath, File file )
99 return new ArchivaDavResource( file.getAbsolutePath(), logicalPath, repository, session, resourceLocator,
100 resourceFactory, mimeTypes, Collections.<AuditListener> emptyList(), null );
103 public void testDeleteNonExistantResourceShould404()
106 File dir = new File( baseDir, "testdir" );
109 DavResource directoryResource = getDavResource( "/testdir", dir );
110 directoryResource.getCollection().removeMember( directoryResource );
111 fail( "Did not throw DavException" );
113 catch ( DavException e )
115 assertEquals( DavServletResponse.SC_NOT_FOUND, e.getErrorCode() );
119 public void testDeleteCollection()
122 File dir = new File( baseDir, "testdir" );
125 assertTrue( dir.mkdir() );
126 DavResource directoryResource = getDavResource( "/testdir", dir );
127 directoryResource.getCollection().removeMember( directoryResource );
128 assertFalse( dir.exists() );
132 FileUtils.deleteDirectory( dir );
136 public void testDeleteResource()
139 assertTrue( myResource.exists() );
140 resource.getCollection().removeMember( resource );
141 assertFalse( myResource.exists() );
144 public void testIsLockable()
146 assertTrue( resource.isLockable( Type.WRITE, Scope.EXCLUSIVE ) );
147 assertFalse( resource.isLockable( Type.WRITE, Scope.SHARED ) );
150 public void testLock()
153 assertEquals( 0, resource.getLocks().length );
155 LockInfo info = new LockInfo( Scope.EXCLUSIVE, Type.WRITE, "/", 0, false );
156 lockManager.createLock( info, resource );
158 assertEquals( 1, resource.getLocks().length );
161 public void testLockIfResourceUnlockable()
164 assertEquals( 0, resource.getLocks().length );
166 LockInfo info = new LockInfo( Scope.SHARED, Type.WRITE, "/", 0, false );
169 lockManager.createLock( info, resource );
170 fail( "Did not throw dav exception" );
172 catch ( Exception e )
174 // Simple lock manager will die
176 assertEquals( 0, resource.getLocks().length );
179 public void testGetLock()
182 LockInfo info = new LockInfo( Scope.EXCLUSIVE, Type.WRITE, "/", 0, false );
183 lockManager.createLock( info, resource );
185 assertEquals( 1, resource.getLocks().length );
188 assertNotNull( resource.getLock( Type.WRITE, Scope.EXCLUSIVE ) );
190 // Lock should not exist
191 assertNull( resource.getLock( Type.WRITE, Scope.SHARED ) );
194 public void testRefreshLockThrowsExceptionIfNoLockIsPresent()
197 LockInfo info = new LockInfo( Scope.EXCLUSIVE, Type.WRITE, "/", 0, false );
199 assertEquals( 0, resource.getLocks().length );
203 lockManager.refreshLock( info, "notoken", resource );
204 fail( "Did not throw dav exception" );
206 catch ( DavException e )
208 assertEquals( DavServletResponse.SC_PRECONDITION_FAILED, e.getErrorCode() );
211 assertEquals( 0, resource.getLocks().length );
214 public void testRefreshLock()
217 LockInfo info = new LockInfo( Scope.EXCLUSIVE, Type.WRITE, "/", 0, false );
219 assertEquals( 0, resource.getLocks().length );
221 lockManager.createLock( info, resource );
223 assertEquals( 1, resource.getLocks().length );
225 ActiveLock lock = resource.getLocks()[0];
227 lockManager.refreshLock( info, lock.getToken(), resource );
229 assertEquals( 1, resource.getLocks().length );
232 public void testUnlock()
235 LockInfo info = new LockInfo( Scope.EXCLUSIVE, Type.WRITE, "/", 0, false );
237 assertEquals( 0, resource.getLocks().length );
239 lockManager.createLock( info, resource );
241 assertEquals( 1, resource.getLocks().length );
243 ActiveLock lock = resource.getLocks()[0];
245 lockManager.releaseLock( lock.getToken(), resource );
247 assertEquals( 0, resource.getLocks().length );
250 public void testUnlockThrowsDavExceptionIfNotLocked()
253 LockInfo info = new LockInfo( Scope.EXCLUSIVE, Type.WRITE, "/", 0, false );
255 assertEquals( 0, resource.getLocks().length );
257 lockManager.createLock( info, resource );
259 assertEquals( 1, resource.getLocks().length );
263 lockManager.releaseLock( "BLAH", resource );
264 fail( "Did not throw DavException" );
266 catch ( DavException e )
268 assertEquals( DavServletResponse.SC_LOCKED, e.getErrorCode() );
271 assertEquals( 1, resource.getLocks().length );
274 public void testUnlockThrowsDavExceptionIfResourceNotLocked()
277 assertEquals( 0, resource.getLocks().length );
281 lockManager.releaseLock( "BLAH", resource );
282 fail( "Did not throw DavException" );
284 catch ( DavException e )
286 assertEquals( DavServletResponse.SC_PRECONDITION_FAILED, e.getErrorCode() );
289 assertEquals( 0, resource.getLocks().length );
292 private class RootContextDavResourceFactory
293 implements DavResourceFactory
295 public DavResource createResource( DavResourceLocator locator, DavServletRequest request,
296 DavServletResponse response )
299 throw new UnsupportedOperationException( "Not supported yet." );
302 public DavResource createResource( DavResourceLocator locator, DavSession session )
305 return new ArchivaDavResource( baseDir.getAbsolutePath(), "/", repository, session, resourceLocator,
306 resourceFactory, mimeTypes, Collections.<AuditListener> emptyList(),