Debian Codegraph @

test/chains/TestChainFault.java

raw
1 /*
2 * Copyright 2002-2004 The Apache Software Foundation.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 package test.chains;
17
18 import junit.framework.Test;
19 import junit.framework.TestCase;
20 import junit.framework.TestSuite;
21 import org.apache.axis.AxisFault;
22 import org.apache.axis.Message;
23 import org.apache.axis.MessageContext;
24 import org.apache.axis.SimpleChain;
25 import org.apache.axis.handlers.BasicHandler;
26 import org.apache.axis.server.AxisServer;
27
28 import javax.xml.soap.SOAPBody;
29
30 /**
31 * Used to verify that Faults are processed properly in the Handler chain
32 * @author Russell Butek ([email protected])
33 * @author Chris Haddad <[email protected]>
34 */
35 public class TestChainFault extends TestCase
36 {
37 // correlation message
38 public static String FAULT_MESSAGE = "Blew a gasket!";
39
40 public TestChainFault (String name) {
41 super(name);
42 }
43
44 public static Test suite() {
45 return new TestSuite(TestChainFault.class);
46 }
47
48 protected void setup() {
49 }
50
51 private class TestMessageContext extends MessageContext {
52
53 private int hcount = 0;
54
55 public TestMessageContext() {
56 super(new AxisServer());
57 }
58
59 public void incCount() {
60 hcount++;
61 }
62
63 public void decCount() {
64 hcount--;
65 }
66
67 public int count() {
68 return hcount;
69 }
70 }
71
72 private class TestHandler extends BasicHandler {
73 private int chainPos;
74 private boolean doFault = false;
75 private String stFaultCatch = null;
76
77 /* The following state really relates to a Message Context, so this Handler
78 * must not be used for more than one Message Context. However, it
79 * is sufficient for the purpose of this testcase.
80 */
81 private boolean invoked = false;
82
83 public TestHandler(int pos) {
84 chainPos = pos;
85 }
86
87 public void setToFault() {
88 doFault = true;
89 }
90
91 public void setFaultCatch(String stValue) { stFaultCatch = stValue; }
92 public String getFaultCatch() { return stFaultCatch; }
93
94 public void invoke(MessageContext msgContext) throws AxisFault {
95 TestMessageContext mc = (TestMessageContext)msgContext;
96 assertEquals("Handler.invoke out of sequence", chainPos, mc.count());
97 invoked = true;
98 if (doFault) {
99 throw new AxisFault(TestChainFault.FAULT_MESSAGE);
100 }
101 mc.incCount();
102 }
103
104 public void onFault(MessageContext msgContext) {
105 TestMessageContext mc = (TestMessageContext)msgContext;
106 mc.decCount();
107 assertEquals("Handler.onFault out of sequence", chainPos, mc.count());
108 assertTrue("Handler.onFault protocol error", invoked);
109 // grap the Soap Fault String
110 stFaultCatch = getFaultString(msgContext);
111 }
112 }
113
114 /**
115 * Extract the fault string from the Soap Response
116 *
117 **/
118 String getFaultString(MessageContext msgContext) {
119 String stRetval = null;
120 Message message = msgContext.getResponseMessage();
121 try {
122 if (message != null) {
123 SOAPBody oBody = message.getSOAPEnvelope().getBody();
124 stRetval = oBody.getFault().getFaultString();
125 }
126 }
127 catch (javax.xml.soap.SOAPException e) {
128 e.printStackTrace();
129 assertTrue("Unforseen soap exception", false);
130 }
131 catch (AxisFault f) {
132 f.printStackTrace();
133 assertTrue("Unforseen axis fault", false);
134 }
135
136 return stRetval;
137 }
138
139 public void testSimpleChainFaultAfterInvoke()
140 {
141 try {
142 SimpleChain c = new SimpleChain();
143
144 for (int i = 0; i < 5; i++) {
145 c.addHandler(new TestHandler(i));
146 }
147
148 TestMessageContext mc = new TestMessageContext();
149 c.invoke(mc);
150 c.onFault(mc);
151 assertEquals("Some onFaults were missed", mc.count(), 0);
152
153 } catch (Exception ex) {
154 assertTrue("Unexpected exception", false);
155 ex.printStackTrace();
156 }
157 }
158
159
160 public void testSimpleChainFaultDuringInvoke()
161 {
162 try {
163 SimpleChain c = new SimpleChain();
164
165 for (int i = 0; i < 5; i++) {
166 TestHandler th = new TestHandler(i);
167 if (i == 3) {
168 th.setToFault();
169 }
170 c.addHandler(th);
171 }
172
173
174 TestMessageContext mc = new TestMessageContext();
175 try {
176 c.invoke(mc);
177 assertTrue("Testcase error - didn't throw fault", false);
178 } catch (AxisFault f) {
179 assertEquals("Some onFaults were missed", mc.count(), 0);
180 }
181
182 } catch (Exception ex) {
183 ex.printStackTrace();
184 assertTrue("Unexpected exception", false);
185 }
186 }
187
188 /**
189 * Ensure that the fault detail is being passed back
190 * to handlers that executed prior to the fault
191 **/
192 public void testFaultDetailAvailableDuringInvoke()
193 {
194 // the handler instance to validate
195 // NOTE:must execute before the handler that throws the fault
196 TestHandler testHandler = null;
197
198 try {
199 SimpleChain c = new SimpleChain();
200
201 for (int i = 0; i < 5; i++) {
202 TestHandler th = new TestHandler(i);
203 if (i == 2)
204 testHandler = th;
205
206 if (i == 3) {
207 th.setToFault();
208 }
209 c.addHandler(th);
210 }
211
212
213 TestMessageContext mc = new TestMessageContext();
214 try {
215 c.invoke(mc);
216 assertTrue("Testcase error - didn't throw fault", false);
217 } catch (AxisFault f) {
218 // did we save off the fault string?
219 assertEquals("faultstring does not match constant",
220 TestChainFault.FAULT_MESSAGE,
221 testHandler.getFaultCatch());
222 // does saved faultString match AxisFault?
223 assertEquals("Fault not caught by handler",
224 testHandler.getFaultCatch(),f.getFaultString());
225 }
226
227 } catch (Exception ex) {
228 assertTrue("Unexpected exception", false);
229 }
230 }
231
232 }