Search in sources :

Example 66 with Bar

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

the class SetPropertiesTest method testAddingToCollectionBasedOnPermission.

public void testAddingToCollectionBasedOnPermission() {
    final MockObjectTypeDeterminer determiner = new MockObjectTypeDeterminer(Long.class, Bar.class, "id", true);
    loadConfigurationProviders(new StubConfigurationProvider() {

        @Override
        public void register(ContainerBuilder builder, LocatableProperties props) throws ConfigurationException {
            builder.factory(ObjectTypeDeterminer.class, new Factory() {

                public Object create(Context context) throws Exception {
                    return determiner;
                }

                @Override
                public Class type() {
                    return determiner.getClass();
                }
            }, Scope.SINGLETON);
        }
    });
    Collection barColl = new HashSet();
    ValueStack vs = ActionContext.getContext().getValueStack();
    ReflectionContextState.setCreatingNullObjects(vs.getContext(), true);
    ReflectionContextState.setReportingConversionErrors(vs.getContext(), true);
    Foo foo = new Foo();
    foo.setBarCollection(barColl);
    vs.push(foo);
    String bar1Title = "title";
    vs.setValue("barCollection(11).title", bar1Title);
    assertEquals(1, barColl.size());
    Object bar = barColl.iterator().next();
    assertTrue(bar instanceof Bar);
    assertEquals(((Bar) bar).getTitle(), bar1Title);
    assertEquals(((Bar) bar).getId(), new Long(11));
    // now test where there is no permission
    determiner.setShouldCreateIfNew(false);
    String bar2Title = "another title";
    vs.setValue("barCollection(22).title", bar1Title);
    assertEquals(1, barColl.size());
    bar = barColl.iterator().next();
    assertTrue(bar instanceof Bar);
    assertEquals(((Bar) bar).getTitle(), bar1Title);
    assertEquals(((Bar) bar).getId(), new Long(11));
}
Also used : Context(com.opensymphony.xwork2.inject.Context) ActionContext(com.opensymphony.xwork2.ActionContext) ValueStack(com.opensymphony.xwork2.util.ValueStack) StubConfigurationProvider(com.opensymphony.xwork2.test.StubConfigurationProvider) Foo(com.opensymphony.xwork2.util.Foo) MockObjectTypeDeterminer(com.opensymphony.xwork2.mock.MockObjectTypeDeterminer) Factory(com.opensymphony.xwork2.inject.Factory) Bar(com.opensymphony.xwork2.util.Bar) ContainerBuilder(com.opensymphony.xwork2.inject.ContainerBuilder) ObjectTypeDeterminer(com.opensymphony.xwork2.conversion.ObjectTypeDeterminer) MockObjectTypeDeterminer(com.opensymphony.xwork2.mock.MockObjectTypeDeterminer) LocatableProperties(com.opensymphony.xwork2.util.location.LocatableProperties) ConfigurationException(com.opensymphony.xwork2.config.ConfigurationException)

Example 67 with Bar

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

the class OgnlUtilTest method testCopyUnevenObjects.

public void testCopyUnevenObjects() {
    Foo foo = new Foo();
    Bar bar = new Bar();
    Map<String, Object> context = ognlUtil.createDefaultContext(foo);
    Calendar cal = Calendar.getInstance();
    cal.clear();
    cal.set(Calendar.MONTH, Calendar.FEBRUARY);
    cal.set(Calendar.DAY_OF_MONTH, 12);
    cal.set(Calendar.YEAR, 1982);
    foo.setTitle("blah");
    foo.setNumber(1);
    foo.setPoints(new long[] { 1, 2, 3 });
    foo.setBirthday(cal.getTime());
    foo.setUseful(false);
    ognlUtil.copy(foo, bar, context);
    assertEquals(foo.getTitle(), bar.getTitle());
    assertEquals(0, bar.getSomethingElse());
}
Also used : Bar(com.opensymphony.xwork2.util.Bar) Foo(com.opensymphony.xwork2.util.Foo) Calendar(java.util.Calendar)

Example 68 with Bar

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

the class ListAction method testFindConversionMappingForInterface.

