Search in sources :

Example 21 with Bar

use of com.opensymphony.xwork2.util.Bar 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 22 with Bar

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

the class OgnlUtilTest method testGetBeanMap.

public void testGetBeanMap() throws Exception {
    Bar bar = new Bar();
    bar.setTitle("I have beer");
    Foo foo = new Foo();
    foo.setALong(123);
    foo.setNumber(44);
    foo.setBar(bar);
    foo.setTitle("Hello Santa");
    foo.setUseful(true);
    // just do some of the 15 tests
    Map<String, Object> beans = ognlUtil.getBeanMap(foo);
    assertNotNull(beans);
    assertEquals(22, beans.size());
    assertEquals("Hello Santa", beans.get("title"));
    assertEquals(new Long("123"), beans.get("ALong"));
    assertEquals(new Integer("44"), beans.get("number"));
    assertEquals(bar, beans.get("bar"));
    assertEquals(Boolean.TRUE, beans.get("useful"));
}
Also used : Bar(com.opensymphony.xwork2.util.Bar) Foo(com.opensymphony.xwork2.util.Foo)

Example 23 with Bar

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

the class OgnlValueStackTest method testGetBarAsString.

public void testGetBarAsString() {
    Foo foo = new Foo();
    Bar bar = new Bar();
    bar.setTitle("bar");
    bar.setSomethingElse(123);
    foo.setBar(bar);
    OgnlValueStack vs = createValueStack();
    vs.push(foo);
    String output = (String) vs.findValue("bar", String.class);
    assertEquals("bar:123", output);
}
Also used : Foo(com.opensymphony.xwork2.util.Foo)

Example 24 with Bar

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

the class OgnlValueStackTest method testSetBarAsString.

public void testSetBarAsString() {
    Foo foo = new Foo();
    OgnlValueStack vs = createValueStack();
    vs.push(foo);
    vs.setValue("bar", "bar:123");
    assertEquals("bar", foo.getBar().getTitle());
    assertEquals(123, foo.getBar().getSomethingElse());
}
Also used : Foo(com.opensymphony.xwork2.util.Foo)

Example 25 with Bar

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

the class OgnlValueStackTest method testGetComplexBarAsString.

public void testGetComplexBarAsString() {
    // children foo->foo->foo
    Foo foo = new Foo();
    Foo foo2 = new Foo();
    foo.setChild(foo2);
    Foo foo3 = new Foo();
    foo2.setChild(foo3);
    // relatives
    Foo fooA = new Foo();
    foo.setRelatives(new Foo[] { fooA });
    Foo fooB = new Foo();
    foo2.setRelatives(new Foo[] { fooB });
    Foo fooC = new Foo();
    foo3.setRelatives(new Foo[] { fooC });
    // the bar
    Bar bar = new Bar();
    bar.setTitle("bar");
    bar.setSomethingElse(123);
    // now place the bar all over
    foo.setBar(bar);
    foo2.setBar(bar);
    foo3.setBar(bar);
    fooA.setBar(bar);
    fooB.setBar(bar);
    fooC.setBar(bar);
    OgnlValueStack vs = createValueStack();
    vs.push(foo);
    vs.getContext().put("foo", foo);
    assertEquals("bar:123", vs.findValue("#foo.bar", String.class));
    assertEquals("bar:123", vs.findValue("bar", String.class));
    assertEquals("bar:123", vs.findValue("child.bar", String.class));
    assertEquals("bar:123", vs.findValue("child.child.bar", String.class));
    assertEquals("bar:123", vs.findValue("relatives[0].bar", String.class));
    assertEquals("bar:123", vs.findValue("child.relatives[0].bar", String.class));
    assertEquals("bar:123", vs.findValue("child.child.relatives[0].bar", String.class));
    vs.push(vs.findValue("child"));
    assertEquals("bar:123", vs.findValue("bar", String.class));
    assertEquals("bar:123", vs.findValue("child.bar", String.class));
    assertEquals("bar:123", vs.findValue("relatives[0].bar", String.class));
    assertEquals("bar:123", vs.findValue("child.relatives[0].bar", String.class));
}
Also used : Foo(com.opensymphony.xwork2.util.Foo)

Aggregations

HashMap (java.util.HashMap)17 Bar (com.opensymphony.xwork2.util.Bar)14 Foo (com.opensymphony.xwork2.util.Foo)13 ValueStack (com.opensymphony.xwork2.util.ValueStack)12 ActionProxy (com.opensymphony.xwork2.ActionProxy)9 ActionConfig (com.opensymphony.xwork2.config.entities.ActionConfig)9 ConfigurationProvider (com.opensymphony.xwork2.config.ConfigurationProvider)7 ValidationAware (com.opensymphony.xwork2.interceptor.ValidationAware)7 List (java.util.List)6 Map (java.util.Map)6 ConversionData (com.opensymphony.xwork2.conversion.impl.ConversionData)5 Mock (com.mockobjects.dynamic.Mock)4 ActionContext (com.opensymphony.xwork2.ActionContext)4 PackageConfig (com.opensymphony.xwork2.config.entities.PackageConfig)4 ResultConfig (com.opensymphony.xwork2.config.entities.ResultConfig)3 ContainerBuilder (com.opensymphony.xwork2.inject.ContainerBuilder)3 MockResult (com.opensymphony.xwork2.mock.MockResult)3 ModelDrivenAction2 (com.opensymphony.xwork2.test.ModelDrivenAction2)3 LocatableProperties (com.opensymphony.xwork2.util.location.LocatableProperties)3 ActionSupport (com.opensymphony.xwork2.ActionSupport)2