Search in sources :

Example 46 with Builder

use of com.opensymphony.xwork2.config.entities.PackageConfig.Builder in project struts by apache.

the class StrutsBeanSelectionProviderTest method testRegister.

public void testRegister() {
    LocalizedTextProvider localizedTextProvider = container.getInstance(LocalizedTextProvider.class);
    assertEquals("The form has already been processed or no token was supplied, please try again.", localizedTextProvider.findDefaultText("struts.messages.invalid.token", Locale.getDefault()));
    loadConfigurationProviders(new StubConfigurationProvider() {

        @Override
        public void register(ContainerBuilder builder, LocatableProperties props) throws ConfigurationException {
            props.setProperty(StrutsConstants.STRUTS_CUSTOM_I18N_RESOURCES, "testmessages,testmessages2");
            props.setProperty(StrutsConstants.STRUTS_LOCALE, "US");
        }
    });
    localizedTextProvider = container.getInstance(LocalizedTextProvider.class);
    assertEquals("Replaced message for token tag", localizedTextProvider.findDefaultText("struts.messages.invalid.token", Locale.getDefault()));
}
Also used : ContainerBuilder(com.opensymphony.xwork2.inject.ContainerBuilder) StubConfigurationProvider(com.opensymphony.xwork2.test.StubConfigurationProvider) LocatableProperties(com.opensymphony.xwork2.util.location.LocatableProperties) ConfigurationException(com.opensymphony.xwork2.config.ConfigurationException) LocalizedTextProvider(com.opensymphony.xwork2.LocalizedTextProvider)

Example 47 with Builder

use of com.opensymphony.xwork2.config.entities.PackageConfig.Builder in project struts by apache.

the class StrutsJavaConfigurationProviderTest method testRegister.

@Test
public void testRegister() throws Exception {
    final ConstantConfig constantConfig = new ConstantConfig();
    constantConfig.setDevMode(true);
    final String expectedUnknownHandler = "expectedUnknownHandler";
    StrutsJavaConfiguration javaConfig = new StrutsJavaConfiguration() {

        @Override
        public List<String> unknownHandlerStack() {
            return Collections.singletonList(expectedUnknownHandler);
        }

        @Override
        public List<ConstantConfig> constants() {
            return Collections.singletonList(constantConfig);
        }

        @Override
        public List<BeanConfig> beans() {
            return Arrays.asList(new BeanConfig(TestBean.class, "struts"), new BeanConfig(TestBean.class, "struts.static", TestBean.class, Scope.PROTOTYPE, true, true), new BeanConfig(TestBean.class, "struts.test.bean", TestBean.class));
        }

        @Override
        public Optional<BeanSelectionConfig> beanSelection() {
            return Optional.of(new BeanSelectionConfig(TestBeanSelectionProvider.class, "testBeans"));
        }
    };
    StrutsJavaConfigurationProvider provider = new StrutsJavaConfigurationProvider(javaConfig);
    Configuration configuration = new MockConfiguration();
    provider.init(configuration);
    ContainerBuilder builder = new ContainerBuilder();
    LocatableProperties props = new LocatableProperties();
    provider.register(builder, props);
    // constant
    Assert.assertEquals(String.valueOf(constantConfig.getDevMode()), props.get(StrutsConstants.STRUTS_DEVMODE));
    // unknown-handler-stack
    Assert.assertNotNull(configuration.getUnknownHandlerStack());
    Assert.assertEquals(1, configuration.getUnknownHandlerStack().size());
    Assert.assertEquals(expectedUnknownHandler, configuration.getUnknownHandlerStack().get(0).getName());
    // bean
    Container container = builder.create(true);
    TestBean testBean = container.getInstance(TestBean.class);
    Assert.assertNotNull(testBean);
    testBean = container.getInstance(TestBean.class, "struts");
    Assert.assertNotNull(testBean);
    // bean selection
    Set<String> names = container.getInstanceNames(TestBean.class);
    Assert.assertTrue(names.contains("struts"));
    Assert.assertTrue(names.contains("struts.test.bean"));
}
Also used : BeanConfig(org.apache.struts2.config.entities.BeanConfig) MockConfiguration(com.opensymphony.xwork2.config.impl.MockConfiguration) Configuration(com.opensymphony.xwork2.config.Configuration) Container(com.opensymphony.xwork2.inject.Container) ContainerBuilder(com.opensymphony.xwork2.inject.ContainerBuilder) TestBean(com.opensymphony.xwork2.TestBean) LocatableProperties(com.opensymphony.xwork2.util.location.LocatableProperties) MockConfiguration(com.opensymphony.xwork2.config.impl.MockConfiguration) BeanSelectionConfig(org.apache.struts2.config.entities.BeanSelectionConfig) ConstantConfig(org.apache.struts2.config.entities.ConstantConfig) Test(org.junit.Test)

