Debian Codegraph @

test/faults/TestAxisFault.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
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;
28 import org.w3c.dom.Document;
29 import org.w3c.dom.Text;
30
31 import javax.xml.namespace.QName;
32 import javax.xml.parsers.ParserConfigurationException;
33
34 /**
35 * unit tests for the ubiquitous AxisFault
36 */
37 public class TestAxisFault extends TestCase {
38
39 public TestAxisFault(String 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 e=new Exception("foo");
52 AxisFault af=AxisFault.makeFault(e);
53 Element stackTrace;
54 stackTrace = af.lookupFaultDetail(Constants.QNAME_FAULTDETAIL_STACKTRACE);
55 assertNotNull(stackTrace);
56 Element exceptionName;
57 exceptionName = af.lookupFaultDetail(Constants.QNAME_FAULTDETAIL_EXCEPTIONNAME);
58 assertNull(exceptionName);
59 QName 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 e = new Exception("foo");
78 AxisFault af = AxisFault.makeFault(e);
79 assertTrue(af.removeFaultDetail(Constants.QNAME_FAULTDETAIL_STACKTRACE));
80 Element stackTrace;
81 stackTrace = af.lookupFaultDetail(Constants.QNAME_FAULTDETAIL_STACKTRACE);
82 assertNull(stackTrace);
83 String 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 exceptionName;
96 exceptionName = af.lookupFaultDetail(Constants.QNAME_FAULTDETAIL_EXCEPTIONNAME);
97 assertNotNull(exceptionName);
98 String 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 match=af.lookupFaultDetail(new QName(null,"string"));
109 assertNotNull(match);
110 }
111
112 public void testArrayAddWorks() {
113 AxisFault af = new AxisFault();
114 af.addFaultDetailString("alles geht gut");
115 Element array[]=new Element[2];
116 array[0] = createElement("ein","un");
117 array[1] = createElement("zwei", "deux");
118 af.setFaultDetail(array);
119 Element match = af.lookupFaultDetail(new QName(null, "zwei"));
120 assertNotNull(match);
121 Element old = af.lookupFaultDetail(new QName(null, "string"));
122 assertNull(old);
123 }
124
125 public void testEmptyArrayAddWorks() {
126 AxisFault af = new AxisFault();
127 af.addFaultDetailString("alles geht gut");
128 Element array[] = new Element[0];
129 af.setFaultDetail(array);
130 Element old = af.lookupFaultDetail(new QName(null, "string"));
131 assertNull(old);
132 }
133
134 public Element createElement(String tag,String child) {
135 Document doc = null;
136 try {
137 doc = XMLUtils.newDocument();
138 } catch (ParserConfigurationException e) {
139 throw new RuntimeException("xml trouble");
140 }
141 Element element = doc.createElement(tag);
142 Text 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 s=af.dumpToString();
153 System.out.println(s);
154 }
155 }