Search in sources :

Example 41 with Foo

use of com.opensymphony.xwork2.util.Foo in project struts by apache.

the class XmlConfigurationProviderActionsTest method testDefaultActionClass.

public void testDefaultActionClass() throws Exception {
    final String filename = "com/opensymphony/xwork2/config/providers/xwork-test-actions.xml";
    final String testDefaultClassName = "com.opensymphony.xwork2.ActionSupport";
    ConfigurationProvider provider = buildConfigurationProvider(filename);
    // setup expectations
    params.put("foo", "17");
    params.put("bar", "23");
    ActionConfig barWithoutClassNameConfig = new ActionConfig.Builder("", "BarWithoutClassName", "").addParams(params).build();
    // execute the configuration
    provider.init(configuration);
    PackageConfig pkg = configuration.getPackageConfig("default");
    Map actionConfigs = pkg.getActionConfigs();
    // assertions
    assertEquals(7, actionConfigs.size());
    assertEquals(barWithoutClassNameConfig, actionConfigs.get("BarWithoutClassName"));
}
Also used : ConfigurationProvider(com.opensymphony.xwork2.config.ConfigurationProvider) Map(java.util.Map) HashMap(java.util.HashMap)

Example 42 with Foo

use of com.opensymphony.xwork2.util.Foo in project struts by apache.

the class XmlConfigurationProviderActionsTest method testActions.

public void testActions() throws Exception {
    final String filename = "com/opensymphony/xwork2/config/providers/xwork-test-actions.xml";
    ConfigurationProvider provider = buildConfigurationProvider(filename);
    // setup expectations
    // bar action is very simple, just two params
    params.put("foo", "17");
    params.put("bar", "23");
    params.put("testXW412", "foo.jspa?fooID=${fooID}&something=bar");
    params.put("testXW412Again", "something");
    ActionConfig barAction = new ActionConfig.Builder("", "Bar", SimpleAction.class.getName()).addParams(params).build();
    // foo action is a little more complex, two params, a result and an interceptor stack
    results = new HashMap<>();
    params = new HashMap<>();
    params.put("foo", "18");
    params.put("bar", "24");
    results.put("success", new ResultConfig.Builder("success", MockResult.class.getName()).build());
    InterceptorConfig noopInterceptorConfig = new InterceptorConfig.Builder("noop", NoOpInterceptor.class.getName()).build();
    interceptors.add(new InterceptorMapping("noop", objectFactory.buildInterceptor(noopInterceptorConfig, new HashMap<String, String>())));
    ActionConfig fooAction = new ActionConfig.Builder("", "Foo", SimpleAction.class.getName()).addParams(params).addResultConfigs(results).addInterceptors(interceptors).build();
    // wildcard action is simple wildcard example
    results = new HashMap<>();
    results.put("*", new ResultConfig.Builder("*", MockResult.class.getName()).build());
    ActionConfig wildcardAction = new ActionConfig.Builder("", "WildCard", SimpleAction.class.getName()).addResultConfigs(results).addInterceptors(interceptors).build();
    // fooBar action is a little more complex, two params, a result and an interceptor stack
    params = new HashMap<String, String>();
    params.put("foo", "18");
    params.put("bar", "24");
    results = new HashMap<>();
    results.put("success", new ResultConfig.Builder("success", MockResult.class.getName()).build());
    ExceptionMappingConfig exceptionConfig = new ExceptionMappingConfig.Builder("runtime", "java.lang.RuntimeException", "exception").build();
    exceptionMappings.add(exceptionConfig);
    ActionConfig fooBarAction = new ActionConfig.Builder("", "FooBar", SimpleAction.class.getName()).addParams(params).addResultConfigs(results).addInterceptors(interceptors).addExceptionMappings(exceptionMappings).build();
    // TestInterceptorParam action tests that an interceptor worked
    HashMap<String, String> interceptorParams = new HashMap<>();
    interceptorParams.put("expectedFoo", "expectedFooValue");
    interceptorParams.put("foo", MockInterceptor.DEFAULT_FOO_VALUE);
    InterceptorConfig mockInterceptorConfig = new InterceptorConfig.Builder("test", MockInterceptor.class.getName()).build();
    interceptors = new ArrayList<>();
    interceptors.add(new InterceptorMapping("test", objectFactory.buildInterceptor(mockInterceptorConfig, interceptorParams)));
    ActionConfig intAction = new ActionConfig.Builder("", "TestInterceptorParam", SimpleAction.class.getName()).addInterceptors(interceptors).build();
    // TestInterceptorParamOverride action tests that an interceptor with a param override worked
    interceptorParams = new HashMap<>();
    interceptorParams.put("expectedFoo", "expectedFooValue");
    interceptorParams.put("foo", "foo123");
    interceptors = new ArrayList<>();
    interceptors.add(new InterceptorMapping("test", objectFactory.buildInterceptor(mockInterceptorConfig, interceptorParams)));
    ActionConfig intOverAction = new ActionConfig.Builder("", "TestInterceptorParamOverride", SimpleAction.class.getName()).addInterceptors(interceptors).build();
    // execute the configuration
    provider.init(configuration);
    provider.loadPackages();
    PackageConfig pkg = configuration.getPackageConfig("default");
    Map actionConfigs = pkg.getActionConfigs();
    // assertions
    assertEquals(7, actionConfigs.size());
    assertEquals(barAction, actionConfigs.get("Bar"));
    assertEquals(fooAction, actionConfigs.get("Foo"));
    assertEquals(wildcardAction, actionConfigs.get("WildCard"));
    assertEquals(fooBarAction, actionConfigs.get("FooBar"));
    assertEquals(intAction, actionConfigs.get("TestInterceptorParam"));
    assertEquals(intOverAction, actionConfigs.get("TestInterceptorParamOverride"));
}
Also used : MockResult(com.opensymphony.xwork2.mock.MockResult) HashMap(java.util.HashMap) ConfigurationProvider(com.opensymphony.xwork2.config.ConfigurationProvider) SimpleAction(com.opensymphony.xwork2.SimpleAction) Map(java.util.Map) HashMap(java.util.HashMap)

