Skip to content

Commit

Permalink
[test] fix SerializationTest in xmldb and webdav
Browse files Browse the repository at this point in the history
refs #4736
  • Loading branch information
line-o committed Jul 25, 2024
1 parent 36b8825 commit 1d955b1
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 8 deletions.
60 changes: 55 additions & 5 deletions exist-core/src/test/java/org/exist/xmldb/SerializationTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
import org.xmlunit.builder.Input;
import org.xmlunit.diff.Diff;

import javax.xml.transform.OutputKeys;
import javax.xml.transform.Source;

import java.util.Arrays;
Expand Down Expand Up @@ -94,8 +95,11 @@ public class SerializationTest {
private static final XmldbURI TEST_XML_DOC_WITH_DOCTYPE_URI = XmldbURI.create("test-with-doctype.xml");

private static final String XML_WITH_DOCTYPE =
"<!DOCTYPE bookmap PUBLIC \"-//OASIS//DTD DITA BookMap//EN\" \"bookmap.dtd\">\n" +
"<bookmap id=\"bookmap-1\">\n asdf\n </bookmap>";
"""
<!DOCTYPE bookmap PUBLIC "-//OASIS//DTD DITA BookMap//EN" "bookmap.dtd">
<bookmap id="bookmap-1">
<title>The Title</title>
</bookmap>""";

private static final XmldbURI TEST_XML_DOC_WITH_XMLDECL_URI = XmldbURI.create("test-with-xmldecl.xml");

Expand Down Expand Up @@ -187,7 +191,7 @@ public void getDocTypeNo() throws XMLDBException {
try {
final Resource res = testCollection.getResource(TEST_XML_DOC_WITH_DOCTYPE_URI.lastSegmentString());
testCollection.setProperty(EXistOutputKeys.OUTPUT_DOCTYPE, "no");
assertEquals("<bookmap id=\"bookmap-1\">\n asdf\n </bookmap>", res.getContent());
assertEquals("<bookmap id=\"bookmap-1\">\n <title>The Title</title>\n</bookmap>", res.getContent());
} finally {
if (prevOutputDocType != null) {
testCollection.setProperty(EXistOutputKeys.OUTPUT_DOCTYPE, prevOutputDocType);
Expand Down Expand Up @@ -216,7 +220,7 @@ public void getXmlDeclDefault() throws XMLDBException {
}

@Test
public void getXmlDeclNo() throws XMLDBException {
public void getOmitOriginalXmlDeclNo() throws XMLDBException {
final String prevOmitOriginalXmlDecl = testCollection.getProperty(EXistOutputKeys.OMIT_ORIGINAL_XML_DECLARATION);
try {
final Resource res = testCollection.getResource(TEST_XML_DOC_WITH_XMLDECL_URI.lastSegmentString());
Expand All @@ -230,7 +234,7 @@ public void getXmlDeclNo() throws XMLDBException {
}

@Test
public void getXmlDeclYes() throws XMLDBException {
public void getOmitOriginalXmlDeclYes() throws XMLDBException {
final String prevOmitOriginalXmlDecl = testCollection.getProperty(EXistOutputKeys.OMIT_ORIGINAL_XML_DECLARATION);
try {
final Resource res = testCollection.getResource(TEST_XML_DOC_WITH_XMLDECL_URI.lastSegmentString());
Expand All @@ -242,6 +246,52 @@ public void getXmlDeclYes() throws XMLDBException {
}
}
}
@Test
public void getOmitXmlDeclNo() throws XMLDBException {
final String prevOmitXmlDecl = testCollection.getProperty(OutputKeys.OMIT_XML_DECLARATION);
try {
final Resource res = testCollection.getResource(TEST_XML_DOC_WITH_XMLDECL_URI.lastSegmentString());
testCollection.setProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
assertEquals(XML_WITH_XMLDECL, res.getContent());
} finally {
if (prevOmitXmlDecl != null) {
testCollection.setProperty(OutputKeys.OMIT_XML_DECLARATION, prevOmitXmlDecl);
}
}
}

@Test
public void getOmitXmlDeclYes() throws XMLDBException {
final String prevOmitXmlDecl = testCollection.getProperty(OutputKeys.OMIT_XML_DECLARATION);
try {
final Resource res = testCollection.getResource(TEST_XML_DOC_WITH_XMLDECL_URI.lastSegmentString());
testCollection.setProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
assertEquals(XML_WITH_XMLDECL, res.getContent());
} finally {
if (prevOmitXmlDecl != null) {
testCollection.setProperty(OutputKeys.OMIT_XML_DECLARATION, prevOmitXmlDecl);
}
}
}

@Test
public void getOmitAllXmlDeclYes() throws XMLDBException {
final String prevOmitXmlDecl = testCollection.getProperty(OutputKeys.OMIT_XML_DECLARATION);
final String prevOmitOriginalXmlDecl = testCollection.getProperty(EXistOutputKeys.OMIT_ORIGINAL_XML_DECLARATION);
try {
final Resource res = testCollection.getResource(TEST_XML_DOC_WITH_XMLDECL_URI.lastSegmentString());
testCollection.setProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
testCollection.setProperty(EXistOutputKeys.OMIT_ORIGINAL_XML_DECLARATION, "yes");
assertEquals("<bookmap id=\"bookmap-2\"/>", res.getContent());
} finally {
if (prevOmitXmlDecl != null) {
testCollection.setProperty(OutputKeys.OMIT_XML_DECLARATION, prevOmitXmlDecl);
}
if (prevOmitOriginalXmlDecl != null) {
testCollection.setProperty(EXistOutputKeys.OMIT_ORIGINAL_XML_DECLARATION, prevOmitOriginalXmlDecl);
}
}
}

private static void assertXMLEquals(final String expected, final Resource actual) throws XMLDBException {
final Source srcExpected = Input.fromString(expected).build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,14 @@
import java.io.IOException;
import java.nio.file.Files;

import static java.nio.charset.StandardCharsets.UTF_8;
import static org.junit.Assert.*;
import static org.junit.Assert.assertEquals;

public class SerializationTest {

private static final String XML_WITH_DOCTYPE =
"<!DOCTYPE bookmap PUBLIC \"-//OASIS//DTD DITA BookMap//EN\" \"bookmap.dtd\">\n" +
"<bookmap id=\"bookmap-1\"/>";
"<!DOCTYPE bookmap PUBLIC \"-//OASIS//DTD DITA BookMap//EN\" \"bookmap.dtd\">" +
"<bookmap id=\"bookmap-1\"><title>The Title</title></bookmap>";

private static final String XML_WITH_XMLDECL =
"<?xml version=\"1.1\" encoding=\"ISO-8859-1\" standalone=\"yes\"?>\n" +
Expand Down

0 comments on commit 1d955b1

Please sign in to comment.