📄 soapserializationenvelopetest.java.svn-base
字号:
* <s:element name="GetStatusResponse"> * <s:complexType> * <s:sequence> * <s:element minOccurs="0" maxOccurs="1" name="GetStatusResult" type="s0:ReplyBase"/> * <s:element minOccurs="0" maxOccurs="1" name="Status" type="s0:ServerStatus"/> * </s:sequence> * </s:complexType> * </s:element> *<s:complexType name="ReplyBase"> * <s:attribute name="RcvTime" type="s:dateTime" use="required"/> * <s:attribute name="ReplyTime" type="s:dateTime" use="required"/> * <s:attribute name="ClientRequestHandle" type="s:string"/> * <s:attribute name="RevisedLocaleID" type="s:string"/> * <s:attribute name="ServerState" type="s0:serverState" use="required"/> *</s:complexType> *<s:simpleType name="serverState"> * <s:restriction base="s:string"> * <s:enumeration value="running"/> * <s:enumeration value="failed"/> * <s:enumeration value="noConfig"/> * <s:enumeration value="suspended"/> * <s:enumeration value="test"/> * <s:enumeration value="commFault"/> * </s:restriction> *</s:simpleType> * <s:complexType name="ServerStatus"> * <s:sequence> * <s:element minOccurs="0" maxOccurs="1" name="StatusInfo" type="s:string"/> * <s:element minOccurs="0" maxOccurs="1" name="VendorInfo" type="s:string"/> * <s:element minOccurs="0" maxOccurs="unbounded" name="SupportedLocaleIDs" type="s:string"/> * <s:element minOccurs="0" maxOccurs="unbounded" name="SupportedInterfaceVersions" type="s0:interfaceVersion"/> * </s:sequence> * <s:attribute name="StartTime" type="s:dateTime" use="required"/> * <s:attribute name="ProductVersion" type="s:string"/> * </s:complexType> * <s:simpleType name="interfaceVersion"> * <s:restriction base="s:string"> * <s:enumeration value="XML_DA_Version_1_0"/> * </s:restriction> * </s:simpleType> * * In other words, we call "getStatus" on the service, with "LocaleID" and "ClientRequestHandle" available * as attributes. The response has two objects "ReplyBase" and "ServerStatus". Each of these have a hodgepog * of attributes, sequences and enumerations. * * The output message should look, more or less like this (from the specification): * * <soap:Body> * <getStatus * LocaleID="de-AT" xmlns="http://opcfoundation.org/webservices/XMLDA/1.0/" * /> *</soap:Body> * * and the response more or less like this (from the specification): * * <soap:Body> * <GetStatusResponse xmlns="http://opcfoundation.org/webservices/XMLDA/1.0/"> * <GetStatusResult * RcvTime="2003-05-26T20:17:42.4781250-07:00" * ReplyTime="2003-05-26T20:17:42.5781250-07:00" * RevisedLocaleID="de" * ServerState="running" * /> * <Status * StartTime="2003-05-26T20:16:45.0937500-07:00" * ProductVersion="1.00.1.00" * > * <VendorInfo>OPC XML Data Access 1.00 Sample Server</VendorInfo> * <SupportedLocaleIDs>en</SupportedLocaleIDs> * <SupportedLocaleIDs>en-US</SupportedLocaleIDs> * <SupportedLocaleIDs>de</SupportedLocaleIDs> * <SupportedInterfaceVersions>XML_DA_Version_1_0</SupportedInterfaceVersions> * </Status> * </GetStatusResponse> *</soap:Body> */ /** * This will generate an XML string from a SoapObject/SoapEnvelope that represents the "getStatus" * call in the WSDL above. The resulting XML will be passed to "public void testGetStatus(String xmlString)" * to ensure AXIS compatibility (and compatibility with the published standards). */ public void testGetStatusSoapObject() throws Throwable { SoapObject getStatus = new SoapObject("http://opcfoundation.org/webservices/XMLDA/1.0/", "getStatus"); getStatus.addAttribute("LocaleID", "de-AT"); getStatus.addAttribute("ClientRequestHandle", "ClientHandle"); SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11); envelope.bodyOut = getStatus; envelope.setAddAdornments(false); byte[] request = myTransport.createRequestData(envelope); String xmlString = new String(request); assertGetStatus(xmlString); } /** The string that represents the "getStatus". This is copied from an Axis call. */ protected static final String getStatusRequest = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">" + "<soapenv:Body><getStatus ClientRequestHandle=\"ClientHandle\" LocaleID=\"de-AT\" xmlns=\"http://opcfoundation.org/webservices/XMLDA/1.0/\"/></soapenv:Body>" + "</soapenv:Envelope>"; /** Test the status using the AXIS generated xml. * @throws Throwable */ public void testGetStatus() throws Throwable { assertGetStatus(getStatusRequest); } /** * Test getStatus by passing in an xmlString. The result should be the same with an Axis or ksoap * generated string (clearly). * @param xmlString the xml string to test. * @throws Throwable */ public void assertGetStatus(String xmlString) throws Throwable { serviceCallCount = 0; // zero this Object service = new GetStatus(); envelope.addMapping("http://opcfoundation.org/webservices/XMLDA/1.0/", "getStatus", SoapObject.class); envelope.setAddAdornments(false); ByteArrayInputStream bis = new ByteArrayInputStream(xmlString.getBytes()); XmlPullParser parser = new KXmlParser(); parser.setInput(bis, "UTF-8"); parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, true); envelope.parse(parser); SoapObject soapReq = (SoapObject) envelope.bodyIn; assertEquals("ClientHandle", soapReq.getAttribute("ClientRequestHandle")); SoapObject result = null; result = MockTransport.invoke(service, soapReq); assertEquals("Service Called ", 1, serviceCallCount); //assert contents of result; } /** * This will generate an XML string from a SoapObject/SoapEnvelope that represents the "GetStatusResponse" * call in the WSDL above. The resulting XML will be passed to * "public void testGetStatusResponse(String xmlString)" * to ensure AXIS compatibility (and compatibility with the published standards). */ public void testGetStatusResponseSoapObject() throws Throwable { SoapObject getStatusResponse = new SoapObject("http://opcfoundation.org/webservices/XMLDA/1.0/", "GetStatusResponse"); SoapObject getStatusResult = new SoapObject("http://opcfoundation.org/webservices/XMLDA/1.0/", "GetStatusResult"); getStatusResult.addAttribute("RcvTime", "2003-05-26T20:17:42.4781250-07:00"); getStatusResult.addAttribute("ReplyTime", "2003-05-26T20:17:42.5781250-07:00"); getStatusResult.addAttribute("RevisedLocaleID", "de"); getStatusResult.addAttribute("ServerState", "running"); SoapObject status = new SoapObject("http://opcfoundation.org/webservices/XMLDA/1.0/", "Status"); status.addAttribute("StartTime", "2003-05-26T20:16:45.0937500-07:00"); status.addAttribute("ProductVersion", "1.00.1.00"); status.addProperty("VendorInfo", "OPC XML Data Access 1.00 Sample Server"); status.addProperty("SupportedLocaleIDs", "en"); status.addProperty("SupportedLocaleIDs", "en-US"); status.addProperty("SupportedLocaleIDs", "de"); status.addProperty("SupportedInterfaceVersions", "XML_DA_Version_1_0"); getStatusResponse.addProperty("GetStatusResult", getStatusResult); getStatusResponse.addProperty("Status", status); SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11); envelope.bodyOut = getStatusResponse; envelope.setImplicitTypes(true); envelope.setAddAdornments(false); byte[] request = null; request = myTransport.createRequestData(envelope); String xmlString = new String(request); assertGetStatusResponse(xmlString); } /** * GetStatusResponse XML. The body is from the OPC Foundation manual, the envelope * is from the AXIS envelope above. */ protected static final String getStatusResponse = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">" + " <soapenv:Body>" + " <GetStatusResponse xmlns=\"http://opcfoundation.org/webservices/XMLDA/1.0/\">" + " <GetStatusResult" + " RcvTime=\"2003-05-26T20:17:42.4781250-07:00\"" + " ReplyTime=\"2003-05-26T20:17:42.5781250-07:00\"" + " RevisedLocaleID=\"de\"" + " ServerState=\"running\"" + " />" + " <Status" + " StartTime=\"2003-05-26T20:16:45.0937500-07:00\"" + " ProductVersion=\"1.00.1.00\"" + " >" + " <VendorInfo>OPC XML Data Access 1.00 Sample Server</VendorInfo>" + " <SupportedLocaleIDs>en</SupportedLocaleIDs>" + " <SupportedLocaleIDs>en-US</SupportedLocaleIDs>" + " <SupportedLocaleIDs>de</SupportedLocaleIDs>" + " <SupportedInterfaceVersions>XML_DA_Version_1_0</SupportedInterfaceVersions>" + " </Status>" + " </GetStatusResponse>" + "</soapenv:Body>" + "</soapenv:Envelope>"; /** * Test the response from "getStatus". The body is from the OPC Foundation manual, the envelope * is from the AXIS envelope above. * @throws Throwable */ public void testGetStatusResponse() throws Throwable { assertGetStatusResponse(getStatusResponse); } /** * Test the response from "getStatus". * @throws Throwable */ public void assertGetStatusResponse(String xmlString) throws Throwable { ByteArrayInputStream is = new ByteArrayInputStream(xmlString.toString().getBytes()); envelope.addMapping("http://opcfoundation.org/webservices/XMLDA/1.0/", "GetStatusResponse", SoapObject.class); envelope.addMapping("http://opcfoundation.org/webservices/XMLDA/1.0/", "Status", SoapObject.class); envelope.addMapping("http://opcfoundation.org/webservices/XMLDA/1.0/", "GetStatusResult", SoapObject.class); myTransport.parseResponse(envelope, is); is.close(); SoapObject response = (SoapObject) envelope.bodyIn; assertEquals(" Two Parameter (GetStatusResponse)", 2, ((SoapObject) response).getPropertyCount()); assertEquals(" No Attributes (GetStatusResponse)", 0, ((SoapObject) response).getAttributeCount()); // first property is "GetStatusResult" SoapObject getStatusResult = (SoapObject) response.getProperty("GetStatusResult"); assertNotNull(" GetStatusResult ", getStatusResult); assertEquals(" No Parameters (GetStatusResult)", 0, getStatusResult.getPropertyCount()); assertEquals(" Four Attributes (GetStatusResult)", 4, getStatusResult.getAttributeCount()); assertEquals("2003-05-26T20:17:42.4781250-07:00", getStatusResult.getAttribute("RcvTime")); assertEquals("2003-05-26T20:17:42.5781250-07:00", getStatusResult.getAttribute("ReplyTime")); assertEquals("running", getStatusResult.getAttribute("ServerState")); // first property is "Status" SoapObject status = (SoapObject) response.getProperty("Status"); assertEquals(" Five Parameters (Status)", 5, status.getPropertyCount()); assertEquals(" Two Attributes (Status)", 2, status.getAttributeCount()); assertEquals("2003-05-26T20:16:45.0937500-07:00", status.getAttribute("StartTime")); assertEquals("1.00.1.00", status.getAttribute("ProductVersion")); } public static class GetStatus { public SoapObject getStatus(SoapObject in) { serviceCallCount++; assertEquals("Should be two attributes. ", 2, in.getAttributeCount()); assertEquals("LocaleID ", "de-AT", in.getAttribute("LocaleID")); assertEquals("ClientRequestHandle ", "ClientHandle", in.getAttribute("ClientRequestHandle")); return null; } };}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -