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.dotbetwixt;
19  
20  
21  import java.io.StringReader;
22  import java.io.StringWriter;
23  
24  import junit.framework.TestCase;
25  
26  import org.apache.commons.betwixt.AttributeDescriptor;
27  import org.apache.commons.betwixt.ElementDescriptor;
28  import org.apache.commons.betwixt.XMLBeanInfo;
29  import org.apache.commons.betwixt.XMLIntrospector;
30  import org.apache.commons.betwixt.io.BeanReader;
31  import org.apache.commons.betwixt.io.BeanWriter;
32  
33  /***
34   * @author Brian Pugh
35   */
36  public class TestLoopType extends TestCase {
37      public void testSimpleList() throws Exception {
38          Father father = new Father();
39          father.setSpouse("Julie");
40          father.addKid("John");
41          father.addKid("Jane");
42  
43          StringWriter outputWriter = new StringWriter();
44  
45          outputWriter.write("<?xml version='1.0' ?>\n");
46          BeanWriter beanWriter = new BeanWriter(outputWriter);
47          beanWriter.enablePrettyPrint();
48          beanWriter.getBindingConfiguration().setMapIDs(true);
49          beanWriter.write(father);
50  
51          BeanReader beanReader = new BeanReader();
52  
53          // Configure the reader
54          beanReader.registerBeanClass(Father.class);
55          StringReader xmlReader = new StringReader(outputWriter.toString());
56  
57          //Parse the xml
58          Father result = (Father) beanReader.parse(xmlReader);
59  
60          assertNotNull("Unexpected null list of children!", result.getKids());
61          assertEquals(
62              "got wrong number of children",
63              father.getKids().size(),
64              result.getKids().size());
65          assertNull(
66              "Spouse should not get set because it is not in the .betwixt file",
67              result.getSpouse());
68      }
69  
70      public void testIgnoredProperty() throws Exception {
71          XMLIntrospector introspector = new XMLIntrospector();
72          XMLBeanInfo beanInfo = introspector.introspect(IgnoreBean.class);
73          ElementDescriptor ignoreDescriptor = beanInfo.getElementDescriptor();
74          
75          assertEquals("element name matches", "ignore", ignoreDescriptor.getLocalName());
76          ElementDescriptor[] childDescriptors = ignoreDescriptor.getElementDescriptors();
77          assertEquals("number of child elements", 1, childDescriptors.length);
78       
79      }
80      
81  
82      public void testIgnoredAdders() throws Exception {
83          XMLIntrospector introspector = new XMLIntrospector();
84          XMLBeanInfo beanInfo = introspector.introspect(IgnoreAddersBean.class);
85          ElementDescriptor ignoreDescriptor = beanInfo.getElementDescriptor();
86          
87          assertEquals("element name matches", "ignore", ignoreDescriptor.getLocalName());
88          ElementDescriptor[] childDescriptors = ignoreDescriptor.getElementDescriptors();
89          assertEquals("number of child elements", 1, childDescriptors.length);
90                      
91      }
92      
93      //TODO: complete these tests after refactoring the element descriptors produced is complete
94      public void _testAddDefaults() throws Exception {
95          XMLIntrospector introspector = new XMLIntrospector();
96          XMLBeanInfo beanInfo = introspector.introspect(LibraryBean.class);
97          ElementDescriptor libraryDescriptor = beanInfo.getElementDescriptor();
98          
99          AttributeDescriptor[] libraryAttributeDescriptors = libraryDescriptor.getAttributeDescriptors();
100         assertEquals("Only one attribute", 1, libraryAttributeDescriptors.length);
101         
102         ElementDescriptor[] libraryElementDescriptors = libraryDescriptor.getElementDescriptors();
103         assertEquals("Only one element", 1, libraryElementDescriptors.length);
104         
105         
106     }
107 }
108