Package org.springframework.beans

Examples of org.springframework.beans.BeanWrapper


  private void writeMapEntry(TagWriter tagWriter, String valueProperty,
      String labelProperty, Map.Entry entry, int itemIndex) throws JspException {

    Object mapKey = entry.getKey();
    Object mapValue = entry.getValue();
    BeanWrapper mapKeyWrapper = PropertyAccessorFactory.forBeanPropertyAccess(mapKey);
    BeanWrapper mapValueWrapper = PropertyAccessorFactory.forBeanPropertyAccess(mapValue);
    Object renderValue = (valueProperty != null ?
        mapKeyWrapper.getPropertyValue(valueProperty) : mapKey.toString());
    Object renderLabel = (labelProperty != null ?
        mapValueWrapper.getPropertyValue(labelProperty) : mapValue.toString());
    writeElementTag(tagWriter, mapKey, renderValue, renderLabel, itemIndex);
  }
View Full Code Here


  public void testComplexObject() {
    TestBean tb = new TestBean();
    String newName = "Rod";
    String tbString = "Kerry_34";

    BeanWrapper bw = new BeanWrapperImpl(tb);
    bw.registerCustomEditor(ITestBean.class, new TestBeanEditor());
    MutablePropertyValues pvs = new MutablePropertyValues();
    pvs.addPropertyValue(new PropertyValue("age", new Integer(55)));
    pvs.addPropertyValue(new PropertyValue("name", newName));
    pvs.addPropertyValue(new PropertyValue("touchy", "valid"));
    pvs.addPropertyValue(new PropertyValue("spouse", tbString));
    bw.setPropertyValues(pvs);
    assertTrue("spouse is non-null", tb.getSpouse() != null);
    assertTrue("spouse name is Kerry and age is 34",
        tb.getSpouse().getName().equals("Kerry") && tb.getSpouse().getAge() == 34);
  }
View Full Code Here

  public void testComplexObjectWithOldValueAccess() {
    TestBean tb = new TestBean();
    String newName = "Rod";
    String tbString = "Kerry_34";

    BeanWrapper bw = new BeanWrapperImpl(tb);
    bw.setExtractOldValueForEditor(true);
    bw.registerCustomEditor(ITestBean.class, new OldValueAccessingTestBeanEditor());
    MutablePropertyValues pvs = new MutablePropertyValues();
    pvs.addPropertyValue(new PropertyValue("age", new Integer(55)));
    pvs.addPropertyValue(new PropertyValue("name", newName));
    pvs.addPropertyValue(new PropertyValue("touchy", "valid"));
    pvs.addPropertyValue(new PropertyValue("spouse", tbString));

    bw.setPropertyValues(pvs);
    assertTrue("spouse is non-null", tb.getSpouse() != null);
    assertTrue("spouse name is Kerry and age is 34",
        tb.getSpouse().getName().equals("Kerry") && tb.getSpouse().getAge() == 34);
    ITestBean spouse = tb.getSpouse();

    bw.setPropertyValues(pvs);
    assertSame("Should have remained same object", spouse, tb.getSpouse());
  }
