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.IOException;
21: import java.io.StringReader;
22: import java.io.StringWriter;
23:
24: import org.apache.commons.betwixt.io.BeanReader;
25: import org.apache.commons.betwixt.io.BeanWriter;
26: import org.xml.sax.InputSource;
27: import org.xml.sax.SAXException;
28:
29: /**
30: * Tests the mapping of a property to the inner text of an element.
31: *
32: * @author Thomas Dudziak (tomdz@apache.org)
33: */
34: public class TestTextMapping extends AbstractTestCase {
35: public static class Element {
36: private String value;
37:
38: public String getValue() {
39: return value;
40: }
41:
42: public void setValue(String value) {
43: this .value = value;
44: }
45: }
46:
47: private static final String MAPPING = "<?xml version=\"1.0\"?>\n"
48: + "<betwixt-config>\n"
49: + " <class name=\"org.apache.commons.betwixt.TestTextMapping$Element\">\n"
50: + " <element name=\"element\">\n"
51: + " <text property=\"value\"/>\n" + " </element>\n"
52: + " </class>\n" + "</betwixt-config>";
53: private static final String EXPECTED = "<?xml version=\"1.0\" ?>\n"
54: + " <element>Some text</element>\n";
55:
56: public TestTextMapping(String testName) {
57: super (testName);
58: }
59:
60: public void testRoundTripWithSingleMappingFile()
61: throws IOException, SAXException, IntrospectionException {
62: Element element = new Element();
63:
64: element.setValue("Some text");
65:
66: StringWriter outputWriter = new StringWriter();
67:
68: outputWriter.write("<?xml version=\"1.0\" ?>\n");
69:
70: BeanWriter beanWriter = new BeanWriter(outputWriter);
71: beanWriter.setEndOfLine("\n");
72: beanWriter.enablePrettyPrint();
73: beanWriter.setWriteEmptyElements(true);
74: beanWriter.getBindingConfiguration().setMapIDs(false);
75: beanWriter.getXMLIntrospector().register(
76: new InputSource(new StringReader(MAPPING)));
77: beanWriter.setEndOfLine("\n"); //force to \n so expected values match for sure
78: beanWriter.write(element);
79:
80: String output = outputWriter.toString();
81:
82: assertEquals(EXPECTED, output);
83:
84: BeanReader beanReader = new BeanReader();
85:
86: beanReader.registerMultiMapping(new InputSource(
87: new StringReader(MAPPING)));
88:
89: StringReader xmlReader = new StringReader(output);
90:
91: element = (Element) beanReader.parse(xmlReader);
92:
93: assertEquals("Some text", element.getValue());
94: }
95:
96: }
|