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  
18  
19  package org.apache.commons.betwixt.dotbetwixt;
20  
21  
22  import java.io.StringReader;
23  import java.io.StringWriter;
24  
25  import junit.framework.TestCase;
26  
27  import org.apache.commons.betwixt.AttributeDescriptor;
28  import org.apache.commons.betwixt.ElementDescriptor;
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   * @author Brian Pugh
36   */
37  public class TestLoopType extends TestCase {
38      public void testSimpleList() throws Exception {
39          Father father = new Father();
40          father.setSpouse("Julie");
41          father.addKid("John");
42          father.addKid("Jane");
43  
44          StringWriter outputWriter = new StringWriter();
45  
46          outputWriter.write("<?xml version='1.0' ?>\n");
47          BeanWriter beanWriter = new BeanWriter(outputWriter);
48          beanWriter.setEndOfLine("\n");
49          beanWriter.enablePrettyPrint();
50          beanWriter.getBindingConfiguration().setMapIDs(true);
51          beanWriter.write(father);
52  
53          BeanReader beanReader = new BeanReader();
54  
55          // Configure the reader
56          beanReader.registerBeanClass(Father.class);
57          StringReader xmlReader = new StringReader(outputWriter.toString());
58  
59          //Parse the xml
60          Father result = (Father) beanReader.parse(xmlReader);
61  
62          assertNotNull("Unexpected null list of children!", result.getKids());
63          assertEquals(
64              "got wrong number of children",
65              father.getKids().size(),
66              result.getKids().size());
67          assertNull(
68              "Spouse should not get set because it is not in the .betwixt file",
69              result.getSpouse());
70      }
71  
72      public void testIgnoredProperty() throws Exception {
73          XMLIntrospector introspector = new XMLIntrospector();
74          XMLBeanInfo beanInfo = introspector.introspect(IgnoreBean.class);
75          ElementDescriptor ignoreDescriptor = beanInfo.getElementDescriptor();
76          
77          assertEquals("element name matches", "ignore", ignoreDescriptor.getLocalName());
78          ElementDescriptor[] childDescriptors = ignoreDescriptor.getElementDescriptors();
79          assertEquals("number of child elements", 1, childDescriptors.length);
80       
81      }
82      
83      /**
84       * Basic test for add-adders attribute of addDefaults tag.
85       * @throws Exception
86       */
87      public void testIgnoredAdders() throws Exception {
88          XMLIntrospector introspector = new XMLIntrospector();
89          // ignore adders bean uses a dot betwixt file with add-adders false
90          XMLBeanInfo beanInfo = introspector.introspect(IgnoreAddersBean.class);
91          ElementDescriptor ignoreDescriptor = beanInfo.getElementDescriptor();
92          
93          assertEquals("element name matches", "ignore", ignoreDescriptor.getLocalName());
94          ElementDescriptor[] childDescriptors = ignoreDescriptor.getElementDescriptors();
95          assertEquals("number of child elements", 2, childDescriptors.length);
96          for (int i=0; i<childDescriptors.length; i++) {
97              ElementDescriptor descriptor = childDescriptors[i];
98              if (descriptor.getLocalName().equals("gammas")) {
99                  assertNull("Expected descriptor to be null since adders must be explicitly listed.", descriptor.getUpdater()); 
100             }
101             else {
102                 assertEquals("alpha", descriptor.getLocalName());
103             }
104         }
105         AttributeDescriptor[] attributes = ignoreDescriptor.getAttributeDescriptors();
106         assertEquals(1, attributes.length);
107         assertEquals("beta", attributes[0].getLocalName());
108     }
109     
110     //TODO: complete these tests after refactoring the element descriptors produced is complete
111     public void _testAddDefaults() throws Exception {
112         XMLIntrospector introspector = new XMLIntrospector();
113         XMLBeanInfo beanInfo = introspector.introspect(LibraryBean.class);
114         ElementDescriptor libraryDescriptor = beanInfo.getElementDescriptor();
115         
116         AttributeDescriptor[] libraryAttributeDescriptors = libraryDescriptor.getAttributeDescriptors();
117         assertEquals("Only one attribute", 1, libraryAttributeDescriptors.length);
118         
119         ElementDescriptor[] libraryElementDescriptors = libraryDescriptor.getElementDescriptors();
120         assertEquals("Only one element", 1, libraryElementDescriptors.length);
121         
122         
123     }
124 }
125