Search in sources :

Example 1 with MyArgument

use of powermock.classloading.classes.MyArgument 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 2 with MyArgument

use of powermock.classloading.classes.MyArgument in project powermock by powermock.

the class XStreamClassloaderExecutorTest 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 MyArgument

use of powermock.classloading.classes.MyArgument in project powermock by powermock.

the class XStreamClassloaderExecutorTest 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 4 with MyArgument

use of powermock.classloading.classes.MyArgument 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 5 with MyArgument

use of powermock.classloading.classes.MyArgument 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)

Aggregations

Test (org.junit.Test)6 SingleClassloaderExecutor (org.powermock.classloading.SingleClassloaderExecutor)6 MockClassLoader (org.powermock.core.classloader.MockClassLoader)6 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 MyClass (powermock.classloading.classes.MyClass)2 MyCollectionHolder (powermock.classloading.classes.MyCollectionHolder)2 ReflectionMethodInvoker (powermock.classloading.classes.ReflectionMethodInvoker)2 Ignore (org.junit.Ignore)1