1 package org.apache.maven.archiva.webdav;
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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
22 import junit.framework.TestCase;
23 import org.apache.archiva.audit.AuditListener;
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.webdav.util.MimeTypes;
40 import org.junit.After;
41 import org.junit.Before;
42 import org.junit.Test;
43 import org.junit.runner.RunWith;
44 import org.springframework.test.context.ContextConfiguration;
45 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
47 import javax.inject.Inject;
49 import java.util.Collections;
51 @RunWith( SpringJUnit4ClassRunner.class )
52 @ContextConfiguration( locations = { "classpath*:/META-INF/spring-context.xml", "classpath*:/spring-context.xml" } )
53 public class DavResourceTest
56 private DavSession session;
59 private MimeTypes mimeTypes;
61 private ArchivaDavResourceLocator resourceLocator;
63 private DavResourceFactory resourceFactory;
67 private final String REPOPATH = "myresource.jar";
69 private File myResource;
71 private DavResource resource;
73 private LockManager lockManager;
75 private ManagedRepositoryConfiguration repository = new ManagedRepositoryConfiguration();
83 session = new ArchivaDavSession();
84 baseDir = new File( "target/DavResourceTest" );
86 myResource = new File( baseDir, "myresource.jar" );
87 assertTrue( "Could not create " + myResource.getAbsolutePath(), myResource.createNewFile() );
88 resourceFactory = new RootContextDavResourceFactory();
91 (ArchivaDavResourceLocator) new ArchivaDavLocatorFactory().createResourceLocator( "/", REPOPATH );
92 resource = getDavResource( resourceLocator.getHref( false ), myResource );
93 lockManager = new SimpleLockManager();
94 resource.addLockManager( lockManager );
98 public void tearDown()
102 FileUtils.deleteDirectory( baseDir );
105 private DavResource getDavResource( String logicalPath, File file )
107 return new ArchivaDavResource( file.getAbsolutePath(), logicalPath, repository, session, resourceLocator,
108 resourceFactory, mimeTypes, Collections.<AuditListener> emptyList(), null );
112 public void testDeleteNonExistantResourceShould404()
115 File dir = new File( baseDir, "testdir" );
118 DavResource directoryResource = getDavResource( "/testdir", dir );
119 directoryResource.getCollection().removeMember( directoryResource );
120 fail( "Did not throw DavException" );
122 catch ( DavException e )
124 assertEquals( DavServletResponse.SC_NOT_FOUND, e.getErrorCode() );
129 public void testDeleteCollection()
132 File dir = new File( baseDir, "testdir" );
135 assertTrue( dir.mkdir() );
136 DavResource directoryResource = getDavResource( "/testdir", dir );
137 directoryResource.getCollection().removeMember( directoryResource );
138 assertFalse( dir.exists() );
142 FileUtils.deleteDirectory( dir );
147 public void testDeleteResource()
150 assertTrue( myResource.exists() );
151 resource.getCollection().removeMember( resource );
152 assertFalse( myResource.exists() );
156 public void testIsLockable()
158 assertTrue( resource.isLockable( Type.WRITE, Scope.EXCLUSIVE ) );
159 assertFalse( resource.isLockable( Type.WRITE, Scope.SHARED ) );
163 public void testLock()
166 assertEquals( 0, resource.getLocks().length );
168 LockInfo info = new LockInfo( Scope.EXCLUSIVE, Type.WRITE, "/", 0, false );
169 lockManager.createLock( info, resource );
171 assertEquals( 1, resource.getLocks().length );
175 public void testLockIfResourceUnlockable()
178 assertEquals( 0, resource.getLocks().length );
180 LockInfo info = new LockInfo( Scope.SHARED, Type.WRITE, "/", 0, false );
183 lockManager.createLock( info, resource );
184 fail( "Did not throw dav exception" );
186 catch ( Exception e )
188 // Simple lock manager will die
190 assertEquals( 0, resource.getLocks().length );
194 public void testGetLock()
197 LockInfo info = new LockInfo( Scope.EXCLUSIVE, Type.WRITE, "/", 0, false );
198 lockManager.createLock( info, resource );
200 assertEquals( 1, resource.getLocks().length );
203 assertNotNull( resource.getLock( Type.WRITE, Scope.EXCLUSIVE ) );
205 // Lock should not exist
206 assertNull( resource.getLock( Type.WRITE, Scope.SHARED ) );
210 public void testRefreshLockThrowsExceptionIfNoLockIsPresent()
213 LockInfo info = new LockInfo( Scope.EXCLUSIVE, Type.WRITE, "/", 0, false );
215 assertEquals( 0, resource.getLocks().length );
219 lockManager.refreshLock( info, "notoken", resource );
220 fail( "Did not throw dav exception" );
222 catch ( DavException e )
224 assertEquals( DavServletResponse.SC_PRECONDITION_FAILED, e.getErrorCode() );
227 assertEquals( 0, resource.getLocks().length );
231 public void testRefreshLock()
234 LockInfo info = new LockInfo( Scope.EXCLUSIVE, Type.WRITE, "/", 0, false );
236 assertEquals( 0, resource.getLocks().length );
238 lockManager.createLock( info, resource );
240 assertEquals( 1, resource.getLocks().length );
242 ActiveLock lock = resource.getLocks()[0];
244 lockManager.refreshLock( info, lock.getToken(), resource );
246 assertEquals( 1, resource.getLocks().length );
250 public void testUnlock()
253 LockInfo info = new LockInfo( Scope.EXCLUSIVE, Type.WRITE, "/", 0, false );
255 assertEquals( 0, resource.getLocks().length );
257 lockManager.createLock( info, resource );
259 assertEquals( 1, resource.getLocks().length );
261 ActiveLock lock = resource.getLocks()[0];
263 lockManager.releaseLock( lock.getToken(), resource );
265 assertEquals( 0, resource.getLocks().length );
269 public void testUnlockThrowsDavExceptionIfNotLocked()
272 LockInfo info = new LockInfo( Scope.EXCLUSIVE, Type.WRITE, "/", 0, false );
274 assertEquals( 0, resource.getLocks().length );
276 lockManager.createLock( info, resource );
278 assertEquals( 1, resource.getLocks().length );
282 lockManager.releaseLock( "BLAH", resource );
283 fail( "Did not throw DavException" );
285 catch ( DavException e )
287 assertEquals( DavServletResponse.SC_LOCKED, e.getErrorCode() );
290 assertEquals( 1, resource.getLocks().length );
294 public void testUnlockThrowsDavExceptionIfResourceNotLocked()
297 assertEquals( 0, resource.getLocks().length );
301 lockManager.releaseLock( "BLAH", resource );
302 fail( "Did not throw DavException" );
304 catch ( DavException e )
306 assertEquals( DavServletResponse.SC_PRECONDITION_FAILED, e.getErrorCode() );
309 assertEquals( 0, resource.getLocks().length );
312 private class RootContextDavResourceFactory
313 implements DavResourceFactory
315 public DavResource createResource( DavResourceLocator locator, DavServletRequest request,
316 DavServletResponse response )
319 throw new UnsupportedOperationException( "Not supported yet." );
322 public DavResource createResource( DavResourceLocator locator, DavSession session )
325 return new ArchivaDavResource( baseDir.getAbsolutePath(), "/", repository, session, resourceLocator,
326 resourceFactory, mimeTypes, Collections.<AuditListener> emptyList(),