PostgreSQLDataRowPacket implements PostgreSQLPacket { @Override public void write(final PostgreSQLPacketPayload payload) { payload.writeInt2(data.size()); for (Object each : data) { if (null == each) { payload.writeInt4(0xFFFFFFFF); } else { if (each instanceof byte[]) { payload.writeInt4(((byte[]) each).length); payload.writeBytes((byte[]) each); } else if (each instanceof SQLXML) { writeSQLXMLData(payload, each); } else { String columnData = each.toString(); payload.writeInt4(columnData.getBytes().length); payload.writeStringEOF(columnData); } } } }  @Override void write(final PostgreSQLPacketPayload payload);  }
@Test public void assertWriteWithNull() { PostgreSQLDataRowPacket actual = new PostgreSQLDataRowPacket(Collections.singletonList(null)); actual.write(payload); verify(payload).writeInt4(0xFFFFFFFF); }
@Test public void assertWriteWithBytes() { PostgreSQLDataRowPacket actual = new PostgreSQLDataRowPacket(Collections.singletonList(new byte[]{'a'})); actual.write(payload); verify(payload).writeInt4(new byte[]{'a'}.length); verify(payload).writeBytes(new byte[]{'a'}); }
@Test public void assertWriteWithSQLXML() throws SQLException { when(sqlxml.getString()).thenReturn("value"); PostgreSQLDataRowPacket actual = new PostgreSQLDataRowPacket(Collections.singletonList(sqlxml)); actual.write(payload); verify(payload).writeInt4("value".getBytes().length); verify(payload).writeStringEOF("value"); }
@Test public void assertWriteWithString() { PostgreSQLDataRowPacket actual = new PostgreSQLDataRowPacket(Collections.singletonList("value")); assertThat(actual.getData(), is(Collections.singletonList("value"))); actual.write(payload); verify(payload).writeInt4("value".getBytes().length); verify(payload).writeStringEOF("value"); }
@Test public void assertWriteWithSQLXML4Error() throws SQLException { when(sqlxml.getString()).thenThrow(new SQLException("mock")); PostgreSQLDataRowPacket actual = new PostgreSQLDataRowPacket(Collections.singletonList(sqlxml)); actual.write(payload); verify(payload, times(0)).writeStringEOF(any()); }