Пример #1
0
 @Override
 public void convertValue(
     final ValueConversionContext conversionContext,
     final Object value,
     final JavaTypeInfo<?> type) {
   if ((value == null) && type.isAllowNull()) {
     conversionContext.setResult(null);
     return;
   }
   final Class<?> clazz = type.getRawClass();
   if (clazz == Value.class) {
     if (value instanceof LocalDate) {
       conversionContext.setResult(ValueUtils.of(((LocalDate) value).toString()));
     } else if (value instanceof InstantProvider) {
       conversionContext.setResult(
           ValueUtils.of((double) ((InstantProvider) value).toInstant().getEpochSeconds()));
     } else {
       conversionContext.setFail();
     }
   } else if (clazz == LocalDate.class) {
     final String str = ((Value) value).getStringValue();
     if (str != null) {
       try {
         conversionContext.setResult(LocalDate.parse(str));
       } catch (CalendricalException e) {
         conversionContext.setFail();
       }
     } else {
       conversionContext.setFail();
     }
   } else if (clazz == Instant.class) {
     final Double epochSeconds = ((Value) value).getDoubleValue();
     if (epochSeconds != null) {
       conversionContext.setResult(Instant.ofEpochSeconds(epochSeconds.longValue()));
     } else {
       conversionContext.setFail();
     }
   } else if (clazz == ZonedDateTime.class) {
     final ZonedDateTime zdt = valueToZonedDateTime((Value) value);
     if (zdt != null) {
       conversionContext.setResult(zdt);
     } else {
       conversionContext.setFail();
     }
   } else if (clazz == Expiry.class) {
     final ZonedDateTime zdt = valueToZonedDateTime((Value) value);
     if (zdt != null) {
       conversionContext.setResult(new Expiry(zdt));
     } else {
       conversionContext.setFail();
     }
   } else {
     conversionContext.setFail();
   }
 }
/** Test VersionCorrection. */
@Test
public class VersionCorrectionTest {

  private static final Instant INSTANT1 = Instant.ofEpochSeconds(1);
  private static final Instant INSTANT2 = Instant.ofEpochSeconds(2);
  private static final Instant INSTANT3 = Instant.ofEpochSeconds(3);

  public void test_LATEST() {
    VersionCorrection test = VersionCorrection.LATEST;
    assertEquals(null, test.getVersionAsOf());
    assertEquals(null, test.getCorrectedTo());
    assertEquals("VLATEST.CLATEST", test.toString());
  }

  // -------------------------------------------------------------------------
  public void test_of_VersionCorrection() {
    VersionCorrection base = VersionCorrection.of(INSTANT1, INSTANT2);
    VersionCorrection test = VersionCorrection.of(base);
    assertSame(base, test);
  }

  public void test_of_VersionCorrection_null() {
    VersionCorrection test = VersionCorrection.of((VersionCorrection) null);
    assertSame(VersionCorrection.LATEST, test);
  }

  // -------------------------------------------------------------------------
  public void test_of_InstantInstant() {
    VersionCorrection test = VersionCorrection.of(INSTANT1, INSTANT2);
    assertEquals(INSTANT1, test.getVersionAsOf());
    assertEquals(INSTANT2, test.getCorrectedTo());
    assertEquals("V1970-01-01T00:00:01Z.C1970-01-01T00:00:02Z", test.toString());
  }

  public void test_of_InstantInstant_nullVersion() {
    VersionCorrection test = VersionCorrection.of((InstantProvider) null, INSTANT2);
    assertEquals(null, test.getVersionAsOf());
    assertEquals(INSTANT2, test.getCorrectedTo());
    assertEquals("VLATEST.C1970-01-01T00:00:02Z", test.toString());
  }

  public void test_of_InstantInstant_nullCorrection() {
    VersionCorrection test = VersionCorrection.of(INSTANT1, (InstantProvider) null);
    assertEquals(INSTANT1, test.getVersionAsOf());
    assertEquals(null, test.getCorrectedTo());
    assertEquals("V1970-01-01T00:00:01Z.CLATEST", test.toString());
  }

  public void test_of_InstantInstant_nulls() {
    VersionCorrection test = VersionCorrection.of((InstantProvider) null, (InstantProvider) null);
    assertSame(VersionCorrection.LATEST, test);
  }

  // -------------------------------------------------------------------------
  public void test_ofVersionAsOf_Instant() {
    VersionCorrection test = VersionCorrection.ofVersionAsOf(INSTANT1);
    assertEquals(INSTANT1, test.getVersionAsOf());
    assertEquals(null, test.getCorrectedTo());
  }

