Search in sources :

Example 1 with MethodRef

use of org.robolectric.internal.bytecode.MethodRef in project robolectric by robolectric.

the class SandboxClassLoaderTest method shouldInterceptFilteredStaticMethodInvocations.

@Test
public void shouldInterceptFilteredStaticMethodInvocations() throws Exception {
    setClassLoader(new SandboxClassLoader(configureBuilder().addInterceptedMethod(new MethodRef(AClassToForget.class, "forgettableStaticMethod")).build()));
    Class<?> theClass = loadClass(AClassThatRefersToAForgettableClass.class);
    Object instance = theClass.newInstance();
    Object output = theClass.getMethod("interactWithForgettableStaticMethod").invoke(shadow.directlyOn(instance, (Class<Object>) theClass));
    assertEquals("yess? forget this: null", output);
}
Also used : MethodRef(org.robolectric.internal.bytecode.MethodRef) AnUninstrumentedClass(org.robolectric.testing.AnUninstrumentedClass) AClassThatRefersToAForgettableClass(org.robolectric.testing.AClassThatRefersToAForgettableClass) AFinalClass(org.robolectric.testing.AFinalClass) AClassThatCallsAMethodReturningAForgettableClass(org.robolectric.testing.AClassThatCallsAMethodReturningAForgettableClass) AnExampleClass(org.robolectric.testing.AnExampleClass) SandboxClassLoader(org.robolectric.internal.bytecode.SandboxClassLoader) Test(org.junit.Test)

Example 2 with MethodRef

use of org.robolectric.internal.bytecode.MethodRef in project robolectric by robolectric.

the class SandboxClassLoaderTest method shouldInterceptFilteredMethodInvocations.

@Test
public void shouldInterceptFilteredMethodInvocations() throws Exception {
    setClassLoader(new SandboxClassLoader(configureBuilder().addInterceptedMethod(new MethodRef(AClassToForget.class, "forgettableMethod")).build()));
    Class<?> theClass = loadClass(AClassThatRefersToAForgettableClass.class);
    Object instance = theClass.newInstance();
    Object output = theClass.getMethod("interactWithForgettableClass").invoke(shadow.directlyOn(instance, (Class<Object>) theClass));
    assertEquals("null, get this!", output);
}
Also used : MethodRef(org.robolectric.internal.bytecode.MethodRef) AnUninstrumentedClass(org.robolectric.testing.AnUninstrumentedClass) AClassThatRefersToAForgettableClass(org.robolectric.testing.AClassThatRefersToAForgettableClass) AFinalClass(org.robolectric.testing.AFinalClass) AClassThatCallsAMethodReturningAForgettableClass(org.robolectric.testing.AClassThatCallsAMethodReturningAForgettableClass) AnExampleClass(org.robolectric.testing.AnExampleClass) SandboxClassLoader(org.robolectric.internal.bytecode.SandboxClassLoader) Test(org.junit.Test)

Example 3 with MethodRef

use of org.robolectric.internal.bytecode.MethodRef in project robolectric by robolectric.

the class SandboxClassLoaderTest method shouldPassArgumentsFromInterceptedMethods.

@Test
public void shouldPassArgumentsFromInterceptedMethods() throws Exception {
    if (InvokeDynamic.ENABLED)
        return;
    classHandler.valueToReturnFromIntercept = 10L;
    setClassLoader(new SandboxClassLoader(configureBuilder().addInterceptedMethod(new MethodRef(AClassToForget.class, "*")).build()));
    Class<?> theClass = loadClass(AClassThatRefersToAForgettableClassInMethodCallsReturningPrimitive.class);
    Object instance = theClass.newInstance();
    shadow.directlyOn(instance, (Class<Object>) theClass, "longMethod");
    assertThat(transcript).containsExactly("methodInvoked: AClassThatRefersToAForgettableClassInMethodCallsReturningPrimitive.__constructor__()", "intercept: org/robolectric/testing/AClassToForget/longReturningMethod(Ljava/lang/String;IJ)J with params (str str, 123 123, 456 456)");
}
Also used : MethodRef(org.robolectric.internal.bytecode.MethodRef) SandboxClassLoader(org.robolectric.internal.bytecode.SandboxClassLoader) Test(org.junit.Test)

