1 16 package org.apache.commons.betwixt.dotbetwixt; 17 18 import java.io.StringReader ; 19 import java.io.StringWriter ; 20 21 import junit.framework.TestCase; 22 23 import org.apache.commons.betwixt.io.BeanReader; 24 import org.apache.commons.betwixt.io.BeanWriter; 25 33 public class TestMsgParser extends TestCase 34 { 35 private static final String XML_PROLOG = "<?xml version='1.0' ?>\n"; 36 private MsgBean msg; 37 38 41 protected void setUp() throws Exception 42 { 43 msg = new MsgBean(); 44 msg.setDescription("Some simple descriptive text"); 45 msg.setToAddress("mike@somewhere.com"); 46 msg.setFromAddress("debbie@somwhere.com"); 47 msg.setName("basicMsg"); 48 msg.setOptionalField1("7-12-99"); 49 msg.setOptionalField2("true"); 50 msg.setStatus("sent"); 51 msg.setType("spam"); 52 } 53 54 public void testGetAsXml() throws Exception 55 { 56 String xmlMsg = null; 57 xmlMsg = getAsXml(msg); 58 assertNotNull("XML String should not be null", xmlMsg); 59 60 } 61 62 public void testParseMsg() throws Exception 63 { 64 MsgBean newMsg = null; 65 String xmlMsg = getAsXml(msg); 67 newMsg = parseMsg(xmlMsg); 68 69 assertNotNull("new MsgBean should not be null.", newMsg); 70 assertEquals( msg.getDescription(), newMsg.getDescription() ); 71 assertEquals( msg.getFromAddress(), newMsg.getFromAddress() ); 72 assertEquals( msg.getName(), newMsg.getName() ); 73 assertEquals( msg.getOptionalField1(), newMsg.getOptionalField1() ); 74 assertEquals( msg.getOptionalField2(), newMsg.getOptionalField2() ); 75 assertEquals( msg.getStatus(), newMsg.getStatus() ); 76 assertEquals( msg.getToAddress(), newMsg.getToAddress() ); 77 assertEquals( msg.getType(), newMsg.getType() ); 78 } 79 80 87 public static final String getAsXml(MsgBean msg) 88 throws Exception 89 { 90 StringWriter writer = new StringWriter (); 91 92 writer.write(XML_PROLOG); 95 96 BeanWriter beanWriter = new BeanWriter(writer); 98 99 beanWriter.getXMLIntrospector().getConfiguration().setAttributesForPrimitives(true); 102 beanWriter.getBindingConfiguration().setMapIDs(false); 103 beanWriter.enablePrettyPrint(); 104 105 beanWriter.write("message", msg); 107 beanWriter.flush(); 108 109 return writer.toString(); 110 } 111 112 119 public static final MsgBean parseMsg(String xmlMessage) 120 throws Exception 121 { 122 MsgBean msg = null; 123 BeanReader beanReader = new BeanReader(); 124 beanReader.getXMLIntrospector().getConfiguration().setAttributesForPrimitives(true); 126 beanReader.registerBeanClass("message", MsgBean.class); 128 StringReader stringReader = new StringReader (xmlMessage); 129 return (MsgBean) beanReader.parse(stringReader); 130 } 131 132 133 134 } 135 | Popular Tags |