  public void test_ofVersionAsOf_Instant_null() {
    VersionCorrection test = VersionCorrection.ofVersionAsOf((InstantProvider) null);
    assertSame(VersionCorrection.LATEST, test);
  }

  // -------------------------------------------------------------------------
  public void test_ofCorrectedTo_Instant() {
    VersionCorrection test = VersionCorrection.ofCorrectedTo(INSTANT2);
    assertEquals(null, test.getVersionAsOf());
    assertEquals(INSTANT2, test.getCorrectedTo());
  }

  public void test_ofCorrectedTo_Instant_null() {
    VersionCorrection test = VersionCorrection.ofCorrectedTo((InstantProvider) null);
    assertSame(VersionCorrection.LATEST, test);
  }

  // -------------------------------------------------------------------------
  public void test_parse() {
    VersionCorrection test = VersionCorrection.parse("V1970-01-01T00:00:01Z.C1970-01-01T00:00:02Z");
    VersionCorrection expected = VersionCorrection.of(INSTANT1, INSTANT2);
    assertEquals(expected, test);
  }

  public void test_parse_latestVersion() {
    VersionCorrection test = VersionCorrection.parse("VLATEST.C1970-01-01T00:00:02Z");
    VersionCorrection expected = VersionCorrection.of(null, INSTANT2);
    assertEquals(expected, test);
  }

  public void test_parse_latestCorrection() {
    VersionCorrection test = VersionCorrection.parse("V1970-01-01T00:00:01Z.CLATEST");
    VersionCorrection expected = VersionCorrection.of(INSTANT1, null);
    assertEquals(expected, test);
  }

  public void test_parse_latests() {
    VersionCorrection test = VersionCorrection.parse("VLATEST.CLATEST");
    assertSame(VersionCorrection.LATEST, test);
  }

  @DataProvider(name = "parseInvalid")
  public Object[][] data_parseInvalid() {
    return new Object[][] {
      {"1970-01-01T00:00:01Z.C1970-01-01T00:00:02Z"}, // no V
      {"V1970-01-01T00:00:01Z.1970-01-01T00:00:02Z"}, // no C
      {""}, // blank
      {"V1970-01-01T00:00:01Z"}, // only half
      {"V1970-12-01 00:00:01Z.C1970-01-01T00:00:02Z"}, // invalid date 1
      {"V1970-12-01T00:00:01Z.C1970-01-20 00:00:02Z"}, // invalid date 2
      {"VLATS.CLATS"}, // invalid latest
    };
  }

  @Test(dataProvider = "parseInvalid", expectedExceptions = IllegalArgumentException.class)
  public void test_parse_invalidNoV(String input) {
    VersionCorrection.parse(input);
  }

  // -------------------------------------------------------------------------
  public void test_withVersionAsOf_instantToInstant() {
    VersionCorrection test = VersionCorrection.of(INSTANT1, INSTANT2);
    assertEquals(VersionCorrection.of(INSTANT3, INSTANT2), test.withVersionAsOf(INSTANT3));
  }

  public void test_withVersionAsOf_instantToNull() {
    VersionCorrection test = VersionCorrection.of(INSTANT1, INSTANT2);
    assertEquals(VersionCorrection.of(null, INSTANT2), test.withVersionAsOf(null));
  }

  public void test_withVersionAsOf_nullToInstant() {
    VersionCorrection test = VersionCorrection.of(null, INSTANT2);
    assertEquals(VersionCorrection.of(INSTANT3, INSTANT2), test.withVersionAsOf(INSTANT3));
  }

  public void test_withVersionAsOf_nullToNull() {
    VersionCorrection test = VersionCorrection.of(null, INSTANT2);
    assertEquals(VersionCorrection.of(null, INSTANT2), test.withVersionAsOf(null));
  }

  // -------------------------------------------------------------------------
  public void test_withCorrectedTo_instantToInstant() {
    VersionCorrection test = VersionCorrection.of(INSTANT1, INSTANT2);
    assertEquals(VersionCorrection.of(INSTANT1, INSTANT3), test.withCorrectedTo(INSTANT3));
  }

  public void test_withCorrectedTo_instantToNull() {
    VersionCorrection test = VersionCorrection.of(INSTANT1, INSTANT2);
    assertEquals(VersionCorrection.of(INSTANT1, null), test.withCorrectedTo(null));
  }

  public void test_withCorrectedTo_nullToInstant() {
    VersionCorrection test = VersionCorrection.of(INSTANT1, null);
    assertEquals(VersionCorrection.of(INSTANT1, INSTANT3), test.withCorrectedTo(INSTANT3));
  }

