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;
19  
20  import java.io.StringReader;
21  import java.io.StringWriter;
22  import java.util.Iterator;
23  
24  import org.apache.commons.betwixt.expression.IteratorExpression;
25  import org.apache.commons.betwixt.io.BeanReader;
26  import org.apache.commons.betwixt.io.BeanWriter;
27  import org.apache.commons.betwixt.strategy.CapitalizeNameMapper;
28  
29  /***
30   * @author <a href='http://jakarta.apache.org/'>Apache Commons Team</a>
31   * @version $Revision: 155402 $
32   */
33  public class TestCollectives extends AbstractTestCase{
34      
35      private IntrospectionConfiguration categoriesIntrospectionConfiguration = new IntrospectionConfiguration();
36      private BindingConfiguration noIDsBindingConfiguration = new BindingConfiguration();
37      
38      public TestCollectives(String testName) {
39          super(testName);
40          
41          CapitalizeNameMapper capitalizeNameMapper = new CapitalizeNameMapper();
42          categoriesIntrospectionConfiguration.setAttributesForPrimitives(false);
43          categoriesIntrospectionConfiguration.setElementNameMapper(capitalizeNameMapper);
44          categoriesIntrospectionConfiguration.setAttributeNameMapper(capitalizeNameMapper);
45          categoriesIntrospectionConfiguration.setWrapCollectionsInElement(false);    
46          
47          noIDsBindingConfiguration.setMapIDs(false);    
48      }
49  
50  
51      public void testWriteCategories() throws Exception {
52          StringWriter out = new StringWriter();
53          out.write("<?xml version='1.0'?>");
54          BeanWriter writer = new BeanWriter(out);
55          writer.getXMLIntrospector().setConfiguration(categoriesIntrospectionConfiguration);
56          writer.setBindingConfiguration(noIDsBindingConfiguration);
57          
58          Categories categories = new Categories();
59          categories.addCategory(new Category("Runs"));
60          categories.addCategory(new Category("Innings"));
61          categories.addCategory(new Category("Dismissals"));
62          categories.addCategory(new Category("High Score"));
63          categories.addCategory(new Category("Average"));
64          
65          writer.write(categories);
66          
67          String xml = out.getBuffer().toString();
68          String expected = "<?xml version='1.0'?><Categories>" +
69              "<Category><Name>Runs</Name></Category>" +
70              "<Category><Name>Innings</Name></Category>" +
71              "<Category><Name>Dismissals</Name></Category>" +
72              "<Category><Name>High Score</Name></Category>" +
73              "<Category><Name>Average</Name></Category>" +
74              "</Categories>";
75              
76         xmlAssertIsomorphicContent(parseString(expected), parseString(xml));
77      }   
78      
79      public void testReadCategories() throws Exception {
80          BeanReader beanReader = new BeanReader();
81          beanReader.getXMLIntrospector().setConfiguration(categoriesIntrospectionConfiguration);
82          beanReader.setBindingConfiguration(noIDsBindingConfiguration);
83          beanReader.registerBeanClass(Categories.class);
84  
85          String xml = "<?xml version='1.0'?><Categories>" +
86              "<Category><Name>Runs</Name></Category>" +
87              "<Category><Name>Innings</Name></Category>" +
88              "<Category><Name>Dismissals</Name></Category>" +
89              "<Category><Name>High Score</Name></Category>" +
90              "<Category><Name>Average</Name></Category>" +
91              "</Categories>";
92         
93         StringReader in = new StringReader(xml);
94         
95         Categories bean = (Categories) beanReader.parse(in);    
96         
97         assertEquals("5 categories", 5, bean.size());
98         
99         Iterator it = bean.getCategories();  
100        assertEquals("Runs category", new Category("Runs"), it.next());
101        assertEquals("Runs category", new Category("Innings"), it.next());
102        assertEquals("Runs category", new Category("Dismissals"), it.next());
103        assertEquals("Runs category", new Category("High Score"), it.next());
104        assertEquals("Runs category", new Category("Average"), it.next());
105        
106     }
107     
108     public void testIntrospectListExtension() throws Exception
109     {      
110         XMLIntrospector xmlIntrospector = new XMLIntrospector();
111         XMLBeanInfo beanInfo = xmlIntrospector.introspect(ArrayListExtender.class);
112         
113         ElementDescriptor elementDescriptor = beanInfo.getElementDescriptor();
114         ElementDescriptor[] childDescriptors = elementDescriptor.getElementDescriptors();
115         assertEquals(2, childDescriptors.length);
116         assertEquals("another", childDescriptors[0].getPropertyName());
117         assertTrue(childDescriptors[1].getContextExpression() instanceof IteratorExpression);
118     }
119 
120     public void testWriteListExtension() throws Exception
121     {
122         ArrayListExtender bean = new ArrayListExtender("Whatever");
123         bean.add(new Long(11));
124         bean.add(new Long(12));
125         bean.add(new Long(13));
126         
127         StringWriter out = new StringWriter();
128         out.write("<?xml version='1.0'?>");
129         
130         BeanWriter writer = new BeanWriter(out);
131         writer.getBindingConfiguration().setMapIDs( false );
132         writer.write(bean);
133         
134         String expected = "<?xml version='1.0'?><ArrayListExtender><another>Whatever</another>" +
135         		"<Long>11</Long><Long>12</Long><Long>13</Long></ArrayListExtender>";
136         
137         xmlAssertIsomorphicContent(parseString( expected ), parseString( out ));
138     }
139     
140 
141     public void testReadListExtension() throws Exception
142     {
143         String xml = "<?xml version='1.0'?><ArrayListExtender><another>Whatever</another>" +
144 		"<Long>11</Long><Long>12</Long><Long>13</Long></ArrayListExtender>";
145 
146         StringReader in = new StringReader( xml );
147         
148         BeanReader reader = new BeanReader();
149         reader.getBindingConfiguration().setMapIDs( false );
150 
151         reader.registerBeanClass( ArrayListExtender.class );
152         ArrayListExtender bean = (ArrayListExtender) reader.parse( in );
153         
154         assertEquals("Whatever", bean.getAnother());
155     }
156 }