1 package org.apache.velocity.tools.generic;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import org.junit.*;
23 import static org.junit.Assert.*;
24 import java.lang.annotation.Annotation;
25 import java.util.HashMap;
26 import java.util.List;
27 import java.util.Map;
28 import java.util.Set;
29 import org.apache.velocity.runtime.log.Log;
30 import org.apache.velocity.tools.config.DefaultKey;
31 import org.apache.velocity.tools.config.SkipSetters;
32 import org.apache.velocity.tools.generic.ValueParser;
33 import org.apache.velocity.tools.view.AbstractSearchTool;
34
35
36
37
38
39
40
41
42 public class ClassToolTests {
43
44 public @Test void ctorClassTool() throws Exception
45 {
46 try
47 {
48 new ClassTool();
49 }
50 catch (Exception e)
51 {
52 fail("Default constructor failed");
53 }
54 }
55
56 public @Test void ctorClassTool_ClassToolClass() throws Exception
57 {
58
59 try
60 {
61 new ClassTool(null, ClassTool.class);
62 fail("Constructor 'ClassTool(null, Class)' worked but shouldn't have.");
63 }
64 catch (Exception e)
65 {
66 }
67
68 ClassTool parent = new ClassTool();
69
70 try
71 {
72 new ClassTool(parent, null);
73 fail("Constructor 'ClassTool(ClassTool, null)' worked but shouldn't have.");
74 }
75 catch (Exception e)
76 {
77 }
78
79
80 try
81 {
82 new ClassTool(parent, ClassToolTests.class);
83 }
84 catch (Exception e)
85 {
86 fail("Constructor 'ClassTool(ClassTool, Class)' failed due to: " + e);
87 }
88 }
89
90 public @Test void methodConfigure_Map() throws Exception
91 {
92 ClassTool classTool = new ClassTool();
93 assertEquals(Object.class, classTool.getType());
94
95
96 Map<String,Object> conf = new HashMap<String,Object>();
97 conf.put(ClassTool.INSPECT_KEY, "java.util.Map");
98 classTool.configure(conf);
99 assertEquals(Map.class, classTool.getType());
100
101 }
102
103 public @Test void methodGetAnnotations() throws Exception
104 {
105 ClassTool classTool = new ClassTool();
106
107 assertTrue(classTool.getAnnotations().isEmpty());
108 classTool.setType(MyDeprecated.class);
109 assertEquals(1, classTool.getAnnotations().size());
110 classTool.setType(ValueParser.class);
111 assertEquals(2, classTool.getAnnotations().size());
112 Class type0 = classTool.getAnnotations().get(0).annotationType();
113 Class type1 = classTool.getAnnotations().get(1).annotationType();
114 assertTrue(type0 != type1);
115 assertTrue(type0 == DefaultKey.class || type1 == DefaultKey.class);
116 assertTrue(type0 == SkipSetters.class || type1 == SkipSetters.class);
117 }
118
119 public @Test void methodGetConstructors() throws Exception
120 {
121 ClassTool classTool = new ClassTool();
122 List result = classTool.getConstructors();
123 assertNotNull(result);
124 assertFalse(result.isEmpty());
125
126 }
127
128
129
130 public @Test void methodGetFields() throws Exception
131 {
132 ClassTool classTool = new ClassTool();
133
134 List result = classTool.getFields();
135 assertNotNull(result);
136 assertTrue(result.isEmpty());
137
138 }
139
140
141
142 public @Test void methodGetMethods() throws Exception
143 {
144 ClassTool classTool = new ClassTool();
145
146 List result = classTool.getMethods();
147 assertNotNull(result);
148 assertFalse(result.isEmpty());
149
150 }
151
152
153
154 public @Test void methodGetTypes() throws Exception
155 {
156 ClassTool classTool = new ClassTool();
157
158 Set result = classTool.getTypes();
159 assertNotNull(result);
160 assertFalse(result.isEmpty());
161
162 }
163
164 public @Test void methodGetFullName() throws Exception
165 {
166 ClassTool classTool = new ClassTool();
167 String result = classTool.getFullName();
168 assertEquals(result, "java.lang.Object");
169 }
170
171 public @Test void methodGetName() throws Exception
172 {
173 ClassTool classTool = new ClassTool();
174
175 String result = classTool.getName();
176 assertEquals(classTool.getName(), "Object");
177 }
178
179 public @Test void methodGetPackage() throws Exception
180 {
181 ClassTool classTool = new ClassTool();
182
183 assertEquals(classTool.getPackage(), "java.lang");
184 }
185
186 public @Test void methodGetType() throws Exception
187 {
188 ClassTool classTool = new ClassTool();
189
190 Class result = classTool.getType();
191 assertEquals(result, Object.class);
192 classTool.setType(ClassTool.class);
193
194 result = classTool.getType();
195 assertEquals(result, ClassTool.class);
196 }
197
198 public @Test void methodGetSuper() throws Exception
199 {
200 ClassTool classTool = new ClassTool();
201
202 assertNull(classTool.getSuper());
203 classTool.setType(ClassTool.class);
204 assertEquals(classTool.getSuper().getType(), SafeConfig.class);
205 }
206
207 public @Test void methodInspect_Class() throws Exception
208 {
209 ClassTool classTool = new ClassTool();
210 ClassTool result = classTool.inspect(ClassTool.class);
211 assertEquals(result.getType(), ClassTool.class);
212 }
213
214 public @Test void methodInspect_Object() throws Exception
215 {
216 ClassTool classTool = new ClassTool();
217 ClassTool result = classTool.inspect(classTool);
218 assertEquals(result.getType(), ClassTool.class);
219 }
220
221 public @Test void methodInspect_String() throws Exception
222 {
223 ClassTool classTool = new ClassTool();
224 assertNull(classTool.inspect((String)null));
225 assertNull(classTool.inspect(""));
226 assertNull(classTool.inspect("bad"));
227 assertEquals(Map.class, classTool.inspect("java.util.Map").getType());
228 }
229
230 public @Test void methodIsAbstract() throws Exception
231 {
232 ClassTool classTool = new ClassTool();
233
234 assertFalse(classTool.isAbstract());
235 classTool.setType(AbstractSearchTool.class);
236 assertTrue(classTool.isAbstract());
237 }
238
239 public @Test void methodIsDeprecated() throws Exception
240 {
241 ClassTool classTool = new ClassTool();
242
243 assertFalse(classTool.isDeprecated());
244 classTool.setType(MyDeprecated.class);
245 assertTrue(classTool.isDeprecated());
246 }
247
248 @Deprecated
249 protected static class MyDeprecated
250 {
251
252 }
253
254 public @Test void methodIsFinal() throws Exception
255 {
256 ClassTool classTool = new ClassTool();
257
258 assertFalse(classTool.isFinal());
259 classTool.setType(String.class);
260 assertTrue(classTool.isFinal());
261 }
262
263 public @Test void methodIsInterface() throws Exception
264 {
265 ClassTool classTool = new ClassTool();
266
267 assertFalse(classTool.isInterface());
268 classTool.setType(Map.class);
269 assertTrue(classTool.isInterface());
270 }
271
272 public @Test void methodIsPrivate() throws Exception
273 {
274 ClassTool classTool = new ClassTool();
275
276 assertFalse(classTool.isPrivate());
277 classTool.setType(PrivateStrictStatic.class);
278 assertTrue(classTool.isPrivate());
279 }
280
281 public @Test void methodIsProtected() throws Exception
282 {
283 ClassTool classTool = new ClassTool();
284
285 assertFalse(classTool.isProtected());
286 classTool.setType(ProtectedNoDefaultCtor.class);
287 assertTrue(classTool.isProtected());
288 }
289
290 public @Test void methodIsPublic() throws Exception
291 {
292 ClassTool classTool = new ClassTool();
293
294 assertTrue(classTool.isPublic());
295 classTool.setType(PrivateStrictStatic.class);
296 assertFalse(classTool.isPublic());
297 }
298
299 public @Test void methodIsStatic() throws Exception
300 {
301 ClassTool classTool = new ClassTool();
302
303 assertFalse(classTool.isStatic());
304 classTool.setType(PrivateStrictStatic.class);
305 assertTrue(classTool.isStatic());
306 }
307
308
309
310
311
312
313
314
315
316
317
318
319
320 public @Test void methodSetClass_Class() throws Exception
321 {
322 ClassTool classTool = new ClassTool();
323 assertEquals(Object.class, classTool.getType());
324 classTool.setType(ClassTool.class);
325 assertEquals(ClassTool.class, classTool.getType());
326 }
327
328 public @Test void methodSupportsNewInstance() throws Exception
329 {
330 ClassTool classTool = new ClassTool();
331
332 assertEquals(classTool.supportsNewInstance(), true);
333 classTool.setType(ProtectedNoDefaultCtor.class);
334 assertEquals(classTool.supportsNewInstance(), false);
335 }
336
337 private static strictfp class PrivateStrictStatic {}
338
339 protected static class ProtectedNoDefaultCtor
340 {
341 public ProtectedNoDefaultCtor(String foo)
342 {
343 }
344 }
345
346 }
347