|
@@ -15,7 +15,14 @@ import java.io.IOException;
|
|
|
import java.net.URI;
|
|
|
import java.net.URISyntaxException;
|
|
|
import java.nio.ByteBuffer;
|
|
|
+import java.util.Arrays;
|
|
|
+import java.util.Collection;
|
|
|
+import java.util.stream.Collectors;
|
|
|
|
|
|
+import javax.xml.parsers.ParserConfigurationException;
|
|
|
+import javax.xml.xpath.XPathException;
|
|
|
+
|
|
|
+import org.apache.commons.collections4.CollectionUtils;
|
|
|
import org.apache.commons.httpclient.Header;
|
|
|
import org.apache.commons.httpclient.HttpClient;
|
|
|
import org.apache.commons.httpclient.HttpException;
|
|
@@ -25,7 +32,14 @@ import org.apache.commons.httpclient.methods.EntityEnclosingMethod;
|
|
|
import org.apache.commons.httpclient.methods.GetMethod;
|
|
|
import org.apache.commons.httpclient.methods.PutMethod;
|
|
|
import org.apache.commons.io.IOUtils;
|
|
|
+import org.apache.jackrabbit.webdav.DavException;
|
|
|
+import org.apache.jackrabbit.webdav.MultiStatus;
|
|
|
+import org.apache.jackrabbit.webdav.MultiStatusResponse;
|
|
|
+import org.apache.jackrabbit.webdav.client.methods.CopyMethod;
|
|
|
+import org.apache.jackrabbit.webdav.client.methods.DavMethodBase;
|
|
|
import org.apache.jackrabbit.webdav.client.methods.MkColMethod;
|
|
|
+import org.apache.jackrabbit.webdav.client.methods.MoveMethod;
|
|
|
+import org.apache.jackrabbit.webdav.client.methods.PropFindMethod;
|
|
|
import org.cryptomator.filesystem.FileSystem;
|
|
|
import org.cryptomator.filesystem.ReadableFile;
|
|
|
import org.cryptomator.filesystem.WritableFile;
|
|
@@ -37,6 +51,7 @@ import org.junit.Assert;
|
|
|
import org.junit.Before;
|
|
|
import org.junit.BeforeClass;
|
|
|
import org.junit.Test;
|
|
|
+import org.xml.sax.SAXException;
|
|
|
|
|
|
public class WebDavServerTest {
|
|
|
|
|
@@ -58,9 +73,9 @@ public class WebDavServerTest {
|
|
|
@Before
|
|
|
public void startServlet() throws Exception {
|
|
|
fs = new InMemoryFileSystem();
|
|
|
- servlet = SERVER.addServlet(fs, URI.create("http://localhost:" + SERVER.getPort() + "/test"));
|
|
|
- servlet.start();
|
|
|
servletRoot = "http://localhost:" + SERVER.getPort() + "/test";
|
|
|
+ servlet = SERVER.addServlet(fs, URI.create(servletRoot));
|
|
|
+ servlet.start();
|
|
|
}
|
|
|
|
|
|
@After
|
|
@@ -122,4 +137,178 @@ public class WebDavServerTest {
|
|
|
putMethod.releaseConnection();
|
|
|
}
|
|
|
|
|
|
+ /* PROPFIND */
|
|
|
+
|
|
|
+ @Test
|
|
|
+ public void testPropfind() throws HttpException, IOException, ParserConfigurationException, SAXException, XPathException, DavException {
|
|
|
+ final HttpClient client = new HttpClient();
|
|
|
+
|
|
|
+ fs.folder("folder1").create();
|
|
|
+ fs.folder("folder2").create();
|
|
|
+ try (WritableFile w = fs.file("file1").openWritable()) {
|
|
|
+ w.write(ByteBuffer.allocate(0));
|
|
|
+ }
|
|
|
+
|
|
|
+ // get directory listing of /:
|
|
|
+ final DavMethodBase propfindMethod = new PropFindMethod(servletRoot + "/");
|
|
|
+ final int statusCode = client.executeMethod(propfindMethod);
|
|
|
+ Assert.assertEquals(207, statusCode);
|
|
|
+
|
|
|
+ // get hrefs contained in dirlisting response:
|
|
|
+ MultiStatus ms = propfindMethod.getResponseBodyAsMultiStatus();
|
|
|
+ propfindMethod.releaseConnection();
|
|
|
+ Collection<String> hrefs = Arrays.asList(ms.getResponses()).stream().map(MultiStatusResponse::getHref).collect(Collectors.toSet());
|
|
|
+
|
|
|
+ Assert.assertTrue(CollectionUtils.containsAll(hrefs, Arrays.asList(servletRoot + "/folder1/", servletRoot + "/folder2/", servletRoot + "/file1")));
|
|
|
+ }
|
|
|
+
|
|
|
+ /* MOVE */
|
|
|
+
|
|
|
+ @Test
|
|
|
+ public void testMoveFolder() throws HttpException, IOException, ParserConfigurationException, SAXException, XPathException, DavException {
|
|
|
+ final HttpClient client = new HttpClient();
|
|
|
+
|
|
|
+ fs.folder("srcFolder").create();
|
|
|
+
|
|
|
+ final DavMethodBase moveMethod = new MoveMethod(servletRoot + "/srcFolder", servletRoot + "/dstFolder", false);
|
|
|
+ client.executeMethod(moveMethod);
|
|
|
+ Assert.assertTrue(moveMethod.succeeded());
|
|
|
+ moveMethod.releaseConnection();
|
|
|
+
|
|
|
+ Assert.assertFalse(fs.folder("srcFolder").exists());
|
|
|
+ Assert.assertTrue(fs.folder("dstFolder").exists());
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ public void testMoveFolderOverwrite() throws HttpException, IOException, ParserConfigurationException, SAXException, XPathException, DavException {
|
|
|
+ final HttpClient client = new HttpClient();
|
|
|
+
|
|
|
+ fs.folder("srcFolder").create();
|
|
|
+ try (WritableFile w = fs.file("dstFolder").openWritable()) {
|
|
|
+ w.write(ByteBuffer.allocate(0));
|
|
|
+ }
|
|
|
+
|
|
|
+ final DavMethodBase moveMethod = new MoveMethod(servletRoot + "/srcFolder", servletRoot + "/dstFolder", true);
|
|
|
+ client.executeMethod(moveMethod);
|
|
|
+ Assert.assertTrue(moveMethod.succeeded());
|
|
|
+ moveMethod.releaseConnection();
|
|
|
+
|
|
|
+ Assert.assertFalse(fs.folder("srcFolder").exists());
|
|
|
+ Assert.assertTrue(fs.folder("dstFolder").exists());
|
|
|
+ Assert.assertFalse(fs.file("dstFolder").exists());
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ public void testMoveFile() throws HttpException, IOException, ParserConfigurationException, SAXException, XPathException, DavException {
|
|
|
+ final HttpClient client = new HttpClient();
|
|
|
+
|
|
|
+ try (WritableFile w = fs.file("srcFile").openWritable()) {
|
|
|
+ w.write(ByteBuffer.allocate(0));
|
|
|
+ }
|
|
|
+
|
|
|
+ final DavMethodBase moveMethod = new MoveMethod(servletRoot + "/srcFile/", servletRoot + "/dstFile/", false);
|
|
|
+ client.executeMethod(moveMethod);
|
|
|
+ Assert.assertTrue(moveMethod.succeeded());
|
|
|
+ moveMethod.releaseConnection();
|
|
|
+
|
|
|
+ Assert.assertFalse(fs.file("srcFile").exists());
|
|
|
+ Assert.assertTrue(fs.file("dstFile").exists());
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ public void testMoveFileOverwrite() throws HttpException, IOException, ParserConfigurationException, SAXException, XPathException, DavException {
|
|
|
+ final HttpClient client = new HttpClient();
|
|
|
+
|
|
|
+ try (WritableFile w = fs.file("srcFile").openWritable()) {
|
|
|
+ w.write(ByteBuffer.allocate(0));
|
|
|
+ }
|
|
|
+ fs.folder("dstFile").create();
|
|
|
+
|
|
|
+ final DavMethodBase moveMethod = new MoveMethod(servletRoot + "/srcFile/", servletRoot + "/dstFile/", true);
|
|
|
+ client.executeMethod(moveMethod);
|
|
|
+ Assert.assertTrue(moveMethod.succeeded());
|
|
|
+ moveMethod.releaseConnection();
|
|
|
+
|
|
|
+ Assert.assertFalse(fs.file("srcFile").exists());
|
|
|
+ Assert.assertTrue(fs.file("dstFile").exists());
|
|
|
+ Assert.assertFalse(fs.folder("dstFile").exists());
|
|
|
+ }
|
|
|
+
|
|
|
+ /* COPY */
|
|
|
+
|
|
|
+ @Test
|
|
|
+ public void testCopyFolder() throws HttpException, IOException, ParserConfigurationException, SAXException, XPathException, DavException {
|
|
|
+ final HttpClient client = new HttpClient();
|
|
|
+
|
|
|
+ fs.folder("srcFolder").folder("sub").create();
|
|
|
+ try (WritableFile w = fs.folder("srcFolder").file("file").openWritable()) {
|
|
|
+ w.write(ByteBuffer.allocate(0));
|
|
|
+ }
|
|
|
+
|
|
|
+ final DavMethodBase copyMethod = new CopyMethod(servletRoot + "/srcFolder", servletRoot + "/dstFolder", false);
|
|
|
+ client.executeMethod(copyMethod);
|
|
|
+ Assert.assertTrue(copyMethod.succeeded());
|
|
|
+ copyMethod.releaseConnection();
|
|
|
+
|
|
|
+ Assert.assertTrue(fs.folder("srcFolder").folder("sub").exists());
|
|
|
+ Assert.assertTrue(fs.folder("srcFolder").file("file").exists());
|
|
|
+ Assert.assertTrue(fs.folder("dstFolder").folder("sub").exists());
|
|
|
+ Assert.assertTrue(fs.folder("dstFolder").file("file").exists());
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ public void testCopyFolderOverwrite() throws HttpException, IOException, ParserConfigurationException, SAXException, XPathException, DavException {
|
|
|
+ final HttpClient client = new HttpClient();
|
|
|
+
|
|
|
+ fs.folder("srcFolder").create();
|
|
|
+ try (WritableFile w = fs.file("dstFolder").openWritable()) {
|
|
|
+ w.write(ByteBuffer.allocate(0));
|
|
|
+ }
|
|
|
+
|
|
|
+ final DavMethodBase copyMethod = new CopyMethod(servletRoot + "/srcFolder", servletRoot + "/dstFolder/", true);
|
|
|
+ client.executeMethod(copyMethod);
|
|
|
+ Assert.assertTrue(copyMethod.succeeded());
|
|
|
+ copyMethod.releaseConnection();
|
|
|
+
|
|
|
+ Assert.assertTrue(fs.folder("srcFolder").exists());
|
|
|
+ Assert.assertTrue(fs.folder("dstFolder").exists());
|
|
|
+ Assert.assertFalse(fs.file("dstFolder").exists());
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ public void testCopyFile() throws HttpException, IOException, ParserConfigurationException, SAXException, XPathException, DavException {
|
|
|
+ final HttpClient client = new HttpClient();
|
|
|
+
|
|
|
+ try (WritableFile w = fs.file("srcFile").openWritable()) {
|
|
|
+ w.write(ByteBuffer.allocate(0));
|
|
|
+ }
|
|
|
+
|
|
|
+ final DavMethodBase copyMethod = new CopyMethod(servletRoot + "/srcFile/", servletRoot + "/dstFile/", false);
|
|
|
+ client.executeMethod(copyMethod);
|
|
|
+ Assert.assertTrue(copyMethod.succeeded());
|
|
|
+ copyMethod.releaseConnection();
|
|
|
+
|
|
|
+ Assert.assertTrue(fs.file("srcFile").exists());
|
|
|
+ Assert.assertTrue(fs.file("dstFile").exists());
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ public void testCopyFileOverwrite() throws HttpException, IOException, ParserConfigurationException, SAXException, XPathException, DavException {
|
|
|
+ final HttpClient client = new HttpClient();
|
|
|
+
|
|
|
+ try (WritableFile w = fs.file("srcFile").openWritable()) {
|
|
|
+ w.write(ByteBuffer.allocate(0));
|
|
|
+ }
|
|
|
+ fs.folder("dstFile").create();
|
|
|
+
|
|
|
+ final DavMethodBase copyMethod = new CopyMethod(servletRoot + "/srcFile/", servletRoot + "/dstFile/", true);
|
|
|
+ client.executeMethod(copyMethod);
|
|
|
+ Assert.assertTrue(copyMethod.succeeded());
|
|
|
+ copyMethod.releaseConnection();
|
|
|
+
|
|
|
+ Assert.assertTrue(fs.file("srcFile").exists());
|
|
|
+ Assert.assertTrue(fs.file("dstFile").exists());
|
|
|
+ Assert.assertFalse(fs.folder("dstFile").exists());
|
|
|
+ }
|
|
|
+
|
|
|
}
|