messagecontextselfmanageddatatest.java

来自「开源的axis2框架的源码。用于开发WEBSERVER」· Java 代码 · 共 1,580 行 · 第 1/4 页

JAVA
1,580
字号
        assertEquals(1, invokecallcount);
    }


    /**
     * Test for verifying the self managed data used during the save and restore
     */
    public void testPause02_saveRestoreSelfManagedData() {
        log.debug(
                "MessageContextSelfManagedDataTest::testPause02_saveRestoreSelfManagedData()=======================================");

        try {
            ArrayList handlers = new ArrayList();
            handlers.add(handler02);
            cfgContext.getAxisConfiguration().setInPhasesUptoAndIncludingPostDispatch(handlers);

            mc.setTo(new EndpointReference("axis2/services/NullService"));
            mc.setWSAAction("DummyOp");

            AxisEngine engine = new AxisEngine(cfgContext);
            engine.receive(mc);
        }
        catch (Exception e) {
            e.printStackTrace();
        }

        // make sure the data in our handler got restored
        assertEquals(handler02.testData, handler02.getTestDataFromMessageContext(mc));
        assertEquals(1, invokecallcount);
    }

    /**
     * Test for save and restore of self managed data and the AxisOperation
     */
    public void testPause03_saveRestoreOperation() {
        log.debug(
                "MessageContextSelfManagedDataTest::testPause03_saveRestoreOperation()=======================================");

        try {
            ArrayList handlers = new ArrayList();
            handlers.add(handler02);
            cfgContext.getAxisConfiguration().setInPhasesUptoAndIncludingPostDispatch(handlers);

            mc.setTo(new EndpointReference("axis2/services/NullService"));
            mc.setWSAAction("DummyOp");

            AxisEngine engine = new AxisEngine(cfgContext);
            engine.receive(mc);
        }
        catch (Exception e) {
            e.printStackTrace();
        }

        // make sure the operation got restored in the MessageContext object
        assertEquals(operationName.toString(), mc.getAxisOperation().getName().toString());
        assertEquals(1, invokecallcount);

    }


    /**
     * Test for save and restore of self managed data and the AxisService
     */
    public void testPause04_saveRestoreAxisService() {
        log.debug(
                "MessageContextSelfManagedDataTest::testPause04_saveRestoreAxisService()=======================================");

        try {
            ArrayList handlers = new ArrayList();
            handlers.add(handler02);
            cfgContext.getAxisConfiguration().setInPhasesUptoAndIncludingPostDispatch(handlers);

            mc.setTo(new EndpointReference("axis2/services/NullService"));
            mc.setWSAAction("DummyOp");

            AxisEngine engine = new AxisEngine(cfgContext);
            engine.receive(mc);
        }
        catch (Exception e) {
            e.printStackTrace();
        }

        // make sure the service got restored in the MessageContext object
        assertEquals(serviceName.toString(), mc.getAxisService().getName().toString());
        assertEquals(1, invokecallcount);
    }


    /**
     * Test for save and restore of self managed data and the AxisServiceGroup
     */
    public void testPause05_saveRestoreAxisServiceGroup() {
        log.debug(
                "MessageContextSelfManagedDataTest::testPause05_saveRestoreAxisServiceGroup()=======================================");

        try {
            ArrayList handlers = new ArrayList();
            handlers.add(handler02);
            cfgContext.getAxisConfiguration().setInPhasesUptoAndIncludingPostDispatch(handlers);

            mc.setTo(new EndpointReference("axis2/services/NullService"));
            mc.setWSAAction("DummyOp");

            AxisEngine engine = new AxisEngine(cfgContext);
            engine.receive(mc);

        }
        catch (Exception e) {
            e.printStackTrace();
        }

        // make sure the serviceGroup got restored in the MessageContext object
        //assertEquals(serviceGroupName.toString(), mc.getAxisServiceGroup().getServiceGroupName());
        assertEquals(1, invokecallcount);
    }


    /**
     * Test for phases
     */
    public void testPause06_saveRestorePhases() {
        log.debug(
                "MessageContextSelfManagedDataTest::testPause06_saveRestorePhases()=======================================");

        TempHandler02 handlerA = new TempHandler02(666);

        Phase phase601 = new Phase("01");
        phase601.addHandler(new TempHandler02(61)); // slot 1
        phase601.addHandler(new TempHandler03(62)); // slot 2

        Phase phase602 = new Phase("02");
        phase602.addHandler(new TempHandler02(63)); // slot 3
        phase602.addHandler(handlerA);              // slot 4
        phase602.addHandler(new TempHandler03(64)); // slot 5

        Phase phase603 = new Phase("03");
        phase603.addHandler(new TempHandler02(65)); // slot 6
        phase603.addHandler(subhandler);            // slot 7
        phase603.addHandler(handlerA);              // slot 8 - same instance, second insertion
        phase603.addHandler(new TempHandler03(66)); // slot 9

        /*
         * TODO:  WARNING WARNING WARNING
         * Ideally inserting subPhase here would make the axis2 engine call
         * the invoke of nested subhandler.  It does not do this.  
         * Please see the warning later in this method.
         */
        Phase subPhase601 = new Phase("sub6");
        subPhase601.addHandler(subhandler);
        phase603.addHandler(subPhase601);           // slot 10
        phase603.addHandler(new TempHandler02(67)); // slot 11
        phase603.addHandler(new TempHandler03(68)); // slot 12

        try {
            ArrayList phases = new ArrayList();
            phases.add(phase601);
            phases.add(phase602);
            phases.add(phase603);

            cfgContext.getAxisConfiguration().setInPhasesUptoAndIncludingPostDispatch(phases);

            mc.setTo(new EndpointReference("axis2/services/NullService"));
            mc.setWSAAction("DummyOp");

            AxisEngine engine = new AxisEngine(cfgContext);
            engine.receive(mc);
        }
        catch (Exception e) {
            e.printStackTrace();
        }

        // get the phase lists and see if they match up
        ArrayList restoredPhases = mc.getExecutionChain();
        int it_count = 0;

        Iterator it = restoredPhases.iterator();
        while (it.hasNext()) {
            // we know everything at this level is a Phase.  
            // if you change it, you might get a ClassCastException 
            Phase restored_phase = (Phase) it.next();

            Phase original_phase = null;

            it_count++;

            if (it_count == 1) {
                original_phase = phase601;
            } else if (it_count == 2) {
                original_phase = phase602;
            } else if (it_count == 3) {
                original_phase = phase603;
            }

            //comparePhases(restored_phase.getHandlers().iterator(), original_phase.getHandlers());

            boolean isOk = comparePhases(restored_phase, original_phase);
            assertTrue(isOk);
        }

        // TODO WARNING WARNING WARNING
        // The axis2 engine is not calling the invoke on nested handlers!  
        // The way this testcase works is that the handler's invoke() method is 
        // what sets the data.  So, any handlers that are in subPhase will 
        // not get called, and thus will not set or restore any data.  
        // Notice we do currently have a subhandler in the subPhase object.
        // When axis2 decides to support nested handlers, the
        // first three assertEquals below will fail.
        log.debug(
                "MessageContextSelfManagedDataTest::testPause06_saveRestorePhases()::  invokecallcount [" +
                        invokecallcount + "]");
        assertEquals(11, invokecallcount);

        // even though there are two occurrances of the same instance of
        // handlerA in the executionChain, its serialize and deserialize 
        // should only be called once per unique instance in the list
        int count_s = handlerA.getSerializecallcount();
        log.debug(
                "MessageContextSelfManagedDataTest::testPause06_saveRestorePhases()::  handlerA serialize call count [" +
                        count_s + "]");
        assertEquals(11, count_s);

        // here comes some fun math...
        // Since a handler (TempHandler02) in this case 
        // doesn't add any data until its invoke method gets called, 
        // and the invoke is what causes the save/restore (and thus 
        // the serialize/deserialize) there is no data for handlerA 
        // to deserialize until the first occurance of handlerA 
        // invoke is called in the executionChain.  Observing our phases, 
        // we see it is in slot #3.  11 - 3 = 8
        int count_d = handlerA.getDeserializecallcount();
        log.debug(
                "MessageContextSelfManagedDataTest::testPause06_saveRestorePhases()::  handlerA deserialize call count [" +
                        count_d + "]");
        assertEquals(8, count_d);
        assertEquals(subhandler.testData, subhandler.getTestDataFromMessageContext(mc));

    }


    /**
     * Test for save and restore of binary self managed data
     */
    public void testSelfManagedData07() {
        log.debug(
                "MessageContextSelfManagedDataTest::testSelfManagedData07()=======================================");
        try {
            ArrayList handlers = new ArrayList();
            handlers.add(handler03);
            cfgContext.getAxisConfiguration().setInPhasesUptoAndIncludingPostDispatch(handlers);

            mc.setTo(new EndpointReference("axis2/services/NullService"));
            mc.setWSAAction("DummyOp");

            AxisEngine engine = new AxisEngine(cfgContext);
            engine.receive(mc);
        }
        catch (Exception e) {
            e.printStackTrace();
        }

        boolean isOk3 = isEquals(testData03, (byte []) handler03.getTestDataFromMessageContext(mc));
        assertTrue(isOk3);

        assertEquals(1, invokecallcount);
    }


    /**
     * Test for handler04
     */
    public void testSelfManagedData08() {
        log.debug(
                "MessageContextSelfManagedDataTest::testSelfManagedData08()=======================================");
        try {

            ArrayList handlers = new ArrayList();
            handlers.add(handler04);
            cfgContext.getAxisConfiguration().setInPhasesUptoAndIncludingPostDispatch(handlers);

            mc.setTo(new EndpointReference("axis2/services/NullService"));
            mc.setWSAAction("DummyOp");

            AxisEngine engine = new AxisEngine(cfgContext);
            engine.receive(mc);

        }
        catch (Exception e) {
            e.printStackTrace();
        }

        boolean isOk4 = isEquals(testData04, (long []) handler04.getTestDataFromMessageContext(mc));
        assertTrue(isOk4);

        assertEquals(1, invokecallcount);
    }


    /**
     * Test for handler03 and handler04
     */
    public void testSelfManagedData09() {
        log.debug(
                "MessageContextSelfManagedDataTest::testSelfManagedData09()=======================================");
        try {

            ArrayList handlers = new ArrayList();
            handlers.add(handler03);
            handlers.add(handler04);
            cfgContext.getAxisConfiguration().setInPhasesUptoAndIncludingPostDispatch(handlers);

            mc.setTo(new EndpointReference("axis2/services/NullService"));
            mc.setWSAAction("DummyOp");

            AxisEngine engine = new AxisEngine(cfgContext);
            engine.receive(mc);

        }
        catch (Exception e) {
            e.printStackTrace();
        }

        boolean isOk3 = isEquals(testData03, (byte []) handler03.getTestDataFromMessageContext(mc));
        assertTrue(isOk3);

        boolean isOk4 = isEquals(testData04, (long []) handler04.getTestDataFromMessageContext(mc));
        assertTrue(isOk4);

        assertEquals(2, invokecallcount);
    }

    //-------------------------------------------------------------------------
    // internal helper methods
    //-------------------------------------------------------------------------

    /**
     * Saves the specified message context to a temporary
     * file, then restores it.
     *
     * @param mc1      The message context object to save
     * @param fnprefix A prefix for the filename of the temporary file
     * @param desc     Text that describes the caller's situation
     * @return The restored message context object or NULL
     */
    private MessageContext saveAndRestore(MessageContext mc1, String fnprefix, String desc) {
        MessageContext msgContext2 = null;
        String title = "MessageContextSelfManagedDataTest::saveAndRestore::[" + desc + "]   ";
        log.debug(title);

        try {
            theFile = File.createTempFile(fnprefix, null);
            log.debug(title + "temp file = [" + theFile.getName() + "]");
        }
        catch (Exception ex) {
            log.debug(title + "error creating temp file = [" + ex.getMessage() + "]");
            theFile = null;
        }

        if (theFile != null) {
            try {
                // setup an output stream to a physical file
                FileOutputStream outStream = new FileOutputStream(theFile);

                // attach a stream capable of writing objects to the 
                // stream connected to the file
                ObjectOutputStream outObjStream = new ObjectOutputStream(outStream);

                // try to save the message context
                log.debug(title + "saving message context ....");
                savedMessageContext = false;

                outObjStream.writeObject(mc1);
                outObjStream.close();
                outStream.close();

                // no exceptions, set savedMessageContext to true
                savedMessageContext = true;

                log.debug(title + "....saved message context .....");

                // setup an input stream to the file
                FileInputStream inStream = new FileInputStream(theFile);

                // attach a stream capable of reading objects from the 
                // stream connected to the file
                ObjectInputStream inObjStream = new ObjectInputStream(inStream);

                // try to restore the message context
                log.debug(title + "restoring message context .....");
                restoredMessageContext = false;
                msgContext2 = (MessageContext) inObjStream.readObject();
                inObjStream.close();
                inStream.close();
                msgContext2.activate(mc1.getConfigurationContext());

                // no exceptions, set restoredMessageContext to true

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?