1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.betwixt.io;
18
19 import java.io.IOException;
20 import java.util.ArrayList;
21 import java.util.List;
22
23 import org.apache.commons.betwixt.AbstractTestCase;
24 import org.apache.commons.betwixt.ElementDescriptor;
25 import org.apache.commons.betwixt.XMLBeanInfo;
26 import org.xml.sax.Attributes;
27 import org.xml.sax.SAXException;
28
29 /***
30 * @author <a href='http://jakarta.apache.org/'>Jakarta Commons Team</a>
31 * @version $Revision: 1.3 $
32 */
33 public class TestAbstractBeanWriter extends AbstractTestCase {
34
35 public TestAbstractBeanWriter(String testName) {
36 super(testName);
37 }
38
39 public void testContextCurrentElement() throws Exception {
40 MovieBean bean =
41 new MovieBean("Excalibur", 1981, new PersonBean("John", "Boorman"));
42 bean.addActor(new PersonBean("Nigel", "Terry"));
43 bean.addActor(new PersonBean("Helen", "Mirren"));
44 bean.addActor(new PersonBean("Nicol", "Williamson"));
45
46 TestWritingAPI writer = new TestWritingAPI();
47 writer.getXMLIntrospector().getConfiguration().setAttributesForPrimitives(true);
48 XMLBeanInfo personXmlBeanInfo
49 = writer.getXMLIntrospector().introspect(PersonBean.class);
50 XMLBeanInfo movieXmlBeanInfo
51 = writer.getXMLIntrospector().introspect(MovieBean.class);
52 writer.write(bean);
53
54 List expected = new ArrayList();
55 ElementDescriptor movieElementdescriptor
56 = movieXmlBeanInfo.getElementDescriptor();
57 ElementDescriptor nameDescriptor
58 = movieElementdescriptor.getElementDescriptors()[0];
59 ElementDescriptor yearDescriptor
60 = movieElementdescriptor.getElementDescriptors()[1];
61 ElementDescriptor directorDescriptor
62 = movieElementdescriptor.getElementDescriptors()[2];
63 ElementDescriptor actorsDescriptor
64 = movieElementdescriptor.getElementDescriptors()[3];
65 ElementDescriptor personDescriptor
66 = personXmlBeanInfo.getElementDescriptor();
67
68 expected.add(
69 new TestWritingAPI.Record(
70 TestWritingAPI.START_ELEMENT,
71 movieElementdescriptor));
72
73 expected.add(
74 new TestWritingAPI.Record(
75 TestWritingAPI.START_ELEMENT,
76 nameDescriptor));
77
78 expected.add(
79 new TestWritingAPI.Record(
80 TestWritingAPI.BODY_TEXT,
81 nameDescriptor));
82
83 expected.add(
84 new TestWritingAPI.Record(
85 TestWritingAPI.END_ELEMENT,
86 nameDescriptor));
87
88 expected.add(
89 new TestWritingAPI.Record(
90 TestWritingAPI.START_ELEMENT,
91 yearDescriptor));
92
93 expected.add(
94 new TestWritingAPI.Record(
95 TestWritingAPI.BODY_TEXT,
96 yearDescriptor));
97
98 expected.add(
99 new TestWritingAPI.Record(
100 TestWritingAPI.END_ELEMENT,
101 yearDescriptor));
102
103 expected.add(
104 new TestWritingAPI.Record(
105 TestWritingAPI.START_ELEMENT,
106 personDescriptor));
107
108 expected.add(
109 new TestWritingAPI.Record(
110 TestWritingAPI.END_ELEMENT,
111 personDescriptor));
112
113 expected.add(
114 new TestWritingAPI.Record(
115 TestWritingAPI.START_ELEMENT,
116 actorsDescriptor));
117
118 expected.add(
119 new TestWritingAPI.Record(
120 TestWritingAPI.START_ELEMENT,
121 personDescriptor));
122
123 expected.add(
124 new TestWritingAPI.Record(
125 TestWritingAPI.END_ELEMENT,
126 personDescriptor));
127
128 expected.add(
129 new TestWritingAPI.Record(
130 TestWritingAPI.START_ELEMENT,
131 personDescriptor));
132
133 expected.add(
134 new TestWritingAPI.Record(
135 TestWritingAPI.END_ELEMENT,
136 personDescriptor));
137
138 expected.add(
139 new TestWritingAPI.Record(
140 TestWritingAPI.START_ELEMENT,
141 personDescriptor));
142
143
144 expected.add(
145 new TestWritingAPI.Record(
146 TestWritingAPI.END_ELEMENT,
147 personDescriptor));
148 expected.add(
149 new TestWritingAPI.Record(
150 TestWritingAPI.END_ELEMENT,
151 actorsDescriptor));
152
153 expected.add(
154 new TestWritingAPI.Record(
155 TestWritingAPI.END_ELEMENT,
156 movieElementdescriptor));
157
158 assertEquals("Collections same size", expected.size(), writer.recording.size());
159
160 assertEquals("Movie element start", expected.get(0), writer.recording.get(0));
161 assertEquals("Name element start", expected.get(1), writer.recording.get(1));
162 assertEquals("Name element body", expected.get(2), writer.recording.get(2));
163 assertEquals("Name element end", expected.get(3), writer.recording.get(3));
164 assertEquals("Year element start", expected.get(4), writer.recording.get(4));
165 assertEquals("Year element body", expected.get(5), writer.recording.get(5));
166 assertEquals("Year element end", expected.get(6), writer.recording.get(6));
167 assertEquals("Director element start", expected.get(7), writer.recording.get(7));
168 assertEquals("Director element end", expected.get(8), writer.recording.get(8));
169 assertEquals("Actors element start", expected.get(9), writer.recording.get(9));;
170 assertEquals("Actor element body", expected.get(10), writer.recording.get(10));
171 assertEquals("Actor element end", expected.get(11), writer.recording.get(12));
172 assertEquals("Actor element body", expected.get(12), writer.recording.get(12));
173 assertEquals("Actor element end", expected.get(13), writer.recording.get(13));
174 assertEquals("Actor element body", expected.get(14), writer.recording.get(14));
175 assertEquals("Actor element end", expected.get(15), writer.recording.get(15));
176 assertEquals("Actors element end", expected.get(16), writer.recording.get(16));
177 assertEquals("Movie element end", expected.get(17), writer.recording.get(17));
178 }
179
180
181 public static class TestWritingAPI extends AbstractBeanWriter {
182
183 public static final int START_ELEMENT = 1;
184 public static final int BODY_TEXT = 2;
185 public static final int END_ELEMENT = 3;
186
187 private List recording = new ArrayList();
188
189 protected void bodyText(String text) throws IOException, SAXException {
190 throw new RuntimeException("Deprecated method called");
191 }
192
193
194 protected void bodyText(WriteContext context, String text)
195 throws IOException, SAXException {
196 recording.add(new Record(BODY_TEXT, context.getCurrentDescriptor()));
197 }
198
199 protected void endElement(String uri, String localName, String qName)
200 throws IOException, SAXException {
201 throw new RuntimeException("Deprecated method called");
202 }
203
204 protected void endElement(
205 WriteContext context,
206 String uri,
207 String localName,
208 String qName)
209 throws IOException, SAXException {;
210 recording.add(new Record(END_ELEMENT, context.getCurrentDescriptor()));
211 }
212
213 protected void startElement(
214 String uri,
215 String localName,
216 String qName,
217 Attributes attr)
218 throws IOException, SAXException {
219 throw new RuntimeException("Deprecated method called");
220 }
221
222 protected void startElement(
223 WriteContext context,
224 String uri,
225 String localName,
226 String qName,
227 Attributes attr)
228 throws IOException, SAXException {
229 recording.add(new Record(START_ELEMENT, context.getCurrentDescriptor()));
230 }
231
232 public static class Record {
233 ElementDescriptor currentDescriptor;
234 int type;
235
236 Record(int type, ElementDescriptor currentDescriptor) {
237 this.currentDescriptor = currentDescriptor;
238 this.type = type;
239 }
240
241 public int hashCode() {
242 return type;
243 }
244
245 public String toString() {
246 return "[Record: type=" + type + "; " + currentDescriptor + "]";
247 }
248
249 public boolean equals(Object arg) {
250 boolean result = false;
251 if (arg instanceof Record) {
252 Record record = (Record) arg;
253 result = (type == type)
254 && currentDescriptor.equals(record.currentDescriptor);
255 }
256 return result;
257 }
258
259 }
260 }
261 }