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 org.apache.commons.io.FileUtils;
24 import org.apache.jackrabbit.webdav.DavException;
25 import org.apache.jackrabbit.webdav.DavResource;
26 import org.apache.jackrabbit.webdav.DavResourceFactory;
27 import org.apache.jackrabbit.webdav.DavResourceLocator;
28 import org.apache.jackrabbit.webdav.DavServletRequest;
29 import org.apache.jackrabbit.webdav.DavServletResponse;
30 import org.apache.jackrabbit.webdav.DavSession;
31 import org.apache.jackrabbit.webdav.lock.ActiveLock;
32 import org.apache.jackrabbit.webdav.lock.LockInfo;
33 import org.apache.jackrabbit.webdav.lock.LockManager;
34 import org.apache.jackrabbit.webdav.lock.Scope;
35 import org.apache.jackrabbit.webdav.lock.SimpleLockManager;
36 import org.apache.jackrabbit.webdav.lock.Type;
37 import org.apache.maven.archiva.webdav.util.MimeTypes;
38 import org.codehaus.plexus.spring.PlexusInSpringTestCase;
39 import org.codehaus.plexus.spring.PlexusToSpringUtils;
41 public class DavResourceTest extends PlexusInSpringTestCase
43 private DavSession session;
45 private MimeTypes mimeTypes;
47 private ArchivaDavResourceLocator resourceLocator;
49 private DavResourceFactory resourceFactory;
53 private final String REPOPATH = "myresource.jar";
55 private File myResource;
57 private DavResource resource;
59 private LockManager lockManager;
62 protected void setUp()
66 session = new ArchivaDavSession();
67 mimeTypes = (MimeTypes)getApplicationContext().getBean(PlexusToSpringUtils.buildSpringId(MimeTypes.class));
68 baseDir = getTestFile("target/DavResourceTest");
70 myResource = new File(baseDir, "myresource.jar");
71 assertTrue("Could not create " + myResource.getAbsolutePath(), myResource.createNewFile());
72 resourceFactory = new RootContextDavResourceFactory();
73 resourceLocator = (ArchivaDavResourceLocator)new ArchivaDavLocatorFactory().createResourceLocator("/", REPOPATH);
74 resource = getDavResource(resourceLocator.getHref(false), myResource);
75 lockManager = new SimpleLockManager();
76 resource.addLockManager(lockManager);
80 protected void tearDown()
85 FileUtils.deleteDirectory(baseDir);
88 private DavResource getDavResource(String logicalPath, File file)
90 return new ArchivaDavResource(file.getAbsolutePath(), logicalPath, mimeTypes, session, resourceLocator, resourceFactory);
93 public void testDeleteCollection()
96 File dir = new File(baseDir, "testdir");
99 assertTrue(dir.mkdir());
100 DavResource directoryResource = getDavResource("/testdir", dir);
101 directoryResource.getCollection().removeMember(directoryResource);
102 assertFalse(dir.exists());
106 FileUtils.deleteDirectory(dir);
110 public void testDeleteResource()
113 assertTrue(myResource.exists());
114 resource.getCollection().removeMember(resource);
115 assertFalse(myResource.exists());
118 public void testIsLockable()
120 assertTrue(resource.isLockable(Type.WRITE, Scope.EXCLUSIVE));
121 assertFalse(resource.isLockable(Type.WRITE, Scope.SHARED));
124 public void testLock()
127 assertEquals(0, resource.getLocks().length);
129 LockInfo info = new LockInfo(Scope.EXCLUSIVE, Type.WRITE, "/", 0, false);
130 lockManager.createLock(info, resource);
132 assertEquals(1, resource.getLocks().length);
135 public void testLockIfResourceUnlockable()
138 assertEquals(0, resource.getLocks().length);
140 LockInfo info = new LockInfo(Scope.SHARED, Type.WRITE, "/", 0, false);
143 lockManager.createLock(info, resource);
144 fail("Did not throw dav exception");
148 //Simple lock manager will die
150 assertEquals(0, resource.getLocks().length);
153 public void testGetLock()
156 LockInfo info = new LockInfo(Scope.EXCLUSIVE, Type.WRITE, "/", 0, false);
157 lockManager.createLock(info, resource);
159 assertEquals(1, resource.getLocks().length);
162 assertNotNull(resource.getLock(Type.WRITE, Scope.EXCLUSIVE));
164 //Lock should not exist
165 assertNull(resource.getLock(Type.WRITE, Scope.SHARED));
169 public void testRefreshLockThrowsExceptionIfNoLockIsPresent()
172 LockInfo info = new LockInfo(Scope.EXCLUSIVE, Type.WRITE, "/", 0, false);
174 assertEquals(0, resource.getLocks().length);
178 lockManager.refreshLock(info, "notoken", resource);
179 fail("Did not throw dav exception");
181 catch (DavException e)
183 assertEquals(DavServletResponse.SC_PRECONDITION_FAILED, e.getErrorCode());
186 assertEquals(0, resource.getLocks().length);
189 public void testRefreshLock()
192 LockInfo info = new LockInfo(Scope.EXCLUSIVE, Type.WRITE, "/", 0, false);
194 assertEquals(0, resource.getLocks().length);
196 lockManager.createLock(info, resource);
198 assertEquals(1, resource.getLocks().length);
200 ActiveLock lock = resource.getLocks()[0];
202 lockManager.refreshLock(info, lock.getToken(), resource);
204 assertEquals(1, resource.getLocks().length);
207 public void testUnlock()
210 LockInfo info = new LockInfo(Scope.EXCLUSIVE, Type.WRITE, "/", 0, false);
212 assertEquals(0, resource.getLocks().length);
214 lockManager.createLock(info, resource);
216 assertEquals(1, resource.getLocks().length);
218 ActiveLock lock = resource.getLocks()[0];
220 lockManager.releaseLock(lock.getToken(), resource);
222 assertEquals(0, resource.getLocks().length);
225 public void testUnlockThrowsDavExceptionIfNotLocked()
228 LockInfo info = new LockInfo(Scope.EXCLUSIVE, Type.WRITE, "/", 0, false);
230 assertEquals(0, resource.getLocks().length);
232 lockManager.createLock(info, resource);
234 assertEquals(1, resource.getLocks().length);
238 lockManager.releaseLock("BLAH", resource);
239 fail("Did not throw DavException");
241 catch (DavException e)
243 assertEquals(DavServletResponse.SC_LOCKED, e.getErrorCode());
246 assertEquals(1, resource.getLocks().length);
249 public void testUnlockThrowsDavExceptionIfResourceNotLocked()
252 assertEquals(0, resource.getLocks().length);
256 lockManager.releaseLock("BLAH", resource);
257 fail("Did not throw DavException");
259 catch (DavException e)
261 assertEquals(DavServletResponse.SC_PRECONDITION_FAILED, e.getErrorCode());
264 assertEquals(0, resource.getLocks().length);
267 private class RootContextDavResourceFactory implements DavResourceFactory
269 public DavResource createResource(DavResourceLocator locator, DavServletRequest request, DavServletResponse response) throws DavException {
270 throw new UnsupportedOperationException("Not supported yet.");
273 public DavResource createResource(DavResourceLocator locator, DavSession session) throws DavException {
274 return new ArchivaDavResource(baseDir.getAbsolutePath(), "/", mimeTypes, session, resourceLocator, resourceFactory);