01: /*
02: * Copyright 2006 the original author or authors.
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
10: * Unless required by applicable law or agreed to in writing, software
11: * distributed under the License is distributed on an "AS IS" BASIS,
12: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13: * See the License for the specific language governing permissions and
14: * limitations under the License.
15: */
16:
17: package org.springframework.xml.dom;
18:
19: import java.io.StringReader;
20: import javax.xml.parsers.DocumentBuilder;
21: import javax.xml.parsers.DocumentBuilderFactory;
22:
23: import org.custommonkey.xmlunit.XMLTestCase;
24: import org.w3c.dom.Document;
25: import org.w3c.dom.Element;
26: import org.xml.sax.InputSource;
27: import org.xml.sax.XMLReader;
28: import org.xml.sax.helpers.XMLReaderFactory;
29:
30: public class DomContentHandlerTest extends XMLTestCase {
31:
32: private static final String XML_1 = "<?xml version='1.0' encoding='UTF-8'?>"
33: + "<?pi content?>"
34: + "<root xmlns='namespace'>"
35: + "<prefix:child xmlns:prefix='namespace2' xmlns:prefix2='namespace3' prefix2:attr='value'>content</prefix:child>"
36: + "</root>";
37:
38: private static final String XML_2_EXPECTED = "<?xml version='1.0' encoding='UTF-8'?>"
39: + "<root xmlns='namespace'>"
40: + "<child xmlns='namespace2' />" + "</root>";
41:
42: private static final String XML_2_SNIPPET = "<?xml version='1.0' encoding='UTF-8'?>"
43: + "<child xmlns='namespace2' />";
44:
45: private Document expected;
46:
47: private DomContentHandler handler;
48:
49: private Document result;
50:
51: private XMLReader xmlReader;
52:
53: private DocumentBuilder documentBuilder;
54:
55: protected void setUp() throws Exception {
56: xmlReader = XMLReaderFactory.createXMLReader();
57: DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory
58: .newInstance();
59: documentBuilderFactory.setNamespaceAware(true);
60: documentBuilder = documentBuilderFactory.newDocumentBuilder();
61: result = documentBuilder.newDocument();
62: xmlReader
63: .setFeature(
64: "http://xml.org/sax/features/namespace-prefixes",
65: false);
66: }
67:
68: public void testContentHandlerDocument() throws Exception {
69: handler = new DomContentHandler(result);
70: expected = documentBuilder.parse(new InputSource(
71: new StringReader(XML_1)));
72: xmlReader.setContentHandler(handler);
73: xmlReader.parse(new InputSource(new StringReader(XML_1)));
74: assertXMLEqual("Invalid result", expected, result);
75: }
76:
77: public void testContentHandlerElement() throws Exception {
78: Element rootElement = result.createElementNS("namespace",
79: "root");
80: result.appendChild(rootElement);
81: handler = new DomContentHandler(rootElement);
82: expected = documentBuilder.parse(new InputSource(
83: new StringReader(XML_2_EXPECTED)));
84: xmlReader.setContentHandler(handler);
85: xmlReader
86: .parse(new InputSource(new StringReader(XML_2_SNIPPET)));
87: assertXMLEqual("Invalid result", expected, result);
88:
89: }
90: }
|