KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > junitx > util > PrivateAccessorTest


1 package junitx.util;
2
3 import junit.framework.TestCase;
4 import junitx.example.PrivateAccessorExample;
5
6 /**
7  * @version $Revision: 1.2 $ $Date: 2003/03/21 06:13:50 $
8  * @author <a HREF="mailto:vbossica@users.sourceforge.net">Vladimir R. Bossicard</a>
9  */

10 public class PrivateAccessorTest
11         extends TestCase {
12
13     private PrivateAccessorExample example;
14     private ExampleSubclass example2;
15
16     public PrivateAccessorTest(String JavaDoc name) {
17         super(name);
18     }
19
20     public void setUp() {
21         this.example = new PrivateAccessorExample();
22         this.example2 = new ExampleSubclass();
23     }
24
25     public void testGetSetIntViaMethod()
26             throws Throwable JavaDoc {
27         Object JavaDoc res = PrivateAccessor.invoke(this.example, "setIntValue", new Class JavaDoc[]{int.class}, new Object JavaDoc[]{new Integer JavaDoc(1)});
28         assertNull(res);
29
30         res = PrivateAccessor.invoke(this.example, "getIntValue", new Class JavaDoc[0], new Object JavaDoc[0]);
31         assertNotNull(res);
32         assertEquals(new Integer JavaDoc(1), res);
33     }
34
35     public void testGetSetIntDirect()
36             throws Exception JavaDoc {
37         PrivateAccessor.setField(this.example, "intvalue", new Integer JavaDoc(1));
38
39         Object JavaDoc res = PrivateAccessor.getField(this.example, "intvalue");
40         assertNotNull(res);
41         assertEquals(new Integer JavaDoc(1), res);
42     }
43
44     public void testGetSetDoubleViaMethod()
45             throws Throwable JavaDoc {
46         Object JavaDoc res = PrivateAccessor.invoke(PrivateAccessorExample.class, "setDoubleValue", new Class JavaDoc[]{double.class}, new Object JavaDoc[]{new Double JavaDoc(1234.567)});
47         assertNull(res);
48
49         res = PrivateAccessor.invoke(PrivateAccessorExample.class, "getDoubleValue", new Class JavaDoc[0], new Object JavaDoc[0]);
50         assertNotNull(res);
51         assertEquals(1234.567, ((Double JavaDoc) res).doubleValue(), 0);
52     }
53
54     public void testGetSetDoubleDirect()
55             throws Exception JavaDoc {
56         PrivateAccessor.setField(PrivateAccessorExample.class, "doublevalue", new Double JavaDoc(1234.567));
57
58         Object JavaDoc res = PrivateAccessor.getField(PrivateAccessorExample.class, "doublevalue");
59         assertNotNull(res);
60         assertEquals(1234.567, ((Double JavaDoc) res).doubleValue(), 0);
61     }
62
63     public void testGetSetBoolViaMethod()
64             throws Throwable JavaDoc {
65         Object JavaDoc res = PrivateAccessor.invoke(this.example, "setBoolValue", new Class JavaDoc[]{Boolean JavaDoc.class}, new Object JavaDoc[]{new Boolean JavaDoc(false)});
66         assertNull(res);
67
68         res = PrivateAccessor.invoke(this.example, "getBoolValue", new Class JavaDoc[0], new Object JavaDoc[0]);
69         assertNotNull(res);
70         assertEquals(new Boolean JavaDoc(false), res);
71     }
72
73     public void testGetSetBoolDirect()
74             throws Exception JavaDoc {
75         PrivateAccessor.setField(this.example, "boolvalue", new Boolean JavaDoc(true));
76
77         Object JavaDoc res = PrivateAccessor.getField(this.example, "boolvalue");
78         assertNotNull(res);
79         assertEquals(new Boolean JavaDoc(true), res);
80     }
81
82     public void testSubclassGetSetIntViaMethod()
83             throws Throwable JavaDoc {
84         Object JavaDoc res = PrivateAccessor.invoke(this.example2, "setIntValue", new Class JavaDoc[]{int.class}, new Object JavaDoc[]{new Integer JavaDoc(8)});
85         assertNull(res);
86
87         res = PrivateAccessor.invoke(this.example2, "getIntValue", new Class JavaDoc[0], new Object JavaDoc[0]);
88         assertNotNull(res);
89         assertEquals(new Integer JavaDoc(10), res);
90     }
91
92     public void testSubclassGetSetIntDirect()
93             throws Exception JavaDoc {
94         PrivateAccessor.setField(this.example2, "intvalue", new Integer JavaDoc(8));
95
96         Object JavaDoc res = PrivateAccessor.getField(this.example2, "intvalue");
97         assertNotNull(res);
98         assertEquals(new Integer JavaDoc(8), res);
99     }
100
101     public void testSubclassGetSetBoolViaMethod()
102             throws Throwable JavaDoc {
103         Object JavaDoc res = PrivateAccessor.invoke(this.example2, "setBoolValue", new Class JavaDoc[]{Boolean JavaDoc.class}, new Object JavaDoc[]{new Boolean JavaDoc(false)});
104         assertNull(res);
105
106         res = PrivateAccessor.invoke(this.example2, "getBoolValue", new Class JavaDoc[0], new Object JavaDoc[0]);
107         assertNotNull(res);
108         assertEquals(new Boolean JavaDoc(false), res);
109     }
110
111     public void testSubclassGetSetBoolDirect()
112             throws Exception JavaDoc {
113         PrivateAccessor.setField(this.example2, "boolvalue", new Boolean JavaDoc(true));
114
115         Object JavaDoc res = PrivateAccessor.getField(this.example2, "boolvalue");
116         assertNotNull(res);
117         assertEquals(new Boolean JavaDoc(true), res);
118     }
119
120     public void testSubclassThrowingException()
121             throws Throwable JavaDoc {
122         try {
123             PrivateAccessor.invoke(this.example2, "throwingException", new Class JavaDoc[0], new Object JavaDoc[0]);
124         } catch (NullPointerException JavaDoc e) {
125             return;
126         }
127         fail();
128     }
129
130     public class ExampleSubclass
131             extends PrivateAccessorExample {
132
133         private int intvalue;
134
135         private int getIntValue() {
136             return this.intvalue + 2;
137         }
138
139         private void setIntValue(int intvalue) {
140             this.intvalue = intvalue;
141         }
142
143         private void throwingException() {
144             throw new NullPointerException JavaDoc();
145         }
146     }
147
148 }
149
Popular Tags