messagecontextsaveatest.java

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

JAVA
1,306
字号
            // 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 = phase1;
            } else if (it_count == 2) {
                original_phase = phase2;
            } else if (it_count == 3) {
                original_phase = phase3;
            } else if (it_count == 4) {
                original_phase = phase4;
            } else if (it_count == 5) {
                original_phase = phase5;
            } else if (it_count == 6) {
                original_phase = phase6;
            } else if (it_count == 7) {
                original_phase = phase7;
            } else {
                // unexpected
                assertTrue(false);
            }

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

        // -------------------------------------------------------------------
        // second resume to start the second pause
        // -------------------------------------------------------------------
        log.debug(
                "MessageContextSaveATest:testReceive(): resume - - engine.resume(mc) - - - - - - - - - - - - - - - -");
        engine.resume(mc);

        assertEquals(35, executedHandlers.size());
        for (int i = 31; i < 35; i++) {
            assertEquals(((Integer) executedHandlers.get(i)).intValue(), i + 1);
        }

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

        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 = phase1;
            } else if (it_count == 2) {
                original_phase = phase2;
            } else if (it_count == 3) {
                original_phase = phase3;
            } else if (it_count == 4) {
                original_phase = phase4;
            } else if (it_count == 5) {
                original_phase = phase5;
            } else if (it_count == 6) {
                original_phase = phase6;
            } else if (it_count == 7) {
                original_phase = phase7;
            } else {
                // unexpected
                assertTrue(false);
            }

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


    /**
     * Gets the ID associated with the handler object.
     *
     * @param o The handler object
     * @return The ID associated with the handler,
     *         -1 otherwise
     */
    private int getHandlerID(Object o) {
        int id = -1;

        if (o instanceof TempHandler) {
            id = ((TempHandler) o).getHandlerID();
        }

        return id;
    }


    /**
     * Check the handler objects to see if they are equivalent.
     *
     * @param o1 The first handler
     * @param o2 The second handler
     * @return TRUE if the handler objects are equivalent,
     *         FALSE otherwise
     */
    private boolean compareHandlers(Object o1, Object o2) {
        if ((o1 == null) && (o2 == null)) {
            return true;
        }

        if ((o1 != null) && (o2 != null)) {
            String c1 = o1.getClass().getName();
            String c2 = o2.getClass().getName();

            if (c1.equals(c2)) {
                log.debug("MessageContextSaveATest::compareHandlers:  class [" + c1 + "] match ");

                int id1 = getHandlerID(o1);
                int id2 = getHandlerID(o2);

                if (id1 == id2) {
                    log.debug("MessageContextSaveATest::compareHandlers:  id [" + id1 + "] match");
                    return true;
                } else {
                    log.debug("MessageContextSaveATest::compareHandlers:  id1 [" + id1 +
                            "] != id2 [" + id2 + "] ");
                    return false;
                }
            } else {
                log.debug("MessageContextSaveATest::compareHandlers:  class1 [" + c1 +
                        "] != class2 [" + c2 + "]   ");
                return false;
            }
        }

        return false;
    }


    /**
     * Compare two phases.
     *
     * @param o1 The first phase object
     * @param o2 The second phase object
     * @return TRUE if the phases are equivalent,
     *         FALSE otherwise
     */
    private boolean comparePhases(Object o1, Object o2) {
        if ((o1 == null) && (o2 == null)) {
            log.debug(
                    "MessageContextSaveATest: comparePhases:  Phase1[] == Phase2[] - both null objects");
            return true;
        }

        if (((o1 != null) && (o2 != null))
                && ((o1 instanceof Phase) && (o2 instanceof Phase))
                ) {

            try {
                Phase p1 = (Phase) o1;
                Phase p2 = (Phase) o2;

                String name1 = p1.getName();
                String name2 = p2.getName();

                ArrayList list1 = p1.getHandlers();
                ArrayList list2 = p2.getHandlers();

                if ((list1 == null) && (list2 == null)) {
                    log.debug("MessageContextSaveATest: comparePhases:  Phase1[" + name1 +
                            "] == Phase2[" + name2 + "]");
                    return true;
                }

                if ((list1 != null) && (list2 != null)) {
                    int size1 = list1.size();
                    int size2 = list2.size();

                    if (size1 != size2) {
                        log.debug("MessageContextSaveATest: comparePhases:  Phase1[" +
                                name1 + "] != Phase2[" + name2 +
                                "] - mismatched size of handler lists");
                        return false;
                    }

                    for (int j = 0; j < size1; j++) {
                        Object obj1 = list1.get(j);
                        Object obj2 = list2.get(j);

                        if ((obj1 == null) && (obj2 == null)) {
                            // ok
                        } else if ((obj1 != null) && (obj2 != null)) {
                            boolean check;

                            if (obj1 instanceof Phase) {
                                check = comparePhases(obj1, obj2);
                            } else {
                                // must be a handler
                                check = compareHandlers(obj1, obj2);
                            }

                            if (!check) {
                                log.debug(
                                        "MessageContextSaveATest: comparePhases:  Phase1[" + name1 +
                                                "] != Phase2[" + name2 +
                                                "] - mismatched handler lists");
                                return false;
                            }
                        } else {
                            // mismatch
                            log.debug("MessageContextSaveATest: comparePhases:  Phase1[" +
                                    name1 + "] != Phase2[" + name2 +
                                    "] - mismatched handler lists");
                            return false;
                        }
                    }

                    // if we got here, the comparison completed ok
                    // with a match

                    log.debug("MessageContextSaveATest: comparePhases:  Phase1[" + name1 +
                            "] == Phase2[" + name2 + "] - matched handler lists");
                    return true;
                }

            }
            catch (Exception e) {
                // some error
                e.printStackTrace();
            }
        }

        log.debug("MessageContextSaveATest: comparePhases:  Phase1[] != Phase2[]");
        return false;
    }

    private void showMcMap(HashMap map) {
        if ((map != null) && (!map.isEmpty())) {
            Iterator itList = map.keySet().iterator();

            while (itList.hasNext()) {
                String key = (String) itList.next();

                MessageContext value = (MessageContext) map.get(key);
                String valueID = null;

                if (value != null) {
                    valueID = value.getMessageID();

                    log.debug(
                            "MessageContextSaveATest: showMcMap:  Message context   ID[" + valueID +
                                    "]   Key Label [" + key + "]");

                }
            }
        } else {
            log.debug(
                    "MessageContextSaveATest: showMcMap:  No entries to display for message contexts table.");
        }
    }


    // this checks the save/restore of a message context that hasn't been
    // through the engine to simulate what some WS-RM implementations 
    // need to do - make a simple message context for a RM ack or other
    // simple message
    public void testSimpleMC() throws Exception {
        String title = "MessageContextSaveATest:testSimpleMC(): ";
        log.debug(title + "start - - - - - - - - - - - - - - - -");

        MessageContext simpleMsg = new MessageContext();
        MessageContext restoredSimpleMsg = null;

        File theFile = null;
        String theFilename = null;

        boolean savedMessageContext = false;
        boolean restoredMessageContext = false;
        boolean comparesOk = false;

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

        if (theFile != null) {
            // ---------------------------------------------------------
            // save to the temporary file
            // ---------------------------------------------------------
            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(simpleMsg);

                // close out the streams
                outObjStream.flush();
                outObjStream.close();
                outStream.flush();
                outStream.close();

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

                long filesize = theFile.length();
                log.debug(title + "file size after save [" + filesize +
                        "]   temp file = [" + theFilename + "]");

            }
            catch (Exception ex2) {
                log.debug(title + "error with saving message context = [" +
                        ex2.getClass().getName() + " : " + ex2.getMessage() + "]");
                ex2.printStackTrace();

⌨️ 快捷键说明

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