### Question:
XMLPackager extends DefaultHandler implements ISOPackager, LogSource { public byte[] pack (ISOComponent c) throws ISOException { LogEvent evt = new LogEvent (this, "pack"); try { if (!(c instanceof ISOMsg)) throw new ISOException ("cannot pack "+c.getClass()); ISOMsg m = (ISOMsg) c; byte[] b; synchronized (this) { m.setDirection(0); m.dump (p, ""); b = out.toByteArray(); out.reset(); } if (logger != null) evt.addMessage (m); return b; } catch (ISOException e) { evt.addMessage (e); throw e; } finally { Logger.log(evt); } } XMLPackager(); void forceBinary(int ... bfields); byte[] pack(ISOComponent c); synchronized int unpack(ISOComponent c, byte[] b); synchronized void unpack(ISOComponent c, InputStream in); void startElement(String ns, String name, String qName, Attributes atts); void characters(char ch[], int start, int length); void endElement(String ns, String name, String qname); String getFieldDescription(ISOComponent m, int fldNumber); void setLogger(Logger logger, String realm); String getRealm(); Logger getLogger(); ISOMsg createISOMsg(); String getDescription(); void setXMLParserFeature(String fname, boolean val); static final String ISOMSG_TAG; static final String ISOFIELD_TAG; static final String ID_ATTR; static final String VALUE_ATTR; static final String TYPE_ATTR; static final String TYPE_BINARY; static final String TYPE_BITMAP; static final String TYPE_AMOUNT; static final String CURRENCY_ATTR; static final String HEADER_TAG; static final String ENCODING_ATTR; static final String ASCII_ENCODING; }### Answer:
@Test public void testPackException() { ISOField field = new ISOField(); try { xMLPackager.pack(field); fail("Exception Expected - not an isomsg"); } catch (ISOException e) { assertThat(e.getMessage(), is("cannot pack class org.jpos.iso.ISOField")); } }
@Test public void testPack() throws IOException, ISOException, SAXException { isoMsg.setMTI("0800"); isoMsg.set(7, "7654321"); isoMsg.set(11, "12345678"); isoMsg.set(12, "20110224112759"); isoMsg.set(24, ""); byte[] data = isoMsg.pack(); String expected = "<isomsg><!-- org.jpos.iso.packager.XMLPackager --><field id=\"0\" value=\"0800\"/>" + "<field id=\"7\" value=\"7654321\"/><field id=\"11\" value=\"12345678\"/>" + "<field id=\"12\" value=\"20110224112759\"/><field id=\"24\" value=\"\"/></isomsg>"; XMLUnit.setIgnoreWhitespace(true); DetailedDiff myDiff = new DetailedDiff(XMLUnit.compareXML(expected, new String(data))); List allDifferences = myDiff.getAllDifferences(); assertEquals(0, allDifferences.size(), myDiff.toString()); }