-
Notifications
You must be signed in to change notification settings - Fork 583
/
TestClassTransformerBuilder.java
81 lines (65 loc) · 3.7 KB
/
TestClassTransformerBuilder.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
package org.powermock.core.transformers;
import javassist.CtMethod;
import org.powermock.core.classloader.ByteCodeFramework;
import org.powermock.core.transformers.javassist.testclass.ForMethodsJavaAssistTestClassTransformer;
import org.powermock.core.transformers.javassist.testclass.FromAllMethodsExceptJavaAssistTestClassTransformer;
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.util.Collection;
public class TestClassTransformerBuilder {
public static TestClassTransformerBuilder forTestClass(final Class<?> testClass) {
return new TestClassTransformerBuilder(testClass);
}
private final Class<?> testClass;
private TestClassTransformerBuilder(final Class<?> testClass) {
this.testClass = testClass;
}
public RemovesTestMethodAnnotation removesTestMethodAnnotation(final Class<? extends Annotation> testMethodAnnotation) {
return new RemovesTestMethodAnnotation(testClass, testMethodAnnotation, ByteCodeFramework.getByteCodeFrameworkForTestClass(testClass));
}
public TestClassTransformerBuilderWithClue bytecodeFrameworkClue(final Method method) {
return new TestClassTransformerBuilderWithClue(testClass, method);
}
public static class TestClassTransformerBuilderWithClue {
private final Class<?> testClass;
private final Method method;
private TestClassTransformerBuilderWithClue(final Class<?> testClass, final Method method) {
this.testClass = testClass;
this.method = method;
}
public RemovesTestMethodAnnotation removesTestMethodAnnotation(final Class<? extends Annotation> testMethodAnnotation) {
return new RemovesTestMethodAnnotation(testClass, testMethodAnnotation, ByteCodeFramework.getByteCodeFrameworkForMethod(testClass, method));
}
}
public static class RemovesTestMethodAnnotation {
private final Class<? extends Annotation> testMethodAnnotation;
private final Class<?> testClass;
private final ByteCodeFramework byteCodeFramework;
private RemovesTestMethodAnnotation(final Class<?> testClass, final Class<? extends Annotation> testMethodAnnotation,
final ByteCodeFramework byteCodeFramework) {
this.testClass = testClass;
this.testMethodAnnotation = testMethodAnnotation;
this.byteCodeFramework = byteCodeFramework;
}
public TestClassTransformer fromMethods(final Collection<Method> testMethodsThatRunOnOtherClassLoaders) {
switch (byteCodeFramework) {
case Javassist:
return new ForMethodsJavaAssistTestClassTransformer(
testClass, testMethodAnnotation, MethodSignatures.Javassist.<CtMethod>methodSignatureWriter(), testMethodsThatRunOnOtherClassLoaders
);
default:
throw new IllegalArgumentException(String.format("Unknown bytecode framework `%s`", byteCodeFramework));
}
}
public TestClassTransformer fromAllMethodsExcept(Method singleMethodToRunOnTargetClassLoader) {
switch (byteCodeFramework) {
case Javassist:
return new FromAllMethodsExceptJavaAssistTestClassTransformer(
testClass, testMethodAnnotation, MethodSignatures.Javassist.<CtMethod>methodSignatureWriter(), singleMethodToRunOnTargetClassLoader
);
default:
throw new IllegalArgumentException(String.format("Unknown bytecode framework `%s`", byteCodeFramework));
}
}
}
}