View Full Code Here

    String s;
    int n;
    MockSourceProvider provider = new MockSourceProvider();
    RefreshablePagedListHolder holder = new RefreshablePagedListHolder(provider);
    holder.setFilter(new MockFilter());
    BeanWrapper bw = new BeanWrapperImpl(holder);

    holder.refresh(false);
    n = holder.getNrOfElements();
    assertTrue("A:number of elements: " + n, n == 12);
    n = holder.getPageCount();
    assertTrue("A:number of pages" + n, n == 2);
    n = holder.getPageSize();
    assertTrue("A:page size: " + n, n == 10);
    n = holder.getPage();
    assertTrue("A:page number: " + n, n == 0);
    n = holder.getFirstElementOnPage();
    assertTrue("A:first element: " + n, n == 0);
    n = holder.getLastElementOnPage();
    assertTrue("A:last element: " + n, n == 9);
    n = holder.getPageList().size();
    assertTrue("A:page list size: " + n, n == 10);
    assertTrue("A:correct filter", holder.getFilter() instanceof MockFilter);
    n = holder.getMaxLinkedPages();
    assertTrue("A:maxLinkedPages: " + n, n == RefreshablePagedListHolder.DEFAULT_MAX_LINKED_PAGES);
    n = holder.getFirstLinkedPage();
    assertTrue("A:first display page: " + n, n == 0);
    n = holder.getLastLinkedPage();
    assertTrue("A:last display page: " + n, n == 1);
    assertTrue("A:sort must be not null", holder.getSort().getProperty() != null);
    n = holder.getSort().getProperty().length();
    assertTrue("A:sort: " + n, n == 0);
    s = ((TestBean)holder.getPageList().get(0)).getName();
    assertTrue("A:page list contents0: " + s, s.equals("Calogero"));
    s = ((TestBean)holder.getPageList().get(4)).getName();
    assertTrue("A:page list contents4: " + s, s.equals("Jesus"));
    n = ((MockSourceProvider) holder.getSourceProvider()).getNrOfCalls();
    assertTrue("Correct number of times called: " + n, n == 1);

    // Go to the second page
    Map cmd = new HashMap();
    cmd.put("page", "1");
    bw.setPropertyValues(cmd);
    holder.refresh(false);
    n = holder.getPage();
    assertTrue("B:page number with array map: " + n, n == 1);
    n = holder.getPageList().size();
    assertTrue("B:page list size: " + n, n == 2);
    n = holder.getFirstElementOnPage();
    assertTrue("B:first element: " + n, n == 10);
    n = holder.getLastElementOnPage();
    assertTrue("B:last element: " + n, n == 11);
    s = ((TestBean)holder.getPageList().get(0)).getName();
    assertTrue("B:page list contents0: " + s, s.equals("Toma"));
    s = ((TestBean)holder.getPageList().get(1)).getName();
    assertTrue("B:page list contents1: " + s, s.equals("Sergio"));

    // Return to the first page and sort by name setting the page size to 5
    cmd.clear();
    cmd.put("sort.property", "name");
    cmd.put("pageSize", "2");
    bw.setPropertyValues(cmd);
    holder.refresh(false);
    n = holder.getPageCount();
    assertTrue("C:number of pages: " + n, n == 6);
    n = holder.getPageSize();
    assertTrue("C:page size: " + n, n == 2);
    n = holder.getPage();
    assertTrue("C:page number: " + n, n == 0);
    n = holder.getFirstLinkedPage();
    assertTrue("C:first display page: " + n, n == 0);
    n = holder.getLastLinkedPage();
    assertTrue("C:last display page: " + n, n == 5);
    n = holder.getFirstElementOnPage();
    assertTrue("C:first element: " + n, n == 0);
    n = holder.getLastElementOnPage();
    assertTrue("C:last element: " + n, n == 1);
    n = holder.getPageList().size();
    assertTrue("C:page list size: " + n, n == 2);
    s = ((TestBean)holder.getPageList().get(0)).getName();
    assertTrue("C:page list contents0: " + s, s.equals("amid"));
    s = ((TestBean)holder.getPageList().get(1)).getName();
    assertTrue("C:page list contents1: " + s, s.equals("Aziz"));
    assertTrue("C:sort must be not null", holder.getSort().getProperty() != null);
    s = holder.getSort().getProperty();
    assertTrue("C:sort: " + s, s.equals("name"));
    n = ((MockSourceProvider) holder.getSourceProvider()).getNrOfCalls();
    assertTrue("Correct number of times called: " + n, n == 1);

    // Ignore case in sort, maxLinkedPages set to 3
    cmd.clear();
    cmd.put("sort.ignoreCase", "false");
    cmd.put("maxLinkedPages", "3");
    bw.setPropertyValues(cmd);
    holder.refresh(false);
    s = ((TestBean)holder.getPageList().get(0)).getName();
    assertTrue("D:page list contents0: " + s, s.equals("Aziz"));
    s = ((TestBean)holder.getPageList().get(1)).getName();
    assertTrue("D:page list contents1: " + s, s.equals("Calogero"));
    n = holder.getFirstLinkedPage();
    assertTrue("D:first display page: " + n, n == 0);
    n = holder.getLastLinkedPage();
    assertTrue("D:last display page: " + n, n == 2);
    n = ((MockSourceProvider) holder.getSourceProvider()).getNrOfCalls();
    assertTrue("Correct number of times called: " + n, n == 1);

    // Go to the fourth page
    holder.setPage(3);
    s = ((TestBean)holder.getPageList().get(0)).getName();
    assertTrue("E:page list contents0: " + s, s.equals("Giuseppe"));
    s = ((TestBean)holder.getPageList().get(1)).getName();
    assertTrue("E:page list contents1: " + s, s.equals("Jesus"));
    n = holder.getFirstLinkedPage();
    assertTrue("E:first display page: " + n, n == 2);
    n = holder.getLastLinkedPage();
    assertTrue("E:last display page: " + n, n == 4);

    // Filter the name
    cmd.clear();
    cmd.put("filter.name", "G");
    bw.setPropertyValues(cmd);
    holder.refresh(false);
    n = holder.getPageCount();
    assertTrue("F:number of pages: " + n, n == 1);
    n = holder.getPageSize();
    assertTrue("F:page size: " + n, n == 2);
    n = holder.getPage();
    assertTrue("F:page number: " + n, n == 0);
    n = holder.getFirstLinkedPage();
    assertTrue("F:first display page: " + n, n == 0);
    n = holder.getLastLinkedPage();
    assertTrue("F:last display page: " + n, n == 0);
    n = holder.getFirstElementOnPage();
    assertTrue("F:first element: " + n, n == 0);
    n = holder.getLastElementOnPage();
    assertTrue("F:last element: " + n, n == 1);
    n = holder.getPageList().size();
    assertTrue("F:page list size: " + n, n == 2);
    s = ((TestBean)holder.getPageList().get(0)).getName();
    assertTrue("F:page list contents0: " + s, s.equals("Garcia"));
    s = ((TestBean)holder.getPageList().get(1)).getName();
    assertTrue("F:page list contents1: " + s, s.equals("Giuseppe"));
    s = ((MockFilter) holder.getFilter()).getName();
    assertTrue("F:filter name: " + s, s.equals("G"));
    n = ((MockSourceProvider) holder.getSourceProvider()).getNrOfCalls();
    assertTrue("Correct number of times called: " + n, n == 2);

    // Request sort on name will reverse the sort
    cmd.clear();
    cmd.put("sort.property", "name");
    bw.setPropertyValues(cmd);
    holder.refresh(false);
    s = ((TestBean)holder.getPageList().get(0)).getName();
    assertTrue("G:page list contents0: " + s, s.equals("Giuseppe"));
    s = ((TestBean)holder.getPageList().get(1)).getName();
    assertTrue("G:page list contents1: " + s, s.equals("Garcia"));

    // Filter the age
    cmd.clear();
    cmd.put("filter.age", "34");
    bw.setPropertyValues(cmd);
    holder.refresh(false);
    n = holder.getLastElementOnPage();
    assertTrue("H:last element: " + n, n == 0);
    n = holder.getPageList().size();
    assertTrue("H:page list size: " + n, n == 1);
    s = ((TestBean)holder.getPageList().get(0)).getName();
    assertTrue("H:page list contents0: " + s, s.equals("Garcia"));
    s = ((MockFilter) holder.getFilter()).getAge();
    assertTrue("H:filter age: " + s, s.equals("34"));
    s = ((MockFilter) holder.getFilter()).getName();
    assertTrue("H:filter name: " + s, s.equals("G"));
    n = ((MockSourceProvider) holder.getSourceProvider()).getNrOfCalls();
    assertTrue("Correct number of times called: " + n, n == 3);

    // Remove the name filter
    // The sort is always name reversed
    cmd.clear();
    cmd.put("filter.name", "");
    bw.setPropertyValues(cmd);
    holder.refresh(false);
    n = holder.getPageList().size();
    assertTrue("I:page list size: " + n, n == 2);
    s = ((MockFilter) holder.getFilter()).getName();
    assertTrue("H:filter name: " + s, s.equals(""));
    s = ((TestBean)holder.getPageList().get(0)).getName();
    assertTrue("I:page list contents0: " + s, s.equals("Garcia"));
    s = ((TestBean)holder.getPageList().get(1)).getName();
    assertTrue("I:page list contents1: " + s, s.equals("Djamel"));
    n = ((MockSourceProvider) holder.getSourceProvider()).getNrOfCalls();
    assertTrue("Correct number of times called: " + n, n == 4);

    // Go to the alternative list
    // The extendedInfo is provided to eventually pass a contextual data on the
    // source provider. BUT, BY ITSELF, IT DOESN'T IMPLY THE REFRESH OF THE LIST.
    cmd.clear();
    cmd.put("filter.extendedInfo", "F");
    bw.setPropertyValues(cmd);
    n = holder.getPageList().size();
    assertTrue("I:page list size: " + n, n == 2);
    s = ((TestBean)holder.getPageList().get(0)).getName();
    assertTrue("I:page list contents0: " + s, s.equals("Garcia"));
    s = ((TestBean)holder.getPageList().get(1)).getName();
    assertTrue("I:page list contents1: " + s, s.equals("Djamel"));

    // When the list is reloaded for any reason, the source provider
    // can use the extendedInfo.
    holder.refresh(true);
    n = holder.getPageList().size();
    assertTrue("J:page list size: " + n, n == 1);
    s = ((TestBean)holder.getPageList().get(0)).getName();
    assertTrue("J:page list contents0: " + s, s.equals("Catherine"));
    n = ((MockSourceProvider) holder.getSourceProvider()).getNrOfCalls();
    assertTrue("Correct number of times called: " + n, n == 5);

    // The locale change imply a refresh of the list.
    // In this test the list will be empty if a locale is provided
    cmd.clear();
    cmd.put("locale", "en_US");
    bw.setPropertyValues(cmd);
    holder.refresh(false);
    n = holder.getPageList().size();
    assertTrue("K:page list size: " + n, n == 0);
    n = holder.getPageCount();
    assertTrue("K:number of pages: " + n, n == 1);
