KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > servicemix > soap > marshalers > FaultTest


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17 package org.apache.servicemix.soap.marshalers;
18
19 import java.io.ByteArrayOutputStream JavaDoc;
20
21 import javax.xml.namespace.QName JavaDoc;
22
23 import junit.framework.TestCase;
24
25 import org.apache.servicemix.jbi.jaxp.SourceTransformer;
26 import org.apache.servicemix.jbi.jaxp.StringSource;
27 import org.apache.servicemix.jbi.util.DOMUtil;
28 import org.apache.servicemix.soap.SoapFault;
29 import org.w3c.dom.Document JavaDoc;
30 import org.w3c.dom.Element JavaDoc;
31 import org.w3c.dom.Node JavaDoc;
32
33 public class FaultTest extends TestCase {
34
35     private SourceTransformer sourceTransformer = new SourceTransformer();
36     
37     protected void testSoap11(boolean useDom) throws Exception JavaDoc {
38         SoapMarshaler marshaler = new SoapMarshaler(true, useDom);
39         try {
40             marshaler.createReader().read(getClass().getResourceAsStream("fault-1.1-ok.xml"));
41         } catch (SoapFault fault) {
42             assertEquals(SoapMarshaler.SOAP_11_CODE_SERVER, fault.getCode());
43             assertNull(fault.getSubcode());
44             assertEquals("Server Error", fault.getReason());
45             assertNotNull(fault.getDetails());
46             Node JavaDoc node = sourceTransformer.toDOMNode(fault.getDetails());
47             Element JavaDoc e = node instanceof Document JavaDoc ? ((Document JavaDoc) node).getDocumentElement() : (Element JavaDoc) node;
48             assertEquals(new QName JavaDoc("Some-URI", "myfaultdetails"), DOMUtil.getQName(e));
49         }
50     }
51     
52     public void testReadSoap11UsingDom() throws Exception JavaDoc {
53         testSoap11(true);
54     }
55     
56     public void testReadSoap11UsingStax() throws Exception JavaDoc {
57         testSoap11(false);
58     }
59     
60     protected void testSoap12(boolean useDom) throws Exception JavaDoc {
61         SoapMarshaler marshaler = new SoapMarshaler(true, useDom);
62         try {
63             marshaler.createReader().read(getClass().getResourceAsStream("fault-1.2-ok.xml"));
64         } catch (SoapFault fault) {
65             assertEquals(SoapMarshaler.SOAP_12_CODE_SENDER, fault.getCode());
66             assertEquals(new QName JavaDoc("http://www.example.org/timeouts", "MessageTimeout"), fault.getSubcode());
67             assertEquals("Sender Timeout", fault.getReason());
68             assertNotNull(fault.getDetails());
69             Node JavaDoc node = sourceTransformer.toDOMNode(fault.getDetails());
70             Element JavaDoc e = node instanceof Document JavaDoc ? ((Document JavaDoc) node).getDocumentElement() : (Element JavaDoc) node;
71             assertEquals(new QName JavaDoc("http://www.example.org/timeouts", "MaxTime"), DOMUtil.getQName(e));
72         }
73     }
74     
75     public void testReadSoap12UsingDom() throws Exception JavaDoc {
76         testSoap12(true);
77     }
78     
79     public void testReadSoap12UsingStax() throws Exception JavaDoc {
80         testSoap12(false);
81     }
82     
83     public void testWriteSoap11UsingDom() throws Exception JavaDoc {
84         testWriteSoap11(true);
85     }
86
87     public void testWriteSoap11UsingStax() throws Exception JavaDoc {
88         testWriteSoap11(false);
89     }
90
91     protected void testWriteSoap11(boolean useDom) throws Exception JavaDoc {
92         SoapMarshaler marshaler = new SoapMarshaler(true, useDom);
93         marshaler.setSoapUri(SoapMarshaler.SOAP_11_URI);
94         SoapMessage msg = new SoapMessage();
95         SoapFault fault = new SoapFault(new QName JavaDoc("my:urn", "code"), null, "My reason", null, null, new StringSource("<ns1:hello xmlns:ns1='my:urn'>world</ns1:hello>"));
96         msg.setFault(fault);
97         ByteArrayOutputStream JavaDoc baos = new ByteArrayOutputStream JavaDoc();
98         marshaler.createWriter(msg).write(baos);
99         System.err.println(baos.toString());
100
101         Node JavaDoc node = sourceTransformer.toDOMNode(new StringSource(baos.toString()));
102         Element JavaDoc e = ((Document JavaDoc) node).getDocumentElement();
103         assertEquals(new QName JavaDoc(SoapMarshaler.SOAP_11_URI, SoapMarshaler.ENVELOPE), DOMUtil.getQName(e));
104         e = DOMUtil.getFirstChildElement(e);
105         assertEquals(new QName JavaDoc(SoapMarshaler.SOAP_11_URI, SoapMarshaler.BODY), DOMUtil.getQName(e));
106         Element JavaDoc elFault = DOMUtil.getFirstChildElement(e);
107         assertEquals(new QName JavaDoc(SoapMarshaler.SOAP_11_URI, SoapMarshaler.FAULT), DOMUtil.getQName(elFault));
108         Element JavaDoc elCode = DOMUtil.getFirstChildElement(elFault);
109         assertEquals(SoapMarshaler.SOAP_11_FAULTCODE, DOMUtil.getQName(elCode));
110         assertEquals(new QName JavaDoc("my:urn", "code"), DOMUtil.createQName(elCode, DOMUtil.getElementText(elCode)));
111         Element JavaDoc elReason = DOMUtil.getNextSiblingElement(elCode);
112         assertEquals(SoapMarshaler.SOAP_11_FAULTSTRING, DOMUtil.getQName(elReason));
113         Element JavaDoc elDetail = DOMUtil.getNextSiblingElement(elReason);
114         assertEquals(SoapMarshaler.SOAP_11_FAULTDETAIL, DOMUtil.getQName(elDetail));
115         e = DOMUtil.getFirstChildElement(elDetail);
116         assertEquals(new QName JavaDoc("my:urn", "hello"), DOMUtil.getQName(e));
117     }
118     
119     public void testWriteSoap12UsingDom() throws Exception JavaDoc {
120         testWriteSoap12(true);
121     }
122     
123     public void testWriteSoap12UsingStax() throws Exception JavaDoc {
124         testWriteSoap12(false);
125     }
126     
127     protected void testWriteSoap12(boolean useDom) throws Exception JavaDoc {
128         SoapMarshaler marshaler = new SoapMarshaler(true, useDom);
129         marshaler.setSoapUri(SoapMarshaler.SOAP_12_URI);
130         SoapMessage msg = new SoapMessage();
131         SoapFault fault = new SoapFault(new QName JavaDoc("my:urn", "code"), null, "My reason", null, null,
132                                         new StringSource("<ns1:hello xmlns:ns1='my:urn'>world</ns1:hello>"));
133         msg.setFault(fault);
134         ByteArrayOutputStream JavaDoc baos = new ByteArrayOutputStream JavaDoc();
135         marshaler.createWriter(msg).write(baos);
136         System.err.println(baos.toString());
137
138         Node JavaDoc node = sourceTransformer.toDOMNode(new StringSource(baos.toString()));
139         Element JavaDoc e = ((Document JavaDoc) node).getDocumentElement();
140         assertEquals(new QName JavaDoc(SoapMarshaler.SOAP_12_URI, SoapMarshaler.ENVELOPE), DOMUtil.getQName(e));
141         e = DOMUtil.getFirstChildElement(e);
142         assertEquals(new QName JavaDoc(SoapMarshaler.SOAP_12_URI, SoapMarshaler.BODY), DOMUtil.getQName(e));
143         Element JavaDoc elFault = DOMUtil.getFirstChildElement(e);
144         assertEquals(new QName JavaDoc(SoapMarshaler.SOAP_12_URI, SoapMarshaler.FAULT), DOMUtil.getQName(elFault));
145         Element JavaDoc elCode = DOMUtil.getFirstChildElement(elFault);
146         assertEquals(SoapMarshaler.SOAP_12_FAULTCODE, DOMUtil.getQName(elCode));
147         e = DOMUtil.getFirstChildElement(elCode);
148         assertEquals(SoapMarshaler.SOAP_12_FAULTVALUE, DOMUtil.getQName(e));
149         e = DOMUtil.getNextSiblingElement(e);
150         assertEquals(SoapMarshaler.SOAP_12_FAULTSUBCODE, DOMUtil.getQName(e));
151         e = DOMUtil.getFirstChildElement(e);
152         assertEquals(SoapMarshaler.SOAP_12_FAULTVALUE, DOMUtil.getQName(e));
153         assertEquals(new QName JavaDoc("my:urn", "code"), DOMUtil.createQName(e, DOMUtil.getElementText(e)));
154         Element JavaDoc elReason = DOMUtil.getNextSiblingElement(elCode);
155         assertEquals(SoapMarshaler.SOAP_12_FAULTREASON, DOMUtil.getQName(elReason));
156         Element JavaDoc elDetail = DOMUtil.getNextSiblingElement(elReason);
157         assertEquals(SoapMarshaler.SOAP_12_FAULTDETAIL, DOMUtil.getQName(elDetail));
158         e = DOMUtil.getFirstChildElement(elDetail);
159         assertEquals(new QName JavaDoc("my:urn", "hello"), DOMUtil.getQName(e));
160     }
161     
162 }
163
Popular Tags