001: package de.schlund.pfixxml.util;
002:
003: import java.io.ByteArrayOutputStream;
004: import javax.xml.parsers.DocumentBuilder;
005: import javax.xml.transform.OutputKeys;
006: import javax.xml.transform.Transformer;
007: import javax.xml.transform.dom.DOMSource;
008: import javax.xml.transform.stream.StreamResult;
009: import junit.framework.TestCase;
010: import org.w3c.dom.CDATASection;
011: import org.w3c.dom.Comment;
012: import org.w3c.dom.Document;
013: import org.w3c.dom.Element;
014: import org.w3c.dom.NamedNodeMap;
015: import org.w3c.dom.Node;
016: import org.w3c.dom.NodeList;
017: import org.xml.sax.SAXException;
018: import org.xml.sax.XMLReader;
019:
020: public class XmlTest extends TestCase {
021:
022: protected XsltVersion getXsltVersion() {
023: return XsltVersion.XSLT1;
024: }
025:
026: public void testCreateDocument() {
027: assertNotNull(Xml.createDocument());
028: }
029:
030: //-- parse tests
031:
032: public void testXmlReaderConfig() throws Exception {
033: XMLReader reader;
034:
035: reader = Xml.createXMLReader();
036: assertFalse(reader
037: .getFeature("http://xml.org/sax/features/validation"));
038: assertTrue(reader
039: .getFeature("http://xml.org/sax/features/namespaces"));
040: }
041:
042: public void testDocumentBuilderConfig() {
043: DocumentBuilder builder;
044:
045: builder = Xml.createDocumentBuilder();
046: assertFalse(builder.isValidating());
047: assertTrue(builder.isNamespaceAware());
048: }
049:
050: public void testParse() throws Exception {
051: parse("<ok/>");
052: try {
053: parse("<wrong>");
054: fail();
055: } catch (SAXException e) {
056: // ok
057: }
058: }
059:
060: public void testParseTailingWhitespaceRemoved() throws Exception {
061: Document doc;
062:
063: doc = parse("<ok/> \n");
064: assertEquals(1, doc.getChildNodes().getLength());
065: }
066:
067: public void testParseCDATA() throws Exception {
068: Document doc;
069: Node node;
070:
071: doc = parse("<ok><![CDATA[bla]]></ok>");
072: node = doc.getDocumentElement();
073: assertEquals(1, node.getChildNodes().getLength());
074: assertTrue(node.getChildNodes().item(0) instanceof CDATASection);
075: }
076:
077: public void testComments() throws Exception {
078: // make sure to get comments
079: Document doc = parse("<hello><!-- commend --></hello>");
080: Node ele = doc.getDocumentElement();
081: NodeList lst = ele.getChildNodes();
082: assertEquals(1, lst.getLength());
083: assertTrue(lst.item(0) instanceof Comment);
084: }
085:
086: public void testNamespaceListInTinyTree() throws Exception {
087: Transformer t;
088:
089: // make sure to get comments
090: Document doc = Xml.parseString(getXsltVersion(),
091: "<pfx:include xmlns:pfx='foo'><a/></pfx:include>");
092: Element root = doc.getDocumentElement();
093: NamedNodeMap lst = root.getAttributes();
094: assertEquals(0, lst.getLength());
095: t = Xslt.createIdentityTransformer(getXsltVersion());
096: t.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
097: t.setOutputProperty(OutputKeys.INDENT, "no");
098: t.transform(new DOMSource(doc), new StreamResult(System.out));
099: }
100:
101: //-- serialize tests
102:
103: public void testSerializeSimple() throws Exception {
104: assertEquals("<ok/>", serialize("<ok/>", false, false));
105: }
106:
107: public void testSerializeCDATA() throws Exception {
108: // CAUTION: saxon's identity transformer doesn't preserve CDATA!!!
109: assertEquals("<ok>bla</ok>", serialize(
110: "<ok><![CDATA[bla]]></ok>", false, false));
111: }
112:
113: public void testSerializePreserveInnerWhitespace() throws Exception {
114: final String STR = "<a>\t<b/> \n<c/></a>";
115: assertEquals(STR, serialize(STR, false, false));
116: }
117:
118: public void testSerializeRemoveTailingWhitespace() throws Exception {
119: assertEquals("<a/>", serialize("<a/>\t \n", false, false)); // the parser removes it!
120: }
121:
122: public void testSerializeMergeEvenWithPreserve() throws Exception {
123: assertEquals("<c/>", serialize("<c></c>", false, false));
124: }
125:
126: public void testSerializePP() throws Exception {
127: assertEquals("<a>\n <b/>\n <c/>\n</a>", serialize(
128: "<a><b/><c/></a>", true, false));
129: }
130:
131: public void testSerializePPTailingWhitespaceRemoved()
132: throws Exception {
133: assertEquals("<ok/>", serialize("<ok/>\n ", true, false));
134: }
135:
136: public void testSerializeDecl() throws Exception {
137: assertEquals(
138: "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?><a/>",
139: serialize("<a/>", false, true));
140: }
141:
142: public void testSerializeElement() throws Exception {
143: Document doc = parse("<a><b>foo</b></a>");
144: assertEquals("<b>foo</b>", Xml.serialize(XPath.selectNode(doc,
145: "/a/b"), true, false));
146: }
147:
148: public void testSerializeText() throws Exception {
149: Document doc = parse("<a>foo</a>");
150: assertEquals("foo", Xml.serialize(XPath.selectNode(doc,
151: "/a/text()"), false, false));
152: }
153:
154: public void testSerializeComment() throws Exception {
155: Document doc = parse("<a><!-- hi --></a>");
156: assertEquals("<!-- hi -->", Xml.serialize(XPath.selectNode(doc,
157: "/a/comment()"), false, false));
158: }
159:
160: public void testSerializeAttribute() throws Exception {
161: Document doc = parse("<a b='foo'/>");
162: try {
163: Xml.serialize(XPath.selectNode(doc, "/a/@b"), false, false);
164: fail();
165: } catch (IllegalArgumentException e) {
166: // ok
167: }
168: }
169:
170: public void testSerializeEncoding() throws Exception {
171: final String STR = "<x>\u00c4</x>";
172: ByteArrayOutputStream dest;
173:
174: dest = new ByteArrayOutputStream();
175: Xml.serialize(parse(STR), dest, false, true);
176: checkEquals(
177: ("<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>" + STR)
178: .getBytes("ISO-8859-1"), dest.toByteArray());
179: }
180:
181: private void checkEquals(byte[] expected, byte[] got) {
182: int len;
183: int i;
184: StringBuffer msg;
185:
186: msg = new StringBuffer();
187: len = Math.min(expected.length, got.length);
188: if (expected.length != got.length) {
189: msg.append("length: expected " + expected.length + ", got "
190: + got.length + "\n");
191: }
192: for (i = 0; i < len; i++) {
193: if (expected[i] != got[i]) {
194: msg.append("ofs " + i + ": expected "
195: + ((int) (char) expected[i]) + ", got "
196: + ((int) (char) got[i]) + "\n");
197: }
198: }
199: if (msg.length() != 0) {
200: fail(new String(expected) + " vs " + new String(got) + ": "
201: + msg);
202: }
203: }
204:
205: public void testSerializeExplicitNamespace() throws Exception {
206: Document doc = parse(serialize("<ns:ok xmlns:ns='foo'/>",
207: false, false));
208: assertEquals("foo", doc.getDocumentElement().getNamespaceURI());
209: }
210:
211: public void testSerializeImplicitNamespace() throws Exception {
212: Document doc = parse(serialize("<ok xmlns='bar'/>", false,
213: false));
214: assertEquals("bar", doc.getDocumentElement().getNamespaceURI());
215: }
216:
217: public void testSerializeNoImplicitNamespaceDeclaration() {
218: // saxon does not represent ns decls as attributes
219: Document doc = Xml.createDocument();
220: doc.appendChild(doc.createElementNS("myuri", "ab:cd"));
221: assertEquals("<ab:cd xmlns:ab=\"myuri\"/>", Xml.serialize(doc,
222: true, false));
223: }
224:
225: //--
226:
227: public void testStrip() {
228: assertEquals("foo", Xml.stripElement("<a>foo</a>"));
229: assertEquals("foo", Xml.stripElement("<b bar='xy'>foo</b>"));
230: assertEquals("", Xml.stripElement("<c/>"));
231: }
232:
233: //-- helper code
234:
235: private static String serialize(String doc, boolean pp, boolean decl)
236: throws Exception {
237: return Xml.serialize(parse(doc), pp, decl);
238: }
239:
240: private static Document parse(String str) throws Exception {
241: return Xml.parseStringMutable(str);
242: }
243: }
|