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  package org.apache.commons.betwixt.io.read;
18  
19  import java.io.StringReader;
20  import java.io.StringWriter;
21  
22  import org.apache.commons.betwixt.AbstractTestCase;
23  import org.apache.commons.betwixt.io.BeanReader;
24  import org.apache.commons.betwixt.io.BeanWriter;
25  
26  /**
27   * @author <a href='http://commons.apache.org'>Apache Commons Team</a>, <a href='http://www.apache.org'>Apache Software Foundation</a>
28   */
29  public class TestReadData extends AbstractTestCase {
30  
31      public TestReadData(String testName) {
32          super(testName);
33      }
34  
35      public void testReadInvalidDate() throws Exception {
36          
37          String xmlWithInvalidDate = "<?xml version='1.0'?>" +
38          		"<AlertBean>" +
39          		"	<message>Whatever</message>" +
40          		"   <summary>Sometime</summary>" +
41          		"   <timestamp>2004-13-32 00:00:00.0</timestamp>" +
42          		"</AlertBean>";
43          StringReader invalidIn = new StringReader(xmlWithInvalidDate);
44      
45          
46          String xmlWithValidDate = "<?xml version='1.0'?>" +
47          		"<AlertBean>" +
48          		"	<message>Whatever</message>" +
49          		"   <summary>Sometime</summary>" +
50          		"   <timestamp>1999-12-31 00:00:00.0</timestamp>" +
51          		"</AlertBean>";
52          StringReader validIn = new StringReader(xmlWithValidDate);
53        
54          
55          BeanReader reader = new BeanReader();
56          reader.registerBeanClass(AlertBean.class);
57          try
58          {
59              AlertBean alterBean = (AlertBean) reader.parse(invalidIn);
60              fail("Invalid date so expected exception");
61          }
62          catch (Exception e)
63          {
64              // expected
65          }
66          
67          AlertBean alterBean = (AlertBean) reader.parse(validIn);
68      }
69      
70      public void testWritePrivateStaticClasses() throws Exception {
71          Nested nested = new Nested();
72          nested.setName("Timothy Taylor");
73          StringWriter out = new StringWriter();
74          out.write("<?xml version='1.0'?>");
75          BeanWriter writer = new BeanWriter(out);
76          writer.getXMLIntrospector().getConfiguration().setAttributesForPrimitives(false);
77          writer.getBindingConfiguration().setMapIDs(false);
78          writer.write("ale", nested);
79          
80          String expected = "<?xml version='1.0'?>" +
81                  "<ale><name>Timothy Taylor</name></ale>"; 
82           
83          xmlAssertIsomorphic(parseString(out), parseString(expected), true);
84      }
85      
86      // This test runs in Eclipse but not in maven :/ 
87      public void _testReadPrivateStaticClasses() throws Exception {
88          
89          StringReader in= new StringReader("<?xml version='1.0'?>" +
90                  "<ale><name>Timothy Taylor</name></ale>"); 
91          BeanReader reader = new BeanReader();
92          reader.registerBeanClass("ale", Nested.class);
93          reader.getXMLIntrospector().getConfiguration().setAttributesForPrimitives(true);
94          Object out = reader.parse(in);
95          assertNotNull("Expected bean to be output", out);
96          assertEquals("Expected bean to be of type Nested", "org.apache.commons.betwixt.io.read.TestReadData$Nested", out.getClass().getName());
97          Nested bean = (Nested) out;
98          assertEquals("Expected name to be set", "Timothy Taylor", bean.getName());
99      }
100 
101     private static class Nested {
102         private String name;
103 
104         public String getName() {
105             return name;
106         }
107 
108         public void setName(String name) {
109             this.name = name;
110         }
111         
112     }
113 }