mirror of
https://github.com/alibaba/testable-mock.git
synced 2025-02-04 00:30:52 +08:00
auto copy testable agent to target folder
This commit is contained in:
parent
d1d34cae1c
commit
c449649679
@ -5,7 +5,7 @@
|
||||
|
||||
<groupId>com.alibaba</groupId>
|
||||
<artifactId>testable-agent</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<version>0.0.2-SNAPSHOT</version>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<dependencies>
|
||||
|
@ -1,39 +0,0 @@
|
||||
package com.alibaba.testable.generator;
|
||||
|
||||
import com.alibaba.testable.model.TestableContext;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.InputStreamReader;
|
||||
|
||||
/**
|
||||
* Generate global n.e class code
|
||||
*
|
||||
* @author flin
|
||||
*/
|
||||
public class StaticNewClassGenerator extends BaseGenerator {
|
||||
|
||||
public StaticNewClassGenerator(TestableContext cx) {
|
||||
super(cx);
|
||||
}
|
||||
|
||||
public String fetch() {
|
||||
InputStream in = getClass().getResourceAsStream("/e.java");
|
||||
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
|
||||
try {
|
||||
String line;
|
||||
StringBuffer buffer = new StringBuffer();
|
||||
while ((line = reader.readLine()) != null)
|
||||
{
|
||||
buffer.append(line).append('\n');
|
||||
}
|
||||
reader.close();
|
||||
return buffer.toString();
|
||||
} catch (IOException e) {
|
||||
cx.logger.error("Failed to generate testable new stand-in.");
|
||||
return "";
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -1,10 +1,10 @@
|
||||
package com.alibaba.testable.processor;
|
||||
|
||||
import com.alibaba.testable.annotation.EnableTestableInject;
|
||||
import com.alibaba.testable.generator.StaticNewClassGenerator;
|
||||
import com.alibaba.testable.translator.EnableTestableInjectTranslator;
|
||||
import com.alibaba.testable.translator.MethodRecordTranslator;
|
||||
import com.alibaba.testable.util.ConstPool;
|
||||
import com.alibaba.testable.util.ResourceUtil;
|
||||
import com.sun.tools.javac.code.Symbol;
|
||||
import com.sun.tools.javac.tree.JCTree;
|
||||
|
||||
@ -17,7 +17,9 @@ import javax.lang.model.element.Element;
|
||||
import javax.lang.model.element.TypeElement;
|
||||
import javax.tools.FileObject;
|
||||
import javax.tools.JavaFileObject;
|
||||
import javax.tools.StandardLocation;
|
||||
import java.io.IOException;
|
||||
import java.io.OutputStream;
|
||||
import java.io.Writer;
|
||||
import java.util.Set;
|
||||
|
||||
@ -48,7 +50,8 @@ public class EnableTestableInjectProcessor extends BaseProcessor {
|
||||
private void createStaticNewClass() {
|
||||
if (!isStaticNewClassExist()) {
|
||||
try {
|
||||
writeSourceFile(ConstPool.NE_PKG_CLS, new StaticNewClassGenerator(cx).fetch());
|
||||
writeSourceFile(ConstPool.NE_PKG_CLS, ResourceUtil.fetchText("e.java"));
|
||||
writeBinaryFile("testable", "agent.jar", ResourceUtil.fetchBinary("testable-agent.jar"));
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
@ -82,7 +85,16 @@ public class EnableTestableInjectProcessor extends BaseProcessor {
|
||||
JavaFileObject jfo = cx.filter.createSourceFile(fullQualityTypeName);
|
||||
Writer writer = jfo.openWriter();
|
||||
writer.write(content);
|
||||
writer.flush();
|
||||
writer.close();
|
||||
}
|
||||
|
||||
private void writeBinaryFile(String path, String fileName, byte[] content) throws IOException {
|
||||
FileObject resource = cx.filter.createResource(StandardLocation.SOURCE_OUTPUT, path, fileName);
|
||||
OutputStream out = resource.openOutputStream();
|
||||
out.write(content);
|
||||
out.flush();
|
||||
out.close();
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -0,0 +1,48 @@
|
||||
package com.alibaba.testable.util;
|
||||
|
||||
import java.io.*;
|
||||
|
||||
/**
|
||||
* Generate global n.e class code
|
||||
*
|
||||
* @author flin
|
||||
*/
|
||||
public class ResourceUtil {
|
||||
|
||||
public static String fetchText(String fileName) {
|
||||
InputStream in = ResourceUtil.class.getResourceAsStream("/" + fileName);
|
||||
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: " + fileName);
|
||||
return "";
|
||||
}
|
||||
}
|
||||
|
||||
public static byte[] fetchBinary(String fileName) {
|
||||
InputStream in = ResourceUtil.class.getResourceAsStream("/" + fileName);
|
||||
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 text file: " + fileName);
|
||||
return new byte[] {};
|
||||
}
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user