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:
018: package org.apache.commons.betwixt.nowrap;
019:
020: import java.io.File;
021: import java.io.FileInputStream;
022: import java.io.StringWriter;
023: import java.util.List;
024:
025: import junit.framework.Test;
026: import junit.framework.TestSuite;
027:
028: import org.apache.commons.betwixt.AbstractTestCase;
029: import org.apache.commons.betwixt.XMLIntrospector;
030: import org.apache.commons.betwixt.io.BeanReader;
031: import org.apache.commons.betwixt.io.BeanWriter;
032: import org.apache.commons.betwixt.strategy.DecapitalizeNameMapper;
033: import org.apache.commons.betwixt.strategy.DefaultPluralStemmer;
034:
035: /**
036: * Test harness for the base PO object
037: *
038: * @author <a href="mailto:john@zenplex.com">John Thorhauer</a>
039: * @version $Id: TestNoWrap.java 438373 2006-08-30 05:17:21Z bayard $
040: */
041: public class TestNoWrap extends AbstractTestCase {
042: private POTest po;
043:
044: /**
045: * A unit test suite for JUnit
046: */
047: public static Test suite() {
048: return new TestSuite(TestNoWrap.class);
049: }
050:
051: /**
052: * Constructor for the TestScarabSettings object
053: *
054: * @param testName
055: */
056: public TestNoWrap(String testName) {
057: super (testName);
058: }
059:
060: /**
061: * Description of the Method
062: */
063: public void testRoundTrip() throws Exception {
064: load();
065: write();
066: }
067:
068: /**
069: * Description of the Method
070: */
071: public void load() throws Exception {
072: String xmlLocation = getTestFile("src/test/org/apache/commons/betwixt/nowrap/po_add_test.xml");
073:
074: FileInputStream in = new FileInputStream(new File(xmlLocation));
075:
076: // create a new BeanReader
077: BeanReader reader = createBeanReader(POTest.class);
078: po = (POTest) reader.parse(in);
079: assertEquals("PO Printing No", "555008805581", po
080: .getPrintingNumber());
081: List componentTests = po.getComponenttests();
082:
083: assertEquals("#Component tests", 3, componentTests.size());
084: Componenttest testOne = (Componenttest) componentTests.get(0);
085: assertEquals("Component Test One", "Text", testOne
086: .getCompDescription());
087: Componenttest testTwo = (Componenttest) componentTests.get(1);
088: assertEquals("Component Test Two", "Binding", testTwo
089: .getCompDescription());
090: Componenttest testThree = (Componenttest) componentTests.get(2);
091: assertEquals("Component Test Three", "Paper Cover", testThree
092: .getCompDescription());
093: }
094:
095: /**
096: * Description of the Method
097: */
098: public void write() throws Exception {
099: // Let's try to write the bean
100: StringWriter out = new StringWriter();
101: out.write("<?xml version='1.0'?>");
102: BeanWriter beanWriter = new BeanWriter(out);
103: beanWriter.setXMLIntrospector(createXMLIntrospector());
104: beanWriter.getBindingConfiguration().setMapIDs(false);
105: beanWriter.setEndOfLine("\n");
106: beanWriter.enablePrettyPrint();
107:
108: beanWriter.write(po);
109: String xml = "<?xml version='1.0'?><content><printingno>555008805581</printingno>"
110: + "<componenttest><compdescription>Text</compdescription></componenttest>"
111: + "<componenttest><compdescription>Binding</compdescription></componenttest>"
112: + "<componenttest><compdescription>Paper Cover</compdescription>"
113: + "</componenttest></content>";
114:
115: xmlAssertIsomorphicContent(parseString(xml), parseString(out
116: .getBuffer().toString()), true);
117: }
118:
119: // Implementation methods
120: //-------------------------------------------------------------------------
121:
122: /**
123: * Description of the Method
124: */
125: protected BeanReader createBeanReader(Class beanClass)
126: throws Exception {
127: BeanReader reader = new BeanReader();
128: reader.setXMLIntrospector(createXMLIntrospector());
129: reader.registerBeanClass(beanClass);
130: return reader;
131: }
132:
133: /**
134: * ### it would be really nice to move this somewhere shareable across Maven
135: * / Turbine projects. Maybe a static helper method - question is what to
136: * call it???
137: */
138: protected XMLIntrospector createXMLIntrospector() {
139: XMLIntrospector introspector = new XMLIntrospector();
140:
141: // set elements for attributes to true
142: introspector.getConfiguration().setAttributesForPrimitives(
143: false);
144:
145: // wrap collections in an XML element
146: introspector.getConfiguration().setWrapCollectionsInElement(
147: false);
148:
149: // turn bean elements first letter into lower case
150: introspector.getConfiguration().setElementNameMapper(
151: new DecapitalizeNameMapper());
152:
153: // Set default plural stemmer.
154: introspector.getConfiguration().setPluralStemmer(
155: new DefaultPluralStemmer());
156:
157: return introspector;
158: }
159: }
|