Skip to content

Commit 3f7b82b

Browse files
authored
Merge pull request #215 from marklogic-community/feature/read-xml-namespaces
#211 Added method to read XML document with namespaces
2 parents 443db79 + 663acd5 commit 3f7b82b

File tree

2 files changed

+53
-7
lines changed

2 files changed

+53
-7
lines changed

marklogic-junit5/src/main/java/com/marklogic/junit5/AbstractMarkLogicTest.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,11 @@
1212
import com.marklogic.test.unit.TestModule;
1313
import com.marklogic.test.unit.TestResult;
1414
import com.marklogic.test.unit.TestSuiteResult;
15+
import org.jdom2.Namespace;
1516
import org.junit.jupiter.api.Assertions;
1617
import org.junit.jupiter.api.BeforeEach;
1718

19+
import java.util.ArrayList;
1820
import java.util.List;
1921

2022
/**
@@ -74,6 +76,15 @@ protected XmlNode parseXml(String xml) {
7476
return new XmlNode(xml, getNamespaceProvider().getNamespaces());
7577
}
7678

79+
/**
80+
* Read an XML document without making any assertions on its collections.
81+
*
82+
* @since 1.5.0
83+
*/
84+
protected XmlNode readXmlDocument(String uri) {
85+
return readXmlDocument(uri, (String[]) null);
86+
}
87+
7788
/**
7889
* Read the XML document at the given URI and return an XmlNode for making assertions on the contents of the XML.
7990
*
@@ -89,6 +100,23 @@ protected XmlNode readXmlDocument(String uri, String... expectedCollections) {
89100
return new XmlNode(uri, xml, getNamespaceProvider().getNamespaces());
90101
}
91102

103+
/**
104+
* Read an XML document with the given namespaces included in the returned {@code XmlNode}.
105+
*
106+
* @since 1.5.0
107+
*/
108+
protected XmlNode readXmlDocument(String uri, Namespace... namespaces) {
109+
String xml = getDatabaseClient().newXMLDocumentManager().read(uri, new StringHandle()).get();
110+
List<Namespace> list = new ArrayList<>();
111+
for (Namespace ns : getNamespaceProvider().getNamespaces()) {
112+
list.add(ns);
113+
}
114+
for (Namespace ns : namespaces) {
115+
list.add(ns);
116+
}
117+
return new XmlNode(uri, xml, list.toArray(new Namespace[0]));
118+
}
119+
92120
/**
93121
* Read the JSON document at the given URI and return a JsonNode.
94122
*

marklogic-junit5/src/test/java/com/marklogic/junit5/spring/XmlNodeTest.java

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,33 +4,44 @@
44
import com.marklogic.junit5.MarkLogicNamespaceProvider;
55
import com.marklogic.junit5.NamespaceProvider;
66
import com.marklogic.junit5.XmlNode;
7+
import org.jdom2.Namespace;
78
import org.junit.jupiter.api.Assertions;
9+
import org.junit.jupiter.api.BeforeEach;
810
import org.junit.jupiter.api.Test;
911

1012
import static org.junit.jupiter.api.Assertions.assertEquals;
1113
import static org.junit.jupiter.api.Assertions.assertNotNull;
1214

13-
public class XmlNodeTest extends AbstractSpringMarkLogicTest {
15+
class XmlNodeTest extends AbstractSpringMarkLogicTest {
16+
17+
private static final String TEST_URI = "/test/1.xml";
18+
19+
private boolean useCustomNamespaceProvider = true;
1420

1521
@Override
1622
protected NamespaceProvider getNamespaceProvider() {
17-
return new MarkLogicNamespaceProvider("m", "org:example");
23+
return useCustomNamespaceProvider ?
24+
new MarkLogicNamespaceProvider("m", "org:example") :
25+
super.getNamespaceProvider();
1826
}
1927

20-
@Test
21-
public void test() {
22-
getDatabaseClient().newXMLDocumentManager().write("/test/1.xml",
28+
@BeforeEach
29+
void setup() {
30+
getDatabaseClient().newXMLDocumentManager().write(TEST_URI,
2331
new StringHandle("" +
2432
"<message xmlns='org:example'>" +
2533
"<color important='true'>red</color>" +
2634
"<color>blue</color>" +
2735
"<size>medium</size>" +
2836
"<parent><kid>hello</kid></parent>" +
2937
"</message>"));
38+
}
3039

31-
XmlNode xml = readXmlDocument("/test/1.xml");
40+
@Test
41+
public void test() {
42+
XmlNode xml = readXmlDocument(TEST_URI);
3243

33-
assertEquals("/test/1.xml", xml.getUri());
44+
assertEquals(TEST_URI, xml.getUri());
3445
xml.assertElementValue("/m:message/m:size", "medium");
3546
assertEquals("medium", xml.getElementValue("/m:message/m:size"));
3647
assertEquals("true", xml.getAttributeValue("/m:message/m:color[. = 'red']", "important"));
@@ -47,4 +58,11 @@ public void test() {
4758
xml.prettyPrint();
4859
assertNotNull(xml.getPrettyXml());
4960
}
61+
62+
@Test
63+
void readWithNamespaces() {
64+
useCustomNamespaceProvider = false;
65+
XmlNode xml = readXmlDocument(TEST_URI, Namespace.getNamespace("m", "org:example"));
66+
xml.assertElementValue("/m:message/m:size", "medium");
67+
}
5068
}

0 commit comments

Comments
 (0)