1 package org.xmldb.xupdate.unittests; 2 3 55 56 import junit.framework.TestCase; 57 import org.apache.xml.serialize.DOMSerializer; 58 import org.apache.xml.serialize.OutputFormat; 59 import org.apache.xml.serialize.XMLSerializer; 60 import org.apache.xpath.XPathAPI; 61 import org.w3c.dom.Document ; 62 import org.w3c.dom.Element ; 63 import org.w3c.dom.Node ; 64 import org.w3c.dom.traversal.DocumentTraversal; 65 import org.w3c.dom.traversal.NodeFilter; 66 import org.w3c.dom.traversal.NodeIterator; 67 import org.xmldb.common.xml.queries.XUpdateQuery; 68 import org.xmldb.xupdate.lexus.XUpdateQueryImpl; 69 70 import javax.xml.parsers.DocumentBuilder ; 71 import javax.xml.parsers.DocumentBuilderFactory ; 72 import java.io.StringWriter ; 73 74 77 public class LexusTestCase extends TestCase { 78 79 private static Document updates; 80 private static DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); 81 82 static final String RESOURCE_LOCATION = "org/xmldb/xupdate/unittests/"; 83 static final String XPATHQUERYFACTORY = "org.xmldb.common.xml.queries.xalan2.XPathQueryFactoryImpl"; 84 85 88 public LexusTestCase(String name) { 89 super(name); 90 } 91 92 public void mainTest(String test) throws Exception { 93 if (updates == null) { 94 updates = parseInputFile(RESOURCE_LOCATION + "tests.xml"); 95 } 96 97 Node xupdateQuery = XPathAPI.selectSingleNode(updates, "/tests/test[@name='" + test + "']/xupdate/*", updates); 98 String query = serialize((Element ) xupdateQuery); 99 100 Document result = getDocument(updates, "/tests/input[@name=/tests/test[@name='" + test + "']/@input]/*"); 101 XUpdateQuery xupdate = new XUpdateQueryImpl(); 102 xupdate.setQString(query); 103 System.setProperty("org.xmldb.common.xml.queries.XPathQueryFactory", XPATHQUERYFACTORY); 104 xupdate.execute(result); 105 106 removeWhiteSpace(result); 107 Document expected = getDocument(updates, "/tests/test[@name='" + test + "']/result/*"); 108 removeWhiteSpace(expected); 109 110 113 new XhiveNodeMatcher().compareNodes(expected, result, true, true); 114 } 115 116 119 private Document parseInputFile(String inputFile) throws Exception { 120 DocumentBuilder builder = getDocumentBuilder(); 121 return builder.parse(inputFile); 122 } 123 124 private String serialize(Element element) throws Exception { 125 StringWriter writer = new StringWriter (); 126 getSerializer(writer).serialize(element); 127 return writer.toString(); 128 } 129 130 private String serialize(Document document) throws Exception { 131 StringWriter writer = new StringWriter (); 132 getSerializer(writer).serialize(document); 133 return writer.toString(); 134 } 135 136 private DOMSerializer getSerializer(StringWriter writer) throws Exception { 137 OutputFormat outputFormat = new OutputFormat("xml", "UTF-8", true); 138 XMLSerializer serializer = new XMLSerializer(writer, outputFormat); 139 return serializer.asDOMSerializer(); 140 } 141 142 private DocumentBuilder getDocumentBuilder() throws Exception { 143 factory.setNamespaceAware(true); 144 DocumentBuilder builder = factory.newDocumentBuilder(); 145 return builder; 146 } 147 148 private Document getDocument(Node contextNode, String query) throws Exception { 149 Node result = XPathAPI.selectSingleNode(contextNode, query, contextNode); 150 DocumentBuilder builder = getDocumentBuilder(); 151 Document document = builder.newDocument(); 152 result = document.importNode(result, true); 153 document.appendChild(result); 154 return document; 155 } 156 157 private void removeWhiteSpace(Node context) { 158 DocumentTraversal traversal = context instanceof Document 159 ? (DocumentTraversal) context 160 : (DocumentTraversal) context.getOwnerDocument(); 161 NodeIterator nodeIterator = traversal.createNodeIterator(context, NodeFilter.SHOW_TEXT, null, true); 162 Node node = nodeIterator.nextNode(); 163 while (node != null) { 164 if (node.getNodeValue().trim().compareTo("") == 0) { 165 node.getParentNode().removeChild(node); 166 } 167 node = nodeIterator.nextNode(); 168 } 169 } 170 } 171 | Popular Tags |