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.jbi.interceptor;
019:
020: import java.io.ByteArrayInputStream;
021: import java.io.ByteArrayOutputStream;
022: import java.util.ResourceBundle;
023:
024: import javax.xml.stream.XMLOutputFactory;
025: import javax.xml.stream.XMLStreamWriter;
026:
027: import org.w3c.dom.Document;
028:
029: import org.apache.cxf.binding.jbi.JBIMessage;
030: import org.apache.cxf.common.i18n.Message;
031: import org.apache.cxf.helpers.DOMUtils;
032: import org.apache.cxf.interceptor.Fault;
033: import org.apache.cxf.message.MessageImpl;
034: import org.apache.cxf.phase.Phase;
035: import org.apache.cxf.phase.PhaseInterceptor;
036: import org.junit.Assert;
037: import org.junit.Test;
038:
039: public class JBIFaultOutInterceptorTest extends Assert {
040:
041: @Test
042: public void testPhase() throws Exception {
043: PhaseInterceptor<JBIMessage> interceptor = new JBIFaultOutInterceptor();
044: assertEquals(Phase.MARSHAL, interceptor.getPhase());
045: }
046:
047: @Test
048: public void testNoWriter() throws Exception {
049: PhaseInterceptor<JBIMessage> interceptor = new JBIFaultOutInterceptor();
050: try {
051: JBIMessage msg = new JBIMessage(new MessageImpl());
052: interceptor.handleMessage(msg);
053: fail("Should have thrown an exception");
054: } catch (IllegalStateException e) {
055: // ok
056: }
057: }
058:
059: @Test
060: public void testNoFault() throws Exception {
061: PhaseInterceptor<JBIMessage> interceptor = new JBIFaultOutInterceptor();
062: try {
063: JBIMessage msg = new JBIMessage(new MessageImpl());
064: msg.setContent(XMLStreamWriter.class, XMLOutputFactory
065: .newInstance().createXMLStreamWriter(
066: new ByteArrayOutputStream()));
067: interceptor.handleMessage(msg);
068: fail("Should have thrown an exception");
069: } catch (IllegalStateException e) {
070: // ok
071: }
072: }
073:
074: @Test
075: public void testEmptyFault() throws Exception {
076: PhaseInterceptor<JBIMessage> interceptor = new JBIFaultOutInterceptor();
077: ByteArrayOutputStream baos = new ByteArrayOutputStream();
078: XMLStreamWriter writer = XMLOutputFactory.newInstance()
079: .createXMLStreamWriter(baos);
080: JBIMessage msg = new JBIMessage(new MessageImpl());
081: msg.setContent(XMLStreamWriter.class, writer);
082: msg.setContent(Exception.class, new Exception("My fault"));
083: interceptor.handleMessage(msg);
084: writer.close();
085: Document doc = DOMUtils.readXml(new ByteArrayInputStream(baos
086: .toByteArray()));
087: assertEquals("fault", doc.getDocumentElement().getFirstChild()
088: .getNodeName());
089: }
090:
091: @Test
092: public void testDetailedFault() throws Exception {
093: PhaseInterceptor<JBIMessage> interceptor = new JBIFaultOutInterceptor();
094: ByteArrayOutputStream baos = new ByteArrayOutputStream();
095: XMLStreamWriter writer = XMLOutputFactory.newInstance()
096: .createXMLStreamWriter(baos);
097: JBIMessage msg = new JBIMessage(new MessageImpl());
098: Fault f = new Fault(new Message("FAULT", (ResourceBundle) null));
099: f.getOrCreateDetail();
100: f.getDetail().appendChild(
101: f.getDetail().getOwnerDocument().createElementNS(
102: "urn:test", "myDetails"));
103: msg.setContent(XMLStreamWriter.class, writer);
104: msg.setContent(Exception.class, f);
105: interceptor.handleMessage(msg);
106: writer.close();
107: Document doc = DOMUtils.readXml(new ByteArrayInputStream(baos
108: .toByteArray()));
109: assertEquals("urn:test", doc.getDocumentElement()
110: .getFirstChild().getNamespaceURI());
111: assertEquals("myDetails", doc.getDocumentElement()
112: .getFirstChild().getNodeName());
113: }
114:
115: }
|