1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    * 
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   * 
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */ 
17   package org.apache.commons.betwixt.dotbetwixt;
18  
19  import java.io.StringReader;
20  import java.io.StringWriter;
21  
22  import junit.framework.TestCase;
23  
24  import org.apache.commons.betwixt.io.BeanReader;
25  import org.apache.commons.betwixt.io.BeanWriter;
26  /**
27   * Tests the marshalling and unmarshalling of MsgBeans with Betwixt.
28   * The problem tested here is that an element without an updater would
29   * not process it's attributes correctly even though they had updaters.
30   * 
31   * @author <a href="mstanley@cauldronsolutions.com">Mike Stanley</a>
32   * @version $Id: TestMsgParser.java 438373 2006-08-30 05:17:21Z bayard $
33   */
34  public class TestMsgParser extends TestCase
35  {
36      private static final String XML_PROLOG = "<?xml version='1.0' ?>\n";
37      private MsgBean msg;
38  
39      /*
40       * @see TestCase#setUp()
41       */
42      protected void setUp() throws Exception
43      {
44          msg = new MsgBean();
45          msg.setDescription("Some simple descriptive text");
46          msg.setToAddress("mike@somewhere.com");
47          msg.setFromAddress("debbie@somwhere.com");
48          msg.setName("basicMsg");
49          msg.setOptionalField1("7-12-99");
50          msg.setOptionalField2("true");
51          msg.setStatus("sent");
52          msg.setType("spam");
53      }
54  
55      public void testGetAsXml() throws Exception
56      {
57          String xmlMsg = null;
58          xmlMsg = getAsXml(msg);            
59          assertNotNull("XML String should not be null", xmlMsg);
60          
61      }
62  
63      public void testParseMsg() throws Exception
64      {
65          MsgBean newMsg = null;
66         // install request marshall/unmarshall
67         String xmlMsg = getAsXml(msg);
68         newMsg = parseMsg(xmlMsg);
69  
70         assertNotNull("new MsgBean should not be null.", newMsg);
71         assertEquals( msg.getDescription(), newMsg.getDescription() );
72         assertEquals( msg.getFromAddress(), newMsg.getFromAddress() );
73         assertEquals( msg.getName(), newMsg.getName() );
74         assertEquals( msg.getOptionalField1(), newMsg.getOptionalField1() );
75         assertEquals( msg.getOptionalField2(), newMsg.getOptionalField2() );
76         assertEquals( msg.getStatus(), newMsg.getStatus() );
77         assertEquals( msg.getToAddress(), newMsg.getToAddress() );
78         assertEquals( msg.getType(), newMsg.getType() );
79      }
80      
81      /**
82       * Returns the bean as an xml string.
83       * 
84       * @param msg
85       * @return
86       * @throws Exception
87       */
88      public static final String getAsXml(MsgBean msg) 
89      throws Exception
90      {
91          StringWriter writer = new StringWriter();
92  
93          // Betwixt just writes out the bean as a fragment
94          // we want well-formed xml, we need to add the prolog
95          writer.write(XML_PROLOG);
96  
97          // Create a BeanWriter which writes to our prepared stream
98          BeanWriter beanWriter = new BeanWriter(writer);
99  
100         // Configure betwixt
101         // For more details see java docs or later in the main documentation
102         beanWriter.getXMLIntrospector().getConfiguration().setAttributesForPrimitives(true);
103         beanWriter.getBindingConfiguration().setMapIDs(false);
104         beanWriter.setEndOfLine("\n");
105         beanWriter.enablePrettyPrint();
106 
107         // Write example bean as base element 'person'
108         beanWriter.write("message", msg);
109         beanWriter.flush();
110 
111         return writer.toString();
112     }
113     
114     /**
115      * Parses the passed in message xml
116      * 
117      * @param xmlMessage
118      * @return
119      * @throws Exception
120      */
121     public static final MsgBean parseMsg(String xmlMessage)
122         throws Exception
123     {
124         MsgBean msg = null;
125         BeanReader beanReader = new BeanReader();
126         // Configure the reader
127         beanReader.getXMLIntrospector().getConfiguration().setAttributesForPrimitives(true);
128         // Register beans so that betwixt knows what the xml is 
129         beanReader.registerBeanClass("message", MsgBean.class);
130         StringReader stringReader = new StringReader(xmlMessage);
131         return  (MsgBean) beanReader.parse(stringReader);
132     }
133     
134     
135 
136 }