View Javadoc
1   /*
2    * Copyright 2007 Werner Guttmann
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5    * in compliance with the License. You may obtain a copy of the License at
6    *
7    * http://www.apache.org/licenses/LICENSE-2.0
8    *
9    * Unless required by applicable law or agreed to in writing, software distributed under the License
10   * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11   * or implied. See the License for the specific language governing permissions and limitations under
12   * the License.
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   * Test case for testing various pieces of functionality of {@link Unmarshaller}.
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     * The Unmarshaller tests need an internal context and an input reader.
43     * 
44     * @see junit.framework.TestCase#setUp()
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     * Closing the reader.
55     * 
56     * @see junit.framework.TestCase#tearDown()
57     */
58    @After
59    public void tearDown() throws Exception {
60      _reader.close();
61      _reader = null;
62    }
63  
64    /**
65     * Tests usage of get-/setProperty() methods.
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     * Creates an Unmarshaller instance without any argument; sets the root class and calls unmarshal.
88     * 
89     * @throws Exception in case of unmarshal problems
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    * Creates an Unmarshaller instance without any argument; sets the root object and calls
102    * unmarshal.
103    * 
104    * @throws Exception in case of unmarshal problems
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    * Creates an Unmarshaller instance with root class and calls unmarshal.
117    * 
118    * @throws Exception in case of unmarshal problems
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    * Creates an Unmarshaller instance with root class and calls unmarshal.
130    * 
131    * @throws Exception in case of unmarshal problems
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    * Creates an Unmarshaller instance with context only; sets root class and calls unmarshal.
144    * 
145    * @throws Exception in case of unmarshal problems
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    * Creates an Unmarshaller instance with context only; sets root class and calls unmarshal.
158    * 
159    * @throws Exception in case of unmarshal problems
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    * Creates an Unmarshaller instance with context and root class and calls unmarshal.
169    * 
170    * @throws Exception in case of unmarshal problems
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    * Creates an Unmarshaller instance with context and root class and calls unmarshal.
182    * 
183    * @throws Exception in case of unmarshal problems
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    * Creates an Unmarshaller instance with context and root class and calls unmarshal.
193    * 
194    * @throws Exception in case of unmarshal problems
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    * Creates an Unmarshaller instance with context, class and class loader arguments and calls
207    * unmarshal.
208    * 
209    * @throws Exception in case of unmarshal problems
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    * Creates an Unmarshaller instance with context, class and class loader arguments and calls
222    * unmarshal.
223    * 
224    * @throws Exception in case of unmarshal problems
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    * Creates an Unmarshaller instance with context, class and class loader arguments and calls
234    * unmarshal.
235    * 
236    * @throws Exception in case of unmarshal problems
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    * Creates an Unmarshaller instance with context, class and class loader arguments and calls
249    * unmarshal.
250    * 
251    * @throws Exception in case of unmarshal problems
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    * Creates an Unmarshaller instance with an root object instance and calls unmarshal.
264    * 
265    * @throws Exception in case of unmarshal problems
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    * Creates an Unmarshaller instance with an root object instance and calls unmarshal.
277    * 
278    * @throws Exception in case of unmarshal problems
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    * Creates an Unmarshaller instance withcontext and an object instance argument and calls
291    * unmarshal.
292    * 
293    * @throws Exception in case of unmarshal problems
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    * Creates an Unmarshaller instance withcontext and an object instance argument and calls
305    * unmarshal.
306    * 
307    * @throws Exception in case of unmarshal problems
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    * Creates an Unmarshaller instance withcontext and an object instance argument and calls
317    * unmarshal.
318    * 
319    * @throws Exception in case of unmarshal problems
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 }