1 package test.encoding; 2 3 import junit.framework.TestCase; 4 import org.apache.axis.Constants; 5 import org.apache.axis.Message; 6 import org.apache.axis.MessageContext; 7 import org.apache.axis.encoding.TypeMapping; 8 import org.apache.axis.encoding.TypeMappingRegistry; 9 import org.apache.axis.message.RPCElement; 10 import org.apache.axis.message.RPCParam; 11 import org.apache.axis.message.SOAPEnvelope; 12 import org.apache.axis.server.AxisServer; 13 import org.apache.axis.utils.JavaUtils; 14 15 import javax.xml.namespace.QName ; 16 import java.util.Vector ; 17 18 21 public class TestBeanDeser extends TestCase { 22 23 private String header; 24 private String footer; 25 private AxisServer server = new AxisServer(); 26 27 public TestBeanDeser(String name) { 28 this(name, Constants.URI_DEFAULT_SCHEMA_XSI, 29 Constants.URI_DEFAULT_SCHEMA_XSD); 30 } 31 32 public TestBeanDeser(String name, String NS_XSI, String NS_XSD) { 33 super(name); 34 35 header = 36 "<?xml version=\"1.0\"?>\n" + 37 "<SOAP-ENV:Envelope\n" + 38 "xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\"\n" + 39 "xmlns:SOAP-ENC=\"http://schemas.xmlsoap.org/soap/encoding/\"\n" + 40 "xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"\n" + 41 "xmlns:xsd-cr=\"http://www.w3.org/2000/10/XMLSchema\"\n" + 42 "xmlns:xsd-lc=\"http://www.w3.org/1999/XMLSchema\"\n" + 43 "xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n" + 44 "SOAP-ENV:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\">\n"+ 45 "<SOAP-ENV:Body>\n"; 46 footer = 47 "</SOAP-ENV:Body>\n"+ 48 "</SOAP-ENV:Envelope>\n"; 49 50 TypeMappingRegistry tmr = server.getTypeMappingRegistry(); 51 TypeMapping tm = (TypeMapping) tmr.createTypeMapping(); 52 tm.setSupportedEncodings(new String []{Constants.URI_DEFAULT_SOAP_ENC}); 53 tmr.register(Constants.URI_DEFAULT_SOAP_ENC, tm); 54 tm.register(test.encoding.RETURN.class, 55 new QName ("urn:test.encoding", "RETURN"), 56 new org.apache.axis.encoding.ser.BeanSerializerFactory( 57 test.encoding.RETURN.class, 58 new QName ("urn:test.encoding", "RETURN")), 59 new org.apache.axis.encoding.ser.BeanDeserializerFactory( 60 test.encoding.RETURN.class, 61 new QName ("urn:test.encoding", "RETURN"))); 62 } 63 64 67 private static boolean equals(Object obj1, Object obj2) { 68 if ((obj1 == null) || (obj2 == null)) return (obj1 == obj2); 69 if (obj1.equals(obj2)) return true; 70 return false; 71 } 72 73 76 protected void deserialize(String data, Object expected) 77 throws Exception { 78 deserialize(data, expected, false); 79 } 80 81 protected void deserialize(String data, Object expected, boolean tryConvert) 82 throws Exception { 83 Message message = new Message(header + data + footer); 84 message.setMessageContext(new MessageContext(server)); 85 86 SOAPEnvelope envelope = (SOAPEnvelope) message.getSOAPEnvelope(); 87 assertNotNull("SOAP envelope should not be null", envelope); 88 89 RPCElement body = (RPCElement) envelope.getFirstBody(); 90 assertNotNull("SOAP body should not be null", body); 91 92 Vector arglist = body.getParams(); 93 assertNotNull("arglist", arglist); 94 assertTrue("param.size()<=0 {Should be > 0}", arglist.size() > 0); 95 96 RPCParam param = (RPCParam) arglist.get(0); 97 assertNotNull("SOAP param should not be null", param); 98 99 Object result = param.getObjectValue(); 100 if (!equals(result, expected)) { 101 if (tryConvert) { 103 Object result2 = JavaUtils.convert(result, expected.getClass()); 104 if (!equals(result2, expected)) { 105 assertEquals("The result is not what is expected.", expected, result); 106 } 107 } else { 108 assertEquals("The result is not what is expected.", expected, result); 109 } 110 } 111 } 112 113 public void testReturn() throws Exception { 115 test.encoding.RETURN ret = new test.encoding.RETURN(); 116 ret.setTYPE("000"); 117 ret.setID("001"); 118 ret.setNUMBER("002"); 119 ret.setMESSAGE("003"); 120 ret.setLOGNO("004"); 121 ret.setLOGMSGNO("005"); 122 ret.setMESSAGEV1("006"); 123 ret.setMESSAGEV2("007"); 124 ret.setMESSAGEV3("008"); 125 ret.setMESSAGEV4("009"); 126 String response = 127 "<ser-root:SrvResponse xmlns:ser-root=\"urn:test.encoding\">\n"+ 128 " <ser-root:RETURN xsi:type=\"ser-root:RETURN\">\n"+ 129 " <TYPE xsi:type=\"xsd:string\">000</TYPE>\n"+ 130 " <ID xsi:type=\"xsd:string\">001</ID>\n"+ 131 " <NUMBER xsi:type=\"xsd:string\">002</NUMBER>\n"+ 132 " <MESSAGE xsi:type=\"xsd:string\">003</MESSAGE>\n"+ 133 " <LOG_NO xsi:type=\"xsd:string\">004</LOG_NO>\n"+ 134 " <LOG_MSG_NO xsi:type=\"xsd:string\">005</LOG_MSG_NO>\n"+ 135 " <MESSAGE_V1 xsi:type=\"xsd:string\">006</MESSAGE_V1>\n"+ 136 " <MESSAGE_V2 xsi:type=\"xsd:string\">007</MESSAGE_V2>\n"+ 137 " <MESSAGE_V3 xsi:type=\"xsd:string\">008</MESSAGE_V3>\n"+ 138 " <MESSAGE_V4 xsi:type=\"xsd:string\">009</MESSAGE_V4>\n"+ 139 " </ser-root:RETURN>\n"+ 140 "</ser-root:SrvResponse>"; 141 deserialize(response,ret,true); 142 } 143 144 176 177 public static void main(String [] args) throws Exception 178 { 179 TestBeanDeser tester = new TestBeanDeser("test"); 180 tester.testReturn(); 181 } 182 } 183 | Popular Tags |