FileDocCategorySizeDatePackage
TestSer.javaAPI DocApache Axis 1.46091Sat Apr 22 18:57:28 BST 2006test.soap12

TestSer

public class TestSer extends junit.framework.TestCase
Little serialization test with a struct.

Fields Summary
Log
log
public static final String
myNS
Constructors Summary
public TestSer(String name)


       
        super(name);
    
Methods Summary
public voiddoTestData(boolean multiref)

        MessageContext msgContext = new MessageContext(new AxisServer());
        msgContext.setSOAPConstants(SOAPConstants.SOAP12_CONSTANTS);
        msgContext.setProperty(Constants.MC_NO_OPERATION_OK, Boolean.TRUE);
        
        SOAPEnvelope msg = new SOAPEnvelope(SOAPConstants.SOAP12_CONSTANTS);
        RPCParam arg1 = new RPCParam("urn:myNamespace", "testParam", "this is a string");

        Data data = new Data();
        data.stringMember = "String member";
        data.floatMember = new Float("4.54");

        RPCParam arg2 = new RPCParam("", "struct", data);
        RPCElement body = new RPCElement("urn:myNamespace", "method1", new Object[]{ arg1, arg2 });
        msg.addBodyElement(body);

        Writer stringWriter = new StringWriter();
        SerializationContext context = new SerializationContext(stringWriter, msgContext);
        context.setDoMultiRefs(multiref);

        // Create a TypeMapping and register the specialized Type Mapping
        TypeMappingRegistry reg = context.getTypeMappingRegistry();
        TypeMapping tm = (TypeMapping) reg.createTypeMapping();
        tm.setSupportedEncodings(new String[] {Constants.URI_SOAP12_ENC});
        reg.register(Constants.URI_SOAP12_ENC, tm);

        QName dataQName = new QName("typeNS", "Data");
        tm.register(Data.class, dataQName, new DataSerFactory(), new DataDeserFactory());

        msg.output(context);

        String msgString = stringWriter.toString();

        log.debug("---");
        log.debug(msgString);
        log.debug("---");

        StringReader reader = new StringReader(msgString);

        DeserializationContext dser = new DeserializationContext(
            new InputSource(reader), msgContext, org.apache.axis.Message.REQUEST);
        dser.parse();

        SOAPEnvelope env = dser.getEnvelope();
        RPCElement rpcElem = (RPCElement)env.getFirstBody();
        RPCParam struct = rpcElem.getParam("struct");
        assertNotNull("No <struct> param", struct);

        Data val = (Data)struct.getObjectValue();
        assertNotNull("No value for struct param", val);

        assertEquals("Data and Val string members are not equal", data.stringMember, val.stringMember);
        assertEquals("Data and Val float members are not equal",data.floatMember.floatValue(),
                     val.floatMember.floatValue(), 0.00001F);
    
public voidtestDataNoHrefs()

        doTestData(false);
    
public voidtestDataWithHrefs()

        doTestData(true);
    
public voidtestEmptyXMLNS()

        try {
            MessageContext msgContext = new MessageContext(new AxisServer());
            msgContext.setSOAPConstants(SOAPConstants.SOAP12_CONSTANTS);
            msgContext.setProperty(Constants.MC_NO_OPERATION_OK, Boolean.TRUE);
            
            String req =
                "<xsd1:A xmlns:xsd1=\"urn:myNamespace\">"
                    + "<xsd1:B>"
                    + "<xsd1:C>foo bar</xsd1:C>"
                    + "</xsd1:B>"
                    + "</xsd1:A>";

            StringWriter stringWriter=new StringWriter();
            StringReader reqReader = new StringReader(req);
            InputSource reqSource = new InputSource(reqReader);

            Document document = XMLUtils.newDocument(reqSource);

            String msgString = null;

            SOAPEnvelope msg = new SOAPEnvelope(SOAPConstants.SOAP12_CONSTANTS);
            RPCParam arg1 = new RPCParam("urn:myNamespace", "testParam", document.getFirstChild());
            arg1.setXSITypeGeneration(Boolean.FALSE);

            RPCElement body = new RPCElement("urn:myNamespace", "method1", new Object[] { arg1 });
            msg.addBodyElement(body);
            body.setEncodingStyle(Constants.URI_LITERAL_ENC);

            SerializationContext context = new SerializationContext(stringWriter, msgContext);
            msg.output(context);

            msgString = stringWriter.toString();
            assertTrue(msgString.indexOf("xmlns=\"\"")==-1);
        } catch (Exception e) {
            fail(e.getMessage());
        }
    
public voidtestRPCElement()
Test RPC element serialization when we have no MessageContext

        try {
            SOAPEnvelope env = new SOAPEnvelope();
            RPCElement method = new RPCElement("ns",
                                               "method",
                                               new Object [] { "argument" });
            env.addBodyElement(method);
            String soapStr = env.toString();
        } catch (Exception e) {
            fail(e.getMessage());
        }

        // If there was no exception, we succeeded in serializing it.