KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > xmldb > xupdate > unittests > LexusTestCase


1 package org.xmldb.xupdate.unittests;
2
3 /*
4  * The XML:DB Initiative Software License, Version 1.0
5  *
6  *
7  * Copyright (c) 2000-2003 The XML:DB Initiative. All rights
8  * reserved.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  *
14  * 1. Redistributions of source code must retain the above copyright
15  * notice, this list of conditions and the following disclaimer.
16  *
17  * 2. Redistributions in binary form must reproduce the above copyright
18  * notice, this list of conditions and the following disclaimer in
19  * the documentation and/or other materials provided with the
20  * distribution.
21  *
22  * 3. The end-user documentation included with the redistribution,
23  * if any, must include the following acknowledgment:
24  * "This product includes software developed by the
25  * XML:DB Initiative (http://www.xmldb.org/)."
26  * Alternately, this acknowledgment may appear in the software itself,
27  * if and wherever such third-party acknowledgments normally appear.
28  *
29  * 4. The name "XML:DB Initiative" must not be used to endorse or
30  * promote products derived from this software without prior written
31  * permission. For written permission, please contact info@xmldb.org.
32  *
33  * 5. Products derived from this software may not be called "XML:DB",
34  * nor may "XML:DB" appear in their name, without prior written
35  * permission of the XML:DB Initiative.
36  *
37  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
38  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
39  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
40  * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
41  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
42  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
43  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
44  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
45  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
46  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
47  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
48  * SUCH DAMAGE.
49  * ====================================================================
50  *
51  * This software consists of voluntary contributions made by many
52  * individuals on behalf of the XML:DB Initiative. For more information
53  * on the XML:DB Initiative, please see <http://www.xmldb.org/>.
54  */

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 JavaDoc;
62 import org.w3c.dom.Element JavaDoc;
63 import org.w3c.dom.Node JavaDoc;
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 JavaDoc;
71 import javax.xml.parsers.DocumentBuilderFactory JavaDoc;
72 import java.io.StringWriter JavaDoc;
73
74 /**
75  *
76  */

77 public class LexusTestCase extends TestCase {
78
79   private static Document JavaDoc updates;
80   private static DocumentBuilderFactory JavaDoc factory = DocumentBuilderFactory.newInstance();
81
82   static final String JavaDoc RESOURCE_LOCATION = "org/xmldb/xupdate/unittests/";
83   static final String JavaDoc XPATHQUERYFACTORY = "org.xmldb.common.xml.queries.xalan2.XPathQueryFactoryImpl";
84
85   /**
86    *
87    */

88   public LexusTestCase(String JavaDoc name) {
89     super(name);
90   }
91
92   public void mainTest(String JavaDoc test) throws Exception JavaDoc {
93     if (updates == null) {
94       updates = parseInputFile(RESOURCE_LOCATION + "tests.xml");
95     }
96
97     Node JavaDoc xupdateQuery = XPathAPI.selectSingleNode(updates, "/tests/test[@name='" + test + "']/xupdate/*", updates);
98     String JavaDoc query = serialize((Element JavaDoc) xupdateQuery);
99
100     Document JavaDoc 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 JavaDoc expected = getDocument(updates, "/tests/test[@name='" + test + "']/result/*");
108     removeWhiteSpace(expected);
109
110 // System.out.println("expected: \n" + serialize((expected)));
111
// System.out.println("result: \n" + serialize(result));
112

113     new XhiveNodeMatcher().compareNodes(expected, result, true, true);
114   }
115
116   /**
117    * Parses input file and generates DOM.
118    */

119   private Document JavaDoc parseInputFile(String JavaDoc inputFile) throws Exception JavaDoc {
120     DocumentBuilder JavaDoc builder = getDocumentBuilder();
121     return builder.parse(inputFile);
122   }
123
124   private String JavaDoc serialize(Element JavaDoc element) throws Exception JavaDoc {
125     StringWriter JavaDoc writer = new StringWriter JavaDoc();
126     getSerializer(writer).serialize(element);
127     return writer.toString();
128   }
129
130   private String JavaDoc serialize(Document JavaDoc document) throws Exception JavaDoc {
131     StringWriter JavaDoc writer = new StringWriter JavaDoc();
132     getSerializer(writer).serialize(document);
133     return writer.toString();
134   }
135
136   private DOMSerializer getSerializer(StringWriter JavaDoc writer) throws Exception JavaDoc {
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 JavaDoc getDocumentBuilder() throws Exception JavaDoc {
143     factory.setNamespaceAware(true);
144     DocumentBuilder JavaDoc builder = factory.newDocumentBuilder();
145     return builder;
146   }
147
148   private Document JavaDoc getDocument(Node JavaDoc contextNode, String JavaDoc query) throws Exception JavaDoc {
149     Node JavaDoc result = XPathAPI.selectSingleNode(contextNode, query, contextNode);
150     DocumentBuilder JavaDoc builder = getDocumentBuilder();
151     Document JavaDoc document = builder.newDocument();
152     result = document.importNode(result, true);
153     document.appendChild(result);
154     return document;
155   }
156
157   private void removeWhiteSpace(Node JavaDoc context) {
158     DocumentTraversal traversal = context instanceof Document JavaDoc
159             ? (DocumentTraversal) context
160             : (DocumentTraversal) context.getOwnerDocument();
161     NodeIterator nodeIterator = traversal.createNodeIterator(context, NodeFilter.SHOW_TEXT, null, true);
162     Node JavaDoc 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