commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From craig...@apache.org
Subject cvs commit: jakarta-commons/beanutils/src/test/org/apache/commons/beanutils DynaPropertyUtilsTestCase.java PropertyUtilsTestCase.java TestBean.java
Date Tue, 16 Jul 2002 02:41:08 GMT
craigmcc    2002/07/15 19:41:08

  Modified:    beanutils/src/java/org/apache/commons/beanutils
                        PropertyUtils.java
               beanutils/src/test/org/apache/commons/beanutils
                        DynaPropertyUtilsTestCase.java
                        PropertyUtilsTestCase.java TestBean.java
  Log:
  Support an extension to the property expression syntax that is similar to
  the way the EL in JSTL and JSP 2.0 works:  If property "a" is a Map, interpret
  "a.b" the same as "a(b)".  This is similar to what the JSP/JSTL expression
  does when interpreting "a.b" as "a['b']" if "a" is a Map.
  
  Revision  Changes    Path
  1.28      +28 -36    jakarta-commons/beanutils/src/java/org/apache/commons/beanutils/PropertyUtils.java
  
  Index: PropertyUtils.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/beanutils/src/java/org/apache/commons/beanutils/PropertyUtils.java,v
  retrieving revision 1.27
  retrieving revision 1.28
  diff -u -r1.27 -r1.28
  --- PropertyUtils.java	7 Jul 2002 23:17:59 -0000	1.27
  +++ PropertyUtils.java	16 Jul 2002 02:41:07 -0000	1.28
  @@ -700,16 +700,14 @@
               String next = name.substring(0, period);
               indexOfINDEXED_DELIM = next.indexOf(INDEXED_DELIM);
               indexOfMAPPED_DELIM = next.indexOf(MAPPED_DELIM);
  -            if (indexOfMAPPED_DELIM >= 0 &&
  -                    (indexOfINDEXED_DELIM < 0 ||
  -                    indexOfMAPPED_DELIM < indexOfINDEXED_DELIM)) {
  +            if (bean instanceof Map) {
  +                bean = ((Map) bean).get(next);
  +            } else if (indexOfMAPPED_DELIM >= 0) {
                   bean = getMappedProperty(bean, next);
  +            } else if (indexOfINDEXED_DELIM >= 0) {
  +                bean = getIndexedProperty(bean, next);
               } else {
  -                if (indexOfINDEXED_DELIM >= 0) {
  -                    bean = getIndexedProperty(bean, next);
  -                } else {
  -                    bean = getSimpleProperty(bean, next);
  -                }
  +                bean = getSimpleProperty(bean, next);
               }
               if (bean == null) {
                   throw new IllegalArgumentException
  @@ -722,16 +720,14 @@
           indexOfINDEXED_DELIM = name.indexOf(INDEXED_DELIM);
           indexOfMAPPED_DELIM = name.indexOf(MAPPED_DELIM);
   
  -        if (indexOfMAPPED_DELIM >= 0 &&
  -                (indexOfINDEXED_DELIM < 0 ||
  -                indexOfMAPPED_DELIM < indexOfINDEXED_DELIM)) {
  +        if (bean instanceof Map) {
  +            bean = ((Map) bean).get(name);
  +        } else if (indexOfMAPPED_DELIM >= 0) {
               bean = getMappedProperty(bean, name);
  +        } else if (indexOfINDEXED_DELIM >= 0) {
  +            bean = getIndexedProperty(bean, name);
           } else {
  -            if (indexOfINDEXED_DELIM >= 0) {
  -                bean = getIndexedProperty(bean, name);
  -            } else {
  -                bean = getSimpleProperty(bean, name);
  -            }
  +            bean = getSimpleProperty(bean, name);
           }
           return bean;
   
  @@ -1498,16 +1494,14 @@
               String next = name.substring(0, delim);
               indexOfINDEXED_DELIM = next.indexOf(INDEXED_DELIM);
               indexOfMAPPED_DELIM = next.indexOf(MAPPED_DELIM);
  -            if (indexOfMAPPED_DELIM >= 0 &&
  -                    (indexOfINDEXED_DELIM < 0 ||
  -                    indexOfMAPPED_DELIM < indexOfINDEXED_DELIM)) {
  +            if (bean instanceof Map) {
  +                bean = ((Map) bean).get(next);
  +            } else if (indexOfMAPPED_DELIM >= 0) {
                   bean = getMappedProperty(bean, next);
  +            } else if (indexOfINDEXED_DELIM >= 0) {
  +                bean = getIndexedProperty(bean, next);
               } else {
  -                if (indexOfINDEXED_DELIM >= 0) {
  -                    bean = getIndexedProperty(bean, next);
  -                } else {
  -                    bean = getSimpleProperty(bean, next);
  -                }
  +                bean = getSimpleProperty(bean, next);
               }
               if (bean == null) {
                   throw new IllegalArgumentException
  @@ -1520,16 +1514,14 @@
           indexOfINDEXED_DELIM = name.indexOf(INDEXED_DELIM);
           indexOfMAPPED_DELIM = name.indexOf(MAPPED_DELIM);
   
  -        if (indexOfMAPPED_DELIM >= 0 &&
  -                (indexOfINDEXED_DELIM < 0 ||
  -                indexOfMAPPED_DELIM < indexOfINDEXED_DELIM)) {
  +        if (bean instanceof Map) {
  +            ((Map) bean).put(name, value);
  +        } else if (indexOfMAPPED_DELIM >= 0) {
               setMappedProperty(bean, name, value);
  +        } else if (indexOfINDEXED_DELIM >= 0) {
  +            setIndexedProperty(bean, name, value);
           } else {
  -            if (indexOfINDEXED_DELIM >= 0) {
  -                setIndexedProperty(bean, name, value);
  -            } else {
  -                setSimpleProperty(bean, name, value);
  -            }
  +            setSimpleProperty(bean, name, value);
           }
   
       }
  
  
  
  1.6       +68 -6     jakarta-commons/beanutils/src/test/org/apache/commons/beanutils/DynaPropertyUtilsTestCase.java
  
  Index: DynaPropertyUtilsTestCase.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/beanutils/src/test/org/apache/commons/beanutils/DynaPropertyUtilsTestCase.java,v
  retrieving revision 1.5
  retrieving revision 1.6
  diff -u -r1.5 -r1.6
  --- DynaPropertyUtilsTestCase.java	7 Jul 2002 23:08:42 -0000	1.5
  +++ DynaPropertyUtilsTestCase.java	16 Jul 2002 02:41:07 -0000	1.6
  @@ -169,6 +169,10 @@
           listIndexed.add("String 4");
           bean.set("listIndexed", listIndexed);
           bean.set("longProperty", new Long((long) 321));
  +        HashMap mapProperty = new HashMap();
  +        mapProperty.put("First Key", "First Value");
  +        mapProperty.put("Second Key", "Second Value");
  +        bean.set("mapProperty", mapProperty);
           HashMap mappedProperty = new HashMap();
           mappedProperty.put("First Key", "First Value");
           mappedProperty.put("Second Key", "Second Value");
  @@ -731,7 +735,7 @@
               fail("Finding third value threw " + t);
           }
   
  -        // Use key expression
  +        // Use key expression with parentheses
   
           try {
               value =
  @@ -760,6 +764,35 @@
               fail("Finding third value threw " + t);
           }
   
  +        // Use key expression with dotted syntax
  +
  +        try {
  +            value =
  +                    PropertyUtils.getNestedProperty(bean,
  +                            "mapProperty.First Key");
  +            assertEquals("Can find first value", "First Value", value);
  +        } catch (Throwable t) {
  +            fail("Finding first value threw " + t);
  +        }
  +
  +        try {
  +            value =
  +                    PropertyUtils.getNestedProperty(bean,
  +                            "mapProperty.Second Key");
  +            assertEquals("Can find second value", "Second Value", value);
  +        } catch (Throwable t) {
  +            fail("Finding second value threw " + t);
  +        }
  +
  +        try {
  +            value =
  +                    PropertyUtils.getNestedProperty(bean,
  +                            "mapProperty.Third Key");
  +            assertNull("Can not find third value", value);
  +        } catch (Throwable t) {
  +            fail("Finding third value threw " + t);
  +        }
  +
       }
   
   
  @@ -1832,7 +1865,7 @@
               fail("Finding fourth value threw " + t);
           }
   
  -        // Use key expression
  +        // Use key expression with parentheses
   
           try {
               value =
  @@ -1860,6 +1893,34 @@
               fail("Finding fifth value threw " + t);
           }
   
  +        // Use key expression with dotted expression
  +
  +        try {
  +            value =
  +                    PropertyUtils.getNestedProperty(bean,
  +                            "mapProperty.Sixth Key");
  +            assertNull("Can not find sixth value", value);
  +        } catch (Throwable t) {
  +            fail("Finding fifth value threw " + t);
  +        }
  +
  +        try {
  +            PropertyUtils.setNestedProperty(bean,
  +                    "mapProperty.Sixth Key",
  +                    "Sixth Value");
  +        } catch (Throwable t) {
  +            fail("Setting sixth value threw " + t);
  +        }
  +
  +        try {
  +            value =
  +                    PropertyUtils.getNestedProperty(bean,
  +                            "mapProperty.Sixth Key");
  +            assertEquals("Can find sixth value", "Sixth Value", value);
  +        } catch (Throwable t) {
  +            fail("Finding sixth value threw " + t);
  +        }
  +
       }
   
   
  @@ -2469,6 +2530,7 @@
                               new DynaProperty("intProperty", Integer.TYPE),
                               new DynaProperty("listIndexed", List.class),
                               new DynaProperty("longProperty", Long.TYPE),
  +                            new DynaProperty("mapProperty", Map.class),
                               new DynaProperty("mappedProperty", Map.class),
                               new DynaProperty("mappedIntProperty", Map.class),
                               new DynaProperty("nested", TestBean.class),
  
  
  
  1.18      +63 -6     jakarta-commons/beanutils/src/test/org/apache/commons/beanutils/PropertyUtilsTestCase.java
  
  Index: PropertyUtilsTestCase.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/beanutils/src/test/org/apache/commons/beanutils/PropertyUtilsTestCase.java,v
  retrieving revision 1.17
  retrieving revision 1.18
  diff -u -r1.17 -r1.18
  --- PropertyUtilsTestCase.java	7 Jul 2002 23:08:42 -0000	1.17
  +++ PropertyUtilsTestCase.java	16 Jul 2002 02:41:08 -0000	1.18
  @@ -991,7 +991,7 @@
               fail("Finding third value threw " + t);
           }
   
  -        // Use key expression
  +        // Use key expression with parentheses
   
           try {
               value =
  @@ -1020,6 +1020,35 @@
               fail("Finding third value threw " + t);
           }
   
  +        // Use key expression with dotted syntax
  +
  +        try {
  +            value =
  +                    PropertyUtils.getNestedProperty(bean,
  +                            "mapProperty.First Key");
  +            assertEquals("Can find first value", "First Value", value);
  +        } catch (Throwable t) {
  +            fail("Finding first value threw " + t);
  +        }
  +
  +        try {
  +            value =
  +                    PropertyUtils.getNestedProperty(bean,
  +                            "mapProperty.Second Key");
  +            assertEquals("Can find second value", "Second Value", value);
  +        } catch (Throwable t) {
  +            fail("Finding second value threw " + t);
  +        }
  +
  +        try {
  +            value =
  +                    PropertyUtils.getNestedProperty(bean,
  +                            "mapProperty.Third Key");
  +            assertNull("Can not find third value", value);
  +        } catch (Throwable t) {
  +            fail("Finding third value threw " + t);
  +        }
  +
       }
   
   
  @@ -2352,7 +2381,7 @@
               fail("Finding fourth value threw " + t);
           }
   
  -        // Use key expression
  +        // Use key expression with parentheses
   
           try {
               value =
  @@ -2378,6 +2407,34 @@
               assertEquals("Can find fifth value", "Fifth Value", value);
           } catch (Throwable t) {
               fail("Finding fifth value threw " + t);
  +        }
  +
  +        // Use key expression with dotted expression
  +
  +        try {
  +            value =
  +                    PropertyUtils.getNestedProperty(bean,
  +                            "mapProperty.Sixth Key");
  +            assertNull("Can not find sixth value", value);
  +        } catch (Throwable t) {
  +            fail("Finding fifth value threw " + t);
  +        }
  +
  +        try {
  +            PropertyUtils.setNestedProperty(bean,
  +                    "mapProperty.Sixth Key",
  +                    "Sixth Value");
  +        } catch (Throwable t) {
  +            fail("Setting sixth value threw " + t);
  +        }
  +
  +        try {
  +            value =
  +                    PropertyUtils.getNestedProperty(bean,
  +                            "mapProperty.Sixth Key");
  +            assertEquals("Can find sixth value", "Sixth Value", value);
  +        } catch (Throwable t) {
  +            fail("Finding sixth value threw " + t);
           }
   
       }
  
  
  
  1.9       +31 -4     jakarta-commons/beanutils/src/test/org/apache/commons/beanutils/TestBean.java
  
  Index: TestBean.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/beanutils/src/test/org/apache/commons/beanutils/TestBean.java,v
  retrieving revision 1.8
  retrieving revision 1.9
  diff -u -r1.8 -r1.9
  --- TestBean.java	23 Jan 2002 22:35:59 -0000	1.8
  +++ TestBean.java	16 Jul 2002 02:41:08 -0000	1.9
  @@ -66,6 +66,7 @@
   import java.util.ArrayList;
   import java.util.HashMap;
   import java.util.List;
  +import java.util.Map;
   
   
   /**
  @@ -208,6 +209,32 @@
   
       public void setLongProperty(long longProperty) {
           this.longProperty = longProperty;
  +    }
  +
  +
  +    /**
  +     * A mapped property with only a getter and setter for a Map.
  +     */
  +    private Map mapProperty = null;
  +
  +    public Map getMapProperty() {
  +        // Create the map the very first time
  +        if (mapProperty == null) {
  +            mapProperty = new HashMap();
  +            mapProperty.put("First Key", "First Value");
  +            mapProperty.put("Second Key", "Second Value");
  +        }
  +        return (mapProperty);
  +    }
  +
  +    public void setMapProperty(Map mapProperty) {
  +        // Create the map the very first time
  +        if (mapProperty == null) {
  +            mapProperty = new HashMap();
  +            mapProperty.put("First Key", "First Value");
  +            mapProperty.put("Second Key", "Second Value");
  +        }
  +        this.mapProperty = mapProperty;
       }
   
   
  
  
  

--
To unsubscribe, e-mail:   <mailto:commons-dev-unsubscribe@jakarta.apache.org>
For additional commands, e-mail: <mailto:commons-dev-help@jakarta.apache.org>


Mime
View raw message