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.io.read;
018:
019: import java.io.StringReader;
020: import java.io.StringWriter;
021: import java.util.ArrayList;
022:
023: import junit.framework.Test;
024: import junit.framework.TestSuite;
025:
026: import org.apache.commons.betwixt.AbstractTestCase;
027: import org.apache.commons.betwixt.io.BeanReader;
028: import org.apache.commons.betwixt.io.BeanWriter;
029:
030: /**
031: * Test harness for bean creation (during reading).
032: *
033: * @author Robert Burrell Donkin
034: * @version $Id: TestBeanCreation.java 438373 2006-08-30 05:17:21Z bayard $
035: */
036: public class TestBeanCreation extends AbstractTestCase {
037:
038: public TestBeanCreation(String name) {
039: super (name);
040: }
041:
042: public static Test suite() {
043: return new TestSuite(TestBeanCreation.class);
044: }
045:
046: public void testCustomCreatorOne() throws Exception {
047: HouseBeans houses = new HouseBeans();
048: HouseBean houseOne = new HouseBean();
049: houseOne.setFacing(CompassPoint.NORTH);
050: houseOne.setAddress(new AddressBean("Black Bull, 46 Briggate",
051: "Brighouse", "England", "HD6 1EF"));
052: houseOne.setHouseholder(new PersonBean("Samual", "Smith"));
053: houseOne.setTenant(false);
054: houses.addHouse(houseOne);
055: HouseBean houseTwo = new HouseBean();
056: houseTwo.setFacing(CompassPoint.SOUTH);
057: houseTwo.setAddress(new AddressBean(
058: "The Commerical Inn, 1 Gooder Lane", "Brighouse",
059: "England", "HD6 1HT"));
060: houseTwo.setHouseholder(new PersonBean("Timothy", "Tayler"));
061: houseTwo.setTenant(true);
062: houses.addHouse(houseTwo);
063:
064: StringWriter out = new StringWriter();
065: out.write("<?xml version='1.0'?>");
066: BeanWriter writer = new BeanWriter(out);
067: writer.getBindingConfiguration().setMapIDs(false);
068: writer.getXMLIntrospector().getConfiguration()
069: .setAttributesForPrimitives(true);
070: writer.getXMLIntrospector().getConfiguration()
071: .setWrapCollectionsInElement(false);
072: writer.write("houses", houses);
073:
074: String xml = "<?xml version='1.0'?><houses>"
075: + "<house tenant='false'>"
076: + "<address street='Black Bull, 46 Briggate' city='Brighouse' country='England' code='HD6 1EF'/>"
077: + "<householder forename='Samual' surname='Smith'/>"
078: + "<facing name='North'/>"
079: + "</house>"
080: + "<house tenant='true'>"
081: + "<address street='The Commerical Inn, 1 Gooder Lane' city='Brighouse'"
082: + " country='England' code='HD6 1HT'/>"
083: + "<householder forename='Timothy' surname='Tayler'/>"
084: + "<facing name='South'/>" + "</house>" + "</houses>";
085:
086: xmlAssertIsomorphic(parseString(xml), parseString(out
087: .toString()), true);
088:
089: BeanCreationList chain = BeanCreationList.createStandardChain();
090: // add a filter that creates enums to the start
091:
092: class EnumCreator implements ChainedBeanCreator {
093:
094: public Object create(ElementMapping mapping,
095: ReadContext context, BeanCreationChain chain) {
096: if ("facing".equals(mapping.getName())) {
097: String value = mapping.getAttributes().getValue(
098: "name");
099: if ("North".equals(value)) {
100: return CompassPoint.NORTH;
101: }
102: if ("South".equals(value)) {
103: return CompassPoint.SOUTH;
104: }
105: if ("East".equals(value)) {
106: return CompassPoint.EAST;
107: }
108: if ("West".equals(value)) {
109: return CompassPoint.WEST;
110: }
111: }
112: return chain.create(mapping, context);
113: }
114: }
115: chain.insertBeanCreator(1, new EnumCreator());
116:
117: BeanReader reader = new BeanReader();
118: reader.getXMLIntrospector().getConfiguration()
119: .setAttributesForPrimitives(true);
120: reader.getXMLIntrospector().getConfiguration()
121: .setWrapCollectionsInElement(false);
122: reader.registerBeanClass("houses", HouseBeans.class);
123: reader.getReadConfiguration().setBeanCreationChain(chain);
124:
125: StringReader in = new StringReader(xml);
126: HouseBeans newHouses = (HouseBeans) reader.parse(in);
127: assertNotNull("Parsing should return a bean", newHouses);
128:
129: ArrayList houseList = newHouses.houses;
130: assertEquals("Should be two houses read", 2, houseList.size());
131: HouseBean newOne = (HouseBean) houseList.get(0);
132: HouseBean newTwo = (HouseBean) houseList.get(1);
133: assertEquals("First house is equal", houseOne, newOne);
134: assertEquals("Second house is equal", houseTwo, newTwo);
135:
136: }
137:
138: public void testCustomCreatorTwo() throws Exception {
139: HouseBeans houses = new HouseBeans();
140: HouseBean houseOne = new HouseBean();
141: houseOne.setFacing(CompassPoint.NORTH);
142: houseOne.setAddress(new AddressBean("Black Bull, 46 Briggate",
143: "Brighouse", "England", "HD6 1EF"));
144: houseOne.setHouseholder(new PersonBean("Samual", "Smith"));
145: houseOne.setTenant(false);
146: houses.addHouse(houseOne);
147: HouseBean houseTwo = new HouseBean();
148: houseTwo.setFacing(CompassPoint.SOUTH);
149: houseTwo.setAddress(new AddressBean(
150: "The Commerical Inn, 1 Gooder Lane", "Brighouse",
151: "England", "HD6 1HT"));
152: houseTwo.setHouseholder(new PersonBean("Timothy", "Tayler"));
153: houseTwo.setTenant(true);
154: houses.addHouse(houseTwo);
155:
156: StringWriter out = new StringWriter();
157: out.write("<?xml version='1.0'?>");
158: BeanWriter writer = new BeanWriter(out);
159: writer.getBindingConfiguration().setMapIDs(false);
160: writer.getXMLIntrospector().getConfiguration()
161: .setAttributesForPrimitives(true);
162: writer.getXMLIntrospector().getConfiguration()
163: .setWrapCollectionsInElement(false);
164: writer.write("houses", houses);
165:
166: String xml = "<?xml version='1.0'?><houses>"
167: + "<house tenant='false'>"
168: + "<address street='Black Bull, 46 Briggate' city='Brighouse' country='England' code='HD6 1EF'/>"
169: + "<householder forename='Samual' surname='Smith'/>"
170: + "<facing name='North'/>"
171: + "</house>"
172: + "<house tenant='true'>"
173: + "<address street='The Commerical Inn, 1 Gooder Lane' city='Brighouse'"
174: + " country='England' code='HD6 1HT'/>"
175: + "<householder forename='Timothy' surname='Tayler'/>"
176: + "<facing name='South'/>" + "</house>" + "</houses>";
177:
178: xmlAssertIsomorphic(parseString(xml), parseString(out
179: .toString()), true);
180:
181: BeanCreationList chain = BeanCreationList.createStandardChain();
182: // add a filter that creates enums to the start
183:
184: class EnumCreator implements ChainedBeanCreator {
185: // match by class this time
186: public Object create(ElementMapping mapping,
187: ReadContext context, BeanCreationChain chain) {
188: if (CompassPoint.class.equals(mapping.getType())) {
189: String value = mapping.getAttributes().getValue(
190: "name");
191: if ("North".equals(value)) {
192: return CompassPoint.NORTH;
193: }
194: if ("South".equals(value)) {
195: return CompassPoint.SOUTH;
196: }
197: if ("East".equals(value)) {
198: return CompassPoint.EAST;
199: }
200: if ("West".equals(value)) {
201: return CompassPoint.WEST;
202: }
203: }
204: return chain.create(mapping, context);
205: }
206: }
207: chain.insertBeanCreator(1, new EnumCreator());
208:
209: BeanReader reader = new BeanReader();
210: reader.getXMLIntrospector().getConfiguration()
211: .setAttributesForPrimitives(true);
212: reader.getXMLIntrospector().getConfiguration()
213: .setWrapCollectionsInElement(false);
214: reader.registerBeanClass("houses", HouseBeans.class);
215: reader.getReadConfiguration().setBeanCreationChain(chain);
216:
217: StringReader in = new StringReader(xml);
218: HouseBeans newHouses = (HouseBeans) reader.parse(in);
219: assertNotNull("Parsing should return a bean", newHouses);
220:
221: ArrayList houseList = newHouses.houses;
222: assertEquals("Should be two houses read", 2, houseList.size());
223: HouseBean newOne = (HouseBean) houseList.get(0);
224: HouseBean newTwo = (HouseBean) houseList.get(1);
225: assertEquals("First house is equal", houseOne, newOne);
226: assertEquals("Second house is equal", houseTwo, newTwo);
227: }
228: }
|