|
18 | 18 | */
|
19 | 19 | package org.apache.ode.utils;
|
20 | 20 |
|
| 21 | +import net.sf.saxon.dom.DocumentBuilderFactoryImpl; |
| 22 | + |
21 | 23 | import org.apache.ode.utils.TestResources;
|
22 | 24 |
|
| 25 | +import java.io.ByteArrayInputStream; |
| 26 | +import java.io.IOException; |
23 | 27 | import java.io.InputStream;
|
| 28 | +import java.util.Map; |
| 29 | + |
| 30 | +import javax.xml.parsers.DocumentBuilder; |
| 31 | +import javax.xml.parsers.DocumentBuilderFactory; |
| 32 | +import javax.xml.parsers.ParserConfigurationException; |
24 | 33 |
|
25 | 34 | import junit.framework.TestCase;
|
26 | 35 |
|
27 | 36 | import org.w3c.dom.Document;
|
28 | 37 | import org.w3c.dom.Element;
|
29 | 38 | import org.w3c.dom.Node;
|
30 | 39 | import org.w3c.dom.NodeList;
|
| 40 | +import org.xml.sax.SAXException; |
31 | 41 |
|
32 | 42 | /**
|
33 | 43 | * Test the {@link DOMUtils} class.
|
34 | 44 | */
|
35 | 45 | public class DOMUtilsTest extends TestCase {
|
36 |
| - |
| 46 | + private static final String SAXON_DOM_DOCUMENT_BUILDER_FACTORY = "net.sf.saxon.dom.DocumentBuilderFactoryImpl"; |
| 47 | + private static final String DOCUMENT_BUILDER_FACTORY = "javax.xml.parsers.DocumentBuilderFactory"; |
| 48 | + String defaultBuilderFactory = null; |
| 49 | + |
| 50 | + @Override |
| 51 | + protected void setUp() throws Exception { |
| 52 | + super.setUp(); |
| 53 | + defaultBuilderFactory = System.getProperty(DOCUMENT_BUILDER_FACTORY, "org.apache.xerces.jaxp.DocumentBuilderFactoryImpl"); |
| 54 | + } |
| 55 | + |
| 56 | + @Override |
| 57 | + protected void tearDown() throws Exception { |
| 58 | + super.tearDown(); |
| 59 | + |
| 60 | + System.setProperty(DOCUMENT_BUILDER_FACTORY, defaultBuilderFactory); |
| 61 | + } |
| 62 | + |
37 | 63 | public void testParseInputStream() throws Exception {
|
38 | 64 | Document doc = DOMUtils.parse(TestResources.getLoanApprovalProcess().openStream());
|
39 | 65 | assertEquals("process", doc.getDocumentElement().getLocalName());
|
@@ -110,5 +136,94 @@ public void testIsEmptyElement() throws Exception {
|
110 | 136 | }
|
111 | 137 | }
|
112 | 138 | }
|
| 139 | + |
| 140 | + public void testCloneNode() throws Exception { |
| 141 | + String testString = "<ns1:parent xmlns:ns1=\"abc\">\n" + |
| 142 | + " <ns1:child xmlns=\"def\">\n" + |
| 143 | + " <ns2:nestedChild xmlns:ns2=\"def\"/>\n" + |
| 144 | + " </ns1:child>\n" + |
| 145 | + "</ns1:parent>"; |
| 146 | + |
| 147 | + Document doc = DOMUtils.parse(new ByteArrayInputStream(testString.getBytes())); |
| 148 | + Node node = doc.getFirstChild(); |
| 149 | + Node clonedNode = DOMUtils.cloneNode(doc, node); |
| 150 | + String actualString = DOMUtils.domToString(clonedNode).replace("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n", ""); |
| 151 | + assertEquals("XML Result", testString, actualString); |
| 152 | + } |
| 153 | + |
| 154 | + public void testCloneNodeNewDocument() throws Exception { |
| 155 | + String testString = "<ns1:parent xmlns:ns1=\"abc\">\n" + |
| 156 | + " <ns1:child xmlns=\"def\">\n" + |
| 157 | + " <ns2:nestedChild xmlns:ns2=\"def\"/>\n" + |
| 158 | + " </ns1:child>\n" + |
| 159 | + "</ns1:parent>"; |
| 160 | + |
| 161 | + Document doc = DOMUtils.parse(new ByteArrayInputStream(testString.getBytes())); |
| 162 | + Document doc2 = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument(); |
| 163 | + Node node = doc.getFirstChild(); |
| 164 | + Node clonedNode = DOMUtils.cloneNode(doc2, node); |
| 165 | + |
| 166 | + assertNotSame("DOM's are the same", doc, doc2); |
| 167 | + String actualString = DOMUtils.domToString(clonedNode).replace("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n", ""); |
| 168 | + assertEquals("XML Result", testString, actualString); |
| 169 | + |
| 170 | + } |
| 171 | + |
| 172 | + /** |
| 173 | + * A Saxon's DOM is read only. cloneNode should throw an UnsupportedOperationException. |
| 174 | + * |
| 175 | + * @throws Exception |
| 176 | + */ |
| 177 | + public void testCloneNodeSaxon() throws Exception { |
| 178 | + String testString = "<ns1:parent xmlns:ns1=\"abc\">\n" + |
| 179 | + " <ns1:child xmlns=\"def\">\n" + |
| 180 | + " <ns2:nestedChild xmlns:ns2=\"def\"/>\n" + |
| 181 | + " </ns1:child>\n" + |
| 182 | + "</ns1:parent>"; |
| 183 | + |
| 184 | + Document doc = createSaxonDOM(testString); |
| 185 | + |
| 186 | + Node node = doc.getFirstChild(); |
| 187 | + try { |
| 188 | + Node clonedNode = DOMUtils.cloneNode(doc, node); |
| 189 | + } catch (UnsupportedOperationException ex) { |
| 190 | + |
| 191 | + } |
| 192 | + |
| 193 | + } |
| 194 | + |
| 195 | + public void testCloneNodeNewDocumentSaxon() throws Exception { |
| 196 | + String testString = "<ns1:parent xmlns:ns1=\"abc\">\n" + |
| 197 | + " <ns1:child xmlns=\"def\">\n" + |
| 198 | + " <ns2:nestedChild xmlns:ns2=\"def\"/>\n" + |
| 199 | + " </ns1:child>\n" + |
| 200 | + "</ns1:parent>"; |
| 201 | + |
| 202 | + String saxonString = "<ns1:parent xmlns:ns1=\"abc\">\n" + |
| 203 | + " <ns1:child xmlns=\"def\">\n" + |
| 204 | + " <nestedChild xmlns:ns2=\"def\"/>\n" + |
| 205 | + " </ns1:child>\n" + |
| 206 | + "</ns1:parent>"; |
| 207 | + |
| 208 | + Document doc = createSaxonDOM(testString); |
| 209 | + Document doc2 = DOMUtils.newDocument(); |
| 210 | + Node node = doc.getFirstChild(); |
| 211 | + Node clonedNode = DOMUtils.cloneNode(doc2, node); |
| 212 | + |
| 213 | + assertNotSame("DOM's are the same", doc, doc2); |
| 214 | + String actualString = DOMUtils.domToString(clonedNode).replace("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n", ""); |
| 215 | + assertEquals("XML Result", saxonString, actualString); |
| 216 | + |
| 217 | + } |
113 | 218 |
|
| 219 | + private Document createSaxonDOM(String testString) |
| 220 | + throws ParserConfigurationException, SAXException, IOException { |
| 221 | + System.setProperty(DOCUMENT_BUILDER_FACTORY, SAXON_DOM_DOCUMENT_BUILDER_FACTORY); |
| 222 | + DocumentBuilderFactory factory = DocumentBuilderFactoryImpl.newInstance(); |
| 223 | + factory.setNamespaceAware(true); |
| 224 | + DocumentBuilder saxonBuilder = factory.newDocumentBuilder(); |
| 225 | + Document doc = saxonBuilder.parse(new ByteArrayInputStream(testString.getBytes())); |
| 226 | + return doc; |
| 227 | + } |
| 228 | + |
114 | 229 | }
|
0 commit comments