public void testShort() throws IOException { ByteArrayOutputStream baos = new ByteArrayOutputStream(); targetPacket.writeShort((short) distinguishableLong); assertEquals("Write of first short wrote wrong number of bytes", 6, targetPacket.getSize()); targetPacket.writeShort((short) distinguishableLong); assertEquals("Write of second short wrote wrong number of bytes", 8, targetPacket.getSize()); int packetSize = targetPacket.xmit('R', new DataOutputStream(baos)); byte[] ba = baos.toByteArray(); assertEquals("Packet type character got mangled", 'R', (char) ba[0]); assertEquals( "Size header in packet is wrong", packetSize, OdbcPacketOutputStreamTest.intFromByteArray(ba, 1)); assertEquals("Retrieved data does not match stream's write count", packetSize + 1, ba.length); assertEquals( "Value of first short got mangled", (short) (distinguishableLong & 0xffff), OdbcPacketOutputStreamTest.shortFromByteArray(ba, 5)); assertEquals( "Value of second short got mangled", (short) (distinguishableLong & 0xffff), OdbcPacketOutputStreamTest.shortFromByteArray(ba, 7)); }
public void testTypeMix() throws IOException { byte[] buffer = new byte[1024]; String resPath = "/org/hsqldb/resources/odbcPacket.data"; InputStream is = OdbcPacketOutputStreamTest.class.getResourceAsStream(resPath); if (is == null) { throw new RuntimeException( "CLASSPATH not set properly. " + "Res file '" + resPath + "' not accessible"); } ByteArrayOutputStream baosA = new ByteArrayOutputStream(); ByteArrayOutputStream baosE = new ByteArrayOutputStream(); DataOutputStream dos = new DataOutputStream(baosA); int i; while ((i = is.read(buffer)) > -1) { baosE.write(buffer, 0, i); } byte[] expectedBa = baosE.toByteArray(); baosE.close(); targetPacket.writeShort((short) distinguishableLong); targetPacket.writeInt((int) distinguishableLong); targetPacket.writeLong(distinguishableLong); targetPacket.writeByte((byte) distinguishableLong); targetPacket.writeByteChar('k'); // the writeByteChar() method is explicitly not for ext. characters. int preLength = targetPacket.getSize(); targetPacket.write("Ein gro\u00df Baum\nwith blossom", false); if (targetPacket.getSize() - preLength != 27) { throw new RuntimeException("Assertion failed. Fix test because encoding size changed"); } targetPacket.write("Another string", true); targetPacket.writeSized("Ein gro\u00df Baum\nmit blossom"); targetPacket.xmit('a', dos); dos.flush(); dos.close(); byte[] actualBa = baosA.toByteArray(); /* Use this to regenerate the test data (which is also used to test * the reader). java.io.FileOutputStream fos = new java.io.FileOutputStream("/tmp/fix.bin"); fos.write(actualBa); fos.flush(); fos.close(); */ // JUnit 4.x has a built-in byte-array comparator. Until then... assertEquals("Byte stream size is wrong", expectedBa.length, actualBa.length); for (i = 0; i < expectedBa.length; i++) { assertEquals("Bye stream corruption at offset " + i, expectedBa[i], actualBa[i]); } }