You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

DavResourceTest.java 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. package org.apache.archiva.webdav;
  2. /*
  3. * Licensed to the Apache Software Foundation (ASF) under one
  4. * or more contributor license agreements. See the NOTICE file
  5. * distributed with this work for additional information
  6. * regarding copyright ownership. The ASF licenses this file
  7. * to you under the Apache License, Version 2.0 (the
  8. * "License"); you may not use this file except in compliance
  9. * with the License. You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing,
  14. * software distributed under the License is distributed on an
  15. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  16. * KIND, either express or implied. See the License for the
  17. * specific language governing permissions and limitations
  18. * under the License.
  19. */
  20. import junit.framework.TestCase;
  21. import org.apache.archiva.admin.model.beans.ManagedRepository;
  22. import org.apache.archiva.repository.events.AuditListener;
  23. import org.apache.archiva.common.filelock.FileLockManager;
  24. import org.apache.commons.io.FileUtils;
  25. import org.apache.commons.io.IOUtils;
  26. import org.apache.jackrabbit.webdav.DavException;
  27. import org.apache.jackrabbit.webdav.DavResource;
  28. import org.apache.jackrabbit.webdav.DavResourceFactory;
  29. import org.apache.jackrabbit.webdav.DavResourceLocator;
  30. import org.apache.jackrabbit.webdav.DavServletRequest;
  31. import org.apache.jackrabbit.webdav.DavServletResponse;
  32. import org.apache.jackrabbit.webdav.DavSession;
  33. import org.apache.jackrabbit.webdav.lock.ActiveLock;
  34. import org.apache.jackrabbit.webdav.lock.LockInfo;
  35. import org.apache.jackrabbit.webdav.lock.LockManager;
  36. import org.apache.jackrabbit.webdav.lock.Scope;
  37. import org.apache.jackrabbit.webdav.lock.SimpleLockManager;
  38. import org.apache.jackrabbit.webdav.lock.Type;
  39. import org.apache.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 javax.inject.Inject;
  46. import java.io.File;
  47. import java.util.Collections;
  48. import org.apache.archiva.test.utils.ArchivaSpringJUnit4ClassRunner;
  49. @RunWith( ArchivaSpringJUnit4ClassRunner.class )
  50. @ContextConfiguration( locations = { "classpath*:/META-INF/spring-context.xml", "classpath*:/spring-context.xml" } )
  51. public class DavResourceTest
  52. extends TestCase
  53. {
  54. private DavSession session;
  55. @Inject
  56. private MimeTypes mimeTypes;
  57. @Inject
  58. private FileLockManager fileLockManager;
  59. private ArchivaDavResourceLocator resourceLocator;
  60. private DavResourceFactory resourceFactory;
  61. private File baseDir;
  62. private final String REPOPATH = "myresource.jar";
  63. private File myResource;
  64. private DavResource resource;
  65. private LockManager lockManager;
  66. private ManagedRepository repository = new ManagedRepository();
  67. @Override
  68. @Before
  69. public void setUp()
  70. throws Exception
  71. {
  72. super.setUp();
  73. session = new ArchivaDavSession();
  74. baseDir = new File( "target/DavResourceTest" );
  75. baseDir.mkdirs();
  76. myResource = new File( baseDir, "myresource.jar" );
  77. assertTrue( "Could not create " + myResource.getAbsolutePath(), myResource.createNewFile() );
  78. resourceFactory = new RootContextDavResourceFactory();
  79. resourceLocator =
  80. (ArchivaDavResourceLocator) new ArchivaDavLocatorFactory().createResourceLocator( "/", REPOPATH );
  81. resource = getDavResource( resourceLocator.getHref( false ), myResource );
  82. lockManager = new SimpleLockManager();
  83. resource.addLockManager( lockManager );
  84. }
  85. @After
  86. @Override
  87. public void tearDown()
  88. throws Exception
  89. {
  90. super.tearDown();
  91. FileUtils.deleteDirectory( baseDir );
  92. }
  93. private DavResource getDavResource( String logicalPath, File file )
  94. {
  95. return new ArchivaDavResource( file.getAbsolutePath(), logicalPath, repository, session, resourceLocator,
  96. resourceFactory, mimeTypes, Collections.<AuditListener> emptyList(), null, fileLockManager );
  97. }
  98. @Test
  99. public void testDeleteNonExistantResourceShould404()
  100. throws Exception
  101. {
  102. File dir = new File( baseDir, "testdir" );
  103. try
  104. {
  105. DavResource directoryResource = getDavResource( "/testdir", dir );
  106. directoryResource.getCollection().removeMember( directoryResource );
  107. fail( "Did not throw DavException" );
  108. }
  109. catch ( DavException e )
  110. {
  111. assertEquals( DavServletResponse.SC_NOT_FOUND, e.getErrorCode() );
  112. }
  113. }
  114. @Test
  115. public void testDeleteCollection()
  116. throws Exception
  117. {
  118. File dir = new File( baseDir, "testdir" );
  119. try
  120. {
  121. assertTrue( dir.mkdir() );
  122. DavResource directoryResource = getDavResource( "/testdir", dir );
  123. directoryResource.getCollection().removeMember( directoryResource );
  124. assertFalse( dir.exists() );
  125. }
  126. finally
  127. {
  128. FileUtils.deleteDirectory( dir );
  129. }
  130. }
  131. @Test
  132. public void testDeleteResource()
  133. throws Exception
  134. {
  135. assertTrue( myResource.exists() );
  136. resource.getCollection().removeMember( resource );
  137. assertFalse( myResource.exists() );
  138. }
  139. @Test
  140. public void testIsLockable()
  141. {
  142. assertTrue( resource.isLockable( Type.WRITE, Scope.EXCLUSIVE ) );
  143. assertFalse( resource.isLockable( Type.WRITE, Scope.SHARED ) );
  144. }
  145. @Test
  146. public void testLock()
  147. throws Exception
  148. {
  149. assertEquals( 0, resource.getLocks().length );
  150. LockInfo info = new LockInfo( Scope.EXCLUSIVE, Type.WRITE, "/", 0, false );
  151. lockManager.createLock( info, resource );
  152. assertEquals( 1, resource.getLocks().length );
  153. }
  154. @Test
  155. public void testLockIfResourceUnlockable()
  156. throws Exception
  157. {
  158. assertEquals( 0, resource.getLocks().length );
  159. LockInfo info = new LockInfo( Scope.SHARED, Type.WRITE, "/", 0, false );
  160. try
  161. {
  162. lockManager.createLock( info, resource );
  163. fail( "Did not throw dav exception" );
  164. }
  165. catch ( Exception e )
  166. {
  167. // Simple lock manager will die
  168. }
  169. assertEquals( 0, resource.getLocks().length );
  170. }
  171. @Test
  172. public void testGetLock()
  173. throws Exception
  174. {
  175. LockInfo info = new LockInfo( Scope.EXCLUSIVE, Type.WRITE, "/", 0, false );
  176. lockManager.createLock( info, resource );
  177. assertEquals( 1, resource.getLocks().length );
  178. // Lock should exist
  179. assertNotNull( resource.getLock( Type.WRITE, Scope.EXCLUSIVE ) );
  180. // Lock should not exist
  181. assertNull( resource.getLock( Type.WRITE, Scope.SHARED ) );
  182. }
  183. @Test
  184. public void testRefreshLockThrowsExceptionIfNoLockIsPresent()
  185. throws Exception
  186. {
  187. LockInfo info = new LockInfo( Scope.EXCLUSIVE, Type.WRITE, "/", 0, false );
  188. assertEquals( 0, resource.getLocks().length );
  189. try
  190. {
  191. lockManager.refreshLock( info, "notoken", resource );
  192. fail( "Did not throw dav exception" );
  193. }
  194. catch ( DavException e )
  195. {
  196. assertEquals( DavServletResponse.SC_PRECONDITION_FAILED, e.getErrorCode() );
  197. }
  198. assertEquals( 0, resource.getLocks().length );
  199. }
  200. @Test
  201. public void testRefreshLock()
  202. throws Exception
  203. {
  204. LockInfo info = new LockInfo( Scope.EXCLUSIVE, Type.WRITE, "/", 0, false );
  205. assertEquals( 0, resource.getLocks().length );
  206. lockManager.createLock( info, resource );
  207. assertEquals( 1, resource.getLocks().length );
  208. ActiveLock lock = resource.getLocks()[0];
  209. lockManager.refreshLock( info, lock.getToken(), resource );
  210. assertEquals( 1, resource.getLocks().length );
  211. }
  212. @Test
  213. public void testUnlock()
  214. throws Exception
  215. {
  216. LockInfo info = new LockInfo( Scope.EXCLUSIVE, Type.WRITE, "/", 0, false );
  217. assertEquals( 0, resource.getLocks().length );
  218. lockManager.createLock( info, resource );
  219. assertEquals( 1, resource.getLocks().length );
  220. ActiveLock lock = resource.getLocks()[0];
  221. lockManager.releaseLock( lock.getToken(), resource );
  222. assertEquals( 0, resource.getLocks().length );
  223. }
  224. @Test
  225. public void testUnlockThrowsDavExceptionIfNotLocked()
  226. throws Exception
  227. {
  228. LockInfo info = new LockInfo( Scope.EXCLUSIVE, Type.WRITE, "/", 0, false );
  229. assertEquals( 0, resource.getLocks().length );
  230. lockManager.createLock( info, resource );
  231. assertEquals( 1, resource.getLocks().length );
  232. try
  233. {
  234. lockManager.releaseLock( "BLAH", resource );
  235. fail( "Did not throw DavException" );
  236. }
  237. catch ( DavException e )
  238. {
  239. assertEquals( DavServletResponse.SC_LOCKED, e.getErrorCode() );
  240. }
  241. assertEquals( 1, resource.getLocks().length );
  242. }
  243. @Test
  244. public void testUnlockThrowsDavExceptionIfResourceNotLocked()
  245. throws Exception
  246. {
  247. assertEquals( 0, resource.getLocks().length );
  248. try
  249. {
  250. lockManager.releaseLock( "BLAH", resource );
  251. fail( "Did not throw DavException" );
  252. }
  253. catch ( DavException e )
  254. {
  255. assertEquals( DavServletResponse.SC_PRECONDITION_FAILED, e.getErrorCode() );
  256. }
  257. assertEquals( 0, resource.getLocks().length );
  258. }
  259. private class RootContextDavResourceFactory
  260. implements DavResourceFactory
  261. {
  262. @Override
  263. public DavResource createResource( DavResourceLocator locator, DavServletRequest request,
  264. DavServletResponse response )
  265. throws DavException
  266. {
  267. throw new UnsupportedOperationException( "Not supported yet." );
  268. }
  269. @Override
  270. public DavResource createResource( DavResourceLocator locator, DavSession session )
  271. throws DavException
  272. {
  273. return new ArchivaDavResource( baseDir.getAbsolutePath(), "/", repository, session, resourceLocator,
  274. resourceFactory, mimeTypes, Collections.<AuditListener> emptyList(),
  275. null, fileLockManager );
  276. }
  277. }
  278. }