public byte[] pack() throws ISOException { try { return fsd.packToBytes(); } catch (Exception e) { throw new ISOException (e); } } public int unpack(byte[] b) throws ISOException {
@Test public void testPackToNoUnpadBDS() throws Throwable { Element schema = createSchema(); appendField(schema, "name", "B", "DS", 32); FSDMsg fSDMsg = new FSDMsg(SCHEMA_PREFIX); byte[] expected = "Test message ".getBytes(); fSDMsg.set("name", ISOUtil.hexString("Test message ".getBytes())); byte[] b = fSDMsg.packToBytes(); assertArrayEquals("FSDMsg.packToBytes() can't unpadding BDS fields", expected, b); }
@Test public void testPackToUnpadADS() throws Throwable { Element schema = createSchema(); appendField(schema, "name", "A", "DS", 32); FSDMsg fSDMsg = new FSDMsg(SCHEMA_PREFIX); byte[] expected = "Test message".getBytes(); fSDMsg.set("name", "Test message "); byte[] b = fSDMsg.packToBytes(); assertArrayEquals("FSDMsg.packToBytes() don't properly handle ADS unpadding", expected, b); }
@Test public void testPackASCIIPadding() throws Throwable { Element schema = createSchema(); appendField(schema, "name", "A", null, 14); FSDMsg fSDMsg = new FSDMsg(SCHEMA_PREFIX); byte[] expected = "Test message ".getBytes(); fSDMsg.set("name", "Test message"); byte[] b = fSDMsg.packToBytes(); assertArrayEquals("FSDMsg.packToBytes() don't properly padd ASCII fields", expected, b); }
@Test public void testPackToOldStyleDS() throws Throwable { Element schema = createSchema(); appendField(schema, "name", "ADS", null, 32); FSDMsg fSDMsg = new FSDMsg(SCHEMA_PREFIX); byte[] expected = "Test message".getBytes(); fSDMsg.set("name", "Test message"); byte[] b = fSDMsg.packToBytes(); assertArrayEquals("FSDMsg.packToBytes() don't properly handle old style DS", expected, b); }
@Test public void testPackNumericPadding() throws Throwable { Element schema = createSchema(); appendField(schema, "name", "N", null, 6); FSDMsg fSDMsg = new FSDMsg(SCHEMA_PREFIX); byte[] expected = "001234".getBytes(); fSDMsg.set("name", "1234"); byte[] b = fSDMsg.packToBytes(); assertArrayEquals("FSDMsg.packToBytes() don't properly padd numeric fields", expected, b); }
@Test public void testPackToADStoLong() throws Throwable { Element schema = createSchema(); appendField(schema, "name", "A", "DS", 8); FSDMsg fSDMsg = new FSDMsg(SCHEMA_PREFIX); byte[] expected = "Test mes".getBytes(); fSDMsg.set("name", "Test message"); byte[] b = fSDMsg.packToBytes(); assertArrayEquals("FSDMsg.packToBytes() don't properly truncat ADS field", expected, b); }
@Test public void testPackBinaryNoPadding() throws Throwable { Element schema = createSchema(); appendField(schema, "name", "B", null, 14); FSDMsg fSDMsg = new FSDMsg(SCHEMA_PREFIX); byte[] expected = "Test message".getBytes(); expected = ISOUtil.concat(new byte[2], expected); fSDMsg.set("name", ISOUtil.hexString("Test message".getBytes())); byte[] b = fSDMsg.packToBytes(); assertArrayEquals("FSDMsg.packToBytes() can't padd binary fields", expected, b); }
@Test public void testPackToBDStoLong() throws Throwable { Element schema = createSchema(); appendField(schema, "name", "B", "DS", 8); FSDMsg fSDMsg = new FSDMsg(SCHEMA_PREFIX); fSDMsg.set("name", ISOUtil.hexString("Test message".getBytes())); try { fSDMsg.packToBytes(); fail("FSDMsg.packToBytes() should throw RuntimeException when content is too long"); } catch (RuntimeException ex) {} }
@Test public void testPackToBytesCharset() throws Throwable { Element schema = createSchema(); appendField(schema, "name", "A", "DS", 32); FSDMsg fSDMsg = new FSDMsg(SCHEMA_PREFIX); //bytes represents "Zażółć żółtą gęś" it's a sample in polish byte[] expected = ISOUtil.hex2byte("5A61BFF3B3E620BFF3B374B12067EAB6"); Charset charset = Charset.forName("ISO-8859-2"); fSDMsg.setCharset(charset); fSDMsg.set("name", new String(expected,charset)); byte[] b = fSDMsg.packToBytes(); // System.out.println(new String(b, charset)); assertArrayEquals("FSDMsg.packToBytes() don't properly handle character encodings", expected, b); fSDMsg.unpack(b); // System.out.println(fSDMsg.get("name")); b = fSDMsg.get("name").getBytes(charset); assertArrayEquals("FSDMsg.unpack(b) don't properly handle character encodings", expected, b); } }
public void testFSDMsgDefaultKey () throws Exception { FSDMsg m0 = new FSDMsg(SCHEMA_DIR_URL + "fsd-"); FSDMsg m1 = new FSDMsg(SCHEMA_DIR_URL + "fsd-"); FSDMsg u0 = new FSDMsg(SCHEMA_DIR_URL + "fsd-"); FSDMsg u1 = new FSDMsg(SCHEMA_DIR_URL + "fsd-"); m0.set("message-id","03"); m0.set("x","X"); m0.set("y","WHYWHY03"); assertEquals ("Default defined - not used - pack", "000X0300WHYWHY03", m0.pack() ); m1.set("message-id","99"); m1.set("x","X"); m1.set("z","DEFAULT"); assertEquals ("Default defined - used - pack", "000X99DEFAULT ", m1.pack() ); u0.unpack(m0.packToBytes()); assertEquals ("Default defined - not used - unpack", "00WHYWHY03", u0.get("y") ); u1.unpack(m1.packToBytes()); assertEquals ("Default defined - used - unpack", "DEFAULT ", u1.get("z") ); }