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
24 import org.apache.commons.io.FileUtils;
25 import org.apache.jackrabbit.webdav.DavException;
26 import org.apache.jackrabbit.webdav.DavResource;
27 import org.apache.jackrabbit.webdav.DavResourceFactory;
28 import org.apache.jackrabbit.webdav.DavResourceLocator;
29 import org.apache.jackrabbit.webdav.DavServletRequest;
30 import org.apache.jackrabbit.webdav.DavServletResponse;
31 import org.apache.jackrabbit.webdav.DavSession;
32 import org.apache.jackrabbit.webdav.lock.ActiveLock;
33 import org.apache.jackrabbit.webdav.lock.LockInfo;
34 import org.apache.jackrabbit.webdav.lock.LockManager;
35 import org.apache.jackrabbit.webdav.lock.Scope;
36 import org.apache.jackrabbit.webdav.lock.SimpleLockManager;
37 import org.apache.jackrabbit.webdav.lock.Type;
38 import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration;
39 import org.apache.maven.archiva.repository.scanner.RepositoryContentConsumers;
40 import org.apache.maven.archiva.webdav.util.MimeTypes;
41 import org.codehaus.plexus.spring.PlexusInSpringTestCase;
42 import org.codehaus.plexus.spring.PlexusToSpringUtils;
44 import edu.emory.mathcs.backport.java.util.Collections;
46 public class DavResourceTest 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 RepositoryContentConsumers consumers;
68 private ManagedRepositoryConfiguration repository = new ManagedRepositoryConfiguration();
71 protected void setUp()
75 session = new ArchivaDavSession();
76 mimeTypes = (MimeTypes)getApplicationContext().getBean(PlexusToSpringUtils.buildSpringId(MimeTypes.class));
77 baseDir = getTestFile("target/DavResourceTest");
79 myResource = new File(baseDir, "myresource.jar");
80 assertTrue("Could not create " + myResource.getAbsolutePath(), myResource.createNewFile());
81 resourceFactory = new RootContextDavResourceFactory();
82 resourceLocator = (ArchivaDavResourceLocator)new ArchivaDavLocatorFactory().createResourceLocator("/", REPOPATH);
83 resource = getDavResource(resourceLocator.getHref(false), myResource);
84 lockManager = new SimpleLockManager();
85 resource.addLockManager(lockManager);
86 consumers = (RepositoryContentConsumers)getApplicationContext().getBean("repositoryContentConsumers");
90 protected void tearDown()
95 FileUtils.deleteDirectory(baseDir);
98 private DavResource getDavResource(String logicalPath, File file)
100 return new ArchivaDavResource( file.getAbsolutePath(), logicalPath, repository, session, resourceLocator,
101 resourceFactory, mimeTypes, Collections.emptyList(), consumers );
104 public void testDeleteNonExistantResourceShould404()
107 File dir = new File(baseDir, "testdir");
110 DavResource directoryResource = getDavResource("/testdir", dir);
111 directoryResource.getCollection().removeMember(directoryResource);
112 fail("Did not throw DavException");
114 catch (DavException e)
116 assertEquals(DavServletResponse.SC_NOT_FOUND, e.getErrorCode());
120 public void testDeleteCollection()
123 File dir = new File(baseDir, "testdir");
126 assertTrue(dir.mkdir());
127 DavResource directoryResource = getDavResource("/testdir", dir);
128 directoryResource.getCollection().removeMember(directoryResource);
129 assertFalse(dir.exists());
133 FileUtils.deleteDirectory(dir);
137 public void testDeleteResource()
140 assertTrue(myResource.exists());
141 resource.getCollection().removeMember(resource);
142 assertFalse(myResource.exists());
145 public void testIsLockable()
147 assertTrue(resource.isLockable(Type.WRITE, Scope.EXCLUSIVE));
148 assertFalse(resource.isLockable(Type.WRITE, Scope.SHARED));
151 public void testLock()
154 assertEquals(0, resource.getLocks().length);
156 LockInfo info = new LockInfo(Scope.EXCLUSIVE, Type.WRITE, "/", 0, false);
157 lockManager.createLock(info, resource);
159 assertEquals(1, resource.getLocks().length);
162 public void testLockIfResourceUnlockable()
165 assertEquals(0, resource.getLocks().length);
167 LockInfo info = new LockInfo(Scope.SHARED, Type.WRITE, "/", 0, false);
170 lockManager.createLock(info, resource);
171 fail("Did not throw dav exception");
175 //Simple lock manager will die
177 assertEquals(0, resource.getLocks().length);
180 public void testGetLock()
183 LockInfo info = new LockInfo(Scope.EXCLUSIVE, Type.WRITE, "/", 0, false);
184 lockManager.createLock(info, resource);
186 assertEquals(1, resource.getLocks().length);
189 assertNotNull(resource.getLock(Type.WRITE, Scope.EXCLUSIVE));
191 //Lock should not exist
192 assertNull(resource.getLock(Type.WRITE, Scope.SHARED));
196 public void testRefreshLockThrowsExceptionIfNoLockIsPresent()
199 LockInfo info = new LockInfo(Scope.EXCLUSIVE, Type.WRITE, "/", 0, false);
201 assertEquals(0, resource.getLocks().length);
205 lockManager.refreshLock(info, "notoken", resource);
206 fail("Did not throw dav exception");
208 catch (DavException e)
210 assertEquals(DavServletResponse.SC_PRECONDITION_FAILED, e.getErrorCode());
213 assertEquals(0, resource.getLocks().length);
216 public void testRefreshLock()
219 LockInfo info = new LockInfo(Scope.EXCLUSIVE, Type.WRITE, "/", 0, false);
221 assertEquals(0, resource.getLocks().length);
223 lockManager.createLock(info, resource);
225 assertEquals(1, resource.getLocks().length);
227 ActiveLock lock = resource.getLocks()[0];
229 lockManager.refreshLock(info, lock.getToken(), resource);
231 assertEquals(1, resource.getLocks().length);
234 public void testUnlock()
237 LockInfo info = new LockInfo(Scope.EXCLUSIVE, Type.WRITE, "/", 0, false);
239 assertEquals(0, resource.getLocks().length);
241 lockManager.createLock(info, resource);
243 assertEquals(1, resource.getLocks().length);
245 ActiveLock lock = resource.getLocks()[0];
247 lockManager.releaseLock(lock.getToken(), resource);
249 assertEquals(0, resource.getLocks().length);
252 public void testUnlockThrowsDavExceptionIfNotLocked()
255 LockInfo info = new LockInfo(Scope.EXCLUSIVE, Type.WRITE, "/", 0, false);
257 assertEquals(0, resource.getLocks().length);
259 lockManager.createLock(info, resource);
261 assertEquals(1, resource.getLocks().length);
265 lockManager.releaseLock("BLAH", resource);
266 fail("Did not throw DavException");
268 catch (DavException e)
270 assertEquals(DavServletResponse.SC_LOCKED, e.getErrorCode());
273 assertEquals(1, resource.getLocks().length);
276 public void testUnlockThrowsDavExceptionIfResourceNotLocked()
279 assertEquals(0, resource.getLocks().length);
283 lockManager.releaseLock("BLAH", resource);
284 fail("Did not throw DavException");
286 catch (DavException e)
288 assertEquals(DavServletResponse.SC_PRECONDITION_FAILED, e.getErrorCode());
291 assertEquals(0, resource.getLocks().length);
294 private class RootContextDavResourceFactory implements DavResourceFactory
296 public DavResource createResource(DavResourceLocator locator, DavServletRequest request, DavServletResponse response) throws DavException {
297 throw new UnsupportedOperationException("Not supported yet.");
300 public DavResource createResource(DavResourceLocator locator, DavSession session) throws DavException {
301 return new ArchivaDavResource( baseDir.getAbsolutePath(), "/", repository, session, resourceLocator,
302 resourceFactory, mimeTypes, Collections.emptyList(), consumers );