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/'>Apache Commons Team</a>
37   * @version $Revision: 155402 $
38   */
39  public class TestSimpleTypeMapper extends AbstractTestCase {
40  
41      public TestSimpleTypeMapper(String name) {
42          super(name);
43      }
44      
45      public void testDefaultExceptionType() throws Exception {
46           assertEquals(TypeBindingStrategy.BindingType.COMPLEX, TypeBindingStrategy.DEFAULT.bindingType(RuntimeException.class));
47      }
48      
49      public void testNewStrategy() throws Exception {
50          XMLIntrospector introspector = new XMLIntrospector();
51          introspector.getConfiguration().setSimpleTypeMapper(new StringsAsElementsSimpleTypeMapper());
52          introspector.getConfiguration().setWrapCollectionsInElement(true);
53          
54          XMLBeanInfo beanInfo = introspector.introspect(TuneBean.class);
55          ElementDescriptor tuneBeanDescriptor = beanInfo.getElementDescriptor();
56          
57          AttributeDescriptor[] tuneBeanAttributes = tuneBeanDescriptor.getAttributeDescriptors();
58          assertEquals("Only expect one attribute", 1, tuneBeanAttributes.length);
59          AttributeDescriptor recordedAttribute = tuneBeanAttributes[0];
60          assertEquals("Expected recorded to be bound as an attribute", "recorded", recordedAttribute.getLocalName());
61          
62          ElementDescriptor[] tuneBeanChildElements = tuneBeanDescriptor.getElementDescriptors();
63          assertEquals("Expected three child elements", 3 , tuneBeanChildElements.length);
64          
65          int bits = 0;
66          for (int i=0, size=tuneBeanChildElements.length; i<size; i++) {
67              String localName = tuneBeanChildElements[i].getLocalName();
68              if ("composers".equals(localName)) {
69                  bits = bits | 1;
70              }
71              if ("artist".equals(localName)) {
72                  bits = bits | 2;
73              }      
74              if ("name".equals(localName)) {
75                  bits = bits | 4;
76              }          
77          }
78          
79          assertEquals("Every element present", 7, bits);
80      }
81      
82      public void testWrite() throws Exception {
83          StringWriter out = new StringWriter();
84          out.write("<?xml version='1.0'?>");
85          BeanWriter writer = new BeanWriter(out);
86          writer.getXMLIntrospector().getConfiguration().setSimpleTypeMapper(new StringsAsElementsSimpleTypeMapper());
87          writer.getXMLIntrospector().getConfiguration().setWrapCollectionsInElement(true);
88          writer.getBindingConfiguration().setMapIDs(false);
89          
90          TuneBean bean = new TuneBean("On The Run", "Pink Floyd", 1972);
91          bean.addComposer(new ComposerBean("David", "Gilmour", 1944));
92          bean.addComposer(new ComposerBean("Roger", "Waters", 1944));
93          
94          writer.write(bean);
95          
96          String xml = out.getBuffer().toString();
97          String expected = "<?xml version='1.0'?>" +
98              "<TuneBean recorded='1972'>" +
99              "    <name>On The Run</name>" +
100             "    <artist>Pink Floyd</artist>" +
101             "    <composers>" +
102             "       <composer born='1944'>" +
103             "           <forename>David</forename>" +
104             "           <surname>Gilmour</surname>" +
105             "       </composer>" +
106             "       <composer born='1944'>" +
107             "           <forename>Roger</forename>" +
108             "           <surname>Waters</surname>" +
109             "       </composer>" +
110             "   </composers>" +
111             "</TuneBean>";
112         
113         xmlAssertIsomorphicContent(parseString(xml), parseString(expected), true);
114     }
115     
116     public void testRead() throws Exception {
117         
118         String xml = "<?xml version='1.0'?>" +
119             "<TuneBean recorded='1972'>" +
120             "    <name>On The Run</name>" +
121             "    <artist>Pink Floyd</artist>" +
122             "    <composers>" +
123             "       <composer born='1944'>" +
124             "           <forename>David</forename>" +
125             "           <surname>Gilmour</surname>" +
126             "       </composer>" +
127             "       <composer born='1944'>" +
128             "           <forename>Roger</forename>" +
129             "           <surname>Waters</surname>" +
130             "       </composer>" +
131             "   </composers>" +
132             "</TuneBean>";
133        StringReader in = new StringReader(xml);
134        
135        BeanReader reader = new BeanReader();
136        reader.getXMLIntrospector().getConfiguration().setSimpleTypeMapper(new StringsAsElementsSimpleTypeMapper());
137        reader.getXMLIntrospector().getConfiguration().setWrapCollectionsInElement(true);
138        reader.getBindingConfiguration().setMapIDs(false);
139        
140        reader.registerBeanClass(TuneBean.class);
141        
142        TuneBean bean = (TuneBean) reader.parse(in);
143        
144        assertNotNull("Parsing failed", bean);
145        assertEquals("Name value", "On The Run", bean.getName());
146        assertEquals("Artist value", "Pink Floyd", bean.getArtist());
147        assertEquals("Recorded value", 1972, bean.getRecorded());
148        
149        Collection expectedComposers = new ArrayList();
150        expectedComposers.add(new ComposerBean("David", "Gilmour", 1944));
151        expectedComposers.add(new ComposerBean("Roger", "Waters", 1944));
152        
153        assertTrue("Right composers", bean.sameComposers(expectedComposers));
154     }
155         
156     /*** Implementation binds strings to elements but everything else to attributes */
157     class StringsAsElementsSimpleTypeMapper extends SimpleTypeMapper {
158 
159         /***
160          * Binds strings to elements but everything else to attributes
161          */
162         public Binding bind(
163                             String propertyName, 
164                             Class propertyType, 
165                             IntrospectionConfiguration configuration) {
166             if (String.class.equals(propertyType)) {
167                 return SimpleTypeMapper.Binding.ELEMENT;
168             }
169             return SimpleTypeMapper.Binding.ATTRIBUTE;
170         }
171                
172     }
173 }