KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > test > faults > TestAxisFault


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
17
18 package test.faults;
19
20 import junit.framework.TestCase;
21 import junit.framework.Test;
22 import junit.framework.TestSuite;
23 import org.apache.axis.AxisFault;
24 import org.apache.axis.Constants;
25 import org.apache.axis.NoEndPointException;
26 import org.apache.axis.utils.XMLUtils;
27 import org.w3c.dom.Element JavaDoc;
28 import org.w3c.dom.Document JavaDoc;
29 import org.w3c.dom.Text JavaDoc;
30
31 import javax.xml.namespace.QName JavaDoc;
32 import javax.xml.parsers.ParserConfigurationException JavaDoc;
33
34 /**
35  * unit tests for the ubiquitous AxisFault
36  */

37 public class TestAxisFault extends TestCase {
38
39     public TestAxisFault(String JavaDoc s) {
40         super(s);
41     }
42
43
44     public static Test suite() {
45         return new TestSuite(TestAxisFault.class);
46     }
47     /**
48      * test that exceptions are filled in
49      */

50     public void testExceptionFillIn() {
51         Exception JavaDoc e=new Exception JavaDoc("foo");
52         AxisFault af=AxisFault.makeFault(e);
53         Element JavaDoc stackTrace;
54         stackTrace = af.lookupFaultDetail(Constants.QNAME_FAULTDETAIL_STACKTRACE);
55         assertNotNull(stackTrace);
56         Element JavaDoc exceptionName;
57         exceptionName = af.lookupFaultDetail(Constants.QNAME_FAULTDETAIL_EXCEPTIONNAME);
58         assertNull(exceptionName);
59         QName JavaDoc faultCode=af.getFaultCode();
60         assertEquals(faultCode.getLocalPart(), Constants.FAULT_SERVER_USER);
61     }
62
63
64     /**
65      * test that making an axis fault from an axis fault retains it
66      */

67     public void testAxisFaultFillIn() {
68         AxisFault af1=new AxisFault("fault1");
69         AxisFault af2=AxisFault.makeFault(af1);
70         assertSame(af1,af2);
71     }
72
73     /**
74      * test we can remove some detail
75      */

76     public void testDetailRemoval() {
77         Exception JavaDoc e = new Exception JavaDoc("foo");
78         AxisFault af = AxisFault.makeFault(e);
79         assertTrue(af.removeFaultDetail(Constants.QNAME_FAULTDETAIL_STACKTRACE));
80         Element JavaDoc stackTrace;
81         stackTrace = af.lookupFaultDetail(Constants.QNAME_FAULTDETAIL_STACKTRACE);
82         assertNull(stackTrace);
83         String JavaDoc text=af.getFaultString();
84         assertNotNull(text);
85         text=af.toString();
86         assertNotNull(text);
87     }
88
89     /**
90      * test what happens with subclasses. We expect the classname to be preserved
91      * in the details
92      */

93     public void testSubclassProcessing() {
94         AxisFault af=new NoEndPointException();
95         Element JavaDoc exceptionName;
96         exceptionName = af.lookupFaultDetail(Constants.QNAME_FAULTDETAIL_EXCEPTIONNAME);
97         assertNotNull(exceptionName);
98         String JavaDoc exceptionClassname= XMLUtils.getInnerXMLString(exceptionName);
99         assertTrue(exceptionClassname.indexOf("NoEndPointException")>=0);
100     }
101
102     /**
103      * verify we can properly lookup empty namespace stuff
104      */

105     public void testEmptyNamespaceLookup() {
106         AxisFault af=new AxisFault();
107         af.addFaultDetailString("alles geht gut");
108         Element JavaDoc match=af.lookupFaultDetail(new QName JavaDoc(null,"string"));
109         assertNotNull(match);
110     }
111
112     public void testArrayAddWorks() {
113         AxisFault af = new AxisFault();
114         af.addFaultDetailString("alles geht gut");
115         Element JavaDoc array[]=new Element JavaDoc[2];
116         array[0] = createElement("ein","un");
117         array[1] = createElement("zwei", "deux");
118         af.setFaultDetail(array);
119         Element JavaDoc match = af.lookupFaultDetail(new QName JavaDoc(null, "zwei"));
120         assertNotNull(match);
121         Element JavaDoc old = af.lookupFaultDetail(new QName JavaDoc(null, "string"));
122         assertNull(old);
123     }
124
125     public void testEmptyArrayAddWorks() {
126         AxisFault af = new AxisFault();
127         af.addFaultDetailString("alles geht gut");
128         Element JavaDoc array[] = new Element JavaDoc[0];
129         af.setFaultDetail(array);
130         Element JavaDoc old = af.lookupFaultDetail(new QName JavaDoc(null, "string"));
131         assertNull(old);
132     }
133
134     public Element JavaDoc createElement(String JavaDoc tag,String JavaDoc child) {
135         Document JavaDoc doc = null;
136         try {
137             doc = XMLUtils.newDocument();
138         } catch (ParserConfigurationException JavaDoc e) {
139             throw new RuntimeException JavaDoc("xml trouble");
140         }
141         Element JavaDoc element = doc.createElement(tag);
142         Text JavaDoc text = doc.createTextNode(child);
143         element.appendChild(text);
144         return element;
145     }
146
147     /**
148      * helper method to stick in when diagnosing stuff
149      * @param af
150      */

151     private void dumpFault(AxisFault af) {
152         String JavaDoc s=af.dumpToString();
153         System.out.println(s);
154     }
155 }
156
Popular Tags