FileDocCategorySizeDatePackage
TestDeser.javaAPI DocApache Axis 1.49865Sat Apr 22 18:57:26 BST 2006test.soap12

TestDeser

public class TestDeser extends TestCase
Test deserialization of SOAP responses

Fields Summary
private org.apache.axis.server.AxisServer
server
private final String
SOAP_HEAD
private final String
ITEM
private final String
BODY_HEAD
private final String
METHOD_HEAD
private final String
METHOD_TAIL
private final String
BODY_TAIL
private final String
SOAP_TAIL
private final String
HEAD
private final String
TAIL
Constructors Summary
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());
    
Methods Summary
public voiddeserialize(java.lang.String soap, java.lang.Object expected, boolean tryConvert)

        Message message = new Message(soap);
        MessageContext context = new MessageContext(server);
        context.setSOAPConstants(SOAPConstants.SOAP12_CONSTANTS);
        context.setProperty(Constants.MC_NO_OPERATION_OK, Boolean.TRUE);

        message.setMessageContext(context);

        SOAPEnvelope envelope = message.getSOAPEnvelope();
        assertNotNull("SOAP envelope should not be null", envelope);

        RPCElement body = (RPCElement)envelope.getFirstBody();
        assertNotNull("SOAP body should not be null", body);

        Vector arglist = body.getParams();
        assertNotNull("arglist", arglist);
        assertTrue("param.size()<=0 {Should be > 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);
           }
        }
    
private static booleanequals(java.lang.Object obj1, java.lang.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<Array.getLength(obj1); i++)
               if (!equals(Array.get(obj1,i),Array.get(obj2,i))) return false;
           return true;
       }
       if ((obj1 instanceof List) && (obj2 instanceof List)) {
           List list1 = (List)obj1;
           List list2 = (List)obj2;
           if (list1.size() != list2.size()) return false;
           for (int i=0; i < list1.size(); i++) {
               if (!equals(list1.get(i), list2.get(i))) return false;
           }
           return true;
       }
       if ((obj1 instanceof Map) && (obj2 instanceof Map)) {
           Map map1 = (Map)obj1;
           Map map2 = (Map)obj2;
           Set keys1 = map1.keySet();
           Set keys2 = map2.keySet();
           if (!(keys1.equals(keys2))) return false;
           Iterator i = keys1.iterator();
           while (i.hasNext()) {
               Object key = i.next();
               if (!map1.get(key).equals(map2.get(key)))
                   return false;
           }
           return true;
       }

       return false;
    
public voidtestAfterBody()

        boolean expectedExceptionThrown = false;
        try {
            deserialize(SOAP_HEAD + BODY_HEAD + METHOD_HEAD + ITEM + METHOD_TAIL + "<hello/>" + 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 voidtestArray()

        Vector v = new Vector();
        v.addElement("abc");
        v.addElement("def");
        deserialize(HEAD +
                    "<result xsi:type=\"soapenc:Array\" " +
                            "soapenc:itemType=\"xsd:string\" soapenc:arraySize=\"2\"> " +
                       "<item xsi:type=\"xsd:string\">abc</item>" +
                       "<item xsi:type=\"xsd:string\">def</item>" +
                    "</result>" +
                    TAIL,
                    v, true);
    
public voidtestDeser1()

        deserialize(SOAP_HEAD + BODY_HEAD + METHOD_HEAD + ITEM + METHOD_TAIL + BODY_TAIL + SOAP_TAIL, "abc", false);
    
public voidtestDeser2()

        boolean expectedExceptionThrown = false;
        try {
            deserialize(SOAP_HEAD + BODY_HEAD + METHOD_HEAD + ITEM + METHOD_TAIL + BODY_TAIL + "<hello/>" + 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 voidtestNoSizeDefinedArray()

        ArrayList a = new ArrayList();
        a.add("abc");
        a.add("def");

        deserialize(HEAD +
                    "<result xsi:type=\"soapenc:Array\" " +
                    "soapenc:itemType=\"xsd:string\" soapenc:arraySize=\"*\">" +
                        "<item xsi:type=\"xsd:string\">abc</item>" +
                        "<item xsi:type=\"xsd:string\">def</item>" +
                    "</result>" +
                    TAIL,
                    a, true);
    
public voidtestSparseArray1()

        ArrayList list = new ArrayList(4);
        list.add(null);
        list.add(null);
        list.add("abc");
        list.add("def");
        boolean expectedExceptionThrown = false;
        try {
            deserialize(HEAD +
                        "<result xsi:type=\"soapenc:Array\" " +
                                "soapenc:itemType=\"xsd:string\" soapenc:arraySize=\"4\" " +
                                " soapenc:offset=\"[2]\">" +
                           "<item xsi:type=\"xsd:string\">abc</item>" +
                           "<item xsi:type=\"xsd:string\">def</item>" +
                        "</result>" +
                        TAIL,
                        list, true);
        } catch (Exception af) {
            String expected = Messages.getMessage("noSparseArray");
            if(af.toString().indexOf(expected)!=-1)
                expectedExceptionThrown = true;
        }
        assertTrue(expectedExceptionThrown);
    
public voidtestSparseArray2()

        ArrayList list = new ArrayList(4);
        list.add("abc");
        list.add(null);
        list.add("def");
        list.add(null);

        boolean expectedExceptionThrown = false;
        try {
            deserialize(HEAD +
                        "<result xsi:type=\"soapenc:Array\" " +
                        "soapenc:itemType=\"xsd:string\" soapenc:arraySize=\"4\">" +
                        "<item soapenc:position=\"[0]\" xsi:type=\"xsd:string\">abc</item>" +
                        "<item soapenc:position=\"[2]\" xsi:type=\"xsd:string\">def</item>" +
                        "</result>" +
                        TAIL,
                        list, true);
        } catch (Exception af) {
            String expected = Messages.getMessage("noSparseArray");
            if(af.toString().indexOf(expected)!=-1)
                expectedExceptionThrown = true;
        }
        assertTrue(expectedExceptionThrown);