@Nullable @Override public OffsetTime getValue(ResultSet rs, int startIndex) throws SQLException { Time time = rs.getTime(startIndex, utc()); return time != null ? OffsetTime.ofInstant(Instant.ofEpochMilli(time.getTime()), ZoneOffset.UTC) : null; }
@Test public void should_fail_as_seconds_fields_are_different() { thrown.expectAssertionError( "%nExpecting:%n" + " <00:00:01Z>%n" + "to have same hour, minute and second as:%n" + " <00:00:00.999999999Z>%n" + "but had not."); assertThat(refOffsetTime).isEqualToIgnoringNanos(refOffsetTime.minusNanos(1)); }
@Test public void should_fail_if_actual_is_not_equal_to_given_OffsetTime_with_nanoseconds_ignored() { thrown.expectAssertionError( "%nExpecting:%n " + "<00:00:01Z>%n" + "to have same hour, minute and second as:%n" + " <00:00:02Z>%n" + "but had not."); assertThat(refOffsetTime).isEqualToIgnoringNanos(refOffsetTime.plusSeconds(1)); }
public class OffsetTimeAssert_isEqualToIgnoringNanoseconds_Test extends BaseTest { private final OffsetTime refOffsetTime = OffsetTime.of(0, 0, 1, 0, ZoneOffset.UTC); @Test public void should_pass_if_actual_is_equal_to_other_ignoring_nanosecond_fields() { assertThat(refOffsetTime).isEqualToIgnoringNanos(refOffsetTime.withNano(55)); assertThat(refOffsetTime).isEqualToIgnoringNanos(refOffsetTime.plusNanos(1)); } @Test public void should_fail_if_actual_is_not_equal_to_given_OffsetTime_with_nanoseconds_ignored() { thrown.expectAssertionError( "%nExpecting:%n " + "<00:00:01Z>%n" + "to have same hour, minute and second as:%n" + " <00:00:02Z>%n" + "but had not."); assertThat(refOffsetTime).isEqualToIgnoringNanos(refOffsetTime.plusSeconds(1)); } @Test public void should_fail_as_seconds_fields_are_different() { thrown.expectAssertionError( "%nExpecting:%n" + " <00:00:01Z>%n" + "to have same hour, minute and second as:%n" + " <00:00:00.999999999Z>%n" + "but had not."); assertThat(refOffsetTime).isEqualToIgnoringNanos(refOffsetTime.minusNanos(1)); } @Test public void should_fail_if_actual_is_null() { expectException(AssertionError.class, actualIsNull()); OffsetTime actual = null; assertThat(actual).isEqualToIgnoringNanos(OffsetTime.now()); } @Test public void should_throw_error_if_given_offsetTime_is_null() { expectIllegalArgumentException(NULL_OFFSET_TIME_PARAMETER_MESSAGE); assertThat(refOffsetTime).isEqualToIgnoringNanos(null); } }
@Test public void should_fail_if_actual_is_not_equal_to_given_offsetTime_with_minute_ignored() { try { assertThat(refOffsetTime).hasSameHourAs(refOffsetTime.minusMinutes(1)); } catch (AssertionError e) { assertThat(e) .hasMessage( format( "%n" + "Expecting:%n" + " <23:00Z>%n" + "to have same hour as:%n" + " <22:59Z>%n" + "but had not.")); return; } failBecauseExpectedAssertionErrorWasNotThrown(); }
@Test public void should_fail_as_minutes_fields_are_different_even_if_time_difference_is_less_than_a_minute() { try { assertThat(refOffsetTime).hasSameHourAs(refOffsetTime.minusNanos(1)); } catch (AssertionError e) { assertThat(e) .hasMessage( format( "%n" + "Expecting:%n" + " <23:00Z>%n" + "to have same hour as:%n" + " <22:59:59.999999999Z>%n" + "but had not.")); return; } failBecauseExpectedAssertionErrorWasNotThrown(); }
@Override public TemporalAccessor parse(String text, Locale locale) throws ParseException { DateTimeFormatter formatterToUse = DateTimeContextHolder.getFormatter(this.formatter, locale); if (LocalDate.class.equals(this.temporalAccessorType)) { return LocalDate.parse(text, formatterToUse); } else if (LocalTime.class.equals(this.temporalAccessorType)) { return LocalTime.parse(text, formatterToUse); } else if (LocalDateTime.class.equals(this.temporalAccessorType)) { return LocalDateTime.parse(text, formatterToUse); } else if (ZonedDateTime.class.equals(this.temporalAccessorType)) { return ZonedDateTime.parse(text, formatterToUse); } else if (OffsetDateTime.class.equals(this.temporalAccessorType)) { return OffsetDateTime.parse(text, formatterToUse); } else if (OffsetTime.class.equals(this.temporalAccessorType)) { return OffsetTime.parse(text, formatterToUse); } else { throw new IllegalStateException( "Unsupported TemporalAccessor type: " + this.temporalAccessorType); } }
@Test public void should_fail_if_actual_is_null() { expectException(AssertionError.class, actualIsNull()); OffsetTime actual = null; assertThat(actual).isEqualToIgnoringNanos(OffsetTime.now()); }
@Test public void should_pass_if_actual_is_equal_to_other_ignoring_nanosecond_fields() { assertThat(refOffsetTime).isEqualToIgnoringNanos(refOffsetTime.withNano(55)); assertThat(refOffsetTime).isEqualToIgnoringNanos(refOffsetTime.plusNanos(1)); }
@Override public void setValue(PreparedStatement st, int startIndex, OffsetTime value) throws SQLException { OffsetTime normalized = value.withOffsetSameInstant(ZoneOffset.UTC); st.setTime(startIndex, new Time(normalized.get(ChronoField.MILLI_OF_DAY)), utc()); }
@Test public void should_be_able_to_catch_exceptions_thrown_by_all_proxied_methods() { try (AutoCloseableBDDSoftAssertions softly = new AutoCloseableBDDSoftAssertions()) { softly.then(BigDecimal.ZERO).isEqualTo(BigDecimal.ONE); softly.then(Boolean.FALSE).isTrue(); softly.then(false).isTrue(); softly.then(new boolean[] {false}).isEqualTo(new boolean[] {true}); softly.then(new Byte((byte) 0)).isEqualTo((byte) 1); softly.then((byte) 2).inHexadecimal().isEqualTo((byte) 3); softly.then(new byte[] {4}).isEqualTo(new byte[] {5}); softly.then(new Character((char) 65)).isEqualTo(new Character((char) 66)); softly.then((char) 67).isEqualTo((char) 68); softly.then(new char[] {69}).isEqualTo(new char[] {70}); softly.then(new StringBuilder("a")).isEqualTo(new StringBuilder("b")); softly.then(Object.class).isEqualTo(String.class); softly .then(parseDatetime("1999-12-31T23:59:59")) .isEqualTo(parseDatetime("2000-01-01T00:00:01")); softly.then(new Double(6.0d)).isEqualTo(new Double(7.0d)); softly.then(8.0d).isEqualTo(9.0d); softly.then(new double[] {10.0d}).isEqualTo(new double[] {11.0d}); softly .then(new File("a")) .overridingErrorMessage("expected:<File(b)> but was:<File(a)>") .isEqualTo(new File("b")); softly.then(new Float(12f)).isEqualTo(new Float(13f)); softly.then(14f).isEqualTo(15f); softly.then(new float[] {16f}).isEqualTo(new float[] {17f}); softly .then(new ByteArrayInputStream(new byte[] {(byte) 65})) .hasSameContentAs(new ByteArrayInputStream(new byte[] {(byte) 66})); softly.then(new Integer(20)).isEqualTo(new Integer(21)); softly.then(22).isEqualTo(23); softly.then(new int[] {24}).isEqualTo(new int[] {25}); softly.then((Iterable<String>) Lists.newArrayList("26")).isEqualTo(Lists.newArrayList("27")); softly.then(Lists.newArrayList("28").iterator()).contains("29"); softly.then(Lists.newArrayList("30")).isEqualTo(Lists.newArrayList("31")); softly.then(new Long(32L)).isEqualTo(new Long(33L)); softly.then(34L).isEqualTo(35L); softly.then(new long[] {36L}).isEqualTo(new long[] {37L}); softly .then(Maps.mapOf(MapEntry.entry("38", "39"))) .isEqualTo(Maps.mapOf(MapEntry.entry("40", "41"))); softly.then(new Short((short) 42)).isEqualTo(new Short((short) 43)); softly.then((short) 44).isEqualTo((short) 45); softly.then(new short[] {(short) 46}).isEqualTo(new short[] {(short) 47}); softly.then("48").isEqualTo("49"); softly .then( new Object() { @Override public String toString() { return "50"; } }) .isEqualTo( new Object() { @Override public String toString() { return "51"; } }); softly .then( new Object[] { new Object() { @Override public String toString() { return "52"; } } }) .isEqualTo( new Object[] { new Object() { @Override public String toString() { return "53"; } } }); final IllegalArgumentException illegalArgumentException = new IllegalArgumentException("IllegalArgumentException message"); softly.then(illegalArgumentException).hasMessage("NullPointerException message"); softly.then(Optional.of("not empty")).isEqualTo("empty"); softly.then(OptionalInt.of(0)).isEqualTo(1); softly.then(OptionalDouble.of(0.0)).isEqualTo(1.0); softly.then(OptionalLong.of(0L)).isEqualTo(1L); softly.then(LocalTime.of(12, 00)).isEqualTo(LocalTime.of(13, 00)); softly .then(OffsetTime.of(12, 0, 0, 0, ZoneOffset.UTC)) .isEqualTo(OffsetTime.of(13, 0, 0, 0, ZoneOffset.UTC)); softly.then(OffsetDateTime.MIN).isEqualTo(OffsetDateTime.MAX); } catch (SoftAssertionError e) { List<String> errors = e.getErrors(); assertThat(errors).hasSize(45); assertThat(errors.get(0)).isEqualTo("expected:<[1]> but was:<[0]>"); assertThat(errors.get(1)).isEqualTo("expected:<[tru]e> but was:<[fals]e>"); assertThat(errors.get(2)).isEqualTo("expected:<[tru]e> but was:<[fals]e>"); assertThat(errors.get(3)).isEqualTo("expected:<[[tru]e]> but was:<[[fals]e]>"); assertThat(errors.get(4)).isEqualTo("expected:<[1]> but was:<[0]>"); assertThat(errors.get(5)).isEqualTo("expected:<0x0[3]> but was:<0x0[2]>"); assertThat(errors.get(6)).isEqualTo("expected:<[[5]]> but was:<[[4]]>"); assertThat(errors.get(7)).isEqualTo("expected:<'[B]'> but was:<'[A]'>"); assertThat(errors.get(8)).isEqualTo("expected:<'[D]'> but was:<'[C]'>"); assertThat(errors.get(9)).isEqualTo("expected:<['[F]']> but was:<['[E]']>"); assertThat(errors.get(10)).isEqualTo("expected:<[b]> but was:<[a]>"); assertThat(errors.get(11)) .isEqualTo("expected:<java.lang.[String]> but was:<java.lang.[Object]>"); assertThat(errors.get(12)) .isEqualTo("expected:<[2000-01-01T00:00:01].000> but was:<[1999-12-31T23:59:59].000>"); assertThat(errors.get(13)).isEqualTo("expected:<[7].0> but was:<[6].0>"); assertThat(errors.get(14)).isEqualTo("expected:<[9].0> but was:<[8].0>"); assertThat(errors.get(15)).isEqualTo("expected:<[1[1].0]> but was:<[1[0].0]>"); assertThat(errors.get(16)).isEqualTo("expected:<File(b)> but was:<File(a)>"); assertThat(errors.get(17)).isEqualTo("expected:<1[3].0f> but was:<1[2].0f>"); assertThat(errors.get(18)).isEqualTo("expected:<1[5].0f> but was:<1[4].0f>"); assertThat(errors.get(19)).isEqualTo("expected:<[1[7].0f]> but was:<[1[6].0f]>"); assertThat(errors.get(20)) .isEqualTo( String.format( "%nInputStreams do not have same content:" + System.getProperty("line.separator") + "line:<1>, expected:<B> but was:<A>")); assertThat(errors.get(21)).isEqualTo("expected:<2[1]> but was:<2[0]>"); assertThat(errors.get(22)).isEqualTo("expected:<2[3]> but was:<2[2]>"); assertThat(errors.get(23)).isEqualTo("expected:<[2[5]]> but was:<[2[4]]>"); assertThat(errors.get(24)).isEqualTo("expected:<[\"2[7]\"]> but was:<[\"2[6]\"]>"); assertThat(errors.get(25)) .isEqualTo( String.format( "%nExpecting:%n" + " <[\"28\"]>%n" + "to contain:%n" + " <[\"29\"]>%n" + "but could not find:%n" + " <[\"29\"]>%n")); assertThat(errors.get(26)).isEqualTo("expected:<[\"3[1]\"]> but was:<[\"3[0]\"]>"); assertThat(errors.get(27)).isEqualTo("expected:<3[3]L> but was:<3[2]L>"); assertThat(errors.get(28)).isEqualTo("expected:<3[5]L> but was:<3[4]L>"); assertThat(errors.get(29)).isEqualTo("expected:<[3[7]L]> but was:<[3[6]L]>"); assertThat(errors.get(30)) .isEqualTo("expected:<{\"[40\"=\"41]\"}> but was:<{\"[38\"=\"39]\"}>"); assertThat(errors.get(31)).isEqualTo("expected:<4[3]> but was:<4[2]>"); assertThat(errors.get(32)).isEqualTo("expected:<4[5]> but was:<4[4]>"); assertThat(errors.get(33)).isEqualTo("expected:<[4[7]]> but was:<[4[6]]>"); assertThat(errors.get(34)).isEqualTo("expected:<\"4[9]\"> but was:<\"4[8]\">"); assertThat(errors.get(35)).isEqualTo("expected:<5[1]> but was:<5[0]>"); assertThat(errors.get(36)).isEqualTo("expected:<[5[3]]> but was:<[5[2]]>"); assertThat(errors.get(37)) .isEqualTo( String.format( "%nExpecting message:%n" + " <\"NullPointerException message\">%n" + "but was:%n" + " <\"IllegalArgumentException message\">")); assertThat(errors.get(38)) .isEqualTo("expected:<[\"empty\"]> but was:<[Optional[not empty]]>"); assertThat(errors.get(39)).isEqualTo("expected:<[1]> but was:<[OptionalInt[0]]>"); assertThat(errors.get(40)).isEqualTo("expected:<[1.0]> but was:<[OptionalDouble[0.0]]>"); assertThat(errors.get(41)).isEqualTo("expected:<[1L]> but was:<[OptionalLong[0]]>"); assertThat(errors.get(42)).isEqualTo("expected:<1[3]:00> but was:<1[2]:00>"); assertThat(errors.get(43)).isEqualTo("expected:<1[3]:00Z> but was:<1[2]:00Z>"); assertThat(errors.get(44)) .isEqualTo( "expected:<[+999999999-12-31T23:59:59.999999999-]18:00> but was:<[-999999999-01-01T00:00+]18:00>"); return; } fail("Should not reach here"); }
@Test public void should_fail_if_actual_is_null() { expectException(AssertionError.class, actualIsNull()); OffsetTime actual = null; assertThat(actual).hasSameHourAs(OffsetTime.now()); }
@Test public void should_pass_if_actual_andexpected_have_same_hour() { assertThat(refOffsetTime).hasSameHourAs(refOffsetTime.plusMinutes(1)); }
public class OffsetTimeAssert_hasSameHourAs_Test extends BaseTest { private final OffsetTime refOffsetTime = OffsetTime.of(23, 0, 0, 0, ZoneOffset.UTC); @Test public void should_pass_if_actual_andexpected_have_same_hour() { assertThat(refOffsetTime).hasSameHourAs(refOffsetTime.plusMinutes(1)); } @Test public void should_fail_if_actual_is_not_equal_to_given_offsetTime_with_minute_ignored() { try { assertThat(refOffsetTime).hasSameHourAs(refOffsetTime.minusMinutes(1)); } catch (AssertionError e) { assertThat(e) .hasMessage( format( "%n" + "Expecting:%n" + " <23:00Z>%n" + "to have same hour as:%n" + " <22:59Z>%n" + "but had not.")); return; } failBecauseExpectedAssertionErrorWasNotThrown(); } @Test public void should_fail_as_minutes_fields_are_different_even_if_time_difference_is_less_than_a_minute() { try { assertThat(refOffsetTime).hasSameHourAs(refOffsetTime.minusNanos(1)); } catch (AssertionError e) { assertThat(e) .hasMessage( format( "%n" + "Expecting:%n" + " <23:00Z>%n" + "to have same hour as:%n" + " <22:59:59.999999999Z>%n" + "but had not.")); return; } failBecauseExpectedAssertionErrorWasNotThrown(); } @Test public void should_fail_if_actual_is_null() { expectException(AssertionError.class, actualIsNull()); OffsetTime actual = null; assertThat(actual).hasSameHourAs(OffsetTime.now()); } @Test public void should_throw_error_if_given_offsetTime_is_null() { expectIllegalArgumentException(NULL_OFFSET_TIME_PARAMETER_MESSAGE); assertThat(refOffsetTime).hasSameHourAs(null); } }