Java tutorial
/* * Copyright (c) 2007 NTT DATA Corporation * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package jp.terasoluna.fw.web.struts.action; import java.util.Iterator; import java.util.Locale; import javax.servlet.http.HttpServletRequest; import javax.servlet.jsp.PageContext; import jp.terasoluna.fw.exception.SystemException; import jp.terasoluna.fw.util.ExceptionUtil; import jp.terasoluna.fw.web.RequestUtil; import jp.terasoluna.utlib.LogUTUtil; import jp.terasoluna.utlib.MockHttpServletRequest; import jp.terasoluna.utlib.MockHttpServletResponse; import jp.terasoluna.utlib.MockHttpSession; import jp.terasoluna.utlib.MockServletContext; import jp.terasoluna.utlib.UTUtil; import junit.framework.TestCase; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.apache.struts.Globals; import org.apache.struts.action.ActionForward; import org.apache.struts.action.ActionMapping; import org.apache.struts.action.ActionMessage; import org.apache.struts.action.ActionMessages; import org.apache.struts.action.DynaActionForm; import org.apache.struts.config.ExceptionConfig; import org.apache.struts.util.MessageResources; import org.apache.struts.util.MessageResourcesFactory; import org.apache.struts.util.PropertyMessageResources; import org.apache.struts.util.PropertyMessageResourcesFactory; /** * {@link jp.terasoluna.fw.web.struts.action.SystemExceptionHandler} NXubN{bNXeXg?B * * <p> * <h4>?yNXTv?z</h4> * SystemExceptionO??NX?B<br> * VXeO?O?o G?[J?s?B<br> * ANV?sVXeO??AG?[??O?o??AYANV}bsO`VXeG?[J?B * <p> * * @see jp.terasoluna.fw.web.struts.action.SystemExceptionHandler */ public class SystemExceptionHandlerTest extends TestCase { /** * eXgP?[X?s * GUI AvP?[VN?B * * @param args java R}h?p??[^ */ public static void main(String[] args) { junit.swingui.TestRunner.run(SystemExceptionHandlerTest.class); } /** * ????s?B * * @throws Exception ?\bh?O * @see junit.framework.TestCase#setUp() */ @Override protected void setUp() throws Exception { super.setUp(); } /** * ?I???s?B * * @throws Exception ?\bh?O * @see junit.framework.TestCase#tearDown() */ @Override protected void tearDown() throws Exception { super.tearDown(); } /** * RXgN^?B * * @param name eXgP?[XO?B */ public SystemExceptionHandlerTest(String name) { super(name); } /** * testExecute01() * <br><br> * * ??n * <br> * _?FA * <br><br> * l?F(?) ex:Exception<br> * (?) eConfig:ExceptionConfig<br> * ?ypathtB?[h"/errorPath.do"??z<br> * (?) mapping:not null<br> * (?) formInstance:not null<br> * (?) request:not null<br> * (?) response:not null<br> * (?) bundle:null<br> * * <br> * l?F(l) ActionForward:tH??[h??F"/errorPath.do"<br> * (?) request:NGXg??F<br> * PageContext.EXCEPTION?Fnull<br> * * <br> * ?OSystemException?Apath?w?Apath?wpXANVtH??[h??AReLXg?[gtrue??B * <br> * * @throws Exception ?\bh?O */ public void testExecute01() throws Exception { // O?? // VXeOnh SystemExceptionHandler handler = new SystemExceptionHandler(); // VXeOO Exception e = new Exception(); // O?? ExceptionConfig eConfig = new ExceptionConfig(); eConfig.setPath("/errorPath.do"); eConfig.setKey("action.message.key"); ActionMapping mapping = new ActionMapping(); DynaActionForm form = new DynaActionForm(); MockHttpServletRequest req = new MockHttpServletRequest(); MockHttpServletResponse res = new MockHttpServletResponse(); MockHttpSession session = new MockHttpSession(); MockServletContext context = new MockServletContext(); // T?[ubgReLXgNGXgQ??B session.setServletContext(context); req.setSession(session); // eXg{ ActionForward forward = handler.execute(e, eConfig, mapping, form, req, res); // // lmF assertEquals("/errorPath.do", forward.getPath()); // NGXg?mF assertNull(req.getAttribute(PageContext.EXCEPTION)); } /** * testExecute02() * <br><br> * * ??n * <br> * _?FA * <br><br> * l?F(?) ex:Exception<br> * (?) eConfig:not null<br> * (?) mapping:not null<br> * ?yinputtB?[h"/errorInput.do"??z * (?) formInstance:not null<br> * (?) request:not null<br> * (?) response:not null<br> * (?) bundle:null<br> * * <br> * l?F(l) ActionForward:tH??[h??F"/errorInput.do"<br> * (?) request:NGXg??F<br> * PageContext.EXCEPTION?A<br> * Oo^?B<br> * * <br> * ?OSystemException?AANV}bsOinput?w?Ainput?wpXANVtH??[h?ReLXg?[gtrue??B * <br> * * @throws Exception ?\bh?O */ public void testExecute02() throws Exception { // O?? SystemExceptionHandler handler = new SystemExceptionHandler(); // VXeOO Exception e = new Exception(); // O?? ExceptionConfig eConfig = new ExceptionConfig(); eConfig.setKey("action.message.key"); // ANV}bsOInput?? ActionMapping mapping = new ActionMapping(); mapping.setInput("/errorInput.do"); DynaActionForm form = new DynaActionForm(); MockHttpServletRequest req = new MockHttpServletRequest(); MockHttpServletResponse res = new MockHttpServletResponse(); MockHttpSession session = new MockHttpSession(); MockServletContext context = new MockServletContext(); // T?[ubgReLXgNGXgQ??B session.setServletContext(context); req.setSession(session); // eXg{ ActionForward forward = handler.execute(e, eConfig, mapping, form, req, res); // // lmF assertEquals("/errorInput.do", forward.getPath()); // NGXg??mF assertNull(req.getAttribute(PageContext.EXCEPTION)); } /** * testExecute03() * <br><br> * * ?n * <br> * _?FA * <br><br> * l?F(?) ex:SystemException optionString=null<br> * (?) eConfig:ExceptionConfig<br> * ?ypathtB?[h"/errorPath.do"??z<br> * (?) mapping:not null<br> * ?yinputtB?[h"/errorInput.do"??z * (?) formInstance:not null<br> * (?) request:not null<br> * (?) response:not null<br> * (?) bundle:null<br> * (?) resources:?yNGXg??z<br> * Globals.MESSAGES_KEYMessageResourceMessageResources<br> * MessageResources?o<br> * SystemExceptionHandlerTest.error.message="O?bZ?[W"<br> * }<br> * * <br> * l?F(l) ActionForward:tH??[h??F"/errorPath.do"<br> * (?) request:NGXg??F<br> * PageContext.EXCEPTION?FSystemException<br> * (?) se:G?[?bZ?[W?F"O?bZ?[W"<br> * (?) ActionMessages?F"action.message.key"o^ * valuesnull * (?) ?O:?yG?[?O?z<br> * ??bZ?[W?<br> * "sessionHash = " + sessionHash<br> * ExceptionUtil.getStackTrace(se)<br> * * <br> * O??path??AANV}bsOinput?w?Apath?D??B * <br> * * @throws Exception ?\bh?O */ @SuppressWarnings("static-access") public void testExecute03() throws Exception { // O?? // VXeOnh SystemExceptionHandler handler = new SystemExceptionHandler(); // VXeO SystemException e = new SystemException(new Exception(), "SystemExceptionHandlerTest.error.message"); // O?? ExceptionConfig eConfig = new ExceptionConfig(); eConfig.setPath("/errorPath.do"); eConfig.setKey("action.message.key"); // ANV}bsOInput?? ActionMapping mapping = new ActionMapping(); mapping.setInput("/errorInput.do"); DynaActionForm form = new DynaActionForm(); MockHttpServletRequest req = new MockHttpServletRequest(); MockHttpServletResponse res = new MockHttpServletResponse(); MockHttpSession session = new MockHttpSession(); MockServletContext context = new MockServletContext(); // MessageResources? MessageResourcesFactory factory = PropertyMessageResourcesFactory.createFactory(); PropertyMessageResources requestResources = new PropertyMessageResources(factory, SystemExceptionHandler.class.getPackage().getName().replace('.', '/') + "/SystemExceptionHandler_MessageResources01"); // NGXgMessageResourceso^ req.setAttribute(Globals.MESSAGES_KEY, requestResources); // T?[ubgReLXgNGXgQ??B session.setServletContext(context); req.setSession(session); // eXg{ ActionForward forward = handler.execute(e, eConfig, mapping, form, req, res); // // lmF assertEquals("/errorPath.do", forward.getPath()); // NGXg?mF assertSame(e, req.getAttribute(PageContext.EXCEPTION)); // O?mF assertEquals("O?bZ?[W", UTUtil.getPrivateField(e, "message")); // ActionMessages?mF ActionMessages messages = (ActionMessages) req.getAttribute(Globals.ERROR_KEY); assertEquals(1, messages.size()); Iterator it = messages.get(); while (it.hasNext()) { ActionMessage actionMessage = (ActionMessage) it.next(); assertEquals("action.message.key", actionMessage.getKey()); assertNull(actionMessage.getValues()); } // ?O?omF String sessionHash = RequestUtil.getSessionHash(req); assertTrue(LogUTUtil.checkError("sessionHash = " + sessionHash)); assertTrue(LogUTUtil.checkError(ExceptionUtil.getStackTrace(e))); } /** * testExecute04() * <br><br> * * ?n * <br> * _?FC * <br><br> * l?F(?) ex:SystemException optionString=null<br> * (?) eConfig:not null<br> * (?) mapping:not null<br> * (?) formInstance:not null<br> * (?) request:not null<br> * (?) response:not null<br> * (?) bundle:null<br> * (?) resources:?yNGXg??z<br> * Globals.MESSAGES_KEYMessageResourceMessageResources<br> * MessageResources?o<br> * SystemExceptionHandlerTest.error.message="O?bZ?[W"<br> * ?p<br> * * <br> * l?F(l) ActionForward:tH??[h??Fnull<br> * (?) request:NGXg??F<br> * PageContext.EXCEPTION?A<br> * SystemExceptiono^?B<br> * (?) se:G?[?bZ?[W?F"O?bZ?[W"<br> * (?) ActionMessages?F"action.message.key"o^ * valuesnull * (?) ?O:?yG?[?O?z<br> * ??bZ?[W?<br> * "sessionHash = " + sessionHash<br> * ExceptionUtil.getStackTrace(se)<br> * * <br> * O??path??AANV}bsOinput?null?AANVtH??[hJ?null?B * <br> * * @throws Exception ?\bh?O */ @SuppressWarnings("static-access") public void testExecute04() throws Exception { // O?? // VXeOnh SystemExceptionHandler handler = new SystemExceptionHandler(); // VXeO SystemException e = new SystemException(new Exception(), "SystemExceptionHandlerTest.error.message"); ExceptionConfig eConfig = new ExceptionConfig(); eConfig.setKey("action.message.key"); ActionMapping mapping = new ActionMapping(); DynaActionForm form = new DynaActionForm(); MockHttpServletRequest req = new MockHttpServletRequest(); MockHttpServletResponse res = new MockHttpServletResponse(); MockHttpSession session = new MockHttpSession(); MockServletContext context = new MockServletContext(); // MessageResources MessageResourcesFactory factory = PropertyMessageResourcesFactory.createFactory(); PropertyMessageResources requestResources = new PropertyMessageResources(factory, SystemExceptionHandler.class.getPackage().getName().replace('.', '/') + "/SystemExceptionHandler_MessageResources01"); // NGXgMessageResources? req.setAttribute(Globals.MESSAGES_KEY, requestResources); // T?[ubgReLXgNGXgQ??B session.setServletContext(context); req.setSession(session); // eXg{ ActionForward forward = handler.execute(e, eConfig, mapping, form, req, res); // // lmF assertNull(forward.getPath()); // NGXg??mF assertSame(e, req.getAttribute(PageContext.EXCEPTION)); // VXeO?mF?B assertEquals("O?bZ?[W", UTUtil.getPrivateField(e, "message")); // ActionMessages?mF ActionMessages messages = (ActionMessages) req.getAttribute(Globals.ERROR_KEY); assertEquals(1, messages.size()); Iterator it = messages.get(); while (it.hasNext()) { ActionMessage actionMessage = (ActionMessage) it.next(); assertEquals("action.message.key", actionMessage.getKey()); assertNull(actionMessage.getValues()); } // ?O?omF String sessionHash = RequestUtil.getSessionHash(req); assertTrue(LogUTUtil.checkError("sessionHash = " + sessionHash)); assertTrue(LogUTUtil.checkError(ExceptionUtil.getStackTrace(e))); } /** * testExecute05() * <br><br> * * ??n * <br> * _?FC * <br><br> * l?F(?) ex:SystemException optionString=null<br> * (?) eConfig:ExceptionConfig<br> * ?ypathtB?[h"/errorPath.do"??z<br> * (?) mapping:MockActionMapping<br> * (?) formInstance:not null<br> * (?) request:not null<br> * (?) response:not null<br> * (?) bundle:Globals.MESSAGE_KEY<br> * (?) resources:?yNGXg??z<br> * Globals.MESSAGES_KEYMessageResourceMessageResources<br> * MessageResources?o<br> * SystemExceptionHandlerTest.error.message ="O?bZ?[W"<br> * ?p<br> * * <br> * l?F(l) ActionForward:tH??[h??F"/errorPath.do"<br> * (?) request:NGXg??F<br> * PageContext.EXCEPTION?A<br> * SystemExceptiono^?B<br> * (?) se:G?[?bZ?[W?F"O?bZ?[W"<br> * ??B<br> * (?) ActionMessages?F"action.message.key"o^ * valuesnull * (?) ?O:?yG?[?O?z<br> * ??bZ?[W?<br> * "sessionHash = " + sessionHash<br> * ExceptionUtil.getStackTrace(se)<br> * * <br> * O??bundle?Globals.MESSAGE_KEY?AL?[Globals.MESSAGE_KEYMessageResourcesp?B * <br> * * @throws Exception ?\bh?O */ @SuppressWarnings("static-access") public void testExecute05() throws Exception { // O?? // VXeOnh SystemExceptionHandler handler = new SystemExceptionHandler(); // VXeO SystemException e = new SystemException(new Exception(), "SystemExceptionHandlerTest.error.message"); // O?? ExceptionConfig eConfig = new ExceptionConfig(); eConfig.setPath("/errorPath.do"); // ANV?bZ?[WL?[w eConfig.setKey("action.message.key"); // ?bZ?[W\?[XohL?[w eConfig.setBundle(Globals.MESSAGES_KEY); ActionMapping mapping = new ActionMapping(); DynaActionForm form = new DynaActionForm(); MockHttpServletRequest req = new MockHttpServletRequest(); MockHttpServletResponse res = new MockHttpServletResponse(); MockHttpSession session = new MockHttpSession(); MockServletContext context = new MockServletContext(); // MessageResources MessageResourcesFactory factory = PropertyMessageResourcesFactory.createFactory(); PropertyMessageResources requestResources = new PropertyMessageResources(factory, SystemExceptionHandler.class.getPackage().getName().replace('.', '/') + "/SystemExceptionHandler_MessageResources01"); // NGXgMessageResourceso^ req.setAttribute(Globals.MESSAGES_KEY, requestResources); // T?[ubgReLXgNGXgQ??B session.setServletContext(context); req.setSession(session); // eXg{ ActionForward forward = handler.execute(e, eConfig, mapping, form, req, res); // // lmF assertEquals("/errorPath.do", forward.getPath()); // NGXg??mF assertSame(e, req.getAttribute(PageContext.EXCEPTION)); // VXeO?mF assertEquals("O?bZ?[W", UTUtil.getPrivateField(e, "message")); // ActionMessages?mF ActionMessages messages = (ActionMessages) req.getAttribute(Globals.ERROR_KEY); assertEquals(1, messages.size()); Iterator it = messages.get(); while (it.hasNext()) { ActionMessage actionMessage = (ActionMessage) it.next(); assertEquals("action.message.key", actionMessage.getKey()); assertNull(actionMessage.getValues()); } // ?O?omF String sessionHash = RequestUtil.getSessionHash(req); assertTrue(LogUTUtil.checkError("sessionHash = " + sessionHash)); assertTrue(LogUTUtil.checkError(ExceptionUtil.getStackTrace(e))); } /** * testExecute06() * <br><br> * * ??n * <br> * _?FA * <br><br> * l?F(?) ex:SystemException optionString=null<br> * (?) eConfig:ExceptionConfig<br> * ?ypathtB?[h"/errorPath.do"??z<br> * (?) mapping:MockActionMapping<br> * (?) formInstance:not null<br> * (?) request:not null<br> * (?) response:not null<br> * (?) bundle:anotherKey<br> * (?) resources:?yNGXg??z<br> * Globals.MESSAGES_KEYMessageResourceMessageResources<br> * MessageResources?o<br> * SystemExceptionHandlerTest.error.message ="O?bZ?[W"<br> * ?p<br> * anotherKeynull<br> * ?yT?[ubgReLXg??z<br> * anotherKeyMessageResourceMessageResources<br> * MessageResources?o<br> * SystemExceptionHandlerTest.error.message ="T?[ubgO?bZ?[W"<br> * ?p<br> * <br> * l?F(l) ActionForward:tH??[h??F"/errorPath.do"<br> * (?) request:NGXg??F<br> * PageContext.EXCEPTION:<br> * SystemExceptiono^?B<br> * (?) se:G?[?bZ?[W?F"T?[ubgO?bZ?[W"<br> * (?) ActionMessages?F"action.message.key"o^ * valuesnull * (?) ?O:?yG?[?O?z<br> * ??bZ?[W?<br> * "sessionHash = " + sessionHash<br> * ExceptionUtil.getStackTrace(se)<br> * * <br> * O??bundle??AT?[ubgReLXgL?[?AT?[ubgReLXgi[?bZ?[W\?[X<br> * p?B * <br> * * @throws Exception ?\bh?O */ @SuppressWarnings("static-access") public void testExecute06() throws Exception { // O?? // VXeOnh SystemExceptionHandler handler = new SystemExceptionHandler(); // VXeO SystemException e = new SystemException(new Exception(), "SystemExceptionHandlerTest.error.message"); // O?? ExceptionConfig eConfig = new ExceptionConfig(); eConfig.setPath("/errorPath.do"); // ?bZ?[W\?[XohL?[w eConfig.setBundle("anotherKey"); // ANV?bZ?[WL?[ eConfig.setKey("action.message.key"); ActionMapping mapping = new ActionMapping(); DynaActionForm form = new DynaActionForm(); MockHttpServletRequest req = new MockHttpServletRequest(); MockHttpServletResponse res = new MockHttpServletResponse(); MockHttpSession session = new MockHttpSession(); MockServletContext context = new MockServletContext(); // MessageResources MessageResourcesFactory factory = PropertyMessageResourcesFactory.createFactory(); PropertyMessageResources requestResources = new PropertyMessageResources(factory, SystemExceptionHandler.class.getPackage().getName().replace('.', '/') + "/SystemExceptionHandler_MessageResources01"); PropertyMessageResources servletResources = new PropertyMessageResources(factory, SystemExceptionHandler.class.getPackage().getName().replace('.', '/') + "/SystemExceptionHandler_MessageResources02"); // T?[ubgReLXgMessageResourceso^ context.setAttribute("anotherKey", servletResources); // NGXg?bZ?[W\?[Xo^ req.setAttribute(Globals.MESSAGES_KEY, requestResources); // T?[ubgReLXgNGXgQ??B session.setServletContext(context); req.setSession(session); // eXg{ ActionForward forward = handler.execute(e, eConfig, mapping, form, req, res); // // lmF assertEquals("/errorPath.do", forward.getPath()); // NGXg??mF assertSame(e, req.getAttribute(PageContext.EXCEPTION)); // ActionMessages?mF ActionMessages messages = (ActionMessages) req.getAttribute(Globals.ERROR_KEY); assertEquals(1, messages.size()); Iterator it = messages.get(); while (it.hasNext()) { ActionMessage actionMessage = (ActionMessage) it.next(); assertEquals("action.message.key", actionMessage.getKey()); assertNull(actionMessage.getValues()); } // VXeO?mF assertEquals("T?[ubgO?bZ?[W", UTUtil.getPrivateField(e, "message")); // ?O?omF String sessionHash = RequestUtil.getSessionHash(req); assertTrue(LogUTUtil.checkError("sessionHash = " + sessionHash)); assertTrue(LogUTUtil.checkError(ExceptionUtil.getStackTrace(e))); } /** * testExecute07() * <br><br> * * ??n * <br> * _?FC * <br><br> * l?F(?) ex:SystemException optionString=null<br> * (?) eConfig:ExceptionConfig<br> * ?ypathtB?[h"/errorPath.do"??z<br> * (?) mapping:MockActionMapping<br> * (?) formInstance:not null<br> * (?) request:not null<br> * (?) response:not null<br> * (?) bundle:null<br> * (?) resources:?yNGXg??z<br> * Globals.MESSAGES_KEYnull<br> * ?yT?[ubgReLXg??z<br> * Globals.MESSAGES_KEYnull<br> * <br> * l?F(l) ActionForward:tH??[h??F"/errorPath.do"<br> * (?) request:NGXg??F<br> * PageContext.EXCEPTION?A<br> * SystemExceptiono^?B<br> * (?) se:G?[?bZ?[W?F"SystemExceptionHandlerTest.error.message"<br> * (?) ActionMessages?F"action.message.key"o^ * resourcetB?[hfalse * valuesnull * (?) ?O:?yG?[?O?z<br> * ??bZ?[W?<br> * "sessionHash = " + sessionHash<br> * ExceptionUtil.getStackTrace(se)<br> * * <br> * ?bZ?[W\?[X?AG?[R?[h?bZ?[W??B * <br> * * @throws Exception ?\bh?O */ public void testExecute07() throws Exception { // O?? // VXeOnh SystemExceptionHandler handler = new SystemExceptionHandler(); // VXeO SystemException e = new SystemException(new Exception(), "SystemExceptionHandlerTest.error.message"); // O?? ExceptionConfig eConfig = new ExceptionConfig(); eConfig.setPath("/errorPath.do"); eConfig.setKey("action.message.key"); ActionMapping mapping = new ActionMapping(); DynaActionForm form = new DynaActionForm(); MockHttpServletRequest req = new MockHttpServletRequest(); MockHttpServletResponse res = new MockHttpServletResponse(); MockHttpSession session = new MockHttpSession(); MockServletContext context = new MockServletContext(); // ?bZ?[W\?[X? // T?[ubgReLXgNGXgQ??B session.setServletContext(context); req.setSession(session); // eXg{ ActionForward forward = handler.execute(e, eConfig, mapping, form, req, res); // // lmF assertEquals("/errorPath.do", forward.getPath()); // NGXg??mF assertSame(e, req.getAttribute(PageContext.EXCEPTION)); // VXeO?mF assertEquals("SystemExceptionHandlerTest.error.message", UTUtil.getPrivateField(e, "message")); // ActionMessages?mF ActionMessages messages = (ActionMessages) req.getAttribute(Globals.ERROR_KEY); assertEquals(1, messages.size()); Iterator it = messages.get(); while (it.hasNext()) { ActionMessage actionMessage = (ActionMessage) it.next(); assertEquals("action.message.key", actionMessage.getKey()); assertFalse(actionMessage.isResource()); assertNull(actionMessage.getValues()); } // ?O?omF String sessionHash = RequestUtil.getSessionHash(req); assertTrue(LogUTUtil.checkError("sessionHash = " + sessionHash)); assertTrue(LogUTUtil.checkError(ExceptionUtil.getStackTrace(e))); } /** * testExecute08() * <br><br> * * ??n * <br> * _?FC * <br><br> * l?F(?) ex:SystemException optionString=null<br> * (?) eConfig:ExceptionConfig<br> * ?ypathtB?[h"/errorPath.do"??z<br> * (?) mapping:MockActionMapping<br> * (?) formInstance:not null<br> * (?) request:not null<br> * (?) response:not null<br> * (?) bundle:null<br> * (?) resources:?yNGXg??z<br> * Globals.MESSAGES_KEYMessageResourceMessageResources<br> * MessageResources?o<br> * "aaaaa"?bZ?[WL?[o^<br> * ?p<br> * * <br> * l?F(l) ActionForward:tH??[h??F"/errorPath.do"<br> * (?) request:NGXg??F<br> * PageContext.EXCEPTION?A<br> * SystemExceptiono^?B<br> * (?) se:G?[?bZ?[W?F(null)<br> * (?) ActionMessages?F"action.message.key"o^ * valuesnull * (?) ?O:?yG?[?O?z<br> * ??bZ?[W?<br> * "sessionHash = " + sessionHash<br> * ExceptionUtil.getStackTrace(se)<br> * * <br> * ?bZ?[W?Anull??B * <br> * * @throws Exception ?\bh?O */ @SuppressWarnings("static-access") public void testExecute08() throws Exception { // O?? // VXeOnh SystemExceptionHandler handler = new SystemExceptionHandler(); // VXeO SystemException e = new SystemException(new Exception(), "aaaaa"); // O?? ExceptionConfig eConfig = new ExceptionConfig(); eConfig.setPath("/errorPath.do"); eConfig.setKey("action.message.key"); ActionMapping mapping = new ActionMapping(); DynaActionForm form = new DynaActionForm(); MockHttpServletRequest req = new MockHttpServletRequest(); MockHttpServletResponse res = new MockHttpServletResponse(); MockHttpSession session = new MockHttpSession(); MockServletContext context = new MockServletContext(); // MessageResources. returnNulltrue? MessageResourcesFactory factory = PropertyMessageResourcesFactory.createFactory(); PropertyMessageResources requestResources = new PropertyMessageResources(factory, SystemExceptionHandler.class.getPackage().getName().replace('.', '/') + "/SystemExceptionHandler_MessageResources01", true); // NGXgMessageResourceso^ req.setAttribute(Globals.MESSAGES_KEY, requestResources); // T?[ubgReLXgNGXgQ??B session.setServletContext(context); req.setSession(session); // eXg{ ActionForward forward = handler.execute(e, eConfig, mapping, form, req, res); // // lmF assertEquals("/errorPath.do", forward.getPath()); // NGXg??mF assertSame(e, req.getAttribute(PageContext.EXCEPTION)); // VXeO?mF // getMessage()Pgetter?AnullG?[R?[hp? assertNull(UTUtil.getPrivateField(e, "message")); // ActionMessages?mF ActionMessages messages = (ActionMessages) req.getAttribute(Globals.ERROR_KEY); assertEquals(1, messages.size()); Iterator it = messages.get(); while (it.hasNext()) { ActionMessage actionMessage = (ActionMessage) it.next(); assertEquals("action.message.key", actionMessage.getKey()); assertNull(actionMessage.getValues()); } // ?O?omF String sessionHash = RequestUtil.getSessionHash(req); assertTrue(LogUTUtil.checkError("sessionHash = " + sessionHash)); assertTrue(LogUTUtil.checkError(ExceptionUtil.getStackTrace(e))); } /** * testExecute09() * <br><br> * * ??n * <br> * _?FA * <br><br> * l?F(?) ex:SystemException optionString=null<br> * (?) eConfig:ExceptionConfigEx<br> * ?ypathtB?[h"/errorPath.do"??z<br> * (?) mapping:MockActionMapping<br> * (?) formInstance:not null<br> * (?) request:not null<br> * (?) response:not null<br> * (?) bundle:null<br> * (?) resources:?yNGXg??z<br> * Globals.MESSAGES_KEYMessageResourceMessageResources<br> * MessageResources?o<br> * SystemExceptionHandlerTest.error.message.null = null<br> * ?p<br> * * <br> * l?F(l) ActionForward:tH??[h??F"/errorPath.do"<br> * O??W?[?FANVpXW?[<br> * (?) request:NGXg??F<br> * PageContext.EXCEPTION?A<br> * SystemExceptiono^?B<br> * (?) se:G?[?bZ?[W?F"error.msg"<br> * (?) ActionMessages?F"action.message.key"o^ * valuesnull * (?) ?O:?yG?[?O?z<br> * ??bZ?[W?<br> * "sessionHash = " + sessionHash<br> * ExceptionUtil.getStackTrace(se)<br> * * <br> * O??ExceptionConfigEx???AO??W?[ANVpXW?[??B * <br> * * @throws Exception ?\bh?O */ @SuppressWarnings("static-access") public void testExecute09() throws Exception { // O?? // VXeOnh SystemExceptionHandler handler = new SystemExceptionHandler(); // VXeO SystemException e = new SystemException(new Exception(), "SystemExceptionHandlerTest.error.message"); // O?? ExceptionConfigEx eConfig = new ExceptionConfigEx(); eConfig.setModule("error"); eConfig.setPath("/errorPath.do"); eConfig.setKey("action.message.key"); ActionMapping mapping = new ActionMapping(); DynaActionForm form = new DynaActionForm(); MockHttpServletRequest req = new MockHttpServletRequest(); MockHttpServletResponse res = new MockHttpServletResponse(); MockHttpSession session = new MockHttpSession(); MockServletContext context = new MockServletContext(); // MessageResources MessageResourcesFactory factory = PropertyMessageResourcesFactory.createFactory(); PropertyMessageResources requestResources = new PropertyMessageResources(factory, SystemExceptionHandler.class.getPackage().getName().replace('.', '/') + "/SystemExceptionHandler_MessageResources01"); // NGXgMessageResourceso^ req.setAttribute(Globals.MESSAGES_KEY, requestResources); // T?[ubgReLXgNGXgQ??B session.setServletContext(context); req.setSession(session); // eXg{ ActionForward forward = handler.execute(e, eConfig, mapping, form, req, res); // // lmF assertEquals("/errorPath.do", forward.getPath()); assertEquals("error", forward.getModule()); // NGXg??mF assertSame(e, req.getAttribute(PageContext.EXCEPTION)); // VXeO?mF assertEquals("O?bZ?[W", UTUtil.getPrivateField(e, "message")); // ActionMessages?mF ActionMessages messages = (ActionMessages) req.getAttribute(Globals.ERROR_KEY); assertEquals(1, messages.size()); Iterator it = messages.get(); while (it.hasNext()) { ActionMessage actionMessage = (ActionMessage) it.next(); assertEquals("action.message.key", actionMessage.getKey()); assertNull(actionMessage.getValues()); } // ?O?omF String sessionHash = RequestUtil.getSessionHash(req); assertTrue(LogUTUtil.checkError("sessionHash = " + sessionHash)); assertTrue(LogUTUtil.checkError(ExceptionUtil.getStackTrace(e))); } /** * testExecute10() * <br><br> * * ??n * <br> * _?FC * <br><br> * l?F(?) ex:SystemException optionString=null<br> * (?) eConfig:ExceptionConfig<br> * ?ypathtB?[h"/errorPath.do"??z<br> * (?) mapping:MockActionMapping<br> * (?) formInstance:not null<br> * (?) request:not null<br> * (?) response:not null<br> * (?) bundle:null<br> * (?) resources:?yNGXg??z<br> * Globals.MESSAGES_KEYMessageResourceMessageResources<br> * MessageResources?o<br> * SystemExceptionHandlerTest.error.message.empty * ?p<br> * * <br> * l?F(l) ActionForward:tH??[h??F"/errorPath.do"<br> * (?) request:NGXg??F<br> * PageContext.EXCEPTION?A<br> * SystemExceptiono^?B<br> * (?) se:G?[?bZ?[W?F""<br> * (?) ActionMessages?F"action.message.key"o^ * valuesnull * (?) ?O:?yG?[?O?z<br> * ??bZ?[W?<br> * "sessionHash = " + sessionHash<br> * ExceptionUtil.getStackTrace(se)<br> * * <br> * ?bZ?[W?A??B<br> * <br> * * @throws Exception ?\bh?O */ @SuppressWarnings("static-access") public void testExecute10() throws Exception { // O?? // VXeOnh SystemExceptionHandler handler = new SystemExceptionHandler(); // VXeO SystemException e = new SystemException(new Exception(), "SystemExceptionHandlerTest.error.message.empty"); // O?? ExceptionConfig eConfig = new ExceptionConfig(); eConfig.setPath("/errorPath.do"); eConfig.setKey("action.message.key"); ActionMapping mapping = new ActionMapping(); DynaActionForm form = new DynaActionForm(); MockHttpServletRequest req = new MockHttpServletRequest(); MockHttpServletResponse res = new MockHttpServletResponse(); MockHttpSession session = new MockHttpSession(); MockServletContext context = new MockServletContext(); // MessageResources MessageResourcesFactory factory = PropertyMessageResourcesFactory.createFactory(); PropertyMessageResources requestResources = new PropertyMessageResources(factory, SystemExceptionHandler.class.getPackage().getName().replace('.', '/') + "/SystemExceptionHandler_MessageResources01"); // NGXgMessageResourceso^ req.setAttribute(Globals.MESSAGES_KEY, requestResources); // T?[ubgReLXgNGXgQ??B session.setServletContext(context); req.setSession(session); // eXg{ ActionForward forward = handler.execute(e, eConfig, mapping, form, req, res); // // lmF assertEquals("/errorPath.do", forward.getPath()); // NGXg??mF assertSame(e, req.getAttribute(PageContext.EXCEPTION)); // VXeO?mF assertEquals("", UTUtil.getPrivateField(e, "message")); // ActionMessages?mF ActionMessages messages = (ActionMessages) req.getAttribute(Globals.ERROR_KEY); assertEquals(1, messages.size()); Iterator it = messages.get(); while (it.hasNext()) { ActionMessage actionMessage = (ActionMessage) it.next(); assertEquals("action.message.key", actionMessage.getKey()); assertNull(actionMessage.getValues()); } // ?O?omF String sessionHash = RequestUtil.getSessionHash(req); assertTrue(LogUTUtil.checkError("sessionHash = " + sessionHash)); assertTrue(LogUTUtil.checkError(ExceptionUtil.getStackTrace(e))); } /** * testExecute11() * <br><br> * * ?n * <br> * _?FA * <br><br> * l?F(?) ex:SystemException optionString={"1","22","333","4444","5555"}<br> * (?) eConfig:ExceptionConfig<br> * ?ypathtB?[h"/errorPath.do"??z<br> * (?) mapping:not null<br> * ?yinputtB?[h"/errorInput.do"??z * (?) formInstance:not null<br> * (?) request:not null<br> * (?) response:not null<br> * (?) bundle:null<br> * (?) resources:?yNGXg??z<br> * Globals.MESSAGES_KEYMessageResourceMessageResources<br> * MessageResources?o<br> * SystemExceptionHandlerTest.error.message="O?bZ?[W"<br> * }<br> * * <br> * l?F(l) ActionForward:tH??[h??F"/errorPath.do"<br> * (?) request:NGXg??F<br> * PageContext.EXCEPTION?FSystemException<br> * (?) se:G?[?bZ?[W?F"O?bZ?[W"<br> * (?) ActionMessages?F"action.message.key"o^ * values{"1","22","333","4444","5555"} * * (?) ?O:?yG?[?O?z<br> * ??bZ?[W?<br> * "sessionHash = " + sessionHash<br> * ExceptionUtil.getStackTrace(se)<br> * * <br> * O??path??AANV}bsOinput?w?Apath?D??B * <br> * * @throws Exception ?\bh?O */ @SuppressWarnings("static-access") public void testExecute11() throws Exception { // O?? // VXeOnh SystemExceptionHandler handler = new SystemExceptionHandler(); String[] options = { "1", "22", "333", "4444", "5555" }; // VXeO SystemException e = new SystemException(new Exception(), "SystemExceptionHandlerTest.error.message", options); // O?? ExceptionConfig eConfig = new ExceptionConfig(); eConfig.setPath("/errorPath.do"); eConfig.setKey("action.message.key"); // ANV}bsOInput?? ActionMapping mapping = new ActionMapping(); mapping.setInput("/errorInput.do"); DynaActionForm form = new DynaActionForm(); MockHttpServletRequest req = new MockHttpServletRequest(); MockHttpServletResponse res = new MockHttpServletResponse(); MockHttpSession session = new MockHttpSession(); MockServletContext context = new MockServletContext(); // MessageResources? MessageResourcesFactory factory = PropertyMessageResourcesFactory.createFactory(); PropertyMessageResources requestResources = new PropertyMessageResources(factory, SystemExceptionHandler.class.getPackage().getName().replace('.', '/') + "/SystemExceptionHandler_MessageResources01"); // NGXgMessageResourceso^ req.setAttribute(Globals.MESSAGES_KEY, requestResources); // T?[ubgReLXgNGXgQ??B session.setServletContext(context); req.setSession(session); // eXg{ ActionForward forward = handler.execute(e, eConfig, mapping, form, req, res); // // lmF assertEquals("/errorPath.do", forward.getPath()); // NGXg?mF assertSame(e, req.getAttribute(PageContext.EXCEPTION)); // O?mF assertEquals("O?bZ?[W", UTUtil.getPrivateField(e, "message")); // ActionMessages?mF ActionMessages messages = (ActionMessages) req.getAttribute(Globals.ERROR_KEY); assertEquals(1, messages.size()); Iterator it = messages.get(); while (it.hasNext()) { ActionMessage actionMessage = (ActionMessage) it.next(); assertEquals("action.message.key", actionMessage.getKey()); assertNotNull(actionMessage.getValues()); assertEquals(options, actionMessage.getValues()); } // ?O?omF String sessionHash = RequestUtil.getSessionHash(req); assertTrue(LogUTUtil.checkError("sessionHash = " + sessionHash)); assertTrue(LogUTUtil.checkError(ExceptionUtil.getStackTrace(e))); } /** * testGetErrorMessage01() * <br><br> * * ??n * <br> * _?FC * <br><br> * l?F(?) req:not null<br> * (?) se:not null<br> * (?) errorCode:null<br> * * <br> * l?F(l) String:null<br> * * <br> * G?[R?[hnull?A?bZ?[Wnullp?B * <br> * * @throws Exception ?\bh?O */ @SuppressWarnings("static-access") public void testGetErrorMessage01() throws Exception { // O?? // VXeOnh SystemExceptionHandler handler = new SystemExceptionHandler(); // VXeO(G?[R?[hnull) SystemException e = new SystemException(new Exception(), null); MockHttpServletRequest req = new MockHttpServletRequest(); // MessageResources MessageResourcesFactory factory = PropertyMessageResourcesFactory.createFactory(); PropertyMessageResources requestResources = new PropertyMessageResources(factory, SystemExceptionHandler.class.getPackage().getName().replace('.', '/') + "/SystemExceptionHandler_MessageResources01"); // ?o? Class[] clazz = { HttpServletRequest.class, SystemException.class, MessageResources.class }; Object[] args = { req, e, requestResources }; // eXg{ Object result = UTUtil.invokePrivate(handler, "getErrorMessage", clazz, args); // assertNull(result); } /** * testGetErrorMessage02() * <br><br> * * ??n * <br> * _?FC * <br><br> * l?F(?) req:not null<br> * (?) se:not null<br> * (?) resource:key="{0}ftHg?bZ?[W"<br> * en_US.key="{0}message"<br> * (?) locale:null<br> * (?) errorCode:"key"<br> * (?) option:null<br> * * <br> * l?F(l) String:"{0}ftHg?bZ?[W"<br> * * <br> * ?P?[?Aunull?AftHg?P?[?bZ?[Wu?o?B * <br> * * @throws Exception ?\bh?O */ @SuppressWarnings("static-access") public void testGetErrorMessage02() throws Exception { // O?? // VXeOnh SystemExceptionHandler handler = new SystemExceptionHandler(); // VXeO(G?[R?[hnull) SystemException e = new SystemException(new Exception(), "SystemExceptionHandlerTest.key"); MockHttpServletRequest req = new MockHttpServletRequest(); // MessageResources MessageResourcesFactory factory = PropertyMessageResourcesFactory.createFactory(); PropertyMessageResources requestResources = new PropertyMessageResources(factory, SystemExceptionHandler.class.getPackage().getName().replace('.', '/') + "/SystemExceptionHandler_MessageResources01"); // ?o? Class[] clazz = { HttpServletRequest.class, SystemException.class, MessageResources.class }; Object[] args = { req, e, requestResources }; // eXg{ // private?\bh??s Object result = UTUtil.invokePrivate(handler, "getErrorMessage", clazz, args); // assertEquals("{0}ftHg?bZ?[W", (String) result); } /** * testGetErrorMessage03() * <br><br> * * ??n * <br> * _?FC * <br><br> * l?F(?) req:not null<br> * (?) se:not null<br> * (?) resource:key="{0}ftHg?bZ?[W"<br> * en_US.key="{0}message"<br> * (?) locale:"en_US"<br> * (?) errorCode:"key"<br> * (?) option:null<br> * * <br> * l?F(l) String:"{0}message"<br> * * <br> * ?P?[w?Aw?P?[?bZ?[W?o?B * <br> * * @throws Exception ?\bh?O */ @SuppressWarnings("static-access") public void testGetErrorMessage03() throws Exception { // O?? // VXeOnh SystemExceptionHandler handler = new SystemExceptionHandler(); // VXeO SystemException e = new SystemException(new Exception(), "SystemExceptionHandlerTest.key"); MockHttpServletRequest req = new MockHttpServletRequest(); Locale locale = new Locale("en_US"); req.setLocale(locale); // MessageResources MessageResourcesFactory factory = PropertyMessageResourcesFactory.createFactory(); PropertyMessageResources requestResources = new PropertyMessageResources(factory, SystemExceptionHandler.class.getPackage().getName().replace('.', '/') + "/SystemExceptionHandler_MessageResources01"); // ?o? Class[] clazz = { HttpServletRequest.class, SystemException.class, MessageResources.class }; Object[] args = { req, e, requestResources }; // eXg{ Object result = UTUtil.invokePrivate(handler, "getErrorMessage", clazz, args); // assertEquals("{0}message", (String) result); } /** * testGetErrorMessage04() * <br><br> * * ??n * <br> * _?FC * <br><br> * l?F(?) req:not null<br> * (?) se:not null<br> * (?) resource:key="{0}ftHg?bZ?[W"<br> * en_US.key="{0}message"<br> * (?) locale:null<br> * (?) errorCode:"key"<br> * (?) option:{"option1"}<br> * * <br> * l?F(l) String:"option1ftHg?bZ?[W"<br> * * <br> * uw?A?bZ?[Wu?s?B * <br> * * @throws Exception ?\bh?O */ @SuppressWarnings("static-access") public void testGetErrorMessage04() throws Exception { // O?? // VXeOnh SystemExceptionHandler handler = new SystemExceptionHandler(); // G?[?bZ?[Wu?bZ?[W String[] options = { "option1" }; // VXeO SystemException e = new SystemException(new Exception(), "SystemExceptionHandlerTest.key", options); MockHttpServletRequest req = new MockHttpServletRequest(); // MessageResources MessageResourcesFactory factory = PropertyMessageResourcesFactory.createFactory(); PropertyMessageResources requestResources = new PropertyMessageResources(factory, SystemExceptionHandler.class.getPackage().getName().replace('.', '/') + "/SystemExceptionHandler_MessageResources01"); // ?o? Class[] clazz = { HttpServletRequest.class, SystemException.class, MessageResources.class }; Object[] args = { req, e, requestResources }; // eXg{ Object result = UTUtil.invokePrivate(handler, "getErrorMessage", clazz, args); // assertEquals("option1ftHg?bZ?[W", (String) result); } /** * testGetErrorMessage05() * <br><br> * * ??n * <br> * _?FC * <br><br> * l?F(?) req:not null<br> * (?) se:not null<br> * (?) resource:key="{0}ftHg?bZ?[W"<br> * en_US.key="{0}message"<br> * (?) locale:"en_US"<br> * (?) errorCode:"key"<br> * (?) option:{"option1"}<br> * * <br> * l?F(l) String:"option1message"<br> * * <br> * ?P?[?Auw?Aw?P?[?bZ?[Wu?o?B * <br> * * @throws Exception ?\bh?O */ @SuppressWarnings("static-access") public void testGetErrorMessage05() throws Exception { // O?? // VXeOnh SystemExceptionHandler handler = new SystemExceptionHandler(); // G?[?bZ?[Wu?bZ?[W String[] options = { "option1" }; // VXeO SystemException e = new SystemException(new Exception(), "SystemExceptionHandlerTest.key", options); // [NGXg MockHttpServletRequest req = new MockHttpServletRequest(); // ?P?[? Locale locale = new Locale("en_US"); req.setLocale(locale); // MessageResources MessageResourcesFactory factory = PropertyMessageResourcesFactory.createFactory(); PropertyMessageResources requestResources = new PropertyMessageResources(factory, SystemExceptionHandler.class.getPackage().getName().replace('.', '/') + "/SystemExceptionHandler_MessageResources01"); // ?o? Class[] clazz = { HttpServletRequest.class, SystemException.class, MessageResources.class }; Object[] args = { req, e, requestResources }; // eXg{ Object result = UTUtil.invokePrivate(handler, "getErrorMessage", clazz, args); // assertEquals("option1message", (String) result); } /** * testGetLogger01() * <br><br> * * ??n * <br> * _?FA * <br><br> * l?F(O??) log:-<br> * <br> * l?F(l) LogFactory.getLog(SystemExceptionHandler.class)CX^X<br> * <br> * ??n1?eXg * <br> * * @throws Exception ?\bh?O */ public void testGetLogger01() throws Exception { // O?? // VXeOnh SystemExceptionHandler handler = new SystemExceptionHandler(); // eXg{ Log logger = handler.getLogger(); // assertSame(LogFactory.getLog(SystemExceptionHandler.class), logger); } }