Ejemplo n.º 1
0
  public void testSetProperty() {
    TstJavaBean o1 = createBean();
    TstJavaBean o2 = new TstJavaBean();

    PropertyUtils.setProperty(o2, "byteArrayField", o1.getByteArrayField());
    PropertyUtils.setProperty(o2, "integerField", o1.getIntegerField());
    PropertyUtils.setProperty(o2, "intField", new Integer(o1.getIntField()));
    PropertyUtils.setProperty(o2, "numberField", o1.getNumberField());
    PropertyUtils.setProperty(o2, "objectField", o1.getObjectField());
    PropertyUtils.setProperty(o2, "stringField", o1.getStringField());
    PropertyUtils.setProperty(o2, "booleanField", Boolean.valueOf(o1.isBooleanField()));
  }
Ejemplo n.º 2
0
  protected TstJavaBean createBean() {
    TstJavaBean o1 = new TstJavaBean();
    o1.setByteArrayField(new byte[] {1, 2, 3});
    o1.setIntegerField(new Integer(33));
    o1.setIntField(-44);
    o1.setNumberField(new BigDecimal("11111"));
    o1.setObjectField(new Object());
    o1.setStringField("aaaaa");
    o1.setBooleanField(true);

    return o1;
  }
Ejemplo n.º 3
0
  public void testGetProperty() {
    TstJavaBean o1 = createBean();

    assertSame(o1.getByteArrayField(), PropertyUtils.getProperty(o1, "byteArrayField"));
    assertSame(o1.getIntegerField(), PropertyUtils.getProperty(o1, "integerField"));
    assertEquals(new Integer(o1.getIntField()), PropertyUtils.getProperty(o1, "intField"));
    assertSame(o1.getNumberField(), PropertyUtils.getProperty(o1, "numberField"));
    assertSame(o1.getObjectField(), PropertyUtils.getProperty(o1, "objectField"));
    assertSame(o1.getStringField(), PropertyUtils.getProperty(o1, "stringField"));
    assertEquals(
        Boolean.valueOf(o1.isBooleanField()), PropertyUtils.getProperty(o1, "booleanField"));
  }
Ejemplo n.º 4
0
  public void testCreateAccessor() {

    Accessor accessor = PropertyUtils.createAccessor(TstJavaBean.class, "byteArrayField");
    assertNotNull(accessor);

    TstJavaBean o1 = createBean();
    assertSame(o1.getByteArrayField(), accessor.getValue(o1));

    TstJavaBean o2 = new TstJavaBean();
    assertNull(o2.getByteArrayField());
    accessor.setValue(o2, o1.getByteArrayField());
    assertNotNull(o2.getByteArrayField());
    assertSame(o1.getByteArrayField(), o2.getByteArrayField());
  }
Ejemplo n.º 5
0
  public void testSetConvertedWithCustomConverter() {
    // save old converter for restore
    Converter<Date> oldConverter = ConverterFactory.factory.getConverter(Date.class);

    try {
      ConverterFactory.addConverter(
          Date.class,
          new Converter<Date>() {
            @Override
            protected Date convert(Object value, Class<Date> type) {
              if (value == null) return null;
              if (value instanceof Date) {
                return (Date) value;
              }
              if (value instanceof String) {
                SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
                try {
                  return format.parse((String) value);
                } catch (ParseException e) {
                  throw new CayenneRuntimeException(
                      "Unable to convert '" + value + "' to a Date", e);
                }
              }
              throw new CayenneRuntimeException("Unable to convert '" + value + "' to a Date");
            }
          });

      TstJavaBean o1 = new TstJavaBean();

      // String to date
      PropertyUtils.setProperty(o1, "dateField", "2013-08-01");

      Calendar cal = new GregorianCalendar(2013, 7, 1, 0, 0, 0);
      assertEquals(cal.getTime(), o1.getDateField());
    } finally {

      // restore global date converter
      ConverterFactory.addConverter(Date.class, oldConverter);
    }
  }
