Search in sources :

Example 1 with ParametersInterceptor

use of com.opensymphony.xwork2.interceptor.ParametersInterceptor in project struts by apache.

the class ValidateAction method doTestParameterNameLengthRestriction.

private void doTestParameterNameLengthRestriction(ParametersInterceptor parametersInterceptor, int paramNameMaxLength) {
    StringBuilder sb = new StringBuilder();
    for (int i = 0; i < paramNameMaxLength + 1; i++) {
        sb.append("x");
    }
    Map<String, Object> actual = new LinkedHashMap<>();
    parametersInterceptor.setValueStackFactory(createValueStackFactory(actual));
    ValueStack stack = createStubValueStack(actual);
    Map<String, Object> parameters = new HashMap<>();
    parameters.put(sb.toString(), "");
    parameters.put("huuhaa", "");
    Action action = new SimpleAction();
    parametersInterceptor.setParameters(action, stack, HttpParameters.create(parameters).build());
    assertEquals(1, actual.size());
}
Also used : SimpleAction(com.opensymphony.xwork2.SimpleAction) ModelDrivenAction(com.opensymphony.xwork2.ModelDrivenAction) Action(com.opensymphony.xwork2.Action) OgnlValueStack(com.opensymphony.xwork2.ognl.OgnlValueStack) ValueStack(com.opensymphony.xwork2.util.ValueStack) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) SimpleAction(com.opensymphony.xwork2.SimpleAction) LinkedHashMap(java.util.LinkedHashMap)

Example 2 with ParametersInterceptor

use of com.opensymphony.xwork2.interceptor.ParametersInterceptor in project struts by apache.

the class ValidateAction method testOrdered.

public void testOrdered() throws Exception {
    ParametersInterceptor pi = new ParametersInterceptor();
    pi.setOrdered(true);
    container.inject(pi);
    final Map<String, Object> actual = new LinkedHashMap<>();
    pi.setValueStackFactory(createValueStackFactory(actual));
    ValueStack stack = createStubValueStack(actual);
    Map<String, Object> parameters = new HashMap<>();
    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, HttpParameters.create(parameters).build());
    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));
}
Also used : SimpleAction(com.opensymphony.xwork2.SimpleAction) ModelDrivenAction(com.opensymphony.xwork2.ModelDrivenAction) Action(com.opensymphony.xwork2.Action) OgnlValueStack(com.opensymphony.xwork2.ognl.OgnlValueStack) ValueStack(com.opensymphony.xwork2.util.ValueStack) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) ArrayList(java.util.ArrayList) SimpleAction(com.opensymphony.xwork2.SimpleAction) LinkedHashMap(java.util.LinkedHashMap)

Example 3 with ParametersInterceptor

use of com.opensymphony.xwork2.interceptor.ParametersInterceptor 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 4 with ParametersInterceptor

use of com.opensymphony.xwork2.interceptor.ParametersInterceptor in project struts by apache.

the class ValidateAction method testExcludedParametersAreIgnored.

public void testExcludedParametersAreIgnored() throws Exception {
    ParametersInterceptor pi = createParametersInterceptor();
    pi.setExcludeParams("dojo\\..*");
    final Map<String, Object> 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, HttpParameters.create(parameters).build());
    assertEquals(expected, actual);
}
Also used : OgnlValueStack(com.opensymphony.xwork2.ognl.OgnlValueStack) ValueStack(com.opensymphony.xwork2.util.ValueStack) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap)

Example 5 with ParametersInterceptor

use of com.opensymphony.xwork2.interceptor.ParametersInterceptor in project struts by apache.

the class ValidateAction method testNoParametersAction.

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();
}
Also used : SimpleAction(com.opensymphony.xwork2.SimpleAction) ModelDrivenAction(com.opensymphony.xwork2.ModelDrivenAction) Action(com.opensymphony.xwork2.Action) MockActionInvocation(com.opensymphony.xwork2.mock.MockActionInvocation)

Aggregations

HashMap (java.util.HashMap)14 LinkedHashMap (java.util.LinkedHashMap)13 OgnlValueStack (com.opensymphony.xwork2.ognl.OgnlValueStack)11 ValueStack (com.opensymphony.xwork2.util.ValueStack)11 SimpleAction (com.opensymphony.xwork2.SimpleAction)6 Action (com.opensymphony.xwork2.Action)4 ModelDrivenAction (com.opensymphony.xwork2.ModelDrivenAction)4 StrutsDefaultConfigurationProvider (com.opensymphony.xwork2.config.providers.StrutsDefaultConfigurationProvider)4 StrutsXmlConfigurationProvider (org.apache.struts2.config.StrutsXmlConfigurationProvider)4 ActionConfig (com.opensymphony.xwork2.config.entities.ActionConfig)3 ActionProxy (com.opensymphony.xwork2.ActionProxy)2 ArrayList (java.util.ArrayList)2 InterceptorMapping (com.opensymphony.xwork2.config.entities.InterceptorMapping)1 PackageConfig (com.opensymphony.xwork2.config.entities.PackageConfig)1 ResultConfig (com.opensymphony.xwork2.config.entities.ResultConfig)1 ContainerBuilder (com.opensymphony.xwork2.inject.ContainerBuilder)1 ParametersInterceptor (com.opensymphony.xwork2.interceptor.ParametersInterceptor)1 MockActionInvocation (com.opensymphony.xwork2.mock.MockActionInvocation)1 MockResult (com.opensymphony.xwork2.mock.MockResult)1 ValidationInterceptor (com.opensymphony.xwork2.validator.ValidationInterceptor)1