]> source.dussan.org Git - archiva.git/blob
46cd3af722e86f83cdf2a0f8c85b71939934b598
[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 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.DavResourceFactory;
27 import org.apache.jackrabbit.webdav.DavResourceLocator;
28 import org.apache.jackrabbit.webdav.DavServletRequest;
29 import org.apache.jackrabbit.webdav.DavServletResponse;
30 import org.apache.jackrabbit.webdav.DavSession;
31 import org.apache.jackrabbit.webdav.lock.ActiveLock;
32 import org.apache.jackrabbit.webdav.lock.LockInfo;
33 import org.apache.jackrabbit.webdav.lock.LockManager;
34 import org.apache.jackrabbit.webdav.lock.Scope;
35 import org.apache.jackrabbit.webdav.lock.SimpleLockManager;
36 import org.apache.jackrabbit.webdav.lock.Type;
37 import org.apache.maven.archiva.webdav.util.MimeTypes;
38 import org.codehaus.plexus.spring.PlexusInSpringTestCase;
39 import org.codehaus.plexus.spring.PlexusToSpringUtils;
40
41 public class DavResourceTest extends PlexusInSpringTestCase
42 {
43     private DavSession session;
44     
45     private MimeTypes mimeTypes;
46     
47     private ArchivaDavResourceLocator resourceLocator;
48     
49     private DavResourceFactory resourceFactory;
50     
51     private File baseDir;
52     
53     private final String REPOPATH = "myresource.jar";
54     
55     private File myResource;
56     
57     private DavResource resource;
58     
59     private LockManager lockManager;
60     
61     @Override
62     protected void setUp()
63         throws Exception
64     {
65         super.setUp();
66         session = new ArchivaDavSession();
67         mimeTypes = (MimeTypes)getApplicationContext().getBean(PlexusToSpringUtils.buildSpringId(MimeTypes.class));
68         baseDir = getTestFile("target/DavResourceTest");
69         baseDir.mkdirs();
70         myResource = new File(baseDir, "myresource.jar");
71         assertTrue("Could not create " + myResource.getAbsolutePath(), myResource.createNewFile());
72         resourceFactory = new RootContextDavResourceFactory();
73         resourceLocator = (ArchivaDavResourceLocator)new ArchivaDavLocatorFactory().createResourceLocator("/", REPOPATH);
74         resource = getDavResource(resourceLocator.getHref(false), myResource);
75         lockManager = new SimpleLockManager();
76         resource.addLockManager(lockManager);
77     }
78
79     @Override
80     protected void tearDown()
81         throws Exception
82     {
83         super.tearDown();
84         release(mimeTypes);
85         FileUtils.deleteDirectory(baseDir);
86     }
87     
88     private DavResource getDavResource(String logicalPath, File file)
89     {
90         return new ArchivaDavResource(file.getAbsolutePath(), logicalPath, mimeTypes, session, resourceLocator, resourceFactory);
91     }
92     
93     public void testDeleteCollection()
94         throws Exception
95     {
96         File dir = new File(baseDir, "testdir");
97         try
98         {
99             assertTrue(dir.mkdir());
100             DavResource directoryResource = getDavResource("/testdir", dir);
101             directoryResource.getCollection().removeMember(directoryResource);
102             assertFalse(dir.exists());
103         }
104         finally
105         {
106             FileUtils.deleteDirectory(dir);
107         }
108     }
109     
110     public void testDeleteResource()
111         throws Exception
112     {
113         assertTrue(myResource.exists());
114         resource.getCollection().removeMember(resource);
115         assertFalse(myResource.exists());
116     }
117     
118     public void testIsLockable()
119     {
120         assertTrue(resource.isLockable(Type.WRITE, Scope.EXCLUSIVE));
121         assertFalse(resource.isLockable(Type.WRITE, Scope.SHARED));
122     }
123     
124     public void testLock()
125         throws Exception
126     {
127         assertEquals(0, resource.getLocks().length);
128        
129         LockInfo info = new LockInfo(Scope.EXCLUSIVE, Type.WRITE, "/", 0, false);
130         lockManager.createLock(info, resource);
131         
132         assertEquals(1, resource.getLocks().length);
133     }
134     
135     public void testLockIfResourceUnlockable()
136         throws Exception
137     {
138         assertEquals(0, resource.getLocks().length);
139        
140         LockInfo info = new LockInfo(Scope.SHARED, Type.WRITE, "/", 0, false);
141         try
142         {
143             lockManager.createLock(info, resource);
144             fail("Did not throw dav exception");
145         }
146         catch (Exception e)
147         {
148             //Simple lock manager will die
149         }
150         assertEquals(0, resource.getLocks().length); 
151     }
152     
153     public void testGetLock()
154         throws Exception
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         //Lock should exist
162         assertNotNull(resource.getLock(Type.WRITE, Scope.EXCLUSIVE));
163         
164         //Lock should not exist
165         assertNull(resource.getLock(Type.WRITE, Scope.SHARED));
166     }
167     
168     
169     public void testRefreshLockThrowsExceptionIfNoLockIsPresent()
170         throws Exception
171     {
172         LockInfo info = new LockInfo(Scope.EXCLUSIVE, Type.WRITE, "/", 0, false);
173         
174         assertEquals(0, resource.getLocks().length);       
175         
176         try
177         {
178             lockManager.refreshLock(info, "notoken", resource);
179             fail("Did not throw dav exception");
180         }
181         catch (DavException e)
182         {
183             assertEquals(DavServletResponse.SC_PRECONDITION_FAILED, e.getErrorCode());
184         }
185         
186         assertEquals(0, resource.getLocks().length);
187     }
188     
189     public void testRefreshLock()
190         throws Exception
191     {
192         LockInfo info = new LockInfo(Scope.EXCLUSIVE, Type.WRITE, "/", 0, false);
193         
194         assertEquals(0, resource.getLocks().length);
195         
196         lockManager.createLock(info, resource);
197         
198         assertEquals(1, resource.getLocks().length);
199         
200         ActiveLock lock = resource.getLocks()[0];
201
202         lockManager.refreshLock(info, lock.getToken(), resource);
203         
204         assertEquals(1, resource.getLocks().length);
205     }
206     
207     public void testUnlock()
208         throws Exception
209     {
210         LockInfo info = new LockInfo(Scope.EXCLUSIVE, Type.WRITE, "/", 0, false);
211         
212         assertEquals(0, resource.getLocks().length);
213         
214         lockManager.createLock(info, resource);
215         
216         assertEquals(1, resource.getLocks().length);
217         
218         ActiveLock lock = resource.getLocks()[0];
219
220         lockManager.releaseLock(lock.getToken(), resource);
221         
222         assertEquals(0, resource.getLocks().length);
223     }    
224     
225     public void testUnlockThrowsDavExceptionIfNotLocked()
226         throws Exception
227     {
228         LockInfo info = new LockInfo(Scope.EXCLUSIVE, Type.WRITE, "/", 0, false);
229         
230         assertEquals(0, resource.getLocks().length);
231         
232         lockManager.createLock(info, resource);
233         
234         assertEquals(1, resource.getLocks().length);
235
236         try
237         {
238             lockManager.releaseLock("BLAH", resource);
239             fail("Did not throw DavException");
240         }
241         catch (DavException e)
242         {
243             assertEquals(DavServletResponse.SC_LOCKED, e.getErrorCode());
244         }
245         
246         assertEquals(1, resource.getLocks().length);      
247     }
248     
249     public void testUnlockThrowsDavExceptionIfResourceNotLocked()
250         throws Exception
251     {        
252         assertEquals(0, resource.getLocks().length);
253
254         try
255         {
256             lockManager.releaseLock("BLAH", resource);
257             fail("Did not throw DavException");
258         }
259         catch (DavException e)
260         {
261             assertEquals(DavServletResponse.SC_PRECONDITION_FAILED, e.getErrorCode());
262         }
263         
264         assertEquals(0, resource.getLocks().length);      
265     }
266     
267     private class RootContextDavResourceFactory implements DavResourceFactory
268     {
269         public DavResource createResource(DavResourceLocator locator, DavServletRequest request, DavServletResponse response) throws DavException {
270             throw new UnsupportedOperationException("Not supported yet.");
271         }
272
273         public DavResource createResource(DavResourceLocator locator, DavSession session) throws DavException {
274             return new ArchivaDavResource(baseDir.getAbsolutePath(), "/", mimeTypes, session, resourceLocator, resourceFactory);
275         }
276     }
277 }