1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
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
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
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 }