Search in sources :

Example 16 with TestBean

use of com.opensymphony.xwork2.TestBean 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 17 with TestBean

use of com.opensymphony.xwork2.TestBean in project struts by apache.

the class BeanConfigTest method testConstructor2.

@Test
public void testConstructor2() throws Exception {
    Class<TestBean> expectedClass = TestBean.class;
    String expectedName = "expectedBeanName";
    Class<Object> expectedType = Object.class;
    Scope expectedScope = Scope.PROTOTYPE;
    boolean expectedOnlyStatic = true;
    boolean expectedOptional = true;
    BeanConfig beanConfig = new BeanConfig(expectedClass, expectedName, expectedType, expectedScope, expectedOnlyStatic, expectedOptional);
    Assert.assertEquals(expectedClass, beanConfig.getClazz());
    Assert.assertEquals(expectedName, beanConfig.getName());
    Assert.assertEquals(expectedScope, beanConfig.getScope());
    Assert.assertEquals(expectedType, beanConfig.getType());
    Assert.assertEquals(expectedOnlyStatic, beanConfig.isOnlyStatic());
    Assert.assertEquals(expectedOptional, beanConfig.isOptional());
}
Also used : Scope(com.opensymphony.xwork2.inject.Scope) TestBean(com.opensymphony.xwork2.TestBean) Test(org.junit.Test)

Example 18 with TestBean

use of com.opensymphony.xwork2.TestBean in project struts by apache.

the class ValidateAction method testModelDrivenParameters.

public void testModelDrivenParameters() throws Exception {
    Map<String, Object> params = new HashMap<>();
    final String fooVal = "com.opensymphony.xwork2.interceptor.ParametersInterceptorTest.foo";
    params.put("foo", fooVal);
    final String nameVal = "com.opensymphony.xwork2.interceptor.ParametersInterceptorTest.name";
    params.put("name", nameVal);
    params.put("count", "15");
    HashMap<String, Object> extraContext = new HashMap<>();
    extraContext.put(ActionContext.PARAMETERS, HttpParameters.create(params).build());
    ActionProxy proxy = actionProxyFactory.createActionProxy("", MockConfigurationProvider.MODEL_DRIVEN_PARAM_TEST, null, extraContext);
    assertEquals(Action.SUCCESS, proxy.execute());
    ModelDrivenAction action = (ModelDrivenAction) proxy.getAction();
    TestBean model = (TestBean) action.getModel();
    assertEquals(nameVal, model.getName());
    assertEquals(15, model.getCount());
    assertEquals(fooVal, action.getFoo());
}
Also used : ActionProxy(com.opensymphony.xwork2.ActionProxy) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) TestBean(com.opensymphony.xwork2.TestBean) ModelDrivenAction(com.opensymphony.xwork2.ModelDrivenAction)

Aggregations

TestBean (com.opensymphony.xwork2.TestBean)11 SimpleAction (com.opensymphony.xwork2.SimpleAction)5 TestBean (org.apache.commons.beanutils2.TestBean)4 ActionInvocation (com.opensymphony.xwork2.ActionInvocation)3 ActionConfig (com.opensymphony.xwork2.config.entities.ActionConfig)3 List (java.util.List)3 ActionProxy (com.opensymphony.xwork2.ActionProxy)2 HashMap (java.util.HashMap)2 Test (org.junit.Test)2 ModelDrivenAction (com.opensymphony.xwork2.ModelDrivenAction)1 Configuration (com.opensymphony.xwork2.config.Configuration)1 ConfigurationException (com.opensymphony.xwork2.config.ConfigurationException)1 MockConfiguration (com.opensymphony.xwork2.config.impl.MockConfiguration)1 Container (com.opensymphony.xwork2.inject.Container)1 ContainerBuilder (com.opensymphony.xwork2.inject.ContainerBuilder)1 Scope (com.opensymphony.xwork2.inject.Scope)1 TestBean2 (com.opensymphony.xwork2.test.TestBean2)1 LocatableProperties (com.opensymphony.xwork2.util.location.LocatableProperties)1 ArrayList (java.util.ArrayList)1 Calendar (java.util.Calendar)1