PrivateMessageParser implements ReceiverListener { @Override public void messageArrived(final String message, final String ipAddress) { try { final int exclamation = message.indexOf("!"); final int hash = message.indexOf("#"); final int colon = message.indexOf(":"); final int fromCode = Integer.parseInt(message.substring(0, exclamation)); final String type = message.substring(exclamation + 1, hash); final String msg = message.substring(colon + 1, message.length()); final int leftPara = msg.indexOf("("); final int rightPara = msg.indexOf(")"); final int toCode = Integer.parseInt(msg.substring(leftPara + 1, rightPara)); final User tempme = settings.getMe(); if (fromCode != tempme.getCode() && toCode == tempme.getCode()) { if (type.equals(PRIVMSG)) { final int leftBracket = msg.indexOf("["); final int rightBracket = msg.indexOf("]"); final int rgb = Integer.parseInt(msg.substring(leftBracket + 1, rightBracket)); final String privmsg = msg.substring(rightBracket + 1, msg.length()); privmsgResponder.messageArrived(fromCode, privmsg, rgb); } } } catch (final StringIndexOutOfBoundsException e) { LOG.log(Level.SEVERE, "Failed to parse message. message=" + message + ", ipAddress=" + ipAddress, e); } catch (final NumberFormatException e) { LOG.log(Level.SEVERE, "Failed to parse message. message=" + message + ", ipAddress=" + ipAddress, e); } } PrivateMessageParser(final PrivateMessageResponder privmsgResponder, final Settings settings); @Override void messageArrived(final String message, final String ipAddress);  }
@Test public void messageArrivedShouldLogIfUnableToFindNecessaryDetailsInMessage() { privateMessageParser.messageArrived("Error", "192.168.1.2"); final ArgumentCaptor<Exception> exceptionCaptor = ArgumentCaptor.forClass(Exception.class); verify(log).log(eq(Level.SEVERE), eq("Failed to parse message. message=Error, ipAddress=192.168.1.2"), exceptionCaptor.capture()); checkException(exceptionCaptor, StringIndexOutOfBoundsException.class, null); }
@Test public void messageArrivedShouldLogIfUnableToParseUserCodeInMessage() { privateMessageParser.messageArrived("a12516938!PRIVMSG#12516938:(18737868)[-6750208]hello", "192.168.1.2"); final ArgumentCaptor<Exception> exceptionCaptor = ArgumentCaptor.forClass(Exception.class); verify(log).log(eq(Level.SEVERE), eq("Failed to parse message. " + "message=a12516938!PRIVMSG#12516938:(18737868)[-6750208]hello, " + "ipAddress=192.168.1.2"), exceptionCaptor.capture()); checkException(exceptionCaptor, NumberFormatException.class, "For input string: \"a12516938\""); }