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 11KB

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