1 16 17 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 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 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 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 System.out.println(re); 111 } 112 } 113 114 118 public void testGeneratedWSDL() { 119 boolean testedAreaCode = false; 120 boolean testedPrefix = false; 121 boolean testedNumber = false; 122 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 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 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 } 174 | Popular Tags |