Example 43 with Foo

use of com.opensymphony.xwork2.util.Foo in project struts by apache.

the class OgnlUtilTest method testSetIndexedValue.

/**
 * Test that type conversion is performed on indexed collection properties.
 */
public void testSetIndexedValue() {
    ValueStack stack = ActionContext.getContext().getValueStack();
    Map<String, Object> stackContext = stack.getContext();
    stackContext.put(ReflectionContextState.CREATE_NULL_OBJECTS, Boolean.TRUE);
    stackContext.put(ReflectionContextState.DENY_METHOD_EXECUTION, Boolean.TRUE);
    stackContext.put(XWorkConverter.REPORT_CONVERSION_ERRORS, Boolean.TRUE);
    User user = new User();
    stack.push(user);
    // indexed string w/ existing array
    user.setList(new ArrayList<>());
    user.getList().add("");
    String[] foo = new String[] { "asdf" };
    stack.setValue("list[0]", foo);
    assertNotNull(user.getList());
    assertEquals(1, user.getList().size());
    assertEquals(String.class, user.getList().get(0).getClass());
    assertEquals("asdf", user.getList().get(0));
}
Also used : User(com.opensymphony.xwork2.test.User) StubValueStack(com.opensymphony.xwork2.StubValueStack) ValueStack(com.opensymphony.xwork2.util.ValueStack)

Example 44 with Foo

use of com.opensymphony.xwork2.util.Foo in project struts by apache.

the class OgnlUtilTest method testAllowCallingMethodsOnObjectClassInDevModeTrue.

public void testAllowCallingMethodsOnObjectClassInDevModeTrue() {
    Exception expected = null;
    try {
        ognlUtil.setExcludedClasses(Foo.class.getName());
        ognlUtil.setDevModeExcludedClasses("");
        ognlUtil.setDevMode(Boolean.TRUE.toString());
        Foo foo = new Foo();
        String result = (String) ognlUtil.getValue("toString", ognlUtil.createDefaultContext(foo), foo, String.class);
        assertEquals("Foo", result);
    } catch (OgnlException e) {
        expected = e;
    }
    assertNull(expected);
}
Also used : OgnlException(ognl.OgnlException) Foo(com.opensymphony.xwork2.util.Foo) NoSuchPropertyException(ognl.NoSuchPropertyException) MethodFailedException(ognl.MethodFailedException) InappropriateExpressionException(ognl.InappropriateExpressionException) ConfigurationException(com.opensymphony.xwork2.config.ConfigurationException) IntrospectionException(java.beans.IntrospectionException) StrutsException(org.apache.struts2.StrutsException) OgnlException(ognl.OgnlException)

Example 45 with Foo

use of com.opensymphony.xwork2.util.Foo in project struts by apache.

the class OgnlUtilTest method testSetPropertiesBoolean.

public void testSetPropertiesBoolean() {
    Foo foo = new Foo();
    Map<String, Object> context = ognlUtil.createDefaultContext(foo);
    Map<String, Object> props = new HashMap<>();
    props.put("useful", "true");
    ognlUtil.setProperties(props, foo, context);
    assertTrue(foo.isUseful());
    props = new HashMap<>();
    props.put("useful", "false");
    ognlUtil.setProperties(props, foo, context);
    assertFalse(foo.isUseful());
}
Also used : HashMap(java.util.HashMap) Foo(com.opensymphony.xwork2.util.Foo)

Aggregations

Foo (com.opensymphony.xwork2.util.Foo)53 HashMap (java.util.HashMap)32 ValueStack (com.opensymphony.xwork2.util.ValueStack)23 ConfigurationException (com.opensymphony.xwork2.config.ConfigurationException)20 ActionConfig (com.opensymphony.xwork2.config.entities.ActionConfig)19 StrutsException (org.apache.struts2.StrutsException)19 OgnlException (ognl.OgnlException)18 IntrospectionException (java.beans.IntrospectionException)17 InappropriateExpressionException (ognl.InappropriateExpressionException)17 MethodFailedException (ognl.MethodFailedException)17 NoSuchPropertyException (ognl.NoSuchPropertyException)17 ActionProxy (com.opensymphony.xwork2.ActionProxy)16 PackageConfig (com.opensymphony.xwork2.config.entities.PackageConfig)9 ActionContext (com.opensymphony.xwork2.ActionContext)8 ConfigurationProvider (com.opensymphony.xwork2.config.ConfigurationProvider)8 Bar (com.opensymphony.xwork2.util.Bar)8 Map (java.util.Map)8 ResultConfig (com.opensymphony.xwork2.config.entities.ResultConfig)7 ConversionData (com.opensymphony.xwork2.conversion.impl.ConversionData)7 StubValueStack (com.opensymphony.xwork2.StubValueStack)6