PostgreSQLErrPacketFactory { public static PostgreSQLErrorResponsePacket newInstance(final Exception cause) { if (cause instanceof PSQLException) { PostgreSQLErrorResponsePacket result = new PostgreSQLErrorResponsePacket(); ServerErrorMessage serverErrorMessage = ((PSQLException) cause).getServerErrorMessage(); if (null == serverErrorMessage) { PSQLException ex = (PSQLException) cause; result.addField(PostgreSQLErrorResponsePacket.FIELD_TYPE_CODE, ex.getSQLState()); result.addField(PostgreSQLErrorResponsePacket.FIELD_TYPE_MESSAGE, ex.getMessage()); } else { result.addField(PostgreSQLErrorResponsePacket.FIELD_TYPE_SEVERITY, serverErrorMessage.getSeverity()); result.addField(PostgreSQLErrorResponsePacket.FIELD_TYPE_CODE, serverErrorMessage.getSQLState()); result.addField(PostgreSQLErrorResponsePacket.FIELD_TYPE_MESSAGE, serverErrorMessage.getMessage()); result.addField(PostgreSQLErrorResponsePacket.FIELD_TYPE_POSITION, Integer.toString(serverErrorMessage.getPosition())); } return result; } PostgreSQLErrorResponsePacket result = new PostgreSQLErrorResponsePacket(); result.addField(PostgreSQLErrorResponsePacket.FIELD_TYPE_MESSAGE, cause.getMessage()); return result; }  static PostgreSQLErrorResponsePacket newInstance(final Exception cause);  }
@Test public void assertPSQLExceptionWithServerErrorMessageNotNull() throws NoSuchFieldException, IllegalAccessException { ServerErrorMessage serverErrorMessage = mock(ServerErrorMessage.class); when(serverErrorMessage.getSeverity()).thenReturn("severity"); when(serverErrorMessage.getSQLState()).thenReturn("sqlState"); when(serverErrorMessage.getMessage()).thenReturn("message"); when(serverErrorMessage.getPosition()).thenReturn(1); PostgreSQLErrorResponsePacket actual = PostgreSQLErrPacketFactory.newInstance(new PSQLException(serverErrorMessage)); Field packetField = PostgreSQLErrorResponsePacket.class.getDeclaredField("fields"); packetField.setAccessible(true); Map<Character, String> fields = (Map<Character, String>) packetField.get(actual); assertThat(fields.get(PostgreSQLErrorResponsePacket.FIELD_TYPE_SEVERITY), is("severity")); assertThat(fields.get(PostgreSQLErrorResponsePacket.FIELD_TYPE_CODE), is("sqlState")); assertThat(fields.get(PostgreSQLErrorResponsePacket.FIELD_TYPE_MESSAGE), is("message")); assertThat(fields.get(PostgreSQLErrorResponsePacket.FIELD_TYPE_POSITION), is("1")); }
@Test public void assertPSQLExceptionWithServerErrorMessageIsNull() throws NoSuchFieldException, IllegalAccessException { PostgreSQLErrorResponsePacket actual = PostgreSQLErrPacketFactory.newInstance(new PSQLException("psqlEx", PSQLState.UNEXPECTED_ERROR, new Exception("test"))); Field packetField = PostgreSQLErrorResponsePacket.class.getDeclaredField("fields"); packetField.setAccessible(true); Map<Character, String> fields = (Map<Character, String>) packetField.get(actual); assertThat(fields.get(PostgreSQLErrorResponsePacket.FIELD_TYPE_CODE), is(PSQLState.UNEXPECTED_ERROR.getState())); assertThat(fields.get(PostgreSQLErrorResponsePacket.FIELD_TYPE_MESSAGE), is("psqlEx")); }
@Test public void assertRuntimeException() throws NoSuchFieldException, IllegalAccessException { PostgreSQLErrorResponsePacket actual = PostgreSQLErrPacketFactory.newInstance(new RuntimeException("test")); Field packetField = PostgreSQLErrorResponsePacket.class.getDeclaredField("fields"); packetField.setAccessible(true); Map<Character, String> fields = (Map<Character, String>) packetField.get(actual); assertThat(fields.get(PostgreSQLErrorResponsePacket.FIELD_TYPE_MESSAGE), is("test")); }