remove agent from core jar, bump to 0.2.0-snapshot

This commit is contained in:
金戟 2020-10-22 20:57:02 +08:00
parent 33a16f34af
commit b6070ad24d
9 changed files with 18 additions and 147 deletions

View File

@ -16,7 +16,7 @@
<properties>
<java.version>1.8</java.version>
<testable.version>0.1.1-SNAPSHOT</testable.version>
<testable.version>0.2.0-SNAPSHOT</testable.version>
</properties>
<dependencies>

View File

@ -17,7 +17,7 @@
<properties>
<java.version>1.8</java.version>
<kotlin.version>1.3.72</kotlin.version>
<testable.version>0.1.1-SNAPSHOT</testable.version>
<testable.version>0.2.0-SNAPSHOT</testable.version>
</properties>
<dependencies>

View File

@ -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

View File

@ -5,7 +5,7 @@
<groupId>com.alibaba.testable</groupId>
<artifactId>testable-agent</artifactId>
<version>0.1.1-SNAPSHOT</version>
<version>0.2.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>testable-agent</name>

View File

@ -7,7 +7,7 @@
<description>Unit test enhancement toolkit</description>
<groupId>com.alibaba.testable</groupId>
<artifactId>testable-core</artifactId>
<version>0.1.1-SNAPSHOT</version>
<version>0.2.0-SNAPSHOT</version>
<name>testable-core</name>
<properties>
@ -46,35 +46,6 @@
<compilerArgument>-proc:none</compilerArgument>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>${plugin.resources.version}</version>
<configuration>
<encoding>${project.build.sourceEncoding}</encoding>
</configuration>
<executions>
<execution>
<id>copy-testable-agent-resources</id>
<phase>prepare-package</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<encoding>utf-8</encoding>
<outputDirectory>${basedir}/target/classes</outputDirectory>
<resources>
<resource>
<directory>${basedir}/../testable-agent/target</directory>
<includes>
<include>testable-agent.jar</include>
</includes>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>

View File

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

View File

@ -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[] {};
}
}
}

View File

@ -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
);
}
}

View File

@ -4,10 +4,15 @@
<groupId>com.alibaba.testable</groupId>
<artifactId>testable-maven-plugin</artifactId>
<packaging>maven-plugin</packaging>
<version>0.1.1-SNAPSHOT</version>
<version>${testable.version}</version>
<name>testable-maven-plugin</name>
<url>http://maven.apache.org</url>
<properties>
<java.version>1.8</java.version>
<testable.version>0.2.0-SNAPSHOT</testable.version>
</properties>
<dependencies>
<dependency>
<groupId>org.apache.maven</groupId>
@ -28,7 +33,7 @@
<dependency>
<groupId>com.alibaba.testable</groupId>
<artifactId>testable-agent</artifactId>
<version>0.1.1-SNAPSHOT</version>
<version>${testable.version}</version>
</dependency>
</dependencies>