You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@struts.apache.org by lu...@apache.org on 2011/12/02 17:33:45 UTC
svn commit: r1209569 [42/50] - in /struts/struts2/branches/STRUTS_3_X:
apps/blank/src/main/java/example/ apps/blank/src/test/java/example/
apps/jboss-blank/src/main/java/example/
apps/jboss-blank/src/test/java/example/ apps/mailreader/src/main/java/mai...
Added: struts/struts2/branches/STRUTS_3_X/xwork-core/src/test/java/org/apache/struts2/xwork2/interceptor/ParametersInterceptorTest.java
URL: http://svn.apache.org/viewvc/struts/struts2/branches/STRUTS_3_X/xwork-core/src/test/java/org/apache/struts2/xwork2/interceptor/ParametersInterceptorTest.java?rev=1209569&view=auto
==============================================================================
--- struts/struts2/branches/STRUTS_3_X/xwork-core/src/test/java/org/apache/struts2/xwork2/interceptor/ParametersInterceptorTest.java (added)
+++ struts/struts2/branches/STRUTS_3_X/xwork-core/src/test/java/org/apache/struts2/xwork2/interceptor/ParametersInterceptorTest.java Fri Dec 2 16:33:03 2011
@@ -0,0 +1,552 @@
+/*
+ * Copyright 2002-2006,2009 The Apache Software Foundation.
+ *
+ * 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 org.apache.struts2.xwork2.interceptor;
+
+import junit.framework.Assert;
+import org.apache.struts2.xwork2.Action;
+import org.apache.struts2.xwork2.ActionContext;
+import org.apache.struts2.xwork2.ActionProxy;
+import org.apache.struts2.xwork2.ModelDrivenAction;
+import org.apache.struts2.xwork2.SimpleAction;
+import org.apache.struts2.xwork2.TestBean;
+import org.apache.struts2.xwork2.TextProvider;
+import org.apache.struts2.xwork2.XWorkTestCase;
+import org.apache.struts2.xwork2.config.entities.ActionConfig;
+import org.apache.struts2.xwork2.config.providers.MockConfigurationProvider;
+import org.apache.struts2.xwork2.config.providers.XmlConfigurationProvider;
+import org.apache.struts2.xwork2.conversion.impl.XWorkConverter;
+import org.apache.struts2.xwork2.mock.MockActionInvocation;
+import org.apache.struts2.xwork2.ognl.OgnlValueStack;
+import org.apache.struts2.xwork2.ognl.OgnlValueStackFactory;
+import org.apache.struts2.xwork2.ognl.accessor.CompoundRootAccessor;
+import org.apache.struts2.xwork2.util.CompoundRoot;
+import org.apache.struts2.xwork2.util.ValueStack;
+import org.apache.struts2.xwork2.util.ValueStackFactory;
+import ognl.PropertyAccessor;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.LinkedHashMap;
+import java.util.List;
+import java.util.Map;
+
+
+/**
+ * Unit test for {@link ParametersInterceptor}.
+ *
+ * @author Jason Carreira
+ */
+public class ParametersInterceptorTest extends XWorkTestCase {
+
+ public void testParameterNameAware() {
+ ParametersInterceptor pi = createParametersInterceptor();
+ final Map actual = injectValueStackFactory(pi);
+ ValueStack stack = createStubValueStack(actual);
+ final Map expected = new HashMap() {
+ {
+ put("fooKey", "fooValue");
+ put("barKey", "barValue");
+ }
+ };
+ Object a = new ParameterNameAware() {
+ public boolean acceptableParameterName(String parameterName) {
+ return expected.containsKey(parameterName);
+ }
+ };
+ Map parameters = new HashMap() {
+ {
+ put("fooKey", "fooValue");
+ put("barKey", "barValue");
+ put("error", "error");
+ }
+ };
+ pi.setParameters(a, stack, parameters);
+ assertEquals(expected, actual);
+ }
+
+ public void testDoesNotAllowMethodInvocations() throws Exception {
+ Map<String, Object> params = new HashMap<String, Object>();
+ params.put("@java.lang.System@exit(1).dummy", "dumb value");
+
+ HashMap<String, Object> extraContext = new HashMap<String, Object>();
+ extraContext.put(ActionContext.PARAMETERS, params);
+
+ ActionProxy proxy = actionProxyFactory.createActionProxy("", MockConfigurationProvider.MODEL_DRIVEN_PARAM_TEST, extraContext);
+ assertEquals(Action.SUCCESS, proxy.execute());
+
+ ModelDrivenAction action = (ModelDrivenAction) proxy.getAction();
+ TestBean model = (TestBean) action.getModel();
+
+ String property = System.getProperty("xwork.security.test");
+ assertNull(property);
+ }
+
+ public void testModelDrivenParameters() throws Exception {
+ Map<String, Object> params = new HashMap<String, Object>();
+ final String fooVal = "org.apache.struts2.xwork2.interceptor.ParametersInterceptorTest.foo";
+ params.put("foo", fooVal);
+
+ final String nameVal = "org.apache.struts2.xwork2.interceptor.ParametersInterceptorTest.name";
+ params.put("name", nameVal);
+ params.put("count", "15");
+
+ HashMap<String, Object> extraContext = new HashMap<String, Object>();
+ extraContext.put(ActionContext.PARAMETERS, params);
+
+ ActionProxy proxy = actionProxyFactory.createActionProxy("", MockConfigurationProvider.MODEL_DRIVEN_PARAM_TEST, extraContext);
+ assertEquals(Action.SUCCESS, proxy.execute());
+
+ ModelDrivenAction action = (ModelDrivenAction) proxy.getAction();
+ TestBean model = (TestBean) action.getModel();
+ assertEquals(nameVal, model.getName());
+ assertEquals(15, model.getCount());
+ assertEquals(fooVal, action.getFoo());
+ }
+
+ public void testParametersDoesNotAffectSession() throws Exception {
+ Map<String, Object> params = new HashMap<String, Object>();
+ params.put("blah", "This is blah");
+ params.put("#session.foo", "Foo");
+ params.put("\u0023session[\'user\']", "0wn3d");
+ params.put("\\u0023session[\'user\']", "0wn3d");
+ params.put("\u0023session.user2", "0wn3d");
+ params.put("\\u0023session.user2", "0wn3d");
+ params.put("('\u0023'%20%2b%20'session[\'user3\']')(unused)", "0wn3d");
+ params.put("('\\u0023' + 'session[\\'user4\\']')(unused)", "0wn3d");
+ params.put("('\u0023'%2b'session[\'user5\']')(unused)", "0wn3d");
+ params.put("('\\u0023'%2b'session[\'user5\']')(unused)", "0wn3d");
+
+ HashMap<String, Object> extraContext = new HashMap<String, Object>();
+ extraContext.put(ActionContext.PARAMETERS, params);
+
+ ActionProxy proxy = actionProxyFactory.createActionProxy("", MockConfigurationProvider.PARAM_INTERCEPTOR_ACTION_NAME, extraContext);
+ ValueStack stack = proxy.getInvocation().getStack();
+ HashMap<String, Object> session = new HashMap<String, Object>();
+ stack.getContext().put("session", session);
+ proxy.execute();
+ Assert.assertEquals("This is blah", ((SimpleAction) proxy.getAction()).getBlah());
+ assertNull(session.get("foo"));
+ assertNull(session.get("user"));
+ assertNull(session.get("user2"));
+ assertNull(session.get("user3"));
+ assertNull(session.get("user4"));
+ assertNull(session.get("user5"));
+ }
+
+ public void testAccessToOgnlInternals() throws Exception {
+ // given
+ Map<String, Object> params = new HashMap<String, Object>();
+ params.put("blah", "This is blah");
+ params.put("('\\u0023_memberAccess[\\'allowStaticMethodAccess\\']')(meh)", "true");
+ params.put("('(aaa)(('\\u0023context[\\'xwork.MethodAccessor.denyMethodExecution\\']\\u003d\\u0023foo')(\\u0023foo\\u003dnew java.lang.Boolean(\"false\")))", "");
+ params.put("(asdf)(('\\u0023rt.exit(1)')(\\u0023rt\\u003d@java.lang.Runtime@getRuntime()))", "1");
+
+ HashMap<String, Object> extraContext = new HashMap<String, Object>();
+ extraContext.put(ActionContext.PARAMETERS, params);
+
+ ActionProxy proxy = actionProxyFactory.createActionProxy("", MockConfigurationProvider.PARAM_INTERCEPTOR_ACTION_NAME, "", extraContext);
+ ValueStack stack = proxy.getInvocation().getStack();
+
+ // when
+ proxy.execute();
+ proxy.getAction();
+
+ //then
+ assertEquals("This is blah", ((SimpleAction) proxy.getAction()).getBlah());
+ Object allowMethodAccess = stack.findValue("\u0023_memberAccess['allowStaticMethodAccess']");
+ assertNotNull(allowMethodAccess);
+ assertEquals(Boolean.FALSE, allowMethodAccess);
+ }
+
+ public void testParameters() throws Exception {
+ Map<String, Object> params = new HashMap<String, Object>();
+ params.put("blah", "This is blah");
+
+ HashMap<String, Object> extraContext = new HashMap<String, Object>();
+ extraContext.put(ActionContext.PARAMETERS, params);
+
+ ActionProxy proxy = actionProxyFactory.createActionProxy("", MockConfigurationProvider.PARAM_INTERCEPTOR_ACTION_NAME, extraContext);
+ proxy.execute();
+ assertEquals("This is blah", ((SimpleAction) proxy.getAction()).getBlah());
+ }
+
+ public void testParametersWithSpacesInTheName() throws Exception {
+ Map<String, Object> params = new HashMap<String, Object>();
+ params.put("theProtectedMap['p0 p1']", "test1");
+ params.put("theProtectedMap['p0p1 ']", "test2");
+ params.put("theProtectedMap[' p0p1 ']", "test3");
+ params.put("theProtectedMap[' p0 p1 ']", "test4");
+
+ HashMap<String, Object> extraContext = new HashMap<String, Object>();
+ extraContext.put(ActionContext.PARAMETERS, params);
+
+ ActionProxy proxy = actionProxyFactory.createActionProxy("", MockConfigurationProvider.PARAM_INTERCEPTOR_ACTION_NAME, extraContext);
+ proxy.execute();
+ Map<String, String> existingMap = ((SimpleAction) proxy.getAction()).getTheProtectedMap();
+ assertEquals(4, existingMap.size());
+ assertEquals("test1", existingMap.get("p0 p1"));
+ assertEquals("test2", existingMap.get("p0p1 "));
+ assertEquals("test3", existingMap.get(" p0p1 "));
+ assertEquals("test4", existingMap.get(" p0 p1 "));
+ }
+
+ public void testExcludedTrickyParameters() throws Exception {
+ Map<String, Object> params = new HashMap<String, Object>() {
+ {
+ put("blah", "This is blah");
+ put("name", "try_1");
+ put("(name)", "try_2");
+ put("['name']", "try_3");
+ put("['na' + 'me']", "try_4");
+ put("{name}[0]", "try_5");
+ put("(new string{'name'})[0]", "try_6");
+ put("#{key: 'name'}.key", "try_7");
+
+ }
+ };
+
+ HashMap<String, Object> extraContext = new HashMap<String, Object>();
+ extraContext.put(ActionContext.PARAMETERS, params);
+
+ ActionProxy proxy = actionProxyFactory.createActionProxy("", MockConfigurationProvider.PARAM_INTERCEPTOR_ACTION_NAME, extraContext);
+
+ ActionConfig config = configuration.getRuntimeConfiguration().getActionConfig("", MockConfigurationProvider.PARAM_INTERCEPTOR_ACTION_NAME);
+ ParametersInterceptor pi =(ParametersInterceptor) config.getInterceptors().get(0).getInterceptor();
+ pi.setExcludeParams("name");
+
+ proxy.execute();
+
+ SimpleAction action = (SimpleAction) proxy.getAction();
+ assertNull(action.getName());
+ assertEquals("This is blah", (action).getBlah());
+ }
+
+ public void testAcceptedTrickyParameters() throws Exception {
+ Map<String, Object> params = new HashMap<String, Object>() {
+ {
+ put("blah", "This is blah");
+ put("baz", "123");
+ put("name", "try_1");
+ put("(name)", "try_2");
+ put("['name']", "try_3");
+ put("['na' + 'me']", "try_4");
+ put("{name}[0]", "try_5");
+ put("(new string{'name'})[0]", "try_6");
+ put("#{key: 'name'}.key", "try_7");
+ }
+ };
+
+ HashMap<String, Object> extraContext = new HashMap<String, Object>();
+ extraContext.put(ActionContext.PARAMETERS, params);
+
+ ActionProxy proxy = actionProxyFactory.createActionProxy("", MockConfigurationProvider.PARAM_INTERCEPTOR_ACTION_NAME, extraContext);
+
+ ActionConfig config = configuration.getRuntimeConfiguration().getActionConfig("", MockConfigurationProvider.PARAM_INTERCEPTOR_ACTION_NAME);
+ ParametersInterceptor pi =(ParametersInterceptor) config.getInterceptors().get(0).getInterceptor();
+ pi.setAcceptParamNames("blah, baz");
+
+ proxy.execute();
+
+ SimpleAction action = (SimpleAction) proxy.getAction();
+ assertNull(action.getName());
+ assertEquals("This is blah", (action).getBlah());
+ assertEquals(123, action.getBaz());
+ }
+
+
+ public void testParametersNotAccessPrivateVariables() throws Exception {
+ Map<String, Object> params = new HashMap<String, Object>();
+ params.put("protectedMap.foo", "This is blah");
+ params.put("theProtectedMap.boo", "This is blah");
+
+ HashMap<String, Object> extraContext = new HashMap<String, Object>();
+ extraContext.put(ActionContext.PARAMETERS, params);
+
+ ActionProxy proxy = actionProxyFactory.createActionProxy("", MockConfigurationProvider.PARAM_INTERCEPTOR_ACTION_NAME, extraContext);
+ proxy.execute();
+ SimpleAction action = (SimpleAction) proxy.getAction();
+ assertEquals(1, action.getTheProtectedMap().size());
+ assertNotNull(action.getTheProtectedMap().get("boo"));
+ assertNull(action.getTheProtectedMap().get("foo"));
+ }
+
+ public void testParametersNotAccessProtectedMethods() throws Exception {
+ Map<String, Object> params = new HashMap<String, Object>();
+ params.put("theSemiProtectedMap.foo", "This is blah");
+ params.put("theProtectedMap.boo", "This is blah");
+
+ HashMap<String, Object> extraContext = new HashMap<String, Object>();
+ extraContext.put(ActionContext.PARAMETERS, params);
+
+ ActionProxy proxy = actionProxyFactory.createActionProxy("", MockConfigurationProvider.PARAM_INTERCEPTOR_ACTION_NAME, extraContext);
+ proxy.execute();
+ SimpleAction action = (SimpleAction) proxy.getAction();
+ assertEquals(1, action.getTheProtectedMap().size());
+ assertNotNull(action.getTheProtectedMap().get("boo"));
+ assertNull(action.getTheProtectedMap().get("foo"));
+ }
+
+ public void testParametersOverwriteField() throws Exception {
+ Map<String, Object> params = new LinkedHashMap<String, Object>();
+ params.put("existingMap.boo", "This is blah");
+
+ HashMap<String, Object> extraContext = new HashMap<String, Object>();
+ extraContext.put(ActionContext.PARAMETERS, params);
+
+ ActionProxy proxy = actionProxyFactory.createActionProxy("", MockConfigurationProvider.PARAM_INTERCEPTOR_ACTION_NAME, extraContext);
+ proxy.execute();
+ SimpleAction action = (SimpleAction) proxy.getAction();
+ assertEquals(1, action.getTheExistingMap().size());
+ assertNotNull(action.getTheExistingMap().get("boo"));
+ assertNull(action.getTheExistingMap().get("existingKey"));
+ }
+
+ public void testNonexistentParametersGetLoggedInDevMode() throws Exception {
+ loadConfigurationProviders(new XmlConfigurationProvider("xwork-test-beans.xml"),
+ new MockConfigurationProvider(Collections.singletonMap("devMode", "true")));
+ Map<String, Object> params = new HashMap<String, Object>();
+ params.put("not_a_property", "There is no action property named like this");
+
+ HashMap<String, Object> extraContext = new HashMap<String, Object>();
+ extraContext.put(ActionContext.PARAMETERS, params);
+ ParametersInterceptor.setDevMode("true");
+
+ ActionConfig config = configuration.getRuntimeConfiguration().getActionConfig("", MockConfigurationProvider.PARAM_INTERCEPTOR_ACTION_NAME);
+ container.inject(config.getInterceptors().get(0).getInterceptor());
+ ActionProxy proxy = actionProxyFactory.createActionProxy("", MockConfigurationProvider.PARAM_INTERCEPTOR_ACTION_NAME, extraContext);
+ proxy.execute();
+ final String actionMessage = "" + ((SimpleAction) proxy.getAction()).getActionMessages().toArray()[0];
+ assertTrue(actionMessage.contains("Error setting expression 'not_a_property' with value 'There is no action property named like this'"));
+ }
+
+ public void testNonexistentParametersAreIgnoredInProductionMode() throws Exception {
+ loadConfigurationProviders(new XmlConfigurationProvider("xwork-test-beans.xml"),
+ new MockConfigurationProvider(Collections.singletonMap("devMode", "false")));
+ Map<String, Object> params = new HashMap<String, Object>();
+ params.put("not_a_property", "There is no action property named like this");
+
+ HashMap<String, Object> extraContext = new HashMap<String, Object>();
+ extraContext.put(ActionContext.PARAMETERS, params);
+
+ ActionConfig config = configuration.getRuntimeConfiguration().getActionConfig("", MockConfigurationProvider.PARAM_INTERCEPTOR_ACTION_NAME);
+ container.inject(config.getInterceptors().get(0).getInterceptor());
+ ActionProxy proxy = actionProxyFactory.createActionProxy("", MockConfigurationProvider.PARAM_INTERCEPTOR_ACTION_NAME, extraContext);
+ proxy.execute();
+ assertTrue(((SimpleAction) proxy.getAction()).getActionMessages().isEmpty());
+ }
+
+ public void testNoParametersAction() throws Exception {
+ ParametersInterceptor interceptor = new ParametersInterceptor();
+ interceptor.init();
+
+ MockActionInvocation mai = new MockActionInvocation();
+ Action action = new NoParametersAction();
+ mai.setAction(action);
+
+ interceptor.doIntercept(mai);
+ interceptor.destroy();
+ }
+
+ public void testNoOrdered() throws Exception {
+ ParametersInterceptor pi = createParametersInterceptor();
+ final Map<String, Object> actual = new LinkedHashMap<String, Object>();
+ pi.setValueStackFactory(createValueStackFactory(actual));
+ ValueStack stack = createStubValueStack(actual);
+
+ Map<String, Object> parameters = new HashMap<String, Object>();
+ parameters.put("user.address.city", "London");
+ parameters.put("user.name", "Superman");
+
+ Action action = new SimpleAction();
+ pi.setParameters(action, stack, parameters);
+
+ assertEquals("ordered should be false by default", false, pi.isOrdered());
+ assertEquals(2, actual.size());
+ assertEquals("London", actual.get("user.address.city"));
+ assertEquals("Superman", actual.get("user.name"));
+
+ // is not ordered
+ List<Object> values = new ArrayList<Object>(actual.values());
+ assertEquals("London", values.get(0));
+ assertEquals("Superman", values.get(1));
+ }
+
+ public void testOrdered() throws Exception {
+ ParametersInterceptor pi = new ParametersInterceptor();
+ pi.setOrdered(true);
+ container.inject(pi);
+ final Map<String, Object> actual = new LinkedHashMap<String, Object>();
+ pi.setValueStackFactory(createValueStackFactory(actual));
+ ValueStack stack = createStubValueStack(actual);
+
+ Map<String, Object> parameters = new HashMap<String, Object>();
+ parameters.put("user.address.city", "London");
+ parameters.put("user.address['postal']", "QJR387");
+ parameters.put("user.name", "Superman");
+
+ Action action = new SimpleAction();
+ pi.setParameters(action, stack, parameters);
+
+ assertEquals(true, pi.isOrdered());
+ assertEquals(3, actual.size());
+ assertEquals("London", actual.get("user.address.city"));
+ assertEquals("QJR387", actual.get("user.address['postal']"));
+ assertEquals("Superman", actual.get("user.name"));
+
+ // should be ordered so user.name should be first
+ List<Object> values = new ArrayList<Object>(actual.values());
+ assertEquals("Superman", values.get(0));
+ assertEquals("London", values.get(1));
+ assertEquals("QJR387", values.get(2));
+ }
+
+ public void testSetOrdered() throws Exception {
+ ParametersInterceptor pi = createParametersInterceptor();
+ assertEquals("ordered should be false by default", false, pi.isOrdered());
+ pi.setOrdered(true);
+ assertEquals(true, pi.isOrdered());
+ }
+
+ public void testExcludedParametersAreIgnored() throws Exception {
+ ParametersInterceptor pi = createParametersInterceptor();
+ pi.setExcludeParams("dojo\\..*");
+ final Map actual = injectValueStackFactory(pi);
+ ValueStack stack = injectValueStack(actual);
+
+ final Map<String, Object> expected = new HashMap<String, Object>() {
+ {
+ put("fooKey", "fooValue");
+ }
+ };
+
+ Map<String, Object> parameters = new HashMap<String, Object>() {
+ {
+ put("dojo.test", "dojoValue");
+ put("fooKey", "fooValue");
+ }
+ };
+ pi.setParameters(new NoParametersAction(), stack, parameters);
+ assertEquals(expected, actual);
+ }
+
+ public void testInternalParametersAreIgnored() throws Exception {
+ // given
+ ParametersInterceptor interceptor = createParametersInterceptor();
+ final Map actual = injectValueStackFactory(interceptor);
+ ValueStack stack = injectValueStack(actual);
+
+
+ final Map<String, Object> expected = new HashMap<String, Object>() {
+ {
+ put("ordinary.bean", "value");
+ }
+ };
+
+ Map<String, Object> parameters = new HashMap<String, Object>() {
+ {
+ put("ordinary.bean", "value");
+ put("#some.internal.object", "true");
+ put("(bla)#some.internal.object", "true");
+ put("#some.internal.object(bla)#some.internal.object", "true");
+ put("#_some.internal.object", "true");
+ put("\u0023_some.internal.object", "true");
+ put("\u0023_some.internal.object,[dfd],bla(\u0023_some.internal.object)", "true");
+ put("\\u0023_some.internal.object", "true");
+ }
+ };
+
+ // when
+ interceptor.setParameters(new NoParametersAction(), stack, parameters);
+
+ // then
+ assertEquals(expected, actual);
+ }
+
+ private ValueStack injectValueStack(Map actual) {
+ ValueStack stack = createStubValueStack(actual);
+ container.inject(stack);
+ return stack;
+ }
+
+ private Map injectValueStackFactory(ParametersInterceptor interceptor) {
+ final Map actual = new HashMap();
+ interceptor.setValueStackFactory(createValueStackFactory(actual));
+ return actual;
+ }
+
+ private ParametersInterceptor createParametersInterceptor() {
+ ParametersInterceptor pi = new ParametersInterceptor();
+ container.inject(pi);
+ return pi;
+ }
+
+ private ValueStackFactory createValueStackFactory(final Map<String, Object> context) {
+ OgnlValueStackFactory factory = new OgnlValueStackFactory() {
+ @Override
+ public ValueStack createValueStack(ValueStack stack) {
+ return createStubValueStack(context);
+ }
+ };
+ container.inject(factory);
+ return factory;
+ }
+
+ private ValueStack createStubValueStack(final Map<String, Object> actual) {
+ ValueStack stack = new OgnlValueStack(
+ container.getInstance(XWorkConverter.class),
+ (CompoundRootAccessor)container.getInstance(PropertyAccessor.class, CompoundRoot.class.getName()),
+ container.getInstance(TextProvider.class, "system"), true) {
+ @Override
+ public void setValue(String expr, Object value) {
+ actual.put(expr, value);
+ }
+ };
+ container.inject(stack);
+ return stack;
+ }
+
+ /*
+ public void testIndexedParameters() throws Exception {
+ Map params = new HashMap();
+ params.put("indexedProp[33]", "This is blah");
+
+ HashMap extraContext = new HashMap();
+ extraContext.put(ActionContext.PARAMETERS, params);
+
+ ActionProxy proxy = actionProxyFactory.createActionProxy("", MockConfigurationProvider.PARAM_INTERCEPTOR_ACTION_NAME, extraContext);
+ proxy.execute();
+ assertEquals("This is blah", ((SimpleAction) proxy.getAction()).getIndexedProp(33));
+ }
+ */
+
+
+ private class NoParametersAction implements Action, NoParameters {
+
+ public String execute() throws Exception {
+ return SUCCESS;
+ }
+ }
+
+ @Override
+ protected void setUp() throws Exception {
+ super.setUp();
+ loadConfigurationProviders(new XmlConfigurationProvider("xwork-test-beans.xml"), new MockConfigurationProvider());
+
+ ActionConfig config = configuration.getRuntimeConfiguration().getActionConfig("", MockConfigurationProvider.PARAM_INTERCEPTOR_ACTION_NAME);
+ container.inject(config.getInterceptors().get(0).getInterceptor());
+ }
+}
Added: struts/struts2/branches/STRUTS_3_X/xwork-core/src/test/java/org/apache/struts2/xwork2/interceptor/PreResultListenerTest.java
URL: http://svn.apache.org/viewvc/struts/struts2/branches/STRUTS_3_X/xwork-core/src/test/java/org/apache/struts2/xwork2/interceptor/PreResultListenerTest.java?rev=1209569&view=auto
==============================================================================
--- struts/struts2/branches/STRUTS_3_X/xwork-core/src/test/java/org/apache/struts2/xwork2/interceptor/PreResultListenerTest.java (added)
+++ struts/struts2/branches/STRUTS_3_X/xwork-core/src/test/java/org/apache/struts2/xwork2/interceptor/PreResultListenerTest.java Fri Dec 2 16:33:03 2011
@@ -0,0 +1,126 @@
+/*
+ * Copyright 2002-2003,2009 The Apache Software Foundation.
+ *
+ * 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 org.apache.struts2.xwork2.interceptor;
+
+import com.mockobjects.dynamic.C;
+import com.mockobjects.dynamic.Mock;
+import org.apache.struts2.xwork2.config.Configuration;
+import org.apache.struts2.xwork2.config.ConfigurationException;
+import org.apache.struts2.xwork2.Action;
+import org.apache.struts2.xwork2.ActionInvocation;
+import org.apache.struts2.xwork2.ActionProxy;
+import org.apache.struts2.xwork2.DefaultActionProxyFactory;
+import org.apache.struts2.xwork2.ObjectFactory;
+import org.apache.struts2.xwork2.config.ConfigurationProvider;
+import org.apache.struts2.xwork2.config.entities.ActionConfig;
+import org.apache.struts2.xwork2.config.entities.PackageConfig;
+import org.apache.struts2.xwork2.XWorkTestCase;
+import org.apache.struts2.xwork2.inject.ContainerBuilder;
+import org.apache.struts2.xwork2.util.location.LocatableProperties;
+import org.apache.struts2.xwork2.ActionProxyFactory;
+import org.apache.struts2.xwork2.SimpleFooAction;
+
+import java.util.HashMap;
+
+
+/**
+ * PreResultListenerTest
+ *
+ * @author Jason Carreira
+ * Date: Nov 13, 2003 11:16:43 PM
+ */
+public class PreResultListenerTest extends XWorkTestCase {
+
+ private int count = 1;
+
+
+ public void testPreResultListenersAreCalled() throws Exception {
+ ActionProxy proxy = actionProxyFactory.createActionProxy("package", "action", new HashMap(), false, true);
+ ActionInvocation invocation = proxy.getInvocation();
+ Mock preResultListenerMock1 = new Mock(PreResultListener.class);
+ preResultListenerMock1.expect("beforeResult", C.args(C.eq(invocation), C.eq(Action.SUCCESS)));
+ invocation.addPreResultListener((PreResultListener) preResultListenerMock1.proxy());
+ proxy.execute();
+ preResultListenerMock1.verify();
+ }
+
+ public void testPreResultListenersAreCalledInOrder() throws Exception {
+ ActionProxy proxy = actionProxyFactory.createActionProxy("package", "action", new HashMap(), false, true);
+ ActionInvocation invocation = proxy.getInvocation();
+ CountPreResultListener listener1 = new CountPreResultListener();
+ CountPreResultListener listener2 = new CountPreResultListener();
+ invocation.addPreResultListener(listener1);
+ invocation.addPreResultListener(listener2);
+ proxy.execute();
+ assertNotNull(listener1.getMyOrder());
+ assertNotNull(listener2.getMyOrder());
+ assertEquals(listener1.getMyOrder().intValue() + 1, listener2.getMyOrder().intValue());
+ }
+
+ @Override
+ protected void setUp() throws Exception {
+ super.setUp();
+ loadConfigurationProviders(new ConfigurationProvider() {
+ Configuration configuration;
+ public void destroy() {
+ }
+
+ public void init(Configuration config) {
+ this.configuration = config;
+ }
+
+ public void loadPackages() {
+ PackageConfig packageConfig = new PackageConfig.Builder("package")
+ .addActionConfig("action", new ActionConfig.Builder("package", "action", SimpleFooAction.class.getName()).build())
+ .build();
+ configuration.addPackageConfig("package", packageConfig);
+ }
+
+ /**
+ * Tells whether the ConfigurationProvider should reload its configuration
+ *
+ * @return
+ */
+ public boolean needsReload() {
+ return false;
+ }
+
+ public void register(ContainerBuilder builder, LocatableProperties props) throws ConfigurationException {
+ builder.factory(ActionProxyFactory.class, DefaultActionProxyFactory.class);
+ builder.factory(ObjectFactory.class);
+
+ }
+ });
+ }
+
+ @Override
+ protected void tearDown() throws Exception {
+ super.tearDown();
+ }
+
+
+ private class CountPreResultListener implements PreResultListener {
+ private Integer myOrder = null;
+
+ public Integer getMyOrder() {
+ return myOrder;
+ }
+
+ public void beforeResult(ActionInvocation invocation, String resultCode) {
+ myOrder = new Integer(count++);
+ }
+ }
+}
Added: struts/struts2/branches/STRUTS_3_X/xwork-core/src/test/java/org/apache/struts2/xwork2/interceptor/PrefixMethodInvocationUtilTest.java
URL: http://svn.apache.org/viewvc/struts/struts2/branches/STRUTS_3_X/xwork-core/src/test/java/org/apache/struts2/xwork2/interceptor/PrefixMethodInvocationUtilTest.java?rev=1209569&view=auto
==============================================================================
--- struts/struts2/branches/STRUTS_3_X/xwork-core/src/test/java/org/apache/struts2/xwork2/interceptor/PrefixMethodInvocationUtilTest.java (added)
+++ struts/struts2/branches/STRUTS_3_X/xwork-core/src/test/java/org/apache/struts2/xwork2/interceptor/PrefixMethodInvocationUtilTest.java Fri Dec 2 16:33:03 2011
@@ -0,0 +1,292 @@
+/*
+ * Copyright 2002-2006,2009 The Apache Software Foundation.
+ *
+ * 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 org.apache.struts2.xwork2.interceptor;
+
+import org.apache.struts2.xwork2.ActionInvocation;
+import org.apache.struts2.xwork2.ActionProxy;
+import junit.framework.TestCase;
+import org.easymock.MockControl;
+
+import java.lang.reflect.Method;
+
+/**
+ * Test case for PrefixMethodInovcationUtil.
+ *
+ * @author tm_jee
+ * @version $Date: 2011-12-02 12:24:48 +0100 (Fri, 02 Dec 2011) $ $Id: PrefixMethodInvocationUtilTest.java 1209415 2011-12-02 11:24:48Z lukaszlenart $
+ */
+public class PrefixMethodInvocationUtilTest extends TestCase {
+
+ // === capitalizeMethodName ===
+ public void testCapitalizeMethodName() throws Exception {
+ assertEquals("SomeMethod",
+ PrefixMethodInvocationUtil.capitalizeMethodName("someMethod"));
+ assertEquals("AnotherMethod",
+ PrefixMethodInvocationUtil.capitalizeMethodName("anotherMethod"));
+ }
+
+ // === getPrefixMethod ===
+ public void testGetPrefixMethod1() throws Exception {
+ Object action = new PrefixMethodInvocationUtilTest.Action1();
+ Method m = PrefixMethodInvocationUtil.getPrefixedMethod(
+ new String[] { "prepare", "prepareDo" }, "save", action);
+ assertNotNull(m);
+ assertEquals(m.getName(), "prepareSave");
+ }
+
+ public void testGetPrefixMethod2() throws Exception {
+ Object action = new PrefixMethodInvocationUtilTest.Action1();
+ Method m = PrefixMethodInvocationUtil.getPrefixedMethod(
+ new String[] { "prepare", "prepareDo" }, "submit", action);
+ assertNotNull(m);
+ assertEquals(m.getName(), "prepareSubmit");
+ }
+
+ public void testGetPrefixMethod3() throws Exception {
+ Object action = new PrefixMethodInvocationUtilTest.Action1();
+ Method m = PrefixMethodInvocationUtil.getPrefixedMethod(
+ new String[] { "prepare", "prepareDo" }, "cancel", action);
+ assertNotNull(m);
+ assertEquals(m.getName(), "prepareDoCancel");
+ }
+
+ public void testGetPrefixMethod4() throws Exception {
+ Object action = new PrefixMethodInvocationUtilTest.Action1();
+ Method m = PrefixMethodInvocationUtil.getPrefixedMethod(
+ new String[] { "prepare", "prepareDo" }, "noSuchMethod", action);
+ assertNull(m);
+ }
+
+ public void testGetPrefixMethod5() throws Exception {
+ Object action = new PrefixMethodInvocationUtilTest.Action1();
+ Method m = PrefixMethodInvocationUtil.getPrefixedMethod(
+ new String[] { "noSuchPrefix", "noSuchPrefixDo" }, "save", action);
+ assertNull(m);
+ }
+
+
+ // === invokePrefixMethod ===
+ public void testInvokePrefixMethod1() throws Exception {
+ PrefixMethodInvocationUtilTest.Action1 action = new PrefixMethodInvocationUtilTest.Action1();
+
+ // ActionProxy
+ MockControl controlActionProxy = MockControl.createControl(ActionProxy.class);
+ ActionProxy mockActionProxy = (ActionProxy) controlActionProxy.getMock();
+ mockActionProxy.getMethod();
+ controlActionProxy.setReturnValue("save");
+
+
+ // ActionInvocation
+ MockControl controlActionInvocation = MockControl.createControl(ActionInvocation.class);
+ ActionInvocation mockActionInvocation = (ActionInvocation) controlActionInvocation.getMock();
+ mockActionInvocation.getAction();
+ controlActionInvocation.setReturnValue(action);
+ mockActionInvocation.getProxy();
+ controlActionInvocation.setReturnValue(mockActionProxy);
+
+ controlActionProxy.replay();
+ controlActionInvocation.replay();
+
+
+ PrefixMethodInvocationUtil.invokePrefixMethod(
+ mockActionInvocation,
+ new String[] { "prepare", "prepareDo" });
+
+ controlActionProxy.verify();
+ controlActionInvocation.verify();
+
+ assertTrue(action.prepareSaveInvoked);
+ assertFalse(action.prepareDoSaveInvoked);
+ assertFalse(action.prepareSubmitInvoked);
+ assertFalse(action.prepareDoCancelInvoked);
+ }
+
+ public void testInvokePrefixMethod2() throws Exception {
+ PrefixMethodInvocationUtilTest.Action1 action = new PrefixMethodInvocationUtilTest.Action1();
+
+ // ActionProxy
+ MockControl controlActionProxy = MockControl.createControl(ActionProxy.class);
+ ActionProxy mockActionProxy = (ActionProxy) controlActionProxy.getMock();
+ mockActionProxy.getMethod();
+ controlActionProxy.setReturnValue("submit");
+
+
+ // ActionInvocation
+ MockControl controlActionInvocation = MockControl.createControl(ActionInvocation.class);
+ ActionInvocation mockActionInvocation = (ActionInvocation) controlActionInvocation.getMock();
+ mockActionInvocation.getAction();
+ controlActionInvocation.setReturnValue(action);
+ mockActionInvocation.getProxy();
+ controlActionInvocation.setReturnValue(mockActionProxy);
+
+ controlActionProxy.replay();
+ controlActionInvocation.replay();
+
+
+ PrefixMethodInvocationUtil.invokePrefixMethod(
+ mockActionInvocation,
+ new String[] { "prepare", "prepareDo" });
+
+ controlActionProxy.verify();
+ controlActionInvocation.verify();
+
+ assertFalse(action.prepareSaveInvoked);
+ assertFalse(action.prepareDoSaveInvoked);
+ assertTrue(action.prepareSubmitInvoked);
+ assertFalse(action.prepareDoCancelInvoked);
+ }
+
+ public void testInvokePrefixMethod3() throws Exception {
+ PrefixMethodInvocationUtilTest.Action1 action = new PrefixMethodInvocationUtilTest.Action1();
+
+ // ActionProxy
+ MockControl controlActionProxy = MockControl.createControl(ActionProxy.class);
+ ActionProxy mockActionProxy = (ActionProxy) controlActionProxy.getMock();
+ mockActionProxy.getMethod();
+ controlActionProxy.setReturnValue("cancel");
+
+
+ // ActionInvocation
+ MockControl controlActionInvocation = MockControl.createControl(ActionInvocation.class);
+ ActionInvocation mockActionInvocation = (ActionInvocation) controlActionInvocation.getMock();
+ mockActionInvocation.getAction();
+ controlActionInvocation.setReturnValue(action);
+ mockActionInvocation.getProxy();
+ controlActionInvocation.setReturnValue(mockActionProxy);
+
+ controlActionProxy.replay();
+ controlActionInvocation.replay();
+
+
+ PrefixMethodInvocationUtil.invokePrefixMethod(
+ mockActionInvocation,
+ new String[] { "prepare", "prepareDo" });
+
+ controlActionProxy.verify();
+ controlActionInvocation.verify();
+
+ assertFalse(action.prepareSaveInvoked);
+ assertFalse(action.prepareDoSaveInvoked);
+ assertFalse(action.prepareSubmitInvoked);
+ assertTrue(action.prepareDoCancelInvoked);
+ }
+
+ public void testInvokePrefixMethod4() throws Exception {
+ PrefixMethodInvocationUtilTest.Action1 action = new PrefixMethodInvocationUtilTest.Action1();
+
+ // ActionProxy
+ MockControl controlActionProxy = MockControl.createControl(ActionProxy.class);
+ ActionProxy mockActionProxy = (ActionProxy) controlActionProxy.getMock();
+ mockActionProxy.getMethod();
+ controlActionProxy.setReturnValue("noSuchMethod");
+
+
+ // ActionInvocation
+ MockControl controlActionInvocation = MockControl.createControl(ActionInvocation.class);
+ ActionInvocation mockActionInvocation = (ActionInvocation) controlActionInvocation.getMock();
+ mockActionInvocation.getAction();
+ controlActionInvocation.setReturnValue(action);
+ mockActionInvocation.getProxy();
+ controlActionInvocation.setReturnValue(mockActionProxy);
+
+ controlActionProxy.replay();
+ controlActionInvocation.replay();
+
+
+ PrefixMethodInvocationUtil.invokePrefixMethod(
+ mockActionInvocation,
+ new String[] { "prepare", "prepareDo" });
+
+ controlActionProxy.verify();
+ controlActionInvocation.verify();
+
+ assertFalse(action.prepareSaveInvoked);
+ assertFalse(action.prepareDoSaveInvoked);
+ assertFalse(action.prepareSubmitInvoked);
+ assertFalse(action.prepareDoCancelInvoked);
+ }
+
+ public void testInvokePrefixMethod5() throws Exception {
+ PrefixMethodInvocationUtilTest.Action1 action = new PrefixMethodInvocationUtilTest.Action1();
+
+ // ActionProxy
+ MockControl controlActionProxy = MockControl.createControl(ActionProxy.class);
+ ActionProxy mockActionProxy = (ActionProxy) controlActionProxy.getMock();
+ mockActionProxy.getMethod();
+ controlActionProxy.setReturnValue("save");
+
+
+ // ActionInvocation
+ MockControl controlActionInvocation = MockControl.createControl(ActionInvocation.class);
+ ActionInvocation mockActionInvocation = (ActionInvocation) controlActionInvocation.getMock();
+ mockActionInvocation.getAction();
+ controlActionInvocation.setReturnValue(action);
+ mockActionInvocation.getProxy();
+ controlActionInvocation.setReturnValue(mockActionProxy);
+
+ controlActionProxy.replay();
+ controlActionInvocation.replay();
+
+
+ PrefixMethodInvocationUtil.invokePrefixMethod(
+ mockActionInvocation,
+ new String[] { "noSuchPrefix", "noSuchPrefixDo" });
+
+ controlActionProxy.verify();
+ controlActionInvocation.verify();
+
+ assertFalse(action.prepareSaveInvoked);
+ assertFalse(action.prepareDoSaveInvoked);
+ assertFalse(action.prepareSubmitInvoked);
+ assertFalse(action.prepareDoCancelInvoked);
+ }
+
+
+
+
+ /**
+ * Just a simple object for testing method invocation on its methods.
+ *
+ * @author tm_jee
+ * @version $Date: 2011-12-02 12:24:48 +0100 (Fri, 02 Dec 2011) $ $Id: PrefixMethodInvocationUtilTest.java 1209415 2011-12-02 11:24:48Z lukaszlenart $
+ */
+ public static class Action1 {
+
+ boolean prepareSaveInvoked = false;
+ boolean prepareDoSaveInvoked = false;
+ boolean prepareSubmitInvoked = false;
+ boolean prepareDoCancelInvoked = false;
+
+
+ // save
+ public void prepareSave() {
+ prepareSaveInvoked = true;
+ }
+ public void prepareDoSave() {
+ prepareDoSaveInvoked = true;
+ }
+
+ // submit
+ public void prepareSubmit() {
+ prepareSubmitInvoked = true;
+ }
+
+ // cancel
+ public void prepareDoCancel() {
+ prepareDoCancelInvoked = true;
+ }
+ }
+}
Added: struts/struts2/branches/STRUTS_3_X/xwork-core/src/test/java/org/apache/struts2/xwork2/interceptor/PrepareInterceptorTest.java
URL: http://svn.apache.org/viewvc/struts/struts2/branches/STRUTS_3_X/xwork-core/src/test/java/org/apache/struts2/xwork2/interceptor/PrepareInterceptorTest.java?rev=1209569&view=auto
==============================================================================
--- struts/struts2/branches/STRUTS_3_X/xwork-core/src/test/java/org/apache/struts2/xwork2/interceptor/PrepareInterceptorTest.java (added)
+++ struts/struts2/branches/STRUTS_3_X/xwork-core/src/test/java/org/apache/struts2/xwork2/interceptor/PrepareInterceptorTest.java Fri Dec 2 16:33:03 2011
@@ -0,0 +1,225 @@
+/*
+ * $Id: PrepareInterceptorTest.java 1209415 2011-12-02 11:24:48Z lukaszlenart $
+ * Copyright 2002-2007,2009 The Apache Software Foundation.
+ *
+ * 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 org.apache.struts2.xwork2.interceptor;
+
+import com.mockobjects.dynamic.Mock;
+import org.apache.struts2.xwork2.mock.MockActionInvocation;
+import org.apache.struts2.xwork2.Action;
+import org.apache.struts2.xwork2.ActionInvocation;
+import org.apache.struts2.xwork2.ActionProxy;
+import org.apache.struts2.xwork2.Preparable;
+import org.apache.struts2.xwork2.mock.MockActionProxy;
+import junit.framework.TestCase;
+import org.apache.struts2.xwork2.SimpleFooAction;
+import org.easymock.MockControl;
+
+/**
+ * Unit test for PrepareInterceptor.
+ *
+ * @author Claus Ibsen
+ * @author tm_jee
+ */
+public class PrepareInterceptorTest extends TestCase {
+
+ private Mock mock;
+ private PrepareInterceptor interceptor;
+
+ public void testPrepareCalledDefault() throws Exception {
+ MockActionInvocation mai = new MockActionInvocation();
+ MockActionProxy mockActionProxy = new MockActionProxy();
+ mockActionProxy.setMethod("execute");
+ mai.setProxy(mockActionProxy);
+ mai.setAction(mock.proxy());
+ mock.expect("prepare");
+
+ interceptor.intercept(mai);
+ }
+
+ public void testPrepareCalledFalse() throws Exception {
+ MockActionInvocation mai = new MockActionInvocation();
+ MockActionProxy mockActionProxy = new MockActionProxy();
+ mockActionProxy.setMethod("execute");
+ mai.setProxy(mockActionProxy);
+ mai.setAction(mock.proxy());
+
+ interceptor.setAlwaysInvokePrepare("false");
+ interceptor.intercept(mai);
+ }
+
+ public void testPrepareCalledTrue() throws Exception {
+ MockActionInvocation mai = new MockActionInvocation();
+ MockActionProxy mockActionProxy = new MockActionProxy();
+ mockActionProxy.setMethod("execute");
+ mai.setProxy(mockActionProxy);
+ mai.setAction(mock.proxy());
+ mock.expect("prepare");
+
+ interceptor.setAlwaysInvokePrepare("true");
+ interceptor.intercept(mai);
+ }
+
+ public void testFirstCallPrepareDoIsTrue() throws Exception {
+ MockActionInvocation mai = new MockActionInvocation();
+ MockActionProxy mockActionProxy = new MockActionProxy();
+ mockActionProxy.setMethod("submit");
+ mai.setProxy(mockActionProxy);
+ mai.setAction(mock.proxy());
+ mock.expect("prepareSubmit");
+ mock.expect("prepare");
+
+ interceptor.setFirstCallPrepareDo("true");
+ interceptor.intercept(mai);
+ }
+
+ public void testFirstCallPrepareDoIsFalse() throws Exception {
+ MockActionInvocation mai = new MockActionInvocation();
+ MockActionProxy mockActionProxy = new MockActionProxy();
+ mockActionProxy.setMethod("submit");
+ mai.setProxy(mockActionProxy);
+ mai.setAction(mock.proxy());
+ mock.expect("prepare");
+ mock.expect("prepareSubmit");
+
+ interceptor.setFirstCallPrepareDo("false");
+ interceptor.intercept(mai);
+ }
+
+ public void testNoPrepareCalled() throws Exception {
+ MockActionInvocation mai = new MockActionInvocation();
+ mai.setAction(new SimpleFooAction());
+
+ interceptor.doIntercept(mai);
+ }
+
+ public void testPrefixInvocation1() throws Exception {
+
+ MockControl controlAction = MockControl.createControl(ActionInterface.class);
+ ActionInterface mockAction = (ActionInterface) controlAction.getMock();
+ mockAction.prepareSubmit();
+ controlAction.setVoidCallable(1);
+ mockAction.prepare();
+ controlAction.setVoidCallable(1);
+
+ MockControl controlActionProxy = MockControl.createControl(ActionProxy.class);
+ ActionProxy mockActionProxy = (ActionProxy) controlActionProxy.getMock();
+ mockActionProxy.getMethod();
+ controlActionProxy.setDefaultReturnValue("submit");
+
+
+ MockControl controlActionInvocation = MockControl.createControl(ActionInvocation.class);
+ ActionInvocation mockActionInvocation = (ActionInvocation) controlActionInvocation.getMock();
+ mockActionInvocation.getAction();
+ controlActionInvocation.setDefaultReturnValue(mockAction);
+ mockActionInvocation.invoke();
+ controlActionInvocation.setDefaultReturnValue("okok");
+ mockActionInvocation.getProxy();
+ controlActionInvocation.setDefaultReturnValue(mockActionProxy);
+
+
+ controlAction.replay();
+ controlActionProxy.replay();
+ controlActionInvocation.replay();
+
+ PrepareInterceptor interceptor = new PrepareInterceptor();
+ String result = interceptor.intercept(mockActionInvocation);
+
+ assertEquals("okok", result);
+
+ controlAction.verify();
+ controlActionProxy.verify();
+ controlActionInvocation.verify();
+ }
+
+ public void testPrefixInvocation2() throws Exception {
+
+ MockControl controlAction = MockControl.createControl(ActionInterface.class);
+ ActionInterface mockAction = (ActionInterface) controlAction.getMock();
+ mockAction.prepare();
+ controlAction.setVoidCallable(1);
+
+ MockControl controlActionProxy = MockControl.createControl(ActionProxy.class);
+ ActionProxy mockActionProxy = (ActionProxy) controlActionProxy.getMock();
+ mockActionProxy.getMethod();
+ controlActionProxy.setDefaultReturnValue("save");
+
+
+ MockControl controlActionInvocation = MockControl.createControl(ActionInvocation.class);
+ ActionInvocation mockActionInvocation = (ActionInvocation) controlActionInvocation.getMock();
+ mockActionInvocation.getAction();
+ controlActionInvocation.setDefaultReturnValue(mockAction);
+ mockActionInvocation.invoke();
+ controlActionInvocation.setDefaultReturnValue("okok");
+ mockActionInvocation.getProxy();
+ controlActionInvocation.setDefaultReturnValue(mockActionProxy);
+
+
+ controlAction.replay();
+ controlActionProxy.replay();
+ controlActionInvocation.replay();
+
+ PrepareInterceptor interceptor = new PrepareInterceptor();
+ String result = interceptor.intercept(mockActionInvocation);
+
+ assertEquals("okok", result);
+
+ controlAction.verify();
+ controlActionProxy.verify();
+ controlActionInvocation.verify();
+ }
+
+ public void testPrepareThrowException() throws Exception {
+ MockActionInvocation mai = new MockActionInvocation();
+ MockActionProxy mockActionProxy = new MockActionProxy();
+ mockActionProxy.setMethod("submit");
+ mai.setProxy(mockActionProxy);
+ mai.setAction(mock.proxy());
+
+ IllegalAccessException illegalAccessException = new IllegalAccessException();
+ mock.expectAndThrow("prepareSubmit", illegalAccessException);
+ mock.matchAndThrow("prepare", new RuntimeException());
+
+ try {
+ interceptor.intercept(mai);
+ fail("Should not have reached this point.");
+ } catch (Throwable t) {
+ assertSame(illegalAccessException, t);
+ }
+ }
+
+ @Override
+ protected void setUp() throws Exception {
+ mock = new Mock(ActionInterface.class);
+ interceptor = new PrepareInterceptor();
+ }
+
+ @Override
+ protected void tearDown() throws Exception {
+ mock.verify();
+ }
+
+
+ /**
+ * Simple interface to test prefix action invocation
+ * eg. prepareSubmit(), prepareSave() etc.
+ *
+ * @author tm_jee
+ */
+ public interface ActionInterface extends Action, Preparable {
+ void prepareSubmit() throws Exception;
+ }
+
+}
Added: struts/struts2/branches/STRUTS_3_X/xwork-core/src/test/java/org/apache/struts2/xwork2/interceptor/ScopedModelDrivenInterceptorTest.java
URL: http://svn.apache.org/viewvc/struts/struts2/branches/STRUTS_3_X/xwork-core/src/test/java/org/apache/struts2/xwork2/interceptor/ScopedModelDrivenInterceptorTest.java?rev=1209569&view=auto
==============================================================================
--- struts/struts2/branches/STRUTS_3_X/xwork-core/src/test/java/org/apache/struts2/xwork2/interceptor/ScopedModelDrivenInterceptorTest.java (added)
+++ struts/struts2/branches/STRUTS_3_X/xwork-core/src/test/java/org/apache/struts2/xwork2/interceptor/ScopedModelDrivenInterceptorTest.java Fri Dec 2 16:33:03 2011
@@ -0,0 +1,239 @@
+/*
+ * Copyright 2002-2007,2009 The Apache Software Foundation.
+ *
+ * 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 org.apache.struts2.xwork2.interceptor;
+
+import org.apache.struts2.xwork2.Action;
+import org.apache.struts2.xwork2.ActionContext;
+import org.apache.struts2.xwork2.ObjectFactory;
+import org.apache.struts2.xwork2.config.entities.ActionConfig;
+import org.apache.struts2.xwork2.mock.MockActionInvocation;
+import org.apache.struts2.xwork2.mock.MockActionProxy;
+import org.apache.struts2.xwork2.test.Equidae;
+import org.apache.struts2.xwork2.test.User;
+import org.apache.struts2.xwork2.XWorkTestCase;
+import org.apache.struts2.xwork2.ProxyObjectFactory;
+import org.apache.struts2.xwork2.SimpleAction;
+
+import java.util.HashMap;
+import java.util.Map;
+
+public class ScopedModelDrivenInterceptorTest extends XWorkTestCase {
+
+ protected ScopedModelDrivenInterceptor inter = null;
+
+ /**
+ * Set up instance variables required by this test case.
+ */
+ @Override
+ public void setUp() throws Exception {
+ super.setUp();
+ inter = new ScopedModelDrivenInterceptor();
+ inter.setObjectFactory(new ProxyObjectFactory());
+ }
+
+ public void testResolveModel() throws Exception {
+ ActionContext ctx = ActionContext.getContext();
+ ctx.setSession(new HashMap());
+
+ ObjectFactory factory = ObjectFactory.getObjectFactory();
+ Object obj = inter.resolveModel(factory, ctx, "java.lang.String", "request", "foo");
+ assertNotNull(obj);
+ assertTrue(obj instanceof String);
+ assertTrue(obj == ctx.get("foo"));
+
+ obj = inter.resolveModel(factory, ctx, "java.lang.String", "session", "foo");
+ assertNotNull(obj);
+ assertTrue(obj instanceof String);
+ assertTrue(obj == ctx.getSession().get("foo"));
+
+ obj = inter.resolveModel(factory, ctx, "java.lang.String", "session", "foo");
+ assertNotNull(obj);
+ assertTrue(obj instanceof String);
+ assertTrue(obj == ctx.getSession().get("foo"));
+ }
+
+ public void testScopedModelDrivenAction() throws Exception {
+ inter.setScope("request");
+
+ ScopedModelDriven action = new MyUserScopedModelDrivenAction();
+ MockActionInvocation mai = new MockActionInvocation();
+ MockActionProxy map = new MockActionProxy();
+ ActionConfig ac = new ActionConfig.Builder("", "", "").build();
+ map.setConfig(ac);
+ mai.setAction(action);
+ mai.setProxy(map);
+
+ inter.intercept(mai);
+ inter.destroy();
+
+ assertNotNull(action.getModel());
+ assertNotNull(action.getScopeKey());
+ assertEquals("org.apache.struts2.xwork2.test.User", action.getScopeKey());
+
+ Object model = ActionContext.getContext().get(action.getScopeKey());
+ assertNotNull(model);
+ assertTrue("Model should be an User object", model instanceof User);
+ }
+
+ public void testScopedModelDrivenActionWithSetClassName() throws Exception {
+ inter.setScope("request");
+ inter.setClassName("org.apache.struts2.xwork2.test.Equidae");
+ inter.setName("queen");
+
+ ScopedModelDriven action = new MyEquidaeScopedModelDrivenAction();
+ MockActionInvocation mai = new MockActionInvocation();
+ MockActionProxy map = new MockActionProxy();
+ ActionConfig ac = new ActionConfig.Builder("", "", "").build();
+ map.setConfig(ac);
+ mai.setAction(action);
+ mai.setProxy(map);
+
+ inter.intercept(mai);
+ inter.destroy();
+
+ assertNotNull(action.getModel());
+ assertNotNull(action.getScopeKey());
+ assertEquals("queen", action.getScopeKey());
+
+ Object model = ActionContext.getContext().get(action.getScopeKey());
+ assertNotNull(model);
+ assertTrue("Model should be an Equidae object", model instanceof Equidae);
+ }
+
+ public void testModelOnSession() throws Exception {
+ inter.setScope("session");
+ inter.setName("king");
+
+ User user = new User();
+ user.setName("King George");
+ Map session = new HashMap();
+ ActionContext.getContext().setSession(session);
+ ActionContext.getContext().getSession().put("king", user);
+
+ ScopedModelDriven action = new MyUserScopedModelDrivenAction();
+ MockActionInvocation mai = new MockActionInvocation();
+ MockActionProxy map = new MockActionProxy();
+ ActionConfig ac = new ActionConfig.Builder("", "", "").build();
+ map.setConfig(ac);
+ mai.setAction(action);
+ mai.setProxy(map);
+
+ inter.intercept(mai);
+ inter.destroy();
+
+ assertNotNull(action.getModel());
+ assertNotNull(action.getScopeKey());
+ assertEquals("king", action.getScopeKey());
+
+ Object model = ActionContext.getContext().getSession().get(action.getScopeKey());
+ assertNotNull(model);
+ assertTrue("Model should be an User object", model instanceof User);
+ assertEquals("King George", ((User) model).getName());
+ }
+
+ public void testModelAlreadySetOnAction() throws Exception {
+ inter.setScope("request");
+ inter.setName("king");
+
+ User user = new User();
+ user.setName("King George");
+
+ ScopedModelDriven action = new MyUserScopedModelDrivenAction();
+ action.setModel(user);
+ MockActionInvocation mai = new MockActionInvocation();
+ MockActionProxy map = new MockActionProxy();
+ ActionConfig ac = new ActionConfig.Builder("", "", "").build();
+ map.setConfig(ac);
+ mai.setAction(action);
+ mai.setProxy(map);
+
+ inter.intercept(mai);
+ inter.destroy();
+
+ assertNotNull(action.getModel());
+ assertNull(action.getScopeKey()); // no scope key as nothing happended
+ }
+
+ public void testNoScopedModelAction() throws Exception {
+ Action action = new SimpleAction();
+ MockActionInvocation mai = new MockActionInvocation();
+ MockActionProxy map = new MockActionProxy();
+ ActionConfig ac = new ActionConfig.Builder("", "", "").build();
+ map.setConfig(ac);
+ mai.setAction(action);
+ mai.setProxy(map);
+
+ inter.intercept(mai);
+ inter.destroy();
+ // nothing happends
+ }
+
+ private class MyUserScopedModelDrivenAction implements ScopedModelDriven, Action {
+
+ private String key;
+ private User model;
+
+ public void setModel(Object model) {
+ this.model = (User) model;
+ }
+
+ public void setScopeKey(String key) {
+ this.key = key;
+ }
+
+ public String getScopeKey() {
+ return key;
+ }
+
+ public User getModel() {
+ return model;
+ }
+
+ public String execute() throws Exception {
+ return SUCCESS;
+ }
+
+ }
+
+ private class MyEquidaeScopedModelDrivenAction implements ScopedModelDriven, Action {
+
+ private String key;
+ private Equidae model;
+
+ public void setModel(Object model) {
+ this.model = (Equidae) model;
+ }
+
+ public void setScopeKey(String key) {
+ this.key = key;
+ }
+
+ public String getScopeKey() {
+ return key;
+ }
+
+ public Equidae getModel() {
+ return model;
+ }
+
+ public String execute() throws Exception {
+ return SUCCESS;
+ }
+
+ }
+
+}
+
Added: struts/struts2/branches/STRUTS_3_X/xwork-core/src/test/java/org/apache/struts2/xwork2/interceptor/StaticParametersInterceptorTest.java
URL: http://svn.apache.org/viewvc/struts/struts2/branches/STRUTS_3_X/xwork-core/src/test/java/org/apache/struts2/xwork2/interceptor/StaticParametersInterceptorTest.java?rev=1209569&view=auto
==============================================================================
--- struts/struts2/branches/STRUTS_3_X/xwork-core/src/test/java/org/apache/struts2/xwork2/interceptor/StaticParametersInterceptorTest.java (added)
+++ struts/struts2/branches/STRUTS_3_X/xwork-core/src/test/java/org/apache/struts2/xwork2/interceptor/StaticParametersInterceptorTest.java Fri Dec 2 16:33:03 2011
@@ -0,0 +1,209 @@
+/*
+ * Copyright 2002-2006,2009 The Apache Software Foundation.
+ *
+ * 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 org.apache.struts2.xwork2.interceptor;
+
+import com.mockobjects.dynamic.Mock;
+import org.apache.struts2.xwork2.ActionContext;
+import org.apache.struts2.xwork2.SimpleFooAction;
+import org.apache.struts2.xwork2.XWorkTestCase;
+import org.apache.struts2.xwork2.config.entities.ActionConfig;
+import org.apache.struts2.xwork2.config.entities.Parameterizable;
+import org.apache.struts2.xwork2.mock.MockActionInvocation;
+import org.apache.struts2.xwork2.mock.MockActionProxy;
+
+import java.util.Map;
+import java.util.HashMap;
+
+/**
+ * Unit test of {@link StaticParametersInterceptor}.
+ *
+ * @author Claus Ibsen
+ */
+public class StaticParametersInterceptorTest extends XWorkTestCase {
+
+ private StaticParametersInterceptor interceptor;
+
+ public void testParameterizable() throws Exception {
+ Mock mock = new Mock(Parameterizable.class);
+
+ MockActionInvocation mai = new MockActionInvocation();
+ MockActionProxy map = new MockActionProxy();
+ ActionConfig ac = new ActionConfig.Builder("", "", "").build();
+
+ Map params = ac.getParams();
+
+ map.setConfig(ac);
+ mai.setProxy(map);
+ mai.setAction(mock.proxy());
+ mock.expect("setParams", params);
+
+ interceptor.intercept(mai);
+ mock.verify();
+ }
+
+ public void testWithOneParameters() throws Exception {
+ MockActionInvocation mai = new MockActionInvocation();
+ MockActionProxy map = new MockActionProxy();
+ ActionConfig ac = new ActionConfig.Builder("", "", "")
+ .addParam("top.name", "Santa")
+ .build();
+
+ map.setConfig(ac);
+ mai.setProxy(map);
+ mai.setAction(new SimpleFooAction());
+
+ User user = new User();
+ ActionContext.getContext().getValueStack().push(user);
+ int before = ActionContext.getContext().getValueStack().size();
+ interceptor.intercept(mai);
+
+ assertEquals(before, ActionContext.getContext().getValueStack().size());
+ assertEquals("Santa", user.getName());
+ }
+
+ public void testWithOneParametersParse() throws Exception {
+ MockActionInvocation mai = new MockActionInvocation();
+ MockActionProxy map = new MockActionProxy();
+ ActionConfig ac = new ActionConfig.Builder("", "", "")
+ .addParam("top.name", "${top.hero}")
+ .build();
+ map.setConfig(ac);
+ mai.setProxy(map);
+ mai.setAction(new SimpleFooAction());
+
+ User user = new User();
+ ActionContext.getContext().getValueStack().push(user);
+ int before = ActionContext.getContext().getValueStack().size();
+ interceptor.setParse("true");
+ interceptor.intercept(mai);
+
+ assertEquals(before, ActionContext.getContext().getValueStack().size());
+ assertEquals("Superman", user.getName());
+ }
+
+ public void testWithOneParametersNoParse() throws Exception {
+ MockActionInvocation mai = new MockActionInvocation();
+ MockActionProxy map = new MockActionProxy();
+ ActionConfig ac = new ActionConfig.Builder("", "", "")
+ .addParam("top.name", "${top.hero}")
+ .build();
+ map.setConfig(ac);
+ mai.setProxy(map);
+ mai.setAction(new SimpleFooAction());
+
+ User user = new User();
+ ActionContext.getContext().getValueStack().push(user);
+ int before = ActionContext.getContext().getValueStack().size();
+ interceptor.setParse("false");
+ interceptor.intercept(mai);
+
+ assertEquals(before, ActionContext.getContext().getValueStack().size());
+ assertEquals("${top.hero}", user.getName());
+ }
+
+ public void testNoMerge() throws Exception {
+ MockActionInvocation mai = new MockActionInvocation();
+ MockActionProxy map = new MockActionProxy();
+ ActionConfig ac = new ActionConfig.Builder("", "", "")
+ .addParam("top.name", "${top.hero}")
+ .build();
+ map.setConfig(ac);
+ mai.setProxy(map);
+ mai.setAction(new SimpleFooAction());
+
+ User user = new User();
+ ActionContext.getContext().getValueStack().push(user);
+ ActionContext.getContext().setParameters(new HashMap<String, Object>());
+ int before = ActionContext.getContext().getValueStack().size();
+ interceptor.setMerge("false");
+ interceptor.intercept(mai);
+
+ assertEquals(before, ActionContext.getContext().getValueStack().size());
+ assertEquals("${top.hero}", user.getName());
+ assertEquals(0, ActionContext.getContext().getParameters().size());
+ }
+
+ public void testFewParametersParse() throws Exception {
+ MockActionInvocation mai = new MockActionInvocation();
+ MockActionProxy map = new MockActionProxy();
+ ActionConfig ac = new ActionConfig.Builder("", "", "")
+ .addParam("top.age", "${top.myAge}")
+ .addParam("top.email", "${top.myEmail}")
+ .build();
+ map.setConfig(ac);
+ mai.setProxy(map);
+ mai.setAction(new SimpleFooAction());
+
+ User user = new User();
+ ActionContext.getContext().getValueStack().push(user);
+ int before = ActionContext.getContext().getValueStack().size();
+ interceptor.setParse("true");
+ interceptor.intercept(mai);
+
+ assertEquals(before, ActionContext.getContext().getValueStack().size());
+ assertEquals(user.getMyAge(), user.age);
+ assertEquals(user.getMyEmail(), user.email);
+ }
+
+ @Override
+ protected void setUp() throws Exception {
+ super.setUp();
+ interceptor = new StaticParametersInterceptor();
+ interceptor.init();
+ container.inject(interceptor);
+ }
+
+ @Override
+ protected void tearDown() throws Exception {
+ super.tearDown();
+ interceptor.destroy();
+ }
+
+ private class User {
+ private String name;
+ private int age;
+ private String email;
+
+ public String getName() {
+ return name;
+ }
+
+ public void setName(String name) {
+ this.name = name;
+ }
+
+ public int getMyAge() {
+ return 33;
+ }
+
+ public void setAge(int age) {
+ this.age = age;
+ }
+
+ public String getMyEmail() {
+ return "lukasz dot lenart at gmail dot com";
+ }
+
+ public void setEmail(String email) {
+ this.email = email;
+ }
+
+ public String getHero() {
+ return "Superman";
+ }
+ }
+
+}
Added: struts/struts2/branches/STRUTS_3_X/xwork-core/src/test/java/org/apache/struts2/xwork2/interceptor/TimerInterceptorTest.java
URL: http://svn.apache.org/viewvc/struts/struts2/branches/STRUTS_3_X/xwork-core/src/test/java/org/apache/struts2/xwork2/interceptor/TimerInterceptorTest.java?rev=1209569&view=auto
==============================================================================
--- struts/struts2/branches/STRUTS_3_X/xwork-core/src/test/java/org/apache/struts2/xwork2/interceptor/TimerInterceptorTest.java (added)
+++ struts/struts2/branches/STRUTS_3_X/xwork-core/src/test/java/org/apache/struts2/xwork2/interceptor/TimerInterceptorTest.java Fri Dec 2 16:33:03 2011
@@ -0,0 +1,169 @@
+/*
+ * Copyright 2002-2006,2009 The Apache Software Foundation.
+ *
+ * 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 org.apache.struts2.xwork2.interceptor;
+
+import org.apache.struts2.xwork2.SimpleFooAction;
+import org.apache.struts2.xwork2.XWorkTestCase;
+import org.apache.struts2.xwork2.mock.MockActionInvocation;
+import org.apache.struts2.xwork2.mock.MockActionProxy;
+import org.apache.struts2.xwork2.util.logging.Logger;
+
+/**
+ * Unit test for {@link TimerInterceptor}.
+ *
+ * @author Claus Ibsen
+ */
+public class TimerInterceptorTest extends XWorkTestCase {
+
+ private MyTimerInterceptor interceptor;
+ private MockActionInvocation mai;
+ private MockActionProxy ap;
+
+
+ public void testTimerInterceptor() throws Exception {
+ TimerInterceptor real = new TimerInterceptor();
+ real.init();
+ real.intercept(mai);
+ real.destroy();
+ }
+
+ public void testInvalidLogLevel() throws Exception {
+ TimerInterceptor real = new TimerInterceptor();
+ real.setLogLevel("xxxx");
+ real.init();
+ try {
+ real.intercept(mai);
+ fail("Should not have reached this point.");
+ } catch (IllegalArgumentException e) {
+ // success
+ }
+ }
+
+ public void testDefault() throws Exception {
+ interceptor.intercept(mai);
+ assertTrue(interceptor.message.startsWith("Executed action [myApp/myAction!execute] took "));
+ assertSame(interceptor.logger, TimerInterceptor.LOG);
+ }
+
+ public void testNoNamespace() throws Exception {
+ ap.setNamespace(null);
+ interceptor.intercept(mai);
+ assertTrue(interceptor.message.startsWith("Executed action [myAction!execute] took "));
+ assertSame(interceptor.logger, TimerInterceptor.LOG);
+ }
+
+ public void testInputMethod() throws Exception {
+ ap.setMethod("input");
+ interceptor.intercept(mai);
+ assertTrue(interceptor.message.startsWith("Executed action [myApp/myAction!input] took "));
+ assertSame(interceptor.logger, TimerInterceptor.LOG);
+ }
+
+ public void testTraceLevel() throws Exception {
+ interceptor.setLogLevel("trace");
+ interceptor.intercept(mai);
+ assertNull(interceptor.message); // no default logging at trace level
+ assertEquals("trace", interceptor.getLogLevel());
+ }
+
+ public void testDebugLevel() throws Exception {
+ interceptor.setLogLevel("debug");
+ interceptor.intercept(mai);
+ assertNull(interceptor.message); // no default logging at debug level
+ }
+
+ public void testInfoLevel() throws Exception {
+ interceptor.setLogLevel("info");
+ interceptor.intercept(mai);
+ assertTrue(interceptor.message.startsWith("Executed action [myApp/myAction!execute] took "));
+ assertSame(interceptor.logger, TimerInterceptor.LOG);
+ }
+
+ public void testWarnLevel() throws Exception {
+ interceptor.setLogLevel("warn");
+ interceptor.intercept(mai);
+ assertTrue(interceptor.message.startsWith("Executed action [myApp/myAction!execute] took "));
+ assertSame(interceptor.logger, TimerInterceptor.LOG);
+ }
+
+ public void testErrorLevel() throws Exception {
+ interceptor.setLogLevel("error");
+ interceptor.intercept(mai);
+ assertTrue(interceptor.message.startsWith("Executed action [myApp/myAction!execute] took "));
+ assertSame(interceptor.logger, TimerInterceptor.LOG);
+ }
+
+ public void testFatalLevel() throws Exception {
+ interceptor.setLogLevel("fatal");
+ interceptor.intercept(mai);
+ assertTrue(interceptor.message.startsWith("Executed action [myApp/myAction!execute] took "));
+ assertSame(interceptor.logger, TimerInterceptor.LOG);
+ }
+
+ public void testLogCategory() throws Exception {
+ interceptor.setLogCategory("com.mycompany.myapp.actiontiming");
+ interceptor.intercept(mai);
+ assertTrue(interceptor.message.startsWith("Executed action [myApp/myAction!execute] took "));
+ assertNotSame(interceptor.logger, TimerInterceptor.LOG);
+ }
+
+ public void testLogCategoryLevel() throws Exception {
+ interceptor.setLogCategory("com.mycompany.myapp.actiontiming");
+ interceptor.setLogLevel("error");
+ interceptor.intercept(mai);
+ assertTrue(interceptor.message.startsWith("Executed action [myApp/myAction!execute] took "));
+ assertNotSame(interceptor.logger, TimerInterceptor.LOG);
+ assertEquals("com.mycompany.myapp.actiontiming", interceptor.getLogCategory());
+ }
+
+ @Override
+ protected void setUp() throws Exception {
+ super.setUp();
+ interceptor = new MyTimerInterceptor();
+ interceptor.init();
+
+ mai = new MockActionInvocation();
+ ap = new MockActionProxy();
+ ap.setActionName("myAction");
+ ap.setNamespace("myApp");
+ ap.setMethod("execute");
+ mai.setAction(new SimpleFooAction());
+ mai.setProxy(ap);
+ }
+
+ @Override
+ protected void tearDown() throws Exception {
+ super.tearDown();
+ interceptor.destroy();
+ ap = null;
+ mai = null;
+ }
+
+ private class MyTimerInterceptor extends TimerInterceptor {
+
+ private Logger logger;
+ private String message;
+
+ @Override
+ protected void doLog(Logger logger, String message) {
+ super.doLog(logger, message);
+
+ this.logger = logger;
+ this.message = message;
+ }
+ }
+
+}
Added: struts/struts2/branches/STRUTS_3_X/xwork-core/src/test/java/org/apache/struts2/xwork2/interceptor/ValidationInterceptorPrefixMethodInvocationTest.java
URL: http://svn.apache.org/viewvc/struts/struts2/branches/STRUTS_3_X/xwork-core/src/test/java/org/apache/struts2/xwork2/interceptor/ValidationInterceptorPrefixMethodInvocationTest.java?rev=1209569&view=auto
==============================================================================
--- struts/struts2/branches/STRUTS_3_X/xwork-core/src/test/java/org/apache/struts2/xwork2/interceptor/ValidationInterceptorPrefixMethodInvocationTest.java (added)
+++ struts/struts2/branches/STRUTS_3_X/xwork-core/src/test/java/org/apache/struts2/xwork2/interceptor/ValidationInterceptorPrefixMethodInvocationTest.java Fri Dec 2 16:33:03 2011
@@ -0,0 +1,114 @@
+/*
+ * Copyright 2002-2006,2009 The Apache Software Foundation.
+ *
+ * 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 org.apache.struts2.xwork2.interceptor;
+
+import org.apache.struts2.xwork2.ActionContext;
+import org.apache.struts2.xwork2.config.entities.InterceptorConfig;
+import org.apache.struts2.xwork2.Action;
+import org.apache.struts2.xwork2.ActionInvocation;
+import org.apache.struts2.xwork2.ActionProxy;
+import org.apache.struts2.xwork2.ObjectFactory;
+import org.apache.struts2.xwork2.ValidationAware;
+import org.apache.struts2.xwork2.config.entities.ActionConfig;
+import org.apache.struts2.xwork2.validator.ValidationInterceptor;
+import org.apache.struts2.xwork2.Validateable;
+import org.apache.struts2.xwork2.XWorkTestCase;
+import org.easymock.EasyMock;
+import org.easymock.IAnswer;
+
+import java.util.HashMap;
+
+/**
+ * Test ValidationInterceptor's prefix method invocation capabilities.
+ *
+ * @author tm_jee
+ * @version $Date: 2011-12-02 12:24:48 +0100 (Fri, 02 Dec 2011) $ $Id: ValidationInterceptorPrefixMethodInvocationTest.java 1209415 2011-12-02 11:24:48Z lukaszlenart $
+ */
+public class ValidationInterceptorPrefixMethodInvocationTest extends XWorkTestCase {
+ private ActionInvocation invocation;
+ private ActionConfig config;
+ private ActionProxy proxy;
+ private ValidateAction action;
+ private String result;
+ private String method;
+
+ public void testPrefixMethodInvocation1() throws Exception {
+ method = "save";
+ result = Action.INPUT;
+
+ ValidationInterceptor interceptor = create();
+ String result = interceptor.intercept(invocation);
+
+ assertEquals(Action.INPUT, result);
+ }
+
+ public void testPrefixMethodInvocation2() throws Exception {
+ method = "save";
+ result = "okok";
+
+ ValidationInterceptor interceptor = create();
+ String result = interceptor.intercept(invocation);
+
+ assertEquals("okok", result);
+ }
+
+ protected ValidationInterceptor create() {
+ ObjectFactory objectFactory = container.getInstance(ObjectFactory.class);
+ return (ValidationInterceptor) objectFactory.buildInterceptor(
+ new InterceptorConfig.Builder("model", ValidationInterceptor.class.getName()).build(), new HashMap());
+ }
+
+ private interface ValidateAction extends Action, Validateable, ValidationAware {
+ void validateDoSave();
+ void validateSubmit();
+ String submit();
+ }
+
+ @Override
+ protected void setUp() throws Exception {
+ super.setUp();
+
+ config = new ActionConfig.Builder("", "action", "").build();
+ invocation = EasyMock.createNiceMock(ActionInvocation.class);
+ proxy = EasyMock.createNiceMock(ActionProxy.class);
+ action = EasyMock.createNiceMock(ValidateAction.class);
+
+
+ EasyMock.expect(invocation.getProxy()).andReturn(proxy).anyTimes();
+ EasyMock.expect(invocation.getAction()).andReturn(action).anyTimes();
+ EasyMock.expect(invocation.invoke()).andAnswer(new IAnswer<String>() {
+ public String answer() throws Throwable {
+ return result;
+ }
+ }).anyTimes();
+
+ EasyMock.expect(proxy.getConfig()).andReturn(config).anyTimes();
+ EasyMock.expect(proxy.getMethod()).andAnswer(new IAnswer<String>() {
+ public String answer() throws Throwable {
+ return method;
+ }
+ }).anyTimes();
+
+
+ EasyMock.replay(invocation);
+ EasyMock.replay(action);
+ EasyMock.replay(proxy);
+
+ ActionContext contex = new ActionContext(new HashMap<String, Object>());
+ ActionContext.setContext(contex);
+ contex.setActionInvocation(invocation);
+ }
+}
Added: struts/struts2/branches/STRUTS_3_X/xwork-core/src/test/java/org/apache/struts2/xwork2/interceptor/annotations/AllowingByDefaultAction.java
URL: http://svn.apache.org/viewvc/struts/struts2/branches/STRUTS_3_X/xwork-core/src/test/java/org/apache/struts2/xwork2/interceptor/annotations/AllowingByDefaultAction.java?rev=1209569&view=auto
==============================================================================
--- struts/struts2/branches/STRUTS_3_X/xwork-core/src/test/java/org/apache/struts2/xwork2/interceptor/annotations/AllowingByDefaultAction.java (added)
+++ struts/struts2/branches/STRUTS_3_X/xwork-core/src/test/java/org/apache/struts2/xwork2/interceptor/annotations/AllowingByDefaultAction.java Fri Dec 2 16:33:03 2011
@@ -0,0 +1,23 @@
+package org.apache.struts2.xwork2.interceptor.annotations;
+
+import org.apache.struts2.xwork2.ActionSupport;
+
+/**
+ * @author martin.gilday
+ *
+ */
+public class AllowingByDefaultAction extends ActionSupport {
+
+ @Blocked
+ private String name;
+ private String job;
+
+ public void setName(String name) {
+ this.name = name;
+ }
+
+ public void setJob(String job) {
+ this.job = job;
+ }
+
+}
Added: struts/struts2/branches/STRUTS_3_X/xwork-core/src/test/java/org/apache/struts2/xwork2/interceptor/annotations/AllowingByDefaultModel.java
URL: http://svn.apache.org/viewvc/struts/struts2/branches/STRUTS_3_X/xwork-core/src/test/java/org/apache/struts2/xwork2/interceptor/annotations/AllowingByDefaultModel.java?rev=1209569&view=auto
==============================================================================
--- struts/struts2/branches/STRUTS_3_X/xwork-core/src/test/java/org/apache/struts2/xwork2/interceptor/annotations/AllowingByDefaultModel.java (added)
+++ struts/struts2/branches/STRUTS_3_X/xwork-core/src/test/java/org/apache/struts2/xwork2/interceptor/annotations/AllowingByDefaultModel.java Fri Dec 2 16:33:03 2011
@@ -0,0 +1,21 @@
+package org.apache.struts2.xwork2.interceptor.annotations;
+
+/**
+ * @author jafl
+ *
+ */
+public class AllowingByDefaultModel {
+
+ @Blocked
+ private String m1;
+ private String m2;
+
+ public void setM1(String s) {
+ m1 = s;
+ }
+
+ public void setM2(String s) {
+ m2 = s;
+ }
+
+}
Added: struts/struts2/branches/STRUTS_3_X/xwork-core/src/test/java/org/apache/struts2/xwork2/interceptor/annotations/AnnotatedAction.java
URL: http://svn.apache.org/viewvc/struts/struts2/branches/STRUTS_3_X/xwork-core/src/test/java/org/apache/struts2/xwork2/interceptor/annotations/AnnotatedAction.java?rev=1209569&view=auto
==============================================================================
--- struts/struts2/branches/STRUTS_3_X/xwork-core/src/test/java/org/apache/struts2/xwork2/interceptor/annotations/AnnotatedAction.java (added)
+++ struts/struts2/branches/STRUTS_3_X/xwork-core/src/test/java/org/apache/struts2/xwork2/interceptor/annotations/AnnotatedAction.java Fri Dec 2 16:33:03 2011
@@ -0,0 +1,46 @@
+/*
+ * Copyright 2002-2006,2009 The Apache Software Foundation.
+ *
+ * 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 org.apache.struts2.xwork2.interceptor.annotations;
+
+import org.apache.struts2.xwork2.Action;
+
+/**
+ * @author Zsolt Szasz, zsolt at lorecraft dot com
+ * @author Rainer Hermanns
+ */
+public class AnnotatedAction extends BaseAnnotatedAction {
+
+ @Before(priority=5)
+ public String before() {
+ log = log + "before";
+ return null;
+ }
+
+ public String execute() {
+ log = log + "-execute";
+ return Action.SUCCESS;
+ }
+
+ @BeforeResult
+ public void beforeResult() throws Exception {
+ log = log +"-beforeResult";
+ }
+
+ @After(priority=5)
+ public void after() {
+ log = log + "-after";
+ }
+}