KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > test > wsdl > omit > OmitTestCase


1 /*
2  * Copyright 2001-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  * OmitTestCase.java
19  *
20  * This tests the omitting of elements when minOccurs=0 and the value
21  * of the element is Null.
22  *
23  * For instance:
24  * <Phone>
25  * <prefix>555</prefix>
26  * <number>1212</number>
27  * </Phone>
28  *
29  * This would normally have the additional areaCode element:
30  * <areaCode xsi:nil=true/>
31  *
32  */

33
34 package test.wsdl.omit;
35
36 import org.w3c.dom.Document JavaDoc;
37 import org.w3c.dom.NodeList JavaDoc;
38 import org.w3c.dom.Element JavaDoc;
39 import org.xml.sax.SAXException JavaDoc;
40
41 import javax.xml.parsers.DocumentBuilderFactory JavaDoc;
42 import javax.xml.parsers.DocumentBuilder JavaDoc;
43 import javax.xml.parsers.ParserConfigurationException JavaDoc;
44 import java.io.IOException JavaDoc;
45
46 /**
47  * Test nillable elements.
48  *
49  * @author Tom Jordahl (tomj@macromedia.com)
50  * @author Dominik Kacprzak (dominik@opentoolbox.com)
51  */

52 public class OmitTestCase extends junit.framework.TestCase {
53     private static final String JavaDoc AREA_CODE = "111";
54     public OmitTestCase(String JavaDoc name) {
55         super(name);
56     }
57     /**
58      * Optimistic scenario:
59      * - area code is echoed successfully
60      * - prefix is not part of XML exchanged between the client and servers
61      * - number is passed as null.
62      * There does not seem to be a good way to verify what's exchanged over the wire.
63      */

64     public void test1OmitEchoPhone() {
65         test.wsdl.omit.Omit binding;
66         try {
67             binding = new test.wsdl.omit.OmitTestLocator().getomit();
68         }
69         catch (javax.xml.rpc.ServiceException JavaDoc jre) {
70             throw new junit.framework.AssertionFailedError("JAX-RPC ServiceException caught: " + jre);
71         }
72         assertTrue("binding is null", binding != null);
73
74         try {
75             test.wsdl.omit.Phone input = new test.wsdl.omit.Phone();
76             input.setAreaCode(AREA_CODE);
77
78             test.wsdl.omit.Phone out = binding.echoPhone(input);
79
80             assertNotNull("The return value from the operation was null", out);
81             assertEquals("area code is incorrect", AREA_CODE, out.getAreaCode());
82             assertNull("prefix is not null", out.getPrefix());
83             assertNull("number is not null", out.getNumber());
84         }
85         catch (java.rmi.RemoteException JavaDoc re) {
86             throw new junit.framework.AssertionFailedError("Remote Exception caught: " + re);
87         }
88     }
89
90     /**
91      * Testing if an exception is thrown when a required elemen is null.
92      */

93     public void test2OmitEchoPhone() {
94         test.wsdl.omit.Omit binding;
95         try {
96             binding = new test.wsdl.omit.OmitTestLocator().getomit();
97         }
98         catch (javax.xml.rpc.ServiceException JavaDoc jre) {
99             throw new junit.framework.AssertionFailedError("JAX-RPC ServiceException caught: " + jre);
100         }
101         assertTrue("binding is null", binding != null);
102
103         try {
104             test.wsdl.omit.Phone input = new test.wsdl.omit.Phone();
105             test.wsdl.omit.Phone out = binding.echoPhone(input);
106             throw new junit.framework.AssertionFailedError("web services call succeeded despite of AreaCode being null.");
107         }
108         catch (java.rmi.RemoteException JavaDoc re) {
109             // this is desired
110
System.out.println(re);
111         }
112     }
113
114     /**
115      * A simple test to validate if WSDL generated by Axis' ?WSDL properly
116      * generates nillable properties.
117      */

118     public void testGeneratedWSDL() {
119         boolean testedAreaCode = false;
120         boolean testedPrefix = false;
121         boolean testedNumber = false;
122         // URL for omit's wsdl
123
String JavaDoc url = new test.wsdl.omit.OmitTestLocator().getomitAddress() + "?WSDL";
124         DocumentBuilderFactory JavaDoc factory = DocumentBuilderFactory.newInstance();
125         DocumentBuilder JavaDoc builder = null;
126         try {
127             builder = factory.newDocumentBuilder();
128         } catch (ParserConfigurationException JavaDoc e) {
129             throw new junit.framework.AssertionFailedError(e.getLocalizedMessage());
130         }
131         try {
132             Document JavaDoc document = builder.parse(url);
133             assertNotNull("WSDL document is null", document);
134             NodeList JavaDoc nodes = document.getDocumentElement().getElementsByTagName("element");
135             assertTrue("There are no \"elements\" in WSDL document", nodes.getLength() > 0);
136             // test elements
137
for (int i = 0; i < nodes.getLength(); i++) {
138                 Element JavaDoc element = (Element JavaDoc) nodes.item(i);
139                 String JavaDoc name = element.getAttribute("name");
140                 String JavaDoc nillable = element.getAttribute("nillable");
141                 if (name.equals("areaCode")) {
142                     testedAreaCode = true;
143                     assertTrue("nillable attribute for \"areaCode\" element should be empty",
144                                nillable.length() == 0);
145                 }
146                 if (name.equals("prefix")) {
147                     testedPrefix = true;
148                     assertTrue("nillable attribute for \"prefix\" element should be empty",
149                                nillable.length() == 0);
150                 }
151                 if (name.equals("number")) {
152                     testedNumber = true;
153                     assertTrue("nillable attribute for \"number\" element should be set to \"true\"",
154                                nillable.equals("true"));
155                 }
156             }
157             // check if all elements were tested
158
assertTrue("areaCode element was not validated", testedAreaCode);
159             assertTrue("prefix element was not validated", testedPrefix);
160             assertTrue("number element was not validated", testedNumber);
161
162         } catch (SAXException JavaDoc e) {
163             throw new junit.framework.AssertionFailedError(e.getLocalizedMessage());
164         } catch (IOException JavaDoc e) {
165             throw new junit.framework.AssertionFailedError(e.getLocalizedMessage());
166         }
167     }
168
169     public static void main(String JavaDoc[] args) {
170         OmitTestCase tester = new OmitTestCase("tester");
171         tester.test1OmitEchoPhone();
172     }
173 }
174
Popular Tags