@Test
  public void exceptionInProtocolClosesClientSession() throws IOException {
    i = 0;
    final Protocol protocol =
        new Protocol() {
          public void init(ClientSession session) {}

          public void parseFirst(InputStream message) throws IOException {}

          public void parsePacket(InputStream message) throws IOException {
            i++;
            throw new IOException("Test");
          }
        };

    context.checking(
        new Expectations() {
          {
            oneOf(mockedClient).close();
          }
        });

    prot.setCurrentProtocol(protocol);

    ByteArrayOutputStream out = new ByteArrayOutputStream();
    CData.writeU16(out, 2);
    CData.writeU16(out, 1337);
    prot.messageReceived(mockedClient, new ByteArrayInputStream(out.toByteArray()));

    assertEquals(1, i);

    context.assertIsSatisfied();
  }
  @Test
  public void receivePacketsSkipsUnusedData() throws IOException {
    i = 0;
    final Protocol protocol =
        new Protocol() {
          public void init(ClientSession session) {}

          public void parseFirst(InputStream message) throws IOException {}

          public void parsePacket(InputStream message) throws IOException {
            assertEquals(2, message.available());
            if (i == 1) {
              assertEquals(1338, CData.readU16(message));
            }
            i++;
          }
        };
    prot.setCurrentProtocol(protocol);

    ByteArrayOutputStream out = new ByteArrayOutputStream();
    CData.writeU16(out, 2);
    CData.writeU16(out, 1337);
    CData.writeU16(out, 2);
    CData.writeU16(out, 1338);
    prot.messageReceived(mockedClient, new ByteArrayInputStream(out.toByteArray()));

    assertEquals(2, i);
  }
  @Test
  public void disconnectsClientUponUnknownProtocol() throws IOException {
    context.checking(
        new Expectations() {
          {
            oneOf(mockedClient).close();
          }
        });

    ByteArrayOutputStream out = new ByteArrayOutputStream();
    CData.writeU16(out, 1);
    CData.writeByte(out, 123);
    prot.messageReceived(mockedClient, new ByteArrayInputStream(out.toByteArray()));

    context.assertIsSatisfied();
  }
  @Test
  public void changesToCorrectProtocol() throws IOException {
    final Protocol protocol = context.mock(Protocol.class);
    final ProtocolProvider provider = context.mock(ProtocolProvider.class);
    context.checking(
        new Expectations() {
          {
            allowing(provider).getProtocol(123);
            will(returnValue(protocol));
            oneOf(protocol).parseFirst(with(any(InputStream.class)));
            oneOf(protocol).init(mockedClient);
          }
        });
    prot.addProtocolProvider(provider);

    ByteArrayOutputStream out = new ByteArrayOutputStream();
    CData.writeU16(out, 1);
    CData.writeByte(out, 123);
    prot.messageReceived(mockedClient, new ByteArrayInputStream(out.toByteArray()));

    assertSame(protocol, prot.getCurrentProtocol());
    context.assertIsSatisfied();
  }