KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > thoughtworks > acceptance > FinalFieldsTest


1 package com.thoughtworks.acceptance;
2
3 import com.thoughtworks.xstream.XStream;
4 import com.thoughtworks.xstream.converters.reflection.ObjectAccessException;
5 import com.thoughtworks.xstream.converters.reflection.PureJavaReflectionProvider;
6 import com.thoughtworks.xstream.converters.reflection.Sun14ReflectionProvider;
7
8 public class FinalFieldsTest extends AbstractAcceptanceTest {
9
10     class ThingWithFinalField extends StandardObject {
11         final int number = 9;
12     }
13
14     public void testSerializeFinalFieldsIfSupported() {
15         xstream = new XStream(new Sun14ReflectionProvider());
16         xstream.alias("thing", ThingWithFinalField.class);
17
18         assertBothWays(new ThingWithFinalField(),
19                 "<thing>\n" +
20                 " <number>9</number>\n" +
21                 "</thing>");
22     }
23
24     public void testExceptionThrownUponSerializationIfNotSupport() {
25         xstream = new XStream(new PureJavaReflectionProvider());
26         xstream.alias("thing", ThingWithFinalField.class);
27
28         try {
29             xstream.toXML(new ThingWithFinalField());
30             fail("Expected exception");
31         } catch (ObjectAccessException expectedException) {
32             assertEquals("Invalid final field " + ThingWithFinalField.class.getName() + ".number",
33                     expectedException.getMessage());
34         }
35     }
36 }
37
Popular Tags