Search in sources :

Example 11 with MockTransformer

use of org.powermock.core.transformers.MockTransformer 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)

Example 12 with MockTransformer

use of org.powermock.core.transformers.MockTransformer in project powermock by powermock.

the class MockClassLoaderTest method canFindResources.

@Test
public void canFindResources() throws Exception {
    final MockClassLoader mockClassLoader = new MockClassLoader(new String[0]);
    List<MockTransformer> list = new LinkedList<MockTransformer>();
    list.add(new ClassMockTransformer());
    mockClassLoader.setMockTransformerChain(list);
    // Force a ClassLoader that can find 'foo/bar/baz/test.txt' into
    // mockClassLoader.deferTo.
    mockClassLoader.deferTo = new ResourcePrefixClassLoader(getClass().getClassLoader(), "org/powermock/core/classloader/");
    // MockClassLoader will only be able to find 'foo/bar/baz/test.txt' if it
    // properly defers the resources lookup to its deferTo ClassLoader.
    Enumeration<URL> resources = mockClassLoader.getResources("foo/bar/baz/test.txt");
    assertThat(resources.nextElement().getPath()).endsWith("test.txt");
}
Also used : MockTransformer(org.powermock.core.transformers.MockTransformer) ClassMockTransformer(org.powermock.core.transformers.impl.ClassMockTransformer) LinkedList(java.util.LinkedList) URL(java.net.URL) ClassMockTransformer(org.powermock.core.transformers.impl.ClassMockTransformer) Test(org.junit.Test)

Example 13 with MockTransformer

use of org.powermock.core.transformers.MockTransformer in project powermock by powermock.

the class MockClassLoaderTest method resourcesNotDoubled.

@Test
public void resourcesNotDoubled() throws Exception {
    final MockClassLoader mockClassLoader = new MockClassLoader(new String[0]);
    List<MockTransformer> list = new LinkedList<MockTransformer>();
    list.add(new ClassMockTransformer());
    mockClassLoader.setMockTransformerChain(list);
    // MockClassLoader will only be able to find 'foo/bar/baz/test.txt' if it
    // properly defers the resources lookup to its deferTo ClassLoader.
    Enumeration<URL> resources = mockClassLoader.getResources("org/powermock/core/classloader/foo/bar/baz/test.txt");
    assertThat(resources.nextElement().getPath()).endsWith("test.txt");
    assertThat(resources.hasMoreElements()).isFalse();
}
Also used : MockTransformer(org.powermock.core.transformers.MockTransformer) ClassMockTransformer(org.powermock.core.transformers.impl.ClassMockTransformer) LinkedList(java.util.LinkedList) URL(java.net.URL) ClassMockTransformer(org.powermock.core.transformers.impl.ClassMockTransformer) Test(org.junit.Test)

Example 14 with MockTransformer

use of org.powermock.core.transformers.MockTransformer in project powermock by powermock.

the class ObjenesisClassloaderExecutorTest method createClassloader.

private MockClassLoader createClassloader() {
    MockClassLoader classloader = new MockClassLoader(new String[] { MyClass.class.getName(), MyArgument.class.getName(), MyReturnValue.class.getName() });
    MockTransformer mainMockTransformer = new MockTransformer() {

        public CtClass transform(CtClass clazz) throws Exception {
            return clazz;
        }
    };
    LinkedList<MockTransformer> linkedList = new LinkedList<MockTransformer>();
    linkedList.add(mainMockTransformer);
    classloader.setMockTransformerChain(linkedList);
    return classloader;
}
Also used : MyClass(powermock.classloading.classes.MyClass) MyArgument(powermock.classloading.classes.MyArgument) MockTransformer(org.powermock.core.transformers.MockTransformer) CtClass(javassist.CtClass) MockClassLoader(org.powermock.core.classloader.MockClassLoader) MyReturnValue(powermock.classloading.classes.MyReturnValue) LinkedList(java.util.LinkedList)

Aggregations

MockTransformer (org.powermock.core.transformers.MockTransformer)14 ClassMockTransformer (org.powermock.core.transformers.impl.ClassMockTransformer)10 LinkedList (java.util.LinkedList)9 Test (org.junit.Test)6 CtClass (javassist.CtClass)5 MockClassLoader (org.powermock.core.classloader.MockClassLoader)5 ArrayList (java.util.ArrayList)4 InterfaceMockTransformer (org.powermock.core.transformers.impl.InterfaceMockTransformer)4 URL (java.net.URL)3 UseClassPathAdjuster (org.powermock.core.classloader.annotations.UseClassPathAdjuster)2 MyArgument (powermock.classloading.classes.MyArgument)2 MyClass (powermock.classloading.classes.MyClass)2 MyReturnValue (powermock.classloading.classes.MyReturnValue)2 Method (java.lang.reflect.Method)1 SingleClassloaderExecutor (org.powermock.classloading.SingleClassloaderExecutor)1 TestChunk (org.powermock.tests.utils.TestChunk)1 PowerMockIgnorePackagesExtractorImpl (org.powermock.tests.utils.impl.PowerMockIgnorePackagesExtractorImpl)1 PrepareForTestExtractorImpl (org.powermock.tests.utils.impl.PrepareForTestExtractorImpl)1 StaticConstructorSuppressExtractorImpl (org.powermock.tests.utils.impl.StaticConstructorSuppressExtractorImpl)1