Example 4 with MethodRef

use of org.robolectric.internal.bytecode.MethodRef in project robolectric by robolectric.

the class AndroidConfigurer method configure.

public void configure(InstrumentationConfiguration.Builder builder, Interceptors interceptors) {
    for (MethodRef methodRef : interceptors.getAllMethodRefs()) {
        builder.addInterceptedMethod(methodRef);
    }
    builder.doNotAcquireClass("org.robolectric.TestLifecycle").doNotAcquireClass("org.robolectric.manifest.AndroidManifest").doNotAcquireClass("org.robolectric.RobolectricTestRunner").doNotAcquireClass("org.robolectric.RobolectricTestRunner.HelperTestRunner").doNotAcquireClass("org.robolectric.shadow.api.ShadowPicker").doNotAcquireClass("org.robolectric.res.ResourcePath").doNotAcquireClass("org.robolectric.res.ResourceTable").doNotAcquireClass("org.robolectric.ApkLoader").doNotAcquireClass("org.robolectric.res.builder.XmlBlock");
    builder.doNotAcquirePackage("javax.").doNotAcquirePackage("jdk.internal.").doNotAcquirePackage("org.junit").doNotAcquirePackage("org.hamcrest").doNotAcquirePackage("org.robolectric.annotation.").doNotAcquirePackage("org.robolectric.internal.").doNotAcquirePackage("org.robolectric.pluginapi.").doNotAcquirePackage("org.robolectric.manifest.").doNotAcquirePackage("org.robolectric.res.").doNotAcquirePackage("org.robolectric.util.").doNotAcquirePackage("org.robolectric.RobolectricTestRunner$").doNotAcquirePackage("sun.").doNotAcquirePackage("com.sun.").doNotAcquirePackage("org.w3c.").doNotAcquirePackage("org.xml.").doNotAcquirePackage(// allows for android projects with mixed scala\java tests to be
    "org.specs2").doNotAcquirePackage(// run with Maven Surefire (see the RoboSpecs project on github)
    "scala.").doNotAcquirePackage("kotlin.").doNotAcquirePackage("io.mockk.proxy.").doNotAcquirePackage("org.bouncycastle.").doNotAcquirePackage("org.conscrypt.").doNotAcquirePackage("com.almworks.sqlite4java").doNotAcquirePackage("org.jacoco.");
    builder.addClassNameTranslation("java.net.ExtendedResponseCache", "org.robolectric.fakes.RoboExtendedResponseCache").addClassNameTranslation("java.net.ResponseSource", "org.robolectric.fakes.RoboResponseSource").addClassNameTranslation("java.nio.charset.Charsets", StandardCharsets.class.getName()).addClassNameTranslation("java.lang.UnsafeByteSequence", Object.class.getName()).addClassNameTranslation("java.util.jar.StrictJarFile", Object.class.getName());
    if (Util.getJavaVersion() >= 9) {
        builder.addClassNameTranslation("sun.misc.Cleaner", "java.lang.ref.Cleaner$Cleanable");
    }
    // Instrumenting these classes causes a weird failure.
    builder.doNotInstrumentClass("android.R").doNotInstrumentClass("android.R$styleable");
    builder.addInstrumentedPackage("dalvik.").addInstrumentedPackage("libcore.").addInstrumentedPackage("android.").addInstrumentedPackage("com.android.internal.").addInstrumentedPackage("org.apache.http.").addInstrumentedPackage("org.ccil.cowan.tagsoup").addInstrumentedPackage("org.kxml2.");
    builder.doNotInstrumentPackage("android.arch");
    builder.doNotInstrumentPackage("android.support.test");
    // Mockito's MockMethodDispatcher must only exist in the Bootstrap class loader.
    builder.doNotAcquireClass("org.mockito.internal.creation.bytebuddy.inject.MockMethodDispatcher");
    for (String packagePrefix : shadowProviders.getInstrumentedPackages()) {
        builder.addInstrumentedPackage(packagePrefix);
    }
}
Also used : MethodRef(org.robolectric.internal.bytecode.MethodRef) StandardCharsets(java.nio.charset.StandardCharsets)

