1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org/apache/commons/betwixt/dotbetwixt/package-summary.html">> 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 /***
26 * Tests the marshalling and unmarshalling of MsgBeans with Betwixt.
27 * The problem tested here is that an element without an updater would
28 * not process it's attributes correctly even though they had updaters.
29 *
30 * @author <a href="mstanley@cauldronsolutions.com">Mike Stanley</a>
31 * @version $Id: TestMsgParser.java,v 1.4 2004/06/13 21:32:47 rdonkin Exp $
32 */
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
39
40
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
66 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 /***
81 * Returns the bean as an xml string.
82 *
83 * @param msg
84 * @return
85 * @throws Exception
86 */
87 public static final String getAsXml(MsgBean msg)
88 throws Exception
89 {
90 StringWriter writer = new StringWriter();
91
92
93
94 writer.write(XML_PROLOG);
95
96
97 BeanWriter beanWriter = new BeanWriter(writer);
98
99
100
101 beanWriter.getXMLIntrospector().getConfiguration().setAttributesForPrimitives(true);
102 beanWriter.getBindingConfiguration().setMapIDs(false);
103 beanWriter.enablePrettyPrint();
104
105
106 beanWriter.write("message", msg);
107 beanWriter.flush();
108
109 return writer.toString();
110 }
111
112 /***
113 * Parses the passed in message xml
114 *
115 * @param xmlMessage
116 * @return
117 * @throws Exception
118 */
119 public static final MsgBean parseMsg(String xmlMessage)
120 throws Exception
121 {
122 MsgBean msg = null;
123 BeanReader beanReader = new BeanReader();
124
125 beanReader.getXMLIntrospector().getConfiguration().setAttributesForPrimitives(true);
126
127 beanReader.registerBeanClass("message", MsgBean.class);
128 StringReader stringReader = new StringReader(xmlMessage);
129 return (MsgBean) beanReader.parse(stringReader);
130 }
131
132
133
134 }