Search in sources :

Example 11 with TestBean

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

the class SpringObjectFactoryTest method testShouldAutowireObjectsObtainedFromTheObjectFactoryByClass.

public void testShouldAutowireObjectsObtainedFromTheObjectFactoryByClass() throws Exception {
    sac.getBeanFactory().registerSingleton("bean", new TestBean());
    TestBean bean = (TestBean) sac.getBean("bean");
    SimpleAction action = (SimpleAction) objectFactory.buildBean(SimpleAction.class, null);
    assertEquals(bean, action.getBean());
}
Also used : TestBean(com.opensymphony.xwork2.TestBean) SimpleAction(com.opensymphony.xwork2.SimpleAction)

Example 12 with TestBean

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

the class ActionAutowiringInterceptorTest method testIfApplicationContextIsNullThenBeanWillNotBeWiredUp.

public void testIfApplicationContextIsNullThenBeanWillNotBeWiredUp() throws Exception {
    ActionContext.of(new HashMap<>()).withApplication(new HashMap<>()).bind();
    ActionAutowiringInterceptor interceptor = new ActionAutowiringInterceptor();
    interceptor.init();
    SimpleAction action = new SimpleAction();
    ActionInvocation invocation = new TestActionInvocation(action);
    TestBean bean = action.getBean();
    // If an exception is thrown here, things are going to go wrong in
    // production
    interceptor.intercept(invocation);
    assertEquals(bean, action.getBean());
}
Also used : HashMap(java.util.HashMap) TestBean(com.opensymphony.xwork2.TestBean) ActionInvocation(com.opensymphony.xwork2.ActionInvocation) SimpleAction(com.opensymphony.xwork2.SimpleAction)

Example 13 with TestBean

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

the class DefaultActionValidatorManagerTest method testBuildsValidatorsForAliasError.

public void testBuildsValidatorsForAliasError() {
    boolean pass = false;
    try {
        mockValidatorFileParser.expectAndReturn("parseActionValidatorConfigs", C.args(C.IS_NOT_NULL, C.IS_NOT_NULL, C.eq("com/opensymphony/xwork2/TestBean-validation.xml")), new ArrayList());
        mockValidatorFileParser.expectAndThrow("parseActionValidatorConfigs", C.args(C.IS_NOT_NULL, C.IS_NOT_NULL, C.eq("com/opensymphony/xwork2/TestBean-badtest-validation.xml")), new ConfigurationException());
        List validatorList = actionValidatorManager.getValidators(TestBean.class, "badtest");
    } catch (StrutsException ex) {
        pass = true;
    }
    mockValidatorFileParser.verify();
    assertTrue("Didn't throw exception on load failure", pass);
}
Also used : StrutsException(org.apache.struts2.StrutsException) ConfigurationException(com.opensymphony.xwork2.config.ConfigurationException) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List)

Example 14 with TestBean

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

the class VisitorFieldValidatorTest method testCollectionValidation.

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

Example 15 with TestBean

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

the class VisitorFieldValidatorTest method setUp.

@Override
public void setUp() throws Exception {
    super.setUp();
    // Force US Locale for date conversion tests on JDK9+
    ActionContext.getContext().withLocale(Locale.US);
    action = container.inject(VisitorValidatorTestAction.class);
    TestBean bean = action.getBean();
    Calendar cal = new GregorianCalendar(1900, Calendar.FEBRUARY, 1);
    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(action).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) ActionProxy(com.opensymphony.xwork2.ActionProxy) TestBean(com.opensymphony.xwork2.TestBean) ActionInvocation(com.opensymphony.xwork2.ActionInvocation) GregorianCalendar(java.util.GregorianCalendar) Calendar(java.util.Calendar) GregorianCalendar(java.util.GregorianCalendar)

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