blob: 6f64b5efc4044d257abbe51c5a769f01032299f2 [file] [log] [blame]
/*
* Copyright 2004,2005 The Apache Software Foundation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.axiom.om.impl.serializer;
import org.apache.axiom.om.AbstractTestCase;
import org.apache.axiom.om.OMAbstractFactory;
import org.apache.axiom.om.OMXMLParserWrapper;
import org.apache.axiom.om.impl.llom.factory.OMXMLBuilderFactory;
import org.apache.axiom.om.impl.serialize.StreamingOMSerializer;
import org.apache.axiom.soap.SOAPBody;
import org.apache.axiom.soap.SOAPEnvelope;
import org.apache.axiom.soap.impl.builder.StAXSOAPModelBuilder;
import javax.xml.stream.*;
import java.io.*;
public class OMSerializerTest extends AbstractTestCase {
private XMLStreamReader reader;
private XMLStreamWriter writer;
private File tempFile;
public OMSerializerTest(String testName) {
super(testName);
}
protected void setUp() throws Exception {
reader =
XMLInputFactory.newInstance().
createXMLStreamReader(
new FileReader(
getTestResourceFile("soap/soapmessage.xml")));
tempFile = File.createTempFile("temp", "xml");
// writer =
// XMLOutputFactory.newInstance().
// createXMLStreamWriter(new FileOutputStream(tempFile));
}
public void testRawSerializer() throws Exception {
StreamingOMSerializer serializer = new StreamingOMSerializer();
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
writer =
XMLOutputFactory.newInstance().
createXMLStreamWriter(byteArrayOutputStream);
//serializer.setNamespacePrefixStack(new Stack());
serializer.serialize(reader, writer);
writer.flush();
String outputString = new String(byteArrayOutputStream.toByteArray());
assertTrue(outputString != null && !"".equals(outputString) && outputString.length() > 1);
}
public void testElementPullStream1() throws Exception {
OMXMLParserWrapper builder = OMXMLBuilderFactory.createStAXSOAPModelBuilder(
OMAbstractFactory.getSOAP11Factory(),
reader);
SOAPEnvelope env = (SOAPEnvelope) builder.getDocumentElement();
StreamingOMSerializer serializer = new StreamingOMSerializer();
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
writer =
XMLOutputFactory.newInstance().
createXMLStreamWriter(byteArrayOutputStream);
serializer.serialize(env.getXMLStreamReaderWithoutCaching(), writer);
writer.flush();
String outputString = new String(byteArrayOutputStream.toByteArray());
assertTrue(outputString != null && !"".equals(outputString) && outputString.length() > 1);
}
public void testElementPullStream1WithCacheOff() throws Exception {
StAXSOAPModelBuilder soapBuilder = new StAXSOAPModelBuilder(reader, null);
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
writer =
XMLOutputFactory.newInstance().
createXMLStreamWriter(byteArrayOutputStream);
SOAPEnvelope env = (SOAPEnvelope) soapBuilder.getDocumentElement();
env.serializeAndConsume(writer);
writer.flush();
String outputString = new String(byteArrayOutputStream.toByteArray());
assertTrue(outputString != null && !"".equals(outputString) && outputString.length() > 1);
writer =
XMLOutputFactory.newInstance().
createXMLStreamWriter(byteArrayOutputStream);
StringWriter stringWriter = new StringWriter();
//now we should not be able to serilaize anything ! this should throw
//an error
try {
env.serializeAndConsume(writer);
fail();
} catch (XMLStreamException e) {
e.printStackTrace(new PrintWriter(stringWriter, true));
assertTrue(stringWriter.toString().indexOf("problem accessing the parser. Parser already accessed!") > -1);
} catch (Exception e) {
fail("Expecting an XMLStreamException " + e.getMessage());
}
}
public void testElementPullStream2() throws Exception {
OMXMLParserWrapper builder = OMXMLBuilderFactory.createStAXSOAPModelBuilder(
OMAbstractFactory.getSOAP11Factory(),
reader);
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
writer =
XMLOutputFactory.newInstance().
createXMLStreamWriter(byteArrayOutputStream);
SOAPEnvelope env = (SOAPEnvelope) builder.getDocumentElement();
SOAPBody body = env.getBody();
StreamingOMSerializer serializer = new StreamingOMSerializer();
serializer.serialize(body.getXMLStreamReaderWithoutCaching(),
writer);
writer.flush();
String outputString = new String(byteArrayOutputStream.toByteArray());
assertTrue(outputString != null && !"".equals(outputString) && outputString.length() > 1);
}
protected void tearDown() throws Exception {
tempFile.delete();
}
}