From b6070ad24d6d0ca113528b413694717ebb1db0ec Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=87=91=E6=88=9F?= Date: Thu, 22 Oct 2020 20:57:02 +0800 Subject: [PATCH] remove agent from core jar, bump to 0.2.0-snapshot --- demo/java-demo/pom.xml | 2 +- demo/kotlin-demo/pom.xml | 2 +- docs/ReleaseNote.md | 5 +- testable-agent/pom.xml | 2 +- testable-core/pom.xml | 31 +--------- .../processor/EnableTestableProcessor.java | 34 ++--------- .../testable/core/util/ResourceUtil.java | 57 ------------------- .../testable/core/util/ResourceUtilTest.java | 23 -------- testable-maven-plugin/pom.xml | 9 ++- 9 files changed, 18 insertions(+), 147 deletions(-) delete mode 100644 testable-core/src/main/java/com/alibaba/testable/core/util/ResourceUtil.java delete mode 100644 testable-core/src/test/java/com/alibaba/testable/core/util/ResourceUtilTest.java diff --git a/demo/java-demo/pom.xml b/demo/java-demo/pom.xml index 3f5192f..6bd7010 100644 --- a/demo/java-demo/pom.xml +++ b/demo/java-demo/pom.xml @@ -16,7 +16,7 @@ 1.8 - 0.1.1-SNAPSHOT + 0.2.0-SNAPSHOT diff --git a/demo/kotlin-demo/pom.xml b/demo/kotlin-demo/pom.xml index 3ae9aa9..7eee262 100644 --- a/demo/kotlin-demo/pom.xml +++ b/demo/kotlin-demo/pom.xml @@ -17,7 +17,7 @@ 1.8 1.3.72 - 0.1.1-SNAPSHOT + 0.2.0-SNAPSHOT diff --git a/docs/ReleaseNote.md b/docs/ReleaseNote.md index 5408852..9346f57 100644 --- a/docs/ReleaseNote.md +++ b/docs/ReleaseNote.md @@ -1,11 +1,12 @@ # Release Note -## v0.1.1 +## v0.2.0 - use `TestableTool` class to expose test context +- add `testable-maven-plugin` module ## v0.1.0 - move generated agent jar to class folder -- now support mock method of any object +- support mock method of any object ## v0.0.5 - use dynamically runtime modification to replace static `e.java` file diff --git a/testable-agent/pom.xml b/testable-agent/pom.xml index a90ac79..09d9e85 100755 --- a/testable-agent/pom.xml +++ b/testable-agent/pom.xml @@ -5,7 +5,7 @@ com.alibaba.testable testable-agent - 0.1.1-SNAPSHOT + 0.2.0-SNAPSHOT jar testable-agent diff --git a/testable-core/pom.xml b/testable-core/pom.xml index f4da04c..cc4faa0 100644 --- a/testable-core/pom.xml +++ b/testable-core/pom.xml @@ -7,7 +7,7 @@ Unit test enhancement toolkit com.alibaba.testable testable-core - 0.1.1-SNAPSHOT + 0.2.0-SNAPSHOT testable-core @@ -46,35 +46,6 @@ -proc:none - - org.apache.maven.plugins - maven-resources-plugin - ${plugin.resources.version} - - ${project.build.sourceEncoding} - - - - copy-testable-agent-resources - prepare-package - - copy-resources - - - utf-8 - ${basedir}/target/classes - - - ${basedir}/../testable-agent/target - - testable-agent.jar - - - - - - - diff --git a/testable-core/src/main/java/com/alibaba/testable/core/processor/EnableTestableProcessor.java b/testable-core/src/main/java/com/alibaba/testable/core/processor/EnableTestableProcessor.java index 0361149..47ae9b6 100644 --- a/testable-core/src/main/java/com/alibaba/testable/core/processor/EnableTestableProcessor.java +++ b/testable-core/src/main/java/com/alibaba/testable/core/processor/EnableTestableProcessor.java @@ -4,7 +4,6 @@ import com.alibaba.testable.core.annotation.EnableTestable; import com.alibaba.testable.core.constant.ConstPool; import com.alibaba.testable.core.model.TestableContext; import com.alibaba.testable.core.translator.EnableTestableTranslator; -import com.alibaba.testable.core.util.ResourceUtil; import com.alibaba.testable.core.util.TestableLogger; import com.sun.tools.javac.api.JavacTrees; import com.sun.tools.javac.code.Symbol; @@ -14,15 +13,14 @@ import com.sun.tools.javac.tree.TreeMaker; import com.sun.tools.javac.util.Context; import com.sun.tools.javac.util.Names; -import javax.annotation.processing.*; +import javax.annotation.processing.AbstractProcessor; +import javax.annotation.processing.ProcessingEnvironment; +import javax.annotation.processing.RoundEnvironment; +import javax.annotation.processing.SupportedAnnotationTypes; import javax.lang.model.SourceVersion; import javax.lang.model.element.Element; import javax.lang.model.element.Name; import javax.lang.model.element.TypeElement; -import javax.tools.FileObject; -import javax.tools.StandardLocation; -import java.io.IOException; -import java.io.OutputStream; import java.util.Set; /** @@ -31,8 +29,6 @@ import java.util.Set; @SupportedAnnotationTypes("com.alibaba.testable.core.annotation.EnableTestable") public class EnableTestableProcessor extends AbstractProcessor { - private static final String TESTABLE_AGENT_JAR = "testable-agent.jar"; - private static final String TEST_OUTPUT_FOLDER_MARK = "/test-classes/"; private TestableContext cx; @Override @@ -47,7 +43,6 @@ public class EnableTestableProcessor extends AbstractProcessor { processingEnv.getElementUtils(), processingEnv.getTypeUtils(), JavacTrees.instance(processingEnv), TreeMaker.instance(context), Names.instance(context)); } - createTestableAgentJar(); cx.logger.info("Testable processor initialized"); } @@ -89,25 +84,4 @@ public class EnableTestableProcessor extends AbstractProcessor { tree.accept(new EnableTestableTranslator(pkgName, clazz.getSimpleName().toString(), cx)); } - private void createTestableAgentJar() { - byte[] bytes = ResourceUtil.fetchBinary(TESTABLE_AGENT_JAR); - if (bytes.length == 0) { - cx.logger.info("Failed to fetch testable agent jar"); - } - try { - FileObject resource = cx.filter.createResource(StandardLocation.CLASS_OUTPUT, "", TESTABLE_AGENT_JAR); - if (!resource.getName().contains(TEST_OUTPUT_FOLDER_MARK)) { - cx.logger.info("Skip generate testable agent jar"); - return; - } - cx.logger.info("Generating " + resource.getName()); - try (OutputStream out = resource.openOutputStream()) { - out.write(bytes); - out.flush(); - } - } catch (IOException e) { - e.printStackTrace(); - cx.logger.error("Failed to generate testable agent jar"); - } - } } diff --git a/testable-core/src/main/java/com/alibaba/testable/core/util/ResourceUtil.java b/testable-core/src/main/java/com/alibaba/testable/core/util/ResourceUtil.java deleted file mode 100644 index 9e105de..0000000 --- a/testable-core/src/main/java/com/alibaba/testable/core/util/ResourceUtil.java +++ /dev/null @@ -1,57 +0,0 @@ -package com.alibaba.testable.core.util; - -import java.io.*; - -/** - * @author flin - */ -public class ResourceUtil { - - /** - * Read content of a text file from resource folder - * @param filePath file to read - */ - public static String fetchText(String filePath) { - InputStream in = ResourceUtil.class.getResourceAsStream("/" + filePath); - BufferedReader reader = new BufferedReader(new InputStreamReader(in)); - StringBuilder buffer = new StringBuilder(); - String line; - try { - while ((line = reader.readLine()) != null) { - buffer.append(line).append('\n'); - } - reader.close(); - return buffer.toString(); - } catch (IOException e) { - System.err.println("Failed to fetch text file: " + filePath); - return ""; - } - } - - /** - * Read content of a binary file from resource folder - * @param filePath file to read - */ - public static byte[] fetchBinary(String filePath) { - InputStream in = ResourceUtil.class.getResourceAsStream("/" + filePath); - if (in == null) { - System.err.println("Resource " + filePath + " not exist"); - return new byte[] {}; - } - ByteArrayOutputStream buffer = new ByteArrayOutputStream(); - final int bufLen = 4 * 1024; - byte[] buf = new byte[bufLen]; - int readLen; - try { - while ((readLen = in.read(buf, 0, bufLen)) != -1) { - buffer.write(buf, 0, readLen); - } - buffer.close(); - return buffer.toByteArray(); - } catch (IOException e) { - System.err.println("Failed to fetch file: " + filePath); - return new byte[] {}; - } - } - -} diff --git a/testable-core/src/test/java/com/alibaba/testable/core/util/ResourceUtilTest.java b/testable-core/src/test/java/com/alibaba/testable/core/util/ResourceUtilTest.java deleted file mode 100644 index 48d30a3..0000000 --- a/testable-core/src/test/java/com/alibaba/testable/core/util/ResourceUtilTest.java +++ /dev/null @@ -1,23 +0,0 @@ -package com.alibaba.testable.core.util; - -import org.junit.jupiter.api.Test; - -import static org.junit.jupiter.api.Assertions.*; - -class ResourceUtilTest { - - @Test - void fetchText() { - assertTrue( - ResourceUtil.fetchText("META-INF/services/javax.annotation.processing.Processor").startsWith("com.") - ); - } - - @Test - void should_able_to_fetch_binary() { - assertTrue( - ResourceUtil.fetchBinary("com/alibaba/testable/core/util/ResourceUtil.class").length > 0 - ); - } - -} diff --git a/testable-maven-plugin/pom.xml b/testable-maven-plugin/pom.xml index 5129979..619997f 100644 --- a/testable-maven-plugin/pom.xml +++ b/testable-maven-plugin/pom.xml @@ -4,10 +4,15 @@ com.alibaba.testable testable-maven-plugin maven-plugin - 0.1.1-SNAPSHOT + ${testable.version} testable-maven-plugin http://maven.apache.org + + 1.8 + 0.2.0-SNAPSHOT + + org.apache.maven @@ -28,7 +33,7 @@ com.alibaba.testable testable-agent - 0.1.1-SNAPSHOT + ${testable.version}