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.security.ArchivaXworkUser;
41 import org.apache.maven.archiva.webdav.util.MimeTypes;
42 import org.codehaus.plexus.spring.PlexusInSpringTestCase;
43 import org.codehaus.plexus.spring.PlexusToSpringUtils;
45 import edu.emory.mathcs.backport.java.util.Collections;
47 public class DavResourceTest extends PlexusInSpringTestCase
49 private DavSession session;
51 private MimeTypes mimeTypes;
53 private ArchivaDavResourceLocator resourceLocator;
55 private DavResourceFactory resourceFactory;
59 private final String REPOPATH = "myresource.jar";
61 private File myResource;
63 private DavResource resource;
65 private LockManager lockManager;
67 private RepositoryContentConsumers consumers;
69 private ManagedRepositoryConfiguration repository = new ManagedRepositoryConfiguration();
71 private ArchivaXworkUser archivaXworkUser;
74 protected void setUp()
78 session = new ArchivaDavSession();
79 mimeTypes = (MimeTypes)getApplicationContext().getBean(PlexusToSpringUtils.buildSpringId(MimeTypes.class));
80 baseDir = getTestFile("target/DavResourceTest");
82 myResource = new File(baseDir, "myresource.jar");
83 assertTrue("Could not create " + myResource.getAbsolutePath(), myResource.createNewFile());
84 resourceFactory = new RootContextDavResourceFactory();
85 resourceLocator = (ArchivaDavResourceLocator)new ArchivaDavLocatorFactory().createResourceLocator("/", REPOPATH);
86 resource = getDavResource(resourceLocator.getHref(false), myResource);
87 lockManager = new SimpleLockManager();
88 resource.addLockManager(lockManager);
89 consumers = (RepositoryContentConsumers)getApplicationContext().getBean("repositoryContentConsumers");
90 archivaXworkUser = (ArchivaXworkUser) getApplicationContext().getBean( PlexusToSpringUtils.buildSpringId( ArchivaXworkUser.class ) );
94 protected void tearDown()
99 FileUtils.deleteDirectory(baseDir);
102 private DavResource getDavResource(String logicalPath, File file)
104 return new ArchivaDavResource( file.getAbsolutePath(), logicalPath, repository, session, resourceLocator,
105 resourceFactory, mimeTypes, Collections.emptyList(), consumers, archivaXworkUser );
108 public void testDeleteNonExistantResourceShould404()
111 File dir = new File(baseDir, "testdir");
114 DavResource directoryResource = getDavResource("/testdir", dir);
115 directoryResource.getCollection().removeMember(directoryResource);
116 fail("Did not throw DavException");
118 catch (DavException e)
120 assertEquals(DavServletResponse.SC_NOT_FOUND, e.getErrorCode());
124 public void testDeleteCollection()
127 File dir = new File(baseDir, "testdir");
130 assertTrue(dir.mkdir());
131 DavResource directoryResource = getDavResource("/testdir", dir);
132 directoryResource.getCollection().removeMember(directoryResource);
133 assertFalse(dir.exists());
137 FileUtils.deleteDirectory(dir);
141 public void testDeleteResource()
144 assertTrue(myResource.exists());
145 resource.getCollection().removeMember(resource);
146 assertFalse(myResource.exists());
149 public void testIsLockable()
151 assertTrue(resource.isLockable(Type.WRITE, Scope.EXCLUSIVE));
152 assertFalse(resource.isLockable(Type.WRITE, Scope.SHARED));
155 public void testLock()
158 assertEquals(0, resource.getLocks().length);
160 LockInfo info = new LockInfo(Scope.EXCLUSIVE, Type.WRITE, "/", 0, false);
161 lockManager.createLock(info, resource);
163 assertEquals(1, resource.getLocks().length);
166 public void testLockIfResourceUnlockable()
169 assertEquals(0, resource.getLocks().length);
171 LockInfo info = new LockInfo(Scope.SHARED, Type.WRITE, "/", 0, false);
174 lockManager.createLock(info, resource);
175 fail("Did not throw dav exception");
179 //Simple lock manager will die
181 assertEquals(0, resource.getLocks().length);
184 public void testGetLock()
187 LockInfo info = new LockInfo(Scope.EXCLUSIVE, Type.WRITE, "/", 0, false);
188 lockManager.createLock(info, resource);
190 assertEquals(1, resource.getLocks().length);
193 assertNotNull(resource.getLock(Type.WRITE, Scope.EXCLUSIVE));
195 //Lock should not exist
196 assertNull(resource.getLock(Type.WRITE, Scope.SHARED));
200 public void testRefreshLockThrowsExceptionIfNoLockIsPresent()
203 LockInfo info = new LockInfo(Scope.EXCLUSIVE, Type.WRITE, "/", 0, false);
205 assertEquals(0, resource.getLocks().length);
209 lockManager.refreshLock(info, "notoken", resource);
210 fail("Did not throw dav exception");
212 catch (DavException e)
214 assertEquals(DavServletResponse.SC_PRECONDITION_FAILED, e.getErrorCode());
217 assertEquals(0, resource.getLocks().length);
220 public void testRefreshLock()
223 LockInfo info = new LockInfo(Scope.EXCLUSIVE, Type.WRITE, "/", 0, false);
225 assertEquals(0, resource.getLocks().length);
227 lockManager.createLock(info, resource);
229 assertEquals(1, resource.getLocks().length);
231 ActiveLock lock = resource.getLocks()[0];
233 lockManager.refreshLock(info, lock.getToken(), resource);
235 assertEquals(1, resource.getLocks().length);
238 public void testUnlock()
241 LockInfo info = new LockInfo(Scope.EXCLUSIVE, Type.WRITE, "/", 0, false);
243 assertEquals(0, resource.getLocks().length);
245 lockManager.createLock(info, resource);
247 assertEquals(1, resource.getLocks().length);
249 ActiveLock lock = resource.getLocks()[0];
251 lockManager.releaseLock(lock.getToken(), resource);
253 assertEquals(0, resource.getLocks().length);
256 public void testUnlockThrowsDavExceptionIfNotLocked()
259 LockInfo info = new LockInfo(Scope.EXCLUSIVE, Type.WRITE, "/", 0, false);
261 assertEquals(0, resource.getLocks().length);
263 lockManager.createLock(info, resource);
265 assertEquals(1, resource.getLocks().length);
269 lockManager.releaseLock("BLAH", resource);
270 fail("Did not throw DavException");
272 catch (DavException e)
274 assertEquals(DavServletResponse.SC_LOCKED, e.getErrorCode());
277 assertEquals(1, resource.getLocks().length);
280 public void testUnlockThrowsDavExceptionIfResourceNotLocked()
283 assertEquals(0, resource.getLocks().length);
287 lockManager.releaseLock("BLAH", resource);
288 fail("Did not throw DavException");
290 catch (DavException e)
292 assertEquals(DavServletResponse.SC_PRECONDITION_FAILED, e.getErrorCode());
295 assertEquals(0, resource.getLocks().length);
298 private class RootContextDavResourceFactory implements DavResourceFactory
300 public DavResource createResource(DavResourceLocator locator, DavServletRequest request, DavServletResponse response) throws DavException {
301 throw new UnsupportedOperationException("Not supported yet.");
304 public DavResource createResource(DavResourceLocator locator, DavSession session) throws DavException {
305 return new ArchivaDavResource( baseDir.getAbsolutePath(), "/", repository, session, resourceLocator,
306 resourceFactory, mimeTypes, Collections.emptyList(), consumers, archivaXworkUser );