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