View Full Code Here

    assertSame("Should have remained same object", spouse, tb.getSpouse());
  }

  public void testCustomEditorForSingleProperty() {
    TestBean tb = new TestBean();
    BeanWrapper bw = new BeanWrapperImpl(tb);
    bw.registerCustomEditor(String.class, "name", new PropertyEditorSupport() {
      public void setAsText(String text) throws IllegalArgumentException {
        setValue("prefix" + text);
      }
    });
    bw.setPropertyValue("name", "value");
    bw.setPropertyValue("touchy", "value");
    assertEquals("prefixvalue", bw.getPropertyValue("name"));
    assertEquals("prefixvalue", tb.getName());
    assertEquals("value", bw.getPropertyValue("touchy"));
    assertEquals("value", tb.getTouchy());
  }
View Full Code Here

    assertEquals("value", tb.getTouchy());
  }

  public void testCustomEditorForAllStringProperties() {
    TestBean tb = new TestBean();
    BeanWrapper bw = new BeanWrapperImpl(tb);
    bw.registerCustomEditor(String.class, new PropertyEditorSupport() {
      public void setAsText(String text) throws IllegalArgumentException {
        setValue("prefix" + text);
      }
    });
    bw.setPropertyValue("name", "value");
    bw.setPropertyValue("touchy", "value");
    assertEquals("prefixvalue", bw.getPropertyValue("name"));
    assertEquals("prefixvalue", tb.getName());
    assertEquals("prefixvalue", bw.getPropertyValue("touchy"));
    assertEquals("prefixvalue", tb.getTouchy());
  }
