Package org.springframework.beans

Examples of org.springframework.beans.BeanWrapper


    assertEquals("prefixname0", bw.getPropertyValue("map['key2'].name"));
  }

  public void testIndexedPropertiesWithCustomEditorForProperty() {
    IndexedTestBean bean = new IndexedTestBean(false);
    BeanWrapper bw = new BeanWrapperImpl(bean);
    bw.registerCustomEditor(String.class, "array.name", new PropertyEditorSupport() {
      public void setAsText(String text) throws IllegalArgumentException {
        setValue("array" + text);
      }
    });
    bw.registerCustomEditor(String.class, "list.name", new PropertyEditorSupport() {
      public void setAsText(String text) throws IllegalArgumentException {
        setValue("list" + text);
      }
    });
    bw.registerCustomEditor(String.class, "map.name", new PropertyEditorSupport() {
      public void setAsText(String text) throws IllegalArgumentException {
        setValue("map" + text);
      }
    });
    bean.populate();

    TestBean tb0 = bean.getArray()[0];
    TestBean tb1 = bean.getArray()[1];
    TestBean tb2 = ((TestBean) bean.getList().get(0));
    TestBean tb3 = ((TestBean) bean.getList().get(1));
    TestBean tb4 = ((TestBean) bean.getMap().get("key1"));
    TestBean tb5 = ((TestBean) bean.getMap().get("key2"));
    assertEquals("name0", tb0.getName());
    assertEquals("name1", tb1.getName());
    assertEquals("name2", tb2.getName());
    assertEquals("name3", tb3.getName());
    assertEquals("name4", tb4.getName());
    assertEquals("name5", tb5.getName());
    assertEquals("name0", bw.getPropertyValue("array[0].name"));
    assertEquals("name1", bw.getPropertyValue("array[1].name"));
    assertEquals("name2", bw.getPropertyValue("list[0].name"));
    assertEquals("name3", bw.getPropertyValue("list[1].name"));
    assertEquals("name4", bw.getPropertyValue("map[key1].name"));
    assertEquals("name5", bw.getPropertyValue("map[key2].name"));
    assertEquals("name4", bw.getPropertyValue("map['key1'].name"));
    assertEquals("name5", bw.getPropertyValue("map[\"key2\"].name"));

    MutablePropertyValues pvs = new MutablePropertyValues();
    pvs.addPropertyValue("array[0].name", "name5");
    pvs.addPropertyValue("array[1].name", "name4");
    pvs.addPropertyValue("list[0].name", "name3");
    pvs.addPropertyValue("list[1].name", "name2");
    pvs.addPropertyValue("map[key1].name", "name1");
    pvs.addPropertyValue("map['key2'].name", "name0");
    bw.setPropertyValues(pvs);
    assertEquals("arrayname5", tb0.getName());
    assertEquals("arrayname4", tb1.getName());
    assertEquals("listname3", tb2.getName());
    assertEquals("listname2", tb3.getName());
    assertEquals("mapname1", tb4.getName());
    assertEquals("mapname0", tb5.getName());
    assertEquals("arrayname5", bw.getPropertyValue("array[0].name"));
    assertEquals("arrayname4", bw.getPropertyValue("array[1].name"));
    assertEquals("listname3", bw.getPropertyValue("list[0].name"));
    assertEquals("listname2", bw.getPropertyValue("list[1].name"));
    assertEquals("mapname1", bw.getPropertyValue("map[\"key1\"].name"));
    assertEquals("mapname0", bw.getPropertyValue("map['key2'].name"));
  }
