]> source.dussan.org Git - archiva.git/blob
511cbeb12d6362f0eecc4a742d097ebe1741be98
[archiva.git] /
1 package org.apache.maven.archiva.webdav;
2
3 /*
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
11  *
12  *  http://www.apache.org/licenses/LICENSE-2.0
13  *
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
19  * under the License.
20  */
21
22 import java.io.File;
23
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;
43
44 import edu.emory.mathcs.backport.java.util.Collections;
45
46 public class DavResourceTest extends PlexusInSpringTestCase
47 {
48     private DavSession session;
49     
50     private MimeTypes mimeTypes;
51     
52     private ArchivaDavResourceLocator resourceLocator;
53     
54     private DavResourceFactory resourceFactory;
55     
56     private File baseDir;
57     
58     private final String REPOPATH = "myresource.jar";
59     
60     private File myResource;
61     
62     private DavResource resource;
63     
64     private LockManager lockManager;
65
66     private RepositoryContentConsumers consumers;
67
68     private ManagedRepositoryConfiguration repository = new ManagedRepositoryConfiguration();
69     
70     @Override
71     protected void setUp()
72         throws Exception
73     {
74         super.setUp();
75         session = new ArchivaDavSession();
76         mimeTypes = (MimeTypes)getApplicationContext().getBean(PlexusToSpringUtils.buildSpringId(MimeTypes.class));
77         baseDir = getTestFile("target/DavResourceTest");
78         baseDir.mkdirs();
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");
87     }
88
89     @Override
90     protected void tearDown()
91         throws Exception
92     {
93         super.tearDown();
94         release(mimeTypes);
95         FileUtils.deleteDirectory(baseDir);
96     }
97     
98     private DavResource getDavResource(String logicalPath, File file)
99     {
100         return new ArchivaDavResource( file.getAbsolutePath(), logicalPath, repository, session, resourceLocator,
101                                        resourceFactory, mimeTypes, Collections.emptyList(), consumers );
102     }
103     
104     public void testDeleteNonExistantResourceShould404()
105         throws Exception
106     {
107         File dir = new File(baseDir, "testdir");
108         try
109         {
110             DavResource directoryResource = getDavResource("/testdir", dir);
111             directoryResource.getCollection().removeMember(directoryResource);
112             fail("Did not throw DavException");
113         }
114         catch (DavException e)
115         {
116             assertEquals(DavServletResponse.SC_NOT_FOUND, e.getErrorCode());
117         }
118     }
119     
120     public void testDeleteCollection()
121         throws Exception
122     {
123         File dir = new File(baseDir, "testdir");
124         try
125         {
126             assertTrue(dir.mkdir());
127             DavResource directoryResource = getDavResource("/testdir", dir);
128             directoryResource.getCollection().removeMember(directoryResource);
129             assertFalse(dir.exists());
130         }
131         finally
132         {
133             FileUtils.deleteDirectory(dir);
134         }
135     }
136     
137     public void testDeleteResource()
138         throws Exception
139     {
140         assertTrue(myResource.exists());
141         resource.getCollection().removeMember(resource);
142         assertFalse(myResource.exists());
143     }
144     
145     public void testIsLockable()
146     {
147         assertTrue(resource.isLockable(Type.WRITE, Scope.EXCLUSIVE));
148         assertFalse(resource.isLockable(Type.WRITE, Scope.SHARED));
149     }
150     
151     public void testLock()
152         throws Exception
153     {
154         assertEquals(0, resource.getLocks().length);
155        
156         LockInfo info = new LockInfo(Scope.EXCLUSIVE, Type.WRITE, "/", 0, false);
157         lockManager.createLock(info, resource);
158         
159         assertEquals(1, resource.getLocks().length);
160     }
161     
162     public void testLockIfResourceUnlockable()
163         throws Exception
164     {
165         assertEquals(0, resource.getLocks().length);
166        
167         LockInfo info = new LockInfo(Scope.SHARED, Type.WRITE, "/", 0, false);
168         try
169         {
170             lockManager.createLock(info, resource);
171             fail("Did not throw dav exception");
172         }
173         catch (Exception e)
174         {
175             //Simple lock manager will die
176         }
177         assertEquals(0, resource.getLocks().length); 
178     }
179     
180     public void testGetLock()
181         throws Exception
182     {
183         LockInfo info = new LockInfo(Scope.EXCLUSIVE, Type.WRITE, "/", 0, false);
184         lockManager.createLock(info, resource);
185         
186         assertEquals(1, resource.getLocks().length);
187         
188         //Lock should exist
189         assertNotNull(resource.getLock(Type.WRITE, Scope.EXCLUSIVE));
190         
191         //Lock should not exist
192         assertNull(resource.getLock(Type.WRITE, Scope.SHARED));
193     }
194     
195     
196     public void testRefreshLockThrowsExceptionIfNoLockIsPresent()
197         throws Exception
198     {
199         LockInfo info = new LockInfo(Scope.EXCLUSIVE, Type.WRITE, "/", 0, false);
200         
201         assertEquals(0, resource.getLocks().length);       
202         
203         try
204         {
205             lockManager.refreshLock(info, "notoken", resource);
206             fail("Did not throw dav exception");
207         }
208         catch (DavException e)
209         {
210             assertEquals(DavServletResponse.SC_PRECONDITION_FAILED, e.getErrorCode());
211         }
212         
213         assertEquals(0, resource.getLocks().length);
214     }
215     
216     public void testRefreshLock()
217         throws Exception
218     {
219         LockInfo info = new LockInfo(Scope.EXCLUSIVE, Type.WRITE, "/", 0, false);
220         
221         assertEquals(0, resource.getLocks().length);
222         
223         lockManager.createLock(info, resource);
224         
225         assertEquals(1, resource.getLocks().length);
226         
227         ActiveLock lock = resource.getLocks()[0];
228
229         lockManager.refreshLock(info, lock.getToken(), resource);
230         
231         assertEquals(1, resource.getLocks().length);
232     }
233     
234     public void testUnlock()
235         throws Exception
236     {
237         LockInfo info = new LockInfo(Scope.EXCLUSIVE, Type.WRITE, "/", 0, false);
238         
239         assertEquals(0, resource.getLocks().length);
240         
241         lockManager.createLock(info, resource);
242         
243         assertEquals(1, resource.getLocks().length);
244         
245         ActiveLock lock = resource.getLocks()[0];
246
247         lockManager.releaseLock(lock.getToken(), resource);
248         
249         assertEquals(0, resource.getLocks().length);
250     }    
251     
252     public void testUnlockThrowsDavExceptionIfNotLocked()
253         throws Exception
254     {
255         LockInfo info = new LockInfo(Scope.EXCLUSIVE, Type.WRITE, "/", 0, false);
256         
257         assertEquals(0, resource.getLocks().length);
258         
259         lockManager.createLock(info, resource);
260         
261         assertEquals(1, resource.getLocks().length);
262
263         try
264         {
265             lockManager.releaseLock("BLAH", resource);
266             fail("Did not throw DavException");
267         }
268         catch (DavException e)
269         {
270             assertEquals(DavServletResponse.SC_LOCKED, e.getErrorCode());
271         }
272         
273         assertEquals(1, resource.getLocks().length);      
274     }
275     
276     public void testUnlockThrowsDavExceptionIfResourceNotLocked()
277         throws Exception
278     {        
279         assertEquals(0, resource.getLocks().length);
280
281         try
282         {
283             lockManager.releaseLock("BLAH", resource);
284             fail("Did not throw DavException");
285         }
286         catch (DavException e)
287         {
288             assertEquals(DavServletResponse.SC_PRECONDITION_FAILED, e.getErrorCode());
289         }
290         
291         assertEquals(0, resource.getLocks().length);      
292     }
293     
294     private class RootContextDavResourceFactory implements DavResourceFactory
295     {
296         public DavResource createResource(DavResourceLocator locator, DavServletRequest request, DavServletResponse response) throws DavException {
297             throw new UnsupportedOperationException("Not supported yet.");
298         }
299
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 );
303         }
304     }
305 }