Search in sources :

Example 6 with SimpleAction

use of com.opensymphony.xwork2.SimpleAction in project struts by apache.

the class ValidateAction method testAcceptedTrickyParameters.

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<>();
    extraContext.put(ActionContext.PARAMETERS, HttpParameters.create(params).build());
    ActionProxy proxy = actionProxyFactory.createActionProxy("", MockConfigurationProvider.PARAM_INTERCEPTOR_ACTION_NAME, null, 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("try_1", action.getName());
    assertEquals("This is blah", (action).getBlah());
    assertEquals(123, action.getBaz());
}
Also used : ActionConfig(com.opensymphony.xwork2.config.entities.ActionConfig) ActionProxy(com.opensymphony.xwork2.ActionProxy) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) SimpleAction(com.opensymphony.xwork2.SimpleAction)

Example 7 with SimpleAction

use of com.opensymphony.xwork2.SimpleAction in project struts by apache.

the class ValidateAction method testNonexistentParametersAreIgnoredInProductionMode.

public void testNonexistentParametersAreIgnoredInProductionMode() throws Exception {
    XmlConfigurationProvider provider = new StrutsXmlConfigurationProvider("xwork-test-beans.xml");
    container.inject(provider);
    loadConfigurationProviders(provider, new MockConfigurationProvider(Collections.singletonMap("struts.devMode", "false")));
    Map<String, Object> params = new HashMap<>();
    params.put("not_a_property", "There is no action property named like this");
    HashMap<String, Object> extraContext = new HashMap<>();
    extraContext.put(ActionContext.PARAMETERS, HttpParameters.create(params).build());
    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, null, extraContext);
    proxy.execute();
    assertTrue(((SimpleAction) proxy.getAction()).getActionMessages().isEmpty());
}
Also used : ActionConfig(com.opensymphony.xwork2.config.entities.ActionConfig) StrutsXmlConfigurationProvider(org.apache.struts2.config.StrutsXmlConfigurationProvider) StrutsXmlConfigurationProvider(org.apache.struts2.config.StrutsXmlConfigurationProvider) XmlConfigurationProvider(com.opensymphony.xwork2.config.providers.XmlConfigurationProvider) ActionProxy(com.opensymphony.xwork2.ActionProxy) MockConfigurationProvider(com.opensymphony.xwork2.config.providers.MockConfigurationProvider) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) SimpleAction(com.opensymphony.xwork2.SimpleAction)

Example 8 with SimpleAction

use of com.opensymphony.xwork2.SimpleAction in project struts by apache.

the class ValidateAction method testParametersOverwriteField.

public void testParametersOverwriteField() throws Exception {
    Map<String, Object> params = new LinkedHashMap<>();
    params.put("existingMap.boo", "This is blah");
    HashMap<String, Object> extraContext = new HashMap<>();
    extraContext.put(ActionContext.PARAMETERS, HttpParameters.create(params).build());
    ActionProxy proxy = actionProxyFactory.createActionProxy("", MockConfigurationProvider.PARAM_INTERCEPTOR_ACTION_NAME, null, extraContext);
    proxy.execute();
    SimpleAction action = (SimpleAction) proxy.getAction();
    assertEquals(1, action.getTheExistingMap().size());
    assertNotNull(action.getTheExistingMap().get("boo"));
    assertNull(action.getTheExistingMap().get("existingKey"));
}
Also used : ActionProxy(com.opensymphony.xwork2.ActionProxy) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) SimpleAction(com.opensymphony.xwork2.SimpleAction) LinkedHashMap(java.util.LinkedHashMap)

Example 9 with SimpleAction

use of com.opensymphony.xwork2.SimpleAction in project struts by apache.

the class ValidateAction method testParametersNotAccessProtectedMethods.

public void testParametersNotAccessProtectedMethods() throws Exception {
    Map<String, Object> params = new HashMap<>();
    params.put("theSemiProtectedMap.foo", "This is blah");
    params.put("theProtectedMap.boo", "This is blah");
    HashMap<String, Object> extraContext = new HashMap<>();
    extraContext.put(ActionContext.PARAMETERS, HttpParameters.create(params).build());
    ActionProxy proxy = actionProxyFactory.createActionProxy("", MockConfigurationProvider.PARAM_INTERCEPTOR_ACTION_NAME, null, extraContext);
    proxy.execute();
    SimpleAction action = (SimpleAction) proxy.getAction();
    assertEquals(1, action.getTheProtectedMap().size());
    assertNotNull(action.getTheProtectedMap().get("boo"));
    assertNull(action.getTheProtectedMap().get("foo"));
}
Also used : ActionProxy(com.opensymphony.xwork2.ActionProxy) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) SimpleAction(com.opensymphony.xwork2.SimpleAction)

