View Javadoc
1   package org.apache.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 junit.framework.TestCase;
23  import org.apache.archiva.admin.model.beans.ManagedRepository;
24  import org.apache.archiva.repository.events.AuditListener;
25  import org.apache.archiva.common.filelock.FileLockManager;
26  import org.apache.commons.io.FileUtils;
27  import org.apache.jackrabbit.webdav.DavException;
28  import org.apache.jackrabbit.webdav.DavResource;
29  import org.apache.jackrabbit.webdav.DavResourceFactory;
30  import org.apache.jackrabbit.webdav.DavResourceLocator;
31  import org.apache.jackrabbit.webdav.DavServletRequest;
32  import org.apache.jackrabbit.webdav.DavServletResponse;
33  import org.apache.jackrabbit.webdav.DavSession;
34  import org.apache.jackrabbit.webdav.lock.ActiveLock;
35  import org.apache.jackrabbit.webdav.lock.LockInfo;
36  import org.apache.jackrabbit.webdav.lock.LockManager;
37  import org.apache.jackrabbit.webdav.lock.Scope;
38  import org.apache.jackrabbit.webdav.lock.SimpleLockManager;
39  import org.apache.jackrabbit.webdav.lock.Type;
40  import org.apache.archiva.webdav.util.MimeTypes;
41  import org.junit.After;
42  import org.junit.Before;
43  import org.junit.Test;
44  import org.junit.runner.RunWith;
45  import org.springframework.test.context.ContextConfiguration;
46  
47  import javax.inject.Inject;
48  import java.io.File;
49  import java.util.Collections;
50  import org.apache.archiva.test.utils.ArchivaSpringJUnit4ClassRunner;
51  
52  @RunWith( ArchivaSpringJUnit4ClassRunner.class )
53  @ContextConfiguration( locations = { "classpath*:/META-INF/spring-context.xml", "classpath*:/spring-context.xml" } )
54  public class DavResourceTest
55      extends TestCase
56  {
57      private DavSession session;
58  
59      @Inject
60      private MimeTypes mimeTypes;
61  
62      @Inject
63      private FileLockManager fileLockManager;
64  
65      private ArchivaDavResourceLocator resourceLocator;
66  
67      private DavResourceFactory resourceFactory;
68  
69      private File baseDir;
70  
71      private final String REPOPATH = "myresource.jar";
72  
73      private File myResource;
74  
75      private DavResource resource;
76  
77      private LockManager lockManager;
78  
79      private ManagedRepository repository = new ManagedRepository();
80      
81      @Override
82      @Before
83      public void setUp()
84          throws Exception
85      {
86          super.setUp();
87          session = new ArchivaDavSession();
88          baseDir = new File( "target/DavResourceTest" );
89          baseDir.mkdirs();
90          myResource = new File( baseDir, "myresource.jar" );
91          assertTrue( "Could not create " + myResource.getAbsolutePath(), myResource.createNewFile() );
92          resourceFactory = new RootContextDavResourceFactory();
93          
94          resourceLocator =
95              (ArchivaDavResourceLocator) new ArchivaDavLocatorFactory().createResourceLocator( "/", REPOPATH );        
96          resource = getDavResource( resourceLocator.getHref( false ), myResource );
97          lockManager = new SimpleLockManager();
98          resource.addLockManager( lockManager );        
99      }
100 
101     @After
102     @Override
103     public void tearDown()
104         throws Exception
105     {
106         super.tearDown();
107         FileUtils.deleteDirectory( baseDir );
108     }
109 
110     private DavResource getDavResource( String logicalPath, File file )
111     {
112         return new ArchivaDavResource( file.getAbsolutePath(), logicalPath, repository, session, resourceLocator,
113                                        resourceFactory, mimeTypes, Collections.<AuditListener> emptyList(), null, fileLockManager );
114     }
115 
116     @Test
117     public void testDeleteNonExistantResourceShould404()
118         throws Exception
119     {
120         File dir = new File( baseDir, "testdir" );
121         try
122         {
123             DavResource directoryResource = getDavResource( "/testdir", dir );
124             directoryResource.getCollection().removeMember( directoryResource );
125             fail( "Did not throw DavException" );
126         }
127         catch ( DavException e )
128         {
129             assertEquals( DavServletResponse.SC_NOT_FOUND, e.getErrorCode() );
130         }
131     }
132 
133     @Test
134     public void testDeleteCollection()
135         throws Exception
136     {
137         File dir = new File( baseDir, "testdir" );
138         try
139         {
140             assertTrue( dir.mkdir() );
141             DavResource directoryResource = getDavResource( "/testdir", dir );
142             directoryResource.getCollection().removeMember( directoryResource );
143             assertFalse( dir.exists() );
144         }
145         finally
146         {
147             FileUtils.deleteDirectory( dir );
148         }
149     }
150 
151     @Test
152     public void testDeleteResource()
153         throws Exception
154     {
155         assertTrue( myResource.exists() );
156         resource.getCollection().removeMember( resource );
157         assertFalse( myResource.exists() );
158     }
159 
160     @Test
161     public void testIsLockable()
162     {
163         assertTrue( resource.isLockable( Type.WRITE, Scope.EXCLUSIVE ) );
164         assertFalse( resource.isLockable( Type.WRITE, Scope.SHARED ) );
165     }
166 
167     @Test
168     public void testLock()
169         throws Exception
170     {
171         assertEquals( 0, resource.getLocks().length );
172 
173         LockInfo info = new LockInfo( Scope.EXCLUSIVE, Type.WRITE, "/", 0, false );
174         lockManager.createLock( info, resource );
175 
176         assertEquals( 1, resource.getLocks().length );
177     }
178 
179     @Test
180     public void testLockIfResourceUnlockable()
181         throws Exception
182     {
183         assertEquals( 0, resource.getLocks().length );
184 
185         LockInfo info = new LockInfo( Scope.SHARED, Type.WRITE, "/", 0, false );
186         try
187         {
188             lockManager.createLock( info, resource );
189             fail( "Did not throw dav exception" );
190         }
191         catch ( Exception e )
192         {
193             // Simple lock manager will die
194         }
195         assertEquals( 0, resource.getLocks().length );
196     }
197 
198     @Test
199     public void testGetLock()
200         throws Exception
201     {
202         LockInfo info = new LockInfo( Scope.EXCLUSIVE, Type.WRITE, "/", 0, false );
203         lockManager.createLock( info, resource );
204 
205         assertEquals( 1, resource.getLocks().length );
206 
207         // Lock should exist
208         assertNotNull( resource.getLock( Type.WRITE, Scope.EXCLUSIVE ) );
209 
210         // Lock should not exist
211         assertNull( resource.getLock( Type.WRITE, Scope.SHARED ) );
212     }
213 
214     @Test
215     public void testRefreshLockThrowsExceptionIfNoLockIsPresent()
216         throws Exception
217     {
218         LockInfo info = new LockInfo( Scope.EXCLUSIVE, Type.WRITE, "/", 0, false );
219 
220         assertEquals( 0, resource.getLocks().length );
221 
222         try
223         {
224             lockManager.refreshLock( info, "notoken", resource );
225             fail( "Did not throw dav exception" );
226         }
227         catch ( DavException e )
228         {
229             assertEquals( DavServletResponse.SC_PRECONDITION_FAILED, e.getErrorCode() );
230         }
231 
232         assertEquals( 0, resource.getLocks().length );
233     }
234 
235     @Test
236     public void testRefreshLock()
237         throws Exception
238     {
239         LockInfo info = new LockInfo( Scope.EXCLUSIVE, Type.WRITE, "/", 0, false );
240 
241         assertEquals( 0, resource.getLocks().length );
242 
243         lockManager.createLock( info, resource );
244 
245         assertEquals( 1, resource.getLocks().length );
246 
247         ActiveLock lock = resource.getLocks()[0];
248 
249         lockManager.refreshLock( info, lock.getToken(), resource );
250 
251         assertEquals( 1, resource.getLocks().length );
252     }
253 
254     @Test
255     public void testUnlock()
256         throws Exception
257     {
258         LockInfo info = new LockInfo( Scope.EXCLUSIVE, Type.WRITE, "/", 0, false );
259 
260         assertEquals( 0, resource.getLocks().length );
261 
262         lockManager.createLock( info, resource );
263 
264         assertEquals( 1, resource.getLocks().length );
265 
266         ActiveLock lock = resource.getLocks()[0];
267 
268         lockManager.releaseLock( lock.getToken(), resource );
269 
270         assertEquals( 0, resource.getLocks().length );
271     }
272 
273     @Test
274     public void testUnlockThrowsDavExceptionIfNotLocked()
275         throws Exception
276     {
277         LockInfo info = new LockInfo( Scope.EXCLUSIVE, Type.WRITE, "/", 0, false );
278 
279         assertEquals( 0, resource.getLocks().length );
280 
281         lockManager.createLock( info, resource );
282 
283         assertEquals( 1, 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_LOCKED, e.getErrorCode() );
293         }
294 
295         assertEquals( 1, resource.getLocks().length );
296     }
297 
298     @Test
299     public void testUnlockThrowsDavExceptionIfResourceNotLocked()
300         throws Exception
301     {
302         assertEquals( 0, resource.getLocks().length );
303 
304         try
305         {
306             lockManager.releaseLock( "BLAH", resource );
307             fail( "Did not throw DavException" );
308         }
309         catch ( DavException e )
310         {
311             assertEquals( DavServletResponse.SC_PRECONDITION_FAILED, e.getErrorCode() );
312         }
313 
314         assertEquals( 0, resource.getLocks().length );
315     }
316 
317     private class RootContextDavResourceFactory
318         implements DavResourceFactory
319     {
320         @Override
321         public DavResource createResource( DavResourceLocator locator, DavServletRequest request,
322                                            DavServletResponse response )
323             throws DavException
324         {
325             throw new UnsupportedOperationException( "Not supported yet." );
326         }
327 
328         @Override
329         public DavResource createResource( DavResourceLocator locator, DavSession session )
330             throws DavException
331         {
332             return new ArchivaDavResource( baseDir.getAbsolutePath(), "/", repository, session, resourceLocator,
333                                            resourceFactory, mimeTypes, Collections.<AuditListener> emptyList(),
334                                            null, fileLockManager );
335         }
336     }
337 }