private void testCast(int typeCode, int length, ObjectInspector inputOI, Object input, String format,
      String expOutput)
      throws HiveException {
    // initialize
    GenericUDFCastFormat udf = new GenericUDFCastFormat();
    ConstantObjectInspector typeCodeOI =
        PrimitiveObjectInspectorFactory.getPrimitiveWritableConstantObjectInspector(
            TypeInfoFactory.getPrimitiveTypeInfo("int"), new IntWritable(typeCode));
    ConstantObjectInspector formatOI =
        PrimitiveObjectInspectorFactory.getPrimitiveWritableConstantObjectInspector(
            TypeInfoFactory.getPrimitiveTypeInfo("string"), new Text(format));
    ConstantObjectInspector lengthOI =
        PrimitiveObjectInspectorFactory.getPrimitiveWritableConstantObjectInspector(
            TypeInfoFactory.getPrimitiveTypeInfo("int"), new IntWritable(length));
    ObjectInspector[] initArgs = {typeCodeOI, inputOI, formatOI, lengthOI};
    udf.initialize(initArgs);

    // evaluate
    GenericUDF.DeferredObject typeCodeObj = new GenericUDF.DeferredJavaObject(typeCode);
    GenericUDF.DeferredObject inputObj = new GenericUDF.DeferredJavaObject(input);
    GenericUDF.DeferredObject formatObj = new GenericUDF.DeferredJavaObject(new Text(format));
    GenericUDF.DeferredObject lengthObj = new GenericUDF.DeferredJavaObject(length);
    GenericUDF.DeferredObject[] evalArgs = {typeCodeObj, inputObj, formatObj, lengthObj};
    Object output = udf.evaluate(evalArgs);
    if (output == null) {
      fail(
          "Cast " + inputOI.getTypeName() + " \"" + input + "\" to " + GenericUDFCastFormat.OUTPUT_TYPES
              .get(typeCode) + " failed, output null");
    }
    assertEquals(
        "Cast " + inputOI.getTypeName() + " \"" + input + "\" to " + GenericUDFCastFormat.OUTPUT_TYPES.get(typeCode)
            + " failed ", expOutput, output.toString());

    // Try with null input
    GenericUDF.DeferredObject[] nullArgs =
        {typeCodeObj, new GenericUDF.DeferredJavaObject(null), formatObj, lengthObj};
    assertNull(udf.getFuncName() + " with NULL arguments failed", udf.evaluate(nullArgs));
  }
Tests CAST (<TIMESTAMP/DATE> AS STRING/CHAR/VARCHAR FORMAT <STRING>) and
CAST (<STRING/CHAR/VARCHAR> AS TIMESTAMP/DATE FORMAT <STRING>).
ql/src/test/org/apache/hadoop/hive/ql/udf/generic/TestGenericUDFCastFormat.java