package de.ispsoft.jaxme.junit; import org.apache.log4j.Category; import junit.framework.Test; import junit.framework.TestCase; import junit.framework.TestSuite; import org.xml.sax.SAXException; import de.ispsoft.jaxme.examples.parselets.*; /**

A test for the Parselets.

* * @author Jochen Wiedmann */ public class ParseletTest extends TestCase { private static final Category cat = Category.getInstance(ParseletTest.class.getName()); public ParseletTest(String testName) { super(testName); } public static void main(java.lang.String[] args) { org.apache.log4j.Appender app = new org.apache.log4j.ConsoleAppender(new org.apache.log4j.TTCCLayout(), org.apache.log4j.ConsoleAppender.SYSTEM_ERR); cat.getRoot().addAppender(app); cat.getRoot().setPriority(org.apache.log4j.Priority.DEBUG); junit.textui.TestRunner.run(suite()); } public static Test suite() { return new TestSuite(ParseletTest.class); } public ClsMessage getMessage() throws Exception { ClsMessage m = new ClsMessage(); m.aServer = "myserver"; m.eId = new Long(2198793242L); java.util.Calendar cal = java.util.Calendar.getInstance(java.util.TimeZone.getTimeZone("UTC")); cal.set(2002, 5, 3, 0, 0, 0); m.eTime = cal.getTime(); java.util.List list = new java.util.ArrayList(); m.ePacket = list; ClsPacket p1 = new ClsPacket(); p1.eType = new Integer(37); p1.eText = "Morning, time for coffee!"; p1.eWeight = new Double(39487.34987); m.ePacket.add(p1); return m; } public String doSerialize(ClsMessage m) throws Exception { StringBuffer sb = new StringBuffer(); ClsMessageParselet mp = new ClsMessageParselet(); mp.serialize(m, sb); return sb.toString(); } public void testSerialize() throws Exception { doSerialize(getMessage()); } public void testParse() throws Exception { ClsMessage m1 = getMessage(); StringBuffer sb = new StringBuffer(doSerialize(m1)); ClsMessageParselet mp = new ClsMessageParselet(); ClsMessage m2 = (ClsMessage) mp.parse(sb); if (!m1.toXML().equals(m2.toXML())) { System.err.println("Input:"); System.err.println(m1.toXML()); System.err.println("Result:"); System.err.println(m2.toXML()); fail("Wrong result"); } } }