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);
}
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);
}
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);
}
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)");
}
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));
}
Aggregations