KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > openedit > modules > ReflectorTest


1 /*
2  * Created on Dec 7, 2003
3  *
4 /*
5 Copyright (c) 2003 eInnovation Inc. All rights reserved
6
7 This library is free software; you can redistribute it and/or modify it under the terms
8 of the GNU Lesser General Public License as published by the Free Software Foundation;
9 either version 2.1 of the License, or (at your option) any later version.
10
11 This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
12 without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13 See the GNU Lesser General Public License for more details.
14 */

15 package com.openedit.modules;
16
17 import java.util.Date JavaDoc;
18 import java.util.List JavaDoc;
19
20 import junit.framework.TestCase;
21
22 import com.openedit.TestWebPageRequest;
23 import com.openedit.WebPageRequest;
24 import com.openedit.modules.reflect.Reflector;
25
26 /**
27  * @author Matt Avery, mavery@einnovation.com
28  */

29 public class ReflectorTest extends TestCase
30 {
31     public static final String JavaDoc KEY = "test-object.testMethod";
32     protected Reflector fieldReflector;
33     protected WebPageRequest fieldContext;
34
35     public ReflectorTest(String JavaDoc arg0)
36     {
37         super(arg0);
38     }
39
40     // public static void main(String[] args)
41
// {
42
// junit.swingui.TestRunner.run(ReflectorTest.class);
43
// }
44

45     protected void setUp() throws Exception JavaDoc
46     {
47         WebPageRequest context = getWebPageContext();
48         context.setRequestParameter(KEY, "Hello");
49     }
50
51     public void testFindMethod()
52     {
53         class TestObject
54         {
55             public void testMethod(WebPageRequest inContext)
56             {
57             }
58         }
59         Object JavaDoc sessionObject = new TestObject();
60         int dotIndex = KEY.indexOf('.');
61         String JavaDoc methodName = KEY.substring(dotIndex + 1, KEY.length());
62         List JavaDoc methods = getReflector().findMethods(sessionObject, methodName);
63         assertEquals( 1, methods.size() );
64         class TestObject2
65         {
66             public void notTheMethod(WebPageRequest inContext)
67             {
68             }
69         }
70         sessionObject = new TestObject2();
71         methods = getReflector().findMethods(sessionObject, methodName);
72         assertEquals( 0, methods.size() );
73         
74         class TestObject3
75         {
76             protected boolean fieldDateStringBoolean;
77             protected boolean fieldDateBoolean;
78             public void setDate(String JavaDoc inDateString)
79             {
80                 fieldDateStringBoolean = true;
81             }
82             
83             public void setDate( Date JavaDoc inDate )
84             {
85                 fieldDateBoolean = true;
86             }
87             
88             public boolean dateStringSet()
89             {
90                 return fieldDateStringBoolean;
91             }
92             
93             public boolean dateSet()
94             {
95                 return fieldDateBoolean;
96             }
97         }
98         Object JavaDoc testObject = new TestObject3();
99         methods = getReflector().findMethods(testObject, "setDate");
100         assertEquals( 2, methods.size() );
101     }
102
103     public void testReflectOnRequestParameters() throws Exception JavaDoc
104     {
105         TestObject testObject = new TestObject();
106         getWebPageContext().setRequestParameter("setString", "Hello");
107         getWebPageContext().setRequestParameter("setSomething", "goodbye");
108         getWebPageContext().setRequestParameter("setStringArray", "Hello");
109         getWebPageContext().setRequestParameter("setInt", "5");
110     // getWebPageContext().setRequestParameter("setInvalidSetter", "blah");
111
getReflector().reflectOnRequestParameters(getWebPageContext(), testObject);
112         assertEquals("Hello", testObject.getString());
113         assertEquals("Hello", testObject.getStringArray()[0]);
114         assertEquals(5, testObject.getInt());
115         assertTrue( !testObject.invalidSetterCalled );
116         
117         getWebPageContext().setRequestParameter(
118             "setStringArray",
119             new String JavaDoc[] { "Yahoo", "Goodbye" });
120         getReflector().reflectOnRequestParameters(getWebPageContext(), testObject);
121         assertEquals("Yahoo", testObject.getStringArray()[0]);
122         assertEquals("Goodbye", testObject.getStringArray()[1]);
123     }
124
125     public Reflector getReflector()
126     {
127         if (fieldReflector == null)
128         {
129             fieldReflector = new Reflector();
130         }
131         return fieldReflector;
132     }
133
134     private WebPageRequest getWebPageContext()
135     {
136         if (fieldContext == null)
137         {
138             fieldContext = new TestWebPageRequest();
139         }
140         return fieldContext;
141     }
142     
143 }
144
Popular Tags