Commits

Anonymous committed d41c6ab

Adding testcase for indexed list and map access
Issue number:
Obtained from:
Submitted by:
Reviewed by:

git-svn-id: http://svn.opensymphony.com/svn/xwork/trunk@779e221344d-f017-0410-9bd5-d282ab1896d7

Comments (0)

Files changed (5)

src/test/com/opensymphony/xwork/util/MyBean.java

+/*
+ * Copyright (c) 2002-2006 by OpenSymphony
+ * All rights reserved.
+ */
+package com.opensymphony.xwork.util;
+
+import java.io.Serializable;
+
+/**
+ * <code>MyBean</code>
+ *
+ * @author Rainer Hermanns
+ */
+public class MyBean implements Serializable {
+
+    private Long id;
+    private String name;
+
+    public Long getId() {
+        return id;
+    }
+
+    public void setId(Long id) {
+        this.id = id;
+    }
+
+    public String getName() {
+        return name;
+    }
+
+    public void setName(String name) {
+        this.name = name;
+    }
+
+
+    public String toString() {
+        return "MyBean{" +
+                "id=" + id +
+                ", name='" + name + '\'' +
+                '}';
+    }
+}

src/test/com/opensymphony/xwork/util/MyBeanAction-conversion.properties

+KeyProperty_beanList=id
+Element_beanList=com.opensymphony.xwork.util.MyBean
+CreateIfNull_beanList=true
+
+Key_beanMap=java.lang.Long
+KeyProperty_beanMap=id
+Element_beanMap=com.opensymphony.xwork.util.MyBean
+

src/test/com/opensymphony/xwork/util/MyBeanAction.java

+/*
+ * Copyright (c) 2002-2006 by OpenSymphony
+ * All rights reserved.
+ */
+package com.opensymphony.xwork.util;
+
+import com.opensymphony.xwork.Action;
+
+import java.util.List;
+import java.util.ArrayList;
+import java.util.Map;
+import java.util.HashMap;
+
+/**
+ * <code>MyBeanAction</code>
+ *
+ * @author Rainer Hermanns
+ */
+public class MyBeanAction implements Action {
+
+    private List beanList = new ArrayList();
+    private Map beanMap = new HashMap();
+
+    public List getBeanList() {
+        return beanList;
+    }
+
+    public void setBeanList(List beanList) {
+        this.beanList = beanList;
+    }
+
+    public Map getBeanMap() {
+        return beanMap;
+    }
+
+    public void setBeanMap(Map beanMap) {
+        this.beanMap = beanMap;
+    }
+
+    public String execute() throws Exception {
+        return SUCCESS;
+    }
+
+}

src/test/com/opensymphony/xwork/util/MyBeanActionTest.java

+/*
+ * Copyright (c) 2002-2006 by OpenSymphony
+ * All rights reserved.
+ */
+package com.opensymphony.xwork.util;
+
+import com.opensymphony.xwork.XWorkTestCase;
+import com.opensymphony.xwork.ActionContext;
+import com.opensymphony.xwork.ActionProxy;
+import com.opensymphony.xwork.ActionProxyFactory;
+import com.opensymphony.xwork.config.ConfigurationManager;
+
+import java.util.HashMap;
+import java.util.Map;
+
+/**
+ * <code>MyBeanActionTest</code>
+ *
+ * @author Rainer Hermanns
+ */
+public class MyBeanActionTest extends XWorkTestCase {
+
+    public void testIndexedList() {
+        HashMap params = new HashMap();
+        params.put("beanList(1234567890).name", "This is the bla bean");
+        params.put("beanList(1234567891).name", "This is the 2nd bla bean");
+
+        HashMap extraContext = new HashMap();
+        extraContext.put(ActionContext.PARAMETERS, params);
+
+        try {
+            ActionProxy proxy = ActionProxyFactory.getFactory().createActionProxy("", "MyBean", extraContext);
+            proxy.execute();
+            assertEquals(2, Integer.parseInt(proxy.getInvocation().getStack().findValue("beanList.size").toString()));
+            assertEquals(MyBean.class.getName(), proxy.getInvocation().getStack().findValue("beanList.get(0)").getClass().getName());
+            assertEquals(MyBean.class.getName(), proxy.getInvocation().getStack().findValue("beanList.get(1)").getClass().getName());
+
+            assertEquals("This is the bla bean", proxy.getInvocation().getStack().findValue("beanList.get(0).name"));
+            assertEquals(new Long(1234567890), Long.valueOf(proxy.getInvocation().getStack().findValue("beanList.get(0).id").toString()));
+            assertEquals("This is the 2nd bla bean", proxy.getInvocation().getStack().findValue("beanList.get(1).name"));
+            assertEquals(new Long(1234567891), Long.valueOf(proxy.getInvocation().getStack().findValue("beanList.get(1).id").toString()));
+        } catch (Exception e) {
+            e.printStackTrace();
+            fail();
+        }
+    }
+
+    public void testIndexedMap() {
+        HashMap params = new HashMap();
+        params.put("beanMap[1234567890].id", "1234567890");
+        params.put("beanMap[1234567891].id", "1234567891");
+
+        params.put("beanMap[1234567890].name", "This is the bla bean");
+        params.put("beanMap[1234567891].name", "This is the 2nd bla bean");
+
+        HashMap extraContext = new HashMap();
+        extraContext.put(ActionContext.PARAMETERS, params);
+
+        try {
+            ActionProxy proxy = ActionProxyFactory.getFactory().createActionProxy("", "MyBean", extraContext);
+            proxy.execute();
+            MyBeanAction action = (MyBeanAction) proxy.getInvocation().getAction();
+
+            assertEquals(2, Integer.parseInt(proxy.getInvocation().getStack().findValue("beanMap.size").toString()));
+
+            Map map = (Map) proxy.getInvocation().getStack().findValue("beanMap");
+            assertEquals(true, action.getBeanMap().containsKey(new Long(1234567890)));
+            assertEquals(true, action.getBeanMap().containsKey(new Long(1234567891)));
+
+
+            assertEquals(MyBean.class.getName(), proxy.getInvocation().getStack().findValue("beanMap.get(1234567890L)").getClass().getName());
+            assertEquals(MyBean.class.getName(), proxy.getInvocation().getStack().findValue("beanMap.get(1234567891L)").getClass().getName());
+
+            assertEquals("This is the bla bean", proxy.getInvocation().getStack().findValue("beanMap.get(1234567890L).name"));
+            assertEquals("This is the 2nd bla bean", proxy.getInvocation().getStack().findValue("beanMap.get(1234567891L).name"));
+
+            assertEquals("1234567890", proxy.getInvocation().getStack().findValue("beanMap.get(1234567890L).id").toString());
+            assertEquals("1234567891", proxy.getInvocation().getStack().findValue("beanMap.get(1234567891L).id").toString());
+        } catch (Exception e) {
+            e.printStackTrace();
+            fail();
+        }
+    }
+
+    protected void setUp() throws Exception {
+        super.setUp();
+
+        // ensure we're using the default configuration, not simple config
+        ConfigurationManager.clearConfigurationProviders();
+        ConfigurationManager.getConfiguration().reload();
+    }
+}

src/test/xwork.xml

             <param name="bar">23</param>
         </action>
 
+        <action name="MyBean" class="com.opensymphony.xwork.util.MyBeanAction">
+            <interceptor-ref name="debugStack"/>
+            <interceptor-ref name="defaultStack"/>
+        </action>
+
         <action name="TestInterceptorParam" class="com.opensymphony.xwork.SimpleAction">
             <interceptor-ref name="test">
                 <param name="expectedFoo">expectedFoo</param>