mirror of
https://github.com/alibaba/testable-mock.git
synced 2025-01-10 20:30:11 +08:00
rename annotations
This commit is contained in:
parent
70b68425b4
commit
fa79075055
@ -0,0 +1,15 @@
|
||||
package com.alibaba.testable.annotation;
|
||||
|
||||
import java.lang.annotation.*;
|
||||
|
||||
/**
|
||||
* Make test class able to access private field and method in source class
|
||||
*
|
||||
* @author flin
|
||||
*/
|
||||
@Retention(RetentionPolicy.SOURCE)
|
||||
@Target(ElementType.TYPE)
|
||||
@Documented
|
||||
public @interface EnableTestable {
|
||||
|
||||
}
|
@ -0,0 +1,15 @@
|
||||
package com.alibaba.testable.annotation;
|
||||
|
||||
import java.lang.annotation.*;
|
||||
|
||||
/**
|
||||
* Make the method in this class able to be injected by testable substitution methods
|
||||
*
|
||||
* @author flin
|
||||
*/
|
||||
@Retention(RetentionPolicy.SOURCE)
|
||||
@Target(ElementType.TYPE)
|
||||
@Documented
|
||||
public @interface EnableTestableInject {
|
||||
|
||||
}
|
@ -1,17 +0,0 @@
|
||||
package com.alibaba.testable.annotation;
|
||||
|
||||
import java.lang.annotation.*;
|
||||
|
||||
/**
|
||||
* On type, make all methods in the class testable
|
||||
* On method, make the method testable
|
||||
* On field (in test class), fit the variable for unit test
|
||||
*
|
||||
* @author flin
|
||||
*/
|
||||
@Retention(RetentionPolicy.SOURCE)
|
||||
@Target(ElementType.TYPE)
|
||||
@Documented
|
||||
public @interface Testable {
|
||||
|
||||
}
|
@ -3,7 +3,7 @@ package com.alibaba.testable.annotation;
|
||||
import java.lang.annotation.*;
|
||||
|
||||
/**
|
||||
* Use marked variable replace the ones in testable class
|
||||
* Use marked method to replace the ones in source class
|
||||
*
|
||||
* @author flin
|
||||
*/
|
||||
|
@ -1,12 +1,10 @@
|
||||
package com.alibaba.testable.processor;
|
||||
|
||||
import com.alibaba.testable.annotation.Testable;
|
||||
import com.alibaba.testable.annotation.EnableTestableInject;
|
||||
import com.alibaba.testable.generator.StaticNewClassGenerator;
|
||||
import com.alibaba.testable.generator.TestableClassDevRoleGenerator;
|
||||
import com.alibaba.testable.translator.TestableClassTestRoleTranslator;
|
||||
import com.alibaba.testable.util.ConstPool;
|
||||
import com.sun.tools.javac.code.Symbol;
|
||||
import com.sun.tools.javac.tree.JCTree;
|
||||
|
||||
import javax.annotation.processing.FilerException;
|
||||
import javax.annotation.processing.RoundEnvironment;
|
||||
@ -27,24 +25,20 @@ import static javax.tools.StandardLocation.SOURCE_OUTPUT;
|
||||
/**
|
||||
* @author flin
|
||||
*/
|
||||
@SupportedAnnotationTypes("com.alibaba.testable.annotation.Testable")
|
||||
@SupportedAnnotationTypes("com.alibaba.testable.annotation.EnableTestableInject")
|
||||
@SupportedSourceVersion(SourceVersion.RELEASE_7)
|
||||
public class TestableProcessor extends BaseProcessor {
|
||||
public class EnableTestableInjectProcessor extends BaseProcessor {
|
||||
|
||||
private static final String JAVA_POSTFIX = ".java";
|
||||
private static final String GENERATED_TEST_SOURCES = "generated-test-sources";
|
||||
|
||||
@Override
|
||||
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
|
||||
Set<? extends Element> elements = roundEnv.getElementsAnnotatedWith(Testable.class);
|
||||
Set<? extends Element> elements = roundEnv.getElementsAnnotatedWith(EnableTestableInject.class);
|
||||
createStaticNewClass();
|
||||
for (Element element : elements) {
|
||||
if (element.getKind().isClass()) {
|
||||
if (isTestClass(element.getSimpleName())) {
|
||||
processTestRoleClassElement((Symbol.ClassSymbol)element);
|
||||
} else {
|
||||
processDevRoleClassElement((Symbol.ClassSymbol)element);
|
||||
}
|
||||
processClassElement((Symbol.ClassSymbol)element);
|
||||
}
|
||||
}
|
||||
return true;
|
||||
@ -72,15 +66,11 @@ public class TestableProcessor extends BaseProcessor {
|
||||
}
|
||||
}
|
||||
|
||||
private boolean isTestClass(Name name) {
|
||||
return name.toString().endsWith("Test");
|
||||
}
|
||||
|
||||
private boolean isCompilingTestClass(FileObject staticNewClassFile) {
|
||||
return staticNewClassFile.getName().contains(GENERATED_TEST_SOURCES);
|
||||
}
|
||||
|
||||
private void processDevRoleClassElement(Symbol.ClassSymbol clazz) {
|
||||
private void processClassElement(Symbol.ClassSymbol clazz) {
|
||||
String packageName = cx.elementUtils.getPackageOf(clazz).getQualifiedName().toString();
|
||||
String testableTypeName = getTestableClassName(clazz.getSimpleName());
|
||||
String fullQualityTypeName = packageName + "." + testableTypeName;
|
||||
@ -92,20 +82,6 @@ public class TestableProcessor extends BaseProcessor {
|
||||
}
|
||||
}
|
||||
|
||||
private void processTestRoleClassElement(Symbol.ClassSymbol clazz) {
|
||||
JCTree tree = cx.trees.getTree(clazz);
|
||||
tree.accept(new TestableClassTestRoleTranslator(getPkgName(clazz), getOriginClassName(clazz), cx));
|
||||
}
|
||||
|
||||
private String getPkgName(Symbol.ClassSymbol clazz) {
|
||||
return ((Symbol.PackageSymbol)clazz.owner).fullname.toString();
|
||||
}
|
||||
|
||||
private String getOriginClassName(Symbol.ClassSymbol clazz) {
|
||||
String testClassName = clazz.getSimpleName().toString();
|
||||
return testClassName.substring(0, testClassName.length() - "Test".length());
|
||||
}
|
||||
|
||||
private void writeSourceFile(String fullQualityTypeName, String content) throws IOException {
|
||||
JavaFileObject jfo = cx.filter.createSourceFile(fullQualityTypeName);
|
||||
Writer writer = jfo.openWriter();
|
@ -0,0 +1,53 @@
|
||||
package com.alibaba.testable.processor;
|
||||
|
||||
import com.alibaba.testable.annotation.EnableTestable;
|
||||
import com.alibaba.testable.translator.TestableClassTestRoleTranslator;
|
||||
import com.sun.tools.javac.code.Symbol;
|
||||
import com.sun.tools.javac.tree.JCTree;
|
||||
|
||||
import javax.annotation.processing.RoundEnvironment;
|
||||
import javax.annotation.processing.SupportedAnnotationTypes;
|
||||
import javax.annotation.processing.SupportedSourceVersion;
|
||||
import javax.lang.model.SourceVersion;
|
||||
import javax.lang.model.element.Element;
|
||||
import javax.lang.model.element.Name;
|
||||
import javax.lang.model.element.TypeElement;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* @author flin
|
||||
*/
|
||||
@SupportedAnnotationTypes("com.alibaba.testable.annotation.EnableTestable")
|
||||
@SupportedSourceVersion(SourceVersion.RELEASE_7)
|
||||
public class EnableTestableProcessor extends BaseProcessor {
|
||||
|
||||
@Override
|
||||
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
|
||||
Set<? extends Element> elements = roundEnv.getElementsAnnotatedWith(EnableTestable.class);
|
||||
for (Element element : elements) {
|
||||
if (element.getKind().isClass() && isTestClass(element.getSimpleName())) {
|
||||
processClassElement((Symbol.ClassSymbol)element);
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
private boolean isTestClass(Name name) {
|
||||
return name.toString().endsWith("Test");
|
||||
}
|
||||
|
||||
private void processClassElement(Symbol.ClassSymbol clazz) {
|
||||
JCTree tree = cx.trees.getTree(clazz);
|
||||
tree.accept(new TestableClassTestRoleTranslator(getPkgName(clazz), getOriginClassName(clazz), cx));
|
||||
}
|
||||
|
||||
private String getPkgName(Symbol.ClassSymbol clazz) {
|
||||
return ((Symbol.PackageSymbol)clazz.owner).fullname.toString();
|
||||
}
|
||||
|
||||
private String getOriginClassName(Symbol.ClassSymbol clazz) {
|
||||
String testClassName = clazz.getSimpleName().toString();
|
||||
return testClassName.substring(0, testClassName.length() - "Test".length());
|
||||
}
|
||||
|
||||
}
|
@ -1 +1,2 @@
|
||||
com.alibaba.testable.processor.TestableProcessor
|
||||
com.alibaba.testable.processor.EnableTestableProcessor
|
||||
com.alibaba.testable.processor.EnableTestableInjectProcessor
|
||||
|
Loading…
Reference in New Issue
Block a user