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

📄 exceptionmappinginterceptortest.java

📁 在Struts2中的jar包xwork的源代码.版本为2.0.7
💻 JAVA
字号:
/* * Copyright (c) 2002-2006 by OpenSymphony * All rights reserved.*/package com.opensymphony.xwork2.interceptor;import com.mockobjects.dynamic.Mock;import com.opensymphony.xwork2.*;import com.opensymphony.xwork2.config.entities.ActionConfig;import com.opensymphony.xwork2.config.entities.ExceptionMappingConfig;import com.opensymphony.xwork2.util.ValueStack;import com.opensymphony.xwork2.util.ValueStackFactory;import com.opensymphony.xwork2.validator.ValidationException;import junit.framework.TestCase;import java.util.HashMap;/** * Unit test for ExceptionMappingInterceptor. *  * @author Matthew E. Porter (matthew dot porter at metissian dot com) */public class ExceptionMappingInterceptorTest extends TestCase {    ActionInvocation invocation;    ExceptionMappingInterceptor interceptor;    Mock mockInvocation;    ValueStack stack;    public void testThrownExceptionMatching() throws Exception {        this.setUpWithExceptionMappings();        Mock action = new Mock(Action.class);        Exception exception = new XWorkException("test");        mockInvocation.expectAndThrow("invoke", exception);        mockInvocation.matchAndReturn("getAction", ((Action) action.proxy()));        String result = interceptor.intercept(invocation);        assertNotNull(stack.findValue("exception"));        assertEquals(stack.findValue("exception"), exception);        assertEquals(result, "spooky");        ExceptionHolder holder = (ExceptionHolder) stack.getRoot().get(0); // is on top of the root        assertNotNull(holder.getExceptionStack()); // to invoke the method for unit test    }    public void testThrownExceptionMatching2() throws Exception {        this.setUpWithExceptionMappings();        Mock action = new Mock(Action.class);        Exception exception = new ValidationException("test");        mockInvocation.expectAndThrow("invoke", exception);        mockInvocation.matchAndReturn("getAction", ((Action) action.proxy()));        String result = interceptor.intercept(invocation);        assertNotNull(stack.findValue("exception"));        assertEquals(stack.findValue("exception"), exception);        assertEquals(result, "throwable");    }    public void testNoThrownException() throws Exception {        this.setUpWithExceptionMappings();        Mock action = new Mock(Action.class);        mockInvocation.expectAndReturn("invoke", Action.SUCCESS);        mockInvocation.matchAndReturn("getAction", ((Action) action.proxy()));        String result = interceptor.intercept(invocation);        assertEquals(result, Action.SUCCESS);        assertNull(stack.findValue("exception"));    }    public void testThrownExceptionNoMatch() throws Exception {        this.setupWithoutExceptionMappings();        Mock action = new Mock(Action.class);        Exception exception = new Exception("test");        mockInvocation.expectAndThrow("invoke", exception);        mockInvocation.matchAndReturn("getAction", ((Action) action.proxy()));        try {            interceptor.intercept(invocation);            fail("Should not have reached this point.");        } catch (Exception e) {            assertEquals(e, exception);        }    }    public void testThrownExceptionNoMatchLogging() throws Exception {        this.setupWithoutExceptionMappings();        Mock action = new Mock(Action.class);        Exception exception = new Exception("test");        mockInvocation.expectAndThrow("invoke", exception);        mockInvocation.matchAndReturn("getAction", ((Action) action.proxy()));        try {        	interceptor.setLogEnabled(true);            interceptor.intercept(invocation);            fail("Should not have reached this point.");        } catch (Exception e) {            assertEquals(e, exception);        }    }    public void testThrownExceptionNoMatchLoggingCategory() throws Exception {        this.setupWithoutExceptionMappings();        Mock action = new Mock(Action.class);        Exception exception = new Exception("test");        mockInvocation.expectAndThrow("invoke", exception);        mockInvocation.matchAndReturn("getAction", ((Action) action.proxy()));        try {        	interceptor.setLogEnabled(true);        	interceptor.setLogCategory("showcase.unhandled");            interceptor.intercept(invocation);            fail("Should not have reached this point.");        } catch (Exception e) {            assertEquals(e, exception);        }    }    public void testThrownExceptionNoMatchLoggingCategoryLevelFatal() throws Exception {        this.setupWithoutExceptionMappings();        Mock action = new Mock(Action.class);        Exception exception = new Exception("test");        mockInvocation.expectAndThrow("invoke", exception);        mockInvocation.matchAndReturn("getAction", ((Action) action.proxy()));        try {        	interceptor.setLogEnabled(true);        	interceptor.setLogCategory("showcase.unhandled");        	interceptor.setLogLevel("fatal");            interceptor.intercept(invocation);            fail("Should not have reached this point.");        } catch (Exception e) {            assertEquals(e, exception);        }                assertEquals("fatal", interceptor.getLogLevel());        assertEquals(true, interceptor.isLogEnabled());        assertEquals("showcase.unhandled", interceptor.getLogCategory());    }    public void testThrownExceptionNoMatchLoggingCategoryLevelError() throws Exception {        this.setupWithoutExceptionMappings();        Mock action = new Mock(Action.class);        Exception exception = new Exception("test");        mockInvocation.expectAndThrow("invoke", exception);        mockInvocation.matchAndReturn("getAction", ((Action) action.proxy()));        try {        	interceptor.setLogEnabled(true);        	interceptor.setLogCategory("showcase.unhandled");        	interceptor.setLogLevel("error");            interceptor.intercept(invocation);            fail("Should not have reached this point.");        } catch (Exception e) {            assertEquals(e, exception);        }    }    public void testThrownExceptionNoMatchLoggingCategoryLevelWarn() throws Exception {        this.setupWithoutExceptionMappings();        Mock action = new Mock(Action.class);        Exception exception = new Exception("test");        mockInvocation.expectAndThrow("invoke", exception);        mockInvocation.matchAndReturn("getAction", ((Action) action.proxy()));        try {        	interceptor.setLogEnabled(true);        	interceptor.setLogCategory("showcase.unhandled");        	interceptor.setLogLevel("warn");            interceptor.intercept(invocation);            fail("Should not have reached this point.");        } catch (Exception e) {            assertEquals(e, exception);        }    }    public void testThrownExceptionNoMatchLoggingCategoryLevelInfo() throws Exception {        this.setupWithoutExceptionMappings();        Mock action = new Mock(Action.class);        Exception exception = new Exception("test");        mockInvocation.expectAndThrow("invoke", exception);        mockInvocation.matchAndReturn("getAction", ((Action) action.proxy()));        try {        	interceptor.setLogEnabled(true);        	interceptor.setLogCategory("showcase.unhandled");        	interceptor.setLogLevel("info");            interceptor.intercept(invocation);            fail("Should not have reached this point.");        } catch (Exception e) {            assertEquals(e, exception);        }    }    public void testThrownExceptionNoMatchLoggingCategoryLevelDebug() throws Exception {        this.setupWithoutExceptionMappings();        Mock action = new Mock(Action.class);        Exception exception = new Exception("test");        mockInvocation.expectAndThrow("invoke", exception);        mockInvocation.matchAndReturn("getAction", ((Action) action.proxy()));        try {        	interceptor.setLogEnabled(true);        	interceptor.setLogCategory("showcase.unhandled");        	interceptor.setLogLevel("debug");            interceptor.intercept(invocation);            fail("Should not have reached this point.");        } catch (Exception e) {            assertEquals(e, exception);        }    }    public void testThrownExceptionNoMatchLoggingCategoryLevelTrace() throws Exception {        this.setupWithoutExceptionMappings();        Mock action = new Mock(Action.class);        Exception exception = new Exception("test");        mockInvocation.expectAndThrow("invoke", exception);        mockInvocation.matchAndReturn("getAction", ((Action) action.proxy()));        try {        	interceptor.setLogEnabled(true);        	interceptor.setLogCategory("showcase.unhandled");        	interceptor.setLogLevel("trace");            interceptor.intercept(invocation);            fail("Should not have reached this point.");        } catch (Exception e) {            assertEquals(e, exception);        }    }    public void testThrownExceptionNoMatchLoggingUnknownLevel() throws Exception {        this.setupWithoutExceptionMappings();        Mock action = new Mock(Action.class);        Exception exception = new Exception("test");        mockInvocation.expectAndThrow("invoke", exception);        mockInvocation.matchAndReturn("getAction", ((Action) action.proxy()));        try {        	interceptor.setLogEnabled(true);        	interceptor.setLogLevel("xxx");            interceptor.intercept(invocation);            fail("Should not have reached this point.");        } catch (IllegalArgumentException e) {        	// success        }    }    private void setupWithoutExceptionMappings() {        ActionConfig actionConfig = new ActionConfig();        Mock actionProxy = new Mock(ActionProxy.class);        actionProxy.expectAndReturn("getConfig", actionConfig);        mockInvocation.expectAndReturn("getProxy", ((ActionProxy) actionProxy.proxy()));        invocation = (ActionInvocation) mockInvocation.proxy();    }    private void setUpWithExceptionMappings() {        ActionConfig actionConfig = new ActionConfig();        actionConfig.addExceptionMapping(new ExceptionMappingConfig("xwork", "com.opensymphony.xwork2.XWorkException", "spooky"));        actionConfig.addExceptionMapping(new ExceptionMappingConfig("throwable", "java.lang.Throwable", "throwable"));        Mock actionProxy = new Mock(ActionProxy.class);        actionProxy.expectAndReturn("getConfig", actionConfig);        mockInvocation.expectAndReturn("getProxy", ((ActionProxy) actionProxy.proxy()));        invocation = (ActionInvocation) mockInvocation.proxy();    }    protected void setUp() throws Exception {        super.setUp();        stack = ValueStackFactory.getFactory().createValueStack();        mockInvocation = new Mock(ActionInvocation.class);        mockInvocation.expectAndReturn("getStack", stack);        mockInvocation.expectAndReturn("getInvocationContext", new ActionContext(new HashMap()));        interceptor = new ExceptionMappingInterceptor();        interceptor.init();    }    protected void tearDown() throws Exception {        super.tearDown();        interceptor.destroy();        invocation = null;        interceptor = null;        mockInvocation = null;        stack = null;    }}

⌨️ 快捷键说明

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