1 package org.apache.velocity.test; 2 3 18 19 import java.util.ArrayList ; 20 21 import java.lang.reflect.Method ; 22 23 import org.apache.velocity.runtime.RuntimeSingleton; 24 25 import junit.framework.TestCase; 26 27 39 public class IntrospectorTestCase extends BaseTestCase 40 { 41 private Method method; 42 private String result; 43 private String type; 44 private ArrayList failures = new ArrayList (); 45 46 IntrospectorTestCase() 47 { 48 super("IntrospectorTestCase"); 49 } 50 51 54 public IntrospectorTestCase (String name) 55 { 56 super(name); 57 } 58 59 65 public static junit.framework.Test suite () 66 { 67 return new IntrospectorTestCase(); 68 } 69 70 public void runTest() 71 { 72 MethodProvider mp = new MethodProvider(); 73 74 try 75 { 76 Object [] booleanParams = { new Boolean (true) }; 78 type = "boolean"; 79 method = RuntimeSingleton.getIntrospector().getMethod( 80 MethodProvider.class, type + "Method", booleanParams); 81 result = (String ) method.invoke(mp, booleanParams); 82 83 if (!result.equals(type)) 84 failures.add(type + "Method could not be found!"); 85 86 Object [] byteParams = { new Byte ("1") }; 88 type = "byte"; 89 method = RuntimeSingleton.getIntrospector().getMethod( 90 MethodProvider.class, type + "Method", byteParams); 91 result = (String ) method.invoke(mp, byteParams); 92 93 if (!result.equals(type)) 94 failures.add(type + "Method could not be found!"); 95 96 Object [] characterParams = { new Character ('a') }; 98 type = "character"; 99 method = RuntimeSingleton.getIntrospector().getMethod( 100 MethodProvider.class, type + "Method", characterParams); 101 result = (String ) method.invoke(mp, characterParams); 102 103 if (!result.equals(type)) 104 failures.add(type + "Method could not be found!"); 105 106 Object [] doubleParams = { new Double ((double)1) }; 108 type = "double"; 109 method = RuntimeSingleton.getIntrospector().getMethod( 110 MethodProvider.class, type + "Method", doubleParams); 111 result = (String ) method.invoke(mp, doubleParams); 112 113 if (!result.equals(type)) 114 failures.add(type + "Method could not be found!"); 115 116 Object [] floatParams = { new Float ((float)1) }; 118 type = "float"; 119 method = RuntimeSingleton.getIntrospector().getMethod( 120 MethodProvider.class, type + "Method", floatParams); 121 result = (String ) method.invoke(mp, floatParams); 122 123 if (!result.equals(type)) 124 failures.add(type + "Method could not be found!"); 125 126 Object [] integerParams = { new Integer ((int)1) }; 128 type = "integer"; 129 method = RuntimeSingleton.getIntrospector().getMethod( 130 MethodProvider.class, type + "Method", integerParams); 131 result = (String ) method.invoke(mp, integerParams); 132 133 if (!result.equals(type)) 134 failures.add(type + "Method could not be found!"); 135 136 Object [] longParams = { new Long ((long)1) }; 138 type = "long"; 139 method = RuntimeSingleton.getIntrospector().getMethod( 140 MethodProvider.class, type + "Method", longParams); 141 result = (String ) method.invoke(mp, longParams); 142 143 if (!result.equals(type)) 144 failures.add(type + "Method could not be found!"); 145 146 Object [] shortParams = { new Short ((short)1) }; 148 type = "short"; 149 method = RuntimeSingleton.getIntrospector().getMethod( 150 MethodProvider.class, type + "Method", shortParams); 151 result = (String ) method.invoke(mp, shortParams); 152 153 if (!result.equals(type)) 154 failures.add(type + "Method could not be found!"); 155 156 158 Object [] params = {}; 159 160 method = RuntimeSingleton.getIntrospector().getMethod( 161 MethodProvider.class, "untouchable", params); 162 163 if (method != null) 164 failures.add(type + "able to access a private-access method."); 165 166 168 method = RuntimeSingleton.getIntrospector().getMethod( 169 MethodProvider.class, "reallyuntouchable", params); 170 171 if (method != null) 172 failures.add(type + "able to access a default-access method."); 173 174 177 int totalFailures = failures.size(); 178 if (totalFailures > 0) 179 { 180 StringBuffer sb = new StringBuffer ("\nIntrospection Errors:\n"); 181 for (int i = 0; i < totalFailures; i++) 182 sb.append((String ) failures.get(i)).append("\n"); 183 184 fail(sb.toString()); 185 } 186 } 187 catch (Exception e) 188 { 189 fail( e.toString() ); 190 } 191 } 192 193 public static class MethodProvider 194 { 195 198 public String booleanMethod (boolean p) { return "boolean"; } 199 public String byteMethod (byte p) { return "byte"; } 200 public String characterMethod (char p) { return "character"; } 201 public String doubleMethod (double p) { return "double"; } 202 public String floatMethod (float p) { return "float"; } 203 public String integerMethod (int p) { return "integer"; } 204 public String longMethod (long p) { return "long"; } 205 public String shortMethod (short p) { return "short"; } 206 207 String untouchable() { return "yech";} 208 private String reallyuntouchable() { return "yech!"; } 209 210 } 211 } 212 | Popular Tags |