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.dotbetwixt;
019:
020: import java.io.StringWriter;
021:
022: import junit.framework.Test;
023: import junit.framework.TestSuite;
024:
025: import org.apache.commons.betwixt.io.BeanWriter;
026: import org.apache.commons.betwixt.strategy.HyphenatedNameMapper;
027: import org.apache.commons.betwixt.xmlunit.XmlTestCase;
028:
029: /**
030: * Provides xml test utilities.
031: * Hopefully, these might be moved into [xmlunit] sometime.
032: *
033: * @author Robert Burrell Donkin
034: */
035: public class TestBeanToXml extends XmlTestCase {
036:
037: //--------------------------------- Test Suite
038:
039: public static Test suite() {
040: return new TestSuite(TestBeanToXml.class);
041: }
042:
043: //--------------------------------- Constructor
044:
045: public TestBeanToXml(String testName) {
046: super (testName);
047: }
048:
049: //---------------------------------- Tests
050:
051: public void testOne() throws Exception {
052: // THIS TEST FAILS IN MAVEN
053: xmlAssertIsomorphicContent(
054: parseFile("src/test/org/apache/commons/betwixt/dotbetwixt/rbean-result.xml"),
055: parseFile("src/test/org/apache/commons/betwixt/dotbetwixt/rbean-result.xml"));
056: }
057:
058: public void testSimpleBean() throws Exception {
059: StringWriter out = new StringWriter();
060: out.write("<?xml version='1.0' encoding='UTF-8'?>");
061: // SimpleLog log = new SimpleLog("[testSimpleBean:XMLIntrospector]");
062: // log.setLevel(SimpleLog.LOG_LEVEL_TRACE);
063: BeanWriter writer = new BeanWriter(out);
064: writer.setWriteEmptyElements(true);
065: // writer.getXMLIntrospector().setLog(log);
066:
067: // log = new SimpleLog("[testSimpleBean:XMLIntrospectorHelper]");
068: // XMLIntrospectorHelper.setLog(log);
069:
070: writer.getBindingConfiguration().setMapIDs(false);
071: SimpleTestBean bean = new SimpleTestBean("alpha-value",
072: "beta-value", "gamma-value");
073: writer.write(bean);
074: out.flush();
075: String xml = out.toString();
076:
077: xmlAssertIsomorphicContent(
078: parseFile("src/test/org/apache/commons/betwixt/dotbetwixt/simpletestone.xml"),
079: parseString(xml));
080:
081: }
082:
083: public void testWriteRecursiveBean() throws Exception {
084: /*
085: StringWriter out = new StringWriter();
086: out.write("<?xml version='1.0' encoding='UTF-8'?>");
087: BeanWriter writer = new BeanWriter(out);
088: RecursiveBean bean
089: = new RecursiveBean(
090: "alpha",
091: new RecursiveBean(
092: "beta",
093: new RecursiveBean("gamma")));
094: writer.setWriteIDs(false);
095: writer.write(bean);
096: out.flush();
097: String xml = out.toString();
098:
099: if (debug) {
100: System.out.println(xml);
101: }
102:
103:
104: xmlAssertIsomorphicContent(
105: parseFile("src/test/org/apache/commons/betwixt/dotbetwixt/rbean-result.xml"),
106: parseString(xml));
107: */
108: }
109:
110: /**
111: * This tests that only well formed names for elements and attributes are allowed by .betwixt files
112: */
113: public void testBadDotBetwixtNames() throws Exception {
114: // this will work by testing that the output is well formed
115:
116: StringWriter out = new StringWriter();
117: out.write("<?xml version='1.0' encoding='UTF-8'?>");
118: BeanWriter writer = new BeanWriter(out);
119: writer.setWriteEmptyElements(true);
120: writer.write(new BadDotBetwixtNamesBean("one", "two"));
121:
122: // System.out.println(out.toString());
123:
124: // this should fail if the output is not well formed
125: parseString(out.toString());
126: }
127:
128: /** Test output of bean with mixed content */
129: public void testMixedContent() throws Exception {
130: StringWriter out = new StringWriter();
131: out.write("<?xml version='1.0' encoding='UTF-8'?>");
132: BeanWriter writer = new BeanWriter(out);
133: writer.getBindingConfiguration().setMapIDs(false);
134: writer.write(new MixedContentBean("First", "Last", "Always"));
135:
136: String xml = "<?xml version='1.0' encoding='UTF-8'?><foo version='1.0'>"
137: + "<bar version='First'>Fiddle sticks<baa>Last</baa>Always</bar></foo>";
138:
139: xmlAssertIsomorphicContent(parseString(xml), parseString(out
140: .toString()));
141: }
142:
143: /** Test output of bean with mixed content */
144: public void testSimpleMixedContent() throws Exception {
145: StringWriter out = new StringWriter();
146: out.write("<?xml version='1.0' encoding='UTF-8'?>");
147: BeanWriter writer = new BeanWriter(out);
148: writer.getBindingConfiguration().setMapIDs(false);
149: writer.write(new MixedContentOne("Life,",
150: "The Universe And Everything", 42));
151:
152: String xml = "<?xml version='1.0' encoding='UTF-8'?><deep-thought alpha='Life,' gamma='42'>"
153: + "The Universe And Everything</deep-thought>";
154:
155: xmlAssertIsomorphicContent(parseString(xml), parseString(out
156: .toString()));
157: }
158:
159: /** Tests basic use of an implementation for an interface */
160: public void testBasicInterfaceImpl() throws Exception {
161: ExampleBean bean = new ExampleBean("Alice");
162: bean.addExample(new ExampleImpl(1, "Mad Hatter"));
163: bean.addExample(new ExampleImpl(2, "March Hare"));
164: bean.addExample(new ExampleImpl(3, "Dormouse"));
165:
166: StringWriter out = new StringWriter();
167: out.write("<?xml version='1.0' encoding='UTF-8'?>");
168:
169: BeanWriter writer = new BeanWriter(out);
170: writer.getBindingConfiguration().setMapIDs(false);
171: writer.getXMLIntrospector().getConfiguration()
172: .setElementNameMapper(new HyphenatedNameMapper());
173: writer.getXMLIntrospector().getConfiguration()
174: .setWrapCollectionsInElement(false);
175:
176: writer.write(bean);
177:
178: String xml = "<?xml version='1.0' encoding='UTF-8'?>"
179: + "<example-bean><name>Alice</name>"
180: + "<example><id>1</id><name>Mad Hatter</name></example>"
181: + "<example><id>2</id><name>March Hare</name></example>"
182: + "<example><id>3</id><name>Dormouse</name></example>"
183: + "</example-bean>";
184:
185: xmlAssertIsomorphicContent(parseString(xml), parseString(out
186: .toString()), true);
187: }
188: }
|