View Full Code Here


    assertEquals("mapname0", bw.getPropertyValue("map['key2'].name"));
  }

  public void testIndexedPropertiesWithIndividualCustomEditorForProperty() {
    IndexedTestBean bean = new IndexedTestBean(false);
    BeanWrapper bw = new BeanWrapperImpl(bean);
    bw.registerCustomEditor(String.class, "array[0].name", new PropertyEditorSupport() {
      public void setAsText(String text) throws IllegalArgumentException {
        setValue("array0" + text);
      }
    });
    bw.registerCustomEditor(String.class, "array[1].name", new PropertyEditorSupport() {
      public void setAsText(String text) throws IllegalArgumentException {
        setValue("array1" + text);
      }
    });
    bw.registerCustomEditor(String.class, "list[0].name", new PropertyEditorSupport() {
      public void setAsText(String text) throws IllegalArgumentException {
        setValue("list0" + text);
      }
    });
    bw.registerCustomEditor(String.class, "list[1].name", new PropertyEditorSupport() {
      public void setAsText(String text) throws IllegalArgumentException {
        setValue("list1" + text);
      }
    });
    bw.registerCustomEditor(String.class, "map[key1].name", new PropertyEditorSupport() {
      public void setAsText(String text) throws IllegalArgumentException {
        setValue("mapkey1" + text);
      }
    });
    bw.registerCustomEditor(String.class, "map[key2].name", new PropertyEditorSupport() {
      public void setAsText(String text) throws IllegalArgumentException {
        setValue("mapkey2" + text);
      }
    });
    bean.populate();

    TestBean tb0 = bean.getArray()[0];
    TestBean tb1 = bean.getArray()[1];
    TestBean tb2 = ((TestBean) bean.getList().get(0));
    TestBean tb3 = ((TestBean) bean.getList().get(1));
    TestBean tb4 = ((TestBean) bean.getMap().get("key1"));
    TestBean tb5 = ((TestBean) bean.getMap().get("key2"));
    assertEquals("name0", tb0.getName());
    assertEquals("name1", tb1.getName());
    assertEquals("name2", tb2.getName());
    assertEquals("name3", tb3.getName());
    assertEquals("name4", tb4.getName());
    assertEquals("name5", tb5.getName());
    assertEquals("name0", bw.getPropertyValue("array[0].name"));
    assertEquals("name1", bw.getPropertyValue("array[1].name"));
    assertEquals("name2", bw.getPropertyValue("list[0].name"));
    assertEquals("name3", bw.getPropertyValue("list[1].name"));
    assertEquals("name4", bw.getPropertyValue("map[key1].name"));
    assertEquals("name5", bw.getPropertyValue("map[key2].name"));
    assertEquals("name4", bw.getPropertyValue("map['key1'].name"));
    assertEquals("name5", bw.getPropertyValue("map[\"key2\"].name"));

    MutablePropertyValues pvs = new MutablePropertyValues();
    pvs.addPropertyValue("array[0].name", "name5");
    pvs.addPropertyValue("array[1].name", "name4");
    pvs.addPropertyValue("list[0].name", "name3");
    pvs.addPropertyValue("list[1].name", "name2");
    pvs.addPropertyValue("map[key1].name", "name1");
    pvs.addPropertyValue("map['key2'].name", "name0");
    bw.setPropertyValues(pvs);
    assertEquals("array0name5", tb0.getName());
    assertEquals("array1name4", tb1.getName());
    assertEquals("list0name3", tb2.getName());
    assertEquals("list1name2", tb3.getName());
    assertEquals("mapkey1name1", tb4.getName());
    assertEquals("mapkey2name0", tb5.getName());
    assertEquals("array0name5", bw.getPropertyValue("array[0].name"));
    assertEquals("array1name4", bw.getPropertyValue("array[1].name"));
    assertEquals("list0name3", bw.getPropertyValue("list[0].name"));
    assertEquals("list1name2", bw.getPropertyValue("list[1].name"));
    assertEquals("mapkey1name1", bw.getPropertyValue("map[\"key1\"].name"));
    assertEquals("mapkey2name0", bw.getPropertyValue("map['key2'].name"));
  }
View Full Code Here

    Map optionMap = (Map) this.optionSource;
    for (Iterator iterator = optionMap.entrySet().iterator(); iterator.hasNext();) {
      Map.Entry entry = (Map.Entry) iterator.next();
      Object mapKey = entry.getKey();
      Object mapValue = entry.getValue();
      BeanWrapper mapKeyWrapper = PropertyAccessorFactory.forBeanPropertyAccess(mapKey);
      BeanWrapper mapValueWrapper = PropertyAccessorFactory.forBeanPropertyAccess(mapValue);
      Object renderValue = (this.valueProperty != null ?
          mapKeyWrapper.getPropertyValue(this.valueProperty) : mapKey.toString());
      Object renderLabel = (this.labelProperty != null ?
          mapValueWrapper.getPropertyValue(this.labelProperty) : mapValue.toString());
      renderOption(tagWriter, mapKey, renderValue, renderLabel);
    }
  }
