KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > test > encoding > TestBeanDeser


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 JavaDoc;
16 import java.util.Vector JavaDoc;
17
18 /**
19  * Test deserialization of SOAP responses
20  */

21 public class TestBeanDeser extends TestCase {
22
23     private String JavaDoc header;
24     private String JavaDoc footer;
25     private AxisServer server = new AxisServer();
26
27     public TestBeanDeser(String JavaDoc name) {
28         this(name, Constants.URI_DEFAULT_SCHEMA_XSI,
29                 Constants.URI_DEFAULT_SCHEMA_XSD);
30     }
31
32     public TestBeanDeser(String JavaDoc name, String JavaDoc NS_XSI, String JavaDoc 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 JavaDoc[]{Constants.URI_DEFAULT_SOAP_ENC});
53         tmr.register(Constants.URI_DEFAULT_SOAP_ENC, tm);
54         tm.register(test.encoding.RETURN.class,
55                 new QName JavaDoc("urn:test.encoding", "RETURN"),
56                 new org.apache.axis.encoding.ser.BeanSerializerFactory(
57                         test.encoding.RETURN.class,
58                         new QName JavaDoc("urn:test.encoding", "RETURN")),
59                 new org.apache.axis.encoding.ser.BeanDeserializerFactory(
60                         test.encoding.RETURN.class,
61                         new QName JavaDoc("urn:test.encoding", "RETURN")));
62     }
63
64     /**
65      * Verify that two objects have the same value, handling arrays...
66      */

67     private static boolean equals(Object JavaDoc obj1, Object JavaDoc obj2) {
68         if ((obj1 == null) || (obj2 == null)) return (obj1 == obj2);
69         if (obj1.equals(obj2)) return true;
70         return false;
71     }
72
73     /**
74      * Verify that a given XML deserialized produces the expected result
75      */

76     protected void deserialize(String JavaDoc data, Object JavaDoc expected)
77             throws Exception JavaDoc {
78         deserialize(data, expected, false);
79     }
80
81     protected void deserialize(String JavaDoc data, Object JavaDoc expected, boolean tryConvert)
82             throws Exception JavaDoc {
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 JavaDoc 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 JavaDoc result = param.getObjectValue();
100         if (!equals(result, expected)) {
101             // Try to convert to the expected class
102
if (tryConvert) {
103                 Object JavaDoc 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     // Struct Return
114
public void testReturn() throws Exception JavaDoc {
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 JavaDoc 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     /*
145     // Struct Return - variation
146     public void testReturn2() throws Exception {
147         test.encoding.RETURN ret = new test.encoding.RETURN();
148         ret.setTYPE("000");
149         ret.setID("001");
150         ret.setNUMBER("002");
151         ret.setMESSAGE("003");
152         ret.setLOGNO("004");
153         ret.setLOGMSGNO("005");
154         ret.setMESSAGEV1("006");
155         ret.setMESSAGEV2("007");
156         ret.setMESSAGEV3("008");
157         ret.setMESSAGEV4("009");
158         String response =
159                 "<SrvResponse xmlns=\"urn:test.encoding\">\n"+
160                 " <RETURN>\n"+
161                 " <TYPE xsi:type=\"xsd:string\">000</TYPE>\n"+
162                 " <ID xsi:type=\"xsd:string\">001</ID>\n"+
163                 " <NUMBER xsi:type=\"xsd:string\">002</NUMBER>\n"+
164                 " <MESSAGE xsi:type=\"xsd:string\">003</MESSAGE>\n"+
165                 " <LOG_NO xsi:type=\"xsd:string\">004</LOG_NO>\n"+
166                 " <LOG_MSG_NO xsi:type=\"xsd:string\">005</LOG_MSG_NO>\n"+
167                 " <MESSAGE_V1 xsi:type=\"xsd:string\">006</MESSAGE_V1>\n"+
168                 " <MESSAGE_V2 xsi:type=\"xsd:string\">007</MESSAGE_V2>\n"+
169                 " <MESSAGE_V3 xsi:type=\"xsd:string\">008</MESSAGE_V3>\n"+
170                 " <MESSAGE_V4 xsi:type=\"xsd:string\">009</MESSAGE_V4>\n"+
171                 " </RETURN>\n"+
172                 "</SrvResponse>";
173         deserialize(response,ret,true);
174     }
175     */

176
177     public static void main(String JavaDoc [] args) throws Exception JavaDoc
178     {
179         TestBeanDeser tester = new TestBeanDeser("test");
180         tester.testReturn();
181     }
182 }
183
Popular Tags