public void testStringArrayProperty() throws Exception {
    PropsTest pt = new PropsTest();
    BeanWrapper bw = new BeanWrapperImpl(pt);

    bw.setPropertyValue("stringArray", "foo,fi,fi,fum");
    assertTrue("stringArray length = 4", pt.stringArray.length == 4);
    assertTrue(
        "correct values",
        pt.stringArray[0].equals("foo")
            && pt.stringArray[1].equals("fi")
            && pt.stringArray[2].equals("fi")
            && pt.stringArray[3].equals("fum"));

    bw.setPropertyValue("stringArray", new String[] {"foo", "fi", "fi", "fum"});
    assertTrue("stringArray length = 4", pt.stringArray.length == 4);
    assertTrue(
        "correct values",
        pt.stringArray[0].equals("foo")
            && pt.stringArray[1].equals("fi")
            && pt.stringArray[2].equals("fi")
            && pt.stringArray[3].equals("fum"));

    bw.setPropertyValue("stringArray", "one");
    assertTrue("stringArray length = 1", pt.stringArray.length == 1);
    assertTrue("stringArray elt is ok", pt.stringArray[0].equals("one"));
  }
 public void testValidNullUpdate() {
   TestBean t = new TestBean();
   t.setName("Frank"); // we need to change it back
   t.setSpouse(t);
   BeanWrapper bw = new BeanWrapperImpl(t);
   assertTrue("name is not null to start off", t.getName() != null);
   bw.setPropertyValue("name", null);
   assertTrue("name is now null", t.getName() == null);
   // Now test with non-string
   assertTrue("spouse is not null to start off", t.getSpouse() != null);
   bw.setPropertyValue("spouse", null);
   assertTrue("spouse is now null", t.getSpouse() == null);
 }
  public void testSetNestedProperty() throws Exception {
    ITestBean rod = new TestBean("rod", 31);
    ITestBean kerry = new TestBean("kerry", 0);

    BeanWrapper bw = new BeanWrapperImpl(rod);
    bw.setPropertyValue("spouse", kerry);

    assertTrue("nested set worked", rod.getSpouse() == kerry);
    assertTrue("no back relation", kerry.getSpouse() == null);
    bw.setPropertyValue(new PropertyValue("spouse.spouse", rod));
    assertTrue("nested set worked", kerry.getSpouse() == rod);
    assertTrue("kerry age not set", kerry.getAge() == 0);
    bw.setPropertyValue(new PropertyValue("spouse.age", new Integer(35)));
    assertTrue("Set primitive on spouse", kerry.getAge() == 35);
  }
  /** Test default conversion of properties */
  public void testPropertiesProperty() throws Exception {
    PropsTest pt = new PropsTest();
    BeanWrapper bw = new BeanWrapperImpl(pt);
    bw.setPropertyValue("name", "ptest");

    // Note format...
    String ps = "peace=war\nfreedom=slavery";
    bw.setPropertyValue("properties", ps);

    assertTrue("name was set", pt.name.equals("ptest"));
    assertTrue("props non null", pt.props != null);
    String freedomVal = pt.props.getProperty("freedom");
    String peaceVal = pt.props.getProperty("peace");
    assertTrue("peace==war", peaceVal.equals("war"));
    assertTrue("Freedom==slavery", freedomVal.equals("slavery"));
  }
 public void testIndividualAllValid() {
   TestBean t = new TestBean();
   String newName = "tony";
   int newAge = 65;
   String newTouchy = "valid";
   try {
     BeanWrapper bw = new BeanWrapperImpl(t);
     bw.setPropertyValue("age", new Integer(newAge));
     bw.setPropertyValue(new PropertyValue("name", newName));
     bw.setPropertyValue(new PropertyValue("touchy", newTouchy));
     assertTrue("Validly set property must stick", t.getName().equals(newName));
     assertTrue("Validly set property must stick", t.getTouchy().equals(newTouchy));
     assertTrue("Validly set property must stick", t.getAge() == newAge);
   } catch (BeansException ex) {
     fail("Shouldn't throw exception when everything is valid");
   }
 }
 public void testPrimitiveArray() {
   PrimitiveArrayBean tb = new PrimitiveArrayBean();
   BeanWrapper bw = new BeanWrapperImpl(tb);
   bw.setPropertyValue("array", new String[] {"1", "2"});
   assertEquals(2, tb.getArray().length);
   assertEquals(1, tb.getArray()[0]);
   assertEquals(2, tb.getArray()[1]);
 }
  public void testSetWrappedInstanceOfDifferentClass() throws Exception {
    ThrowsException tex = new ThrowsException();
    BeanWrapper bw = new BeanWrapperImpl(tex);

    TestBean tb2 = new TestBean();
    bw.setWrappedInstance(tb2);

    bw.setPropertyValue("age", new Integer(14));
    assertTrue("2nd changed", tb2.getAge() == 14);
  }
 public void testNestedProperties() {
   String doctorCompany = "";
   String lawyerCompany = "Dr. Sueem";
   TestBean tb = new TestBean();
   BeanWrapper bw = new BeanWrapperImpl(tb);
   bw.setPropertyValue("doctor.company", doctorCompany);
   bw.setPropertyValue("lawyer.company", lawyerCompany);
   assertEquals(doctorCompany, tb.getDoctor().getCompany());
   assertEquals(lawyerCompany, tb.getLawyer().getCompany());
 }
  public void testSetNestedPropertyPolymorphic() throws Exception {
    ITestBean rod = new TestBean("rod", 31);
    ITestBean kerry = new Employee();

    BeanWrapper bw = new BeanWrapperImpl(rod);
    bw.setPropertyValue("spouse", kerry);
    bw.setPropertyValue("spouse.age", new Integer(35));
    bw.setPropertyValue("spouse.name", "Kerry");
    bw.setPropertyValue("spouse.company", "Lewisham");
    assertTrue("kerry name is Kerry", kerry.getName().equals("Kerry"));

    assertTrue("nested set worked", rod.getSpouse() == kerry);
    assertTrue("no back relation", kerry.getSpouse() == null);
    bw.setPropertyValue(new PropertyValue("spouse.spouse", rod));
    assertTrue("nested set worked", kerry.getSpouse() == rod);

    BeanWrapper kbw = new BeanWrapperImpl(kerry);
    assertTrue(
        "spouse.spouse.spouse.spouse.company=Lewisham",
        "Lewisham".equals(kbw.getPropertyValue("spouse.spouse.spouse.spouse.company")));
  }
 public void testEmptyValueForPrimitiveProperty() {
   TestBean t = new TestBean();
   try {
     BeanWrapper bw = new BeanWrapperImpl(t);
     bw.setPropertyValue("age", "");
     fail("Should throw exception on type mismatch");
   } catch (TypeMismatchException ex) {
     // expected
   } catch (Exception ex) {
     fail("Shouldn't throw exception other than Type mismatch");
   }
 }
  public void testSetWrappedInstanceOfSameClass() throws Exception {
    TestBean tb = new TestBean();
    BeanWrapper bw = new BeanWrapperImpl(tb);
    tb.setAge(11);

    TestBean tb2 = new TestBean();
    bw.setWrappedInstance(tb2);

    bw.setPropertyValue("age", new Integer(14));
    assertTrue("2nd changed", tb2.getAge() == 14);
    assertTrue("1 didn't change", tb.getAge() == 11);
  }
 public void testArrayToStringConversion() throws PropertyVetoException {
   TestBean tb = new TestBean();
   BeanWrapper bw = new BeanWrapperImpl(tb);
   bw.registerCustomEditor(
       String.class,
       new PropertyEditorSupport() {
         public void setAsText(String text) throws IllegalArgumentException {
           setValue("-" + text + "-");
         }
       });
   bw.setPropertyValue("name", new String[] {"a", "b"});
   assertEquals("-a,b-", tb.getName());
 }
 public void testSetNestedPropertyNullValue() throws Exception {
   ITestBean rod = new TestBean("rod", 31);
   BeanWrapper bw = new BeanWrapperImpl(rod);
   try {
     bw.setPropertyValue("spouse.age", new Integer(31));
     fail("Shouldn't have succeded with null path");
   } catch (NullValueInNestedPathException ex) {
     // ok
     assertTrue(
         "it was the spouse property that was null, not " + ex.getPropertyName(),
         ex.getPropertyName().equals("spouse"));
   }
 }
 public void testArrayToArrayConversion() throws PropertyVetoException {
   IndexedTestBean tb = new IndexedTestBean();
   BeanWrapper bw = new BeanWrapperImpl(tb);
   bw.registerCustomEditor(
       TestBean.class,
       new PropertyEditorSupport() {
         public void setAsText(String text) throws IllegalArgumentException {
           setValue(new TestBean(text, 99));
         }
       });
   bw.setPropertyValue("array", new String[] {"a", "b"});
   assertEquals(2, tb.getArray().length);
   assertEquals("a", tb.getArray()[0].getName());
   assertEquals("b", tb.getArray()[1].getName());
 }
  public void testIntArrayProperty() {
    PropsTest pt = new PropsTest();
    BeanWrapper bw = new BeanWrapperImpl(pt);

    bw.setPropertyValue("intArray", new int[] {4, 5, 2, 3});
    assertTrue("intArray length = 4", pt.intArray.length == 4);
    assertTrue(
        "correct values",
        pt.intArray[0] == 4 && pt.intArray[1] == 5 && pt.intArray[2] == 2 && pt.intArray[3] == 3);

    bw.setPropertyValue("intArray", new String[] {"4", "5", "2", "3"});
    assertTrue("intArray length = 4", pt.intArray.length == 4);
    assertTrue(
        "correct values",
        pt.intArray[0] == 4 && pt.intArray[1] == 5 && pt.intArray[2] == 2 && pt.intArray[3] == 3);

    bw.setPropertyValue("intArray", new Integer(1));
    assertTrue("intArray length = 4", pt.intArray.length == 1);
    assertTrue("correct values", pt.intArray[0] == 1);

    bw.setPropertyValue("intArray", new String[] {"1"});
    assertTrue("intArray length = 4", pt.intArray.length == 1);
    assertTrue("correct values", pt.intArray[0] == 1);
  }
  /**
   * Extract the values for all attributes in the struct.
   *
   * <p>Utilizes public setters and result set metadata.
   *
   * @see java.sql.ResultSetMetaData
   */
  public Object fromStruct(STRUCT struct) throws SQLException {
    Assert.state(this.mappedClass != null, "Mapped class was not specified");
    Object mappedObject = BeanUtils.instantiateClass(this.mappedClass);
    BeanWrapper bw = PropertyAccessorFactory.forBeanPropertyAccess(mappedObject);
    initBeanWrapper(bw);

    ResultSetMetaData rsmd = struct.getDescriptor().getMetaData();
    Object[] attr = struct.getAttributes();
    int columnCount = rsmd.getColumnCount();
    for (int index = 1; index <= columnCount; index++) {
      String column = JdbcUtils.lookupColumnName(rsmd, index).toLowerCase();
      PropertyDescriptor pd = (PropertyDescriptor) this.mappedFields.get(column);
      if (pd != null) {
        Object value = attr[index - 1];
        if (logger.isDebugEnabled()) {
          logger.debug(
              "Mapping column '"
                  + column
                  + "' to property '"
                  + pd.getName()
                  + "' of type "
                  + pd.getPropertyType());
        }
        if (bw.isWritableProperty(pd.getName())) {
          try {
            bw.setPropertyValue(pd.getName(), value);
          } catch (Exception e) {
            e.printStackTrace();
          }
        } else {
          // Если нету сеттера для проперти не нужно кидать ошибку!
          logger.warn(
              "Unable to access the setter for "
                  + pd.getName()
                  + ".  Check that "
                  + "set"
                  + StringUtils.capitalize(pd.getName())
                  + " is declared and has public access.");
        }
      }
    }

    return mappedObject;
  }
 public void testGetterThrowsException() {
   GetterBean gb = new GetterBean();
   BeanWrapper bw = new BeanWrapperImpl(gb);
   bw.setPropertyValue("name", "tom");
   assertTrue("Set name to tom", gb.getName().equals("tom"));
 }