⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 testactionservlet.java

📁 ActionServlet源码 struts的一个步骤都有 知道本来有视频的太大了 就没有上传了
💻 JAVA
📖 第 1 页 / 共 3 页
字号:

        ForwardConfig result =
            actionServlet.processForwardConfigClass(customSub, moduleConfig,
                null);

        assertSame("The instance returned should be the param given it.",
            customSub, result);
    }

    /**
     * Make sure the code throws the correct exception when it can't create an
     * instance of the base config's custom class.
     */
    public void notestProcessForwardConfigClassError()
        throws Exception {
        ForwardConfig customBase =
            new CustomForwardConfigArg("success", "/success.jsp");

        moduleConfig.addForwardConfig(customBase);

        ForwardConfig customSub = new ActionForward();

        customSub.setName("failure");
        customSub.setExtends("success");
        moduleConfig.addForwardConfig(customSub);

        try {
            actionServlet.processForwardConfigClass(customSub, moduleConfig,
                null);
            fail("Exception should be thrown");
        } catch (UnavailableException e) {
            // success
        } catch (Exception e) {
            fail("Unexpected exception thrown.");
        }
    }

    /**
     * Test the case where the subconfig has already specified its own config
     * class.  If the code still attempts to create a new instance, an error
     * will be thrown.
     */
    public void testProcessForwardConfigClassOverriddenSubConfigClass()
        throws Exception {
        moduleConfig.addForwardConfig(baseForward);

        ForwardConfig customSub =
            new CustomForwardConfigArg("failure", "/failure.jsp");

        customSub.setExtends("success");
        moduleConfig.addForwardConfig(customSub);

        try {
            actionServlet.processForwardConfigClass(customSub, moduleConfig,
                null);
        } catch (Exception e) {
            fail("Exception should not be thrown");
        }
    }

    // --------------------------------------------------- ActionConfig Tests

    /**
     * Test that nothing fails if there are no extensions.
     */
    public void testInitModuleActionConfigsNoExtends()
        throws ServletException {
        moduleConfig.addActionConfig(baseAction);

        try {
            actionServlet.initModuleActions(moduleConfig);
        } catch (Exception e) {
            fail("Unexpected exception caught.");
        }
    }

    /**
     * Test that processActionConfigExtension() calls processExtends()
     */
    public void testProcessActionExtension()
        throws ServletException {
        CustomActionConfig action = new CustomActionConfig("/action");

        moduleConfig.addActionConfig(action);
        actionServlet.processActionConfigExtension(action, moduleConfig);

        assertTrue("processExtends() was not called",
            action.processExtendsCalled);
    }

    /**
     * Test that an ActionConfig's ForwardConfig can inherit from a
     * global ForwardConfig.
     */
    public void testProcessActionExtensionWithForwardConfig()
        throws ServletException {
        ForwardConfig forwardConfig = new ForwardConfig();
        forwardConfig.setName("sub");
        forwardConfig.setExtends("success");
        baseAction.addForwardConfig(forwardConfig);

        moduleConfig.addActionConfig(baseAction);
        moduleConfig.addForwardConfig(baseForward);
        actionServlet.processActionConfigExtension(baseAction, moduleConfig);

        forwardConfig = baseAction.findForwardConfig("sub");

        assertEquals("'sub' forward's inheritance was not processed.",
            baseForward.getPath(), forwardConfig.getPath());
    }

    /**
     * Test that an ActionConfig's ExceptionConfig can inherit from a
     * global ExceptionConfig.
     */
    public void testProcessActionExtensionWithExceptionConfig()
        throws ServletException {
        ExceptionConfig exceptionConfig = new ExceptionConfig();
        exceptionConfig.setType("SomeException");
        exceptionConfig.setExtends("java.lang.NullPointerException");
        baseAction.addExceptionConfig(exceptionConfig);

        moduleConfig.addActionConfig(baseAction);
        moduleConfig.addExceptionConfig(baseException);
        actionServlet.processActionConfigExtension(baseAction, moduleConfig);

        exceptionConfig = baseAction.findExceptionConfig("SomeException");

        assertEquals("SomeException's inheritance was not processed.",
            baseException.getKey(), exceptionConfig.getKey());
    }

    /**
     * Make sure processActionConfigClass() returns an instance of the correct
     * class if the base config is using a custom class.
     */
    public void testProcessActionConfigClass()
        throws Exception {
        CustomActionConfig customBase = new CustomActionConfig("/base");

        moduleConfig.addActionConfig(customBase);

        ActionMapping customSub = new ActionMapping();

        customSub.setPath("/sub");
        customSub.setExtends("/base");
        moduleConfig.addActionConfig(customSub);

        ActionConfig result =
            actionServlet.processActionConfigClass(customSub, moduleConfig);

        assertTrue("Incorrect class of action config",
            result instanceof CustomActionConfig);
        assertEquals("Incorrect path", customSub.getPath(), result.getPath());
        assertEquals("Incorrect extends", customSub.getExtends(),
            result.getExtends());

        assertSame("Result was not registered in the module config", result,
            moduleConfig.findActionConfig("/sub"));
    }

    /**
     * Make sure processActionConfigClass() returns what it was given if the
     * action passed to it doesn't extend anything.
     */
    public void testProcessActionConfigClassNoExtends()
        throws Exception {
        moduleConfig.addActionConfig(baseAction);

        ActionConfig result = null;

        try {
            result =
                actionServlet.processActionConfigClass(baseAction, moduleConfig);
        } catch (UnavailableException e) {
            fail("An exception should not be thrown here");
        }

        assertSame("Result should be the same as the input.", baseAction, result);
    }

    /**
     * Make sure processActionConfigClass() returns the same class instance if
     * the base config isn't using a custom class.
     */
    public void testProcessActionConfigClassSubConfigCustomClass()
        throws Exception {
        moduleConfig.addActionConfig(baseAction);

        ActionConfig customSub = new ActionMapping();

        customSub.setPath("/sub");
        customSub.setExtends("/index");
        moduleConfig.addActionConfig(customSub);

        ActionConfig result =
            actionServlet.processActionConfigClass(customSub, moduleConfig);

        assertSame("The instance returned should be the param given it.",
            customSub, result);
    }

    /**
     * Make sure the code throws the correct exception when it can't create an
     * instance of the base config's custom class.
     */
    public void notestProcessActionConfigClassError()
        throws Exception {
        ActionConfig customBase = new CustomActionConfigArg("/index");

        moduleConfig.addActionConfig(customBase);

        ActionConfig customSub = new ActionMapping();

        customSub.setPath("/sub");
        customSub.setExtends("/index");
        moduleConfig.addActionConfig(customSub);

        try {
            actionServlet.processActionConfigClass(customSub, moduleConfig);
            fail("Exception should be thrown");
        } catch (UnavailableException e) {
            // success
        } catch (Exception e) {
            fail("Unexpected exception thrown.");
        }
    }

    /**
     * Test the case where the subconfig has already specified its own config
     * class.  If the code still attempts to create a new instance, an error
     * will be thrown.
     */
    public void testProcessActionConfigClassOverriddenSubConfigClass()
        throws Exception {
        moduleConfig.addActionConfig(baseAction);

        ActionConfig customSub = new CustomActionConfigArg("/sub");

        customSub.setExtends("/index");
        moduleConfig.addActionConfig(customSub);

        try {
            actionServlet.processActionConfigClass(customSub, moduleConfig);
        } catch (Exception e) {
            fail("Exception should not be thrown");
        }
    }

    /**
     * Used for testing custom FormBeanConfig classes.
     */
    public static class CustomFormBeanConfig extends FormBeanConfig {
        public boolean processExtendsCalled = false;

        public CustomFormBeanConfig() {
            super();
        }

        /**
         * Set a flag so we know this method was called.
         */
        public void processExtends(ModuleConfig moduleConfig)
            throws ClassNotFoundException, IllegalAccessException,
                InstantiationException {
            processExtendsCalled = true;
        }
    }

    /**
     * Used to test cases where the subclass cannot be created with a no-arg
     * constructor.
     */
    private class CustomFormBeanConfigArg extends FormBeanConfig {
        CustomFormBeanConfigArg(String name) {
            super();
            setName(name);
        }
    }

    /**
     * Used for testing custom ExceptionConfig classes.
     */
    public static class CustomExceptionConfig extends ExceptionConfig {
        public boolean processExtendsCalled = false;

        public CustomExceptionConfig() {
            super();
        }

        /**
         * Set a flag so we know this method was called.
         */
        public void processExtends(ModuleConfig moduleConfig,
            ActionConfig actionConfig)
            throws ClassNotFoundException, IllegalAccessException,
                InstantiationException {
            processExtendsCalled = true;
        }
    }

    /**
     * Used to test cases where the subclass cannot be created with a no-arg
     * constructor.
     */
    private class CustomExceptionConfigArg extends ExceptionConfig {
        CustomExceptionConfigArg(String type) {
            super();
            setType(type);
        }
    }

    /**
     * Used for testing custom ForwardConfig classes.
     */
    public static class CustomForwardConfig extends ForwardConfig {
        public boolean processExtendsCalled = false;

        public CustomForwardConfig() {
            super();
        }

        public CustomForwardConfig(String name, String path) {
            super(name, path, false);
        }

        /**
         * Set a flag so we know this method was called.
         */
        public void processExtends(ModuleConfig moduleConfig,
            ActionConfig actionConfig)
            throws ClassNotFoundException, IllegalAccessException,
                InstantiationException {
            processExtendsCalled = true;
        }
    }

    /**
     * Used to test cases where the subclass cannot be created with a no-arg
     * constructor.
     */
    private class CustomForwardConfigArg extends ForwardConfig {
        CustomForwardConfigArg(String name, String path) {
            super();
            setName(name);
            setPath(path);
        }
    }

    /**
     * Used for testing custom ActionConfig classes.
     */
    public static class CustomActionConfig extends ActionConfig {
        public boolean processExtendsCalled = false;

        public CustomActionConfig() {
            super();
        }

        public CustomActionConfig(String path) {
            super();
            setPath(path);
        }

        /**
         * Set a flag so we know this method was called.
         */
        public void processExtends(ModuleConfig moduleConfig)
            throws ClassNotFoundException, IllegalAccessException,
                InstantiationException {
            processExtendsCalled = true;
        }
    }

    /**
     * Used to test cases where the subclass cannot be created with a no-arg
     * constructor.
     */
    private class CustomActionConfigArg extends ActionConfig {
        CustomActionConfigArg(String path) {
            super();
            setPath(path);
        }
    }

    // [...]
}

⌨️ 快捷键说明

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