1
2
3
4
5
6
7
8
9
10
11
12
13
14 package org.exolab.castor.xml;
15
16 import static org.junit.Assert.assertEquals;
17 import static org.junit.Assert.assertNotNull;
18
19 import java.io.Reader;
20 import java.io.StringReader;
21
22 import org.castor.xml.InternalContext;
23 import org.castor.xml.XMLProperties;
24 import org.junit.After;
25 import org.junit.Assert;
26 import org.junit.Before;
27 import org.junit.Test;
28
29
30
31
32 public class TestUnmarshaller {
33
34 private static final String testXML =
35 "<?xml version=\"1.0\" encoding=\"UTF-8\"?><UnmarshalFranz content=\"Bla Bla Bla\" />";
36
37 private Reader _reader;
38
39 private InternalContext _internalContext;
40
41
42
43
44
45
46 @Before
47 public void setUp() {
48 XMLContext xmlContext = new XMLContext();
49 _internalContext = xmlContext.getInternalContext();
50 _reader = new StringReader(testXML);
51 }
52
53
54
55
56
57
58 @After
59 public void tearDown() throws Exception {
60 _reader.close();
61 _reader = null;
62 }
63
64
65
66
67 @Test
68 public void testSetProperty() {
69
70 XMLContext xmlContext = new XMLContext();
71 Unmarshaller unmarshaller = xmlContext.createUnmarshaller();
72 assertNotNull(unmarshaller);
73
74 String lenientSequenceValidation =
75 unmarshaller.getProperty(XMLProperties.LENIENT_SEQUENCE_ORDER);
76 assertNotNull(lenientSequenceValidation);
77 assertEquals("false", lenientSequenceValidation);
78
79 unmarshaller.setProperty(XMLProperties.LENIENT_SEQUENCE_ORDER, "true");
80
81 lenientSequenceValidation = unmarshaller.getProperty(XMLProperties.LENIENT_SEQUENCE_ORDER);
82 assertNotNull(lenientSequenceValidation);
83 assertEquals("true", lenientSequenceValidation);
84 }
85
86
87
88
89
90
91 @Test
92 public void testUnmarshallerNoArgs() throws Exception {
93 Unmarshaller u = new Unmarshaller();
94 u.setClass(UnmarshalFranz.class);
95 UnmarshalFranz f = (UnmarshalFranz) u.unmarshal(_reader);
96 Assert.assertNotNull(f);
97 Assert.assertEquals("Bla Bla Bla", f.getContent());
98 }
99
100
101
102
103
104
105
106 @Test
107 public void testUnmarshallerNoArgs2() throws Exception {
108 Unmarshaller u = new Unmarshaller();
109 u.setObject(new UnmarshalFranz());
110 UnmarshalFranz f = (UnmarshalFranz) u.unmarshal(_reader);
111 Assert.assertNotNull(f);
112 Assert.assertEquals("Bla Bla Bla", f.getContent());
113 }
114
115
116
117
118
119
120 @Test
121 public void testUnmarshallerClassArg() throws Exception {
122 Unmarshaller u = new Unmarshaller(UnmarshalFranz.class);
123 UnmarshalFranz f = (UnmarshalFranz) u.unmarshal(_reader);
124 Assert.assertNotNull(f);
125 Assert.assertEquals("Bla Bla Bla", f.getContent());
126 }
127
128
129
130
131
132
133 @Test
134 public void testUnmarshallerClassArgNull() throws Exception {
135 Unmarshaller u = new Unmarshaller((Class<?>) null);
136 u.setClass(UnmarshalFranz.class);
137 UnmarshalFranz f = (UnmarshalFranz) u.unmarshal(_reader);
138 Assert.assertNotNull(f);
139 Assert.assertEquals("Bla Bla Bla", f.getContent());
140 }
141
142
143
144
145
146
147 @Test
148 public void testUnmarshallerCtxArg() throws Exception {
149 Unmarshaller u = new Unmarshaller(_internalContext);
150 u.setClass(UnmarshalFranz.class);
151 UnmarshalFranz f = (UnmarshalFranz) u.unmarshal(_reader);
152 Assert.assertNotNull(f);
153 Assert.assertEquals("Bla Bla Bla", f.getContent());
154 }
155
156
157
158
159
160
161 @Test(expected = IllegalArgumentException.class)
162 public void testUnmarshallerCtxArgNull() throws Exception {
163 new Unmarshaller((InternalContext) null);
164 Assert.fail("It must not be possible to instantiate Unmarshaller with internalContext == null");
165 }
166
167
168
169
170
171
172 @Test
173 public void testUnmarshallerCtxClassArg() throws Exception {
174 Unmarshaller u = new Unmarshaller(_internalContext, UnmarshalFranz.class);
175 UnmarshalFranz f = (UnmarshalFranz) u.unmarshal(_reader);
176 Assert.assertNotNull(f);
177 Assert.assertEquals("Bla Bla Bla", f.getContent());
178 }
179
180
181
182
183
184
185 @Test(expected = IllegalArgumentException.class)
186 public void testUnmarshallerCtxClassArgNullNull() throws Exception {
187 new Unmarshaller((InternalContext) null, (Class<?>) null);
188 Assert.fail("It must not be possible to instantiate Unmarshaller with internalContext == null");
189 }
190
191
192
193
194
195
196 @Test
197 public void testUnmarshallerCtxClassArgNull() throws Exception {
198 Unmarshaller u = new Unmarshaller(_internalContext, (Class<?>) null);
199 u.setClass(UnmarshalFranz.class);
200 UnmarshalFranz f = (UnmarshalFranz) u.unmarshal(_reader);
201 Assert.assertNotNull(f);
202 Assert.assertEquals("Bla Bla Bla", f.getContent());
203 }
204
205
206
207
208
209
210
211 @Test
212 public void testUnmarshallerCtxClassClassloaderArg() throws Exception {
213 Unmarshaller u = new Unmarshaller(_internalContext, UnmarshalFranz.class,
214 UnmarshalFranz.class.getClassLoader());
215 UnmarshalFranz f = (UnmarshalFranz) u.unmarshal(_reader);
216 Assert.assertNotNull(f);
217 Assert.assertEquals("Bla Bla Bla", f.getContent());
218 }
219
220
221
222
223
224
225
226 @Test(expected = IllegalArgumentException.class)
227 public void testUnmarshallerCtxClassClassloaderArgNullNullNull() throws Exception {
228 new Unmarshaller((InternalContext) null, (Class<?>) null, (ClassLoader) null);
229 Assert.fail("It must not be possible to instantiate Unmarshaller with internalContext == null");
230 }
231
232
233
234
235
236
237
238 @Test
239 public void testUnmarshallerCtxClassClassloaderArgNullNull() throws Exception {
240 Unmarshaller u = new Unmarshaller(_internalContext, (Class<?>) null, (ClassLoader) null);
241 u.setClass(UnmarshalFranz.class);
242 UnmarshalFranz f = (UnmarshalFranz) u.unmarshal(_reader);
243 Assert.assertNotNull(f);
244 Assert.assertEquals("Bla Bla Bla", f.getContent());
245 }
246
247
248
249
250
251
252
253 @Test
254 public void testUnmarshallerCtxClassClassloaderArgNull() throws Exception {
255 Unmarshaller u = new Unmarshaller(_internalContext, UnmarshalFranz.class, (ClassLoader) null);
256 u.setClass(UnmarshalFranz.class);
257 UnmarshalFranz f = (UnmarshalFranz) u.unmarshal(_reader);
258 Assert.assertNotNull(f);
259 Assert.assertEquals("Bla Bla Bla", f.getContent());
260 }
261
262
263
264
265
266
267 @Test
268 public void testUnmarshallerObjectArg() throws Exception {
269 Unmarshaller u = new Unmarshaller(new UnmarshalFranz());
270 UnmarshalFranz f = (UnmarshalFranz) u.unmarshal(_reader);
271 Assert.assertNotNull(f);
272 Assert.assertEquals("Bla Bla Bla", f.getContent());
273 }
274
275
276
277
278
279
280 @Test
281 public void testUnmarshallerObjectArgNull() throws Exception {
282 Unmarshaller u = new Unmarshaller((Object) null);
283 u.setObject(new UnmarshalFranz());
284 UnmarshalFranz f = (UnmarshalFranz) u.unmarshal(_reader);
285 Assert.assertNotNull(f);
286 Assert.assertEquals("Bla Bla Bla", f.getContent());
287 }
288
289
290
291
292
293
294
295 @Test
296 public void testUnmarshallerCtxObjectArg() throws Exception {
297 Unmarshaller u = new Unmarshaller(_internalContext, new UnmarshalFranz());
298 UnmarshalFranz f = (UnmarshalFranz) u.unmarshal(_reader);
299 Assert.assertNotNull(f);
300 Assert.assertEquals("Bla Bla Bla", f.getContent());
301 }
302
303
304
305
306
307
308
309 @Test(expected = IllegalArgumentException.class)
310 public void testUnmarshallerCtxObjectArgNullNull() throws Exception {
311 new Unmarshaller((InternalContext) null, (Object) null);
312 Assert.fail("It must not be possible to instantiate Unmarshaller with internalContext == null");
313 }
314
315
316
317
318
319
320
321 @Test
322 public void testUnmarshallerCtxObjectArgNull() throws Exception {
323 Unmarshaller u = new Unmarshaller(_internalContext, (Object) null);
324 u.setObject(new UnmarshalFranz());
325 UnmarshalFranz f = (UnmarshalFranz) u.unmarshal(_reader);
326 Assert.assertNotNull(f);
327 Assert.assertEquals("Bla Bla Bla", f.getContent());
328 }
329 }