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  package org.apache.commons.betwixt.io.read;
18  
19  import java.io.StringReader;
20  import java.util.List;
21  
22  import junit.framework.Test;
23  import junit.framework.TestSuite;
24  
25  import org.apache.commons.betwixt.AbstractTestCase;
26  import org.apache.commons.betwixt.BindingConfiguration;
27  import org.apache.commons.betwixt.ElementDescriptor;
28  import org.apache.commons.betwixt.XMLIntrospector;
29  import org.apache.commons.betwixt.io.BeanReader;
30  
31  /*** 
32   * Test harness for Mapping Actions.
33   * 
34   * @author Robert Burrell Donkin
35   * @version $Id: TestMappingActions.java 155402 2005-02-26 12:52:00Z dirkv $
36   */
37  public class TestMappingActions extends AbstractTestCase {
38  
39  
40      public TestMappingActions(String name) {
41          super(name);
42      }
43          
44      public static Test suite() {
45          return new TestSuite(TestMappingActions.class);
46      }    
47      
48      public void testSimpleRead() throws Exception {
49      
50          String xml="<?xml version='1.0'?><AddressBean><street>1 Main Street</street><city>New Town</city>"
51                  + "<code>NT1 1AA</code><country>UK</country></AddressBean>";
52                  
53          //SimpleLog log = new SimpleLog("[test]");
54          //log.setLevel(SimpleLog.LOG_LEVEL_TRACE);
55          //BeanRuleSet.setLog(log);
56          BeanReader reader = new BeanReader();
57          reader.registerBeanClass(AddressBean.class);
58          AddressBean address = (AddressBean) reader.parse(new StringReader(xml));
59          
60          assertFalse("Address is mapped", address == null);
61          assertEquals("Street", "1 Main Street", address.getStreet());
62          assertEquals("City", "New Town", address.getCity());
63          assertEquals("Code", "NT1 1AA", address.getCode());
64          assertEquals("Country", "UK", address.getCountry());
65      }
66      
67      public void testPrimitiveCollective() throws Exception{
68      
69          String xml="<?xml version='1.0'?><SimpleStringCollective><strings>"
70                      + "<string>one</string><string>two</string><string>three</string>"
71                      + "</strings></SimpleStringCollective>";
72                  
73          //SimpleLog log = new SimpleLog("[test]");
74          //log.setLevel(SimpleLog.LOG_LEVEL_TRACE);
75         // BeanRuleSet.setLog(log);
76          BeanReader reader = new BeanReader();
77          reader.registerBeanClass(SimpleStringCollective.class);
78          SimpleStringCollective collective = (SimpleStringCollective) reader.parse(new StringReader(xml));
79          
80          assertFalse("SimpleStringCollective mapped", collective == null);
81          List strings = collective.getStrings();
82          assertEquals("String count", 3, strings.size());
83          assertEquals("First string", "one", strings.get(0));
84          assertEquals("Second string", "two", strings.get(1));
85          assertEquals("Third string", "three", strings.get(2));
86      }
87      
88  
89      
90      public void testBodyUpdateActionNoMatch() throws Exception {
91          AddressBean bean = new AddressBean();
92          bean.setStreet("DEFAULT");
93          bean.setCode("DEFAULT");
94          bean.setCountry("DEFAULT");
95          
96          XMLIntrospector introspector = new XMLIntrospector();
97          ElementDescriptor elementDescriptor = introspector.introspect(AddressBean.class).getElementDescriptor();
98          
99          ReadContext context = new ReadContext(new BindingConfiguration(), new ReadConfiguration());
100         context.setBean(bean);
101         context.markClassMap(AddressBean.class);
102         context.pushElement("NoMatch");
103         context.setXMLIntrospector(introspector);
104         SimpleTypeBindAction action = new SimpleTypeBindAction();
105         action.body("Street value", context);
106         assertEquals("Street is unset", "DEFAULT", bean.getStreet());
107         assertEquals("Country is unset", "DEFAULT", bean.getCountry());
108         assertEquals("Code is unset", "DEFAULT", bean.getCode());
109     }
110     
111     
112     public void testBodyUpdateActionMatch() throws Exception {
113         AddressBean bean = new AddressBean();
114         bean.setStreet("DEFAULT");
115         bean.setCode("DEFAULT");
116         bean.setCountry("DEFAULT");
117         
118         XMLIntrospector introspector = new XMLIntrospector();
119         ReadContext context = new ReadContext(new BindingConfiguration(), new ReadConfiguration());
120         context.pushBean(bean);
121         context.markClassMap(AddressBean.class);
122         context.pushElement("street");
123         context.setXMLIntrospector(introspector);
124         SimpleTypeBindAction action = new SimpleTypeBindAction();
125         action.body("Street value", context);
126         assertEquals("Street is set", "Street value", bean.getStreet());
127         assertEquals("Country is unset", "DEFAULT", bean.getCountry());
128         assertEquals("Code is unset", "DEFAULT", bean.getCode());
129     } 
130     
131     public void testCollection() throws Exception {
132         String xml = "<?xml version='1.0'?>"
133                 + "<elements><element><value>alpha</value></element></elements>";
134         StringReader in = new StringReader(xml);
135         BeanReader reader = new BeanReader();
136         reader.registerBeanClass(Elements.class);
137         Elements result = (Elements) reader.parse(in);
138         assertNotNull("Element alpha exists", result.getElement("alpha"));
139     }
140 }