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: */
019: package org.apache.axis2.jaxws.message;
020:
021: import java.io.ByteArrayOutputStream;
022: import java.io.StringReader;
023: import java.util.Locale;
024:
025: import javax.xml.stream.XMLInputFactory;
026: import javax.xml.stream.XMLStreamReader;
027: import javax.xml.transform.Source;
028: import javax.xml.transform.Transformer;
029: import javax.xml.transform.TransformerFactory;
030: import javax.xml.transform.stream.StreamResult;
031:
032: import junit.framework.TestCase;
033: import org.apache.axiom.om.OMElement;
034: import org.apache.axiom.soap.impl.builder.StAXSOAPModelBuilder;
035: import org.apache.axis2.jaxws.message.factory.BlockFactory;
036: import org.apache.axis2.jaxws.message.factory.MessageFactory;
037: import org.apache.axis2.jaxws.message.factory.SourceBlockFactory;
038: import org.apache.axis2.jaxws.registry.FactoryRegistry;
039:
040: /**
041: * MessageTests
042: * Tests to create and validate Message processing
043: * These are not client/server tests.
044: */
045: public class FaultTests extends TestCase {
046:
047: private static final String faultString = "Internal server error from WAS";
048:
049: // String test variables
050: private static final String sampleSOAP11FaultEnvelope1 = "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\">"
051: + "<soapenv:Body>"
052: + "<soapenv:Fault>"
053: + "<faultcode>soapenv:Server</faultcode>"
054: + "<faultstring>"
055: + faultString
056: + "sampleSOAP11FaultEnvelope1</faultstring>"
057: + "</soapenv:Fault>"
058: + "</soapenv:Body>"
059: + "</soapenv:Envelope>";
060:
061: private static final String sampleSOAP11FaultEnvelope2 = "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\""
062: + " xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\""
063: + " xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\""
064: + " xmlns:cwmp=\"http://cwmp.com\">"
065: + " <soapenv:Header>"
066: + " <cwmp:ID soapenv:mustUnderstand=\"1\">HEADERID-7867678</cwmp:ID>"
067: + " </soapenv:Header>"
068: + " <soapenv:Body>"
069: + " <soapenv:Fault>"
070: + " <faultcode>soapenv:Client</faultcode>"
071: + " <faultstring>"
072: + faultString
073: + "sampleSOAP11FaultEnvelope2</faultstring>"
074: + " <faultactor>http://gizmos.com/order</faultactor>"
075: + " <detail>"
076: + " <cwmp:Fault>"
077: + " <cwmp:FaultCode>This is the fault code</cwmp:FaultCode>"
078: + " <cwmp:FaultString>Fault Message</cwmp:FaultString>"
079: + " <cwmp:Message>This is a test fault</cwmp:Message>"
080: + " </cwmp:Fault>"
081: + " </detail>"
082: + /**/
083: " </soapenv:Fault>"
084: + " </soapenv:Body>"
085: + " </soapenv:Envelope>";
086:
087: private final static String sampleSOAP12FaultEnvelope1 =
088: //"<?xml version='1.0' encoding='UTF-8'?>"
089: "<env:Envelope xmlns:env=\"http://www.w3.org/2003/05/soap-envelope\">"
090: + "<env:Body>"
091: + "<env:Fault>"
092: + "<env:Code><env:Value>env:Receiver</env:Value></env:Code>"
093: + "<env:Reason><env:Text lang=\""
094: + Locale.getDefault().getLanguage() + "\">" + faultString
095: + "sampleSOAP12FaultEnvelope1</env:Text></env:Reason>"
096: + "</env:Fault>" + "</env:Body>" + "</env:Envelope>";
097:
098: // missing namespace for faultcode value
099: private final static String sampleSOAP12FaultEnvelope2 =
100: //"<?xml version='1.0' encoding='UTF-8'?>"
101: "<env:Envelope xmlns:env=\"http://www.w3.org/2003/05/soap-envelope\">"
102: + "<env:Body>" + "<env:Fault>"
103: + "<env:Code><env:Value>Sender</env:Value></env:Code>"
104: + "<env:Reason><env:Text lang=\""
105: + Locale.getDefault().getLanguage() + "\">" + faultString
106: + "sampleSOAP12FaultEnvelope2</env:Text></env:Reason>"
107: + "</env:Fault>" + "</env:Body>" + "</env:Envelope>";
108:
109: private static XMLInputFactory inputFactory = XMLInputFactory
110: .newInstance();
111:
112: public FaultTests() {
113: super ();
114: }
115:
116: public FaultTests(String arg0) {
117: super (arg0);
118: }
119:
120: /**
121: * This test effectively tests XMLFault construction from
122: *
123: * org.apache.axiom.soap.SOAPFault soapfault, List<Block> detailBlks
124: *
125: * which is a client-side operation. Also tests the "serialization" of the
126: * XMLFault object into a Message object which is a server-side operation.
127: *
128: * @throws Exception
129: */
130:
131: public void testStringInflow1() throws Exception {
132:
133: try {
134: // On inbound, there will already be an OM
135: // which represents the message. The following code simulates the input
136: // OM
137: StringReader sr = new StringReader(
138: sampleSOAP11FaultEnvelope1);
139: XMLStreamReader inflow = inputFactory
140: .createXMLStreamReader(sr);
141: StAXSOAPModelBuilder builder = new StAXSOAPModelBuilder(
142: inflow, null);
143: OMElement omElement = builder.getSOAPEnvelope();
144:
145: // The JAX-WS layer creates a Message from the OM
146: MessageFactory mf = (MessageFactory) FactoryRegistry
147: .getFactory(MessageFactory.class);
148: Message m = mf.createFrom(omElement, null);
149:
150: assertTrue(m.isFault());
151:
152: if (m.isFault()) {
153: XMLFault x = m.getXMLFault();
154: assertEquals(
155: faultString + "sampleSOAP11FaultEnvelope1", x
156: .getReason().getText());
157: assertEquals("Server", x.getCode().toQName(
158: "http://schemas.xmlsoap.org/soap/envelope/")
159: .getLocalPart());
160: } else {
161: fail("Message should be marked as a fault.");
162: }
163:
164: } catch (Exception e) {
165: e.printStackTrace();
166: fail(e.toString());
167: }
168:
169: }
170:
171: public void testStringInflow2() throws Exception {
172:
173: try {
174: // On inbound, there will already be an OM
175: // which represents the message. The following code simulates the
176: // input
177: // OM
178: StringReader sr = new StringReader(
179: sampleSOAP11FaultEnvelope2);
180: XMLStreamReader inflow = inputFactory
181: .createXMLStreamReader(sr);
182: StAXSOAPModelBuilder builder = new StAXSOAPModelBuilder(
183: inflow, null);
184: OMElement omElement = builder.getSOAPEnvelope();
185:
186: // The JAX-WS layer creates a Message from the OM
187: MessageFactory mf = (MessageFactory) FactoryRegistry
188: .getFactory(MessageFactory.class);
189: Message m = mf.createFrom(omElement, null);
190:
191: assertTrue(m.isFault());
192:
193: if (m.isFault()) {
194: XMLFault x = m.getXMLFault();
195: assertEquals(
196: faultString + "sampleSOAP11FaultEnvelope2", x
197: .getReason().getText());
198: assertEquals("Client", x.getCode().toQName(
199: "http://schemas.xmlsoap.org/soap/envelope/")
200: .getLocalPart());
201:
202: // drill down to the faultcode text in the detail to make sure it's there and it's set
203: Block[] blocks = x.getDetailBlocks();
204: Block block = blocks[0];
205: OMElement element = block.getOMElement();
206: OMElement child = (OMElement) element
207: .getChildElements().next();
208: String text = child.getText();
209:
210: assertEquals("This is the fault code", text);
211: } else {
212: fail("Message should be marked as a fault.");
213: }
214:
215: } catch (Exception e) {
216: e.printStackTrace();
217: fail(e.toString());
218: }
219:
220: }
221:
222: /**
223: * This test effectively tests XMLFault construction from
224: *
225: * org.apache.axiom.soap.SOAPFault soapfault, List<Block> detailBlks
226: *
227: * which is a client-side operation. Also tests the "serialization" of the
228: * XMLFault object into a Message object which is a server-side operation.
229: *
230: * @throws Exception
231: */
232:
233: public void testStringInflow3() throws Exception {
234:
235: try {
236: // On inbound, there will already be an OM
237: // which represents the message. The following code simulates the input
238: // OM
239: StringReader sr = new StringReader(
240: sampleSOAP12FaultEnvelope1);
241: XMLStreamReader inflow = inputFactory
242: .createXMLStreamReader(sr);
243: StAXSOAPModelBuilder builder = new StAXSOAPModelBuilder(
244: inflow, null);
245: OMElement omElement = builder.getSOAPEnvelope();
246:
247: // The JAX-WS layer creates a Message from the OM
248: MessageFactory mf = (MessageFactory) FactoryRegistry
249: .getFactory(MessageFactory.class);
250: Message m = mf.createFrom(omElement, null);
251:
252: assertTrue(m.isFault());
253:
254: if (m.isFault()) {
255: XMLFault x = m.getXMLFault();
256: assertEquals(
257: faultString + "sampleSOAP12FaultEnvelope1", x
258: .getReason().getText());
259: assertEquals("Receiver", x.getCode().toQName(
260: "http://www.w3.org/2003/05/soap-envelope")
261: .getLocalPart());
262: } else {
263: fail("Message should be marked as a fault.");
264: }
265:
266: } catch (Exception e) {
267: e.printStackTrace();
268: fail(e.toString());
269: }
270: }
271:
272: public void testStringInflow4() throws Exception {
273:
274: try {
275: // On inbound, there will already be an OM
276: // which represents the message. The following code simulates the input
277: // OM
278: StringReader sr = new StringReader(
279: sampleSOAP12FaultEnvelope2);
280: XMLStreamReader inflow = inputFactory
281: .createXMLStreamReader(sr);
282: StAXSOAPModelBuilder builder = new StAXSOAPModelBuilder(
283: inflow, null);
284: OMElement omElement = builder.getSOAPEnvelope();
285:
286: // The JAX-WS layer creates a Message from the OM
287: MessageFactory mf = (MessageFactory) FactoryRegistry
288: .getFactory(MessageFactory.class);
289: Message m = mf.createFrom(omElement, null);
290:
291: assertTrue(m.isFault());
292:
293: if (m.isFault()) {
294: XMLFault x = m.getXMLFault();
295: assertEquals(
296: faultString + "sampleSOAP12FaultEnvelope2", x
297: .getReason().getText());
298: assertEquals("Sender", x.getCode().toQName(
299: "http://www.w3.org/2003/05/soap-envelope")
300: .getLocalPart());
301: } else {
302: fail("Message should be marked as a fault.");
303: }
304:
305: } catch (Exception e) {
306: e.printStackTrace();
307: fail(e.toString());
308: }
309: }
310:
311: public void testGetSOAP11XMLFaultAsOM() throws Exception {
312: MessageFactory factory = (MessageFactory) FactoryRegistry
313: .getFactory(MessageFactory.class);
314: Message msg = factory.create(Protocol.soap11);
315:
316: XMLFaultReason reason = new XMLFaultReason(
317: "sample fault reason");
318: XMLFault fault = new XMLFault(XMLFaultCode.SENDER, reason);
319: msg.setXMLFault(fault);
320:
321: OMElement om = msg.getAsOMElement();
322: ByteArrayOutputStream baos = new ByteArrayOutputStream();
323: om.serializeAndConsume(baos);
324:
325: String env = new String(baos.toByteArray());
326: assertTrue(env.indexOf("faultcode") > 0);
327: assertTrue(env.indexOf("faultstring") > 0);
328: }
329:
330: public void testGetSOAP11XMLFaultAsBlock() throws Exception {
331: MessageFactory factory = (MessageFactory) FactoryRegistry
332: .getFactory(MessageFactory.class);
333: Message msg = factory.create(Protocol.soap11);
334:
335: XMLFaultReason reason = new XMLFaultReason(
336: "sample fault reason");
337: XMLFault fault = new XMLFault(XMLFaultCode.SENDER, reason);
338: msg.setXMLFault(fault);
339:
340: BlockFactory bf = (BlockFactory) FactoryRegistry
341: .getFactory(SourceBlockFactory.class);
342: Block b = msg.getBodyBlock(null, bf);
343:
344: Source content = (Source) b.getBusinessObject(true);
345: byte[] bytes = _getBytes(content);
346: String faultContent = new String(bytes);
347:
348: System.out.println(">> fault content: " + faultContent);
349: assertTrue(faultContent.indexOf("faultcode") > 0);
350: assertTrue(faultContent.indexOf("faultstring") > 0);
351: }
352:
353: private byte[] _getBytes(Source input) throws Exception {
354: TransformerFactory tf = TransformerFactory.newInstance();
355: Transformer t = tf.newTransformer();
356:
357: ByteArrayOutputStream baos = new ByteArrayOutputStream();
358: StreamResult output = new StreamResult(baos);
359:
360: t.transform(input, output);
361:
362: return baos.toByteArray();
363: }
364:
365: }
|