001: /*
002: * Copyright (C) 2004 Joe Walnes.
003: * Copyright (C) 2006, 2007 XStream Committers.
004: * All rights reserved.
005: *
006: * The software in this package is published under the terms of the BSD
007: * style license a copy of which has been included with this distribution in
008: * the LICENSE.txt file.
009: *
010: * Created on 14. May 2004 by Joe Walnes
011: */
012: package com.thoughtworks.xstream.converters.reflection;
013:
014: import org.jmock.Mock;
015: import org.jmock.MockObjectTestCase;
016:
017: public abstract class AbstractReflectionProviderTest extends
018: MockObjectTestCase {
019:
020: protected ReflectionProvider reflectionProvider;
021:
022: public abstract ReflectionProvider createReflectionProvider();
023:
024: protected void setUp() throws Exception {
025: super .setUp();
026: reflectionProvider = createReflectionProvider();
027: }
028:
029: public void testConstructsStandardClass() {
030: assertCanCreate(OuterClass.class);
031: }
032:
033: public void testConstructsStaticInnerClass() {
034: assertCanCreate(PublicStaticInnerClass.class);
035: }
036:
037: public static class WithFields {
038: private int a;
039: private int b = 2;
040:
041: public int getParentB() {
042: return b;
043: }
044: }
045:
046: public void testVisitsEachFieldInClass() {
047: // setup
048: Mock mockBlock = new Mock(ReflectionProvider.Visitor.class);
049:
050: // expect
051: mockBlock.expects(once()).method("visit").with(eq("a"),
052: eq(int.class), eq(WithFields.class), ANYTHING);
053: mockBlock.expects(once()).method("visit").with(eq("b"),
054: eq(int.class), eq(WithFields.class), ANYTHING);
055:
056: // execute
057: reflectionProvider.visitSerializableFields(new WithFields(),
058: (ReflectionProvider.Visitor) mockBlock.proxy());
059:
060: // verify
061: mockBlock.verify();
062: }
063:
064: public static class SubClassWithFields extends WithFields {
065: private int c;
066: }
067:
068: public void testVisitsEachFieldInHeirarchy() {
069: // setup
070: Mock mockBlock = new Mock(ReflectionProvider.Visitor.class);
071:
072: // expect
073: mockBlock.expects(once()).method("visit").with(eq("a"),
074: eq(int.class), eq(WithFields.class), ANYTHING);
075: mockBlock.expects(once()).method("visit").with(eq("b"),
076: eq(int.class), eq(WithFields.class), ANYTHING);
077: mockBlock.expects(once()).method("visit").with(eq("c"),
078: eq(int.class), eq(SubClassWithFields.class), ANYTHING);
079:
080: // execute
081: reflectionProvider.visitSerializableFields(
082: new SubClassWithFields(),
083: (ReflectionProvider.Visitor) mockBlock.proxy());
084:
085: // verify
086: mockBlock.verify();
087: }
088:
089: public static class SubClassWithHiddenFields extends WithFields {
090: private int b = 3;
091:
092: public int getChildB() {
093: return b;
094: }
095: }
096:
097: public void testVisitsFieldsHiddenBySubclass() {
098: // setup
099: Mock mockBlock = new Mock(ReflectionProvider.Visitor.class);
100:
101: // expect
102: mockBlock.expects(once()).method("visit").with(eq("b"),
103: eq(int.class), eq(WithFields.class), ANYTHING).id(
104: "first");
105: mockBlock.expects(once()).method("visit").with(eq("b"),
106: eq(int.class), eq(SubClassWithHiddenFields.class),
107: ANYTHING).after("first");
108: mockBlock.expects(once()).method("visit").with(eq("a"),
109: ANYTHING, ANYTHING, ANYTHING);
110:
111: // execute
112: reflectionProvider.visitSerializableFields(
113: new SubClassWithHiddenFields(),
114: (ReflectionProvider.Visitor) mockBlock.proxy());
115:
116: // verify
117: mockBlock.verify();
118: }
119:
120: public void testWritesHiddenFields() {
121: SubClassWithHiddenFields o = new SubClassWithHiddenFields();
122: reflectionProvider.writeField(o, "b", new Integer(10), null);
123: reflectionProvider.writeField(o, "b", new Integer(20),
124: WithFields.class);
125: assertEquals(10, o.getChildB());
126: assertEquals(20, o.getParentB());
127: }
128:
129: protected void assertCanCreate(Class type) {
130: Object result = reflectionProvider.newInstance(type);
131: assertEquals(type, result.getClass());
132: }
133:
134: protected void assertCannotCreate(Class type) {
135: try {
136: reflectionProvider.newInstance(type);
137: fail("Should not have been able to newInstance " + type);
138: } catch (ObjectAccessException goodException) {
139: }
140: }
141:
142: public static class PublicStaticInnerClass {
143: }
144:
145: }
146:
147: class OuterClass {
148: }
|