KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > test > encoding > TestXsiType


1 package test.encoding;
2
3 import junit.framework.TestCase;
4 import org.apache.axis.AxisEngine;
5 import org.apache.axis.MessageContext;
6 import org.apache.axis.client.Call;
7 import org.apache.axis.client.Service;
8 import org.apache.axis.configuration.SimpleProvider;
9 import org.apache.axis.encoding.SerializationContext;
10 import org.apache.axis.encoding.SerializationContext;
11 import org.apache.axis.encoding.XMLType;
12 import org.apache.axis.handlers.soap.SOAPService;
13 import org.apache.axis.message.RPCElement;
14 import org.apache.axis.message.RPCParam;
15 import org.apache.axis.message.SOAPEnvelope;
16 import org.apache.axis.providers.java.RPCProvider;
17 import org.apache.axis.server.AxisServer;
18 import org.apache.axis.transport.local.LocalTransport;
19
20 import java.io.StringWriter JavaDoc;
21 import java.io.Writer JavaDoc;
22
23 /**
24  * Verify that shutting off xsi:types in the Message Context works
25  * as expected.
26  */

27 public class TestXsiType extends TestCase {
28     private SimpleProvider provider = new SimpleProvider();
29     private AxisServer server = new AxisServer(provider);
30     
31     public TestXsiType()
32     {
33         super("testing");
34     }
35
36     public TestXsiType(String JavaDoc name) {
37         super(name);
38     }
39
40     /**
41      * Trivial test just to make sure there aren't xsi:type attributes
42      */

43     public void testNoXsiTypes()
44        throws Exception JavaDoc
45     {
46         MessageContext msgContext = new MessageContext(server);
47
48         // Don't serialize xsi:type attributes
49
msgContext.setProperty(Call.SEND_TYPE_ATTR, "false" );
50
51         SOAPEnvelope msg = new SOAPEnvelope();
52         RPCParam arg1 = new RPCParam("urn:myNamespace",
53                                      "testParam",
54                                      "this is a string");
55         RPCElement body = new RPCElement("urn:myNamespace",
56                                          "method1",
57                                          new Object JavaDoc[]{ arg1 });
58         msg.addBodyElement(body);
59
60         Writer JavaDoc stringWriter = new StringWriter JavaDoc();
61         SerializationContext context = new SerializationContext(stringWriter,
62                                                                     msgContext);
63
64         msg.output(context);
65
66         String JavaDoc msgString = stringWriter.toString();
67         assertTrue("Found unexpected xsi:type!",
68                    msgString.indexOf("xsi:type") == -1);
69     }
70     
71     /**
72      * More complex test which checks to confirm that we can still
73      * in fact deserialize if we know the type via the Call object.
74      */

75     public void testTypelessDeserialization() throws Exception JavaDoc
76     {
77         server.setOption(AxisEngine.PROP_SEND_XSI, Boolean.FALSE);
78         
79         SOAPService service = new SOAPService(new RPCProvider());
80         service.setOption("className", "test.encoding.TestXsiType");
81         service.setOption("allowedMethods", "*");
82         provider.deployService("TestService", service);
83         
84         // Call that same server, accessing a method we know returns
85
// a double. We should figure this out and deserialize it
86
// correctly, even without the xsi:type attribute, because
87
// we set the return type manually.
88

89         Service S_service = new Service();
90         Call call = (Call) S_service.createCall();
91         call.setTransport(new LocalTransport(server));
92         call.setReturnType(XMLType.XSD_DOUBLE);
93         
94         Object JavaDoc result = call.invoke("TestService",
95                                     "serviceMethod",
96                                     new Object JavaDoc [] {});
97         
98         assertTrue("Return value (" + result.getClass().getName() +
99                    ") was not the expected type (Double)!",
100                     (result instanceof Double JavaDoc));
101     }
102     
103     /**
104      * A method for our service, returning a double.
105      */

106     public double serviceMethod()
107     {
108         return 3.14159;
109     }
110
111     public static void main(String JavaDoc [] args) throws Exception JavaDoc
112     {
113         TestXsiType tester = new TestXsiType("test");
114         tester.testNoXsiTypes();
115         tester.testTypelessDeserialization();
116     }
117 }
118
Popular Tags