001: /*
002: * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
003: */
004: package com.tctest;
005:
006: import com.tc.exception.TCRuntimeException;
007: import com.tc.object.TCObject;
008: import com.tc.object.bytecode.ByteCodeUtil;
009: import com.tc.simulator.app.ApplicationConfig;
010: import com.tc.simulator.listener.ListenerProvider;
011: import com.tctest.runner.AbstractTransparentApp;
012:
013: import java.lang.reflect.Constructor;
014: import java.lang.reflect.InvocationTargetException;
015: import java.lang.reflect.Method;
016: import java.lang.reflect.Modifier;
017: import java.math.BigInteger;
018: import java.util.Random;
019:
020: public class BigIntegerTestApp extends AbstractTransparentApp {
021: private final static BigInteger refInt = new BigInteger("100");
022:
023: public BigIntegerTestApp(String appId, ApplicationConfig cfg,
024: ListenerProvider listenerProvider) {
025: super (appId, cfg, listenerProvider);
026: }
027:
028: public void run() {
029: invokeAllBigIntegerConstructors();
030: invokeAllBigIntegerMethods();
031: }
032:
033: private void invokeAllBigIntegerMethods() {
034: Class bClazz = BigInteger.class;
035: Method[] methods = bClazz.getDeclaredMethods();
036: for (int i = 0; i < methods.length; i++) {
037: try {
038: System.out.println("Method name: "
039: + methods[i].getName());
040: if (!Modifier.isPrivate(methods[i].getModifiers())
041: && !methods[i].getName().startsWith(
042: ByteCodeUtil.TC_METHOD_PREFIX)
043: && !methods[i].getName().endsWith("class$")) {
044: System.out.println("Executing method: "
045: + methods[i].getName());
046: if (!Modifier.isPublic(methods[i].getModifiers())) {
047: methods[i].setAccessible(true);
048: }
049: invokeMethod(methods[i]);
050: }
051: } catch (IllegalArgumentException e) {
052: throw new TCRuntimeException(e);
053: } catch (IllegalAccessException e) {
054: throw new TCRuntimeException(e);
055: } catch (InvocationTargetException e) {
056: throw new TCRuntimeException(e);
057: }
058: }
059: }
060:
061: private void invokeMethod(Method method)
062: throws IllegalArgumentException, IllegalAccessException,
063: InvocationTargetException {
064: BigInteger bigInteger = new BigInteger("101");
065: method.invoke(bigInteger, getMethodArguments(method));
066: }
067:
068: private Object[] getMethodArguments(Method method) {
069: Object[] arguments = new Object[method.getParameterTypes().length];
070: String methodName = method.getName();
071: if ("add".equals(methodName) || "and".equals(methodName)
072: || "andNot".equals(methodName)) {
073: arguments[0] = new BigInteger("200");
074: } else if ("clearBit".equals(methodName)) {
075: arguments[0] = new Integer(1);
076: } else if ("compareTo".equals(methodName)
077: || "divide".equals(methodName)
078: || "divideAndRemainder".equals(methodName)
079: || "equals".equals(methodName)
080: || "gcd".equals(methodName) || "max".equals(methodName)
081: || "min".equals(methodName) || "mod".equals(methodName)
082: || "modInverse".equals(methodName)
083: || "multiply".equals(methodName)
084: || "or".equals(methodName)
085: || "remainder".equals(methodName)
086: || "subtract".equals(methodName)
087: || "xor".equals(methodName)) {
088: arguments[0] = refInt;
089: } else if ("flipBit".equals(methodName)
090: || "isProbablePrime".equals(methodName)
091: || "pow".equals(methodName)
092: || "setBit".equals(methodName)
093: || "shiftLeft".equals(methodName)
094: || "shiftRight".equals(methodName)
095: || "testBit".equals(methodName)
096: || "trailingZeroCnt".equals(methodName)
097: || "bitLen".equals(methodName)
098: || "primeToCertainty".equals(methodName)
099: || "bitCnt".equals(methodName)) {
100: arguments[0] = new Integer(1);
101: } else if ("modPow".equals(methodName)) {
102: arguments[0] = refInt;
103: arguments[1] = refInt;
104: } else if ("probablePrime".equals(methodName)) {
105: arguments[0] = new Integer(5);
106: arguments[1] = new Random();
107: } else if ("toString".equals(methodName)
108: && arguments.length == 1) {
109: arguments[0] = new Integer(1);
110: } else if ("valueOf".equals(methodName)) {
111: arguments[0] = new Long(1);
112: } else if ("mulAdd".equals(methodName)) {
113: arguments[0] = new int[] { 1 };
114: arguments[1] = new int[] { 1 };
115: arguments[2] = new Integer(0);
116: arguments[3] = new Integer(1);
117: arguments[4] = new Integer(1);
118: } else if ("addOne".equals(methodName)) {
119: arguments[0] = new int[] { 1 };
120: arguments[1] = new Integer(0);
121: arguments[2] = new Integer(0);
122: arguments[3] = new Integer(1);
123: } else if ("primitiveLeftShift".equals(methodName)
124: || "primitiveRightShift".equals(methodName)) {
125: arguments[0] = new int[] { 1 };
126: arguments[1] = new Integer(1);
127: arguments[2] = new Integer(1);
128: } else if ("javaIncrement".equals(methodName)) {
129: arguments[0] = new int[] { 1 };
130: } else if ("jacobiSymbol".equals(methodName)) {
131: arguments[0] = new Integer(1);
132: arguments[1] = refInt;
133: }
134:
135: return arguments;
136: }
137:
138: private void invokeAllBigIntegerConstructors() {
139: Class bClazz = BigInteger.class;
140: Constructor[] constructors = bClazz.getConstructors();
141: for (int i = 0; i < constructors.length; i++) {
142: try {
143: invokeConstructor(constructors[i]);
144: } catch (IllegalArgumentException e) {
145: throw new TCRuntimeException(e);
146: } catch (InstantiationException e) {
147: throw new TCRuntimeException(e);
148: } catch (IllegalAccessException e) {
149: throw new TCRuntimeException(e);
150: } catch (InvocationTargetException e) {
151: throw new TCRuntimeException(e);
152: }
153: }
154: }
155:
156: private void invokeConstructor(Constructor constructor)
157: throws IllegalArgumentException, InstantiationException,
158: IllegalAccessException, InvocationTargetException {
159: Class[] parameterTypes = constructor.getParameterTypes();
160: if (parameterTypes[0] != TCObject.class) {
161: constructor.newInstance(getArguments(parameterTypes));
162: }
163: }
164:
165: private Object[] getArguments(Class[] parameterTypes) {
166: Object[] arguments = new Object[parameterTypes.length];
167: switch (parameterTypes.length) {
168: case 1:
169: if (parameterTypes[0] == String.class) {
170: arguments[0] = "100";
171: } else {
172: arguments[0] = new byte[] { (byte) 100 };
173: }
174: break;
175: case 2:
176: if (parameterTypes[0] == String.class) {
177: arguments[0] = "100";
178: arguments[1] = new Integer(10);
179: } else if (parameterTypes[1] == Random.class) {
180: arguments[0] = new Integer(10);
181: arguments[1] = new Random();
182: } else {
183: arguments[0] = new Integer(refInt.signum());
184: arguments[1] = new byte[] { (byte) 100 };
185: }
186: break;
187: case 3:
188: arguments[0] = new Integer(refInt.bitLength());
189: arguments[1] = new Integer(5);
190: arguments[2] = new Random();
191: break;
192: }
193:
194: return arguments;
195: }
196: }
|