use of org.robolectric.internal.bytecode.SandboxClassLoader in project robolectric by robolectric.
the class SandboxClassLoaderTest method shouldPerformClassLoadForAcquiredClasses.
@Test
public void shouldPerformClassLoadForAcquiredClasses() throws Exception {
ClassLoader classLoader = new SandboxClassLoader(configureBuilder().build());
Class<?> exampleClass = classLoader.loadClass(AnUninstrumentedClass.class.getName());
assertSame(classLoader, exampleClass.getClassLoader());
try {
exampleClass.getField(ShadowConstants.CLASS_HANDLER_DATA_FIELD_NAME);
fail("class shouldn't be instrumented!");
} catch (Exception e) {
// expected
}
}
use of org.robolectric.internal.bytecode.SandboxClassLoader in project robolectric by robolectric.
the class SandboxClassLoaderTest method shouldDelegateClassLoadForUnacquiredClasses.
@Test
public void shouldDelegateClassLoadForUnacquiredClasses() throws Exception {
InstrumentationConfiguration config = mock(InstrumentationConfiguration.class);
when(config.shouldAcquire(anyString())).thenReturn(false);
when(config.shouldInstrument(any(ClassInfo.class))).thenReturn(false);
ClassLoader classLoader = new SandboxClassLoader(config);
Class<?> exampleClass = classLoader.loadClass(AnExampleClass.class.getName());
assertSame(getClass().getClassLoader(), exampleClass.getClassLoader());
}
use of org.robolectric.internal.bytecode.SandboxClassLoader in project robolectric by robolectric.
the class SandboxClassLoaderTest method invokeInterceptedMethodOnAClassToForget.
private Object invokeInterceptedMethodOnAClassToForget(String methodName) throws Exception {
setClassLoader(new SandboxClassLoader(configureBuilder().addInterceptedMethod(new MethodRef(AClassToForget.class, "*")).build()));
Class<?> theClass = loadClass(AClassThatRefersToAForgettableClassInMethodCallsReturningPrimitive.class);
Object instance = theClass.newInstance();
Method m = theClass.getDeclaredMethod(methodName);
m.setAccessible(true);
return m.invoke(shadow.directlyOn(instance, (Class<Object>) theClass));
}
use of org.robolectric.internal.bytecode.SandboxClassLoader in project robolectric by robolectric.
the class SandboxClassLoaderTest method shouldPerformClassLoadAndInstrumentLoadForInstrumentedClasses.
@Test
public void shouldPerformClassLoadAndInstrumentLoadForInstrumentedClasses() throws Exception {
ClassLoader classLoader = new SandboxClassLoader(configureBuilder().build());
Class<?> exampleClass = classLoader.loadClass(AnExampleClass.class.getName());
assertSame(classLoader, exampleClass.getClassLoader());
Field roboDataField = exampleClass.getField(ShadowConstants.CLASS_HANDLER_DATA_FIELD_NAME);
assertNotNull(roboDataField);
assertThat(Modifier.isPublic(roboDataField.getModifiers())).isTrue();
// field should be marked final so Mockito doesn't try to @InjectMocks on it;
// see https://github.com/robolectric/robolectric/issues/2442
assertThat(Modifier.isFinal(roboDataField.getModifiers())).isTrue();
}
use of org.robolectric.internal.bytecode.SandboxClassLoader in project robolectric by robolectric.
the class SandboxClassLoaderTest method loadClass.
private Class<?> loadClass(Class<?> clazz) throws ClassNotFoundException {
if (classLoader == null) {
classLoader = new SandboxClassLoader(configureBuilder().build());
}
setStaticField(classLoader.loadClass(InvokeDynamicSupport.class.getName()), "INTERCEPTORS", new Interceptors(Collections.<Interceptor>emptyList()));
setStaticField(classLoader.loadClass(Shadow.class.getName()), "SHADOW_IMPL", newInstance(classLoader.loadClass(ShadowImpl.class.getName())));
ShadowInvalidator invalidator = Mockito.mock(ShadowInvalidator.class);
when(invalidator.getSwitchPoint(any(Class.class))).thenReturn(new SwitchPoint());
String className = RobolectricInternals.class.getName();
Class<?> robolectricInternalsClass = ReflectionHelpers.loadClass(classLoader, className);
ReflectionHelpers.setStaticField(robolectricInternalsClass, "classHandler", classHandler);
ReflectionHelpers.setStaticField(robolectricInternalsClass, "shadowInvalidator", invalidator);
return classLoader.loadClass(clazz.getName());
}
Aggregations