MySQLTimeBinaryProtocolValue implements MySQLBinaryProtocolValue { @Override public void write(final MySQLPacketPayload payload, final Object value) { Calendar calendar = Calendar.getInstance(); calendar.setTimeInMillis(((Time) value).getTime()); int hourOfDay = calendar.get(Calendar.HOUR_OF_DAY); int minutes = calendar.get(Calendar.MINUTE); int seconds = calendar.get(Calendar.SECOND); int nanos = new Timestamp(calendar.getTimeInMillis()).getNanos(); boolean isTimeAbsent = 0 == hourOfDay && 0 == minutes && 0 == seconds; boolean isNanosAbsent = 0 == nanos; if (isTimeAbsent && isNanosAbsent) { payload.writeInt1(0); return; } if (isNanosAbsent) { payload.writeInt1(8); writeTime(payload, hourOfDay, minutes, seconds); return; } payload.writeInt1(12); writeTime(payload, hourOfDay, minutes, seconds); writeNanos(payload, nanos); }  @Override Object read(final MySQLPacketPayload payload); @Override void write(final MySQLPacketPayload payload, final Object value);  }
@Test public void assertWriteWithZeroByte() { MySQLTimeBinaryProtocolValue actual = new MySQLTimeBinaryProtocolValue(); actual.write(payload, Time.valueOf("00:00:00")); verify(payload).writeInt1(0); }
@Test public void assertWriteWithEightBytes() { MySQLTimeBinaryProtocolValue actual = new MySQLTimeBinaryProtocolValue(); actual.write(payload, Time.valueOf("01:30:10")); verify(payload).writeInt1(8); verify(payload).writeInt1(0); verify(payload).writeInt4(0); payload.writeInt1(0); payload.writeInt4(0); verify(payload).writeInt1(1); verify(payload).writeInt1(30); verify(payload).writeInt1(10); }
@Test public void assertWriteWithTwelveBytes() { MySQLTimeBinaryProtocolValue actual = new MySQLTimeBinaryProtocolValue(); actual.write(payload, new Time(1L)); verify(payload).writeInt1(12); verify(payload, times(5)).writeInt1(anyInt()); verify(payload).writeInt4(0); verify(payload).writeInt4(1000000); }