package test.soap12; import junit.framework.TestCase; import org.apache.axis.Constants; import org.apache.axis.Message; import org.apache.axis.MessageContext; import org.apache.axis.encoding.TypeMapping; import org.apache.axis.encoding.TypeMappingRegistry; import org.apache.axis.message.RPCElement; import org.apache.axis.message.RPCParam; import org.apache.axis.message.SOAPEnvelope; import org.apache.axis.server.AxisServer; import org.apache.axis.soap.SOAPConstants; import org.apache.axis.utils.JavaUtils; import org.apache.axis.utils.Messages; import javax.xml.namespace.QName; import java.lang.reflect.Array; import java.util.ArrayList; import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.Set; import java.util.Vector; /** * Test deserialization of SOAP responses */ public class TestDeser extends TestCase { private AxisServer server = null; public TestDeser(String name) { super(name); server = new AxisServer(); TypeMappingRegistry tmr = server.getTypeMappingRegistry(); TypeMapping tm = (TypeMapping) tmr.createTypeMapping(); tm.setSupportedEncodings(new String[] {Constants.URI_SOAP12_ENC}); tmr.register(Constants.URI_SOAP12_ENC, tm); tm.register(java.lang.String[].class, new QName("http://soapinterop.org/xsd", "ArrayOfString"), new org.apache.axis.encoding.ser.ArraySerializerFactory(), new org.apache.axis.encoding.ser.ArrayDeserializerFactory()); } private final String SOAP_HEAD = "\n" + "\n"; private final String ITEM = "abc\n"; private final String BODY_HEAD = "\n"; private final String METHOD_HEAD = "\n"; private final String METHOD_TAIL = "\n"; private final String BODY_TAIL = "\n"; private final String SOAP_TAIL = "\n"; private final String HEAD = SOAP_HEAD + BODY_HEAD + METHOD_HEAD; private final String TAIL = METHOD_TAIL + BODY_TAIL + SOAP_TAIL; private static boolean equals(Object obj1, Object obj2) { if ( (obj1 == null) || (obj2 == null) ) return (obj1 == obj2); if (obj1.equals(obj2)) return true; if (obj2.getClass().isArray() && obj1.getClass().isArray()) { if (Array.getLength(obj1) != Array.getLength(obj2)) return false; for (int i=0; i 0}", arglist.size()>0); RPCParam param = (RPCParam) arglist.get(0); assertNotNull("SOAP param should not be null", param); Object result = param.getObjectValue(); if (!equals(result, expected)) { // Try to convert to the expected class if (tryConvert) { Object result2 = JavaUtils.convert(result, expected.getClass()); if (!equals(result2, expected)) { assertEquals("The result is not what is expected.", expected, result); } } else { assertEquals("The result is not what is expected.", expected, result); } } } public void testDeser1() throws Exception { deserialize(SOAP_HEAD + BODY_HEAD + METHOD_HEAD + ITEM + METHOD_TAIL + BODY_TAIL + SOAP_TAIL, "abc", false); } public void testDeser2() throws Exception { boolean expectedExceptionThrown = false; try { deserialize(SOAP_HEAD + BODY_HEAD + METHOD_HEAD + ITEM + METHOD_TAIL + BODY_TAIL + "" + SOAP_TAIL, null, false); } catch (org.apache.axis.AxisFault af) { String expected = Messages.getMessage("noElemAfterBody12"); if(af.getFaultString().indexOf(expected)!=-1) expectedExceptionThrown = true; } assertTrue(expectedExceptionThrown); } public void testAfterBody() throws Exception { boolean expectedExceptionThrown = false; try { deserialize(SOAP_HEAD + BODY_HEAD + METHOD_HEAD + ITEM + METHOD_TAIL + "" + BODY_TAIL + SOAP_TAIL, null, false); } catch (org.apache.axis.AxisFault af) { // Should drop an ex about soap 1.2 doesn't permit any element after body String expected = Messages.getMessage("onlyOneBodyFor12"); if(af.getFaultString().indexOf(expected)!=-1) expectedExceptionThrown = true; } assertTrue(expectedExceptionThrown); } public void testArray() throws Exception { Vector v = new Vector(); v.addElement("abc"); v.addElement("def"); deserialize(HEAD + " " + "abc" + "def" + "" + TAIL, v, true); } public void testSparseArray1() throws Exception { ArrayList list = new ArrayList(4); list.add(null); list.add(null); list.add("abc"); list.add("def"); boolean expectedExceptionThrown = false; try { deserialize(HEAD + "" + "abc" + "def" + "" + TAIL, list, true); } catch (Exception af) { String expected = Messages.getMessage("noSparseArray"); if(af.toString().indexOf(expected)!=-1) expectedExceptionThrown = true; } assertTrue(expectedExceptionThrown); } public void testSparseArray2() throws Exception { ArrayList list = new ArrayList(4); list.add("abc"); list.add(null); list.add("def"); list.add(null); boolean expectedExceptionThrown = false; try { deserialize(HEAD + "" + "abc" + "def" + "" + TAIL, list, true); } catch (Exception af) { String expected = Messages.getMessage("noSparseArray"); if(af.toString().indexOf(expected)!=-1) expectedExceptionThrown = true; } assertTrue(expectedExceptionThrown); } public void testNoSizeDefinedArray() throws Exception { ArrayList a = new ArrayList(); a.add("abc"); a.add("def"); deserialize(HEAD + "" + "abc" + "def" + "" + TAIL, a, true); } }