Search in sources :

Example 21 with SingleClassloaderExecutor

use of org.powermock.classloading.SingleClassloaderExecutor in project powermock by powermock.

the class ObjenesisClassloaderExecutorTest method worksWithReflection.

@Test
public void worksWithReflection() throws Exception {
    final MockClassLoader classloader = createClassloader();
    final MyArgument myArgument = new MyArgument("test");
    final MyReturnValue instance = new MyReturnValue(myArgument);
    Method method = instance.getClass().getMethod("getMyArgument");
    final ReflectionMethodInvoker tested = new ReflectionMethodInvoker(method, instance);
    new SingleClassloaderExecutor(classloader).execute(new Runnable() {

        public void run() {
            Object invoke = tested.invoke();
            assertSame(invoke, myArgument);
        }
    });
}
Also used : MyArgument(powermock.classloading.classes.MyArgument) MockClassLoader(org.powermock.core.classloader.MockClassLoader) SingleClassloaderExecutor(org.powermock.classloading.SingleClassloaderExecutor) Method(java.lang.reflect.Method) ReflectionMethodInvoker(powermock.classloading.classes.ReflectionMethodInvoker) MyReturnValue(powermock.classloading.classes.MyReturnValue) Test(org.junit.Test)

Example 22 with SingleClassloaderExecutor

use of org.powermock.classloading.SingleClassloaderExecutor in project powermock by powermock.

the class ObjenesisClassloaderExecutorTest method loadsObjectGraphThatIncludesCollectionInSpecifiedClassloaderAndReturnsResultInOriginalClassloader.

@Ignore("It seems like this test works on certain JVM's and fails on others. WHY!??!")
@Test
public void loadsObjectGraphThatIncludesCollectionInSpecifiedClassloaderAndReturnsResultInOriginalClassloader() throws Exception {
    final MockClassLoader classloader = createClassloader();
    final Collection<MyReturnValue> expected = new LinkedList<MyReturnValue>();
    expected.add(new MyReturnValue(new MyArgument("one")));
    expected.add(new MyReturnValue(new MyArgument("two")));
    final MyCollectionHolder myClass = new MyCollectionHolder(expected);
    Collection<?> actual = new SingleClassloaderExecutor(classloader).execute(new Callable<Collection<?>>() {

        public Collection<?> call() throws Exception {
            assertEquals(MockClassLoader.class.getName(), this.getClass().getClassLoader().getClass().getName());
            Collection<?> myCollection = myClass.getMyCollection();
            for (Object object : myCollection) {
                assertEquals(MockClassLoader.class.getName(), object.getClass().getClassLoader().getClass().getName());
            }
            return myCollection;
        }
    });
    assertFalse(MockClassLoader.class.getName().equals(this.getClass().getClassLoader().getClass().getName()));
    assertEquals(2, actual.size());
    for (Object object : actual) {
        final String value = ((MyReturnValue) object).getMyArgument().getValue();
        assertTrue(value.equals("one") || value.equals("two"));
    }
}
Also used : MyArgument(powermock.classloading.classes.MyArgument) MockClassLoader(org.powermock.core.classloader.MockClassLoader) SingleClassloaderExecutor(org.powermock.classloading.SingleClassloaderExecutor) LinkedList(java.util.LinkedList) MyCollectionHolder(powermock.classloading.classes.MyCollectionHolder) Collection(java.util.Collection) MyReturnValue(powermock.classloading.classes.MyReturnValue) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 23 with SingleClassloaderExecutor

use of org.powermock.classloading.SingleClassloaderExecutor in project powermock by powermock.

the class ObjenesisClassloaderExecutorTest method loadsObjectGraphThatIncludesPrimitiveArraysInSpecifiedClassloaderAndReturnsResultInOriginalClassloader.

@Test
public void loadsObjectGraphThatIncludesPrimitiveArraysInSpecifiedClassloaderAndReturnsResultInOriginalClassloader() throws Exception {
    MockClassLoader classloader = createClassloader();
    final int[] expected = new int[] { 1, 2 };
    final MyPrimitiveArrayHolder myClass = new MyPrimitiveArrayHolder(expected);
    int[] actual = new SingleClassloaderExecutor(classloader).execute(new Callable<int[]>() {

        public int[] call() throws Exception {
            assertEquals(MockClassLoader.class.getName(), this.getClass().getClassLoader().getClass().getName());
            int[] myArray = myClass.getMyArray();
            assertArrayEquals(expected, myArray);
            return myArray;
        }
    });
    assertFalse(MockClassLoader.class.getName().equals(this.getClass().getClassLoader().getClass().getName()));
    assertArrayEquals(expected, actual);
}
Also used : MockClassLoader(org.powermock.core.classloader.MockClassLoader) MyPrimitiveArrayHolder(powermock.classloading.classes.MyPrimitiveArrayHolder) SingleClassloaderExecutor(org.powermock.classloading.SingleClassloaderExecutor) Test(org.junit.Test)

