Skip to content

Commit deb713e

Browse files
committed
[test] Add tests for WebDAV Doctype Serialization
1 parent b37c209 commit deb713e

File tree

2 files changed

+108
-1
lines changed

2 files changed

+108
-1
lines changed

extensions/webdav/src/main/resources/webdav.properties

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,5 @@
2727
#expand-xincludes=no
2828
#process-xsl-pi=no
2929
#encoding=UTF-8
30-
#omit-xml-declaration=no
30+
#omit-xml-declaration=no
31+
output-doctype=false
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
/*
2+
* eXist-db Open Source Native XML Database
3+
* Copyright (C) 2001 The eXist-db Authors
4+
*
5+
6+
* http://www.exist-db.org
7+
*
8+
* This library is free software; you can redistribute it and/or
9+
* modify it under the terms of the GNU Lesser General Public
10+
* License as published by the Free Software Foundation; either
11+
* version 2.1 of the License, or (at your option) any later version.
12+
*
13+
* This library is distributed in the hope that it will be useful,
14+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
15+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16+
* Lesser General Public License for more details.
17+
*
18+
* You should have received a copy of the GNU Lesser General Public
19+
* License along with this library; if not, write to the Free Software
20+
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21+
*/
22+
23+
package org.exist.webdav;
24+
25+
import com.bradmcevoy.http.exceptions.BadRequestException;
26+
import com.bradmcevoy.http.exceptions.ConflictException;
27+
import com.bradmcevoy.http.exceptions.NotAuthorizedException;
28+
import com.bradmcevoy.http.exceptions.NotFoundException;
29+
import com.ettrema.httpclient.*;
30+
import org.apache.http.impl.client.AbstractHttpClient;
31+
import org.exist.TestUtils;
32+
import org.exist.test.ExistWebServer;
33+
import org.junit.AfterClass;
34+
import org.junit.BeforeClass;
35+
import org.junit.ClassRule;
36+
import org.junit.Test;
37+
import org.junit.rules.TemporaryFolder;
38+
39+
import java.io.IOException;
40+
import java.nio.file.Files;
41+
42+
import static java.nio.charset.StandardCharsets.UTF_8;
43+
import static org.junit.Assert.*;
44+
import static org.junit.Assert.assertEquals;
45+
46+
public class SerializationTest {
47+
48+
private static final String XML_WITH_DOCTYPE =
49+
"<!DOCTYPE bookmap PUBLIC \"-//OASIS//DTD DITA BookMap//EN\" \"bookmap.dtd\">\n" +
50+
"<bookmap id=\"bookmap-1\"/>";
51+
52+
private static String PREV_PROPFIND_METHOD_XML_SIZE = null;
53+
54+
@ClassRule
55+
public static final ExistWebServer EXIST_WEB_SERVER = new ExistWebServer(true, false, true, true);
56+
57+
@ClassRule
58+
public static final TemporaryFolder TEMP_FOLDER = new TemporaryFolder();
59+
60+
@BeforeClass
61+
public static void setup() {
62+
PREV_PROPFIND_METHOD_XML_SIZE = System.setProperty(MiltonDocument.PROPFIND_METHOD_XML_SIZE, "exact");
63+
}
64+
65+
@AfterClass
66+
public static void cleanup() {
67+
if (PREV_PROPFIND_METHOD_XML_SIZE == null) {
68+
System.clearProperty(MiltonDocument.PROPFIND_METHOD_XML_SIZE);
69+
} else {
70+
System.setProperty(MiltonDocument.PROPFIND_METHOD_XML_SIZE, PREV_PROPFIND_METHOD_XML_SIZE);
71+
}
72+
}
73+
74+
@Test
75+
public void getDocTypeDefault() throws IOException, NotAuthorizedException, BadRequestException, HttpException, ConflictException, NotFoundException {
76+
final String docName = "test-with-doctype.xml";
77+
final HostBuilder builder = new HostBuilder();
78+
builder.setServer("localhost");
79+
final int port = EXIST_WEB_SERVER.getPort();
80+
builder.setPort(port);
81+
builder.setRootPath("webdav/db");
82+
final Host host = builder.buildHost();
83+
84+
// workaround pre-emptive auth issues of Milton Client
85+
final AbstractHttpClient httpClient = (AbstractHttpClient)host.getClient();
86+
httpClient.addRequestInterceptor(new AlwaysBasicPreAuth(TestUtils.ADMIN_DB_USER, TestUtils.ADMIN_DB_PWD));
87+
88+
final Folder folder = host.getFolder("/");
89+
assertNotNull(folder);
90+
91+
// store document
92+
final byte data[] = XML_WITH_DOCTYPE.getBytes(UTF_8);
93+
final java.io.File tmpStoreFile = TEMP_FOLDER.newFile();
94+
Files.write(tmpStoreFile.toPath(), data);
95+
assertNotNull(folder.uploadFile(docName, tmpStoreFile, null));
96+
97+
// retrieve document
98+
final Resource resource = folder.child(docName);
99+
assertNotNull(resource);
100+
assertTrue(resource instanceof File);
101+
assertEquals("application/xml", ((File) resource).contentType);
102+
final java.io.File tempRetrieveFile = TEMP_FOLDER.newFile();
103+
resource.downloadTo(tempRetrieveFile, null);
104+
assertEquals("<bookmap id=\"bookmap-1\"/>", new String(Files.readAllBytes(tempRetrieveFile.toPath()), UTF_8));
105+
}
106+
}

0 commit comments

Comments
 (0)