1   /*
2    * Copyright 2001-2004 The Apache Software Foundation.
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */ 
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  /***
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.3 2004/03/31 21:11:53 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       * @see TestCase#setUp()
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         // install request marshall/unmarshall
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          // Betwixt just writes out the bean as a fragment
93          // we want well-formed xml, we need to add the prolog
94          writer.write(XML_PROLOG);
95  
96          // Create a BeanWriter which writes to our prepared stream
97          BeanWriter beanWriter = new BeanWriter(writer);
98  
99          // Configure betwixt
100         // For more details see java docs or later in the main documentation
101         beanWriter.getXMLIntrospector().setAttributesForPrimitives(true);
102         beanWriter.setWriteIDs(false);
103         beanWriter.enablePrettyPrint();
104 
105         // Write example bean as base element 'person'
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         // Configure the reader
125         beanReader.getXMLIntrospector().setAttributesForPrimitives(true);
126         // Register beans so that betwixt knows what the xml is 
127         beanReader.registerBeanClass("message", MsgBean.class);
128         StringReader stringReader = new StringReader(xmlMessage);
129         return  (MsgBean) beanReader.parse(stringReader);
130     }
131     
132     
133 
134 }