KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > test > soap12 > TestSer


1 package test.soap12;
2
3 import junit.framework.TestCase;
4 import org.apache.axis.Constants;
5 import org.apache.axis.MessageContext;
6 import org.apache.axis.components.logger.LogFactory;
7 import org.apache.axis.encoding.DeserializationContext;
8 import org.apache.axis.encoding.SerializationContext;
9 import org.apache.axis.encoding.SerializationContext;
10 import org.apache.axis.encoding.TypeMapping;
11 import org.apache.axis.encoding.TypeMappingRegistry;
12 import org.apache.axis.message.RPCElement;
13 import org.apache.axis.message.RPCParam;
14 import org.apache.axis.message.SOAPEnvelope;
15 import org.apache.axis.server.AxisServer;
16 import org.apache.axis.soap.SOAPConstants;
17 import org.apache.axis.utils.XMLUtils;
18 import org.apache.commons.logging.Log;
19 import org.w3c.dom.Document JavaDoc;
20 import org.xml.sax.InputSource JavaDoc;
21 import test.encoding.Data;
22 import test.encoding.DataDeserFactory;
23 import test.encoding.DataSerFactory;
24
25 import javax.xml.namespace.QName JavaDoc;
26 import java.io.StringReader JavaDoc;
27 import java.io.StringWriter JavaDoc;
28 import java.io.Writer JavaDoc;
29
30 /** Little serialization test with a struct.
31  */

32 public class TestSer extends TestCase {
33     Log log =
34             LogFactory.getLog(TestSer.class.getName());
35
36     public static final String JavaDoc myNS = "urn:myNS";
37
38     public TestSer(String JavaDoc name) {
39         super(name);
40     }
41
42     public void testDataNoHrefs () throws Exception JavaDoc {
43         doTestData(false);
44     }
45
46     public void testDataWithHrefs () throws Exception JavaDoc {
47         doTestData(true);
48     }
49
50     public void doTestData (boolean multiref) throws Exception JavaDoc {
51         MessageContext msgContext = new MessageContext(new AxisServer());
52         msgContext.setSOAPConstants(SOAPConstants.SOAP12_CONSTANTS);
53         msgContext.setProperty(Constants.MC_NO_OPERATION_OK, Boolean.TRUE);
54         
55         SOAPEnvelope msg = new SOAPEnvelope(SOAPConstants.SOAP12_CONSTANTS);
56         RPCParam arg1 = new RPCParam("urn:myNamespace", "testParam", "this is a string");
57
58         Data data = new Data();
59         data.stringMember = "String member";
60         data.floatMember = new Float JavaDoc("4.54");
61
62         RPCParam arg2 = new RPCParam("", "struct", data);
63         RPCElement body = new RPCElement("urn:myNamespace", "method1", new Object JavaDoc[]{ arg1, arg2 });
64         msg.addBodyElement(body);
65
66         Writer JavaDoc stringWriter = new StringWriter JavaDoc();
67         SerializationContext context = new SerializationContext(stringWriter, msgContext);
68         context.setDoMultiRefs(multiref);
69
70         // Create a TypeMapping and register the specialized Type Mapping
71
TypeMappingRegistry reg = context.getTypeMappingRegistry();
72         TypeMapping tm = (TypeMapping) reg.createTypeMapping();
73         tm.setSupportedEncodings(new String JavaDoc[] {Constants.URI_SOAP12_ENC});
74         reg.register(Constants.URI_SOAP12_ENC, tm);
75
76         QName JavaDoc dataQName = new QName JavaDoc("typeNS", "Data");
77         tm.register(Data.class, dataQName, new DataSerFactory(), new DataDeserFactory());
78
79         msg.output(context);
80
81         String JavaDoc msgString = stringWriter.toString();
82
83         log.debug("---");
84         log.debug(msgString);
85         log.debug("---");
86
87         StringReader JavaDoc reader = new StringReader JavaDoc(msgString);
88
89         DeserializationContext dser = new DeserializationContext(
90             new InputSource JavaDoc(reader), msgContext, org.apache.axis.Message.REQUEST);
91         dser.parse();
92
93         SOAPEnvelope env = dser.getEnvelope();
94         RPCElement rpcElem = (RPCElement)env.getFirstBody();
95         RPCParam struct = rpcElem.getParam("struct");
96         assertNotNull("No <struct> param", struct);
97
98         Data val = (Data)struct.getObjectValue();
99         assertNotNull("No value for struct param", val);
100
101         assertEquals("Data and Val string members are not equal", data.stringMember, val.stringMember);
102         assertEquals("Data and Val float members are not equal",data.floatMember.floatValue(),
103                      val.floatMember.floatValue(), 0.00001F);
104     }
105
106     /**
107      * Test RPC element serialization when we have no MessageContext
108      */

109     public void testRPCElement()
110     {
111         try {
112             SOAPEnvelope env = new SOAPEnvelope();
113             RPCElement method = new RPCElement("ns",
114                                                "method",
115                                                new Object JavaDoc [] { "argument" });
116             env.addBodyElement(method);
117             String JavaDoc soapStr = env.toString();
118         } catch (Exception JavaDoc e) {
119             fail(e.getMessage());
120         }
121
122         // If there was no exception, we succeeded in serializing it.
123
}
124
125     public void testEmptyXMLNS()
126     {
127         try {
128             MessageContext msgContext = new MessageContext(new AxisServer());
129             msgContext.setSOAPConstants(SOAPConstants.SOAP12_CONSTANTS);
130             msgContext.setProperty(Constants.MC_NO_OPERATION_OK, Boolean.TRUE);
131             
132             String JavaDoc req =
133                 "<xsd1:A xmlns:xsd1=\"urn:myNamespace\">"
134                     + "<xsd1:B>"
135                     + "<xsd1:C>foo bar</xsd1:C>"
136                     + "</xsd1:B>"
137                     + "</xsd1:A>";
138
139             StringWriter JavaDoc stringWriter=new StringWriter JavaDoc();
140             StringReader JavaDoc reqReader = new StringReader JavaDoc(req);
141             InputSource JavaDoc reqSource = new InputSource JavaDoc(reqReader);
142
143             Document JavaDoc document = XMLUtils.newDocument(reqSource);
144
145             String JavaDoc msgString = null;
146
147             SOAPEnvelope msg = new SOAPEnvelope(SOAPConstants.SOAP12_CONSTANTS);
148             RPCParam arg1 = new RPCParam("urn:myNamespace", "testParam", document.getFirstChild());
149             arg1.setXSITypeGeneration(Boolean.FALSE);
150
151             RPCElement body = new RPCElement("urn:myNamespace", "method1", new Object JavaDoc[] { arg1 });
152             msg.addBodyElement(body);
153             body.setEncodingStyle(Constants.URI_LITERAL_ENC);
154
155             SerializationContext context = new SerializationContext(stringWriter, msgContext);
156             msg.output(context);
157
158             msgString = stringWriter.toString();
159             assertTrue(msgString.indexOf("xmlns=\"\"")==-1);
160         } catch (Exception JavaDoc e) {
161             fail(e.getMessage());
162         }
163     }
164 }
165
Popular Tags