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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  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.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.archiva.webdav.util.MimeTypes;
  39. import org.junit.After;
  40. import org.junit.Before;
  41. import org.junit.Test;
  42. import org.junit.runner.RunWith;
  43. import org.springframework.test.context.ContextConfiguration;
  44. import javax.inject.Inject;
  45. import java.io.File;
  46. import java.util.Collections;
  47. import org.apache.archiva.test.utils.ArchivaSpringJUnit4ClassRunner;
  48. @RunWith( ArchivaSpringJUnit4ClassRunner.class )
  49. @ContextConfiguration( locations = { "classpath*:/META-INF/spring-context.xml", "classpath*:/spring-context.xml" } )
  50. public class DavResourceTest
  51. extends TestCase
  52. {
  53. private DavSession session;
  54. @Inject
  55. private MimeTypes mimeTypes;
  56. @Inject
  57. private FileLockManager fileLockManager;
  58. private ArchivaDavResourceLocator resourceLocator;
  59. private DavResourceFactory resourceFactory;
  60. private File baseDir;
  61. private final String REPOPATH = "myresource.jar";
  62. private File myResource;
  63. private DavResource resource;
  64. private LockManager lockManager;
  65. private ManagedRepository repository = new ManagedRepository();
  66. @Override
  67. @Before
  68. public void setUp()
  69. throws Exception
  70. {
  71. super.setUp();
  72. session = new ArchivaDavSession();
  73. baseDir = new File( "target/DavResourceTest" );
  74. baseDir.mkdirs();
  75. myResource = new File( baseDir, "myresource.jar" );
  76. assertTrue( "Could not create " + myResource.getAbsolutePath(), myResource.createNewFile() );
  77. resourceFactory = new RootContextDavResourceFactory();
  78. resourceLocator =
  79. (ArchivaDavResourceLocator) new ArchivaDavLocatorFactory().createResourceLocator( "/", REPOPATH );
  80. resource = getDavResource( resourceLocator.getHref( false ), myResource );
  81. lockManager = new SimpleLockManager();
  82. resource.addLockManager( lockManager );
  83. }
  84. @After
  85. @Override
  86. public void tearDown()
  87. throws Exception
  88. {
  89. super.tearDown();
  90. FileUtils.deleteDirectory( baseDir );
  91. }
  92. private DavResource getDavResource( String logicalPath, File file )
  93. {
  94. return new ArchivaDavResource( file.getAbsolutePath(), logicalPath, repository, session, resourceLocator,
  95. resourceFactory, mimeTypes, Collections.<AuditListener> emptyList(), null, fileLockManager );
  96. }
  97. @Test
  98. public void testDeleteNonExistantResourceShould404()
  99. throws Exception
  100. {
  101. File dir = new File( baseDir, "testdir" );
  102. try
  103. {
  104. DavResource directoryResource = getDavResource( "/testdir", dir );
  105. directoryResource.getCollection().removeMember( directoryResource );
  106. fail( "Did not throw DavException" );
  107. }
  108. catch ( DavException e )
  109. {
  110. assertEquals( DavServletResponse.SC_NOT_FOUND, e.getErrorCode() );
  111. }
  112. }
  113. @Test
  114. public void testDeleteCollection()
  115. throws Exception
  116. {
  117. File dir = new File( baseDir, "testdir" );
  118. try
  119. {
  120. assertTrue( dir.mkdir() );
  121. DavResource directoryResource = getDavResource( "/testdir", dir );
  122. directoryResource.getCollection().removeMember( directoryResource );
  123. assertFalse( dir.exists() );
  124. }
  125. finally
  126. {
  127. FileUtils.deleteDirectory( dir );
  128. }
  129. }
  130. @Test
  131. public void testDeleteResource()
  132. throws Exception
  133. {
  134. assertTrue( myResource.exists() );
  135. resource.getCollection().removeMember( resource );
  136. assertFalse( myResource.exists() );
  137. }
  138. @Test
  139. public void testIsLockable()
  140. {
  141. assertTrue( resource.isLockable( Type.WRITE, Scope.EXCLUSIVE ) );
  142. assertFalse( resource.isLockable( Type.WRITE, Scope.SHARED ) );
  143. }
  144. @Test
  145. public void testLock()
  146. throws Exception
  147. {
  148. assertEquals( 0, resource.getLocks().length );
  149. LockInfo info = new LockInfo( Scope.EXCLUSIVE, Type.WRITE, "/", 0, false );
  150. lockManager.createLock( info, resource );
  151. assertEquals( 1, resource.getLocks().length );
  152. }
  153. @Test
  154. public void testLockIfResourceUnlockable()
  155. throws Exception
  156. {
  157. assertEquals( 0, resource.getLocks().length );
  158. LockInfo info = new LockInfo( Scope.SHARED, Type.WRITE, "/", 0, false );
  159. try
  160. {
  161. lockManager.createLock( info, resource );
  162. fail( "Did not throw dav exception" );
  163. }
  164. catch ( Exception e )
  165. {
  166. // Simple lock manager will die
  167. }
  168. assertEquals( 0, resource.getLocks().length );
  169. }
  170. @Test
  171. public void testGetLock()
  172. throws Exception
  173. {
  174. LockInfo info = new LockInfo( Scope.EXCLUSIVE, Type.WRITE, "/", 0, false );
  175. lockManager.createLock( info, resource );
  176. assertEquals( 1, resource.getLocks().length );
  177. // Lock should exist
  178. assertNotNull( resource.getLock( Type.WRITE, Scope.EXCLUSIVE ) );
  179. // Lock should not exist
  180. assertNull( resource.getLock( Type.WRITE, Scope.SHARED ) );
  181. }
  182. @Test
  183. public void testRefreshLockThrowsExceptionIfNoLockIsPresent()
  184. throws Exception
  185. {
  186. LockInfo info = new LockInfo( Scope.EXCLUSIVE, Type.WRITE, "/", 0, false );
  187. assertEquals( 0, resource.getLocks().length );
  188. try
  189. {
  190. lockManager.refreshLock( info, "notoken", resource );
  191. fail( "Did not throw dav exception" );
  192. }
  193. catch ( DavException e )
  194. {
  195. assertEquals( DavServletResponse.SC_PRECONDITION_FAILED, e.getErrorCode() );
  196. }
  197. assertEquals( 0, resource.getLocks().length );
  198. }
  199. @Test
  200. public void testRefreshLock()
  201. throws Exception
  202. {
  203. LockInfo info = new LockInfo( Scope.EXCLUSIVE, Type.WRITE, "/", 0, false );
  204. assertEquals( 0, resource.getLocks().length );
  205. lockManager.createLock( info, resource );
  206. assertEquals( 1, resource.getLocks().length );
  207. ActiveLock lock = resource.getLocks()[0];
  208. lockManager.refreshLock( info, lock.getToken(), resource );
  209. assertEquals( 1, resource.getLocks().length );
  210. }
  211. @Test
  212. public void testUnlock()
  213. throws Exception
  214. {
  215. LockInfo info = new LockInfo( Scope.EXCLUSIVE, Type.WRITE, "/", 0, false );
  216. assertEquals( 0, resource.getLocks().length );
  217. lockManager.createLock( info, resource );
  218. assertEquals( 1, resource.getLocks().length );
  219. ActiveLock lock = resource.getLocks()[0];
  220. lockManager.releaseLock( lock.getToken(), resource );
  221. assertEquals( 0, resource.getLocks().length );
  222. }
  223. @Test
  224. public void testUnlockThrowsDavExceptionIfNotLocked()
  225. throws Exception
  226. {
  227. LockInfo info = new LockInfo( Scope.EXCLUSIVE, Type.WRITE, "/", 0, false );
  228. assertEquals( 0, resource.getLocks().length );
  229. lockManager.createLock( info, resource );
  230. assertEquals( 1, resource.getLocks().length );
  231. try
  232. {
  233. lockManager.releaseLock( "BLAH", resource );
  234. fail( "Did not throw DavException" );
  235. }
  236. catch ( DavException e )
  237. {
  238. assertEquals( DavServletResponse.SC_LOCKED, e.getErrorCode() );
  239. }
  240. assertEquals( 1, resource.getLocks().length );
  241. }
  242. @Test
  243. public void testUnlockThrowsDavExceptionIfResourceNotLocked()
  244. throws Exception
  245. {
  246. assertEquals( 0, resource.getLocks().length );
  247. try
  248. {
  249. lockManager.releaseLock( "BLAH", resource );
  250. fail( "Did not throw DavException" );
  251. }
  252. catch ( DavException e )
  253. {
  254. assertEquals( DavServletResponse.SC_PRECONDITION_FAILED, e.getErrorCode() );
  255. }
  256. assertEquals( 0, resource.getLocks().length );
  257. }
  258. private class RootContextDavResourceFactory
  259. implements DavResourceFactory
  260. {
  261. @Override
  262. public DavResource createResource( DavResourceLocator locator, DavServletRequest request,
  263. DavServletResponse response )
  264. throws DavException
  265. {
  266. throw new UnsupportedOperationException( "Not supported yet." );
  267. }
  268. @Override
  269. public DavResource createResource( DavResourceLocator locator, DavSession session )
  270. throws DavException
  271. {
  272. return new ArchivaDavResource( baseDir.getAbsolutePath(), "/", repository, session, resourceLocator,
  273. resourceFactory, mimeTypes, Collections.<AuditListener> emptyList(),
  274. null, fileLockManager );
  275. }
  276. }
  277. }