diff --git a/integration/src/test/java/org/apache/struts2/s1/DynaBeanPropertyAccessorTest.java b/integration/src/test/java/org/apache/struts2/s1/DynaBeanPropertyAccessorTest.java new file mode 100644 index 000000000..767bcb629 --- /dev/null +++ b/integration/src/test/java/org/apache/struts2/s1/DynaBeanPropertyAccessorTest.java @@ -0,0 +1,134 @@ +package org.apache.struts2.legacy; + +import junit.framework.*; +import java.io.*; +import java.util.*; +import org.apache.commons.beanutils.*; + +import ognl.*; + +/** Description of the Class */ +public class DynaBeanPropertyAccessorTest extends TestCase { + + protected DynaBean bean = null; + + public DynaBeanPropertyAccessorTest(String name) throws Exception { + super(name); + } + + + public static void main(String args[]) { + junit.textui.TestRunner.run(DynaBeanPropertyAccessorTest.class); + } + + /** + * Set up instance variables required by this test case. + */ + public void setUp() throws Exception { + + // Instantiate a new DynaBean instance + DynaClass dynaClass = createDynaClass(); + bean = dynaClass.newInstance(); + + // Initialize the DynaBean's property values (like TestBean) + bean.set("booleanProperty", new Boolean(true)); + bean.set("booleanSecond", new Boolean(true)); + bean.set("doubleProperty", new Double(321.0)); + bean.set("floatProperty", new Float((float) 123.0)); + int intArray[] = { 0, 10, 20, 30, 40 }; + bean.set("intArray", intArray); + int intIndexed[] = { 0, 10, 20, 30, 40 }; + bean.set("intIndexed", intIndexed); + bean.set("intProperty", new Integer(123)); + List listIndexed = new ArrayList(); + listIndexed.add("String 0"); + listIndexed.add("String 1"); + listIndexed.add("String 2"); + listIndexed.add("String 3"); + listIndexed.add("String 4"); + bean.set("listIndexed", listIndexed); + bean.set("longProperty", new Long((long) 321)); + HashMap mappedProperty = new HashMap(); + mappedProperty.put("First Key", "First Value"); + mappedProperty.put("Second Key", "Second Value"); + bean.set("mappedProperty", mappedProperty); + HashMap mappedIntProperty = new HashMap(); + mappedIntProperty.put("One", new Integer(1)); + mappedIntProperty.put("Two", new Integer(2)); + bean.set("mappedIntProperty", mappedIntProperty); + // Property "nullProperty" is not initialized, so it should return null + bean.set("shortProperty", new Short((short) 987)); + String stringArray[] = + { "String 0", "String 1", "String 2", "String 3", "String 4" }; + bean.set("stringArray", stringArray); + String stringIndexed[] = + { "String 0", "String 1", "String 2", "String 3", "String 4" }; + bean.set("stringIndexed", stringIndexed); + bean.set("stringProperty", "This is a string"); + + } + + + + + public void testGetProperty() throws Exception { + + DynaBeanPropertyAccessor trans = new DynaBeanPropertyAccessor(); + assertTrue("This is a string".equals(trans.getProperty(null, bean, "stringProperty"))); + assertTrue(trans.getProperty(null, bean, "listIndexed") instanceof List); + + } + + public void testSetProperty() throws Exception { + + DynaBeanPropertyAccessor trans = new DynaBeanPropertyAccessor(); + trans.setProperty(null, bean, "stringProperty", "bob"); + assertTrue("bob".equals(trans.getProperty(null, bean, "stringProperty"))); + + } + + public void testOGNL() throws Exception { + + OgnlRuntime.setPropertyAccessor(DynaBean.class, new DynaBeanPropertyAccessor()); + + assertTrue("This is a string".equals(Ognl.getValue("stringProperty", bean))); + + } + + + /** + * Create and return a DynaClass instance for our test + * DynaBean. + */ + protected DynaClass createDynaClass() { + + int intArray[] = new int[0]; + String stringArray[] = new String[0]; + + DynaClass dynaClass = new BasicDynaClass + ("TestDynaClass", null, + new DynaProperty[]{ + new DynaProperty("booleanProperty", Boolean.TYPE), + new DynaProperty("booleanSecond", Boolean.TYPE), + new DynaProperty("doubleProperty", Double.TYPE), + new DynaProperty("floatProperty", Float.TYPE), + new DynaProperty("intArray", intArray.getClass()), + new DynaProperty("intIndexed", intArray.getClass()), + new DynaProperty("intProperty", Integer.TYPE), + new DynaProperty("listIndexed", List.class), + new DynaProperty("longProperty", Long.TYPE), + new DynaProperty("mappedProperty", Map.class), + new DynaProperty("mappedIntProperty", Map.class), + new DynaProperty("nullProperty", String.class), + new DynaProperty("shortProperty", Short.TYPE), + new DynaProperty("stringArray", stringArray.getClass()), + new DynaProperty("stringIndexed", stringArray.getClass()), + new DynaProperty("stringProperty", String.class), + }); + return (dynaClass); + + } + + +} + diff --git a/integration/src/test/java/org/apache/struts2/s1/StrutsFactoryTest.java b/integration/src/test/java/org/apache/struts2/s1/StrutsFactoryTest.java new file mode 100644 index 000000000..05498fc62 --- /dev/null +++ b/integration/src/test/java/org/apache/struts2/s1/StrutsFactoryTest.java @@ -0,0 +1,235 @@ +package org.apache.struts2.legacy; + +import java.lang.reflect.InvocationTargetException; + +import junit.framework.TestCase; + +import org.apache.struts.action.ActionErrors; +import org.apache.struts.action.ActionForward; +import org.apache.struts.action.ActionMapping; +import org.apache.struts.action.ActionMessage; +import org.apache.struts.config.ActionConfig; +import org.apache.struts.config.ExceptionConfig; +import org.apache.struts.config.ForwardConfig; +import org.apache.struts.config.ModuleConfig; +import org.apache.struts2.config.StrutsXmlConfigurationProvider; + +import com.opensymphony.xwork2.ActionSupport; +import com.opensymphony.xwork2.config.Configuration; +import com.opensymphony.xwork2.config.ConfigurationManager; +import com.opensymphony.xwork2.config.ConfigurationProvider; +import com.opensymphony.xwork2.config.entities.ExceptionMappingConfig; +import com.opensymphony.xwork2.config.entities.PackageConfig; +import com.opensymphony.xwork2.config.entities.ResultConfig; + +/** + * Test of StrutsFactory, which creates Struts 1.x wrappers around XWork config objects. + */ +public class StrutsFactoryTest extends TestCase { + + private static final String PACKAGE_NAME = "org/apache/struts2/legacy"; + + protected StrutsFactory factory = null; + protected Configuration config; + + public StrutsFactoryTest(String name) throws Exception { + super(name); + } + + + public static void main(String args[]) { + junit.textui.TestRunner.run(StrutsFactoryTest.class); + } + + /** + * Set up instance variables required by this test case. + */ + public void setUp() { + ConfigurationManager manager = new ConfigurationManager(); + ConfigurationProvider provider = new StrutsXmlConfigurationProvider(PACKAGE_NAME + "/test-struts-factory.xml", true); + manager.addConfigurationProvider(provider); + config = manager.getConfiguration(); + factory = new StrutsFactory(config); + } + + /** + * Test the creation of a Struts 1.x ModuleConfig wrapper around an XWork PackageConfig. + * The PackageConfig is loaded from test-struts-factory.xml. + */ + public void testCreateModuleConfig() { + ModuleConfig moduleConfig = factory.createModuleConfig(PACKAGE_NAME); + assertNotNull(moduleConfig); + + assertEquals("/"+PACKAGE_NAME, moduleConfig.getPrefix()); + + ActionConfig actionConfig = moduleConfig.findActionConfig("/action1"); + assertNotNull(actionConfig); + assertEquals("/action1", actionConfig.getPath()); + + ActionConfig[] actionConfigs = moduleConfig.findActionConfigs(); + assertNotNull(actionConfigs); + assertEquals(2, actionConfigs.length); + + ExceptionConfig exceptionConfig = moduleConfig.findExceptionConfig(Exception.class.getName()); + assertNotNull(exceptionConfig); + assertEquals(Exception.class.getName(), exceptionConfig.getType()); + + ExceptionConfig[] exceptionConfigs = moduleConfig.findExceptionConfigs(); + assertNotNull(exceptionConfigs); + assertEquals(1, exceptionConfigs.length); + + ForwardConfig fwdConfig = moduleConfig.findForwardConfig("globalResult"); + assertNotNull(fwdConfig); + assertEquals("globalResult", fwdConfig.getName()); + + // These methods are currently not implemented -- replace as functionality is added. + assertNYI(moduleConfig, "getControllerConfig", null); + assertNYI(moduleConfig, "getActionFormBeanClass", null); + assertNYI(moduleConfig, "getActionMappingClass", null); + assertNYI(moduleConfig, "getActionForwardClass", null); + assertNYI(moduleConfig, "findException", Class.class); + assertNYI(moduleConfig, "findFormBeanConfig", String.class); + assertNYI(moduleConfig, "findFormBeanConfigs", null); + assertNYI(moduleConfig, "findMessageResourcesConfig", String.class); + assertNYI(moduleConfig, "findMessageResourcesConfigs", null); + assertNYI(moduleConfig, "findPlugInConfigs", null); + } + + /** + * Test the creation of a Struts 1.x ActionMapping wrapper around an XWork ActionConfig. + * The ActionConfig is loaded from test-struts-factory.xml. + */ + public void testCreateActionMapping() { + PackageConfig packageConfig = config.getPackageConfig(PACKAGE_NAME); + com.opensymphony.xwork2.config.entities.ActionConfig actionConfig = + (com.opensymphony.xwork2.config.entities.ActionConfig) packageConfig.getActionConfigs().get("action1"); + ActionMapping mapping = factory.createActionMapping(actionConfig); + assertNotNull(mapping); + + assertNotNull(mapping.findForward("result1")); + assertNotNull(mapping.findForwardConfig("result2")); + + ForwardConfig[] configs = mapping.findForwardConfigs(); + assertNotNull(configs); + assertEquals(2, configs.length); + + String[] forwards = mapping.findForwards(); + assertNotNull(forwards); + assertEquals(2, forwards.length); + + ActionForward fwd = mapping.findForward("result1"); + assertNotNull(fwd); + assertEquals("result1", fwd.getName()); + + assertNotNull(mapping.findException(NullPointerException.class)); + assertNotNull(mapping.findExceptionConfig("java.lang.IllegalStateException")); + + ExceptionConfig[] exceptionConfigs = mapping.findExceptionConfigs(); + assertNotNull(exceptionConfigs); + assertEquals(3, exceptionConfigs.length); + + ModuleConfig moduleConfig = mapping.getModuleConfig(); + assertNotNull(moduleConfig); + + // For now, the path will be null if the ActionMapping was created on its own (as opposed to from a + // WrapperModuleConfig, which knows the path). + assertNull(mapping.getPath()); + + // These methods are currently not implemented -- replace as functionality is added. + assertNYI(mapping, "getInputForward", null); + assertNYI(mapping, "getAttribute", null); + assertNYI(mapping, "getForward", null); + assertNYI(mapping, "getInclude", null); + assertNYI(mapping, "getInput", null); + assertNYI(mapping, "getMultipartClass", null); + assertNYI(mapping, "getName", null); + assertNYI(mapping, "getParameter", null); + assertNYI(mapping, "getPrefix", null); + assertNYI(mapping, "getRoles", null); + assertNYI(mapping, "getRoleNames", null); + assertNYI(mapping, "getScope", null); + assertNYI(mapping, "getSuffix", null); + assertNYI(mapping, "getType", null); + assertNYI(mapping, "getUnknown", null); + assertNYI(mapping, "getValidate", null); + } + + /** + * Test the creation of a Struts 1.x ActionForward wrapper around an XWork ResultConfig. + * The ResultConfig is loaded from test-struts-factory.xml. + */ + public void testCreateActionForward() { + PackageConfig packageConfig = config.getPackageConfig(PACKAGE_NAME); + ResultConfig resultConfig = (ResultConfig) packageConfig.getGlobalResultConfigs().get("globalResult"); + ActionForward fwd = factory.createActionForward(resultConfig); + assertNotNull(fwd); + assertEquals("globalResult", fwd.getName()); + + // These methods are currently not implemented -- replace as functionality is added. + assertNYI(fwd, "getPath", null); + assertNYI(fwd, "getModule", null); + assertNYI(fwd, "getRedirect", null); + } + + /** + * Test the creation of a Struts 1.x ExceptionConfig wrapper around an XWork ExceptionHandlerConfig. + * The ExceptionConfig is loaded from test-struts-factory.xml. + */ + public void testCreateExceptionConfig() { + PackageConfig packageConfig = config.getPackageConfig(PACKAGE_NAME); + ExceptionMappingConfig cfg = (ExceptionMappingConfig) packageConfig.getGlobalExceptionMappingConfigs().get(0); + ExceptionConfig exceptionConfig = factory.createExceptionConfig(cfg); + assertNotNull(exceptionConfig); + assertEquals(Exception.class.getName(), exceptionConfig.getType()); + + assertNYI(exceptionConfig, "getBundle", null); + assertNYI(exceptionConfig, "getHandler", null); + assertNYI(exceptionConfig, "getKey", null); + assertNYI(exceptionConfig, "getPath", null); + assertNYI(exceptionConfig, "getScope", null); + } + + public void testConvertErrors() throws Exception { + + ActionMessage err1 = new ActionMessage("error1"); + ActionMessage err2 = new ActionMessage("error2", new Integer(1)); + ActionErrors errors = new ActionErrors(); + errors.add(errors.GLOBAL_MESSAGE, err1); + errors.add("foo", err2); + + ActionSupport action = new ActionSupport(); + factory.convertErrors(errors, action); + + assertTrue(1 == action.getActionErrors().size()); + assertTrue(1 == action.getFieldErrors().size()); + } + + /** + * Assert that the given method throws UnsupportedOperationException. + */ + private void assertNYI(Object o, String methodName, Class argType) { + try { + Class[] argTypes = argType != null ? new Class[]{argType} : null; + + Object[] args = null; + if (argType != null) { + if (Class.class == argType) { + args = new Object[]{argType}; + } else { + args = new Object[]{argType.newInstance()}; + } + } + o.getClass().getMethod(methodName, argTypes).invoke(o, args); + } catch (InvocationTargetException e) { + Throwable cause = e.getCause(); + assertEquals(cause.getMessage(), UnsupportedOperationException.class, cause.getClass()); + + // OK -- it's what we expected + return; + } catch (Exception e) { + fail(e.getClass().getName() + ": " + e.getMessage()); + } + + fail("Expected UnsupportedOperationException for " + methodName + "() on " + o.getClass().getName()); + } +}