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;
46 private ArchivaDavResourceFactory factory;
50 private final String REPOPATH = "/myresource.jar";
52 private File myResource;
54 private DavResource resource;
56 private LockManager lockManager;
59 protected void setUp()
63 session = new ArchivaDavSession();
64 mimeTypes = (MimeTypes)getApplicationContext().getBean(PlexusToSpringUtils.buildSpringId(MimeTypes.class));
65 baseDir = getTestFile("target/DavResourceTest");
67 myResource = new File(baseDir, "myresource.jar");
68 myResource.createNewFile();
69 resourceLocator = (ArchivaDavResourceLocator)new ArchivaDavLocatorFactory().createResourceLocator("/", REPOPATH);
70 resource = getDavResource(REPOPATH, myResource);
71 lockManager = new SimpleLockManager();
72 resource.addLockManager(lockManager);
76 protected void tearDown()
81 FileUtils.deleteDirectory(baseDir);
84 private DavResource getDavResource(String logicalPath, File file)
86 return new ArchivaDavResource(logicalPath, file.getAbsolutePath(), mimeTypes, session, resourceLocator, null);
89 public void testIsLockable()
91 assertTrue(resource.isLockable(Type.WRITE, Scope.EXCLUSIVE));
92 assertFalse(resource.isLockable(Type.WRITE, Scope.SHARED));
95 public void testLock()
98 assertEquals(0, resource.getLocks().length);
100 LockInfo info = new LockInfo(Scope.EXCLUSIVE, Type.WRITE, "/", 0, false);
101 lockManager.createLock(info, resource);
103 assertEquals(1, resource.getLocks().length);
106 public void testLockIfResourceUnlockable()
109 assertEquals(0, resource.getLocks().length);
111 LockInfo info = new LockInfo(Scope.SHARED, Type.WRITE, "/", 0, false);
114 lockManager.createLock(info, resource);
115 fail("Did not throw dav exception");
119 //Simple lock manager will die
121 assertEquals(0, resource.getLocks().length);
124 public void testGetLock()
127 LockInfo info = new LockInfo(Scope.EXCLUSIVE, Type.WRITE, "/", 0, false);
128 lockManager.createLock(info, resource);
130 assertEquals(1, resource.getLocks().length);
133 assertNotNull(resource.getLock(Type.WRITE, Scope.EXCLUSIVE));
135 //Lock should not exist
136 assertNull(resource.getLock(Type.WRITE, Scope.SHARED));
140 public void testRefreshLockThrowsExceptionIfNoLockIsPresent()
143 LockInfo info = new LockInfo(Scope.EXCLUSIVE, Type.WRITE, "/", 0, false);
145 assertEquals(0, resource.getLocks().length);
149 lockManager.refreshLock(info, "notoken", resource);
150 fail("Did not throw dav exception");
152 catch (DavException e)
154 assertEquals(DavServletResponse.SC_PRECONDITION_FAILED, e.getErrorCode());
157 assertEquals(0, resource.getLocks().length);
160 public void testRefreshLock()
163 LockInfo info = new LockInfo(Scope.EXCLUSIVE, Type.WRITE, "/", 0, false);
165 assertEquals(0, resource.getLocks().length);
167 lockManager.createLock(info, resource);
169 assertEquals(1, resource.getLocks().length);
171 ActiveLock lock = resource.getLocks()[0];
173 lockManager.refreshLock(info, lock.getToken(), resource);
175 assertEquals(1, resource.getLocks().length);
178 public void testUnlock()
181 LockInfo info = new LockInfo(Scope.EXCLUSIVE, Type.WRITE, "/", 0, false);
183 assertEquals(0, resource.getLocks().length);
185 lockManager.createLock(info, resource);
187 assertEquals(1, resource.getLocks().length);
189 ActiveLock lock = resource.getLocks()[0];
191 lockManager.releaseLock(lock.getToken(), resource);
193 assertEquals(0, resource.getLocks().length);
196 public void testUnlockThrowsDavExceptionIfNotLocked()
199 LockInfo info = new LockInfo(Scope.EXCLUSIVE, Type.WRITE, "/", 0, false);
201 assertEquals(0, resource.getLocks().length);
203 lockManager.createLock(info, resource);
205 assertEquals(1, resource.getLocks().length);
209 lockManager.releaseLock("BLAH", resource);
210 fail("Did not throw DavException");
212 catch (DavException e)
214 assertEquals(DavServletResponse.SC_LOCKED, e.getErrorCode());
217 assertEquals(1, resource.getLocks().length);
220 public void testUnlockThrowsDavExceptionIfResourceNotLocked()
223 assertEquals(0, resource.getLocks().length);
227 lockManager.releaseLock("BLAH", resource);
228 fail("Did not throw DavException");
230 catch (DavException e)
232 assertEquals(DavServletResponse.SC_PRECONDITION_FAILED, e.getErrorCode());
235 assertEquals(0, resource.getLocks().length);