MySQLCommandPacketDecoder extends ByteToMessageDecoder { @Override protected void decode(final ChannelHandlerContext ctx, final ByteBuf in, final List<Object> out) { MySQLPacketPayload payload = new MySQLPacketPayload(in); if (!auth) { out.add(decodeHandshakePacket(payload)); auth = true; } else { decodeCommandPacket(payload, out); } }    }
@Test(expected = IllegalArgumentException.class) public void assertDecodeUnsupportedProtocolVersion() { MySQLCommandPacketDecoder commandPacketDecoder = new MySQLCommandPacketDecoder(); commandPacketDecoder.decode(null, byteBuf, null); }
@Test(expected = UnsupportedOperationException.class) public void assertDecodeUnsupportedAuthenticationMethod() { when(byteBuf.readUnsignedByte()).thenReturn((short) 0, (short) MySQLServerInfo.PROTOCOL_VERSION); when(byteBuf.readUnsignedShortLE()).thenReturn(MySQLStatusFlag.SERVER_STATUS_AUTOCOMMIT.getValue()); MySQLCommandPacketDecoder commandPacketDecoder = new MySQLCommandPacketDecoder(); commandPacketDecoder.decode(null, byteBuf, null); }
@Test public void assertDecodeHandshakePacket() { MySQLCommandPacketDecoder commandPacketDecoder = new MySQLCommandPacketDecoder(); List<Object> actual = new ArrayList<>(); commandPacketDecoder.decode(null, mockHandshakePacket(), actual); assertHandshakePacket(actual); }
@Test public void assertDecodeOkPacket() throws NoSuchFieldException, IllegalAccessException { MySQLCommandPacketDecoder commandPacketDecoder = new MySQLCommandPacketDecoder(); List<Object> actual = new ArrayList<>(); ReflectionUtil.setFieldValueToClass(commandPacketDecoder, "auth", true); commandPacketDecoder.decode(null, mockOkPacket(), actual); assertPacketByType(actual, MySQLOKPacket.class); }
@Test public void assertDecodeErrPacket() throws NoSuchFieldException, IllegalAccessException { MySQLCommandPacketDecoder commandPacketDecoder = new MySQLCommandPacketDecoder(); List<Object> actual = new ArrayList<>(); ReflectionUtil.setFieldValueToClass(commandPacketDecoder, "auth", true); commandPacketDecoder.decode(null, mockErrPacket(), actual); assertPacketByType(actual, MySQLErrPacket.class); }
@Test public void assertDecodeQueryCommPacket() throws NoSuchFieldException, IllegalAccessException { MySQLCommandPacketDecoder commandPacketDecoder = new MySQLCommandPacketDecoder(); List<Object> actual = new ArrayList<>(); ReflectionUtil.setFieldValueToClass(commandPacketDecoder, "auth", true); commandPacketDecoder.decode(null, mockEmptyResultSetPacket(), actual); commandPacketDecoder.decode(null, mockFieldDefinition41Packet(), actual); commandPacketDecoder.decode(null, mockEofPacket(), actual); commandPacketDecoder.decode(null, mockEmptyResultSetPacket(), actual); commandPacketDecoder.decode(null, mockEofPacket(), actual); assertPacketByType(actual, InternalResultSet.class); }