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