001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.commons.betwixt.dotbetwixt;
018:
019: import java.io.StringReader;
020: import java.io.StringWriter;
021:
022: import junit.framework.TestCase;
023:
024: import org.apache.commons.betwixt.io.BeanReader;
025: import org.apache.commons.betwixt.io.BeanWriter;
026:
027: /**
028: * Tests the marshalling and unmarshalling of MsgBeans with Betwixt.
029: * The problem tested here is that an element without an updater would
030: * not process it's attributes correctly even though they had updaters.
031: *
032: * @author <a href="mstanley@cauldronsolutions.com">Mike Stanley</a>
033: * @version $Id: TestMsgParser.java 438373 2006-08-30 05:17:21Z bayard $
034: */
035: public class TestMsgParser extends TestCase {
036: private static final String XML_PROLOG = "<?xml version='1.0' ?>\n";
037: private MsgBean msg;
038:
039: /*
040: * @see TestCase#setUp()
041: */
042: protected void setUp() throws Exception {
043: msg = new MsgBean();
044: msg.setDescription("Some simple descriptive text");
045: msg.setToAddress("mike@somewhere.com");
046: msg.setFromAddress("debbie@somwhere.com");
047: msg.setName("basicMsg");
048: msg.setOptionalField1("7-12-99");
049: msg.setOptionalField2("true");
050: msg.setStatus("sent");
051: msg.setType("spam");
052: }
053:
054: public void testGetAsXml() throws Exception {
055: String xmlMsg = null;
056: xmlMsg = getAsXml(msg);
057: assertNotNull("XML String should not be null", xmlMsg);
058:
059: }
060:
061: public void testParseMsg() throws Exception {
062: MsgBean newMsg = null;
063: // install request marshall/unmarshall
064: String xmlMsg = getAsXml(msg);
065: newMsg = parseMsg(xmlMsg);
066:
067: assertNotNull("new MsgBean should not be null.", newMsg);
068: assertEquals(msg.getDescription(), newMsg.getDescription());
069: assertEquals(msg.getFromAddress(), newMsg.getFromAddress());
070: assertEquals(msg.getName(), newMsg.getName());
071: assertEquals(msg.getOptionalField1(), newMsg
072: .getOptionalField1());
073: assertEquals(msg.getOptionalField2(), newMsg
074: .getOptionalField2());
075: assertEquals(msg.getStatus(), newMsg.getStatus());
076: assertEquals(msg.getToAddress(), newMsg.getToAddress());
077: assertEquals(msg.getType(), newMsg.getType());
078: }
079:
080: /**
081: * Returns the bean as an xml string.
082: *
083: * @param msg
084: * @return
085: * @throws Exception
086: */
087: public static final String getAsXml(MsgBean msg) throws Exception {
088: StringWriter writer = new StringWriter();
089:
090: // Betwixt just writes out the bean as a fragment
091: // we want well-formed xml, we need to add the prolog
092: writer.write(XML_PROLOG);
093:
094: // Create a BeanWriter which writes to our prepared stream
095: BeanWriter beanWriter = new BeanWriter(writer);
096:
097: // Configure betwixt
098: // For more details see java docs or later in the main documentation
099: beanWriter.getXMLIntrospector().getConfiguration()
100: .setAttributesForPrimitives(true);
101: beanWriter.getBindingConfiguration().setMapIDs(false);
102: beanWriter.setEndOfLine("\n");
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: MsgBean msg = null;
122: BeanReader beanReader = new BeanReader();
123: // Configure the reader
124: beanReader.getXMLIntrospector().getConfiguration()
125: .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: }
|