Search in sources :

Example 16 with SingleClassloaderExecutor

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

the class XStreamClassloaderExecutorTest method clonesStaticFinalNumberFields.

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

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

Example 17 with SingleClassloaderExecutor

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

the class XStreamClassloaderExecutorTest method loadsObjectGraphThatIncludesCollectionInSpecifiedClassloaderAndReturnsResultInOriginalClassloader.

@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) Test(org.junit.Test)

Example 18 with SingleClassloaderExecutor

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

the class XStreamClassloaderExecutorTest 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 19 with SingleClassloaderExecutor

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

the class ObjenesisClassloaderExecutorTest method loadsObjectGraphThatIncludesPrimitiveValuesInSpecifiedClassloaderAndReturnsResultInOriginalClassloader.

@Test
public void loadsObjectGraphThatIncludesPrimitiveValuesInSpecifiedClassloaderAndReturnsResultInOriginalClassloader() throws Exception {
    MockClassLoader classloader = createClassloader();
    final Integer expected = 42;
    final MyIntegerHolder myClass = new MyIntegerHolder(expected);
    Integer actual = new SingleClassloaderExecutor(classloader).execute(new Callable<Integer>() {

        public Integer call() throws Exception {
            assertEquals(MockClassLoader.class.getName(), this.getClass().getClassLoader().getClass().getName());
            final int myInteger = myClass.getMyInteger();
            assertEquals((int) expected, myInteger);
            return myInteger;
        }
    });
    assertFalse(MockClassLoader.class.getName().equals(this.getClass().getClassLoader().getClass().getName()));
    assertEquals(expected, actual);
}
Also used : MockClassLoader(org.powermock.core.classloader.MockClassLoader) SingleClassloaderExecutor(org.powermock.classloading.SingleClassloaderExecutor) MyIntegerHolder(powermock.classloading.classes.MyIntegerHolder) Test(org.junit.Test)

Example 20 with SingleClassloaderExecutor

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

the class ObjenesisClassloaderExecutorTest method clonesStaticFinalNumberFields.

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

        public MyStaticFinalNumberHolder call() throws Exception {
            assertEquals(MockClassLoader.class.getName(), this.getClass().getClassLoader().getClass().getName());
            MyStaticFinalNumberHolder actual = new MyStaticFinalNumberHolder();
            assertEquals(expected.getMyLong(), actual.getMyLong());
            return actual;
        }
    });
    assertFalse(MockClassLoader.class.getName().equals(this.getClass().getClassLoader().getClass().getName()));
    assertEquals(expected.getMyLong(), actual.getMyLong());
}
Also used : MockClassLoader(org.powermock.core.classloader.MockClassLoader) SingleClassloaderExecutor(org.powermock.classloading.SingleClassloaderExecutor) MyStaticFinalNumberHolder(powermock.classloading.classes.MyStaticFinalNumberHolder) 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