View Javadoc

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