/** date by timestamp. */
  @SuppressWarnings("deprecation")
  @Test
  public void date_by_timestamp() {
    ValueSerde serde = TimestampValueSerdeFactory.DATE;
    TimestampObjectInspector inspector = (TimestampObjectInspector) serde.getInspector();

    DateOption option = new DateOption(new Date(2014, 7, 1));
    Timestamp value = new Timestamp(2014 - 1900, 7 - 1, 1, 0, 0, 0, 0);

    assertThat(inspector.copyObject(option), is((Object) option));
    assertThat(inspector.copyObject(option), is(not(sameInstance((Object) option))));
    assertThat(inspector.copyObject(null), is(nullValue()));
    assertThat(inspector.getPrimitiveJavaObject(option), is(value));
    assertThat(inspector.getPrimitiveJavaObject(null), is(nullValue()));
    assertThat(inspector.getPrimitiveWritableObject(option), is(new TimestampWritable(value)));
    assertThat(inspector.getPrimitiveWritableObject(null), is(nullValue()));

    ValueDriver driver = serde.getDriver(inspector);
    DateOption copy = new DateOption();

    driver.set(copy, option);
    assertThat(copy, is(option));
    driver.set(copy, null);
    assertThat(copy.isNull(), is(true));
  }
  /** varchar. */
  @Test
  public void getVarchar() {
    ValueSerde serde = ValueSerdeFactory.getVarchar(10);
    HiveVarcharObjectInspector inspector = (HiveVarcharObjectInspector) serde.getInspector();

    StringOption option = new StringOption("hello");
    HiveVarchar value = new HiveVarchar("hello", 10);

    assertThat(inspector.copyObject(option), is((Object) option));
    assertThat(inspector.copyObject(option), is(not(sameInstance((Object) option))));
    assertThat(inspector.copyObject(null), is(nullValue()));
    // Note: HiveVarchar.equals(Object) is not defined, but equals(HiveVarchar) is defined
    assertThat(inspector.getPrimitiveJavaObject(option).equals(value), is(true));
    assertThat(inspector.getPrimitiveJavaObject(null), is(nullValue()));
    assertThat(inspector.getPrimitiveWritableObject(option), is(new HiveVarcharWritable(value)));
    assertThat(inspector.getPrimitiveWritableObject(null), is(nullValue()));

    ValueDriver driver = serde.getDriver(inspector);
    StringOption copy = new StringOption();

    driver.set(copy, option);
    assertThat(copy, is(option));
    driver.set(copy, null);
    assertThat(copy.isNull(), is(true));
  }
 private Object get(T instance, Method property) {
   assert instance != null;
   assert property != null;
   ValueOption<?> holder = getHolder(instance, property);
   if (holder.isNull()) {
     return null;
   }
   ValueDriver<?> driver = VALUE_DRIVERS.get(property.getReturnType());
   assert driver != null : property;
   return driver.extractUnsafe(holder);
 }
 @SuppressWarnings("deprecation")
 private void set(T instance, Method property, Object value) {
   assert instance != null;
   assert property != null;
   ValueOption<?> holder = getHolder(instance, property);
   if (value == null) {
     holder.setNull();
   } else {
     ValueDriver<?> driver = VALUE_DRIVERS.get(property.getReturnType());
     assert driver != null : property;
     driver.modifyUnsafe(holder, value);
   }
 }
 @Override
 public Object resolveRawValue(Object value) {
   if (value == null) {
     return null;
   }
   Class<?> type = value.getClass();
   ValueDriver<?> driver = VALUE_DRIVERS.get(type);
   if (driver == null) {
     throw new IllegalArgumentException(
         MessageFormat.format(
             "unsupported property type: {0} ({1})", value, type.getSimpleName()));
   }
   if (((ValueOption<?>) value).isNull()) {
     return null;
   }
   return driver.extractUnsafe(value);
 }
  /** date-time by string. */
  @Test
  public void datetime_by_string_w_zeros() {
    ValueSerde serde = StringValueSerdeFactory.DATETIME;
    StringObjectInspector inspector = (StringObjectInspector) serde.getInspector();

    DateTimeOption option = new DateTimeOption(new DateTime(1, 1, 1, 0, 0, 0));
    String value = "0001-01-01 00:00:00";

    assertThat(inspector.copyObject(option), is((Object) option));
    assertThat(inspector.copyObject(option), is(not(sameInstance((Object) option))));
    assertThat(inspector.copyObject(null), is(nullValue()));
    assertThat(inspector.getPrimitiveJavaObject(option), is(value));
    assertThat(inspector.getPrimitiveJavaObject(null), is(nullValue()));
    assertThat(inspector.getPrimitiveWritableObject(option), is(new Text(value)));
    assertThat(inspector.getPrimitiveWritableObject(null), is(nullValue()));

    ValueDriver driver = serde.getDriver(inspector);
    DateTimeOption copy = new DateTimeOption();

    driver.set(copy, option);
    assertThat(copy, is(option));
    driver.set(copy, null);
    assertThat(copy.isNull(), is(true));
  }
  /** date by string. */
  @Test
  public void date_by_string() {
    ValueSerde serde = StringValueSerdeFactory.DATE;
    StringObjectInspector inspector = (StringObjectInspector) serde.getInspector();

    DateOption option = new DateOption(new Date(2014, 7, 1));
    String value = "2014-07-01";

    assertThat(inspector.copyObject(option), is((Object) option));
    assertThat(inspector.copyObject(option), is(not(sameInstance((Object) option))));
    assertThat(inspector.copyObject(null), is(nullValue()));
    assertThat(inspector.getPrimitiveJavaObject(option), is(value));
    assertThat(inspector.getPrimitiveJavaObject(null), is(nullValue()));
    assertThat(inspector.getPrimitiveWritableObject(option), is(new Text(value)));
    assertThat(inspector.getPrimitiveWritableObject(null), is(nullValue()));

    ValueDriver driver = serde.getDriver(inspector);
    DateOption copy = new DateOption();

    driver.set(copy, option);
    assertThat(copy, is(option));
    driver.set(copy, null);
    assertThat(copy.isNull(), is(true));
  }
  /** decimal by string. */
  @Test
  public void decimal_by_string() {
    ValueSerde serde = StringValueSerdeFactory.DECIMAL;
    StringObjectInspector inspector = (StringObjectInspector) serde.getInspector();

    DecimalOption option = new DecimalOption(new BigDecimal("123.45"));
    String value = "123.45";

    assertThat(inspector.copyObject(option), is((Object) option));
    assertThat(inspector.copyObject(option), is(not(sameInstance((Object) option))));
    assertThat(inspector.copyObject(null), is(nullValue()));
    assertThat(inspector.getPrimitiveJavaObject(option), is(value));
    assertThat(inspector.getPrimitiveJavaObject(null), is(nullValue()));
    assertThat(inspector.getPrimitiveWritableObject(option), is(new Text(value)));
    assertThat(inspector.getPrimitiveWritableObject(null), is(nullValue()));

    ValueDriver driver = serde.getDriver(inspector);
    DecimalOption copy = new DecimalOption();

    driver.set(copy, option);
    assertThat(copy, is(option));
    driver.set(copy, null);
    assertThat(copy.isNull(), is(true));
  }
  /** qualified decimal. */
  @Test
  public void getDecimal() {
    ValueSerde serde = ValueSerdeFactory.getDecimal(10, 2);
    HiveDecimalObjectInspector inspector = (HiveDecimalObjectInspector) serde.getInspector();

    DecimalOption option = new DecimalOption(new BigDecimal("123.45"));
    HiveDecimal value = HiveDecimal.create(new BigDecimal("123.45"));

    assertThat(inspector.copyObject(option), is((Object) option));
    assertThat(inspector.copyObject(option), is(not(sameInstance((Object) option))));
    assertThat(inspector.copyObject(null), is(nullValue()));
    assertThat(inspector.getPrimitiveJavaObject(option), is(value));
    assertThat(inspector.getPrimitiveJavaObject(null), is(nullValue()));
    assertThat(inspector.getPrimitiveWritableObject(option), is(new HiveDecimalWritable(value)));
    assertThat(inspector.getPrimitiveWritableObject(null), is(nullValue()));

    ValueDriver driver = serde.getDriver(inspector);
    DecimalOption copy = new DecimalOption();

    driver.set(copy, option);
    assertThat(copy, is(option));
    driver.set(copy, null);
    assertThat(copy.isNull(), is(true));
  }