1 4 package com.tctest; 5 6 import com.tc.exception.TCRuntimeException; 7 import com.tc.object.TCObject; 8 import com.tc.object.bytecode.ByteCodeUtil; 9 import com.tc.object.config.ConfigVisitor; 10 import com.tc.object.config.DSOClientConfigHelper; 11 import com.tc.simulator.app.ApplicationConfig; 12 import com.tc.simulator.listener.ListenerProvider; 13 import com.tctest.runner.AbstractTransparentApp; 14 15 import java.lang.reflect.Constructor ; 16 import java.lang.reflect.Field ; 17 import java.lang.reflect.InvocationTargetException ; 18 import java.lang.reflect.Method ; 19 import java.lang.reflect.Modifier ; 20 import java.math.BigDecimal ; 21 import java.math.BigInteger ; 22 import java.util.Random ; 23 24 29 public class BigIntegerDecimalTestApp extends AbstractTransparentApp { 30 private final static BigInteger refInt = new BigInteger ("100"); 31 private final static BigDecimal refDecimal = new BigDecimal ("100.0"); 32 33 public BigIntegerDecimalTestApp(String appId, ApplicationConfig cfg, ListenerProvider listenerProvider) { 34 super(appId, cfg, listenerProvider); 35 } 36 37 public void run() { 38 runBigIntegerTests(); 39 runBigDecimalTests(); 40 } 41 42 private void runBigIntegerTests() { 43 System.out.println("==========BigInteger tests Begin.=========="); 44 invokeAllBigIntegerConstructors(); 45 invokeAllBigIntegerMethods(); 46 System.out.println("==========BigInteger tests End.=========="); 47 } 48 49 private void runBigDecimalTests() { 50 System.out.println("==========BigDecimal tests Begin.=========="); 51 invokeAllBigDecimalConstructors(); 52 invokeAllBigDecimalMethods(); 53 System.out.println("==========BigDecimal tests End.=========="); 54 } 55 56 private void invokeAllBigDecimalMethods() { 57 invokeAllMethods(BigDecimal .class, false); 58 invokeAllMethods(BigDecimal .class, true); 59 } 60 61 private void invokeAllBigIntegerMethods() { 62 invokeAllMethods(BigInteger .class, false); 63 invokeAllMethods(BigInteger .class, true); 64 } 65 66 private void invokeAllMethods(Class bClazz, boolean lock) { 67 Object [] methodArguments = null; 68 Object object = null; 69 70 boolean bigIntegerClass = false; 71 if (bClazz.equals(BigInteger .class)) { 72 bigIntegerClass = true; 73 } 74 75 Method [] methods = bClazz.getDeclaredMethods(); 76 for (int i = 0; i < methods.length; i++) { 77 try { 78 if (!Modifier.isPrivate(methods[i].getModifiers()) 79 && !methods[i].getName().startsWith(ByteCodeUtil.TC_METHOD_PREFIX) 80 && !methods[i].getName().endsWith("class$")) { 81 System.out.println("Executing method: " + methods[i].getName()); 82 if (!Modifier.isPublic(methods[i].getModifiers())) { 83 methods[i].setAccessible(true); 84 } 85 86 methodArguments = bigIntegerClass ? getBigIntegerMethodArguments(methods[i]) 87 : getBigDecimalMethodArguments(methods[i]); 88 89 if (bigIntegerClass) { 90 object = new BigInteger ("101"); 91 } else { 92 object = new BigDecimal ("101.0"); 93 } 94 95 invokeMethod(object, methods[i], methodArguments, lock); 96 } 97 } catch (IllegalArgumentException e) { 98 throw new TCRuntimeException(e); 99 } catch (IllegalAccessException e) { 100 throw new TCRuntimeException(e); 101 } catch (InvocationTargetException e) { 102 throw new TCRuntimeException(e); 103 } 104 } 105 } 106 107 private void invokeMethod(Object object, Method method, Object [] methodArguments, boolean lock) throws IllegalArgumentException , 108 IllegalAccessException , InvocationTargetException { 109 if (lock) { 110 synchronized(object) { 111 method.invoke(object, methodArguments); 112 } 113 } else { 114 method.invoke(object, methodArguments); 115 } 116 } 117 118 private Object [] getBigDecimalMethodArguments(Method method) { 120 Class [] parameterTypes = method.getParameterTypes(); 121 Object [] arguments = new Object [parameterTypes.length]; 122 String methodName = method.getName(); 123 124 Class mathContextClazz = getMathContextClass(); 125 Object decimal32MathContext = getDecimal32MathContext(mathContextClazz); 126 127 Class roundingModeClazz = getRoundingModeClass(); 128 Object upRoundingMode = getUpRoundingMode(roundingModeClazz); 129 130 if (("abs".equals(methodName) || "negate".equals(methodName) || "plus".equals(methodName)) && arguments.length == 1) { 131 if (parameterTypes[0].equals(mathContextClazz)) { 132 arguments[0] = decimal32MathContext; 133 } 134 } else if ("add".equals(methodName)) { 135 arguments[0] = refDecimal; 136 if (arguments.length == 2 && parameterTypes[1].equals(mathContextClazz)) { 137 arguments[1] = decimal32MathContext; 138 } 139 } else if ("compareTo".equals(methodName) || "equals".equals(methodName) || "max".equals(methodName) 140 || "min".equals(methodName)) { 141 arguments[0] = refDecimal; 142 } else if ("divide".equals(methodName) || "multiply".equals(methodName) || "subtract".equals(methodName)) { 143 arguments[0] = refDecimal; 144 if (arguments.length == 2) { 145 if (parameterTypes[1].equals(mathContextClazz)) { 146 arguments[1] = decimal32MathContext; 147 } else if (parameterTypes[1].equals(roundingModeClazz)) { 148 arguments[1] = upRoundingMode; 149 } else { 150 arguments[1] = new Integer (BigDecimal.ROUND_UP); 151 } 152 } else if (arguments.length == 3) { 153 arguments[1] = new Integer (1); 154 if (parameterTypes[2].equals(roundingModeClazz)) { 155 arguments[2] = upRoundingMode; 156 } else { 157 arguments[2] = new Integer (BigDecimal.ROUND_UP); 158 } 159 } 160 } else if ("divideAndRemainder".equals(methodName) || "divideToIntegralValue".equals(methodName) || 161 "remainder".equals(methodName)) { 162 arguments[0] = refDecimal; 163 if (arguments.length == 2 && parameterTypes[1].equals(mathContextClazz)) { 164 arguments[1] = decimal32MathContext; 165 } 166 } else if ("movePointLeft".equals(methodName) || "movePointRight".equals(methodName) || 167 "scaleByPowerOfTen".equals(methodName)) { 168 arguments[0] = new Integer (1); 169 } else if ("pow".equals(methodName)) { 170 arguments[0] = new Integer (1); 171 if (arguments.length == 2 && parameterTypes[1].equals(mathContextClazz)) { 172 arguments[1] = decimal32MathContext; 173 } 174 } else if ("round".equals(methodName)) { 175 arguments[0] = decimal32MathContext; 176 } else if ("setScale".equals(methodName)) { 177 arguments[0] = new Integer (1); 178 if (arguments.length == 2) { 179 if (parameterTypes[1].equals(roundingModeClazz)) { 180 arguments[1] = upRoundingMode; 181 } else { 182 arguments[1] = new Integer (BigDecimal.ROUND_UP); 183 } 184 } 185 } else if ("valueOf".equals(methodName)) { 186 if (parameterTypes[0].equals(Double.TYPE)) { 187 arguments[0] = new Double (100.0); 188 } else if (parameterTypes[0].equals(Long.TYPE)) { 189 arguments[0] = new Long (100); 190 if (arguments.length == 2) { 191 arguments[1] = new Integer (1); 192 } 193 } 194 } 195 196 return arguments; 197 } 198 199 private Object [] getBigIntegerMethodArguments(Method method) { 201 Object [] arguments = new Object [method.getParameterTypes().length]; 202 String methodName = method.getName(); 203 if ("add".equals(methodName) || "and".equals(methodName) || "andNot".equals(methodName)) { 204 arguments[0] = new BigInteger ("200"); 205 } else if ("clearBit".equals(methodName)) { 206 arguments[0] = new Integer (1); 207 } else if ("compareTo".equals(methodName) || "divide".equals(methodName) || "divideAndRemainder".equals(methodName) 208 || "equals".equals(methodName) || "gcd".equals(methodName) || "max".equals(methodName) 209 || "min".equals(methodName) || "mod".equals(methodName) || "modInverse".equals(methodName) 210 || "multiply".equals(methodName) || "or".equals(methodName) || "remainder".equals(methodName) 211 || "subtract".equals(methodName) || "xor".equals(methodName)) { 212 arguments[0] = refInt; 213 } else if ("flipBit".equals(methodName) || "isProbablePrime".equals(methodName) || "pow".equals(methodName) 214 || "setBit".equals(methodName) || "shiftLeft".equals(methodName) || "shiftRight".equals(methodName) 215 || "testBit".equals(methodName) || "trailingZeroCnt".equals(methodName) || "bitLen".equals(methodName) 216 || "primeToCertainty".equals(methodName) || "bitCnt".equals(methodName)) { 217 arguments[0] = new Integer (1); 218 } else if ("modPow".equals(methodName)) { 219 arguments[0] = refInt; 220 arguments[1] = refInt; 221 } else if ("probablePrime".equals(methodName)) { 222 arguments[0] = new Integer (5); 223 arguments[1] = new Random (); 224 } else if ("toString".equals(methodName) && arguments.length == 1) { 225 arguments[0] = new Integer (1); 226 } else if ("valueOf".equals(methodName)) { 227 arguments[0] = new Long (1); 228 } else if ("mulAdd".equals(methodName)) { 229 arguments[0] = new int[] { 1 }; 230 arguments[1] = new int[] { 1 }; 231 arguments[2] = new Integer (0); 232 arguments[3] = new Integer (1); 233 arguments[4] = new Integer (1); 234 } else if ("addOne".equals(methodName)) { 235 arguments[0] = new int[] { 1 }; 236 arguments[1] = new Integer (0); 237 arguments[2] = new Integer (0); 238 arguments[3] = new Integer (1); 239 } else if ("primitiveLeftShift".equals(methodName) || "primitiveRightShift".equals(methodName)) { 240 arguments[0] = new int[] { 1 }; 241 arguments[1] = new Integer (1); 242 arguments[2] = new Integer (1); 243 } else if ("javaIncrement".equals(methodName)) { 244 arguments[0] = new int[] { 1 }; 245 } else if ("jacobiSymbol".equals(methodName)) { 246 arguments[0] = new Integer (1); 247 arguments[1] = refInt; 248 } 249 250 return arguments; 251 } 252 253 private void invokeAllBigDecimalConstructors() { 254 invokeAllConstructors(BigDecimal .class); 255 } 256 257 private void invokeAllBigIntegerConstructors() { 258 invokeAllConstructors(BigInteger .class); 259 } 260 261 private void invokeAllConstructors(Class bClazz) { 262 boolean bigIntegerClass = false; 263 if (bClazz.equals(BigInteger .class)) { 264 bigIntegerClass = true; 265 } 266 Object [] arguments = null; 267 Constructor [] constructors = bClazz.getConstructors(); 268 for (int i = 0; i < constructors.length; i++) { 269 try { 270 arguments = (bigIntegerClass) ? getBigIntegerConstructorArguments(constructors[i].getParameterTypes()) 271 : getBigDecimalConstructorArguments(constructors[i].getParameterTypes()); 272 invokeConstructor(constructors[i], arguments); 273 } catch (IllegalArgumentException e) { 274 throw new TCRuntimeException(e); 275 } catch (InstantiationException e) { 276 throw new TCRuntimeException(e); 277 } catch (IllegalAccessException e) { 278 throw new TCRuntimeException(e); 279 } catch (InvocationTargetException e) { 280 throw new TCRuntimeException(e); 281 } 282 } 283 } 284 285 private void invokeConstructor(Constructor constructor, Object [] arguments) throws IllegalArgumentException , 286 InstantiationException , IllegalAccessException , InvocationTargetException { 287 Class [] parameterTypes = constructor.getParameterTypes(); 288 if (parameterTypes[0] != TCObject.class) { 289 constructor.newInstance(arguments); 290 } 291 } 292 293 private Object [] getBigDecimalConstructorArguments(Class [] parameterTypes) { 294 Class mathContextClazz = getMathContextClass(); 295 Object decimal32MathContext = getDecimal32MathContext(mathContextClazz); 296 297 Object [] arguments = new Object [parameterTypes.length]; 298 if (parameterTypes[0].equals(BigInteger .class)) { 299 arguments[0] = new BigInteger ("100"); 300 if (parameterTypes.length == 2) { 301 if (parameterTypes[1].equals(mathContextClazz)) { 302 arguments[1] = decimal32MathContext; 303 } else { 304 arguments[1] = new Integer (1); 305 } 306 } else if (parameterTypes.length == 3) { 307 arguments[1] = new Integer (1); 308 arguments[2] = decimal32MathContext; 309 } 310 } else if (parameterTypes[0].equals(char[].class)) { 311 arguments[0] = new char[] { '1' }; 312 if (parameterTypes.length == 2) { 313 arguments[1] = decimal32MathContext; 314 } else if (parameterTypes.length == 3) { 315 arguments[1] = new Integer (0); 316 arguments[2] = new Integer (1); 317 } else if (parameterTypes.length == 4) { 318 arguments[1] = new Integer (0); 319 arguments[2] = new Integer (1); 320 arguments[3] = decimal32MathContext; 321 } 322 } else if (parameterTypes[0].equals(Double.TYPE)) { 323 arguments[0] = new Double (100.0); 324 if (parameterTypes.length == 2) { 325 arguments[1] = decimal32MathContext; 326 } 327 } else if (parameterTypes[0].equals(Integer.TYPE)) { 328 arguments[0] = new Integer (100); 329 if (parameterTypes.length == 2) { 330 arguments[1] = decimal32MathContext; 331 } 332 } else if (parameterTypes[0].equals(Long.TYPE)) { 333 arguments[0] = new Long (100); 334 if (parameterTypes.length == 2) { 335 arguments[1] = decimal32MathContext; 336 } 337 } else if (parameterTypes[0].equals(String .class)) { 338 arguments[0] = "100.0"; 339 if (parameterTypes.length == 2) { 340 arguments[1] = decimal32MathContext; 341 } 342 } 343 344 return arguments; 345 } 346 347 private Object [] getBigIntegerConstructorArguments(Class [] parameterTypes) { 348 Object [] arguments = new Object [parameterTypes.length]; 349 switch (parameterTypes.length) { 350 case 1: 351 if (parameterTypes[0] == String .class) { 352 arguments[0] = "100"; 353 } else { 354 arguments[0] = new byte[] { (byte) 100 }; 355 } 356 break; 357 case 2: 358 if (parameterTypes[0] == String .class) { 359 arguments[0] = "100"; 360 arguments[1] = new Integer (10); 361 } else if (parameterTypes[1] == Random .class) { 362 arguments[0] = new Integer (10); 363 arguments[1] = new Random (); 364 } else { 365 arguments[0] = new Integer (refInt.signum()); 366 arguments[1] = new byte[] { (byte) 100 }; 367 } 368 break; 369 case 3: 370 arguments[0] = new Integer (refInt.bitLength()); 371 arguments[1] = new Integer (5); 372 arguments[2] = new Random (); 373 break; 374 } 375 376 return arguments; 377 } 378 379 private Class getRoundingModeClass() { 380 return getClazz("java.math.RoundingMode"); 381 } 382 383 private Class getMathContextClass() { 384 return getClazz("java.math.MathContext"); 385 } 386 387 391 private Class getClazz(String className) { 392 Class mathContextClazz = null; 393 try { 394 mathContextClazz = Class.forName(className); 395 } catch (ClassNotFoundException e) { 396 } catch (SecurityException e) { 398 } 400 return mathContextClazz; 401 } 402 403 private Object getUpRoundingMode(Class roundingModeClazz) { 404 return getField(roundingModeClazz, "UP"); 405 } 406 407 private Object getDecimal32MathContext(Class mathContextClazz) { 408 return getField(mathContextClazz, "DECIMAL32"); 409 } 410 411 415 private Object getField(Class mathContextClazz, String fieldName) { 416 if (mathContextClazz == null) return null; 417 Object decimal32MathContext = null; 418 try { 419 Field decimal32Field = mathContextClazz.getDeclaredField(fieldName); 420 decimal32MathContext = decimal32Field.get(null); 421 } catch (SecurityException e) { 422 } catch (NoSuchFieldException e) { 424 } catch (IllegalArgumentException e) { 426 } catch (IllegalAccessException e) { 428 } 430 return decimal32MathContext; 431 } 432 433 public static void visitL1DSOConfig(ConfigVisitor visitor, DSOClientConfigHelper config) { 434 String testClass = BigIntegerDecimalTestApp.class.getName(); 435 config.getOrCreateSpec(testClass); 436 String readOnlyMethodExpression = "* " + testClass + "*.*ReadOnly*(..)"; 437 config.addReadAutolock(readOnlyMethodExpression); 438 String writeAllowedMethodExpression = "* " + testClass + "*.*(..)"; 439 config.addWriteAutolock(writeAllowedMethodExpression); 440 } 441 } 442 | Popular Tags |