Search in sources :

Example 6 with MockClassLoader

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

the class MockClassLoaderFactory method createMockClassLoader.

protected ClassLoader createMockClassLoader(final String[] classesToLoadByMockClassloader) {
    List<MockTransformer> mockTransformerChain = getMockTransformers(extraMockTransformers);
    final UseClassPathAdjuster useClassPathAdjuster = testClass.getAnnotation(UseClassPathAdjuster.class);
    ClassLoader mockLoader = AccessController.doPrivileged(new PrivilegedAction<MockClassLoader>() {

        @Override
        public MockClassLoader run() {
            return new MockClassLoader(classesToLoadByMockClassloader, packagesToIgnore, useClassPathAdjuster);
        }
    });
    MockClassLoader mockClassLoader = (MockClassLoader) mockLoader;
    mockClassLoader.setMockTransformerChain(mockTransformerChain);
    new MockPolicyInitializerImpl(testClass).initialize(mockLoader);
    return mockLoader;
}
Also used : MockTransformer(org.powermock.core.transformers.MockTransformer) ClassMockTransformer(org.powermock.core.transformers.impl.ClassMockTransformer) InterfaceMockTransformer(org.powermock.core.transformers.impl.InterfaceMockTransformer) UseClassPathAdjuster(org.powermock.core.classloader.annotations.UseClassPathAdjuster) MockClassLoader(org.powermock.core.classloader.MockClassLoader) MockClassLoader(org.powermock.core.classloader.MockClassLoader)

Example 7 with MockClassLoader

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

the class PowerMockClassloaderExecutor method forClass.

public static SingleClassloaderExecutor forClass(Class<?> testClass, MockPolicyInitializer mockPolicyInitializer) {
    List<MockTransformer> mockTransformerChain = new ArrayList<MockTransformer>();
    mockTransformerChain.add(new ClassMockTransformer());
    mockTransformerChain.add(new InterfaceMockTransformer());
    MockClassLoader mockLoader = new MockClassLoader(new String[0], new String[0]);
    mockLoader.setMockTransformerChain(mockTransformerChain);
    mockLoader.addIgnorePackage(new PowerMockIgnorePackagesExtractorImpl().getPackagesToIgnore(testClass));
    mockLoader.addClassesToModify(new PrepareForTestExtractorImpl().getTestClasses(testClass));
    mockLoader.addClassesToModify(new StaticConstructorSuppressExtractorImpl().getTestClasses(testClass));
    mockPolicyInitializer.initialize(mockLoader);
    return new SingleClassloaderExecutor(mockLoader);
}
Also used : MockTransformer(org.powermock.core.transformers.MockTransformer) ClassMockTransformer(org.powermock.core.transformers.impl.ClassMockTransformer) InterfaceMockTransformer(org.powermock.core.transformers.impl.InterfaceMockTransformer) PowerMockIgnorePackagesExtractorImpl(org.powermock.tests.utils.impl.PowerMockIgnorePackagesExtractorImpl) StaticConstructorSuppressExtractorImpl(org.powermock.tests.utils.impl.StaticConstructorSuppressExtractorImpl) MockClassLoader(org.powermock.core.classloader.MockClassLoader) ArrayList(java.util.ArrayList) SingleClassloaderExecutor(org.powermock.classloading.SingleClassloaderExecutor) InterfaceMockTransformer(org.powermock.core.transformers.impl.InterfaceMockTransformer) ClassMockTransformer(org.powermock.core.transformers.impl.ClassMockTransformer) PrepareForTestExtractorImpl(org.powermock.tests.utils.impl.PrepareForTestExtractorImpl)

Example 8 with MockClassLoader

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

the class ClassMockTransformerTest method loadWithMockClassLoader.

private Class<?> loadWithMockClassLoader(String className) throws ClassNotFoundException {
    MockClassLoader loader = new MockClassLoader(new String[] { MockClassLoader.MODIFY_ALL_CLASSES });
    loader.setMockTransformerChain(Collections.<MockTransformer>singletonList(new ClassMockTransformer()));
    return Class.forName(className, true, loader);
}
Also used : MockClassLoader(org.powermock.core.classloader.MockClassLoader)

Example 9 with MockClassLoader

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

the class XStreamClassloaderExecutorTest 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 10 with MockClassLoader

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

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

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