Search in sources :

Example 1 with SandboxClassLoader

use of org.robolectric.internal.bytecode.SandboxClassLoader 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 SandboxClassLoader

use of org.robolectric.internal.bytecode.SandboxClassLoader 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 SandboxClassLoader

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

the class SandboxClassLoaderTest method shouldFixTypesInFieldAccess.

@Test
public void shouldFixTypesInFieldAccess() throws Exception {
    setClassLoader(new SandboxClassLoader(createRemappingConfig()));
    Class<?> theClass = loadClass(AClassThatRefersToAForgettableClassInItsConstructor.class);
    Object instance = theClass.newInstance();
    Method method = theClass.getDeclaredMethod(shadow.directMethodName(ShadowConstants.CONSTRUCTOR_METHOD_NAME));
    method.setAccessible(true);
    method.invoke(instance);
}
Also used : Method(java.lang.reflect.Method) AClassWithNativeMethod(org.robolectric.testing.AClassWithNativeMethod) AClassWithStaticMethod(org.robolectric.testing.AClassWithStaticMethod) SandboxClassLoader(org.robolectric.internal.bytecode.SandboxClassLoader) Test(org.junit.Test)

Example 4 with SandboxClassLoader

use of org.robolectric.internal.bytecode.SandboxClassLoader 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 5 with SandboxClassLoader

use of org.robolectric.internal.bytecode.SandboxClassLoader 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

SandboxClassLoader (org.robolectric.internal.bytecode.SandboxClassLoader)13 Test (org.junit.Test)10 AnExampleClass (org.robolectric.testing.AnExampleClass)7 AnUninstrumentedClass (org.robolectric.testing.AnUninstrumentedClass)6 MethodRef (org.robolectric.internal.bytecode.MethodRef)5 AClassThatCallsAMethodReturningAForgettableClass (org.robolectric.testing.AClassThatCallsAMethodReturningAForgettableClass)5 AClassThatRefersToAForgettableClass (org.robolectric.testing.AClassThatRefersToAForgettableClass)5 AFinalClass (org.robolectric.testing.AFinalClass)5 InstrumentationConfiguration (org.robolectric.internal.bytecode.InstrumentationConfiguration)3 Method (java.lang.reflect.Method)2 Matchers.anyString (org.mockito.Matchers.anyString)2 AClassToRemember (org.robolectric.testing.AClassToRemember)2 AClassWithEqualsHashCodeToString (org.robolectric.testing.AClassWithEqualsHashCodeToString)2 AClassWithNativeMethod (org.robolectric.testing.AClassWithNativeMethod)2 AClassWithStaticMethod (org.robolectric.testing.AClassWithStaticMethod)2 AClassWithoutEqualsHashCodeToString (org.robolectric.testing.AClassWithoutEqualsHashCodeToString)2 AnInstrumentedClassWithoutToStringWithSuperToString (org.robolectric.testing.AnInstrumentedClassWithoutToStringWithSuperToString)2 SwitchPoint (java.lang.invoke.SwitchPoint)1 Field (java.lang.reflect.Field)1 URLClassLoader (java.net.URLClassLoader)1