use of org.powermock.core.transformers.MockTransformer in project powermock by powermock.
the class MockClassLoader method loadMockClass.
/**
* Load a mocked version of the class.
*/
private Class<?> loadMockClass(String name, ProtectionDomain protectionDomain) {
final byte[] clazz;
ClassPool.doPruning = false;
try {
CtClass type = classPool.get(name);
for (MockTransformer transformer : mockTransformerChain) {
type = transformer.transform(type);
}
javaAssistClassMarker.mark(type);
/*
* ClassPool may cause huge memory consumption if the number of CtClass
* objects becomes amazingly large (this rarely happens since Javassist
* tries to reduce memory consumption in various ways). To avoid this
* problem, you can explicitly remove an unnecessary CtClass object from
* the ClassPool. If you call detach() on a CtClass object, then that
* CtClass object is removed from the ClassPool.
*/
type.detach();
clazz = type.toBytecode();
} catch (Exception e) {
throw new IllegalStateException("Failed to transform class with name " + name + ". Reason: " + e.getMessage(), e);
}
return defineClass(name, clazz, 0, clazz.length, protectionDomain);
}
use of org.powermock.core.transformers.MockTransformer in project powermock by powermock.
the class ClassLoaderFactory method getMockTransformers.
private List<MockTransformer> getMockTransformers() {
List<MockTransformer> mockTransformerChain = new ArrayList<MockTransformer>();
mockTransformerChain.add(new ClassMockTransformer());
mockTransformerChain.add(new InterfaceMockTransformer());
return mockTransformerChain;
}
use of org.powermock.core.transformers.MockTransformer in project powermock by powermock.
the class AbstractCommonTestSuiteChunkerImpl method chunkClass.
protected void chunkClass(final Class<?> testClass) throws Exception {
List<Method> testMethodsForOtherClassLoaders = new ArrayList<Method>();
MockTransformer[] extraMockTransformers = createDefaultExtraMockTransformers(testClass, testMethodsForOtherClassLoaders);
final String[] ignorePackages = ignorePackagesExtractor.getPackagesToIgnore(testClass);
final ClassLoader defaultMockLoader = createDefaultMockLoader(testClass, extraMockTransformers, ignorePackages);
List<Method> currentClassloaderMethods = new LinkedList<Method>();
// Put the first suite in the map of internal suites.
TestChunk defaultTestChunk = new TestChunkImpl(defaultMockLoader, currentClassloaderMethods);
List<TestChunk> testChunks = new LinkedList<TestChunk>();
testChunks.add(defaultTestChunk);
internalSuites.add(new TestCaseEntry(testClass, testChunks));
initEntries(internalSuites);
if (!currentClassloaderMethods.isEmpty()) {
List<TestChunk> allTestChunks = internalSuites.get(0).getTestChunks();
for (TestChunk chunk : allTestChunks.subList(1, allTestChunks.size())) {
for (Method m : chunk.getTestMethodsToBeExecutedByThisClassloader()) {
testMethodsForOtherClassLoaders.add(m);
}
}
} else if (2 <= internalSuites.size() || 1 == internalSuites.size() && 2 <= internalSuites.get(0).getTestChunks().size()) {
/*
* If we don't have any test that should be executed by the default
* class loader remove it to avoid duplicate test print outs.
*/
internalSuites.get(0).getTestChunks().remove(0);
}
//else{ /*Delegation-runner maybe doesn't use test-method annotations!*/ }
}
use of org.powermock.core.transformers.MockTransformer in project powermock by powermock.
the class MockClassLoaderFactory method getMockTransformers.
protected List<MockTransformer> getMockTransformers(MockTransformer[] extraMockTransformers) {
List<MockTransformer> mockTransformerChain = new ArrayList<MockTransformer>();
mockTransformerChain.add(new ClassMockTransformer());
mockTransformerChain.add(new InterfaceMockTransformer());
Collections.addAll(mockTransformerChain, extraMockTransformers);
return mockTransformerChain;
}
use of org.powermock.core.transformers.MockTransformer 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");
}
Aggregations