package test.encoding;
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.utils.JavaUtils;
import samples.echo.SOAPStruct;
import samples.echo.SOAPStructStruct;
import javax.xml.namespace.QName;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Hashtable;
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 String header;
private String footer;
private AxisServer server = new AxisServer();
public TestDeser(String name) {
this(name, Constants.URI_DEFAULT_SCHEMA_XSI,
Constants.URI_DEFAULT_SCHEMA_XSD);
}
public TestDeser(String name, String NS_XSI, String NS_XSD) {
super(name);
header =
"\n" +
"\n" +
"\n" +
"\n";
footer =
"\n" +
"\n" +
"\n";
TypeMappingRegistry tmr = server.getTypeMappingRegistry();
TypeMapping tm = (TypeMapping) tmr.createTypeMapping();
tm.setSupportedEncodings(new String[] {Constants.URI_DEFAULT_SOAP_ENC});
tmr.register(Constants.URI_DEFAULT_SOAP_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());
tm.register(java.lang.Object[].class,
new QName("http://soapinterop.org/xsd", "ArrayOfObject"),
new org.apache.axis.encoding.ser.ArraySerializerFactory(),
new org.apache.axis.encoding.ser.ArrayDeserializerFactory());
tm.register(samples.echo.SOAPStruct.class,
new QName("http://soapinterop.org/xsd", "SOAPStruct"),
new org.apache.axis.encoding.ser.BeanSerializerFactory(
samples.echo.SOAPStruct.class,
new QName("http://soapinterop.org/xsd", "SOAPStruct")),
new org.apache.axis.encoding.ser.BeanDeserializerFactory(
samples.echo.SOAPStruct.class,
new QName("http://soapinterop.org/xsd", "SOAPStruct")));
tm.register(samples.echo.SOAPStruct[].class,
new QName("http://soapinterop.org/xsd", "ArrayOfSOAPStruct"),
new org.apache.axis.encoding.ser.ArraySerializerFactory(),
new org.apache.axis.encoding.ser.ArrayDeserializerFactory());
tm.register(samples.echo.SOAPStructStruct.class,
new QName("http://soapinterop.org/xsd", "SOAPStructStruct"),
new org.apache.axis.encoding.ser.BeanSerializerFactory(
samples.echo.SOAPStructStruct.class,
new QName("http://soapinterop.org/xsd", "SOAPStructStruct")),
new org.apache.axis.encoding.ser.BeanDeserializerFactory(
samples.echo.SOAPStructStruct.class,
new QName("http://soapinterop.org/xsd", "SOAPStructStruct")));
tm.register(test.encoding.IndexPropBean.class,
new QName("http://encoding.test", "IndexPropBean"),
new org.apache.axis.encoding.ser.BeanSerializerFactory(
test.encoding.IndexPropBean.class,
new QName("http://encoding.test", "IndexPropBean")),
new org.apache.axis.encoding.ser.BeanDeserializerFactory(
test.encoding.IndexPropBean.class,
new QName("http://encoding.test", "IndexPropBean")));
}
/**
* Verify that two objects have the same value, handling arrays...
*/
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}" + postfix, arglist.size()>0);
RPCParam param = (RPCParam) arglist.get(0);
assertNotNull("SOAP param should not be null" + postfix, param);
Object result = param.getObjectValue();
if (!equals(result, expected)) {
String errorText = "Failed to decode " + data + postfix+" : ";
// Try to convert to the expected class
if (tryConvert) {
Object result2 = JavaUtils.convert(result, expected.getClass());
if (!equals(result2, expected)) {
assertEquals(errorText, expected, result);
}
} else {
assertEquals(errorText, expected, result);
}
}
}
public void testString() throws Exception {
deserialize("abc",
"abc");
}
public void testBoolean() throws Exception {
deserialize("false",
new Boolean(false));
deserialize("true",
new Boolean(true));
deserialize("0",
new Boolean(false));
deserialize("1",
new Boolean(true));
}
public void testDouble() throws Exception {
deserialize("3.14",
new Double(3.14));
}
public void testDoubleNaN() throws Exception {
deserialize("NaN",
new Double(Double.NaN));
}
public void testDoubleINF() throws Exception {
deserialize("INF",
new Double(Double.POSITIVE_INFINITY));
}
public void testDoubleNINF() throws Exception {
deserialize("-INF",
new Double(Double.NEGATIVE_INFINITY));
}
public void testFloat() throws Exception {
deserialize("3.14",
new Float(3.14F));
}
public void testFloatNaN() throws Exception {
deserialize("NaN",
new Float(Float.NaN));
}
public void testFloatINF() throws Exception {
deserialize("INF",
new Float(Float.POSITIVE_INFINITY));
}
public void testFloatNINF() throws Exception {
deserialize("-INF",
new Float(Float.NEGATIVE_INFINITY));
}
public void testInt() throws Exception {
deserialize("10",
new Integer(10));
}
public void testLong() throws Exception {
deserialize("17",
new Long(17));
}
public void testShort() throws Exception {
deserialize("3",
new Short((short)3));
}
public void testQName() throws Exception {
String ns = "http://test";
String local = "bar";
deserialize("foo:" + local + "",
new QName(ns, local));
deserialize("\n foo:" + local + " \r\n ",
new QName(ns, local));
}
public void testArray() throws Exception {
Vector v = new Vector();
v.addElement("abc");
v.addElement("def");
deserialize(" " +
"- abc
" +
"- def
" +
"",
v, true);
}
public void testSparseArray1() throws Exception {
ArrayList list = new ArrayList(4);
list.add(null);
list.add(null);
list.add("abc");
list.add("def");
deserialize(" " +
"- abc
" +
"- def
" +
"",
list, true);
}
public void testSparseArray2() throws Exception {
ArrayList list = new ArrayList(4);
list.add("abc");
list.add(null);
list.add("def");
deserialize(" " +
"- abc
" +
"- def
" +
"",
list, true);
}
public void testHugeSparseArray() throws Exception {
ArrayList list = new ArrayList(4);
list.add("abc");
list.add(null);
list.add("def");
deserialize(" " +
"- abc
" +
"- def
" +
"",
list, true);
}
public void testMap() throws Exception {
HashMap m = new HashMap();
m.put("abcKey", "abcVal");
m.put("defKey", "defVal");
deserialize(" " +
"- " +
"abcKey" +
"abcVal" +
"
- " +
"defKey" +
"defVal" +
"
" +
"",
m);
}
public void testHashtable() throws Exception {
Hashtable ht = new Hashtable();
ht.put("abcKey", "abcVal");
ht.put("defKey", "defVal");
deserialize(" " +
"- " +
"abcKey" +
"abcVal" +
"
- " +
"defKey" +
"defVal" +
"
" +
"",
ht, true);
}
public void testUntyped() throws Exception {
deserialize("10", "10");
}
public void testSOAPString() throws Exception {
deserialize("abc",
"abc");
}
// Complicated array tests
// type=soapenc:Array
public void testArrayA() throws Exception {
String[] s = new String[] {"abc", "def"};
deserialize(" " +
"- abc
" +
"- def
" +
"",
s, true);
}
// Like above but missing [2] dimension
public void testArrayB() throws Exception {
String[] s = new String[] {"abc", "def"};
deserialize(" " +
"- abc
" +
"- def
" +
"",
s, true);
}
// Like above but no xsi:type on elements
public void testArrayC() throws Exception {
String[] s = new String[] {"abc", "def"};
deserialize(" " +
"- abc
" +
"- def
" +
"",
s, true);
}
// Now try with arrayType=xsd:anyType
public void testArrayD() throws Exception {
String[] s = new String[] {"abc", "def"};
deserialize(" " +
"- abc
" +
"- def
" +
"",
s, true);
}
// Now try without arrayType
public void testArrayE() throws Exception {
String[] s = new String[] {"abc", "def"};
deserialize(" " +
"- abc
" +
"- def
" +
"",
s, true);
}
// Use a specific array type, not soapenc:Array
public void testArrayF() throws Exception {
String[] s = new String[] {"abc", "def"};
deserialize(" " +
"- abc
" +
"- def
" +
"",
s, true);
}
// Same as above without individual item types
public void testArrayG() throws Exception {
String[] s = new String[] {"abc", "def"};
deserialize(" " +
"- abc
" +
"- def
" +
"",
s, true);
}
// Same as above except result is an Object[]
public void testArrayH() throws Exception {
Object[] s = new Object[] {new String("abc"), new String("def")};
deserialize(" " +
"- abc
" +
"- def
" +
"",
s, true);
}
// Ooh La La
// Array of Object containing a String and an Integer
public void testArrayI() throws Exception {
Object[] s = new Object[] {new String("abc"), new Integer(123)};
deserialize(" " +
"- abc
" +
"- 123
" +
"",
s, true);
}
// Same as above using arrayType=xsd:anyType
public void testArrayJ() throws Exception {
Object[] s = new Object[] {new String("abc"), new Integer(123)};
deserialize(" " +
"- abc
" +
"- 123
" +
"",
s, true);
}
// Same as above using type=soapenc:Array and no arrayType...bare bones
public void testArrayK() throws Exception {
Object[] s = new Object[] {new String("abc"), new Integer(123)};
deserialize(" " +
"- abc
" +
"- 123
" +
"",
s, true);
}
// This was created from a return received from a .NET service
public void testArrayL() throws Exception {
SOAPStruct[] s = new SOAPStruct[] {
new SOAPStruct(),
new SOAPStruct(),
new SOAPStruct()};
s[0].setVarInt(1);
s[0].setVarString("one");
s[0].setVarFloat(1.1F);
s[1].setVarInt(2);
s[1].setVarString("two");
s[1].setVarFloat(2.2F);
s[2].setVarInt(3);
s[2].setVarString("three");
s[2].setVarFloat(3.3F);
deserialize("" +
"- " +
"
- " +
"
- " +
"
" +
"" +
"one" +
"1" +
"1.1" +
"" +
"" +
"two" +
"2" +
"2.2" +
"" +
"" +
"three" +
"3" +
"3.3" +
"",
s, true);
}
// Like above without multiref
public void testArrayM() throws Exception {
SOAPStruct[] s = new SOAPStruct[] {
new SOAPStruct(),
new SOAPStruct(),
new SOAPStruct()};
s[0].setVarInt(1);
s[0].setVarString("one");
s[0].setVarFloat(1.1F);
s[1].setVarInt(2);
s[1].setVarString("two");
s[1].setVarFloat(2.2F);
s[2].setVarInt(3);
s[2].setVarString("three");
s[2].setVarFloat(3.3F);
deserialize("" +
"" +
"one" +
"1" +
"1.1" +
"" +
"" +
"two" +
"2" +
"2.2" +
"" +
"" +
"three" +
"3" +
"3.3" +
"" +
"",
s, true);
}
// Deserialize Bean with discreet names
public void testBeanWithIndexedPropA() throws Exception {
IndexPropBean s = new IndexPropBean();
s.setName(new String[] {"hello", "goodbye"});
deserialize(" " +
"" +
"" +
"" +
"- hello
" +
"- goodbye
",
s, true);
}
// Deserialize Bean with names in an array
public void testBeanWithIndexedPropB() throws Exception {
IndexPropBean s = new IndexPropBean();
s.setName(new String[] {"hello", "goodbye"});
deserialize(" " +
" " +
"" +
" " +
"- hello
" +
"- goodbye
" +
"",
s, true);
}
// Struct within Struct
public void testStructStruct() throws Exception {
SOAPStruct s = new samples.echo.SOAPStruct();
s.setVarInt(1);
s.setVarString("one");
s.setVarFloat(1.1F);
SOAPStructStruct ss = new SOAPStructStruct();
ss.setVarString("hello");
ss.setVarInt(2);
ss.setVarFloat(2.2F);
ss.setVarStruct(s);
deserialize("" +
"hello" +
"2" +
"2.2" +
"" +
"one" +
"1" +
"1.1" +
"" +
"" , ss, true);
}
/*
public void testBug18390() throws Exception {
String[] s = new String[] {"GW671055X"};
deserialize(" " +
"GW671055X" +
"",
s, true);
}
public void testBug25536() throws Exception {
int[] array = new int[]{3,4};
deserialize("\n" +
" 3 \n" +
" 4 \n" +
"",array,true);
}
*/
// Struct within Struct
public void testStructStruct2() throws Exception {
SOAPStruct s = new samples.echo.SOAPStruct();
s.setVarInt(1);
s.setVarString("one");
s.setVarFloat(1.1F);
SOAPStructStruct ss = new SOAPStructStruct();
ss.setVarString("hello");
ss.setVarInt(2);
ss.setVarFloat(2.2F);
ss.setVarStruct(s);
deserialize("" +
"hello" +
"2" +
"2.2" +
"" +
"one" +
"1" +
"1.1" +
"" +
"" , ss, true);
}
}