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