Ejemplo n.º 6
0
  public void testCreateAccessorNested() {

    Accessor accessor = PropertyUtils.createAccessor(TstJavaBean.class, "related.byteArrayField");
    assertNotNull(accessor);

    TstJavaBean o1 = createBean();
    o1.setRelated(new TstJavaBean());
    o1.getRelated().setByteArrayField(new byte[] {3, 4, 5});
    assertSame(o1.getRelated().getByteArrayField(), accessor.getValue(o1));

    TstJavaBean o2 = new TstJavaBean();
    o2.setRelated(new TstJavaBean());

    byte[] b1 = new byte[] {6, 7, 8};
    accessor.setValue(o2, b1);
    assertSame(b1, o2.getRelated().getByteArrayField());
  }
Ejemplo n.º 7
0
  public void testSetNull() {
    TstJavaBean o1 = new TstJavaBean();

    o1.setStringField("xyz");
    PropertyUtils.setProperty(o1, "stringField", null);
    assertNull(o1.getStringField());

    o1.setBooleanField(true);
    PropertyUtils.setProperty(o1, "booleanField", null);
    assertEquals(false, o1.isBooleanField());

    o1.setByteField((byte) 2);
    PropertyUtils.setProperty(o1, "byteField", null);
    assertEquals((byte) 0, o1.getByteField());

    o1.setShortField((short) 3);
    PropertyUtils.setProperty(o1, "shortField", null);
    assertEquals((short) 0, o1.getShortField());

    o1.setIntField(99);
    PropertyUtils.setProperty(o1, "intField", null);
    assertEquals(0, o1.getIntField());

    o1.setLongField(98);
    PropertyUtils.setProperty(o1, "longField", null);
    assertEquals(0L, o1.getLongField());

    o1.setFloatField(4.5f);
    PropertyUtils.setProperty(o1, "floatField", null);
    assertEquals(0.0f, o1.getFloatField());

    o1.setDoubleField(5.5f);
    PropertyUtils.setProperty(o1, "doubleField", null);
    assertEquals(0.0, o1.getDoubleField());
  }
Ejemplo n.º 8
0
  public void testSetConverted() {
    TstJavaBean o1 = new TstJavaBean();

    // Object -> String
    Object object = new Object();
    PropertyUtils.setProperty(o1, "stringField", object);
    assertEquals(object.toString(), o1.getStringField());

    // String to number
    PropertyUtils.setProperty(o1, "integerField", "25");
    assertEquals(new Integer(25), o1.getIntegerField());

    // string to byte primitive
    PropertyUtils.setProperty(o1, "byteField", "2");
    assertEquals(2, o1.getByteField());

    // string to short primitive
    PropertyUtils.setProperty(o1, "shortField", "3");
    assertEquals(3, o1.getShortField());

    // string to int primitive
    PropertyUtils.setProperty(o1, "intField", "28");
    assertEquals(28, o1.getIntField());

    // string to long primitive
    PropertyUtils.setProperty(o1, "longField", "29");
    assertEquals(29, o1.getLongField());

    // string to float primitive
    PropertyUtils.setProperty(o1, "floatField", "4.5");
    assertEquals(4.5f, o1.getFloatField());

    // string to double primitive
    PropertyUtils.setProperty(o1, "doubleField", "5.5");
    assertEquals(5.5, o1.getDoubleField());

    // string to boolean
    PropertyUtils.setProperty(o1, "booleanField", "true");
    assertTrue(o1.isBooleanField());
    PropertyUtils.setProperty(o1, "booleanField", "false");
    assertFalse(o1.isBooleanField());

    // int to boolean
    PropertyUtils.setProperty(o1, "booleanField", 1);
    assertTrue(o1.isBooleanField());
    PropertyUtils.setProperty(o1, "booleanField", 0);
    assertFalse(o1.isBooleanField());

    // long to boolean
    PropertyUtils.setProperty(o1, "booleanField", 1L);
    assertTrue(o1.isBooleanField());
    PropertyUtils.setProperty(o1, "booleanField", 0L);
    assertFalse(o1.isBooleanField());

    // long to date
    PropertyUtils.setProperty(o1, "dateField", 0L);
    assertEquals(new Date(0L), o1.getDateField());

    // long to timestamp
    PropertyUtils.setProperty(o1, "timestampField", 0L);
    assertEquals(new Timestamp(0L), o1.getTimestampField());

    // arbitrary string/object to field
    PropertyUtils.setProperty(o1, "stringBuilderField", "abc");
    assertEquals(new StringBuilder("abc").toString(), o1.getStringBuilderField().toString());
  }