View Javadoc

1   package org.apache.velocity.tools.generic;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
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   * <p>Tests for {@link ClassTool}</p>
37   *
38   * @author Nathan Bubna
39   * @since VelocityTools 2.0
40   * @version $Id$
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          // null parent should fail
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          // null class should fail
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          // this one should work
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          // change the inspected type to Map
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         //TODO: test other configuration settings
101     }
102 
103     public @Test void methodGetAnnotations() throws Exception
104     {
105         ClassTool classTool = new ClassTool();
106         // default type is java.lang.Object
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         //TODO: test contents of list?
126     }
127 
128     //TODO: add ConstructorSub tests
129 
130     public @Test void methodGetFields() throws Exception
131     {
132         ClassTool classTool = new ClassTool();
133         // default type is java.lang.Object
134         List result = classTool.getFields();
135         assertNotNull(result);
136         assertTrue(result.isEmpty());
137         //TODO: test a class that does have fields
138     }
139 
140     //TODO: add FieldSub tests
141 
142     public @Test void methodGetMethods() throws Exception
143     {
144         ClassTool classTool = new ClassTool();
145         // default type is java.lang.Object
146         List result = classTool.getMethods();
147         assertNotNull(result);
148         assertFalse(result.isEmpty());
149         //TODO: test contents of list?
150     }
151 
152     //TODO: add MethodSub tests
153 
154     public @Test void methodGetTypes() throws Exception
155     {
156         ClassTool classTool = new ClassTool();
157         // default type is java.lang.Object
158         Set result = classTool.getTypes();
159         assertNotNull(result);
160         assertFalse(result.isEmpty());
161         //TODO: test contents of set?
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         // default type is java.lang.Object
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         // default type is java.lang.Object
183         assertEquals(classTool.getPackage(), "java.lang");
184     }
185 
186     public @Test void methodGetType() throws Exception
187     {
188         ClassTool classTool = new ClassTool();
189         // default type is java.lang.Object
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         // default type is java.lang.Object which has no super
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         // default type is java.lang.Object
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         // default type is java.lang.Object
243         assertFalse(classTool.isDeprecated());
244         classTool.setType(MyDeprecated.class);
245         assertTrue(classTool.isDeprecated());
246     }
247 
248     @Deprecated
249     protected static class MyDeprecated
250     {
251         // do nothing
252     }
253 
254     public @Test void methodIsFinal() throws Exception
255     {
256         ClassTool classTool = new ClassTool();
257         // default type is java.lang.Object
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         // default type is java.lang.Object
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         // default type is java.lang.Object
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         // default type is java.lang.Object
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         // default type is java.lang.Object
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         // default type is java.lang.Object
303         assertFalse(classTool.isStatic());
304         classTool.setType(PrivateStrictStatic.class);
305         assertTrue(classTool.isStatic());
306     }
307 
308 /*  CB - commented because on some JVM (ex: 1.6.0_02-b05 linux)
309     the strictfp modifier is lost at runtime on Classes
310 
311     public @Test void methodIsStrict() throws Exception
312     {
313         ClassTool classTool = new ClassTool();
314         // default type is java.lang.Object
315         assertFalse(classTool.isStrict());
316         classTool.setType(PrivateStrictStatic.class);
317         assertTrue(classTool.isStrict());
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         // default type class is java.lang.Object
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