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.stream;
18:
19: import java.io.StringReader;
20: import java.io.StringWriter;
21: import java.io.Writer;
22: import javax.xml.stream.XMLStreamException;
23:
24: import org.custommonkey.xmlunit.XMLTestCase;
25: import org.xml.sax.InputSource;
26: import org.xml.sax.XMLReader;
27: import org.xml.sax.helpers.XMLReaderFactory;
28:
29: public abstract class AbstractStaxContentHandlerTestCase extends
30: XMLTestCase {
31:
32: private static final String XML_CONTENT_HANDLER = "<?xml version='1.0' encoding='UTF-8'?><?pi content?><root xmlns='namespace'><prefix:child xmlns:prefix='namespace2' prefix:attr='value'>content</prefix:child></root>";
33:
34: private XMLReader xmlReader;
35:
36: protected void setUp() throws Exception {
37: xmlReader = XMLReaderFactory.createXMLReader();
38: }
39:
40: public void testContentHandler() throws Exception {
41: StringWriter stringWriter = new StringWriter();
42: AbstractStaxContentHandler handler = createStaxContentHandler(stringWriter);
43: xmlReader
44: .setFeature(
45: "http://xml.org/sax/features/namespace-prefixes",
46: false);
47: xmlReader.setContentHandler(handler);
48: xmlReader.parse(new InputSource(new StringReader(
49: XML_CONTENT_HANDLER)));
50: assertXMLEqual("Invalid result", XML_CONTENT_HANDLER,
51: stringWriter.toString());
52: }
53:
54: public void testContentHandlerNamespacePrefixes() throws Exception {
55: StringWriter stringWriter = new StringWriter();
56: AbstractStaxContentHandler handler = createStaxContentHandler(stringWriter);
57: xmlReader.setFeature(
58: "http://xml.org/sax/features/namespace-prefixes", true);
59: xmlReader.setContentHandler(handler);
60: xmlReader.parse(new InputSource(new StringReader(
61: XML_CONTENT_HANDLER)));
62: assertXMLEqual("Invalid result", XML_CONTENT_HANDLER,
63: stringWriter.toString());
64: }
65:
66: protected abstract AbstractStaxContentHandler createStaxContentHandler(
67: Writer writer) throws XMLStreamException;
68:
69: }
|