1   /*
2    * Copyright 2004 The Apache Software Foundation.
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16   
17  
18  package org.apache.commons.betwixt.strategy;
19  
20  import java.io.StringReader;
21  import java.io.StringWriter;
22  import java.util.ArrayList;
23  import java.util.Collection;
24  
25  import org.apache.commons.betwixt.AbstractTestCase;
26  import org.apache.commons.betwixt.AttributeDescriptor;
27  import org.apache.commons.betwixt.ElementDescriptor;
28  import org.apache.commons.betwixt.IntrospectionConfiguration;
29  import org.apache.commons.betwixt.XMLBeanInfo;
30  import org.apache.commons.betwixt.XMLIntrospector;
31  import org.apache.commons.betwixt.io.BeanReader;
32  import org.apache.commons.betwixt.io.BeanWriter;
33  
34  /***
35   * Tests for SimpleTypeMapper and the associated strategy.
36   * @author <a href='http://jakarta.apache.org/'>Jakarta Commons Team</a>
37   * @version $Revision: 1.2 $
38   */
39  public class TestSimpleTypeMapper extends AbstractTestCase {
40  
41      public TestSimpleTypeMapper(String name) {
42          super(name);
43      }
44      
45      public void testNewStrategy() throws Exception {
46          XMLIntrospector introspector = new XMLIntrospector();
47          introspector.getConfiguration().setSimpleTypeMapper(new StringsAsElementsSimpleTypeMapper());
48          introspector.getConfiguration().setWrapCollectionsInElement(true);
49          
50          XMLBeanInfo beanInfo = introspector.introspect(TuneBean.class);
51          ElementDescriptor tuneBeanDescriptor = beanInfo.getElementDescriptor();
52          
53          AttributeDescriptor[] tuneBeanAttributes = tuneBeanDescriptor.getAttributeDescriptors();
54          assertEquals("Only expect one attribute", 1, tuneBeanAttributes.length);
55          AttributeDescriptor recordedAttribute = tuneBeanAttributes[0];
56          assertEquals("Expected recorded to be bound as an attribute", "recorded", recordedAttribute.getLocalName());
57          
58          ElementDescriptor[] tuneBeanChildElements = tuneBeanDescriptor.getElementDescriptors();
59          assertEquals("Expected three child elements", 3 , tuneBeanChildElements.length);
60          
61          int bits = 0;
62          for (int i=0, size=tuneBeanChildElements.length; i<size; i++) {
63              String localName = tuneBeanChildElements[i].getLocalName();
64              if ("composers".equals(localName)) {
65                  bits = bits | 1;
66              }
67              if ("artist".equals(localName)) {
68                  bits = bits | 2;
69              }      
70              if ("name".equals(localName)) {
71                  bits = bits | 4;
72              }          
73          }
74          
75          assertEquals("Every element present", 7, bits);
76      }
77      
78      public void testWrite() throws Exception {
79          StringWriter out = new StringWriter();
80          out.write("<?xml version='1.0'?>");
81          BeanWriter writer = new BeanWriter(out);
82          writer.getXMLIntrospector().getConfiguration().setSimpleTypeMapper(new StringsAsElementsSimpleTypeMapper());
83          writer.getXMLIntrospector().getConfiguration().setWrapCollectionsInElement(true);
84          writer.getBindingConfiguration().setMapIDs(false);
85          
86          TuneBean bean = new TuneBean("On The Run", "Pink Floyd", 1972);
87          bean.addComposer(new ComposerBean("David", "Gilmour", 1944));
88          bean.addComposer(new ComposerBean("Roger", "Waters", 1944));
89          
90          writer.write(bean);
91          
92          String xml = out.getBuffer().toString();
93          String expected = "<?xml version='1.0'?>" +
94              "<TuneBean recorded='1972'>" +
95              "    <name>On The Run</name>" +
96              "    <artist>Pink Floyd</artist>" +
97              "    <composers>" +
98              "       <composer born='1944'>" +
99              "           <forename>David</forename>" +
100             "           <surname>Gilmour</surname>" +
101             "       </composer>" +
102             "       <composer born='1944'>" +
103             "           <forename>Roger</forename>" +
104             "           <surname>Waters</surname>" +
105             "       </composer>" +
106             "   </composers>" +
107             "</TuneBean>";
108         
109         xmlAssertIsomorphicContent(parseString(xml), parseString(expected), true);
110     }
111     
112     public void testRead() throws Exception {
113         
114         String xml = "<?xml version='1.0'?>" +
115             "<TuneBean recorded='1972'>" +
116             "    <name>On The Run</name>" +
117             "    <artist>Pink Floyd</artist>" +
118             "    <composers>" +
119             "       <composer born='1944'>" +
120             "           <forename>David</forename>" +
121             "           <surname>Gilmour</surname>" +
122             "       </composer>" +
123             "       <composer born='1944'>" +
124             "           <forename>Roger</forename>" +
125             "           <surname>Waters</surname>" +
126             "       </composer>" +
127             "   </composers>" +
128             "</TuneBean>";
129        StringReader in = new StringReader(xml);
130        
131        BeanReader reader = new BeanReader();
132        reader.getXMLIntrospector().getConfiguration().setSimpleTypeMapper(new StringsAsElementsSimpleTypeMapper());
133        reader.getXMLIntrospector().getConfiguration().setWrapCollectionsInElement(true);
134        reader.getBindingConfiguration().setMapIDs(false);
135        
136        reader.registerBeanClass(TuneBean.class);
137        
138        TuneBean bean = (TuneBean) reader.parse(in);
139        
140        assertNotNull("Parsing failed", bean);
141        assertEquals("Name value", "On The Run", bean.getName());
142        assertEquals("Artist value", "Pink Floyd", bean.getArtist());
143        assertEquals("Recorded value", 1972, bean.getRecorded());
144        
145        Collection expectedComposers = new ArrayList();
146        expectedComposers.add(new ComposerBean("David", "Gilmour", 1944));
147        expectedComposers.add(new ComposerBean("Roger", "Waters", 1944));
148        
149        assertTrue("Right composers", bean.sameComposers(expectedComposers));
150     }
151         
152     /*** Implementation binds strings to elements but everything else to attributes */
153     class StringsAsElementsSimpleTypeMapper extends SimpleTypeMapper {
154 
155         /***
156          * Binds strings to elements but everything else to attributes
157          */
158         public Binding bind(
159                             String propertyName, 
160                             Class propertyType, 
161                             IntrospectionConfiguration configuration) {
162             if (String.class.equals(propertyType)) {
163                 return SimpleTypeMapper.Binding.ELEMENT;
164             }
165             return SimpleTypeMapper.Binding.ATTRIBUTE;
166         }
167                
168     }
169 }