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.transform;
18:
19: import java.io.StringReader;
20: import javax.xml.stream.XMLEventReader;
21: import javax.xml.stream.XMLInputFactory;
22: import javax.xml.stream.XMLStreamReader;
23: import javax.xml.transform.Result;
24: import javax.xml.transform.Transformer;
25: import javax.xml.transform.TransformerFactory;
26:
27: import org.custommonkey.xmlunit.XMLTestCase;
28:
29: public class StaxSourceTest extends XMLTestCase {
30:
31: private static final String XML = "<root xmlns='namespace'><child/></root>";
32:
33: private Transformer transformer;
34:
35: private XMLInputFactory inputFactory;
36:
37: protected void setUp() throws Exception {
38: TransformerFactory transformerFactory = TransformerFactory
39: .newInstance();
40: transformer = transformerFactory.newTransformer();
41: inputFactory = XMLInputFactory.newInstance();
42: }
43:
44: public void testStreamReaderSource() throws Exception {
45: XMLStreamReader streamReader = inputFactory
46: .createXMLStreamReader(new StringReader(XML));
47: StaxSource source = new StaxSource(streamReader);
48: assertEquals("Invalid streamReader returned", streamReader,
49: source.getXMLStreamReader());
50: assertNull("EventReader returned", source.getXMLEventReader());
51: Result result = new StringResult();
52: transformer.transform(source, result);
53: assertXMLEqual("Invalid result", XML, result.toString());
54: }
55:
56: public void testEventReaderSource() throws Exception {
57: XMLEventReader eventReader = inputFactory
58: .createXMLEventReader(new StringReader(XML));
59: StaxSource source = new StaxSource(eventReader);
60: assertEquals("Invalid eventReader returned", eventReader,
61: source.getXMLEventReader());
62: assertNull("StreamReader returned", source.getXMLStreamReader());
63: Result result = new StringResult();
64: transformer.transform(source, result);
65: assertXMLEqual("Invalid result", XML, result.toString());
66: }
67: }
|