Example 10 with SimpleAction

use of com.opensymphony.xwork2.SimpleAction in project struts by apache.

the class AliasInterceptorTest method testNameNotAccepted.

public void testNameNotAccepted() throws Exception {
    Map<String, Object> params = new HashMap<>();
    params.put("aliasSource", "source here");
    Map<String, String> httpParams = new HashMap<>();
    httpParams.put("name", "getAliasSource()");
    httpParams.put("value", "aliasDest");
    params.put("parameters", HttpParameters.create(httpParams).build());
    XmlConfigurationProvider provider = new StrutsXmlConfigurationProvider("xwork-sample.xml");
    container.inject(provider);
    loadConfigurationProviders(provider);
    ActionProxy proxy = actionProxyFactory.createActionProxy("", "dynamicAliasTest", null, params);
    SimpleAction actionOne = (SimpleAction) proxy.getAction();
    actionOne.setAliasSource("name to be copied");
    // prevent ERROR result
    actionOne.setFoo(-1);
    actionOne.setBar(1);
    proxy.execute();
    assertEquals("name to be copied", actionOne.getAliasSource());
    assertNotEquals(actionOne.getAliasSource(), actionOne.getAliasDest());
    proxy = actionProxyFactory.createActionProxy("", "dynamicAliasTest", null, params);
    ((AliasInterceptor) proxy.getConfig().getInterceptors().get(1).getInterceptor()).setExcludedPatterns(NO_EXCLUSION_PATTERNS_CHECKER);
    ((AliasInterceptor) proxy.getConfig().getInterceptors().get(1).getInterceptor()).setAcceptedPatterns(ACCEPT_ALL_PATTERNS_CHECKER);
    actionOne = (SimpleAction) proxy.getAction();
    actionOne.setAliasSource("name to be copied");
    // prevent ERROR result
    actionOne.setFoo(-1);
    actionOne.setBar(1);
    proxy.execute();
    assertEquals("name to be copied", actionOne.getAliasSource());
    assertEquals(actionOne.getAliasSource(), actionOne.getAliasDest());
}
Also used : StrutsXmlConfigurationProvider(org.apache.struts2.config.StrutsXmlConfigurationProvider) XmlConfigurationProvider(com.opensymphony.xwork2.config.providers.XmlConfigurationProvider) StrutsXmlConfigurationProvider(org.apache.struts2.config.StrutsXmlConfigurationProvider) ActionProxy(com.opensymphony.xwork2.ActionProxy) MockActionProxy(com.opensymphony.xwork2.mock.MockActionProxy) HashMap(java.util.HashMap) SimpleAction(com.opensymphony.xwork2.SimpleAction)

Aggregations

SimpleAction (com.opensymphony.xwork2.SimpleAction)32 ActionProxy (com.opensymphony.xwork2.ActionProxy)24 HashMap (java.util.HashMap)23 LinkedHashMap (java.util.LinkedHashMap)17 MockActionProxy (com.opensymphony.xwork2.mock.MockActionProxy)14 ActionConfig (com.opensymphony.xwork2.config.entities.ActionConfig)8 XmlConfigurationProvider (com.opensymphony.xwork2.config.providers.XmlConfigurationProvider)7 StrutsXmlConfigurationProvider (org.apache.struts2.config.StrutsXmlConfigurationProvider)7 OgnlUtil (com.opensymphony.xwork2.ognl.OgnlUtil)6 Action (com.opensymphony.xwork2.Action)5 TestBean (com.opensymphony.xwork2.TestBean)5 OgnlValueStack (com.opensymphony.xwork2.ognl.OgnlValueStack)5 ValueStack (com.opensymphony.xwork2.util.ValueStack)5 ActionInvocation (com.opensymphony.xwork2.ActionInvocation)4 ModelDrivenAction (com.opensymphony.xwork2.ModelDrivenAction)3 ConversionData (com.opensymphony.xwork2.conversion.impl.ConversionData)3 MockActionInvocation (com.opensymphony.xwork2.mock.MockActionInvocation)3 StaticWebApplicationContext (org.springframework.web.context.support.StaticWebApplicationContext)3 MockConfigurationProvider (com.opensymphony.xwork2.config.providers.MockConfigurationProvider)2 MockResult (com.opensymphony.xwork2.mock.MockResult)2