### Question:
MessageOutputStream extends DataOutputStream { public void writeMessage(Message message) throws IOException { if (closed) { throw new IOException("Cannot write message: stream closed"); } Object[] args = message.getArgs(); MessageType messageType = messageRegistry.getMessageType(message.getID()); List<ArgumentType> argumentTypes = messageType.getArguments(); int id = message.getID(); writeShort(id); for (int i = 0; i < args.length; i++) writeArgument(args[i], argumentTypes.get(i).isEncrypt()); } MessageOutputStream(OutputStream out); void writeMessage(Message message); boolean isClosed(); void setPublicKey(Key publicKey); @Override void close();  }### Answer:
@Test public void testWriteMessage() throws Exception { ByteArrayOutputStream bos = new ByteArrayOutputStream(); MessageOutputStream mos = new MessageOutputStream(bos); Message msg = MESSAGE_REGISTRY.createMessage("InvalidMessage"); msg.setArgument("messageID", (short) -255); msg.setArgument("messageName", "FooBar"); mos.writeMessage(msg); byte[] bytes = bos.toByteArray(); mos.close(); ByteArrayInputStream bis = new ByteArrayInputStream(bytes); MessageInputStream mis = new MessageInputStream(bis); Message readMsg = mis.readMessage(); Assert.assertEquals(readMsg.getArgs(), msg.getArgs(), "Message arguments not equal."); }
@Test(expectedExceptions = IOException.class) public void testEncryptedMessageNoPublicKey() throws Exception { ByteArrayOutputStream bos = new ByteArrayOutputStream(); MessageOutputStream mos = new MessageOutputStream(bos); Message msg = MESSAGE_REGISTRY.createMessage("TestEncryptedMessage"); msg.setArgument("encryptedString", "FooBar"); mos.writeMessage(msg); }