1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28 package org.objectledge.selector;
29
30 import java.util.HashMap;
31 import java.util.Map;
32
33 import junit.framework.TestCase;
34
35 /***
36 *
37 * @author <a href="mailto:rafal@caltha.pl">Rafal Krzewski</a>
38 * @version $Id: IntrospectionVariablesTest.java,v 1.4 2005/02/10 17:47:00 rafal Exp $
39 */
40 public class IntrospectionVariablesTest extends TestCase
41 {
42 /***
43 * Constructor for IntrospectionVariablesTest.
44 * @param arg0
45 */
46 public IntrospectionVariablesTest(String arg0)
47 {
48 super(arg0);
49 }
50
51 public void testIsDefined()
52 throws Exception
53 {
54 Variables variables;
55
56 variables = new IntrospectionVariables(new PlainObject());
57 assertTrue(variables.isDefined("publicMethod"));
58 assertTrue(variables.isDefined("publicBooleanMethod"));
59 assertTrue(variables.isDefined("publicBooleanPrimitiveMethod"));
60 assertFalse(variables.isDefined("undefined"));
61 assertFalse(variables.isDefined("voidPublicMethod"));
62 assertFalse(variables.isDefined("nonBooleanIsMethod"));
63
64 variables = new IntrospectionVariables(new StringGetObject());
65 assertTrue(variables.isDefined("defined"));
66
67
68
69 Map map = new HashMap();
70 map.put("defined", "defined");
71 map.put("null", null);
72 variables = new IntrospectionVariables(map);
73 assertTrue(variables.isDefined("defined"));
74
75
76
77 }
78
79 public void testNesting()
80 throws Exception
81 {
82 Variables variables;
83 variables = new IntrospectionVariables(new PlainObject());
84 assertTrue(variables.isDefined("nested.publicMethod"));
85 assertTrue(variables.isDefined("nested.publicBooleanMethod"));
86 assertTrue(variables.isDefined("nested.publicBooleanPrimitiveMethod"));
87 assertFalse(variables.isDefined("nested.undefined"));
88 assertFalse(variables.isDefined("nested.voidPublicMethod"));
89 assertFalse(variables.isDefined("nested.nonBooleanIsMethod"));
90 }
91
92 public void testGet()
93 throws Exception
94 {
95 Variables variables;
96 variables = new IntrospectionVariables(new PlainObject());
97 assertEquals("publicMethod", variables.get("nested.publicMethod"));
98 assertEquals(Boolean.TRUE, variables.get("publicBooleanMethod"));
99 assertEquals(Boolean.TRUE, variables.get("publicBooleanPrimitiveMethod"));
100
101 assertEquals("publicMethod", variables.get("nested.publicMethod"));
102 assertEquals(Boolean.TRUE, variables.get("nested.publicBooleanMethod"));
103 assertEquals(Boolean.TRUE, variables.get("nested.publicBooleanPrimitiveMethod"));
104
105 try
106 {
107 variables.get("undefined");
108 fail("exception expected");
109 }
110 catch(Exception e)
111 {
112 assertEquals("Undefined variable undefined", e.getMessage());
113 }
114
115 try
116 {
117 variables.get("nested.undefined");
118 fail("exception expected");
119 }
120 catch(Exception e)
121 {
122 assertEquals("Undefined variable nested.undefined", e.getMessage());
123 }
124 }
125
126 public void testExceptions()
127 {
128 Variables variables;
129 variables = new IntrospectionVariables(new PlainObject());
130 try
131 {
132 variables.get("failing");
133 fail("exception expected");
134 }
135 catch(Exception e)
136 {
137 assertEquals(EvaluationException.class, e.getClass());
138 assertEquals(IllegalStateException.class, e.getCause().getClass());
139 }
140 variables = new IntrospectionVariables(new StringGetObject());
141 try
142 {
143 variables.get("failing");
144 fail("exception expected");
145 }
146 catch(Exception e)
147 {
148 assertEquals(EvaluationException.class, e.getClass());
149 assertEquals(IllegalStateException.class, e.getCause().getClass());
150 }
151
152 variables = new IntrospectionVariables(new FailingObjectGetObject());
153 try
154 {
155 variables.get("failing");
156 fail("exception expected");
157 }
158 catch(Exception e)
159 {
160 assertEquals(EvaluationException.class, e.getClass());
161 assertEquals(IllegalStateException.class, e.getCause().getClass());
162 }
163 }
164
165 private static class PlainObject
166 {
167 public String getPublicMethod()
168 {
169 return "publicMethod";
170 }
171
172 public void getVoidPublicMethod()
173 {
174
175 }
176
177 public String isNonBooleanIsMethod()
178 {
179 return null;
180 }
181
182 public boolean isPublicBooleanPrimitiveMethod()
183 {
184 return true;
185 }
186
187 public Boolean isPublicBooleanMethod()
188 {
189 return Boolean.TRUE;
190 }
191
192 public PlainObject getNested()
193 {
194 return this;
195 }
196
197 public Object getFailing()
198 {
199 throw new IllegalStateException("failed");
200 }
201 }
202
203 private static class StringGetObject
204 {
205 public String get(String name)
206 {
207 if(name.equals("defined"))
208 {
209 return "defined";
210 }
211 else if(name.equals("failing"))
212 {
213 throw new IllegalStateException("failed");
214 }
215 else
216 {
217 return null;
218 }
219 }
220 }
221
222 private static class FailingObjectGetObject
223 {
224 public Object get(Object param)
225 {
226 throw new IllegalStateException("failed");
227 }
228 }
229 }