]> source.dussan.org Git - archiva.git/blob
6df8624f117b69924cc26531ed1001ad99693dcb
[archiva.git] /
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.common.filelock.FileLockManager;
24 import org.apache.archiva.common.utils.FileUtils;
25 import org.apache.archiva.repository.content.LayoutException;
26 import org.apache.archiva.repository.RepositoryRegistry;
27 import org.apache.archiva.repository.storage.fs.FilesystemAsset;
28 import org.apache.archiva.metadata.audit.AuditListener;
29 import org.apache.archiva.repository.maven.MavenManagedRepository;
30 import org.apache.archiva.repository.storage.fs.FilesystemStorage;
31 import org.apache.archiva.test.utils.ArchivaSpringJUnit4ClassRunner;
32 import org.apache.archiva.webdav.util.MimeTypes;
33 import org.apache.commons.lang3.StringUtils;
34 import org.apache.jackrabbit.webdav.DavException;
35 import org.apache.jackrabbit.webdav.DavResource;
36 import org.apache.jackrabbit.webdav.DavResourceFactory;
37 import org.apache.jackrabbit.webdav.DavResourceLocator;
38 import org.apache.jackrabbit.webdav.DavServletRequest;
39 import org.apache.jackrabbit.webdav.DavServletResponse;
40 import org.apache.jackrabbit.webdav.DavSession;
41 import org.apache.jackrabbit.webdav.lock.ActiveLock;
42 import org.apache.jackrabbit.webdav.lock.LockInfo;
43 import org.apache.jackrabbit.webdav.lock.LockManager;
44 import org.apache.jackrabbit.webdav.lock.Scope;
45 import org.apache.jackrabbit.webdav.lock.SimpleLockManager;
46 import org.apache.jackrabbit.webdav.lock.Type;
47 import org.junit.After;
48 import org.junit.Before;
49 import org.junit.Test;
50 import org.junit.runner.RunWith;
51 import org.springframework.test.context.ContextConfiguration;
52
53 import javax.inject.Inject;
54 import javax.servlet.http.HttpServletResponse;
55 import java.nio.file.Files;
56 import java.nio.file.Path;
57 import java.nio.file.Paths;
58 import java.util.Collections;
59
60 @RunWith( ArchivaSpringJUnit4ClassRunner.class )
61 @ContextConfiguration( locations = { "classpath*:/META-INF/spring-context.xml", "classpath*:/spring-context.xml" } )
62 public class DavResourceTest
63     extends TestCase
64 {
65     private DavSession session;
66
67     @Inject
68     private MimeTypes mimeTypes;
69
70     @Inject
71     private FileLockManager fileLockManager;
72
73     @Inject
74     private RepositoryRegistry repositoryRegistry;
75
76     private ArchivaDavResourceLocator resourceLocator;
77
78     private DavResourceFactory resourceFactory;
79
80     private Path baseDir;
81
82     private final String REPOPATH = "myresource.jar";
83
84     private Path myResource;
85
86     private DavResource resource;
87
88     private LockManager lockManager;
89
90     private MavenManagedRepository repository;
91     @Override
92     @Before
93     public void setUp()
94         throws Exception
95     {
96         super.setUp();
97         session = new ArchivaDavSession();
98         baseDir = Paths.get( "target/DavResourceTest" );
99         Files.createDirectories( baseDir );
100         Files.createDirectories( baseDir.resolve( "conf" ) );
101         repository = MavenManagedRepository.newLocalInstance( "repo001", "repo001", baseDir);
102         repositoryRegistry.putRepository( repository );
103
104         myResource = baseDir.resolve( "myresource.jar" );
105         Files.createFile(myResource);
106         resourceFactory = new RootContextDavResourceFactory();
107         
108         resourceLocator =
109             (ArchivaDavResourceLocator) new ArchivaDavLocatorFactory().createResourceLocator( "/", REPOPATH );        
110         resource = getDavResource( resourceLocator.getHref( false ), myResource );
111         lockManager = new SimpleLockManager();
112         resource.addLockManager( lockManager );        
113     }
114
115     @After
116     @Override
117     public void tearDown()
118         throws Exception
119     {
120         super.tearDown();
121         org.apache.archiva.common.utils.FileUtils.deleteDirectory( baseDir );
122         String appserverBase = System.getProperty( "appserver.base" );
123         if ( StringUtils.isNotEmpty( appserverBase ) )
124         {
125             FileUtils.deleteDirectory( Paths.get( appserverBase ) );
126         }
127     }
128
129     private DavResource getDavResource( String logicalPath, Path file ) throws LayoutException
130     {
131         return new ArchivaDavResource( new FilesystemAsset( (FilesystemStorage) repository.getRoot().getStorage(), logicalPath, file.toAbsolutePath()) , logicalPath, repository, session, resourceLocator,
132                                        resourceFactory, mimeTypes, Collections.<AuditListener> emptyList(), null);
133     }
134
135     @Test
136     public void testDeleteNonExistantResourceShould404()
137         throws Exception
138     {
139         Path dir = baseDir.resolve( "testdir" );
140         try
141         {
142             DavResource directoryResource = getDavResource( "/testdir", dir );
143             directoryResource.getCollection().removeMember( directoryResource );
144             fail( "Did not throw DavException" );
145         }
146         catch ( DavException e )
147         {
148             assertEquals( DavServletResponse.SC_NOT_FOUND, e.getErrorCode() );
149         }
150     }
151
152     @Test
153     public void testDeleteCollection()
154         throws Exception
155     {
156         Path dir = baseDir.resolve( "testdir" );
157         try
158         {
159             assertNotNull( Files.createDirectories(dir) );
160             DavResource directoryResource = getDavResource( "/testdir", dir );
161             directoryResource.getCollection().removeMember( directoryResource );
162             assertFalse( Files.exists(dir) );
163         }
164         finally
165         {
166             org.apache.archiva.common.utils.FileUtils.deleteDirectory( dir );
167         }
168     }
169
170     @Test
171     public void testDeleteResource()
172         throws Exception
173     {
174         assertTrue( Files.exists(myResource) );
175         resource.getCollection().removeMember( resource );
176         assertFalse( Files.exists(myResource) );
177     }
178
179     @Test
180     public void testIsLockable()
181     {
182         assertTrue( resource.isLockable( Type.WRITE, Scope.EXCLUSIVE ) );
183         assertFalse( resource.isLockable( Type.WRITE, Scope.SHARED ) );
184     }
185
186     @Test
187     public void testLock()
188         throws Exception
189     {
190         assertEquals( 0, resource.getLocks().length );
191
192         LockInfo info = new LockInfo( Scope.EXCLUSIVE, Type.WRITE, "/", 0, false );
193         lockManager.createLock( info, resource );
194
195         assertEquals( 1, resource.getLocks().length );
196     }
197
198     @Test
199     public void testLockIfResourceUnlockable()
200         throws Exception
201     {
202         assertEquals( 0, resource.getLocks().length );
203
204         LockInfo info = new LockInfo( Scope.SHARED, Type.WRITE, "/", 0, false );
205         try
206         {
207             lockManager.createLock( info, resource );
208             fail( "Did not throw dav exception" );
209         }
210         catch ( Exception e )
211         {
212             // Simple lock manager will die
213         }
214         assertEquals( 0, resource.getLocks().length );
215     }
216
217     @Test
218     public void testGetLock()
219         throws Exception
220     {
221         LockInfo info = new LockInfo( Scope.EXCLUSIVE, Type.WRITE, "/", 0, false );
222         lockManager.createLock( info, resource );
223
224         assertEquals( 1, resource.getLocks().length );
225
226         // Lock should exist
227         assertNotNull( resource.getLock( Type.WRITE, Scope.EXCLUSIVE ) );
228
229         // Lock should not exist
230         assertNull( resource.getLock( Type.WRITE, Scope.SHARED ) );
231     }
232
233     @Test
234     public void testRefreshLockThrowsExceptionIfNoLockIsPresent()
235         throws Exception
236     {
237         LockInfo info = new LockInfo( Scope.EXCLUSIVE, Type.WRITE, "/", 0, false );
238
239         assertEquals( 0, resource.getLocks().length );
240
241         try
242         {
243             lockManager.refreshLock( info, "notoken", resource );
244             fail( "Did not throw dav exception" );
245         }
246         catch ( DavException e )
247         {
248             assertEquals( DavServletResponse.SC_PRECONDITION_FAILED, e.getErrorCode() );
249         }
250
251         assertEquals( 0, resource.getLocks().length );
252     }
253
254     @Test
255     public void testRefreshLock()
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.refreshLock( info, lock.getToken(), resource );
269
270         assertEquals( 1, resource.getLocks().length );
271     }
272
273     @Test
274     public void testUnlock()
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         ActiveLock lock = resource.getLocks()[0];
286
287         lockManager.releaseLock( lock.getToken(), resource );
288
289         assertEquals( 0, resource.getLocks().length );
290     }
291
292     @Test
293     public void testUnlockThrowsDavExceptionIfNotLocked()
294         throws Exception
295     {
296         LockInfo info = new LockInfo( Scope.EXCLUSIVE, Type.WRITE, "/", 0, false );
297
298         assertEquals( 0, resource.getLocks().length );
299
300         lockManager.createLock( info, resource );
301
302         assertEquals( 1, 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_LOCKED, e.getErrorCode() );
312         }
313
314         assertEquals( 1, resource.getLocks().length );
315     }
316
317     @Test
318     public void testUnlockThrowsDavExceptionIfResourceNotLocked()
319         throws Exception
320     {
321         assertEquals( 0, resource.getLocks().length );
322
323         try
324         {
325             lockManager.releaseLock( "BLAH", resource );
326             fail( "Did not throw DavException" );
327         }
328         catch ( DavException e )
329         {
330             assertEquals( DavServletResponse.SC_PRECONDITION_FAILED, e.getErrorCode() );
331         }
332
333         assertEquals( 0, resource.getLocks().length );
334     }
335
336     private class RootContextDavResourceFactory
337         implements DavResourceFactory
338     {
339         @Override
340         public DavResource createResource( DavResourceLocator locator, DavServletRequest request,
341                                            DavServletResponse response )
342             throws DavException
343         {
344             throw new UnsupportedOperationException( "Not supported yet." );
345         }
346
347         @Override
348         public DavResource createResource( DavResourceLocator locator, DavSession session )
349             throws DavException
350         {
351             try
352             {
353                 return new ArchivaDavResource( new FilesystemAsset( (FilesystemStorage) repository.getRoot().getStorage(), "/" , baseDir.toAbsolutePath()), "/", repository, session, resourceLocator,
354                                                resourceFactory, mimeTypes, Collections.<AuditListener> emptyList(),
355                                                null );
356             }
357             catch ( LayoutException e )
358             {
359                 throw new DavException( HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e );
360             }
361         }
362     }
363 }