01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * 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;
18:
19: import java.beans.IntrospectionException;
20: import java.io.FileReader;
21: import java.io.IOException;
22: import java.io.StringReader;
23: import java.io.StringWriter;
24: import java.util.Date;
25:
26: import org.apache.commons.betwixt.io.BeanReader;
27: import org.apache.commons.betwixt.io.BeanWriter;
28: import org.xml.sax.InputSource;
29: import org.xml.sax.SAXException;
30:
31: /**
32: * @author Brian Pugh
33: */
34: public class TestMultiMapping extends AbstractTestCase {
35:
36: public TestMultiMapping(String testName) {
37: super (testName);
38:
39: }
40:
41: public void testRoundTripWithSingleMappingFile()
42: throws IOException, SAXException, IntrospectionException {
43: AddressBean addressBean = new AddressBean();
44: addressBean.setCity("New York");
45: addressBean.setCode("92342");
46: addressBean.setCountry("USA");
47: addressBean.setStreet("12312 Here");
48: PartyBean partyBean = new PartyBean();
49: partyBean.setDateOfParty(new Date());
50: partyBean.setExcuse("too late");
51: partyBean.setFromHour(22);
52: partyBean.setVenue(addressBean);
53:
54: InputSource source = new InputSource(
55: new FileReader(
56: getTestFile("src/test/org/apache/commons/betwixt/mapping.xml")));
57:
58: StringWriter outputWriter = new StringWriter();
59: outputWriter.write("<?xml version='1.0' ?>\n");
60: BeanWriter beanWriter = new BeanWriter(outputWriter);
61: beanWriter.setEndOfLine("\n");
62: beanWriter.enablePrettyPrint();
63: beanWriter.setWriteEmptyElements(true);
64: beanWriter.getXMLIntrospector().register(source);
65: beanWriter.setEndOfLine("\n"); //force to ensure matches on expected
66: beanWriter.write(partyBean);
67: String expectedOut = "<?xml version='1.0' ?>\n"
68: + " <party id=\"1\">\n"
69: + " <the-excuse>too late</the-excuse>\n"
70: + " <location id=\"2\">\n"
71: + " <street>12312 Here</street>\n"
72: + " <city>New York</city>\n"
73: + " <code>92342</code>\n"
74: + " <country>USA</country>\n"
75: + " </location>\n" + " <time>22</time>\n"
76: + " </party>\n";
77: assertEquals(expectedOut, outputWriter.toString());
78:
79: BeanReader beanReader = new BeanReader();
80: beanReader
81: .registerMultiMapping(new InputSource(
82: new FileReader(
83: getTestFile("src/test/org/apache/commons/betwixt/mapping.xml"))));
84: StringReader xmlReader = new StringReader(outputWriter
85: .toString());
86: //Parse the xml
87: PartyBean result = (PartyBean) beanReader.parse(xmlReader);
88: assertEquals(partyBean.getExcuse(), result.getExcuse());
89: assertEquals(partyBean.getFromHour(), result.getFromHour());
90: AddressBean addressResult = result.getVenue();
91: assertEquals(addressBean.getCity(), addressResult.getCity());
92: assertEquals(addressBean.getCode(), addressResult.getCode());
93: assertEquals(addressBean.getCountry(), addressResult
94: .getCountry());
95: assertEquals(addressBean.getStreet(), addressResult.getStreet());
96:
97: }
98:
99: }
|