KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > test > chains > TestChainFault


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 JavaDoc;
29
30 /**
31 * Used to verify that Faults are processed properly in the Handler chain
32 * @author Russell Butek (butek@us.ibm.com)
33 * @author Chris Haddad <haddadc@cobia.net>
34 */

35 public class TestChainFault extends TestCase
36 {
37   // correlation message
38
public static String JavaDoc FAULT_MESSAGE = "Blew a gasket!";
39
40     public TestChainFault (String JavaDoc 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 JavaDoc 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 JavaDoc stValue) { stFaultCatch = stValue; }
92         public String JavaDoc 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 JavaDoc getFaultString(MessageContext msgContext) {
119       String JavaDoc stRetval = null;
120       Message message = msgContext.getResponseMessage();
121       try {
122           if (message != null) {
123             SOAPBody JavaDoc oBody = message.getSOAPEnvelope().getBody();
124             stRetval = oBody.getFault().getFaultString();
125           }
126       }
127       catch (javax.xml.soap.SOAPException JavaDoc 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 JavaDoc 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 JavaDoc ex) {
183             assertTrue("Unexpected exception", false);
184         }
185     }
186
187 /**
188 * Ensure that the fault detail is being passed back
189 * to handlers that executed prior to the fault
190 **/

191     public void testFaultDetailAvailableDuringInvoke()
192     {
193       // the handler instance to validate
194
// NOTE:must execute before the handler that throws the fault
195
TestHandler testHandler = null;
196
197         try {
198             SimpleChain c = new SimpleChain();
199
200             for (int i = 0; i < 5; i++) {
201                 TestHandler th = new TestHandler(i);
202                 if (i == 2)
203                   testHandler = th;
204
205                 if (i == 3) {
206                     th.setToFault();
207                 }
208                 c.addHandler(th);
209             }
210
211
212             TestMessageContext mc = new TestMessageContext();
213             try {
214                 c.invoke(mc);
215                 assertTrue("Testcase error - didn't throw fault", false);
216             } catch (AxisFault f) {
217                 // did we save off the fault string?
218
assertEquals("faultstring does not match constant",
219                 testHandler.getFaultCatch(),TestChainFault.FAULT_MESSAGE);
220                 // does saved faultString match AxisFault?
221
assertEquals("Fault not caught by handler",
222                 testHandler.getFaultCatch(),f.getFaultString());
223             }
224
225         } catch (Exception JavaDoc ex) {
226             assertTrue("Unexpected exception", false);
227         }
228     }
229
230 }
231
Popular Tags