Search in sources :

Example 31 with Factory

use of com.opensymphony.xwork2.inject.Factory 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)

Example 32 with Factory

use of com.opensymphony.xwork2.inject.Factory in project struts by apache.

the class ScopedModelDrivenInterceptorTest method testResolveModel.

public void testResolveModel() throws Exception {
    ActionContext ctx = ActionContext.getContext().withSession(new HashMap<>());
    ObjectFactory factory = ActionContext.getContext().getContainer().getInstance(ObjectFactory.class);
    Object obj = inter.resolveModel(factory, ctx, "java.lang.String", "request", "foo");
    assertNotNull(obj);
    assertTrue(obj instanceof String);
    assertSame(obj, ctx.get("foo"));
    obj = inter.resolveModel(factory, ctx, "java.lang.String", "session", "foo");
    assertNotNull(obj);
    assertTrue(obj instanceof String);
    assertSame(obj, ctx.getSession().get("foo"));
    obj = inter.resolveModel(factory, ctx, "java.lang.String", "session", "foo");
    assertNotNull(obj);
    assertTrue(obj instanceof String);
    assertSame(obj, ctx.getSession().get("foo"));
}
Also used : ProxyObjectFactory(com.opensymphony.xwork2.ProxyObjectFactory) ObjectFactory(com.opensymphony.xwork2.ObjectFactory) ActionContext(com.opensymphony.xwork2.ActionContext)

Example 33 with Factory

use of com.opensymphony.xwork2.inject.Factory in project onebusaway-application-modules by camsys.

the class SpringContainer method register.

@Override
public void register(ContainerBuilder builder, LocatableProperties props) throws ConfigurationException {
    // Since we're about to override...
    builder.setAllowDuplicates(true);
    builder.factory(ObjectFactory.class, new Factory<ObjectFactory>() {

        public ObjectFactory create(Context xworkContext) throws Exception {
            SpringObjectFactory f = new SpringObjectFactory();
            xworkContext.getContainer().inject(f);
            f.setApplicationContext(_applicationContext);
            f.setAutowireStrategy(_autoWireStrategy);
            return f;
        }

        @Override
        public Class<? extends ObjectFactory> type() {
            return ObjectFactory.class;
        }
    });
}
Also used : Context(com.opensymphony.xwork2.inject.Context) ApplicationContext(org.springframework.context.ApplicationContext) SpringObjectFactory(com.opensymphony.xwork2.spring.SpringObjectFactory) SpringObjectFactory(com.opensymphony.xwork2.spring.SpringObjectFactory) ObjectFactory(com.opensymphony.xwork2.ObjectFactory) ConfigurationException(com.opensymphony.xwork2.config.ConfigurationException)

Aggregations

ConfigurationException (com.opensymphony.xwork2.config.ConfigurationException)10 ObjectFactory (com.opensymphony.xwork2.ObjectFactory)7 ValueStack (com.opensymphony.xwork2.util.ValueStack)5 DefaultFileManagerFactory (com.opensymphony.xwork2.util.fs.DefaultFileManagerFactory)5 ActionProxyFactory (com.opensymphony.xwork2.ActionProxyFactory)4 InterceptorMapping (com.opensymphony.xwork2.config.entities.InterceptorMapping)4 Context (com.opensymphony.xwork2.inject.Context)4 ArrayList (java.util.ArrayList)4 StrutsException (org.apache.struts2.StrutsException)4 ActionContext (com.opensymphony.xwork2.ActionContext)3 DefaultActionProxyFactory (com.opensymphony.xwork2.DefaultActionProxyFactory)3 FileManager (com.opensymphony.xwork2.FileManager)3 ContainerBuilder (com.opensymphony.xwork2.inject.ContainerBuilder)3 Factory (com.opensymphony.xwork2.inject.Factory)3 StubConfigurationProvider (com.opensymphony.xwork2.test.StubConfigurationProvider)3 DefaultFileManager (com.opensymphony.xwork2.util.fs.DefaultFileManager)3 LocatableProperties (com.opensymphony.xwork2.util.location.LocatableProperties)3 HashMap (java.util.HashMap)3 HashSet (java.util.HashSet)3 HttpServletRequest (javax.servlet.http.HttpServletRequest)3