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