Package org.springframework.beans

Examples of org.springframework.beans.BeanWrapperImpl$OptionalUnwrapper


  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

    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

    assertNull("Correct bool2 value", tb.getBool2());
  }

  public void testCustomBooleanEditorWithAllowEmpty() {
    BooleanTestBean tb = new BooleanTestBean();
    BeanWrapper bw = new BeanWrapperImpl(tb);
    bw.registerCustomEditor(Boolean.class, new CustomBooleanEditor(true));

    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", "");
    assertTrue("Correct bool2 value", bw.getPropertyValue("bool2") == null);
    assertTrue("Correct bool2 value", tb.getBool2() == null);
  }
View Full Code Here

    assertEquals(falseString, editor.getAsText());
  }

  public void testDefaultNumberEditor() {
    NumberTestBean tb = new NumberTestBean();
    BeanWrapper bw = new BeanWrapperImpl(tb);

    bw.setPropertyValue("short1", "1");
    bw.setPropertyValue("short2", "2");
    bw.setPropertyValue("int1", "7");
    bw.setPropertyValue("int2", "8");
    bw.setPropertyValue("long1", "5");
    bw.setPropertyValue("long2", "6");
    bw.setPropertyValue("bigInteger", "3");
    bw.setPropertyValue("float1", "7.1");
    bw.setPropertyValue("float2", "8.1");
    bw.setPropertyValue("double1", "5.1");
    bw.setPropertyValue("double2", "6.1");
    bw.setPropertyValue("bigDecimal", "4.5");

    assertTrue("Correct short1 value", new Short("1").equals(bw.getPropertyValue("short1")));
    assertTrue("Correct short1 value", tb.getShort1() == 1);
    assertTrue("Correct short2 value", new Short("2").equals(bw.getPropertyValue("short2")));
    assertTrue("Correct short2 value", new Short("2").equals(tb.getShort2()));
    assertTrue("Correct int1 value", new Integer("7").equals(bw.getPropertyValue("int1")));
    assertTrue("Correct int1 value", tb.getInt1() == 7);
    assertTrue("Correct int2 value", new Integer("8").equals(bw.getPropertyValue("int2")));
    assertTrue("Correct int2 value", new Integer("8").equals(tb.getInt2()));
    assertTrue("Correct long1 value", new Long("5").equals(bw.getPropertyValue("long1")));
    assertTrue("Correct long1 value", tb.getLong1() == 5);
    assertTrue("Correct long2 value", new Long("6").equals(bw.getPropertyValue("long2")));
    assertTrue("Correct long2 value", new Long("6").equals(tb.getLong2()));
    assertTrue("Correct bigInteger value", new BigInteger("3").equals(bw.getPropertyValue("bigInteger")));
    assertTrue("Correct bigInteger value", new BigInteger("3").equals(tb.getBigInteger()));
    assertTrue("Correct float1 value", new Float("7.1").equals(bw.getPropertyValue("float1")));
    assertTrue("Correct float1 value", new Float("7.1").equals(new Float(tb.getFloat1())));
    assertTrue("Correct float2 value", new Float("8.1").equals(bw.getPropertyValue("float2")));
    assertTrue("Correct float2 value", new Float("8.1").equals(tb.getFloat2()));
    assertTrue("Correct double1 value", new Double("5.1").equals(bw.getPropertyValue("double1")));
    assertTrue("Correct double1 value", tb.getDouble1() == 5.1);
    assertTrue("Correct double2 value", new Double("6.1").equals(bw.getPropertyValue("double2")));
    assertTrue("Correct double2 value", new Double("6.1").equals(tb.getDouble2()));
    assertTrue("Correct bigDecimal value", new BigDecimal("4.5").equals(bw.getPropertyValue("bigDecimal")));
    assertTrue("Correct bigDecimal value", new BigDecimal("4.5").equals(tb.getBigDecimal()));
  }
View Full Code Here

TOP

Related Classes of org.springframework.beans.BeanWrapperImpl$OptionalUnwrapper

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.