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;
37 import quicktime.std.qtcomponents.SCInfo;
40 public class DavResourceTest extends PlexusInSpringTestCase
42 private DavSession session;
44 private MimeTypes mimeTypes;
46 private ArchivaDavResourceLocator resourceLocator;
48 private ArchivaDavResourceFactory factory;
52 private final String REPOPATH = "/myresource.jar";
54 private final File myResource = new File(baseDir, REPOPATH);
56 private DavResource resource;
58 private LockManager lockManager;
61 protected void setUp()
65 session = new ArchivaDavSession();
66 mimeTypes = (MimeTypes)getApplicationContext().getBean(PlexusToSpringUtils.buildSpringId(MimeTypes.class));
67 baseDir = new File("target/DavResourceTest");
69 myResource.createNewFile();
70 resourceLocator = (ArchivaDavResourceLocator)new ArchivaDavLocatorFactory().createResourceLocator("/", REPOPATH);
71 resource = getDavResource(REPOPATH, myResource);
72 lockManager = new SimpleLockManager();
73 resource.addLockManager(lockManager);
77 protected void tearDown()
82 FileUtils.deleteDirectory(baseDir);
85 private DavResource getDavResource(String logicalPath, File file)
87 return new ArchivaDavResource(logicalPath, file.getAbsolutePath(), mimeTypes, session, resourceLocator, null);
90 public void testIsLockable()
92 assertTrue(resource.isLockable(Type.WRITE, Scope.EXCLUSIVE));
93 assertFalse(resource.isLockable(Type.WRITE, Scope.SHARED));
96 public void testLock()
99 assertEquals(0, resource.getLocks().length);
101 LockInfo info = new LockInfo(Scope.EXCLUSIVE, Type.WRITE, "/", 0, false);
102 lockManager.createLock(info, resource);
104 assertEquals(1, resource.getLocks().length);
107 public void testLockIfResourceUnlockable()
110 assertEquals(0, resource.getLocks().length);
112 LockInfo info = new LockInfo(Scope.SHARED, Type.WRITE, "/", 0, false);
115 lockManager.createLock(info, resource);
116 fail("Did not throw dav exception");
120 //Simple lock manager will die
122 assertEquals(0, resource.getLocks().length);
125 public void testGetLock()
128 LockInfo info = new LockInfo(Scope.EXCLUSIVE, Type.WRITE, "/", 0, false);
129 lockManager.createLock(info, resource);
131 assertEquals(1, resource.getLocks().length);
134 assertNotNull(resource.getLock(Type.WRITE, Scope.EXCLUSIVE));
136 //Lock should not exist
137 assertNull(resource.getLock(Type.WRITE, Scope.SHARED));
141 public void testRefreshLockThrowsExceptionIfNoLockIsPresent()
144 LockInfo info = new LockInfo(Scope.EXCLUSIVE, Type.WRITE, "/", 0, false);
146 assertEquals(0, resource.getLocks().length);
150 lockManager.refreshLock(info, "notoken", resource);
151 fail("Did not throw dav exception");
153 catch (DavException e)
155 assertEquals(DavServletResponse.SC_PRECONDITION_FAILED, e.getErrorCode());
158 assertEquals(0, resource.getLocks().length);
161 public void testRefreshLock()
164 LockInfo info = new LockInfo(Scope.EXCLUSIVE, Type.WRITE, "/", 0, false);
166 assertEquals(0, resource.getLocks().length);
168 lockManager.createLock(info, resource);
170 assertEquals(1, resource.getLocks().length);
172 ActiveLock lock = resource.getLocks()[0];
174 lockManager.refreshLock(info, lock.getToken(), resource);
176 assertEquals(1, resource.getLocks().length);
179 public void testUnlock()
182 LockInfo info = new LockInfo(Scope.EXCLUSIVE, Type.WRITE, "/", 0, false);
184 assertEquals(0, resource.getLocks().length);
186 lockManager.createLock(info, resource);
188 assertEquals(1, resource.getLocks().length);
190 ActiveLock lock = resource.getLocks()[0];
192 lockManager.releaseLock(lock.getToken(), resource);
194 assertEquals(0, resource.getLocks().length);
197 public void testUnlockThrowsDavExceptionIfNotLocked()
200 LockInfo info = new LockInfo(Scope.EXCLUSIVE, Type.WRITE, "/", 0, false);
202 assertEquals(0, resource.getLocks().length);
204 lockManager.createLock(info, resource);
206 assertEquals(1, resource.getLocks().length);
210 lockManager.releaseLock("BLAH", resource);
211 fail("Did not throw DavException");
213 catch (DavException e)
215 assertEquals(DavServletResponse.SC_LOCKED, e.getErrorCode());
218 assertEquals(1, resource.getLocks().length);
221 public void testUnlockThrowsDavExceptionIfResourceNotLocked()
224 assertEquals(0, resource.getLocks().length);
228 lockManager.releaseLock("BLAH", resource);
229 fail("Did not throw DavException");
231 catch (DavException e)
233 assertEquals(DavServletResponse.SC_PRECONDITION_FAILED, e.getErrorCode());
236 assertEquals(0, resource.getLocks().length);