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.DavServletResponse;
27 import org.apache.jackrabbit.webdav.DavSession;
28 import org.apache.jackrabbit.webdav.lock.ActiveLock;
29 import org.apache.jackrabbit.webdav.lock.LockInfo;
30 import org.apache.jackrabbit.webdav.lock.LockManager;
31 import org.apache.jackrabbit.webdav.lock.Scope;
32 import org.apache.jackrabbit.webdav.lock.SimpleLockManager;
33 import org.apache.jackrabbit.webdav.lock.Type;
34 import org.apache.maven.archiva.webdav.util.MimeTypes;
35 import org.codehaus.plexus.spring.PlexusInSpringTestCase;
36 import org.codehaus.plexus.spring.PlexusToSpringUtils;
38 public class DavResourceTest extends PlexusInSpringTestCase
40 private DavSession session;
42 private MimeTypes mimeTypes;
44 private ArchivaDavResourceLocator resourceLocator;
48 private final String REPOPATH = "myresource.jar";
50 private File myResource;
52 private DavResource resource;
54 private LockManager lockManager;
57 protected void setUp()
61 session = new ArchivaDavSession();
62 mimeTypes = (MimeTypes)getApplicationContext().getBean(PlexusToSpringUtils.buildSpringId(MimeTypes.class));
63 baseDir = getTestFile("target/DavResourceTest");
65 myResource = new File(baseDir, "myresource.jar");
66 assertTrue("Could not create " + myResource.getAbsolutePath(), myResource.createNewFile());
67 resourceLocator = (ArchivaDavResourceLocator)new ArchivaDavLocatorFactory().createResourceLocator("/", REPOPATH);
68 resource = getDavResource(resourceLocator.getHref(false), myResource);
69 lockManager = new SimpleLockManager();
70 resource.addLockManager(lockManager);
74 protected void tearDown()
79 FileUtils.deleteDirectory(baseDir);
82 private DavResource getDavResource(String logicalPath, File file)
84 return new ArchivaDavResource(file.getAbsolutePath(), logicalPath, mimeTypes, session, resourceLocator, null);
87 public void testIsLockable()
89 assertTrue(resource.isLockable(Type.WRITE, Scope.EXCLUSIVE));
90 assertFalse(resource.isLockable(Type.WRITE, Scope.SHARED));
93 public void testLock()
96 assertEquals(0, resource.getLocks().length);
98 LockInfo info = new LockInfo(Scope.EXCLUSIVE, Type.WRITE, "/", 0, false);
99 lockManager.createLock(info, resource);
101 assertEquals(1, resource.getLocks().length);
104 public void testLockIfResourceUnlockable()
107 assertEquals(0, resource.getLocks().length);
109 LockInfo info = new LockInfo(Scope.SHARED, Type.WRITE, "/", 0, false);
112 lockManager.createLock(info, resource);
113 fail("Did not throw dav exception");
117 //Simple lock manager will die
119 assertEquals(0, resource.getLocks().length);
122 public void testGetLock()
125 LockInfo info = new LockInfo(Scope.EXCLUSIVE, Type.WRITE, "/", 0, false);
126 lockManager.createLock(info, resource);
128 assertEquals(1, resource.getLocks().length);
131 assertNotNull(resource.getLock(Type.WRITE, Scope.EXCLUSIVE));
133 //Lock should not exist
134 assertNull(resource.getLock(Type.WRITE, Scope.SHARED));
138 public void testRefreshLockThrowsExceptionIfNoLockIsPresent()
141 LockInfo info = new LockInfo(Scope.EXCLUSIVE, Type.WRITE, "/", 0, false);
143 assertEquals(0, resource.getLocks().length);
147 lockManager.refreshLock(info, "notoken", resource);
148 fail("Did not throw dav exception");
150 catch (DavException e)
152 assertEquals(DavServletResponse.SC_PRECONDITION_FAILED, e.getErrorCode());
155 assertEquals(0, resource.getLocks().length);
158 public void testRefreshLock()
161 LockInfo info = new LockInfo(Scope.EXCLUSIVE, Type.WRITE, "/", 0, false);
163 assertEquals(0, resource.getLocks().length);
165 lockManager.createLock(info, resource);
167 assertEquals(1, resource.getLocks().length);
169 ActiveLock lock = resource.getLocks()[0];
171 lockManager.refreshLock(info, lock.getToken(), resource);
173 assertEquals(1, resource.getLocks().length);
176 public void testUnlock()
179 LockInfo info = new LockInfo(Scope.EXCLUSIVE, Type.WRITE, "/", 0, false);
181 assertEquals(0, resource.getLocks().length);
183 lockManager.createLock(info, resource);
185 assertEquals(1, resource.getLocks().length);
187 ActiveLock lock = resource.getLocks()[0];
189 lockManager.releaseLock(lock.getToken(), resource);
191 assertEquals(0, resource.getLocks().length);
194 public void testUnlockThrowsDavExceptionIfNotLocked()
197 LockInfo info = new LockInfo(Scope.EXCLUSIVE, Type.WRITE, "/", 0, false);
199 assertEquals(0, resource.getLocks().length);
201 lockManager.createLock(info, resource);
203 assertEquals(1, resource.getLocks().length);
207 lockManager.releaseLock("BLAH", resource);
208 fail("Did not throw DavException");
210 catch (DavException e)
212 assertEquals(DavServletResponse.SC_LOCKED, e.getErrorCode());
215 assertEquals(1, resource.getLocks().length);
218 public void testUnlockThrowsDavExceptionIfResourceNotLocked()
221 assertEquals(0, resource.getLocks().length);
225 lockManager.releaseLock("BLAH", resource);
226 fail("Did not throw DavException");
228 catch (DavException e)
230 assertEquals(DavServletResponse.SC_PRECONDITION_FAILED, e.getErrorCode());
233 assertEquals(0, resource.getLocks().length);