KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > activemq > tool > ReflectionUtilTest


1 /**
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one or more
4  * contributor license agreements. See the NOTICE file distributed with
5  * this work for additional information regarding copyright ownership.
6  * The ASF licenses this file to You under the Apache License, Version 2.0
7  * (the "License"); you may not use this file except in compliance with
8  * the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18 package org.apache.activemq.tool;
19
20 import junit.framework.TestCase;
21
22 import java.util.Properties JavaDoc;
23 import java.io.File JavaDoc;
24
25 import org.apache.activemq.tool.properties.ReflectionUtil;
26 import org.apache.activemq.tool.properties.ReflectionConfigurable;
27
28 public class ReflectionUtilTest extends TestCase {
29     public void testConfigurableOption() {
30         TestClass5 data = new TestClass5();
31
32         data.willIntercept = true;
33         ReflectionUtil.configureClass(data, "this-should-not-matter", "this-should-not-matter");
34         assertTrue(data.intercepted);
35
36         data.willIntercept = false;
37         data.nest = new TestClass5();
38         data.nest.willIntercept = true;
39         ReflectionUtil.configureClass(data, "nest.this-should-not-matter", "this-should-not-matter");
40         assertTrue(data.intercepted);
41         assertTrue(data.nest.intercepted);
42
43         data.willIntercept = false;
44         data.nest = new TestClass5();
45         data.nest.willIntercept = false;
46         data.nest.nest = new TestClass5();
47         data.nest.nest.willIntercept = true;
48         ReflectionUtil.configureClass(data, "nest.nest.this-should-not-matter", "this-should-not-matter");
49         assertTrue(data.intercepted);
50         assertTrue(data.nest.intercepted);
51         assertTrue(data.nest.nest.intercepted);
52
53         TestClass6 data2 = new TestClass6();
54         data2.nestConfig = new TestClass5();
55         data2.nestConfig.willIntercept = true;
56         ReflectionUtil.configureClass(data2, "nestConfig.this-should-not-matter", "this-should-not-matter");
57         assertTrue(data2.nestConfig.intercepted);
58
59         data2.nestNotConfig = new TestClass6();
60         data2.nestNotConfig.nestConfig = new TestClass5();
61         data2.nestNotConfig.nestConfig.willIntercept = true;
62         ReflectionUtil.configureClass(data2, "nestNotConfig.nestConfig.this-should-not-matter", "this-should-not-matter");
63         assertTrue(data2.nestNotConfig.nestConfig.intercepted);
64     }
65
66     public void testDataTypeConfig() {
67         TestClass3 targetObj = new TestClass3();
68
69         // Initialize variables;
70
targetObj.setBooleanData(false);
71         targetObj.setIntData(0);
72         targetObj.setLongData(0);
73         targetObj.setShortData((short)0);
74         targetObj.setDoubleData(0.0);
75         targetObj.setFloatData(0.0F);
76         targetObj.setByteData((byte)0);
77         targetObj.setCharData('0');
78         targetObj.setStringData("false");
79
80         // Set properties
81
Properties JavaDoc props = new Properties JavaDoc();
82         props.setProperty("booleanData", "true");
83         props.setProperty("intData", "1000");
84         props.setProperty("longData", "2000");
85         props.setProperty("shortData", "3000");
86         props.setProperty("doubleData", "1234.567");
87         props.setProperty("floatData", "9876.543");
88         props.setProperty("byteData", "127");
89         props.setProperty("charData", "A");
90         props.setProperty("stringData", "true");
91
92         ReflectionUtil.configureClass(targetObj, props);
93
94         // Check config
95
assertEquals(true, targetObj.isBooleanData());
96         assertEquals(1000, targetObj.getIntData());
97         assertEquals(2000, targetObj.getLongData());
98         assertEquals(3000, targetObj.getShortData());
99         assertEquals(1234.567, targetObj.getDoubleData(), 0.0001);
100         assertEquals(9876.543, targetObj.getFloatData(), 0.0001);
101         assertEquals(127, targetObj.getByteData());
102         assertEquals('A', targetObj.getCharData());
103         assertEquals("true", targetObj.getStringData());
104     }
105
106     public void testValueOfMethod() {
107         TestClass4 targetObj = new TestClass4();
108
109         ReflectionUtil.configureClass(targetObj, "testFile", "TEST.FOO.BAR");
110
111         assertEquals("TEST.FOO.BAR", targetObj.testFile.toString());
112     }
113
114     public void testGetProperties() {
115
116         TestClass3 testData = new TestClass3();
117         testData.setBooleanData(false);
118         testData.setByteData((byte)15);
119         testData.setCharData('G');
120         testData.setDoubleData(765.43);
121         testData.setFloatData(543.21F);
122         testData.setIntData(654321);
123         testData.setLongData(987654321);
124         testData.setShortData((short)4321);
125         testData.setStringData("BAR.TEST.FOO");
126
127         TestClass3 targetObj = new TestClass3();
128         targetObj.setBooleanData(true);
129         targetObj.setByteData((byte)10);
130         targetObj.setCharData('D');
131         targetObj.setDoubleData(1234.567);
132         targetObj.setFloatData(4567.89F);
133         targetObj.setIntData(123456);
134         targetObj.setLongData(1234567890);
135         targetObj.setShortData((short)1234);
136         targetObj.setStringData("Test.FOO.BAR");
137         targetObj.setTestData(testData);
138
139         Properties JavaDoc p = ReflectionUtil.retrieveObjectProperties(targetObj);
140         assertEquals("true", p.getProperty("booleanData"));
141         assertEquals("10", p.getProperty("byteData"));
142         assertEquals("D", p.getProperty("charData"));
143         assertEquals("1234.567", p.getProperty("doubleData"));
144         assertEquals("4567.89", p.getProperty("floatData"));
145         assertEquals("123456", p.getProperty("intData"));
146         assertEquals("1234567890", p.getProperty("longData"));
147         assertEquals("1234", p.getProperty("shortData"));
148         assertEquals("Test.FOO.BAR", p.getProperty("stringData"));
149         assertEquals("false", p.getProperty("testData.booleanData"));
150         assertEquals("15", p.getProperty("testData.byteData"));
151         assertEquals("G", p.getProperty("testData.charData"));
152         assertEquals("765.43", p.getProperty("testData.doubleData"));
153         assertEquals("543.21", p.getProperty("testData.floatData"));
154         assertEquals("654321", p.getProperty("testData.intData"));
155         assertEquals("987654321", p.getProperty("testData.longData"));
156         assertEquals("4321", p.getProperty("testData.shortData"));
157         assertEquals("BAR.TEST.FOO", p.getProperty("testData.stringData"));
158
159     }
160
161     public void testNestedConfig() {
162         TestClass3 t1 = new TestClass3();
163         TestClass3 t2 = new TestClass3();
164         TestClass3 t3 = new TestClass3();
165         TestClass3 t4 = new TestClass3();
166         TestClass3 t5 = new TestClass3();
167
168         ReflectionUtil.configureClass(t1, "stringData", "t1");
169         assertEquals("t1", t1.getStringData());
170
171         t1.setTestData(t2);
172         ReflectionUtil.configureClass(t1, "testData.stringData", "t2");
173         assertEquals("t2", t2.getStringData());
174
175         t2.setTestData(t3);
176         ReflectionUtil.configureClass(t1, "testData.testData.stringData", "t3");
177         assertEquals("t3", t3.getStringData());
178
179         t3.setTestData(t4);
180         ReflectionUtil.configureClass(t1, "testData.testData.testData.stringData", "t4");
181         assertEquals("t4", t4.getStringData());
182
183         t4.setTestData(t5);
184         ReflectionUtil.configureClass(t1, "testData.testData.testData.testData.stringData", "t5");
185         assertEquals("t5", t5.getStringData());
186     }
187
188     public class TestClass1 {
189         private boolean booleanData;
190         private int intData;
191         private long longData;
192
193         public boolean isBooleanData() {
194             return booleanData;
195         }
196
197         public void setBooleanData(boolean booleanData) {
198             this.booleanData = booleanData;
199         }
200
201         public int getIntData() {
202             return intData;
203         }
204
205         public void setIntData(int intData) {
206             this.intData = intData;
207         }
208
209         public long getLongData() {
210             return longData;
211         }
212
213         public void setLongData(long longData) {
214             this.longData = longData;
215         }
216     }
217
218     public class TestClass2 extends TestClass1 {
219         private float floatData;
220         private byte byteData;
221         private char charData;
222
223         public float getFloatData() {
224             return floatData;
225         }
226
227         public void setFloatData(float floatData) {
228             this.floatData = floatData;
229         }
230
231         public byte getByteData() {
232             return byteData;
233         }
234
235         public void setByteData(byte byteData) {
236             this.byteData = byteData;
237         }
238
239         public char getCharData() {
240             return charData;
241         }
242
243         public void setCharData(char charData) {
244             this.charData = charData;
245         }
246     }
247
248     public class TestClass3 extends TestClass2 {
249         private short shortData;
250         private double doubleData;
251         private String JavaDoc stringData;
252         private TestClass3 testData;
253
254         public short getShortData() {
255             return shortData;
256         }
257
258         public void setShortData(short shortData) {
259             this.shortData = shortData;
260         }
261
262         public double getDoubleData() {
263             return doubleData;
264         }
265
266         public void setDoubleData(double doubleData) {
267             this.doubleData = doubleData;
268         }
269
270         public String JavaDoc getStringData() {
271             return stringData;
272         }
273
274         public void setStringData(String JavaDoc stringData) {
275             this.stringData = stringData;
276         }
277
278         public TestClass3 getTestData() {
279             return testData;
280         }
281
282         public void setTestData(TestClass3 testData) {
283             this.testData = testData;
284         }
285     }
286
287     public class TestClass4 {
288         private File JavaDoc testFile;
289
290         public String JavaDoc getTestFile() {
291             return testFile.toString();
292         }
293
294         public void setTestFile(String JavaDoc testFile) {
295             this.testFile = new File JavaDoc(testFile);
296         }
297     }
298
299     public class TestClass5 implements ReflectionConfigurable {
300         public boolean intercepted = false;
301         public boolean willIntercept = true;
302         public TestClass5 nest = null;
303
304         public void configureProperties(Properties JavaDoc props) {
305             // Do nothing
306
}
307
308         public Properties JavaDoc retrieveProperties(Properties JavaDoc props) {
309             return null;
310         }
311
312         public boolean acceptConfig(String JavaDoc key, String JavaDoc val) {
313             intercepted = true;
314
315             return !willIntercept;
316         }
317
318         public TestClass5 getNest() {
319             return nest;
320         }
321
322         public void setNest(TestClass5 nest) {
323             this.nest = nest;
324         }
325     }
326
327     public class TestClass6 {
328         public TestClass6 nestNotConfig = null;
329         public TestClass5 nestConfig = null;
330
331         public TestClass6 getNestNotConfig() {
332             return nestNotConfig;
333         }
334
335         public void setNestNotConfig(TestClass6 nestNotConfig) {
336             this.nestNotConfig = nestNotConfig;
337         }
338
339         public TestClass5 getNestConfig() {
340             return nestConfig;
341         }
342
343         public void setNestConfig(TestClass5 nestConfig) {
344             this.nestConfig = nestConfig;
345         }
346     }
347 }
348
Popular Tags