### Question:
XBeePacket { public byte[] generateByteArray() { checksum.reset(); byte[] packetData = getPacketData(); ByteArrayOutputStream os = new ByteArrayOutputStream(); os.write(SpecialByte.HEADER_BYTE.getValue()); if (packetData != null) { byte[] length = ByteUtils.shortToByteArray((short)packetData.length); int msb = length[0]; int lsb = length[1]; os.write(msb); os.write(lsb); for (int i = 0; i < packetData.length; i++) { checksum.add(packetData[i]); os.write(packetData[i]); } } else { os.write(0); os.write(0); } os.write((byte)checksum.generate() & 0xFF); return os.toByteArray(); } protected  XBeePacket(); byte[] generateByteArray(); byte[] generateByteArrayEscaped(); abstract byte[] getPacketData(); int getPacketLength(); int getChecksum(); LinkedHashMap<String, String> getParameters(); @Override boolean equals(Object obj); @Override int hashCode(); @Override String toString(); String toPrettyString(); static XBeePacket parsePacket(String packet, OperatingMode mode); static XBeePacket parsePacket(byte[] packet, OperatingMode mode);  }### Answer:
@Test public final void testGenerateByteArrayNullData() { TestXBeePacket packet = new TestXBeePacket(); byte[] expectedByteArray = new byte[] {(byte)SpecialByte.HEADER_BYTE.getValue(), 0x00, 0x00,(byte)0xFF}; byte[] data = packet.generateByteArray(); assertThat("Returned byte array cannot be null", data, is(not(equalTo(null)))); assertThat("Returned byte array length is not the expected", data.length, is(equalTo(expectedByteArray.length))); assertThat("Returned byte array is not the expected", data, is(equalTo(expectedByteArray))); }
@Test public final void testGenerateByteArrayEmptyData() { TestXBeePacket packet = new TestXBeePacket() { public byte[] getPacketData() { return new byte[0]; } }; byte[] expectedByteArray = new byte[] {(byte)SpecialByte.HEADER_BYTE.getValue(), 0x00, 0x00,(byte)0xFF}; byte[] data = packet.generateByteArray(); assertThat("Returned byte array cannot be null", data, is(not(equalTo(null)))); assertThat("Returned byte array length is not the expected", data.length, is(equalTo(expectedByteArray.length))); assertThat("Returned byte array is not the expected", data, is(equalTo(expectedByteArray))); }
@Test public final void testGenerateByteArray() { final byte[] dataArray = new byte[]{0x08, 0x01, 0x4E, 0x49, 0x4E, 0x61, 0x6D, 0x65}; TestXBeePacket packet = new TestXBeePacket() { public byte[] getPacketData() { return dataArray; } }; byte[] expectedByteArray = new byte[dataArray.length + 4]; expectedByteArray[0] = (byte)SpecialByte.HEADER_BYTE.getValue(); expectedByteArray[1] = 0x00; expectedByteArray[2] = 0x08; System.arraycopy(dataArray, 0, expectedByteArray, 3, dataArray.length); expectedByteArray[expectedByteArray.length - 1] = (byte)0xDE; byte[] data = packet.generateByteArray(); assertThat("Returned byte array cannot be null", data, is(not(equalTo(null)))); assertThat("Returned byte array length is not the expected", data.length, is(equalTo(expectedByteArray.length))); assertThat("Returned byte array is not the expected", data, is(equalTo(expectedByteArray))); }