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.strategy;
18  
19  import java.io.StringReader;
20  import java.io.StringWriter;
21  import java.util.Collection;
22  
23  import org.apache.commons.betwixt.AbstractTestCase;
24  import org.apache.commons.betwixt.expression.Context;
25  import org.apache.commons.betwixt.io.BeanReader;
26  import org.apache.commons.betwixt.io.BeanWriter;
27  
28  public class TestConversionFlavour extends AbstractTestCase {
29  
30      public TestConversionFlavour(String testName) {
31          super(testName);
32      }
33  
34      
35      public void testRead() throws Exception {
36          String xml = "<alpha>" +
37          "        <name>BananasSIX</name>" +
38          "        <betaBean>" +
39          "            <name>PeachONE</name>" +
40          "        </betaBean>" +
41          "        <children>" +
42          "            <child>" +
43          "                <name>PeachTWO</name>" +
44          "            </child>" +
45          "        </children>" +
46          "        <mapped>" +
47          "          <entry>" +
48          "            <key>Key</key>" +
49          "            <value>" +
50          "               <name>PeachTHREE</name>" +
51          "            </value>" +
52          "          </entry>" +
53          "        </mapped>" +
54          "        </alpha>";
55          
56          StringReader in = new StringReader(xml);
57          BeanReader reader = new BeanReader();
58          reader.getBindingConfiguration().setMapIDs(false);
59          reader.getBindingConfiguration().setObjectStringConverter(new PrependingConverter());
60          reader.registerBeanClass(AlphaBean.class);
61          AlphaBean bean = (AlphaBean) reader.parse(in);
62          assertNotNull(bean);
63          assertEquals("SIX", bean.getName());
64          BetaBean betaBean = bean.getBetaBean();
65          assertNotNull(betaBean);
66          assertEquals("ONE", betaBean.getName());
67          Collection children = bean.getChildren();
68          assertEquals(1, children.size());
69          BetaBean child = (BetaBean) children.iterator().next();
70          assertEquals("TWO", child.getName());
71      }
72      
73      public void testWrite() throws Exception {
74          AlphaBean alphaBean = new AlphaBean();
75          alphaBean.setName("SIX");
76          BetaBean betaBeanOne = new BetaBean("ONE");
77          alphaBean.setBetaBean(betaBeanOne);
78          BetaBean betaBeanTwo = new BetaBean("TWO");
79          alphaBean.addChild(betaBeanTwo);
80          BetaBean betaBeanThree = new BetaBean("THREE");
81          alphaBean.put("Key", betaBeanThree);
82          
83          StringWriter out = new StringWriter();
84          BeanWriter writer = new BeanWriter(out);
85          writer.getBindingConfiguration().setMapIDs(false);
86          writer.getBindingConfiguration().setObjectStringConverter(new PrependingConverter());
87          writer.write(alphaBean);
88          
89          String xml = "<alpha>" +
90                  "        <name>BananasSIX</name>" +
91                  "        <betaBean>" +
92                  "            <name>PeachONE</name>" +
93                  "        </betaBean>" +
94                  "        <children>" +
95                  "            <child>" +
96                  "                <name>PeachTWO</name>" +
97                  "            </child>" +
98                  "        </children>" +
99                  "        <mapped>" +
100                 "          <entry>" +
101                 "            <key>Key</key>" +
102                 "            <value>" +
103                 "               <name>PeachTHREE</name>" +
104                 "            </value>" +
105                 "          </entry>" +
106                 "        </mapped>" +
107                 "        </alpha>";
108          
109         xmlAssertIsomorphicContent(parseString(xml), parseString(out), true);
110     }
111     
112 
113     public static final class PrependingConverter extends DefaultObjectStringConverter {
114         
115         public String objectToString(Object object, Class type, String flavour,
116                 Context context) {
117             String result = super.objectToString(object, type, flavour, context);
118             if (flavour != null) {
119                 result = flavour + result;
120             }
121             return result;
122         }
123 
124         public Object stringToObject(String value, Class type, String flavour,
125                 Context context) {
126             if (flavour != null) {
127                 value = value.substring(flavour.length());
128             }
129             return super.stringToObject(value, type, flavour, context);
130         }
131 }
132 }