diff --git a/core/src/main/java/org/apache/struts2/interceptor/I18nInterceptor.java b/core/src/main/java/org/apache/struts2/interceptor/I18nInterceptor.java index ba4993660..682bbe6d7 100644 --- a/core/src/main/java/org/apache/struts2/interceptor/I18nInterceptor.java +++ b/core/src/main/java/org/apache/struts2/interceptor/I18nInterceptor.java @@ -281,7 +281,6 @@ public class I18nInterceptor extends AbstractInterceptor { @Override public Locale store(ActionInvocation invocation, Locale locale) { - LOG.debug("Do not create session if it doesn't exist"); HttpSession session = ServletActionContext.getRequest().getSession(false); if (session != null) { @@ -289,6 +288,8 @@ public class I18nInterceptor extends AbstractInterceptor { synchronized (sessionId.intern()) { invocation.getInvocationContext().getSession().put(attributeName, locale); } + } else { + LOG.debug("session creation avoided as it doesn't exist already"); } return locale; diff --git a/core/src/test/java/org/apache/struts2/interceptor/I18nInterceptorTest.java b/core/src/test/java/org/apache/struts2/interceptor/I18nInterceptorTest.java index 9159633fe..f31f96c3f 100644 --- a/core/src/test/java/org/apache/struts2/interceptor/I18nInterceptorTest.java +++ b/core/src/test/java/org/apache/struts2/interceptor/I18nInterceptorTest.java @@ -52,7 +52,7 @@ public class I18nInterceptorTest extends TestCase { interceptor.intercept(mai); } - public void testNoSession() throws Exception { + public void testNoSessionNoLocale() throws Exception { request.setSession(null); try { interceptor.intercept(mai); @@ -60,6 +60,25 @@ public class I18nInterceptorTest extends TestCase { } catch (Exception ignore) { fail("Shouldn't throw any exception!"); } + + assertFalse(mai.getInvocationContext().getParameters().get(I18nInterceptor.DEFAULT_PARAMETER).isDefined()); // should have been removed + + assertNull(session.get(I18nInterceptor.DEFAULT_SESSION_ATTRIBUTE)); // should not be stored here + } + + public void testNoSessionButLocale() throws Exception { + prepare(I18nInterceptor.DEFAULT_PARAMETER, "da_DK"); //prevents shouldStore to being false + request.setSession(null); + try { + interceptor.intercept(mai); + assertTrue(true); + } catch (Exception ignore) { + fail("Shouldn't throw any exception!"); + } + + assertFalse(mai.getInvocationContext().getParameters().get(I18nInterceptor.DEFAULT_PARAMETER).isDefined()); // should have been removed + + assertNull(session.get(I18nInterceptor.DEFAULT_SESSION_ATTRIBUTE)); // should not be stored here } public void testDefaultLocale() throws Exception {