Example 24 with SingleClassloaderExecutor

use of org.powermock.classloading.SingleClassloaderExecutor in project powermock by powermock.

the class ObjenesisClassloaderExecutorTest method worksWithObjectHierarchy.

@Test
public void worksWithObjectHierarchy() throws Exception {
    final MockClassLoader classloader = createClassloader();
    final MyHierarchicalFieldHolder tested = new MyHierarchicalFieldHolder();
    assertSame(tested.getMyArgument1(), tested.getMyArgument2());
    assertEquals(tested.getMyArgument3(), tested.getMyArgument2());
    new SingleClassloaderExecutor(classloader).execute(new Runnable() {

        public void run() {
            assertEquals(MockClassLoader.class.getName(), this.getClass().getClassLoader().getClass().getName());
            assertSame(tested.getMyArgument1(), tested.getMyArgument2());
            assertEquals(tested.getMyArgument3(), tested.getMyArgument2());
        }
    });
}
Also used : MyHierarchicalFieldHolder(powermock.classloading.classes.MyHierarchicalFieldHolder) MockClassLoader(org.powermock.core.classloader.MockClassLoader) SingleClassloaderExecutor(org.powermock.classloading.SingleClassloaderExecutor) Test(org.junit.Test)

Example 25 with SingleClassloaderExecutor

use of org.powermock.classloading.SingleClassloaderExecutor in project powermock by powermock.

the class ObjenesisClassloaderExecutorTest method usesReferenceCloningWhenTwoFieldsPointToSameInstance.

@Test
public void usesReferenceCloningWhenTwoFieldsPointToSameInstance() throws Exception {
    final MockClassLoader classloader = createClassloader();
    final MyReferenceFieldHolder tested = new MyReferenceFieldHolder();
    assertSame(tested.getMyArgument1(), tested.getMyArgument2());
    assertSame(tested.getMyArgument1(), MyReferenceFieldHolder.MY_ARGUMENT);
    new SingleClassloaderExecutor(classloader).execute(new Runnable() {

        public void run() {
            assertEquals(MockClassLoader.class.getName(), this.getClass().getClassLoader().getClass().getName());
            assertEquals(tested.getMyArgument1(), tested.getMyArgument2());
            assertEquals(tested.getMyArgument1(), MyReferenceFieldHolder.MY_ARGUMENT);
            assertSame(tested.getMyArgument1(), tested.getMyArgument2());
        // FIXME: This assertion should work:
        // assertSame(tested.getMyArgument1(), MyReferenceFieldHolder.MY_ARGUMENT);
        }
    });
}
Also used : MockClassLoader(org.powermock.core.classloader.MockClassLoader) MyReferenceFieldHolder(powermock.classloading.classes.MyReferenceFieldHolder) SingleClassloaderExecutor(org.powermock.classloading.SingleClassloaderExecutor) Test(org.junit.Test)

Aggregations

SingleClassloaderExecutor (org.powermock.classloading.SingleClassloaderExecutor)25 MockClassLoader (org.powermock.core.classloader.MockClassLoader)25 Test (org.junit.Test)24 MyArgument (powermock.classloading.classes.MyArgument)6 MyReturnValue (powermock.classloading.classes.MyReturnValue)6 Method (java.lang.reflect.Method)2 Collection (java.util.Collection)2 LinkedList (java.util.LinkedList)2 Ignore (org.junit.Ignore)2 MyClass (powermock.classloading.classes.MyClass)2 MyCollectionHolder (powermock.classloading.classes.MyCollectionHolder)2 MyEnum (powermock.classloading.classes.MyEnum)2 MyEnumHolder (powermock.classloading.classes.MyEnumHolder)2 MyHierarchicalFieldHolder (powermock.classloading.classes.MyHierarchicalFieldHolder)2 MyHierarchicalOverloadedFieldHolder (powermock.classloading.classes.MyHierarchicalOverloadedFieldHolder)2 MyIntegerHolder (powermock.classloading.classes.MyIntegerHolder)2 MyPrimitiveArrayHolder (powermock.classloading.classes.MyPrimitiveArrayHolder)2 MyReferenceFieldHolder (powermock.classloading.classes.MyReferenceFieldHolder)2 MyStaticFinalArgumentHolder (powermock.classloading.classes.MyStaticFinalArgumentHolder)2 MyStaticFinalNumberHolder (powermock.classloading.classes.MyStaticFinalNumberHolder)2