KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > thoughtworks > xstream > converters > reflection > AbstractReflectionProviderTest


1 package com.thoughtworks.xstream.converters.reflection;
2
3 import org.jmock.Mock;
4 import org.jmock.MockObjectTestCase;
5
6 public abstract class AbstractReflectionProviderTest extends MockObjectTestCase {
7
8     protected ReflectionProvider reflectionProvider;
9
10     public abstract ReflectionProvider createReflectionProvider();
11
12     protected void setUp() throws Exception JavaDoc {
13         super.setUp();
14         reflectionProvider = createReflectionProvider();
15     }
16
17     public void testConstructsStandardClass() {
18         assertCanCreate(OuterClass.class);
19     }
20
21     public void testConstructsStaticInnerClass() {
22         assertCanCreate(PublicStaticInnerClass.class);
23     }
24
25     public static class WithFields {
26         private int a;
27         private int b = 2;
28
29         public int getParentB() {
30             return b;
31         }
32     }
33
34     public void testVisitsEachFieldInClass() {
35         // setup
36
Mock mockBlock = new Mock(ReflectionProvider.Visitor.class);
37
38         // expect
39
mockBlock.expect(once())
40                 .method("visit")
41                 .with(eq("a"), eq(int.class), eq(WithFields.class), ANYTHING);
42         mockBlock.expect(once())
43                 .method("visit")
44                 .with(eq("b"), eq(int.class), eq(WithFields.class), ANYTHING);
45
46         // execute
47
reflectionProvider.visitSerializableFields(new WithFields(), (ReflectionProvider.Visitor) mockBlock.proxy());
48
49         // verify
50
mockBlock.verify();
51     }
52
53     public static class SubClassWithFields extends WithFields {
54         private int c;
55     }
56
57     public void testVisitsEachFieldInHeirarchy() {
58         // setup
59
Mock mockBlock = new Mock(ReflectionProvider.Visitor.class);
60
61         // expect
62
mockBlock.expect(once())
63                 .method("visit")
64                 .with(eq("a"), eq(int.class), eq(WithFields.class), ANYTHING);
65         mockBlock.expect(once())
66                 .method("visit")
67                 .with(eq("b"), eq(int.class), eq(WithFields.class), ANYTHING);
68         mockBlock.expect(once())
69                 .method("visit")
70                 .with(eq("c"), eq(int.class), eq(SubClassWithFields.class), ANYTHING);
71
72         // execute
73
reflectionProvider.visitSerializableFields(new SubClassWithFields(), (ReflectionProvider.Visitor) mockBlock.proxy());
74
75         // verify
76
mockBlock.verify();
77     }
78
79     public static class SubClassWithHiddenFields extends WithFields {
80         private int b = 3;
81
82         public int getChildB() {
83             return b;
84         }
85     }
86
87     public void testVisitsFieldsHiddenBySubclass() {
88         // setup
89
Mock mockBlock = new Mock(ReflectionProvider.Visitor.class);
90
91         // expect
92
mockBlock.expect(once())
93                 .method("visit")
94                 .with(eq("b"), eq(int.class), eq(SubClassWithHiddenFields.class), ANYTHING)
95                 .id("first");
96         mockBlock.expect(once())
97                 .method("visit")
98                 .with(eq("b"), eq(int.class), eq(WithFields.class), ANYTHING)
99                 .after("first");
100         mockBlock.expect(once())
101                 .method("visit")
102                 .with(eq("a"), ANYTHING, ANYTHING, ANYTHING);
103
104         // execute
105
reflectionProvider.visitSerializableFields(new SubClassWithHiddenFields(), (ReflectionProvider.Visitor) mockBlock.proxy());
106
107         // verify
108
mockBlock.verify();
109     }
110
111     public void testWritesHiddenFields() {
112         SubClassWithHiddenFields o = new SubClassWithHiddenFields();
113         reflectionProvider.writeField(o, "b", new Integer JavaDoc(10), null);
114         reflectionProvider.writeField(o, "b", new Integer JavaDoc(20), WithFields.class);
115         assertEquals(10, o.getChildB());
116         assertEquals(20, o.getParentB());
117     }
118
119     private void assertCanCreate(Class JavaDoc type) {
120         Object JavaDoc result = reflectionProvider.newInstance(type);
121         assertEquals(type, result.getClass());
122     }
123
124     protected void assertCannotCreate(Class JavaDoc type) {
125         try {
126             reflectionProvider.newInstance(type);
127             fail("Should not have been able to newInstance " + type);
128         } catch (ObjectAccessException goodException) {
129         }
130     }
131
132     public static class PublicStaticInnerClass {
133     }
134
135 }
136
137 class OuterClass {
138 }
139
140
Popular Tags