public void testFindConversionMappingForInterface() {
    ModelDrivenAction2 action = new ModelDrivenAction2();
    stack.push(action);
    stack.push(action.getModel());
    Map<String, Object> ognlStackContext = stack.getContext();
    ognlStackContext.put(XWorkConverter.REPORT_CONVERSION_ERRORS, Boolean.TRUE);
    String value = "asdf:123";
    Object o = converter.convertValue(ognlStackContext, action.getModel(), null, "barObj", value, Bar.class);
    assertNotNull(o);
    assertTrue(o instanceof Bar);
    Bar b = (Bar) o;
    assertEquals(value, b.getTitle() + ":" + b.getSomethingElse());
}
Also used : ModelDrivenAction2(com.opensymphony.xwork2.test.ModelDrivenAction2) Bar(com.opensymphony.xwork2.util.Bar)

Example 69 with Bar

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

the class XmlConfigurationProviderActionsTest method testPackageDefaultClassRef.

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

Example 70 with Bar

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

the class XmlConfigurationProviderAllowedMethodsTest method testDefaultAllowedMethods.

public void testDefaultAllowedMethods() throws ConfigurationException {
    final String filename = "com/opensymphony/xwork2/config/providers/xwork-test-allowed-methods.xml";
    ConfigurationProvider provider = buildConfigurationProvider(filename);
    // execute the configuration
    provider.init(configuration);
    provider.loadPackages();
    PackageConfig pkg = configuration.getPackageConfig("default");
    Map actionConfigs = pkg.getActionConfigs();
    // assertions
    assertEquals(5, actionConfigs.size());
    ActionConfig action = (ActionConfig) actionConfigs.get("Default");
    assertEquals(1, action.getAllowedMethods().size());
    assertTrue(action.isAllowedMethod("execute"));
    assertTrue(action.isAllowedMethod("input"));
    assertTrue(action.isAllowedMethod("cancel"));
    assertTrue(action.isAllowedMethod("foo"));
    assertTrue(action.isAllowedMethod("bar"));
    assertTrue(action.isAllowedMethod("baz"));
    assertTrue(action.isAllowedMethod("xyz"));
    action = (ActionConfig) actionConfigs.get("Boring");
    assertEquals(2, action.getAllowedMethods().size());
    assertTrue(action.isAllowedMethod("execute"));
    assertTrue(action.isAllowedMethod("input"));
    assertTrue(action.isAllowedMethod("cancel"));
    assertFalse(action.isAllowedMethod("foo"));
    assertFalse(action.isAllowedMethod("bar"));
    assertFalse(action.isAllowedMethod("baz"));
    assertFalse(action.isAllowedMethod("xyz"));
    action = (ActionConfig) actionConfigs.get("Foo");
    assertEquals(3, action.getAllowedMethods().size());
    assertTrue(action.isAllowedMethod("execute"));
    assertTrue(action.isAllowedMethod("input"));
    assertTrue(action.isAllowedMethod("cancel"));
    assertTrue(action.isAllowedMethod("foo"));
    assertFalse(action.isAllowedMethod("bar"));
    assertFalse(action.isAllowedMethod("baz"));
    assertFalse(action.isAllowedMethod("xyz"));
    action = (ActionConfig) actionConfigs.get("Bar");
    assertEquals(4, action.getAllowedMethods().size());
    assertTrue(action.isAllowedMethod("execute"));
    assertTrue(action.isAllowedMethod("input"));
    assertTrue(action.isAllowedMethod("cancel"));
    assertTrue(action.isAllowedMethod("foo"));
    assertTrue(action.isAllowedMethod("bar"));
    assertFalse(action.isAllowedMethod("baz"));
    assertFalse(action.isAllowedMethod("xyz"));
    action = (ActionConfig) actionConfigs.get("Baz");
    assertEquals(5, action.getAllowedMethods().size());
    assertFalse(action.isAllowedMethod("execute"));
    assertTrue(action.isAllowedMethod("input"));
    assertTrue(action.isAllowedMethod("cancel"));
    assertTrue(action.isAllowedMethod("foo"));
    assertTrue(action.isAllowedMethod("bar"));
    assertTrue(action.isAllowedMethod("baz"));
    assertFalse(action.isAllowedMethod("xyz"));
}
Also used : ActionConfig(com.opensymphony.xwork2.config.entities.ActionConfig) ConfigurationProvider(com.opensymphony.xwork2.config.ConfigurationProvider) Map(java.util.Map) PackageConfig(com.opensymphony.xwork2.config.entities.PackageConfig)

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