001: /**
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: */package org.apache.cxf.staxutils;
019:
020: import java.io.*;
021:
022: import javax.xml.namespace.QName;
023: import javax.xml.parsers.DocumentBuilderFactory;
024: import javax.xml.stream.XMLStreamReader;
025: import javax.xml.stream.XMLStreamWriter;
026: import javax.xml.transform.Source;
027: import javax.xml.transform.dom.DOMSource;
028:
029: import org.w3c.dom.Document;
030:
031: import org.xml.sax.InputSource;
032:
033: import org.junit.Assert;
034: import org.junit.Test;
035:
036: public class StaxUtilsTest extends Assert {
037:
038: @Test
039: public void testFactoryCreation() {
040: XMLStreamReader reader = StaxUtils
041: .createXMLStreamReader(getTestStream("./resources/amazon.xml"));
042: assertTrue(reader != null);
043: }
044:
045: private InputStream getTestStream(String resource) {
046: return getClass().getResourceAsStream(resource);
047: }
048:
049: @Test
050: public void testToNextElement() {
051: String soapMessage = "./resources/sayHiRpcLiteralReq.xml";
052: XMLStreamReader r = StaxUtils
053: .createXMLStreamReader(getTestStream(soapMessage));
054: DepthXMLStreamReader reader = new DepthXMLStreamReader(r);
055: assertTrue(StaxUtils.toNextElement(reader));
056: assertEquals("Envelope", reader.getLocalName());
057:
058: StaxUtils.nextEvent(reader);
059:
060: assertTrue(StaxUtils.toNextElement(reader));
061: assertEquals("Body", reader.getLocalName());
062: }
063:
064: @Test
065: public void testToNextTag() throws Exception {
066: String soapMessage = "./resources/headerSoapReq.xml";
067: XMLStreamReader r = StaxUtils
068: .createXMLStreamReader(getTestStream(soapMessage));
069: DepthXMLStreamReader reader = new DepthXMLStreamReader(r);
070: reader.nextTag();
071: StaxUtils.toNextTag(reader, new QName(
072: "http://schemas.xmlsoap.org/soap/envelope/", "Body"));
073: assertEquals("Body", reader.getLocalName());
074: }
075:
076: @Test
077: public void testCopy() throws Exception {
078:
079: // do the stream copying
080: String soapMessage = "./resources/headerSoapReq.xml";
081: ByteArrayOutputStream baos = new ByteArrayOutputStream();
082: XMLStreamReader reader = StaxUtils
083: .createXMLStreamReader(getTestStream(soapMessage));
084: XMLStreamWriter writer = StaxUtils.createXMLStreamWriter(baos);
085: StaxUtils.copy(reader, writer);
086: writer.flush();
087: baos.flush();
088:
089: // write output to a string
090: String output = baos.toString();
091:
092: // re-read the input xml doc to a string
093: InputStreamReader inputStreamReader = new InputStreamReader(
094: getTestStream(soapMessage));
095: StringWriter stringWriter = new StringWriter();
096: char[] buffer = new char[4096];
097: int n = 0;
098: n = inputStreamReader.read(buffer);
099: while (n > 0) {
100: stringWriter.write(buffer, 0, n);
101: n = inputStreamReader.read(buffer);
102: }
103: String input = stringWriter.toString();
104: // seach for the first begin of "<soap:Envelope" to escape the apache licenses header
105: int beginIndex = input.indexOf("<soap:Envelope");
106: input = input.substring(beginIndex);
107: output = output.replaceAll("\r\n", "\n");
108: input = input.replaceAll("\r\n", "\n");
109: // compare the input and output string
110: assertEquals(input, output);
111: }
112:
113: @Test
114: public void testNonNamespaceAwareParser() throws Exception {
115: String xml = "<blah xmlns=\"http://blah.org/\" xmlns:snarf=\"http://snarf.org\">"
116: + "<foo snarf:blop=\"blop\">foo</foo></blah>";
117:
118: StringReader reader = new StringReader(xml);
119: DocumentBuilderFactory dbf = DocumentBuilderFactory
120: .newInstance();
121: dbf.setNamespaceAware(false);
122: dbf.setValidating(false);
123: Document doc = dbf.newDocumentBuilder().parse(
124: new InputSource(reader));
125: Source source = new DOMSource(doc);
126:
127: dbf.setNamespaceAware(true);
128: reader = new StringReader(xml);
129: Document docNs = dbf.newDocumentBuilder().parse(
130: new InputSource(reader));
131: Source sourceNs = new DOMSource(docNs);
132:
133: XMLStreamReader sreader = StaxUtils
134: .createXMLStreamReader(source);
135:
136: StringWriter sw = new StringWriter();
137: XMLStreamWriter swriter = StaxUtils.createXMLStreamWriter(sw);
138:
139: //should not throw an exception
140: StaxUtils.copy(sreader, swriter);
141: swriter.flush();
142: swriter.close();
143:
144: String output = sw.toString();
145: assertTrue(output.contains("blah"));
146: assertTrue(output.contains("foo"));
147: assertTrue(output.contains("snarf"));
148: assertTrue(output.contains("blop"));
149:
150: sreader = StaxUtils.createXMLStreamReader(sourceNs);
151: sw = new StringWriter();
152: swriter = StaxUtils.createXMLStreamWriter(sw);
153: //should not throw an exception
154: StaxUtils.copy(sreader, swriter);
155: swriter.flush();
156: swriter.close();
157:
158: output = sw.toString();
159: assertTrue(output.contains("blah"));
160: assertTrue(output.contains("foo"));
161: assertTrue(output.contains("snarf"));
162: assertTrue(output.contains("blop"));
163:
164: sreader = StaxUtils.createXMLStreamReader(source);
165:
166: ByteArrayOutputStream bout = new ByteArrayOutputStream();
167: swriter = StaxUtils.createXMLStreamWriter(bout);
168: StaxUtils.copy(sreader, swriter);
169: swriter.flush();
170: swriter.close();
171:
172: output = bout.toString();
173: assertTrue(output.contains("blah"));
174: assertTrue(output.contains("foo"));
175: assertTrue(output.contains("snarf"));
176: assertTrue(output.contains("blop"));
177: }
178: }
|