Search in sources :

Example 1 with MockObjectTypeDeterminer

use of com.opensymphony.xwork2.mock.MockObjectTypeDeterminer in project struts by apache.

the class SetPropertiesTest method doTestAddingToMapsWithObjects.

public void doTestAddingToMapsWithObjects(boolean allowAdditions) throws Exception {
    loadButAdd(ObjectTypeDeterminer.class, new MockObjectTypeDeterminer(Long.class, Cat.class, null, allowAdditions));
    Foo foo = new Foo();
    foo.setAnotherCatMap(new HashMap());
    String spielname = "Spielen";
    ValueStack vs = ActionContext.getContext().getValueStack();
    vs.getContext().put(XWorkConverter.REPORT_CONVERSION_ERRORS, Boolean.TRUE);
    vs.getContext().put(ReflectionContextState.CREATE_NULL_OBJECTS, Boolean.TRUE);
    vs.push(foo);
    vs.getContext().put(XWorkConverter.REPORT_CONVERSION_ERRORS, Boolean.TRUE);
    vs.setValue("anotherCatMap[\"3\"].name", spielname);
    Object setCat = foo.getAnotherCatMap().get(new Long(3));
    if (allowAdditions) {
        assertNotNull(setCat);
        assertTrue(setCat instanceof Cat);
        assertTrue(((Cat) setCat).getName().equals(spielname));
    } else {
        assertNull(setCat);
    }
}
Also used : ValueStack(com.opensymphony.xwork2.util.ValueStack) Cat(com.opensymphony.xwork2.util.Cat) Foo(com.opensymphony.xwork2.util.Foo) MockObjectTypeDeterminer(com.opensymphony.xwork2.mock.MockObjectTypeDeterminer)

Example 2 with MockObjectTypeDeterminer

use of com.opensymphony.xwork2.mock.MockObjectTypeDeterminer 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 3 with MockObjectTypeDeterminer

use of com.opensymphony.xwork2.mock.MockObjectTypeDeterminer in project struts by apache.

the class SetPropertiesTest method doTestAddingToListsWithObjects.

public void doTestAddingToListsWithObjects(final boolean allowAdditions) {
    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 new MockObjectTypeDeterminer(null, Cat.class, null, allowAdditions);
                }

                @Override
                public Class type() {
                    return Cat.class;
                }
            });
        }
    });
    Foo foo = new Foo();
    foo.setMoreCats(new ArrayList());
    String spielname = "Spielen";
    ValueStack vs = ActionContext.getContext().getValueStack();
    vs.getContext().put(XWorkConverter.REPORT_CONVERSION_ERRORS, Boolean.TRUE);
    vs.getContext().put(ReflectionContextState.CREATE_NULL_OBJECTS, Boolean.TRUE);
    vs.push(foo);
    try {
        vs.setValue("moreCats[2].name", spielname);
    } catch (IndexOutOfBoundsException e) {
        if (allowAdditions) {
            throw e;
        }
    }
    Object setCat = null;
    if (allowAdditions) {
        setCat = foo.getMoreCats().get(2);
        assertNotNull(setCat);
        assertTrue(setCat instanceof Cat);
        assertTrue(((Cat) setCat).getName().equals(spielname));
    } else {
        assertTrue(foo.getMoreCats() == null || foo.getMoreCats().size() == 0);
    }
    // has been created
    if (allowAdditions) {
        spielname = "paws";
        vs.setValue("moreCats[0].name", spielname);
        setCat = foo.getMoreCats().get(0);
        assertNotNull(setCat);
        assertTrue(setCat instanceof Cat);
        assertTrue(((Cat) setCat).getName().equals(spielname));
    }
}
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) Factory(com.opensymphony.xwork2.inject.Factory) MockObjectTypeDeterminer(com.opensymphony.xwork2.mock.MockObjectTypeDeterminer) 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) Cat(com.opensymphony.xwork2.util.Cat)

Aggregations

MockObjectTypeDeterminer (com.opensymphony.xwork2.mock.MockObjectTypeDeterminer)3 Foo (com.opensymphony.xwork2.util.Foo)3 ValueStack (com.opensymphony.xwork2.util.ValueStack)3 ActionContext (com.opensymphony.xwork2.ActionContext)2 ConfigurationException (com.opensymphony.xwork2.config.ConfigurationException)2 ObjectTypeDeterminer (com.opensymphony.xwork2.conversion.ObjectTypeDeterminer)2 ContainerBuilder (com.opensymphony.xwork2.inject.ContainerBuilder)2 Context (com.opensymphony.xwork2.inject.Context)2 Factory (com.opensymphony.xwork2.inject.Factory)2 StubConfigurationProvider (com.opensymphony.xwork2.test.StubConfigurationProvider)2 Cat (com.opensymphony.xwork2.util.Cat)2 LocatableProperties (com.opensymphony.xwork2.util.location.LocatableProperties)2 Bar (com.opensymphony.xwork2.util.Bar)1