1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.betwixt;
18
19 import java.io.StringReader;
20 import java.io.StringWriter;
21
22 import org.apache.commons.betwixt.io.BeanReader;
23 import org.apache.commons.betwixt.io.BeanWriter;
24
25 public class TestStringCollections extends AbstractTestCase {
26
27 public TestStringCollections(String testName) {
28 super(testName);
29 }
30
31 public void testIntrospection() throws Exception {
32 XMLIntrospector introspector = new XMLIntrospector();
33 XMLBeanInfo xmlBeanInfo = introspector.introspect(PoemBean.class);
34 ElementDescriptor beanDescriptor = xmlBeanInfo.getElementDescriptor();
35 ElementDescriptor[] beanChildren = beanDescriptor.getElementDescriptors();
36 assertEquals("Only one child", 1, beanChildren.length);
37 ElementDescriptor[] linesChildren = beanChildren[0].getElementDescriptors();
38 assertEquals("Only one lines child", 1, linesChildren.length);
39 assertFalse("Line child is not hollow", linesChildren[0].isHollow());
40 }
41
42 public void testWritePoem() throws Exception {
43 String expected = "<?xml version='1.0'?>" +
44 "<PoemBean>" +
45 "<lines>" +
46 "<line>It is an ancient Mariner,</line>" +
47 "<line>And he stoppeth one of three.</line>" +
48 "<line>\"By thy long grey beard and the glittering eye,</line>" +
49 "<line>Now wherefore stopp'st thou me?\"</line>" +
50 "</lines>" +
51 "</PoemBean>";
52 PoemBean bean = new PoemBean();
53 bean.addLine("It is an ancient Mariner,");
54 bean.addLine("And he stoppeth one of three.");
55 bean.addLine("\"By thy long grey beard and the glittering eye,");
56 bean.addLine("Now wherefore stopp'st thou me?\"");
57
58 StringWriter out = new StringWriter();
59 out.write("<?xml version='1.0'?>");
60 BeanWriter writer = new BeanWriter(out);
61 writer.getBindingConfiguration().setMapIDs(false);
62 writer.write(bean);
63
64 String xml = out.toString();
65 xmlAssertIsomorphic(parseString(expected), parseString(xml));
66 }
67
68 public void testReadPoem() throws Exception {
69 String xml = "<?xml version='1.0'?>" +
70 "<PoemBean>" +
71 "<lines>" +
72 "<line>It is an ancient Mariner,</line>" +
73 "<line>And he stoppeth one of three.</line>" +
74 "<line>\"By thy long grey beard and the glittering eye,</line>" +
75 "<line>Now wherefore stopp'st thou me?\"</line>" +
76 "</lines>" +
77 "</PoemBean>";
78 BeanReader reader = new BeanReader();
79 reader.registerBeanClass(PoemBean.class);
80 PoemBean bean = (PoemBean) reader.parse(new StringReader(xml));
81 assertNotNull("Expected bean to be output");
82 Object[] lines = bean.getLines().toArray();
83 assertEquals("Expected four lines", 4, lines.length);
84 assertEquals("First line of Rime Of The Ancient Mariner", "It is an ancient Mariner,", lines[0]);
85 assertEquals("Second line of Rime Of The Ancient Mariner", "And he stoppeth one of three.", lines[1]);
86 assertEquals("Third line of Rime Of The Ancient Mariner", "\"By thy long grey beard and the glittering eye,", lines[2]);
87 assertEquals("Fourth line of Rime Of The Ancient Mariner", "Now wherefore stopp'st thou me?\"", lines[3]);
88
89 }
90 }