View Full Code Here

   * {@link #labelProperty} property is used when rendering the label.
   */
  private void doRenderFromCollection(Collection optionCollection, TagWriter tagWriter) throws JspException {
    for (Iterator it = optionCollection.iterator(); it.hasNext();) {
      Object item = it.next();
      BeanWrapper wrapper = PropertyAccessorFactory.forBeanPropertyAccess(item);
      Object value = (this.valueProperty != null ? wrapper.getPropertyValue(this.valueProperty) : item);
      Object label = (this.labelProperty != null ? wrapper.getPropertyValue(this.labelProperty) : item);
      renderOption(tagWriter, item, value, label);
    }
  }
View Full Code Here

    }

    protected EnvironmentTemplate parseRoot(Element root) {

        EnvironmentTemplate result = new EnvironmentTemplate();
        BeanWrapper beanWrapper = new BeanWrapperImpl(result);
        result.setName(root.getAttribute("env-name"));
        String cygwinAttribute = root.getAttribute("cygwin");
        if (cygwinAttribute != null && cygwinAttribute.length() > 0)
            beanWrapper.setPropertyValue("cygwin", cygwinAttribute);

        NodeList nl = root.getChildNodes();
        Map<String, VariableDefinition> definitions = new LinkedHashMap<String, VariableDefinition>();
        Map<String, VariableDefinition> rDefinitions = new LinkedHashMap<String, VariableDefinition>();
View Full Code Here

    }

    private VariableDefinition importVariableDefinition(Element element) {
        VariableDefinition result = new VariableDefinition();

        BeanWrapper beanWrapper = new BeanWrapperImpl(result);

        final Boolean optional = element.getAttribute("optional") == null ? false : true;             

        beanWrapper.setPropertyValue("variableName", element.getAttribute("var-name"));
        beanWrapper.setPropertyValue("optional", optional);
        beanWrapper.setPropertyValue("path", element.getAttribute("path"));

        NodeList childNodes = element.getChildNodes();
        for (int i = 0; i < childNodes.getLength(); i++) {
            Node node = childNodes.item(i);
            if ("description".equals(node.getNodeName())) {
View Full Code Here

        return result;
    }

    private CalculatedVariableDefinition importVariableCalculation(Element element) {
        CalculatedVariableDefinition result = new CalculatedVariableDefinition();
        BeanWrapper beanWrapper = new BeanWrapperImpl(result);
        beanWrapper.setPropertyValue("variableName", element.getAttribute("var-name"));
        beanWrapper.setPropertyValue("path", element.getAttribute("path"));

        NodeList childNodes = element.getChildNodes();
        for (int i = 0; i < childNodes.getLength(); i++) {
            Node node = childNodes.item(i);
            if ("description".equals(node.getNodeName())) {
View Full Code Here

        }
        return result;
    }

    private void setProperty(Object object, Element element) {
        BeanWrapper beanWrapper = new BeanWrapperImpl(object);
        NodeList childNodes = element.getChildNodes();
        for (int i = 0; i < childNodes.getLength(); i++) {
            Node node = childNodes.item(i);
            if ("value".equals(node.getNodeName())) {
                String value = node.getTextContent();
                beanWrapper.setPropertyValue(element.getAttribute("name"), value);
            }
        }
    }
View Full Code Here

*/
public class BeanInfoTests extends TestCase {

  public void testComplexObject() {
    ValueBean bean = new ValueBean();
    BeanWrapper bw = new BeanWrapperImpl(bean);
    Integer value = new Integer(1);

    bw.setPropertyValue("value", value);
    assertEquals("value not set correctly", bean.getValue(), value);

    value = new Integer(2);
    bw.setPropertyValue("value", value.toString());
    assertEquals("value not converted", bean.getValue(), value);

    bw.setPropertyValue("value", null);
    assertNull("value not null", bean.getValue());

    bw.setPropertyValue("value", "");
    assertNull("value not converted to null", bean.getValue());
  }
View Full Code Here

  }

  private void writeObjectEntry(TagWriter tagWriter, String valueProperty,
      String labelProperty, Object item, int itemIndex) throws JspException {

    BeanWrapper wrapper = PropertyAccessorFactory.forBeanPropertyAccess(item);
    Object renderValue = (valueProperty != null ? wrapper.getPropertyValue(valueProperty) : item);
    Object renderLabel = (labelProperty != null ? wrapper.getPropertyValue(labelProperty) : item);
    writeElementTag(tagWriter, item, renderValue, renderLabel, itemIndex);
  }
View Full Code Here

TOP

Related Classes of org.springframework.beans.BeanWrapper

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.