Updates test
Project: http://git-wip-us.apache.org/repos/asf/struts/repo Commit: http://git-wip-us.apache.org/repos/asf/struts/commit/2733f158 Tree: http://git-wip-us.apache.org/repos/asf/struts/tree/2733f158 Diff: http://git-wip-us.apache.org/repos/asf/struts/diff/2733f158 Branch: refs/heads/master Commit: 2733f158d892efb2febdd99fc1772ed6564ea7be Parents: 4ef6418 Author: Lukasz Lenart <lukaszlen...@apache.org> Authored: Wed Mar 8 17:43:28 2017 +0100 Committer: Lukasz Lenart <lukaszlen...@apache.org> Committed: Wed Mar 8 17:43:28 2017 +0100 ---------------------------------------------------------------------- .../xwork2/util/XWorkTestCaseHelper.java | 2 +- .../xwork2/util/LocalizedTextUtilTest.java | 54 +++++++++++--------- .../DefaultBeanSelectionProviderTest.java | 10 ++-- .../org/apache/struts2/config/SettingsTest.java | 6 ++- .../struts2/dispatcher/DispatcherTest.java | 8 +-- 5 files changed, 45 insertions(+), 35 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/struts/blob/2733f158/core/src/main/java/com/opensymphony/xwork2/util/XWorkTestCaseHelper.java ---------------------------------------------------------------------- diff --git a/core/src/main/java/com/opensymphony/xwork2/util/XWorkTestCaseHelper.java b/core/src/main/java/com/opensymphony/xwork2/util/XWorkTestCaseHelper.java index d81b84e..717a3ef 100644 --- a/core/src/main/java/com/opensymphony/xwork2/util/XWorkTestCaseHelper.java +++ b/core/src/main/java/com/opensymphony/xwork2/util/XWorkTestCaseHelper.java @@ -40,7 +40,7 @@ public class XWorkTestCaseHelper { ActionContext.setContext(new ActionContext(stack.getContext())); // clear out localization - LocalizedTextUtil.reset(); + container.getInstance(LocalizedTextUtil.class).reset(); //ObjectFactory.setObjectFactory(container.getInstance(ObjectFactory.class)); http://git-wip-us.apache.org/repos/asf/struts/blob/2733f158/core/src/test/java/com/opensymphony/xwork2/util/LocalizedTextUtilTest.java ---------------------------------------------------------------------- diff --git a/core/src/test/java/com/opensymphony/xwork2/util/LocalizedTextUtilTest.java b/core/src/test/java/com/opensymphony/xwork2/util/LocalizedTextUtilTest.java index c14d529..e8dc982 100644 --- a/core/src/test/java/com/opensymphony/xwork2/util/LocalizedTextUtilTest.java +++ b/core/src/test/java/com/opensymphony/xwork2/util/LocalizedTextUtilTest.java @@ -39,10 +39,12 @@ import java.util.ResourceBundle; */ public class LocalizedTextUtilTest extends XWorkTestCase { + private LocalizedTextUtil localizedTextUtil; + public void testNpeWhenClassIsPrimitive() throws Exception { ValueStack stack = ActionContext.getContext().getValueStack(); stack.push(new MyObject()); - String result = LocalizedTextUtil.findText(MyObject.class, "someObj.someI18nKey", Locale.ENGLISH, "default message", null, stack); + String result = localizedTextUtil.findText(MyObject.class, "someObj.someI18nKey", Locale.ENGLISH, "default message", null, stack); System.out.println(result); } @@ -96,7 +98,7 @@ public class LocalizedTextUtilTest extends XWorkTestCase { } public void testNullKeys() { - LocalizedTextUtil.findText(this.getClass(), null, Locale.getDefault()); + localizedTextUtil.findText(this.getClass(), null, Locale.getDefault()); } public void testActionGetTextXXX() throws Exception { @@ -118,12 +120,12 @@ public class LocalizedTextUtilTest extends XWorkTestCase { } public void testAddDefaultResourceBundle() { - String text = LocalizedTextUtil.findDefaultText("foo.range", Locale.getDefault()); + String text = localizedTextUtil.findDefaultText("foo.range", Locale.getDefault()); assertNull("Found message when it should not be available.", text); LocalizedTextUtil.addDefaultResourceBundle("com/opensymphony/xwork2/SimpleAction"); - String message = LocalizedTextUtil.findDefaultText("foo.range", Locale.US); + String message = localizedTextUtil.findDefaultText("foo.range", Locale.US); assertEquals("Foo Range Message", message); } @@ -135,17 +137,17 @@ public class LocalizedTextUtilTest extends XWorkTestCase { } public void testDefaultMessage() throws Exception { - String message = LocalizedTextUtil.findDefaultText("xwork.error.action.execution", Locale.getDefault()); + String message = localizedTextUtil.findDefaultText("xwork.error.action.execution", Locale.getDefault()); assertEquals("Error during Action invocation", message); } public void testDefaultMessageOverride() throws Exception { - String message = LocalizedTextUtil.findDefaultText("xwork.error.action.execution", Locale.getDefault()); + String message = localizedTextUtil.findDefaultText("xwork.error.action.execution", Locale.getDefault()); assertEquals("Error during Action invocation", message); LocalizedTextUtil.addDefaultResourceBundle("com/opensymphony/xwork2/test"); - message = LocalizedTextUtil.findDefaultText("xwork.error.action.execution", Locale.getDefault()); + message = localizedTextUtil.findDefaultText("xwork.error.action.execution", Locale.getDefault()); assertEquals("Testing resource bundle override", message); } @@ -162,7 +164,7 @@ public class LocalizedTextUtilTest extends XWorkTestCase { ActionContext.getContext().getValueStack().push(action); ActionContext.getContext().getValueStack().push(action.getModel()); - String message = LocalizedTextUtil.findText(ModelDrivenAction2.class, "invalid.fieldvalue.barObj.title", Locale.getDefault()); + String message = localizedTextUtil.findText(ModelDrivenAction2.class, "invalid.fieldvalue.barObj.title", Locale.getDefault()); assertEquals("Title is invalid!", message); } @@ -172,7 +174,7 @@ public class LocalizedTextUtilTest extends XWorkTestCase { mockActionInvocation.expectAndReturn("getAction", action); ActionContext.getContext().setActionInvocation((ActionInvocation) mockActionInvocation.proxy()); - String message = LocalizedTextUtil.findText(ModelDrivenAction2.class, "test.foo", Locale.getDefault()); + String message = localizedTextUtil.findText(ModelDrivenAction2.class, "test.foo", Locale.getDefault()); assertEquals("Foo!", message); } @@ -183,46 +185,46 @@ public class LocalizedTextUtilTest extends XWorkTestCase { mockActionInvocation.expectAndReturn("getAction", action); ActionContext.getContext().setActionInvocation((ActionInvocation) mockActionInvocation.proxy()); - String message = LocalizedTextUtil.findText(ModelDrivenAction2.class, "package.properties", Locale.getDefault()); + String message = localizedTextUtil.findText(ModelDrivenAction2.class, "package.properties", Locale.getDefault()); assertEquals("It works!", message); } public void testParameterizedDefaultMessage() throws Exception { - String message = LocalizedTextUtil.findDefaultText("xwork.exception.missing-action", Locale.getDefault(), new String[]{"AddUser"}); + String message = localizedTextUtil.findDefaultText("xwork.exception.missing-action", Locale.getDefault(), new String[]{"AddUser"}); assertEquals("There is no Action mapped for action name AddUser.", message); } public void testParameterizedDefaultMessageWithPackage() throws Exception { - String message = LocalizedTextUtil.findDefaultText("xwork.exception.missing-package-action", Locale.getDefault(), new String[]{"blah", "AddUser"}); + String message = localizedTextUtil.findDefaultText("xwork.exception.missing-package-action", Locale.getDefault(), new String[]{"blah", "AddUser"}); assertEquals("There is no Action mapped for namespace blah and action name AddUser.", message); } public void testLocalizedDateFormatIsUsed() throws ParseException { - LocalizedTextUtil.addDefaultResourceBundle("com/opensymphony/xwork2/util/LocalizedTextUtilTest"); + localizedTextUtil.addDefaultResourceBundle("com/opensymphony/xwork2/util/localizedTextUtilTest"); Date date = DateFormat.getDateInstance(DateFormat.SHORT, Locale.US).parse("01/01/2015"); Object[] params = new Object[]{ date }; - String usDate = LocalizedTextUtil.findDefaultText("test.format.date", Locale.US, params); - String germanDate = LocalizedTextUtil.findDefaultText("test.format.date", Locale.GERMANY, params); + String usDate = localizedTextUtil.findDefaultText("test.format.date", Locale.US, params); + String germanDate = localizedTextUtil.findDefaultText("test.format.date", Locale.GERMANY, params); assertEquals(usDate, "1/1/15"); assertEquals(germanDate, "01.01.15"); } public void testXW377() { - LocalizedTextUtil.addDefaultResourceBundle("com/opensymphony/xwork2/util/LocalizedTextUtilTest"); + localizedTextUtil.addDefaultResourceBundle("com/opensymphony/xwork2/util/localizedTextUtilTest"); - String text = LocalizedTextUtil.findText(Bar.class, "xw377", ActionContext.getContext().getLocale(), "xw377", null, ActionContext.getContext().getValueStack()); + String text = localizedTextUtil.findText(Bar.class, "xw377", ActionContext.getContext().getLocale(), "xw377", null, ActionContext.getContext().getValueStack()); assertEquals("xw377", text); // should not log - String text2 = LocalizedTextUtil.findText(LocalizedTextUtilTest.class, "notinbundle", ActionContext.getContext().getLocale(), "hello", null, ActionContext.getContext().getValueStack()); + String text2 = localizedTextUtil.findText(LocalizedTextUtilTest.class, "notinbundle", ActionContext.getContext().getLocale(), "hello", null, ActionContext.getContext().getValueStack()); assertEquals("hello", text2); // should log WARN - String text3 = LocalizedTextUtil.findText(LocalizedTextUtilTest.class, "notinbundle.key", ActionContext.getContext().getLocale(), "notinbundle.key", null, ActionContext.getContext().getValueStack()); + String text3 = localizedTextUtil.findText(LocalizedTextUtilTest.class, "notinbundle.key", ActionContext.getContext().getLocale(), "notinbundle.key", null, ActionContext.getContext().getValueStack()); assertEquals("notinbundle.key", text3); // should log WARN - String text4 = LocalizedTextUtil.findText(LocalizedTextUtilTest.class, "xw377", ActionContext.getContext().getLocale(), "hello", null, ActionContext.getContext().getValueStack()); + String text4 = localizedTextUtil.findText(LocalizedTextUtilTest.class, "xw377", ActionContext.getContext().getLocale(), "hello", null, ActionContext.getContext().getValueStack()); assertEquals("xw377", text4); // should not log - String text5 = LocalizedTextUtil.findText(LocalizedTextUtilTest.class, "username", ActionContext.getContext().getLocale(), null, null, ActionContext.getContext().getValueStack()); + String text5 = localizedTextUtil.findText(LocalizedTextUtilTest.class, "username", ActionContext.getContext().getLocale(), null, null, ActionContext.getContext().getValueStack()); assertEquals("Santa", text5); // should not log } @@ -231,9 +233,9 @@ public class LocalizedTextUtilTest extends XWorkTestCase { // Before this fix loading the bundle for Germany failed since Italy have previously failed and thus the misses cache // contained a false entry - ResourceBundle rbFrance = LocalizedTextUtil.findResourceBundle("com/opensymphony/xwork2/util/XW404", Locale.FRANCE); - ResourceBundle rbItaly = LocalizedTextUtil.findResourceBundle("com/opensymphony/xwork2/util/XW404", Locale.ITALY); - ResourceBundle rbGermany = LocalizedTextUtil.findResourceBundle("com/opensymphony/xwork2/util/XW404", Locale.GERMANY); + ResourceBundle rbFrance = localizedTextUtil.findResourceBundle("com/opensymphony/xwork2/util/XW404", Locale.FRANCE); + ResourceBundle rbItaly = localizedTextUtil.findResourceBundle("com/opensymphony/xwork2/util/XW404", Locale.ITALY); + ResourceBundle rbGermany = localizedTextUtil.findResourceBundle("com/opensymphony/xwork2/util/XW404", Locale.GERMANY); assertNotNull(rbFrance); assertEquals("Bonjour", rbFrance.getString("hello")); @@ -251,13 +253,15 @@ public class LocalizedTextUtilTest extends XWorkTestCase { container.inject(provider); loadConfigurationProviders(provider); + this.localizedTextUtil = container.inject(LocalizedTextUtil.class); + ActionContext.getContext().setLocale(Locale.US); } @Override protected void tearDown() throws Exception { super.tearDown(); - LocalizedTextUtil.clearDefaultResourceBundles(); + localizedTextUtil.clearDefaultResourceBundles(); } } http://git-wip-us.apache.org/repos/asf/struts/blob/2733f158/core/src/test/java/org/apache/struts2/config/DefaultBeanSelectionProviderTest.java ---------------------------------------------------------------------- diff --git a/core/src/test/java/org/apache/struts2/config/DefaultBeanSelectionProviderTest.java b/core/src/test/java/org/apache/struts2/config/DefaultBeanSelectionProviderTest.java index f7c4066..6b4d29a 100644 --- a/core/src/test/java/org/apache/struts2/config/DefaultBeanSelectionProviderTest.java +++ b/core/src/test/java/org/apache/struts2/config/DefaultBeanSelectionProviderTest.java @@ -23,6 +23,7 @@ package org.apache.struts2.config; import java.util.Locale; +import com.opensymphony.xwork2.XWorkTestCase; import org.apache.struts2.StrutsConstants; import com.opensymphony.xwork2.inject.ContainerBuilder; @@ -31,12 +32,15 @@ import com.opensymphony.xwork2.util.location.LocatableProperties; import junit.framework.TestCase; -public class DefaultBeanSelectionProviderTest extends TestCase { +public class DefaultBeanSelectionProviderTest extends XWorkTestCase { public void testRegister() { LocalizedTextUtil.clearDefaultResourceBundles(); LocalizedTextUtil.addDefaultResourceBundle("org/apache/struts2/struts-messages"); - assertEquals("The form has already been processed or no token was supplied, please try again.", LocalizedTextUtil.findDefaultText("struts.messages.invalid.token", Locale.getDefault())); + + LocalizedTextUtil localizedTextUtil = container.inject(LocalizedTextUtil.class); + + assertEquals("The form has already been processed or no token was supplied, please try again.", localizedTextUtil.findDefaultText("struts.messages.invalid.token", Locale.getDefault())); LocatableProperties props = new LocatableProperties(); props.setProperty(StrutsConstants.STRUTS_CUSTOM_I18N_RESOURCES, "testmessages,testmessages2"); @@ -44,7 +48,7 @@ public class DefaultBeanSelectionProviderTest extends TestCase { new DefaultBeanSelectionProvider().register(new ContainerBuilder(), props); - assertEquals("Replaced message for token tag", LocalizedTextUtil.findDefaultText("struts.messages.invalid.token", Locale.getDefault())); + assertEquals("Replaced message for token tag", localizedTextUtil.findDefaultText("struts.messages.invalid.token", Locale.getDefault())); } } http://git-wip-us.apache.org/repos/asf/struts/blob/2733f158/core/src/test/java/org/apache/struts2/config/SettingsTest.java ---------------------------------------------------------------------- diff --git a/core/src/test/java/org/apache/struts2/config/SettingsTest.java b/core/src/test/java/org/apache/struts2/config/SettingsTest.java index 6c775f7..a5af5f7 100644 --- a/core/src/test/java/org/apache/struts2/config/SettingsTest.java +++ b/core/src/test/java/org/apache/struts2/config/SettingsTest.java @@ -52,9 +52,11 @@ public class SettingsTest extends StrutsInternalTestCase { public void testDefaultResourceBundlesLoaded() { Settings settings = new DefaultSettings(); + LocalizedTextUtil localizedTextUtil = container.inject(LocalizedTextUtil.class); + assertEquals("testmessages,testmessages2", settings.get(StrutsConstants.STRUTS_CUSTOM_I18N_RESOURCES)); - assertEquals("This is a test message", LocalizedTextUtil.findDefaultText("default.testmessage", Locale.getDefault())); - assertEquals("This is another test message", LocalizedTextUtil.findDefaultText("default.testmessage2", Locale.getDefault())); + assertEquals("This is a test message", localizedTextUtil.findDefaultText("default.testmessage", Locale.getDefault())); + assertEquals("This is another test message", localizedTextUtil.findDefaultText("default.testmessage2", Locale.getDefault())); } public void testSetSettings() { http://git-wip-us.apache.org/repos/asf/struts/blob/2733f158/core/src/test/java/org/apache/struts2/dispatcher/DispatcherTest.java ---------------------------------------------------------------------- diff --git a/core/src/test/java/org/apache/struts2/dispatcher/DispatcherTest.java b/core/src/test/java/org/apache/struts2/dispatcher/DispatcherTest.java index edcf987..c0994a5 100644 --- a/core/src/test/java/org/apache/struts2/dispatcher/DispatcherTest.java +++ b/core/src/test/java/org/apache/struts2/dispatcher/DispatcherTest.java @@ -55,14 +55,14 @@ import java.util.Map; public class DispatcherTest extends StrutsInternalTestCase { public void testDefaultResurceBundlePropertyLoaded() throws Exception { + LocalizedTextUtil localizedTextUtil = container.inject(LocalizedTextUtil.class); + // some i18n messages from xwork-messages.properties - assertEquals( - LocalizedTextUtil.findDefaultText("xwork.error.action.execution", Locale.US), + assertEquals(localizedTextUtil.findDefaultText("xwork.error.action.execution", Locale.US), "Error during Action invocation"); // some i18n messages from struts-messages.properties - assertEquals( - LocalizedTextUtil.findDefaultText("struts.messages.error.uploading", Locale.US, + assertEquals(localizedTextUtil.findDefaultText("struts.messages.error.uploading", Locale.US, new Object[] { "some error messages" }), "Error uploading: some error messages"); }