Search in sources :

Example 1 with UseClassPathAdjuster

use of org.powermock.core.classloader.annotations.UseClassPathAdjuster 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 2 with UseClassPathAdjuster

use of org.powermock.core.classloader.annotations.UseClassPathAdjuster in project powermock by powermock.

the class MockClassLoaderTest method canFindDynamicClassFromAdjustedClasspath.

@Test
public void canFindDynamicClassFromAdjustedClasspath() throws Exception {
    // Construct MockClassLoader with @UseClassPathAdjuster annotation.
    // It activates our MyClassPathAdjuster class which appends our dynamic
    // class to the MockClassLoader's classpool.
    UseClassPathAdjuster useClassPathAdjuster = new UseClassPathAdjuster() {

        public Class<? extends Annotation> annotationType() {
            return UseClassPathAdjuster.class;
        }

        public Class<? extends ClassPathAdjuster> value() {
            return MyClassPathAdjuster.class;
        }
    };
    final MockClassLoader mockClassLoader = new MockClassLoader(new String[0], useClassPathAdjuster);
    List<MockTransformer> list = new LinkedList<MockTransformer>();
    list.add(new ClassMockTransformer());
    mockClassLoader.setMockTransformerChain(list);
    // setup custom classloader providing our dynamic class, for MockClassLoader to defer to
    mockClassLoader.deferTo = new ClassLoader(getClass().getClassLoader()) {

        @Override
        public Class<?> loadClass(String name) throws ClassNotFoundException {
            if (name.equals(DynamicClassHolder.clazz.getName())) {
                return DynamicClassHolder.clazz;
            }
            return super.loadClass(name);
        }
    };
    // verify that MockClassLoader can successfully load the class
    Class<?> dynamicTestClass = Class.forName(DynamicClassHolder.clazz.getName(), false, mockClassLoader);
    assertThat(dynamicTestClass).isNotSameAs(DynamicClassHolder.clazz);
}
Also used : MockTransformer(org.powermock.core.transformers.MockTransformer) ClassMockTransformer(org.powermock.core.transformers.impl.ClassMockTransformer) UseClassPathAdjuster(org.powermock.core.classloader.annotations.UseClassPathAdjuster) LinkedList(java.util.LinkedList) CtClass(javassist.CtClass) ClassMockTransformer(org.powermock.core.transformers.impl.ClassMockTransformer) Test(org.junit.Test)

Aggregations

UseClassPathAdjuster (org.powermock.core.classloader.annotations.UseClassPathAdjuster)2 MockTransformer (org.powermock.core.transformers.MockTransformer)2 ClassMockTransformer (org.powermock.core.transformers.impl.ClassMockTransformer)2 LinkedList (java.util.LinkedList)1 CtClass (javassist.CtClass)1 Test (org.junit.Test)1 MockClassLoader (org.powermock.core.classloader.MockClassLoader)1 InterfaceMockTransformer (org.powermock.core.transformers.impl.InterfaceMockTransformer)1