Search in sources :

Example 6 with SandboxClassLoader

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
    }
}
Also used : AnUninstrumentedClass(org.robolectric.testing.AnUninstrumentedClass) SandboxClassLoader(org.robolectric.internal.bytecode.SandboxClassLoader) SandboxClassLoader(org.robolectric.internal.bytecode.SandboxClassLoader) Test(org.junit.Test)

Example 7 with SandboxClassLoader

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());
}
Also used : AnExampleClass(org.robolectric.testing.AnExampleClass) InstrumentationConfiguration(org.robolectric.internal.bytecode.InstrumentationConfiguration) SandboxClassLoader(org.robolectric.internal.bytecode.SandboxClassLoader) ClassInfo(org.robolectric.internal.bytecode.ClassInfo) SandboxClassLoader(org.robolectric.internal.bytecode.SandboxClassLoader) Test(org.junit.Test)

Example 8 with SandboxClassLoader

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));
}
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) Method(java.lang.reflect.Method) AClassWithNativeMethod(org.robolectric.testing.AClassWithNativeMethod) AClassWithStaticMethod(org.robolectric.testing.AClassWithStaticMethod) SandboxClassLoader(org.robolectric.internal.bytecode.SandboxClassLoader)

Example 9 with SandboxClassLoader

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();
}
Also used : AnExampleClass(org.robolectric.testing.AnExampleClass) ReflectionHelpers.setStaticField(org.robolectric.util.ReflectionHelpers.setStaticField) Field(java.lang.reflect.Field) SandboxClassLoader(org.robolectric.internal.bytecode.SandboxClassLoader) SandboxClassLoader(org.robolectric.internal.bytecode.SandboxClassLoader) Test(org.junit.Test)

Example 10 with SandboxClassLoader

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());
}
Also used : Interceptors(org.robolectric.internal.bytecode.Interceptors) ShadowInvalidator(org.robolectric.internal.bytecode.ShadowInvalidator) SwitchPoint(java.lang.invoke.SwitchPoint) ShadowImpl(org.robolectric.internal.bytecode.ShadowImpl) 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) Matchers.anyString(org.mockito.Matchers.anyString) AnInstrumentedClassWithoutToStringWithSuperToString(org.robolectric.testing.AnInstrumentedClassWithoutToStringWithSuperToString) AClassWithoutEqualsHashCodeToString(org.robolectric.testing.AClassWithoutEqualsHashCodeToString) AClassWithEqualsHashCodeToString(org.robolectric.testing.AClassWithEqualsHashCodeToString) Interceptor(org.robolectric.internal.bytecode.Interceptor) SandboxClassLoader(org.robolectric.internal.bytecode.SandboxClassLoader)

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