Example 48 with Builder

use of com.opensymphony.xwork2.config.entities.PackageConfig.Builder in project struts by apache.

the class OgnlValueStackTest method testFailOnTooLongExpressionLongerThan192_ViaOverriddenProperty.

public void testFailOnTooLongExpressionLongerThan192_ViaOverriddenProperty() {
    try {
        loadConfigurationProviders(new StubConfigurationProvider() {

            @Override
            public void register(ContainerBuilder builder, LocatableProperties props) throws ConfigurationException {
                props.setProperty(StrutsConstants.STRUTS_OGNL_EXPRESSION_MAX_LENGTH, "192");
            }
        });
        Integer repeat = Integer.parseInt(container.getInstance(String.class, StrutsConstants.STRUTS_OGNL_EXPRESSION_MAX_LENGTH));
        OgnlValueStack vs = createValueStack();
        try {
            vs.findValue(StringUtils.repeat('.', repeat + 1), true);
            fail("Failed to throw exception on too long expression");
        } catch (Exception ex) {
            assertTrue(ex.getCause() instanceof OgnlException);
            assertTrue(((OgnlException) ex.getCause()).getReason() instanceof SecurityException);
        }
    } finally {
        // Reset expressionMaxLength value to default (disabled)
        ognlUtil.applyExpressionMaxLength(null);
    }
}
Also used : OgnlException(ognl.OgnlException) ContainerBuilder(com.opensymphony.xwork2.inject.ContainerBuilder) StubConfigurationProvider(com.opensymphony.xwork2.test.StubConfigurationProvider) LocatableProperties(com.opensymphony.xwork2.util.location.LocatableProperties) ConfigurationException(com.opensymphony.xwork2.config.ConfigurationException) ParseException(ognl.ParseException) ConfigurationException(com.opensymphony.xwork2.config.ConfigurationException) StrutsException(org.apache.struts2.StrutsException) OgnlException(ognl.OgnlException)

Example 49 with Builder

use of com.opensymphony.xwork2.config.entities.PackageConfig.Builder 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 50 with Builder

use of com.opensymphony.xwork2.config.entities.PackageConfig.Builder 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

PackageConfig (com.opensymphony.xwork2.config.entities.PackageConfig)23 ResultConfig (com.opensymphony.xwork2.config.entities.ResultConfig)21 ServletContext (javax.servlet.ServletContext)21 ContainerBuilder (com.opensymphony.xwork2.inject.ContainerBuilder)19 LocatableProperties (com.opensymphony.xwork2.util.location.LocatableProperties)18 ConfigurationException (com.opensymphony.xwork2.config.ConfigurationException)17 HashSet (java.util.HashSet)14 StubConfigurationProvider (com.opensymphony.xwork2.test.StubConfigurationProvider)10 NoAnnotationAction (org.apache.struts2.convention.actions.NoAnnotationAction)7 ClassLevelResultPathAction (org.apache.struts2.convention.actions.resultpath.ClassLevelResultPathAction)6 Action (org.apache.struts2.convention.annotation.Action)6 ObjectFactory (com.opensymphony.xwork2.ObjectFactory)4 Configuration (com.opensymphony.xwork2.config.Configuration)4 Container (com.opensymphony.xwork2.inject.Container)4 Context (com.opensymphony.xwork2.inject.Context)4 ArrayList (java.util.ArrayList)4 ActionContext (com.opensymphony.xwork2.ActionContext)3 ConfigurationProvider (com.opensymphony.xwork2.config.ConfigurationProvider)3 ResultTypeConfig (com.opensymphony.xwork2.config.entities.ResultTypeConfig)3 StrutsDefaultConfigurationProvider (com.opensymphony.xwork2.config.providers.StrutsDefaultConfigurationProvider)3