private static void setupRuntimeProperties(){ RuntimeSettings.useVFS = Properties.VIRTUAL_FS; RuntimeSettings.mockJVMNonDeterminism = Properties.REPLACE_CALLS; RuntimeSettings.mockSystemIn = Properties.REPLACE_SYSTEM_IN; RuntimeSettings.mockGUI = Properties.REPLACE_GUI; RuntimeSettings.sandboxMode = Properties.SANDBOX_MODE; RuntimeSettings.maxNumberOfThreads = Properties.MAX_STARTED_THREADS; RuntimeSettings.maxNumberOfIterationsPerLoop = Properties.MAX_LOOP_ITERATIONS; RuntimeSettings.useVNET = Properties.VIRTUAL_NET; RuntimeSettings.useSeparateClassLoader = Properties.USE_SEPARATE_CLASSLOADER; RuntimeSettings.className = Properties.TARGET_CLASS; RuntimeSettings.useJEE = Properties.JEE; RuntimeSettings.applyUIDTransformation = true; RuntimeSettings.isRunningASystemTest = Properties.IS_RUNNING_A_SYSTEM_TEST; MethodCallReplacementCache.resetSingleton(); }
public static MethodCallReplacementCache getInstance() { if (instance == null) { instance = new MethodCallReplacementCache(); } return instance; }
private MethodCallReplacementCache() { if (RuntimeSettings.mockJVMNonDeterminism) { // java.lang.* addJavaLangCalls(); // javax.swing.JComponent.getPreferredSize() addReplacementCall(new MethodCallReplacement("javax/swing/JComponent", "getPreferredSize", "()Ljava/awt/Dimension;", Opcodes.INVOKEVIRTUAL, PackageInfo.getNameWithSlash(org.evosuite.runtime.mock.javax.swing.MockJComponent.class), "getPreferredSize", "()Ljava/awt/Dimension;", true, false)); addExtraceExceptionReplacements(); } handleMockList(); }
if(MethodCallReplacementCache.getInstance().hasReplacementCall(owner, name+desc) && (opcode != Opcodes.INVOKESPECIAL || name.equals("<init>"))) { MethodCallReplacement replacement = MethodCallReplacementCache.getInstance().getReplacementCall(owner, name+desc); isReplaced = true; replacement.insertMethodCall(this, Opcodes.INVOKESTATIC); if(MethodCallReplacementCache.getInstance().hasSpecialReplacementCall(owner, name+desc)) { MethodCallReplacement replacement = MethodCallReplacementCache.getInstance().getSpecialReplacementCall(owner, name+desc); if (replacement.isTarget(owner, name, desc) && opcode == Opcodes.INVOKESPECIAL && name.equals("<init>")) {
private void replaceAllStaticMethods(Class<?> mockClass, Class<?> target) throws IllegalArgumentException { for (Method m : target.getMethods()) { if (!Modifier.isStatic(m.getModifiers())) { continue; } String desc = Type.getMethodDescriptor(m); addReplacementCall(new MethodCallReplacement(target.getCanonicalName().replace('.', '/'), m.getName(), desc, Opcodes.INVOKESTATIC, mockClass.getCanonicalName().replace('.', '/'), m.getName(), desc, false, false)); } }
private void handleStaticReplacementMethods(Class<? extends EvoSuiteMock> mockClass) { for (Method m : mockClass.getMethods()) { StaticReplacementMethod srm = m.getAnnotation(StaticReplacementMethod.class); if (srm == null) { continue; } if (!Modifier.isStatic(m.getModifiers())) { throw new RuntimeException("EvoSuite Bug: improper annotations in class " + mockClass.getName()); } String target; if (OverrideMock.class.isAssignableFrom(mockClass)) { target = mockClass.getSuperclass().getCanonicalName(); } else { throw new RuntimeException("EvoSuite Bug: StaticReplacementMethod can only be used in OverrideMock"); } String desc = Type.getMethodDescriptor(m); addSpecialReplacementCall( new MethodCallReplacement(target.replace('.', '/'), m.getName(), desc, Opcodes.INVOKESPECIAL, mockClass.getCanonicalName().replace('.', '/'), m.getName(), desc, false, false)); } }
/** * Replace all the constructors of {@code target} with a static call (with * same input parameters) of static mock class {@code mockClass}. * * @param mockClass * @param target * @throws IllegalArgumentException */ private void replaceAllConstructorsWithStaticCalls(Class<?> mockClass, Class<?> target) throws IllegalArgumentException { for (Constructor<?> constructor : target.getConstructors()) { String desc = Type.getConstructorDescriptor(constructor); String replacementDesc = desc.substring(0, desc.length() - 1) + Type.getDescriptor(target); addReplacementCall(new MethodCallReplacement(target.getCanonicalName().replace('.', '/'), "<init>", desc, Opcodes.INVOKESPECIAL, mockClass.getCanonicalName().replace('.', '/'), target.getSimpleName(), replacementDesc, true, true)); } }
/** * Replace all the methods of {@code target} with a method (with same input * parameters) of mock subclass {@code mockClass}. * * @param mockClass * @param target * @throws IllegalArgumentException */ private void replaceAllInvokeSpecial(Class<?> mockClass, Class<?> target) throws IllegalArgumentException { if (!target.isAssignableFrom(mockClass)) { throw new IllegalArgumentException("Method replacement can be done only for subclasses. Class " + mockClass + " is not an instance of " + target); } // logger.debug("Static Mock: " + mockClass.getCanonicalName() + " for " + target.getCanonicalName()); for (Method method : mockClass.getMethods()) { String desc = Type.getMethodDescriptor(method); addSpecialReplacementCall(new MethodCallReplacement(target.getCanonicalName().replace('.', '/'), method.getName(), desc, Opcodes.INVOKESPECIAL, mockClass.getCanonicalName().replace('.', '/'), method.getName(), desc, false, false)); } }
@Before public void storeValues() { RuntimeSettings.mockJVMNonDeterminism = true; RuntimeSettings.useVFS = true; RuntimeSettings.useVNET = true; MethodCallReplacementCache.resetSingleton(); Runtime.getInstance().resetRuntime(); }
String jvmMock = MockThrowable.class.getName().replace('.', '/'); addReplacementCall(new MethodCallReplacement(jvmOriginal, "getStackTrace", "()[Ljava/lang/StackTraceElement;", Opcodes.INVOKEVIRTUAL, jvmMock, "replacement_getStackTrace", "(Ljava/lang/Throwable;)[Ljava/lang/StackTraceElement;", false, false)); addReplacementCall(new MethodCallReplacement(jvmOriginal, "printStackTrace", "(Ljava/io/PrintStream;)V", Opcodes.INVOKEVIRTUAL, jvmMock, "replacement_printStackTrace", "(Ljava/lang/Throwable;Ljava/io/PrintStream;)V", false, false)); addReplacementCall(new MethodCallReplacement(jvmOriginal, "printStackTrace", "(Ljava/io/PrintWriter;)V", Opcodes.INVOKEVIRTUAL, jvmMock, "replacement_printStackTrace", "(Ljava/lang/Throwable;Ljava/io/PrintWriter;)V", false, false));
/** * Replace all the constructors of {@code target} with a constructor (with * same input parameters) of mock subclass {@code mockClass}. * * @param mockClass * @param target * @throws IllegalArgumentException */ private void replaceAllConstructors(Class<?> mockClass, Class<?> target) throws IllegalArgumentException { if (!target.isAssignableFrom(mockClass)) { throw new IllegalArgumentException("Constructor replacement can be done only for subclasses. Class " + mockClass + " is not an instance of " + target); } for (Constructor<?> constructor : ReflectionUtils.getDeclaredConstructors(mockClass)) { String desc = Type.getConstructorDescriptor(constructor); addSpecialReplacementCall(new MethodCallReplacement(target.getCanonicalName().replace('.', '/'), "<init>", desc, Opcodes.INVOKESPECIAL, mockClass.getCanonicalName().replace('.', '/'), "<init>", desc, false, false)); } }
@Before public void storeValues() { RuntimeSettings.mockJVMNonDeterminism = true; MethodCallReplacementCache.resetSingleton(); Runtime.getInstance().resetRuntime(); }
mockedArgumentTypes[i + 1] = argumentTypes[i]; String mockedDesc = Type.getMethodDescriptor(Type.getReturnType(m), mockedArgumentTypes); addReplacementCall(new MethodCallReplacement(target.getCanonicalName().replace('.', '/'), m.getName(), desc, Opcodes.INVOKEVIRTUAL, mockClass.getCanonicalName().replace('.', '/'), m.getName(), mockedDesc, false, false));
@Before public void storeValues() { KillSwitchHandler.getInstance().setKillSwitch(false); RuntimeSettings.mockJVMNonDeterminism = true; MethodCallReplacementCache.resetSingleton(); Runtime.getInstance().resetRuntime(); }
addReplacementCall(new MethodCallReplacement("java/lang/System", "exit", "(I)V", Opcodes.INVOKESTATIC, PackageInfo.getNameWithSlash(org.evosuite.runtime.System.class), "exit", "(I)V", false, false)); addReplacementCall(new MethodCallReplacement("java/lang/System", "setSecurityManager", "(Ljava/lang/SecurityManager;)V", Opcodes.INVOKESTATIC, PackageInfo.getNameWithSlash(org.evosuite.runtime.System.class), "setSecurityManager", "(Ljava/lang/SecurityManager;)V", false, false)); addReplacementCall(new MethodCallReplacement("java/lang/System", "currentTimeMillis", "()J", Opcodes.INVOKESTATIC, PackageInfo.getNameWithSlash(org.evosuite.runtime.System.class), "currentTimeMillis", "()J", false, false)); addReplacementCall(new MethodCallReplacement("java/lang/System", "nanoTime", "()J", Opcodes.INVOKESTATIC, PackageInfo.getNameWithSlash(org.evosuite.runtime.System.class), "nanoTime", "()J", false, false)); addReplacementCall(new MethodCallReplacement("java/lang/System", "identityHashCode", "(Ljava/lang/Object;)I", Opcodes.INVOKESTATIC, "org/evosuite/runtime/System", "identityHashCode", "(Ljava/lang/Object;)I", false, false)); addReplacementCall(new MethodCallReplacement("java/lang/Object", "hashCode", "()I", Opcodes.INVOKEVIRTUAL, PackageInfo.getNameWithSlash(org.evosuite.runtime.System.class), "identityHashCode", "(Ljava/lang/Object;)I", false, false)); addReplacementCall(new MethodCallReplacement("java/lang/Object", "toString", "()Ljava/lang/String;", Opcodes.INVOKEVIRTUAL, PackageInfo.getNameWithSlash(org.evosuite.runtime.System.class), "toString", "(Ljava/lang/Object;)Ljava/lang/String;", false, false)); addReplacementCall(new MethodCallReplacement("java/lang/Math", "random", "()D", Opcodes.INVOKESTATIC, PackageInfo.getNameWithSlash(org.evosuite.runtime.Random.class), "nextDouble", "()D", false, false)); addReplacementCall(new MethodCallReplacement("java/lang/Class", "getClasses", "()[Ljava/lang/Class;", Opcodes.INVOKEVIRTUAL, PackageInfo.getNameWithSlash(org.evosuite.runtime.Reflection.class), "getClasses", "(Ljava/lang/Class;)[Ljava/lang/Class;", false, false)); addReplacementCall( new MethodCallReplacement("java/lang/Class", "getAnnotations", "()[Ljava/lang/annotation/Annotation;",
JOptionPaneInputs.resetSingleton(); Runtime.resetSingleton(); MethodCallReplacementCache.resetSingleton();