  public void test_withCorrectedTo_nullToNull() {
    VersionCorrection test = VersionCorrection.of(INSTANT1, null);
    assertEquals(VersionCorrection.of(INSTANT1, null), test.withCorrectedTo(null));
  }

  // -------------------------------------------------------------------------
  public void test_withLatestFixed_noNulls() {
    VersionCorrection test = VersionCorrection.of(INSTANT1, INSTANT2);
    assertSame(test, test.withLatestFixed(INSTANT3));
  }

  public void test_withLatestFixed_nullVersion() {
    VersionCorrection test = VersionCorrection.of(null, INSTANT2);
    assertEquals(VersionCorrection.of(INSTANT3, INSTANT2), test.withLatestFixed(INSTANT3));
  }

  public void test_withLatestFixed_nullCorrection() {
    VersionCorrection test = VersionCorrection.of(INSTANT1, null);
    assertEquals(VersionCorrection.of(INSTANT1, INSTANT3), test.withLatestFixed(INSTANT3));
  }

  public void test_withLatestFixed_nulls() {
    VersionCorrection test = VersionCorrection.of(null, null);
    assertEquals(VersionCorrection.of(INSTANT3, INSTANT3), test.withLatestFixed(INSTANT3));
  }

  // -------------------------------------------------------------------------
  public void test_compareTo_nonNull() {
    VersionCorrection a = VersionCorrection.of(INSTANT1, INSTANT2);
    VersionCorrection b = VersionCorrection.of(INSTANT1, INSTANT3);
    VersionCorrection c = VersionCorrection.of(INSTANT2, INSTANT3);

    assertEquals(true, a.compareTo(a) == 0);
    assertEquals(true, a.compareTo(b) < 0);
    assertEquals(true, a.compareTo(c) < 0);

    assertEquals(true, b.compareTo(a) > 0);
    assertEquals(true, b.compareTo(b) == 0);
    assertEquals(true, b.compareTo(c) < 0);

    assertEquals(true, c.compareTo(a) > 0);
    assertEquals(true, c.compareTo(b) > 0);
    assertEquals(true, c.compareTo(c) == 0);
  }

  public void test_compareTo_nullVersion() {
    VersionCorrection a = VersionCorrection.of(INSTANT1, INSTANT2);
    VersionCorrection b = VersionCorrection.of(null, INSTANT2);

    assertEquals(true, a.compareTo(a) == 0);
    assertEquals(true, a.compareTo(b) < 0);

    assertEquals(true, b.compareTo(a) > 0);
    assertEquals(true, b.compareTo(b) == 0);
  }

  public void test_compareTo_nullCorrection() {
    VersionCorrection a = VersionCorrection.of(INSTANT1, INSTANT2);
    VersionCorrection b = VersionCorrection.of(INSTANT1, null);

    assertEquals(true, a.compareTo(a) == 0);
    assertEquals(true, a.compareTo(b) < 0);

    assertEquals(true, b.compareTo(a) > 0);
    assertEquals(true, b.compareTo(b) == 0);
  }

  @Test(expectedExceptions = NullPointerException.class)
  public void test_compareTo_null() {
    VersionCorrection test = VersionCorrection.of(INSTANT1, INSTANT2);
    test.compareTo(null);
  }

  // -------------------------------------------------------------------------
  public void test_equals() {
    VersionCorrection d1a = VersionCorrection.of(INSTANT1, INSTANT2);
    VersionCorrection d1b = VersionCorrection.of(INSTANT1, INSTANT2);
    VersionCorrection d2 = VersionCorrection.of(INSTANT1, INSTANT3);

    assertEquals(true, d1a.equals(d1a));
    assertEquals(true, d1a.equals(d1b));
    assertEquals(false, d1a.equals(d2));

    assertEquals(true, d1b.equals(d1a));
    assertEquals(true, d1b.equals(d1b));
    assertEquals(false, d1b.equals(d2));

    assertEquals(false, d2.equals(d1a));
    assertEquals(false, d2.equals(d1b));
    assertEquals(true, d2.equals(d2));

    assertEquals(false, d1b.equals("d1"));
    assertEquals(false, d1b.equals(null));
  }

  public void test_hashCode() {
    VersionCorrection d1a = VersionCorrection.of(INSTANT1, INSTANT2);
    VersionCorrection d1b = VersionCorrection.of(INSTANT1, INSTANT2);

    assertEquals(d1a.hashCode(), d1b.hashCode());
  }
}