Search in sources :

Example 1 with MockClassLoader

use of org.powermock.core.classloader.MockClassLoader in project powermock by powermock.

the class ObjenesisClassloaderExecutorTest method clonesStaticFinalObjectFields.

@Test
public void clonesStaticFinalObjectFields() throws Exception {
    MockClassLoader classloader = createClassloader();
    final MyStaticFinalArgumentHolder expected = new MyStaticFinalArgumentHolder();
    MyStaticFinalArgumentHolder actual = new SingleClassloaderExecutor(classloader).execute(new Callable<MyStaticFinalArgumentHolder>() {

        public MyStaticFinalArgumentHolder call() throws Exception {
            assertEquals(MockClassLoader.class.getName(), this.getClass().getClassLoader().getClass().getName());
            MyStaticFinalArgumentHolder actual = new MyStaticFinalArgumentHolder();
            assertEquals(expected.getMyObject(), actual.getMyObject());
            return actual;
        }
    });
    assertFalse(MockClassLoader.class.getName().equals(this.getClass().getClassLoader().getClass().getName()));
    assertEquals(expected.getMyObject(), actual.getMyObject());
}
Also used : MockClassLoader(org.powermock.core.classloader.MockClassLoader) SingleClassloaderExecutor(org.powermock.classloading.SingleClassloaderExecutor) MyStaticFinalArgumentHolder(powermock.classloading.classes.MyStaticFinalArgumentHolder) Test(org.junit.Test)

Example 2 with MockClassLoader

use of org.powermock.core.classloader.MockClassLoader in project powermock by powermock.

the class ObjenesisClassloaderExecutorTest method loadsObjectGraphInSpecifiedClassloaderAndReturnsResultInOriginalClassloader.

@Test
public void loadsObjectGraphInSpecifiedClassloaderAndReturnsResultInOriginalClassloader() throws Exception {
    MockClassLoader classloader = createClassloader();
    final MyReturnValue expectedConstructorValue = new MyReturnValue(new MyArgument("first value"));
    final MyClass myClass = new MyClass(expectedConstructorValue);
    final MyArgument expected = new MyArgument("A value");
    MyReturnValue[] actual = new SingleClassloaderExecutor(classloader).execute(new Callable<MyReturnValue[]>() {

        public MyReturnValue[] call() throws Exception {
            assertEquals(MockClassLoader.class.getName(), this.getClass().getClassLoader().getClass().getName());
            return myClass.myMethod(expected);
        }
    });
    assertFalse(MockClassLoader.class.getName().equals(this.getClass().getClassLoader().getClass().getName()));
    final MyReturnValue myReturnValue = actual[0];
    assertEquals(expectedConstructorValue.getMyArgument().getValue(), myReturnValue.getMyArgument().getValue());
    assertEquals(expected.getValue(), actual[1].getMyArgument().getValue());
}
Also used : MyArgument(powermock.classloading.classes.MyArgument) MyClass(powermock.classloading.classes.MyClass) MockClassLoader(org.powermock.core.classloader.MockClassLoader) SingleClassloaderExecutor(org.powermock.classloading.SingleClassloaderExecutor) MyReturnValue(powermock.classloading.classes.MyReturnValue) Test(org.junit.Test)

Example 3 with MockClassLoader

use of org.powermock.core.classloader.MockClassLoader in project powermock by powermock.

the class ObjenesisClassloaderExecutorTest method loadsObjectGraphThatIncludesEnumsInSpecifiedClassloaderAndReturnsResultInOriginalClassloader.

@Test
public void loadsObjectGraphThatIncludesEnumsInSpecifiedClassloaderAndReturnsResultInOriginalClassloader() throws Exception {
    MockClassLoader classloader = createClassloader();
    final MyEnum expected = MyEnum.MyEnum1;
    final MyEnumHolder myClass = new MyEnumHolder(expected);
    MyEnum actual = new SingleClassloaderExecutor(classloader).execute(new Callable<MyEnum>() {

        public MyEnum call() throws Exception {
            assertEquals(MockClassLoader.class.getName(), this.getClass().getClassLoader().getClass().getName());
            MyEnum myEnum = myClass.getMyEnum();
            assertEquals(expected, myEnum);
            return myEnum;
        }
    });
    assertFalse(MockClassLoader.class.getName().equals(this.getClass().getClassLoader().getClass().getName()));
    assertEquals(expected, actual);
}
Also used : MyEnum(powermock.classloading.classes.MyEnum) MockClassLoader(org.powermock.core.classloader.MockClassLoader) SingleClassloaderExecutor(org.powermock.classloading.SingleClassloaderExecutor) MyEnumHolder(powermock.classloading.classes.MyEnumHolder) Test(org.junit.Test)

