1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    * 
9    *      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  
20  import java.beans.IntrospectionException;
21  import java.io.IOException;
22  import java.io.StringReader;
23  import java.io.StringWriter;
24  
25  import org.apache.commons.betwixt.io.BeanReader;
26  import org.apache.commons.betwixt.io.BeanWriter;
27  import org.xml.sax.InputSource;
28  import org.xml.sax.SAXException;
29  
30  /**
31   * Tests the mapping of a property to the inner text of an element.
32   * 
33   * @author Thomas Dudziak (tomdz@apache.org)
34   */
35  public class TestTextMapping extends AbstractTestCase
36  {
37      public static class Element
38      {
39          private String value;
40  
41          public String getValue()
42          {
43              return value;
44          }
45          public void setValue(String value)
46          {
47              this.value = value;
48          }
49      }
50  
51      private static final String MAPPING =
52          "<?xml version=\"1.0\"?>\n"+
53          "<betwixt-config>\n"+
54          "  <class name=\"org.apache.commons.betwixt.TestTextMapping$Element\">\n"+
55          "    <element name=\"element\">\n"+
56          "      <text property=\"value\"/>\n"+
57          "    </element>\n"+
58          "  </class>\n"+
59          "</betwixt-config>";
60      private static final String EXPECTED =
61          "<?xml version=\"1.0\" ?>\n"+
62          "  <element>Some text</element>\n";
63      
64      public TestTextMapping(String testName)
65      {
66          super(testName);
67      }
68  
69      public void testRoundTripWithSingleMappingFile() throws IOException, SAXException, IntrospectionException
70      {
71          Element element = new Element();
72  
73          element.setValue("Some text");
74  
75          StringWriter outputWriter = new StringWriter();
76  
77          outputWriter.write("<?xml version=\"1.0\" ?>\n");
78  
79          BeanWriter beanWriter = new BeanWriter(outputWriter);
80          beanWriter.setEndOfLine("\n");
81          beanWriter.enablePrettyPrint();
82          beanWriter.setWriteEmptyElements(true);
83          beanWriter.getBindingConfiguration().setMapIDs(false);
84          beanWriter.getXMLIntrospector().register(new InputSource(new StringReader(MAPPING)));
85          beanWriter.setEndOfLine("\n"); //force to \n so expected values match for sure
86          beanWriter.write(element);
87  
88          String output = outputWriter.toString();
89  
90          assertEquals(EXPECTED, output);
91              
92          BeanReader beanReader = new BeanReader();
93  
94          beanReader.registerMultiMapping(new InputSource(new StringReader(MAPPING)));
95  
96          StringReader xmlReader = new StringReader(output);
97  
98          element = (Element)beanReader.parse(xmlReader);
99  
100         assertEquals("Some text",
101                      element.getValue());
102     }
103     
104 }