Example 5 with MethodRef

use of org.robolectric.internal.bytecode.MethodRef in project robolectric by robolectric.

the class SandboxClassLoaderTest method shouldRemapClassesWhileInterceptingMethods.

@Test
public void shouldRemapClassesWhileInterceptingMethods() throws Exception {
    InstrumentationConfiguration config = configureBuilder().addClassNameTranslation(AClassToForget.class.getName(), AClassToRemember.class.getName()).addInterceptedMethod(new MethodRef(AClassThatCallsAMethodReturningAForgettableClass.class, "getAForgettableClass")).build();
    setClassLoader(new SandboxClassLoader(config));
    Class<?> theClass = loadClass(AClassThatCallsAMethodReturningAForgettableClass.class);
    theClass.getMethod("callSomeMethod").invoke(shadow.directlyOn(theClass.newInstance(), (Class<Object>) theClass));
}
Also used : MethodRef(org.robolectric.internal.bytecode.MethodRef) AClassToForget(org.robolectric.testing.AClassToForget) InstrumentationConfiguration(org.robolectric.internal.bytecode.InstrumentationConfiguration) AnUninstrumentedClass(org.robolectric.testing.AnUninstrumentedClass) AClassThatRefersToAForgettableClass(org.robolectric.testing.AClassThatRefersToAForgettableClass) AFinalClass(org.robolectric.testing.AFinalClass) AClassThatCallsAMethodReturningAForgettableClass(org.robolectric.testing.AClassThatCallsAMethodReturningAForgettableClass) AnExampleClass(org.robolectric.testing.AnExampleClass) AClassToRemember(org.robolectric.testing.AClassToRemember) SandboxClassLoader(org.robolectric.internal.bytecode.SandboxClassLoader) Test(org.junit.Test)

Aggregations

MethodRef (org.robolectric.internal.bytecode.MethodRef)7 SandboxClassLoader (org.robolectric.internal.bytecode.SandboxClassLoader)5 Test (org.junit.Test)4 AClassThatCallsAMethodReturningAForgettableClass (org.robolectric.testing.AClassThatCallsAMethodReturningAForgettableClass)4 AClassThatRefersToAForgettableClass (org.robolectric.testing.AClassThatRefersToAForgettableClass)4 AFinalClass (org.robolectric.testing.AFinalClass)4 AnExampleClass (org.robolectric.testing.AnExampleClass)4 AnUninstrumentedClass (org.robolectric.testing.AnUninstrumentedClass)4 Method (java.lang.reflect.Method)1 StandardCharsets (java.nio.charset.StandardCharsets)1 RoboCharsets (org.robolectric.android.fakes.RoboCharsets)1 RoboExtendedResponseCache (org.robolectric.android.fakes.RoboExtendedResponseCache)1 InstrumentationConfiguration (org.robolectric.internal.bytecode.InstrumentationConfiguration)1 AndroidManifest (org.robolectric.manifest.AndroidManifest)1 ResourceTable (org.robolectric.res.ResourceTable)1 AClassToForget (org.robolectric.testing.AClassToForget)1 AClassToRemember (org.robolectric.testing.AClassToRemember)1 AClassWithNativeMethod (org.robolectric.testing.AClassWithNativeMethod)1 AClassWithStaticMethod (org.robolectric.testing.AClassWithStaticMethod)1