Example 4 with MockClassLoader

use of org.powermock.core.classloader.MockClassLoader in project powermock by powermock.

the class ObjenesisClassloaderExecutorTest method worksWithObjectHierarchyAndOverloadedFields.

@Test
@Ignore("It seems like this test works on certain JVM's and fails on others. WHY!??!")
public void worksWithObjectHierarchyAndOverloadedFields() throws Exception {
    final MockClassLoader classloader = createClassloader();
    final MyHierarchicalOverloadedFieldHolder tested = new MyHierarchicalOverloadedFieldHolder();
    assertSame(tested.getMyArgument1(), tested.getMyArgument2());
    assertEquals(tested.getMyArgument1(), tested.getMyArgument3());
    assertSame(tested.getMyArgument3(), MyHierarchicalOverloadedFieldHolder.MY_ARGUMENT);
    assertNotSame(MyReferenceFieldHolder.MY_ARGUMENT, MyHierarchicalOverloadedFieldHolder.MY_ARGUMENT);
    assertEquals(MyReferenceFieldHolder.MY_ARGUMENT, MyHierarchicalOverloadedFieldHolder.MY_ARGUMENT);
    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.getMyArgument1(), tested.getMyArgument3());
            assertSame(tested.getMyArgument3(), MyHierarchicalOverloadedFieldHolder.MY_ARGUMENT);
            assertNotSame(MyReferenceFieldHolder.MY_ARGUMENT, MyHierarchicalOverloadedFieldHolder.MY_ARGUMENT);
            assertEquals(MyReferenceFieldHolder.MY_ARGUMENT, MyHierarchicalOverloadedFieldHolder.MY_ARGUMENT);
        }
    });
}
Also used : MyHierarchicalOverloadedFieldHolder(powermock.classloading.classes.MyHierarchicalOverloadedFieldHolder) MockClassLoader(org.powermock.core.classloader.MockClassLoader) SingleClassloaderExecutor(org.powermock.classloading.SingleClassloaderExecutor) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 5 with MockClassLoader

use of org.powermock.core.classloader.MockClassLoader in project powermock by powermock.

the class ObjenesisClassloaderExecutorTest method clonesStaticFinalPrimitiveFields.

@Test
public void clonesStaticFinalPrimitiveFields() throws Exception {
    MockClassLoader classloader = createClassloader();
    final MyStaticFinalPrimitiveHolder expected = new MyStaticFinalPrimitiveHolder();
    MyStaticFinalPrimitiveHolder actual = new SingleClassloaderExecutor(classloader).execute(new Callable<MyStaticFinalPrimitiveHolder>() {

        public MyStaticFinalPrimitiveHolder call() throws Exception {
            assertEquals(MockClassLoader.class.getName(), this.getClass().getClassLoader().getClass().getName());
            MyStaticFinalPrimitiveHolder actual = new MyStaticFinalPrimitiveHolder();
            assertEquals(expected.getMyInt(), actual.getMyInt());
            return actual;
        }
    });
    assertFalse(MockClassLoader.class.getName().equals(this.getClass().getClassLoader().getClass().getName()));
    assertEquals(expected.getMyInt(), actual.getMyInt());
}
Also used : MockClassLoader(org.powermock.core.classloader.MockClassLoader) SingleClassloaderExecutor(org.powermock.classloading.SingleClassloaderExecutor) MyStaticFinalPrimitiveHolder(powermock.classloading.classes.MyStaticFinalPrimitiveHolder) Test(org.junit.Test)

Aggregations

MockClassLoader (org.powermock.core.classloader.MockClassLoader)35 Test (org.junit.Test)26 SingleClassloaderExecutor (org.powermock.classloading.SingleClassloaderExecutor)25 MyArgument (powermock.classloading.classes.MyArgument)8 MyReturnValue (powermock.classloading.classes.MyReturnValue)8 LinkedList (java.util.LinkedList)5 MockTransformer (org.powermock.core.transformers.MockTransformer)4 MyClass (powermock.classloading.classes.MyClass)4 Method (java.lang.reflect.Method)3 Collection (java.util.Collection)2 CtClass (javassist.CtClass)2 Ignore (org.junit.Ignore)2 IndicateReloadClass (org.powermock.core.IndicateReloadClass)2 ClassMockTransformer (org.powermock.core.transformers.impl.ClassMockTransformer)2 InterfaceMockTransformer (org.powermock.core.transformers.impl.InterfaceMockTransformer)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