Debian Codegraph @

test/encoding/TestSer.java

raw
1 package test.encoding;
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.utils.XMLUtils;
17 import org.apache.commons.logging.Log;
18 import org.w3c.dom.Document;
19 import org.w3c.dom.Element;
20 import org.w3c.dom.Node;
21 import org.w3c.dom.NodeList;
22 import org.xml.sax.InputSource;
23 import org.xml.sax.helpers.AttributesImpl;
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 SOAPEnvelope msg = new SOAPEnvelope();
53 RPCParam arg1 = new RPCParam("urn:myNamespace", "testParam", "this is a string");
54
55 Data data = new Data();
56 data.stringMember = "String member";
57 data.floatMember = new Float("4.54");
58
59 RPCParam arg2 = new RPCParam("", "struct", data);
60 RPCElement body = new RPCElement("urn:myNamespace", "method1", new Object[]{ arg1, arg2 });
61 msg.addBodyElement(body);
62
63 Writer stringWriter = new StringWriter();
64 SerializationContext context = new SerializationContext(stringWriter, msgContext);
65 context.setDoMultiRefs(multiref);
66
67 // Create a TypeMapping and register the specialized Type Mapping
68 TypeMappingRegistry reg = context.getTypeMappingRegistry();
69 TypeMapping tm = (TypeMapping) reg.createTypeMapping();
70 tm.setSupportedEncodings(new String[] {Constants.URI_DEFAULT_SOAP_ENC});
71 reg.register(Constants.URI_DEFAULT_SOAP_ENC, tm);
72
73 QName dataQName = new QName("typeNS", "Data");
74 tm.register(Data.class, dataQName, new DataSerFactory(), new DataDeserFactory());
75
76 msg.output(context);
77
78 String msgString = stringWriter.toString();
79
80 log.debug("---");
81 log.debug(msgString);
82 log.debug("---");
83
84 StringReader reader = new StringReader(msgString);
85
86 DeserializationContext dser = new DeserializationContext(
87 new InputSource(reader), msgContext, org.apache.axis.Message.REQUEST);
88 dser.parse();
89
90 SOAPEnvelope env = dser.getEnvelope();
91 RPCElement rpcElem = (RPCElement)env.getFirstBody();
92 RPCParam struct = rpcElem.getParam("struct");
93 assertNotNull("No <struct> param", struct);
94
95 Data val = (Data)struct.getObjectValue();
96 assertNotNull("No value for struct param", val);
97
98 assertEquals("Data and Val string members are not equal", data.stringMember, val.stringMember);
99 assertEquals("Data and Val float members are not equal",data.floatMember.floatValue(),
100 val.floatMember.floatValue(), 0.00001F);
101 }
102
103 public void testAttributeQNames() throws Exception {
104 String NS1 = "urn:foo";
105 MessageContext context = new MessageContext(new AxisServer());
106 StringWriter writer = new StringWriter();
107 SerializationContext ser = new SerializationContext(writer);
108 ser.registerPrefixForURI("", NS1);
109 ser.startElement(new QName(NS1, "e1"), null);
110 String foo = ser.attributeQName2String(new QName(NS1, "attr"));
111 AttributesImpl attrs = new AttributesImpl();
112 attrs.addAttribute("a", "a", "a", "CDATA", foo);
113 ser.startElement(new QName(NS1, "e2"), attrs);
114 ser.endElement();
115 foo = ser.attributeQName2String(new QName(NS1, "attr"));
116 attrs = new AttributesImpl();
117 attrs.addAttribute("a", "a", "a", "CDATA", foo);
118 ser.startElement(new QName(NS1, "e3"), null);
119 ser.endElement();
120 ser.endElement();
121 System.out.println(writer.getBuffer().toString());
122 }
123
124 /**
125 * Test RPC element serialization when we have no MessageContext
126 */
127 public void testRPCElement()
128 {
129 try {
130 SOAPEnvelope env = new SOAPEnvelope();
131 RPCElement method = new RPCElement("ns",
132 "method",
133 new Object [] { "argument" });
134 env.addBodyElement(method);
135 env.toString();
136 } catch (Exception e) {
137 fail(e.getMessage());
138 }
139
140 // If there was no exception, we succeeded in serializing it.
141 }
142
143 public void testEmptyXMLNS() throws Exception
144 {
145 MessageContext msgContext = new MessageContext(new AxisServer());
146 String req =
147 "<xsd1:A xmlns:xsd1=\"urn:myNamespace\">"
148 + "<xsd1:B>"
149 + "<xsd1:C>foo bar</xsd1:C>"
150 + "</xsd1:B>"
151 + "</xsd1:A>";
152
153 StringWriter stringWriter=new StringWriter();
154 StringReader reqReader = new StringReader(req);
155 InputSource reqSource = new InputSource(reqReader);
156
157 Document document = XMLUtils.newDocument(reqSource);
158
159 String msgString = null;
160
161 SOAPEnvelope msg = new SOAPEnvelope();
162 RPCParam arg1 = new RPCParam("urn:myNamespace", "testParam", document.getFirstChild());
163 arg1.setXSITypeGeneration(Boolean.FALSE);
164
165 RPCElement body = new RPCElement("urn:myNamespace", "method1", new Object[] { arg1 });
166 msg.addBodyElement(body);
167 body.setEncodingStyle(Constants.URI_LITERAL_ENC);
168
169 SerializationContext context = new SerializationContext(stringWriter, msgContext);
170 msg.output(context);
171
172 msgString = stringWriter.toString();
173 assertTrue(msgString.indexOf("xmlns=\"\"")==-1);
174 }
175
176 /**
177 * Confirm that default namespaces when writing doc/lit messages don't
178 * trample namespace mappings.
179 *
180 * @throws Exception
181 */
182 public void testDefaultNamespace() throws Exception
183 {
184 MessageContext msgContext = new MessageContext(new AxisServer());
185 String req =
186 "<xsd1:A xmlns:xsd1=\"urn:myNamespace\">"
187 + "<B>" // Note that B and C are in no namespace!
188 + "<C>foo bar</C>"
189 + "</B>"
190 + "</xsd1:A>";
191
192 StringWriter stringWriter=new StringWriter();
193 StringReader reqReader = new StringReader(req);
194 InputSource reqSource = new InputSource(reqReader);
195
196 Document document = XMLUtils.newDocument(reqSource);
197
198 String msgString = null;
199
200 SOAPEnvelope msg = new SOAPEnvelope();
201 RPCParam arg1 = new RPCParam("urn:myNamespace", "testParam", document.getFirstChild());
202 arg1.setXSITypeGeneration(Boolean.FALSE);
203
204 RPCElement body = new RPCElement("urn:myNamespace", "method1", new Object[] { arg1 });
205 msg.addBodyElement(body);
206 body.setEncodingStyle(Constants.URI_LITERAL_ENC);
207
208 SerializationContext context = new SerializationContext(stringWriter, msgContext);
209 msg.output(context);
210
211 msgString = stringWriter.toString();
212
213 // Now reparse into DOM so we can check namespaces.
214 StringReader resReader = new StringReader(msgString);
215 InputSource resSource = new InputSource(resReader);
216 Document doc = XMLUtils.newDocument(resSource);
217
218 // Go make sure B and C are in fact in no namespace
219 Element el = findChildElementByLocalName(doc.getDocumentElement(),
220 "B");
221 assertNotNull("Couldn't find <B> element!", el);
222 assertNull("Element <B> has a namespace!", el.getNamespaceURI());
223 el = findChildElementByLocalName(el, "C");
224 assertNotNull("Couldn't find <C> element!", el);
225 assertNull("Element <C> has a namespace!", el.getNamespaceURI());
226 }
227
228 private Element findChildElementByLocalName(Element src, String localName) {
229 NodeList nl = src.getChildNodes();
230 for (int i = 0; i < nl.getLength(); i++) {
231 Node node = nl.item(i);
232 if (node instanceof Element) {
233 Element e = (Element)node;
234 if (e.getLocalName().equals(localName)) {
235 return e;
236 }
237 // Depth-first search
238 e = findChildElementByLocalName(e, localName);
239 if (e != null) {
240 return e;
241 }
242 }
243 }
244 return null;
245 }
246 }