View Full Code Here

  }

  public void testCustomEditorForSingleNestedProperty() {
    TestBean tb = new TestBean();
    tb.setSpouse(new TestBean());
    BeanWrapper bw = new BeanWrapperImpl(tb);
    bw.registerCustomEditor(String.class, "spouse.name", new PropertyEditorSupport() {
      public void setAsText(String text) throws IllegalArgumentException {
        setValue("prefix" + text);
      }
    });
    bw.setPropertyValue("spouse.name", "value");
    bw.setPropertyValue("touchy", "value");
    assertEquals("prefixvalue", bw.getPropertyValue("spouse.name"));
    assertEquals("prefixvalue", tb.getSpouse().getName());
    assertEquals("value", bw.getPropertyValue("touchy"));
    assertEquals("value", tb.getTouchy());
  }
View Full Code Here

  }

  public void testCustomEditorForAllNestedStringProperties() {
    TestBean tb = new TestBean();
    tb.setSpouse(new TestBean());
    BeanWrapper bw = new BeanWrapperImpl(tb);
    bw.registerCustomEditor(String.class, new PropertyEditorSupport() {
      public void setAsText(String text) throws IllegalArgumentException {
        setValue("prefix" + text);
      }
    });
    bw.setPropertyValue("spouse.name", "value");
    bw.setPropertyValue("touchy", "value");
    assertEquals("prefixvalue", bw.getPropertyValue("spouse.name"));
    assertEquals("prefixvalue", tb.getSpouse().getName());
    assertEquals("prefixvalue", bw.getPropertyValue("touchy"));
    assertEquals("prefixvalue", tb.getTouchy());
  }
