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.binding.xml.interceptor;
019:
020: import java.io.ByteArrayInputStream;
021: import java.io.ByteArrayOutputStream;
022: import java.io.InputStream;
023: import java.io.OutputStream;
024:
025: import javax.xml.bind.JAXBContext;
026: import javax.xml.bind.Marshaller;
027: import javax.xml.stream.XMLStreamReader;
028: import javax.xml.stream.XMLStreamWriter;
029:
030: import org.w3c.dom.Element;
031:
032: import org.apache.cxf.binding.xml.XMLConstants;
033: import org.apache.cxf.binding.xml.XMLFault;
034: import org.apache.cxf.interceptor.Fault;
035: import org.apache.cxf.staxutils.DepthXMLStreamReader;
036: import org.apache.cxf.staxutils.StaxUtils;
037: import org.apache.hello_world_doc_lit.PingMeFault;
038: import org.apache.hello_world_doc_lit.types.FaultDetail;
039: import org.junit.Before;
040: import org.junit.Test;
041:
042: public class XMLFaultOutInterceptorTest extends TestBase {
043:
044: XMLFaultOutInterceptor out = new XMLFaultOutInterceptor();
045:
046: @Before
047: public void setUp() throws Exception {
048: super .setUp();
049: }
050:
051: @Test
052: public void testFault() throws Exception {
053: FaultDetail detail = new FaultDetail();
054: detail.setMajor((short) 2);
055: detail.setMinor((short) 1);
056: PingMeFault fault = new PingMeFault("TEST_FAULT", detail);
057:
058: XMLFault xmlFault = XMLFault.createFault(new Fault(fault));
059: Element el = xmlFault.getOrCreateDetail();
060: JAXBContext ctx = JAXBContext.newInstance(FaultDetail.class);
061: Marshaller m = ctx.createMarshaller();
062: m.marshal(detail, el);
063:
064: OutputStream outputStream = new ByteArrayOutputStream();
065: xmlMessage.setContent(OutputStream.class, outputStream);
066: XMLStreamWriter writer = StaxUtils
067: .createXMLStreamWriter(outputStream);
068: xmlMessage.setContent(XMLStreamWriter.class, writer);
069: xmlMessage.setContent(Exception.class, xmlFault);
070:
071: out.handleMessage(xmlMessage);
072: outputStream.flush();
073:
074: XMLStreamReader reader = getXMLReader();
075: DepthXMLStreamReader dxr = new DepthXMLStreamReader(reader);
076:
077: dxr.nextTag();
078: StaxUtils.toNextElement(dxr);
079: assertEquals(XMLConstants.NS_XML_FORMAT, dxr.getNamespaceURI());
080: assertEquals(XMLFault.XML_FAULT_ROOT, dxr.getLocalName());
081:
082: dxr.nextTag();
083: StaxUtils.toNextElement(dxr);
084: assertEquals(XMLFault.XML_FAULT_STRING, dxr.getLocalName());
085: assertEquals(fault.toString(), dxr.getElementText());
086:
087: dxr.nextTag();
088: StaxUtils.toNextElement(dxr);
089: assertEquals(XMLFault.XML_FAULT_DETAIL, dxr.getLocalName());
090:
091: dxr.nextTag();
092: StaxUtils.toNextElement(dxr);
093: assertEquals("faultDetail", dxr.getLocalName());
094: }
095:
096: private XMLStreamReader getXMLReader() throws Exception {
097: ByteArrayOutputStream o = (ByteArrayOutputStream) xmlMessage
098: .getContent(OutputStream.class);
099: InputStream in = new ByteArrayInputStream(o.toByteArray());
100: return StaxUtils.createXMLStreamReader(in);
101: }
102: }
|