Debian Codegraph @

test/soap12/TestSer.java

raw
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;
20 import org.xml.sax.InputSource;
21 import test.encoding.Data;
22 import test.encoding.DataDeserFactory;
23 import test.encoding.DataSerFactory;
24
25 import javax.xml.namespace.QName;
26 import java.io.StringReader;
27 import java.io.StringWriter;
28 import java.io.Writer;
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 myNS = "urn:myNS";
37
38 public TestSer(String name) {
39 super(name);
40 }
41
42 public void testDataNoHrefs () throws Exception {
43 doTestData(false);
44 }
45
46 public void testDataWithHrefs () throws Exception {
47 doTestData(true);
48 }
49
50 public void doTestData (boolean multiref) throws Exception {
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("4.54");
61
62 RPCParam arg2 = new RPCParam("", "struct", data);
63 RPCElement body = new RPCElement("urn:myNamespace", "method1", new Object[]{ arg1, arg2 });
64 msg.addBodyElement(body);
65
66 Writer stringWriter = new StringWriter();
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[] {Constants.URI_SOAP12_ENC});
74 reg.register(Constants.URI_SOAP12_ENC, tm);
75
76 QName dataQName = new QName("typeNS", "Data");
77 tm.register(Data.class, dataQName, new DataSerFactory(), new DataDeserFactory());
78
79 msg.output(context);
80
81 String msgString = stringWriter.toString();
82
83 log.debug("---");
84 log.debug(msgString);
85 log.debug("---");
86
87 StringReader reader = new StringReader(msgString);
88
89 DeserializationContext dser = new DeserializationContext(
90 new InputSource(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 [] { "argument" });
116 env.addBodyElement(method);
117 String soapStr = env.toString();
118 } catch (Exception 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 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 stringWriter=new StringWriter();
140 StringReader reqReader = new StringReader(req);
141 InputSource reqSource = new InputSource(reqReader);
142
143 Document document = XMLUtils.newDocument(reqSource);
144
145 String 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[] { 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 e) {
161 fail(e.getMessage());
162 }
163 }
164 }