Search in sources :

Example 16 with MockClassLoader

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

the class AbstractCommonTestSuiteChunkerImpl method putMethodToChunk.

private void putMethodToChunk(TestCaseEntry testCaseEntry, Class<?> testClass, Method method) {
    if (shouldExecuteTestForMethod(testClass, method)) {
        currentTestIndex++;
        if (hasChunkAnnotation(method)) {
            LinkedList<Method> methodsInThisChunk = new LinkedList<Method>();
            methodsInThisChunk.add(method);
            final String[] staticSuppressionClasses = getStaticSuppressionClasses(testClass, method);
            TestClassTransformer[] extraTransformers = null == testMethodAnnotation() ? new TestClassTransformer[0] : new TestClassTransformer[] { TestClassTransformer.forTestClass(testClass).removesTestMethodAnnotation(testMethodAnnotation()).fromAllMethodsExcept(method) };
            final ClassLoader mockClassloader;
            if (method.isAnnotationPresent(PrepareEverythingForTest.class)) {
                mockClassloader = createNewClassloader(testClass, new String[] { MockClassLoader.MODIFY_ALL_CLASSES }, ignorePackagesExtractor.getPackagesToIgnore(testClass), extraTransformers);
            } else {
                mockClassloader = createNewClassloader(testClass, arrayMerger.mergeArrays(String.class, prepareForTestExtractor.getTestClasses(method), staticSuppressionClasses), ignorePackagesExtractor.getPackagesToIgnore(testClass), extraTransformers);
            }
            TestChunkImpl chunk = new TestChunkImpl(mockClassloader, methodsInThisChunk);
            testCaseEntry.getTestChunks().add(chunk);
            updatedIndexes();
        } else {
            testCaseEntry.getTestChunks().get(0).getTestMethodsToBeExecutedByThisClassloader().add(method);
            // currentClassloaderMethods.add(method);
            final int currentDelegateIndex = internalSuites.size() - 1;
            /*
                 * Add this test index to the main junit runner
                 * delegator.
                 */
            List<Integer> testList = testAtDelegateMapper.get(currentDelegateIndex);
            if (testList == null) {
                testList = new LinkedList<Integer>();
                testAtDelegateMapper.put(currentDelegateIndex, testList);
            }
            testList.add(currentTestIndex);
        }
    }
}
Also used : Method(java.lang.reflect.Method) TestClassTransformer(org.powermock.core.transformers.impl.TestClassTransformer) LinkedList(java.util.LinkedList) MockClassLoader(org.powermock.core.classloader.MockClassLoader)

Example 17 with MockClassLoader

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

the class XStreamClassloaderExecutorTest method worksWithObjectHierarchyAndOverloadedFields.

@Test
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());
            // Note: Cannot be same using X-Stream
            assertEquals(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) Test(org.junit.Test)

Example 18 with MockClassLoader

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

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

Example 19 with MockClassLoader

use of org.powermock.core.classloader.MockClassLoader 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 20 with MockClassLoader

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

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

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