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]); } }
public void testString() throws IOException { /** TODO: Test high-order characters */ ByteArrayOutputStream baos = new ByteArrayOutputStream(); String testString1 = "Ein groß Baum\nwith blossom"; String testString2 = "Another string"; targetPacket.write(testString1, false); targetPacket.write(testString2, false); targetPacket.xmit('R', new DataOutputStream(baos)); byte[] ba = baos.toByteArray(); assertEquals("Packet type character got mangled", 'R', (char) ba[0]); BufferedReader utfReader = new BufferedReader( new InputStreamReader(new ByteArrayInputStream(ba, 5, ba.length - 5), "UTF-8")); assertTrue("Packet did not provide a good character stream (1)", utfReader.ready()); char[] ca = new char[100]; int charsRead = utfReader.read(ca); assertEquals( "A test string got mangled (length mismatch)", (testString1 + testString2).length(), charsRead); assertEquals( "A test string got mangled", testString1 + testString2, new String(ca, 0, charsRead)); assertFalse("Packet has extra stuff after character stream", utfReader.ready()); }