View Full Code Here

    assertEquals("prefixvalue", tb.getTouchy());
  }

  public void testDefaultBooleanEditorForPrimitiveType() {
    BooleanTestBean tb = new BooleanTestBean();
    BeanWrapper bw = new BeanWrapperImpl(tb);

    bw.setPropertyValue("bool1", "true");
    assertTrue("Correct bool1 value", Boolean.TRUE.equals(bw.getPropertyValue("bool1")));
    assertTrue("Correct bool1 value", tb.isBool1());

    bw.setPropertyValue("bool1", "false");
    assertTrue("Correct bool1 value", Boolean.FALSE.equals(bw.getPropertyValue("bool1")));
    assertTrue("Correct bool1 value", !tb.isBool1());

    bw.setPropertyValue("bool1", "  true  ");
    assertTrue("Correct bool1 value", tb.isBool1());

    bw.setPropertyValue("bool1", "  false  ");
    assertTrue("Correct bool1 value", !tb.isBool1());

    bw.setPropertyValue("bool1", "on");
    assertTrue("Correct bool1 value", tb.isBool1());

    bw.setPropertyValue("bool1", "off");
    assertTrue("Correct bool1 value", !tb.isBool1());

    bw.setPropertyValue("bool1", "yes");
    assertTrue("Correct bool1 value", tb.isBool1());

    bw.setPropertyValue("bool1", "no");
    assertTrue("Correct bool1 value", !tb.isBool1());

    bw.setPropertyValue("bool1", "1");
    assertTrue("Correct bool1 value", tb.isBool1());

    bw.setPropertyValue("bool1", "0");
    assertTrue("Correct bool1 value", !tb.isBool1());

    try {
      bw.setPropertyValue("bool1", "argh");
      fail("Should have thrown BeansException");
    }
    catch (BeansException ex) {
      // expected
    }
View Full Code Here

    }
  }

  public void testDefaultBooleanEditorForWrapperType() {
    BooleanTestBean tb = new BooleanTestBean();
    BeanWrapper bw = new BeanWrapperImpl(tb);

    bw.setPropertyValue("bool2", "true");
    assertTrue("Correct bool2 value", Boolean.TRUE.equals(bw.getPropertyValue("bool2")));
    assertTrue("Correct bool2 value", tb.getBool2().booleanValue());

    bw.setPropertyValue("bool2", "false");
    assertTrue("Correct bool2 value", Boolean.FALSE.equals(bw.getPropertyValue("bool2")));
    assertTrue("Correct bool2 value", !tb.getBool2().booleanValue());

    bw.setPropertyValue("bool2", "on");
    assertTrue("Correct bool2 value", tb.getBool2().booleanValue());

    bw.setPropertyValue("bool2", "off");
    assertTrue("Correct bool2 value", !tb.getBool2().booleanValue());

    bw.setPropertyValue("bool2", "yes");
    assertTrue("Correct bool2 value", tb.getBool2().booleanValue());

    bw.setPropertyValue("bool2", "no");
    assertTrue("Correct bool2 value", !tb.getBool2().booleanValue());

    bw.setPropertyValue("bool2", "1");
    assertTrue("Correct bool2 value", tb.getBool2().booleanValue());

    bw.setPropertyValue("bool2", "0");
    assertTrue("Correct bool2 value", !tb.getBool2().booleanValue());

    bw.setPropertyValue("bool2", "");
    assertNull("Correct bool2 value", tb.getBool2());
  }
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.