Search in sources :

Example 6 with ClassMockTransformer

use of org.powermock.core.transformers.impl.ClassMockTransformer in project powermock by powermock.

the class MockClassLoaderTest method canFindResource.

@Test
public void canFindResource() 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 resource lookup to its deferTo ClassLoader.
    URL resource = mockClassLoader.getResource("foo/bar/baz/test.txt");
    assertThat(resource).isNotNull();
    assertThat(resource.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 7 with ClassMockTransformer

use of org.powermock.core.transformers.impl.ClassMockTransformer 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 8 with ClassMockTransformer

use of org.powermock.core.transformers.impl.ClassMockTransformer 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 9 with ClassMockTransformer

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

Aggregations

MockTransformer (org.powermock.core.transformers.MockTransformer)9 ClassMockTransformer (org.powermock.core.transformers.impl.ClassMockTransformer)9 LinkedList (java.util.LinkedList)6 Test (org.junit.Test)6 URL (java.net.URL)3 ArrayList (java.util.ArrayList)3 InterfaceMockTransformer (org.powermock.core.transformers.impl.InterfaceMockTransformer)3 CtClass (javassist.CtClass)2 SingleClassloaderExecutor (org.powermock.classloading.SingleClassloaderExecutor)1 MockClassLoader (org.powermock.core.classloader.MockClassLoader)1 UseClassPathAdjuster (org.powermock.core.classloader.annotations.UseClassPathAdjuster)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