MySQLDateBinaryProtocolValue implements MySQLBinaryProtocolValue { @Override public Object read(final MySQLPacketPayload payload) throws SQLException { int length = payload.readInt1(); switch (length) { case 0: throw new SQLFeatureNotSupportedException("Can not support date format if year, month, day is absent."); case 4: return getTimestampForDate(payload); case 7: return getTimestampForDatetime(payload); case 11: Timestamp result = getTimestampForDatetime(payload); result.setNanos(payload.readInt4()); return result; default: throw new IllegalArgumentException(String.format("Wrong length '%d' of MYSQL_TYPE_TIME", length)); } }  @Override Object read(final MySQLPacketPayload payload); @Override void write(final MySQLPacketPayload payload, final Object value);  }
@Test(expected = SQLFeatureNotSupportedException.class) public void assertReadWithZeroByte() throws SQLException { new MySQLDateBinaryProtocolValue().read(payload); }
@Test public void assertReadWithFourBytes() throws SQLException { when(payload.readInt1()).thenReturn(4, 12, 31); when(payload.readInt2()).thenReturn(2018); Calendar actual = Calendar.getInstance(); actual.setTimeInMillis(((Timestamp) new MySQLDateBinaryProtocolValue().read(payload)).getTime()); assertThat(actual.get(Calendar.YEAR), is(2018)); assertThat(actual.get(Calendar.MONTH), is(Calendar.DECEMBER)); assertThat(actual.get(Calendar.DAY_OF_MONTH), is(31)); }
@Test public void assertReadWithSevenBytes() throws SQLException { when(payload.readInt1()).thenReturn(7, 12, 31, 10, 59, 0); when(payload.readInt2()).thenReturn(2018); Calendar actual = Calendar.getInstance(); actual.setTimeInMillis(((Timestamp) new MySQLDateBinaryProtocolValue().read(payload)).getTime()); assertThat(actual.get(Calendar.YEAR), is(2018)); assertThat(actual.get(Calendar.MONTH), is(Calendar.DECEMBER)); assertThat(actual.get(Calendar.DAY_OF_MONTH), is(31)); 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 assertReadWithElevenBytes() throws SQLException { when(payload.readInt1()).thenReturn(11, 12, 31, 10, 59, 0); when(payload.readInt2()).thenReturn(2018); when(payload.readInt4()).thenReturn(500); Calendar actual = Calendar.getInstance(); actual.setTimeInMillis(((Timestamp) new MySQLDateBinaryProtocolValue().read(payload)).getTime()); assertThat(actual.get(Calendar.YEAR), is(2018)); assertThat(actual.get(Calendar.MONTH), is(Calendar.DECEMBER)); assertThat(actual.get(Calendar.DAY_OF_MONTH), is(31)); 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() throws SQLException { when(payload.readInt1()).thenReturn(100); new MySQLDateBinaryProtocolValue().read(payload); }