01: /**
02: * Licensed to the Apache Software Foundation (ASF) under one
03: * or more contributor license agreements. See the NOTICE file
04: * distributed with this work for additional information
05: * regarding copyright ownership. The ASF licenses this file
06: * to you under the Apache License, Version 2.0 (the
07: * "License"); you may not use this file except in compliance
08: * with the License. You may obtain a copy of the License at
09: *
10: * http://www.apache.org/licenses/LICENSE-2.0
11: *
12: * Unless required by applicable law or agreed to in writing,
13: * software distributed under the License is distributed on an
14: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15: * KIND, either express or implied. See the License for the
16: * specific language governing permissions and limitations
17: * under the License.
18: */package org.apache.cxf.binding.xml.interceptor;
19:
20: import java.io.ByteArrayInputStream;
21: import java.io.ByteArrayOutputStream;
22: import java.io.InputStream;
23: import java.io.OutputStream;
24:
25: import javax.xml.namespace.QName;
26: import javax.xml.stream.XMLStreamReader;
27: import javax.xml.stream.XMLStreamWriter;
28:
29: import org.apache.cxf.binding.xml.XMLFault;
30: import org.apache.cxf.interceptor.AbstractInDatabindingInterceptor;
31: import org.apache.cxf.interceptor.ClientFaultConverter;
32: import org.apache.cxf.interceptor.Fault;
33: import org.apache.cxf.message.Message;
34: import org.apache.cxf.staxutils.StaxUtils;
35: import org.apache.hello_world_xml_http.bare.types.MyComplexStructType;
36: import org.junit.Test;
37:
38: public class XMLFaultInterceptorsTest extends TestBase {
39:
40: @Test
41: public void testRuntimeExceptionOfImpl() throws Exception {
42:
43: String ns = "http://apache.org/hello_world_xml_http/wrapped";
44: common("/wsdl/hello_world_xml_wrapped.wsdl", new QName(ns,
45: "XMLPort"), MyComplexStructType.class);
46:
47: ByteArrayOutputStream baos = new ByteArrayOutputStream();
48: xmlMessage.setContent(OutputStream.class, baos);
49: xmlMessage.setContent(XMLStreamWriter.class, StaxUtils
50: .createXMLStreamWriter(baos));
51: xmlMessage.setContent(Exception.class, new Fault(
52: new RuntimeException("dummy exception")));
53: XMLFaultOutInterceptor xfo = new XMLFaultOutInterceptor(
54: "phase1");
55: chain.add(xfo);
56: InHelpInterceptor ih = new InHelpInterceptor("phase2");
57: ClientFaultConverter cfc = new ClientFaultConverter("phase3");
58: XMLFaultInInterceptor xfi = new XMLFaultInInterceptor("phase3");
59: chain.add(ih);
60: chain.add(cfc);
61: chain.add(xfi);
62: chain.doIntercept(xmlMessage);
63: assertNotNull(xmlMessage.getContent(Exception.class));
64: assertTrue(xmlMessage.getContent(Exception.class) instanceof XMLFault);
65: XMLFault xfault = (XMLFault) xmlMessage
66: .getContent(Exception.class);
67: assertTrue("check message expected - dummy exception", xfault
68: .getMessage().indexOf("dummy exception") >= 0);
69: }
70:
71: private class InHelpInterceptor extends
72: AbstractInDatabindingInterceptor {
73: InHelpInterceptor(String phase) {
74: super (phase);
75: }
76:
77: public void handleMessage(Message message) {
78:
79: try {
80: ByteArrayOutputStream baos = (ByteArrayOutputStream) message
81: .getContent(OutputStream.class);
82: ByteArrayInputStream bis = new ByteArrayInputStream(
83: baos.toByteArray());
84: message.setContent(InputStream.class, bis);
85: XMLStreamReader xsr = StaxUtils
86: .createXMLStreamReader(bis);
87: xsr.nextTag();
88: message.setContent(XMLStreamReader.class, xsr);
89: } catch (Exception e) {
90: throw new RuntimeException(e);
91: }
92:
93: }
94:
95: }
96: }
|