KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > test > encoding > TestRoundTrip


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
14 import javax.xml.namespace.QName JavaDoc;
15 import java.util.Vector JavaDoc;
16
17 /**
18  * Test round-trip serialization/deserialization of SOAP messages
19  */

20 public class TestRoundTrip extends TestCase {
21     private AxisServer server = new AxisServer();
22     private String JavaDoc header =
23             "<?xml version=\"1.0\"?>\n" +
24             "<SOAP-ENV:Envelope\n" +
25             "xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\"\n" +
26             "xmlns:SOAP-ENC=\"http://schemas.xmlsoap.org/soap/encoding/\"\n" +
27             "xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"\n" +
28             "xmlns:xsd-cr=\"http://www.w3.org/2000/10/XMLSchema\"\n" +
29             "xmlns:xsd-lc=\"http://www.w3.org/1999/XMLSchema\"\n" +
30             "xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n" +
31             "SOAP-ENV:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\">\n" +
32             "<SOAP-ENV:Body>\n";
33     private String JavaDoc footer =
34             "</SOAP-ENV:Body>\n" +
35             "</SOAP-ENV:Envelope>\n";
36     private String JavaDoc response =
37             "<ser-root:SrvResponse xmlns:ser-root=\"urn:test.encoding\">\n" +
38             " <ser-root:RETURN xsi:type=\"ser-root:RETURN\">\n" +
39             " <TYPE xsi:type=\"xsd:string\">000</TYPE>\n" +
40             " <ID xsi:type=\"xsd:string\">001</ID>\n" +
41             " <NUMBER xsi:type=\"xsd:string\">002</NUMBER>\n" +
42             " <MESSAGE xsi:type=\"xsd:string\">003</MESSAGE>\n" +
43             " <LOG_NO xsi:type=\"xsd:string\">004</LOG_NO>\n" +
44             " <LOG_MSG_NO xsi:type=\"xsd:string\">005</LOG_MSG_NO>\n" +
45             " <MESSAGE_V1 xsi:type=\"xsd:string\">006</MESSAGE_V1>\n" +
46             " <MESSAGE_V2 xsi:type=\"xsd:string\">007</MESSAGE_V2>\n" +
47             " <MESSAGE_V3 xsi:type=\"xsd:string\">008</MESSAGE_V3>\n" +
48             " <MESSAGE_V4 xsi:type=\"xsd:string\">009</MESSAGE_V4>\n" +
49             " </ser-root:RETURN>\n" +
50             "</ser-root:SrvResponse>";
51
52     public TestRoundTrip(String JavaDoc name) {
53         this(name, Constants.URI_DEFAULT_SCHEMA_XSI,
54                    Constants.URI_DEFAULT_SCHEMA_XSD);
55     }
56
57     public TestRoundTrip(String JavaDoc name, String JavaDoc NS_XSI, String JavaDoc NS_XSD) {
58         super(name);
59         TypeMappingRegistry tmr = server.getTypeMappingRegistry();
60         TypeMapping tm = (TypeMapping) tmr.createTypeMapping();
61         tm.setSupportedEncodings(new String JavaDoc[]{Constants.URI_DEFAULT_SOAP_ENC});
62         tmr.register(Constants.URI_DEFAULT_SOAP_ENC, tm);
63         tm.register(test.encoding.RETURN.class,
64                 new QName JavaDoc("urn:test.encoding", "RETURN"),
65                 new org.apache.axis.encoding.ser.BeanSerializerFactory(
66                         test.encoding.RETURN.class,
67                         new QName JavaDoc("urn:test.encoding", "RETURN")),
68                 new org.apache.axis.encoding.ser.BeanDeserializerFactory(
69                         test.encoding.RETURN.class,
70                         new QName JavaDoc("urn:test.encoding", "RETURN")));
71     }
72
73     // test if objects are equal
74
private static boolean equals(Object JavaDoc obj1, Object JavaDoc obj2) {
75         if ((obj1 == null) || (obj2 == null)) return (obj1 == obj2);
76         if (obj1.equals(obj2)) return true;
77         return false;
78     }
79
80     // Test RoundTrip
81
public void testRoundTrip() throws Exception JavaDoc {
82         checkRoundTrip(header + response + footer);
83     }
84
85     protected void checkRoundTrip(String JavaDoc xml1) throws Exception JavaDoc {
86         Message message = new Message(xml1);
87         message.setMessageContext(new MessageContext(server));
88
89         SOAPEnvelope envelope = (SOAPEnvelope) message.getSOAPEnvelope();
90         RPCElement body = (RPCElement) envelope.getFirstBody();
91         Vector JavaDoc arglist = body.getParams();
92         Object JavaDoc ret1 = ((RPCParam) arglist.get(0)).getObjectValue();
93
94         String JavaDoc xml2 = message.getSOAPPartAsString();
95         Message message2 = new Message(xml2);
96         message2.setMessageContext(new MessageContext(server));
97
98         SOAPEnvelope envelope2 = (SOAPEnvelope) message2.getSOAPEnvelope();
99         RPCElement body2 = (RPCElement) envelope2.getFirstBody();
100         Vector JavaDoc arglist2 = body2.getParams();
101         Object JavaDoc ret2 = ((RPCParam) arglist2.get(0)).getObjectValue();
102
103         if (!equals(ret1, ret2)) {
104             assertEquals("The result is not what is expected.", ret1, ret2);
105         }
106     }
107
108     public static void main(String JavaDoc[] args) throws Exception JavaDoc {
109         TestRoundTrip trip = new TestRoundTrip("Test RoundTrip");
110         trip.testRoundTrip();
111     }
112 }
113
Popular Tags