Search in sources :

Example 1 with TestBean

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

the class VisitorFieldValidatorModelTest method setUp.

@Override
public void setUp() throws Exception {
    super.setUp();
    action = new VisitorValidatorModelAction();
    TestBean bean = action.getBean();
    Calendar cal = new GregorianCalendar(1900, 01, 01);
    bean.setBirth(cal.getTime());
    bean.setCount(-1);
    ActionConfig config = new ActionConfig.Builder("", "name", "").build();
    ActionInvocation invocation = EasyMock.createNiceMock(ActionInvocation.class);
    ActionProxy proxy = EasyMock.createNiceMock(ActionProxy.class);
    EasyMock.expect(invocation.getProxy()).andReturn(proxy).anyTimes();
    EasyMock.expect(invocation.getAction()).andReturn(null).anyTimes();
    EasyMock.expect(invocation.invoke()).andReturn(Action.SUCCESS).anyTimes();
    EasyMock.expect(proxy.getMethod()).andReturn("execute").anyTimes();
    EasyMock.expect(proxy.getConfig()).andReturn(config).anyTimes();
    EasyMock.replay(invocation);
    EasyMock.replay(proxy);
    ActionContext.getContext().withActionInvocation(invocation);
}
Also used : ActionConfig(com.opensymphony.xwork2.config.entities.ActionConfig)

Example 2 with TestBean

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

the class VisitorFieldValidatorModelTest method testModelFieldErrorsAddedWithoutFieldPrefixForInterface.

public void testModelFieldErrorsAddedWithoutFieldPrefixForInterface() throws Exception {
    TestBean origBean = action.getBean();
    TestBean2 bean = new TestBean2();
    bean.setBirth(origBean.getBirth());
    bean.setCount(origBean.getCount());
    action.setBean(bean);
    assertTrue(action.getBean() instanceof TestBean2);
    container.getInstance(ActionValidatorManager.class).validate(action, null);
    assertTrue(action.hasFieldErrors());
    Map<String, List<String>> fieldErrors = action.getFieldErrors();
    // the required string validation inherited from the VisitorValidatorTestAction
    assertTrue(fieldErrors.containsKey("context"));
    // the bean validation which is now at the top level because we set the appendPrefix to false
    assertTrue(fieldErrors.containsKey("name"));
    List<String> nameMessages = fieldErrors.get("name");
    assertEquals(1, nameMessages.size());
    String nameMessage = nameMessages.get(0);
    assertEquals("You must enter a name.", nameMessage);
    // should also have picked up validation check for DataAware interface
    assertTrue(fieldErrors.containsKey("data"));
    List<String> dataMessages = fieldErrors.get("data");
    assertEquals(1, dataMessages.size());
    String dataMessage = dataMessages.get(0);
    assertEquals("You must enter a value for data.", dataMessage);
}
Also used : TestBean2(com.opensymphony.xwork2.test.TestBean2)

Example 3 with TestBean

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

the class VisitorFieldValidatorTest method testArrayValidation.

public void testArrayValidation() throws Exception {
    TestBean[] beanArray = action.getTestBeanArray();
    TestBean testBean = beanArray[0];
    testBean.setName("foo");
    validate("validateArray");
    assertTrue(action.hasFieldErrors());
    Map<String, List<String>> fieldErrors = action.getFieldErrors();
    // 4 errors for the array, one for context
    assertEquals(5, fieldErrors.size());
    assertTrue(fieldErrors.containsKey("testBeanArray[1].name"));
    // the error from the action should be there too
    assertTrue(fieldErrors.containsKey("context"));
    List<String> errors = fieldErrors.get("testBeanArray[1].name");
    assertEquals(1, errors.size());
    errors = fieldErrors.get("testBeanArray[2].name");
    assertEquals(1, errors.size());
    errors = fieldErrors.get("testBeanArray[3].name");
    assertEquals(1, errors.size());
    errors = fieldErrors.get("testBeanArray[4].name");
    assertEquals(1, errors.size());
}
Also used : TestBean(com.opensymphony.xwork2.TestBean) List(java.util.List)

Example 4 with TestBean

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

the class SpringObjectFactoryTest method testSetAutowireStrategy.

public void testSetAutowireStrategy() throws Exception {
    assertEquals(objectFactory.getAutowireStrategy(), AutowireCapableBeanFactory.AUTOWIRE_BY_NAME);
    objectFactory.setAutowireStrategy(AutowireCapableBeanFactory.AUTOWIRE_BY_TYPE);
    sac.getBeanFactory().registerSingleton("bean", new TestBean());
    TestBean bean = (TestBean) sac.getBean("bean");
    sac.registerPrototype("simple-action", SimpleAction.class, new MutablePropertyValues());
    ActionConfig actionConfig = new ActionConfig.Builder("jim", "bob", "simple-action").build();
    SimpleAction simpleAction = (SimpleAction) objectFactory.buildBean(actionConfig.getClassName(), null);
    objectFactory.autoWireBean(simpleAction);
    assertEquals(simpleAction.getBean(), bean);
}
Also used : ActionConfig(com.opensymphony.xwork2.config.entities.ActionConfig) TestBean(com.opensymphony.xwork2.TestBean) MutablePropertyValues(org.springframework.beans.MutablePropertyValues) SimpleAction(com.opensymphony.xwork2.SimpleAction)

Example 5 with TestBean

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

the class ActionAutowiringInterceptorTest method testShouldAutowireAction.

public void testShouldAutowireAction() throws Exception {
    StaticWebApplicationContext context = new StaticWebApplicationContext();
    context.getBeanFactory().registerSingleton("bean", new TestBean());
    TestBean bean = (TestBean) context.getBean("bean");
    loadSpringApplicationContextIntoApplication(context);
    SimpleAction action = new SimpleAction();
    ActionInvocation invocation = new TestActionInvocation(action);
    ActionAutowiringInterceptor interceptor = new ActionAutowiringInterceptor();
    interceptor.setApplicationContext(context);
    interceptor.init();
    interceptor.intercept(invocation);
    assertEquals(bean, action.getBean());
}
Also used : TestBean(com.opensymphony.xwork2.TestBean) ActionInvocation(com.opensymphony.xwork2.ActionInvocation) StaticWebApplicationContext(org.springframework.web.context.support.StaticWebApplicationContext) SimpleAction(com.opensymphony.xwork2.SimpleAction)

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