01: /*
02: * Copyright (C) 2004, 2005 Joe Walnes.
03: * Copyright (C) 2006, 2007, 2008 XStream Committers.
04: * All rights reserved.
05: *
06: * The software in this package is published under the terms of the BSD
07: * style license a copy of which has been included with this distribution in
08: * the LICENSE.txt file.
09: *
10: * Created on 09. May 2004 by Joe Walnes
11: */
12: package com.thoughtworks.acceptance;
13:
14: import com.thoughtworks.acceptance.objects.StandardObject;
15: import com.thoughtworks.xstream.XStream;
16: import com.thoughtworks.xstream.converters.reflection.ObjectAccessException;
17: import com.thoughtworks.xstream.converters.reflection.PureJavaReflectionProvider;
18: import com.thoughtworks.xstream.core.JVM;
19:
20: public class FinalFieldsTest extends AbstractAcceptanceTest {
21:
22: static class ThingWithFinalField extends StandardObject {
23: final int number = 9;
24: }
25:
26: public void testSerializeFinalFieldsIfSupported() {
27: xstream = new XStream(new JVM().bestReflectionProvider());
28: xstream.alias("thing", ThingWithFinalField.class);
29:
30: assertBothWays(new ThingWithFinalField(), "<thing>\n"
31: + " <number>9</number>\n" + "</thing>");
32: }
33:
34: public void testExceptionThrownUponSerializationIfNotSupport() {
35: xstream = new XStream(new PureJavaReflectionProvider());
36: xstream.alias("thing", ThingWithFinalField.class);
37:
38: try {
39: xstream.toXML(new ThingWithFinalField());
40: if (!JVM.is15()) {
41: fail("Expected exception");
42: }
43: } catch (ObjectAccessException expectedException) {
44: assertEquals("Invalid final field "
45: + ThingWithFinalField.class.getName() + ".number",
46: expectedException.getMessage());
47: }
48: }
49: }
|