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);
}
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");
}
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();
}
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;
}
Aggregations