Search in sources :

Example 46 with Instant

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
    }
}
Also used : Instant(java.time.Instant) Test(org.junit.Test)

Example 47 with Instant

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));
}
Also used : JsonArray(io.vertx.core.json.JsonArray) Instant(java.time.Instant) JsonObject(io.vertx.core.json.JsonObject) Test(org.junit.Test)

Example 48 with Instant

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);
}
Also used : Instant(java.time.Instant) Date(java.util.Date) Test(org.junit.Test)

Example 49 with 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());
}
Also used : LocalDateTime(java.time.LocalDateTime) LocalTime(java.time.LocalTime) Instant(java.time.Instant) LocalDate(java.time.LocalDate) Test(org.junit.Test)

Example 50 with Instant

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);
}
Also used : LocalDateTime(java.time.LocalDateTime) LocalTime(java.time.LocalTime) Instant(java.time.Instant) LocalDate(java.time.LocalDate) Test(org.junit.Test)

Aggregations

Instant (java.time.Instant)463 Test (org.testng.annotations.Test)143 Test (org.junit.Test)85 ZonedDateTime (java.time.ZonedDateTime)39 Duration (java.time.Duration)30 Clock (java.time.Clock)26 Lifetime (org.apache.cxf.sts.request.Lifetime)26 OffsetDateTime (java.time.OffsetDateTime)23 LocalDateTime (java.time.LocalDateTime)20 ArrayList (java.util.ArrayList)18 Element (org.w3c.dom.Element)18 LocalDate (java.time.LocalDate)17 IOException (java.io.IOException)14 Date (java.util.Date)14 LocalTime (java.time.LocalTime)12 DateTimeFormatter (java.time.format.DateTimeFormatter)12 STSException (org.apache.cxf.ws.security.sts.provider.STSException)12 SecurityToken (org.apache.cxf.ws.security.tokenstore.SecurityToken)12 Timestamp (java.sql.Timestamp)11 DefaultConditionsProvider (org.apache.cxf.sts.token.provider.DefaultConditionsProvider)10