| 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; |
| 37 |
import org.w3c.dom.NodeList; |
| 38 |
import org.w3c.dom.Element; |
| 39 |
import org.xml.sax.SAXException; |
| 40 |
|
| 41 |
import javax.xml.parsers.DocumentBuilderFactory; |
| 42 |
import javax.xml.parsers.DocumentBuilder; |
| 43 |
import javax.xml.parsers.ParserConfigurationException; |
| 44 |
import java.io.IOException; |
| 45 |
|
| 46 |
/** |
| 47 |
* Test nillable elements. |
| 48 |
* |
| 49 |
* @author Tom Jordahl ([email protected]) |
| 50 |
* @author Dominik Kacprzak ([email protected]) |
| 51 |
*/ |
| 52 |
public class OmitTestCase extends junit.framework.TestCase { |
| 53 |
private static final String AREA_CODE = "111"; |
| 54 |
public OmitTestCase(String 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 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 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 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 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 url = new test.wsdl.omit.OmitTestLocator().getomitAddress() + "?WSDL"; |
| 124 |
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); |
| 125 |
DocumentBuilder builder = null; |
| 126 |
try { |
| 127 |
builder = factory.newDocumentBuilder(); |
| 128 |
} catch (ParserConfigurationException e) { |
| 129 |
throw new junit.framework.AssertionFailedError(e.getLocalizedMessage()); |
| 130 |
} |
| 131 |
try { |
| 132 |
Document document = builder.parse(url); |
| 133 |
assertNotNull("WSDL document is null", document); |
| 134 |
NodeList 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 element = (Element) nodes.item(i); |
| 139 |
String name = element.getAttribute("name"); |
| 140 |
String 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 e) { |
| 163 |
throw new junit.framework.AssertionFailedError(e.getLocalizedMessage()); |
| 164 |
} catch (IOException e) { |
| 165 |
throw new junit.framework.AssertionFailedError(e.getLocalizedMessage()); |
| 166 |
} |
| 167 |
} |
| 168 |
|
| 169 |
public static void main(String[] args) { |
| 170 |
OmitTestCase tester = new OmitTestCase("tester"); |
| 171 |
tester.test1OmitEchoPhone(); |
| 172 |
} |
| 173 |
} |