use of java.time.Instant in project vert.x by eclipse.
the class JsonObjectTest method testGetInstant.
@Test
public void testGetInstant() {
Instant now = Instant.now();
jsonObject.put("foo", now);
assertEquals(now, jsonObject.getInstant("foo"));
// Can also get as string:
String val = jsonObject.getString("foo");
assertNotNull(val);
Instant retrieved = Instant.from(ISO_INSTANT.parse(val));
assertEquals(now, retrieved);
jsonObject.put("foo", 123);
try {
jsonObject.getInstant("foo");
fail();
} catch (ClassCastException e) {
// Ok
}
jsonObject.putNull("foo");
assertNull(jsonObject.getInstant("foo"));
assertNull(jsonObject.getInstant("absent"));
try {
jsonObject.getInstant(null);
fail();
} catch (NullPointerException e) {
// OK
}
try {
jsonObject.getInstant(null, null);
fail();
} catch (NullPointerException e) {
// OK
}
}
use of java.time.Instant in project vert.x by eclipse.
the class JsonArrayTest method testDecode.
@Test
public void testDecode() {
byte[] bytes = TestUtils.randomByteArray(10);
String strBytes = Base64.getEncoder().encodeToString(bytes);
Instant now = Instant.now();
String strInstant = ISO_INSTANT.format(now);
String json = "[\"foo\",123,1234,1.23,2.34,true,\"" + strBytes + "\",\"" + strInstant + "\",null,{\"foo\":\"bar\"},[\"foo\",123]]";
JsonArray arr = new JsonArray(json);
assertEquals("foo", arr.getString(0));
assertEquals(Integer.valueOf(123), arr.getInteger(1));
assertEquals(Long.valueOf(1234l), arr.getLong(2));
assertEquals(Float.valueOf(1.23f), arr.getFloat(3));
assertEquals(Double.valueOf(2.34d), arr.getDouble(4));
assertEquals(true, arr.getBoolean(5));
assertTrue(TestUtils.byteArraysEqual(bytes, arr.getBinary(6)));
assertEquals(now, arr.getInstant(7));
assertTrue(arr.hasNull(8));
JsonObject obj = arr.getJsonObject(9);
assertEquals("bar", obj.getString("foo"));
JsonArray arr2 = arr.getJsonArray(10);
assertEquals("foo", arr2.getString(0));
assertEquals(Integer.valueOf(123), arr2.getInteger(1));
}
use of java.time.Instant in project morphia by mongodb.
the class InstantConverterTest method testConversion.
@Test
public void testConversion() {
Instant instant = Instant.ofEpochSecond(42);
assertFormat(instant, new Date(42000));
compare(Instant.class, instant);
}
use of java.time.Instant in project morphia by mongodb.
the class Java8EntityTest method rangeQueries.
@Test
public void rangeQueries() {
Instant instant = Instant.ofEpochMilli(System.currentTimeMillis());
LocalDate localDate = LocalDate.of(1995, 10, 15);
LocalDateTime localDateTime = LocalDateTime.of(2016, 4, 10, 14, 15, 16, 123 * 1000000);
LocalTime localTime = LocalTime.of(10, 29, 15, 848493);
for (int i = 0; i < 10; i++) {
createEntity(getDs(), instant.plus(i, DAYS), localDate.plus(i, DAYS), localDateTime.plus(i, DAYS), localTime.plus(i, ChronoUnit.HOURS));
}
Assert.assertEquals(2L, getDs().find(Java8Entity.class).field("instant").lessThanOrEq(instant.plus(1, DAYS)).count());
Assert.assertEquals(1L, getDs().find(Java8Entity.class).field("localDate").equal(localDate.plus(1, DAYS)).count());
Assert.assertEquals(0L, getDs().find(Java8Entity.class).field("localDate").equal(localDate.minus(1, DAYS)).count());
Assert.assertEquals(9L, getDs().find(Java8Entity.class).field("localDateTime").notEqual(localDateTime.plus(6, DAYS)).count());
}
use of java.time.Instant in project morphia by mongodb.
the class Java8EntityTest method queries.
@Test
public void queries() {
Instant instant = Instant.ofEpochMilli(System.currentTimeMillis());
LocalDate localDate = LocalDate.of(1995, 10, 15);
LocalDateTime localDateTime = LocalDateTime.of(2016, 4, 10, 14, 15, 16, 123 * 1000000);
LocalTime localTime = LocalTime.of(10, 29, 15, 848000000);
Java8Entity entity = createEntity(getDs(), instant, localDate, localDateTime, localTime);
compare(getDs(), entity, "instant", instant);
compare(getDs(), entity, "localDate", localDate);
compare(getDs(), entity, "localDateTime", localDateTime);
compare(getDs(), entity, "localTime", localTime);
}
Aggregations