1. opensymphony
  2. xwork

Commits

gjz22  committed 9caa33e

Issue number:
Obtained from:
Submitted by:
Reviewed by:
CVS: ----------------------------------------------------------------------
CVS: Issue number: XW-309

Fixes List functionality using KeyProperty and modifies the test cases to reflect that.

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

  • Participants
  • Parent commits 92e1846
  • Branches master

Comments (0)

Files changed (5)

File src/java/com/opensymphony/xwork/util/OgnlValueStack.java

View file
         OgnlRuntime.setPropertyAccessor(Enumeration.class, new XWorkEnumerationAcccessor());
         OgnlRuntime.setPropertyAccessor(List.class, new XWorkListPropertyAccessor());
         OgnlRuntime.setPropertyAccessor(Map.class, new XWorkMapPropertyAccessor());
+        OgnlRuntime.setPropertyAccessor(Collection.class, new XWorkCollectionPropertyAccessor());
         OgnlRuntime.setPropertyAccessor(Set.class, new XWorkCollectionPropertyAccessor());
         OgnlRuntime.setPropertyAccessor(ObjectProxy.class, new ObjectProxyPropertyAccessor());
         OgnlRuntime.setMethodAccessor(Object.class, new XWorkMethodAccessor());

File src/java/com/opensymphony/xwork/util/XWorkMethodAccessor.java

View file
 import ognl.OgnlContext;
 import ognl.OgnlException;
 import ognl.OgnlRuntime;
+import ognl.PropertyAccessor;
 
 import java.beans.PropertyDescriptor;
 import java.util.Collection;
                   	    //thereafter
 
                   	    Object propVal=OgnlRuntime.getProperty(ogContext, object, string);
-                  	    return OgnlRuntime.getProperty(ogContext,propVal,objects[0]);
+                  	    //use the Collection property accessor instead of the individual property accessor, because 
+                  	    //in the case of Lists otherwise the index property could be used
+                  	    PropertyAccessor accessor=OgnlRuntime.getPropertyAccessor(Collection.class);
+                  	    
+                  	    return accessor.getProperty(ogContext,propVal,objects[0]);
                   	}
               }
             }	catch (Exception oe) {
                 //this exception should theoretically never happen
                 //log it
+                oe.printStackTrace();
             }
 
         }

File src/test/com/opensymphony/xwork/util/Foo-conversion.properties

View file
 Element_catMap=com.opensymphony.xwork.util.Cat
 Key_anotherCatMap=java.lang.Long
 Element_anotherCatMap=com.opensymphony.xwork.util.Cat
-KeyProperty_barSet=id
-Element_barSet=com.opensymphony.xwork.util.Bar
+KeyProperty_barCollection=id
+Element_barCollection=com.opensymphony.xwork.util.Bar
 KeyProperty_barList=id
 Element_barList=com.opensymphony.xwork.util.Bar
 

File src/test/com/opensymphony/xwork/util/Foo.java

View file
  */
 package com.opensymphony.xwork.util;
 
+import java.util.Collection;
 import java.util.Date;
 import java.util.List;
 import java.util.Map;
     List cats;
     List moreCats;
     List strings;
-    Set barSet;
+    Collection barCollection;
     Map catMap;
     Map anotherCatMap;
     String title;
     /**
      * @return Returns the catSet.
      */
-    public Set getBarSet() {
-        return barSet;
+    public Collection getBarCollection() {
+        return barCollection;
     }
 
     /**
      * @param catSet The catSet to set.
      */
-    public void setBarSet(Set catSet) {
-        this.barSet = catSet;
+    public void setBarCollection(Collection barCollection) {
+        this.barCollection = barCollection;
     }
 
     public void setPoints(long[] points) {

File src/test/com/opensymphony/xwork/util/SetPropertiesTest.java

View file
 
     }
 
-    public void testAddingAndModifyingSetsWithObjects() {
+    
+    public void testAddingAndModifyingCollectionWithObjects() {
+        doTestAddingAndModifyingCollectionWithObjects(new HashSet());
+        doTestAddingAndModifyingCollectionWithObjects(new ArrayList());
+        
+    }
+    public void doTestAddingAndModifyingCollectionWithObjects(Collection barColl) {
         OgnlValueStack vs = new OgnlValueStack();
         Foo foo = new Foo();
-        HashSet barSet = new HashSet();
-        foo.setBarSet(barSet);
+        
+        foo.setBarCollection(barColl);
         Bar bar1 = new Bar();
         bar1.setId(new Long(11));
-        barSet.add(bar1);
+        barColl.add(bar1);
         Bar bar2 = new Bar();
         bar2.setId(new Long(22));
-        barSet.add(bar2);
+        barColl.add(bar2);
         //try modifying bar1 and bar2
         //check the logs here to make sure
         //the Map is being created
         vs.push(foo);
         String bar1Title = "The Phantom Menace";
         String bar2Title = "The Clone Wars";
-        vs.setValue("barSet(22).title", bar2Title);
-        vs.setValue("barSet(11).title", bar1Title);
-        Iterator barSetIter = barSet.iterator();
+        vs.setValue("barCollection(22).title", bar2Title);
+        vs.setValue("barCollection(11).title", bar1Title);
+        Iterator barSetIter = barColl.iterator();
         while (barSetIter.hasNext()) {
             Bar next = (Bar) barSetIter.next();
             if (next.getId().intValue() == 22) {
         //now test adding
         String bar3Title = "Revenge of the Sith";
         String bar4Title = "A New Hope";
-        vs.setValue("barSet.makeNew[4].title", bar4Title, true);
-        vs.setValue("barSet.makeNew[0].title", bar3Title, true);
-        assertEquals(4, barSet.size());
-        barSetIter = barSet.iterator();
+        vs.setValue("barCollection.makeNew[4].title", bar4Title, true);
+        vs.setValue("barCollection.makeNew[0].title", bar3Title, true);
+        assertEquals(4, barColl.size());
+        barSetIter = barColl.iterator();
 
         while (barSetIter.hasNext()) {
             Bar next = (Bar) barSetIter.next();