MySQLTimeBinaryProtocolValue implements MySQLBinaryProtocolValue { @Override public Object read(final MySQLPacketPayload payload) { int length = payload.readInt1(); payload.readInt1(); payload.readInt4(); switch (length) { case 0: return new Timestamp(0L); case 8: return getTimestamp(payload); case 12: Timestamp result = getTimestamp(payload); result.setNanos(payload.readInt4()); return result; default: throw new IllegalArgumentException(String.format("Wrong length '%d' of MYSQL_TYPE_DATE", length)); } }  @Override Object read(final MySQLPacketPayload payload); @Override void write(final MySQLPacketPayload payload, final Object value);  }
@Test public void assertReadWithZeroByte() { assertThat(new MySQLTimeBinaryProtocolValue().read(payload), is(new Timestamp(0))); }
@Test public void assertReadWithEightBytes() { when(payload.readInt1()).thenReturn(8, 0, 10, 59, 0); Calendar actual = Calendar.getInstance(); actual.setTimeInMillis(((Timestamp) new MySQLTimeBinaryProtocolValue().read(payload)).getTime()); assertThat(actual.get(Calendar.HOUR_OF_DAY), is(10)); assertThat(actual.get(Calendar.MINUTE), is(59)); assertThat(actual.get(Calendar.SECOND), is(0)); }
@Test public void assertReadWithTwelveBytes() { when(payload.readInt1()).thenReturn(12, 0, 10, 59, 0); Calendar actual = Calendar.getInstance(); actual.setTimeInMillis(((Timestamp) new MySQLTimeBinaryProtocolValue().read(payload)).getTime()); assertThat(actual.get(Calendar.HOUR_OF_DAY), is(10)); assertThat(actual.get(Calendar.MINUTE), is(59)); assertThat(actual.get(Calendar.SECOND), is(0)); }
@Test(expected = IllegalArgumentException.class) public void assertReadWithIllegalArgument() { when(payload.readInt1()).thenReturn(100); new MySQLTimeBinaryProtocolValue().read(payload); }