@Test public void whenARuntimeExceptionOccurredTheConnectionShouldBeClosedAndTheDeveloperBeWarned() throws Exception { final RuntimeException exampleRuntimeException = new RuntimeException( "The answer to everything could not be found."); cut.handleException(exampleRuntimeException); final ArgumentCaptor<Throwable> cause = ArgumentCaptor.forClass(Throwable.class); final ArgumentCaptor<String> developerMessage = ArgumentCaptor.forClass(String.class); verify(developer).unrecoverableProgrammingError(developerMessage.capture(), cause.capture()); assertThat(developerMessage.getValue()).contains("runtime").contains("exception").contains("unhandled"); assertThat(cause.getValue()).isSameAs(exampleRuntimeException); final ArgumentCaptor<String> adminMessage = ArgumentCaptor.forClass(String.class); verify(admin).unexpectedEvent(adminMessage.capture()); assertThat(adminMessage.getValue()).contains("internal").contains("server").contains("client") .contains("connection").containsIgnoringCase("closing"); verify(client).close(); }
public void handleException(final CommunicationException exception) { if (exception instanceof NormalCloseException) { admin.informalEvent(format("The connection to the client '%s' was closed.", client)); } else if (exception instanceof AbnormalCloseException) { admin.unexpectedEvent(format("The connection to the client '%s' was closed unexpectetly.", client), exception); } else if (exception instanceof ProtocolException) { admin.unexpectedEvent( format("A violation of the communication protocol with client '%s' occurred. Closing the connection to that client.", client), exception); closeConnection(); } else if (exception instanceof MessageReadException) { admin.unexpectedEvent( format("Failed to read an expected message from the connection to the client '%s'. Closing the connection to that client.", client), exception); closeConnection(); } else { developer.unrecoverableProgrammingError( "A CommunicationException occurred that is unknown to the ExceptionHandler.", exception); admin.unexpectedEvent(format( "An unspecified error in the communication with the client '%s' occurred. Closing the connection to this client.", client)); closeConnection(); } }
ExceptionHandler { public void handleException(final CommunicationException exception) { if (exception instanceof NormalCloseException) { admin.informalEvent(format("The connection to the client '%s' was closed.", client)); } else if (exception instanceof AbnormalCloseException) { admin.unexpectedEvent(format("The connection to the client '%s' was closed unexpectetly.", client), exception); } else if (exception instanceof ProtocolException) { admin.unexpectedEvent( format("A violation of the communication protocol with client '%s' occurred. Closing the connection to that client.", client), exception); closeConnection(); } else if (exception instanceof MessageReadException) { admin.unexpectedEvent( format("Failed to read an expected message from the connection to the client '%s'. Closing the connection to that client.", client), exception); closeConnection(); } else { developer.unrecoverableProgrammingError( "A CommunicationException occurred that is unknown to the ExceptionHandler.", exception); admin.unexpectedEvent(format( "An unspecified error in the communication with the client '%s' occurred. Closing the connection to this client.", client)); closeConnection(); } } }
ExceptionHandler { public void handleException(final CommunicationException exception) { if (exception instanceof NormalCloseException) { admin.informalEvent(format("The connection to the client '%s' was closed.", client)); } else if (exception instanceof AbnormalCloseException) { admin.unexpectedEvent(format("The connection to the client '%s' was closed unexpectetly.", client), exception); } else if (exception instanceof ProtocolException) { admin.unexpectedEvent( format("A violation of the communication protocol with client '%s' occurred. Closing the connection to that client.", client), exception); closeConnection(); } else if (exception instanceof MessageReadException) { admin.unexpectedEvent( format("Failed to read an expected message from the connection to the client '%s'. Closing the connection to that client.", client), exception); closeConnection(); } else { developer.unrecoverableProgrammingError( "A CommunicationException occurred that is unknown to the ExceptionHandler.", exception); admin.unexpectedEvent(format( "An unspecified error in the communication with the client '%s' occurred. Closing the connection to this client.", client)); closeConnection(); } } ExceptionHandler(final ClientConnection client, final AdmininstratorNotifier admin,
            final DeveloperNotifier developer); }
ExceptionHandler { public void handleException(final CommunicationException exception) { if (exception instanceof NormalCloseException) { admin.informalEvent(format("The connection to the client '%s' was closed.", client)); } else if (exception instanceof AbnormalCloseException) { admin.unexpectedEvent(format("The connection to the client '%s' was closed unexpectetly.", client), exception); } else if (exception instanceof ProtocolException) { admin.unexpectedEvent( format("A violation of the communication protocol with client '%s' occurred. Closing the connection to that client.", client), exception); closeConnection(); } else if (exception instanceof MessageReadException) { admin.unexpectedEvent( format("Failed to read an expected message from the connection to the client '%s'. Closing the connection to that client.", client), exception); closeConnection(); } else { developer.unrecoverableProgrammingError( "A CommunicationException occurred that is unknown to the ExceptionHandler.", exception); admin.unexpectedEvent(format( "An unspecified error in the communication with the client '%s' occurred. Closing the connection to this client.", client)); closeConnection(); } } ExceptionHandler(final ClientConnection client, final AdmininstratorNotifier admin,
            final DeveloperNotifier developer); void handleException(final CommunicationException exception); void handleException(final RuntimeException exception); }
ExceptionHandler { public void handleException(final CommunicationException exception) { if (exception instanceof NormalCloseException) { admin.informalEvent(format("The connection to the client '%s' was closed.", client)); } else if (exception instanceof AbnormalCloseException) { admin.unexpectedEvent(format("The connection to the client '%s' was closed unexpectetly.", client), exception); } else if (exception instanceof ProtocolException) { admin.unexpectedEvent( format("A violation of the communication protocol with client '%s' occurred. Closing the connection to that client.", client), exception); closeConnection(); } else if (exception instanceof MessageReadException) { admin.unexpectedEvent( format("Failed to read an expected message from the connection to the client '%s'. Closing the connection to that client.", client), exception); closeConnection(); } else { developer.unrecoverableProgrammingError( "A CommunicationException occurred that is unknown to the ExceptionHandler.", exception); admin.unexpectedEvent(format( "An unspecified error in the communication with the client '%s' occurred. Closing the connection to this client.", client)); closeConnection(); } } ExceptionHandler(final ClientConnection client, final AdmininstratorNotifier admin,
            final DeveloperNotifier developer); void handleException(final CommunicationException exception); void handleException(final RuntimeException exception);  }