mirror of
https://github.com/alibaba/testable-mock.git
synced 2025-01-10 12:20:09 +08:00
refactor log methods
This commit is contained in:
parent
08aa73b4bd
commit
ad63de628d
@ -1,7 +1,8 @@
|
||||
package com.alibaba.testable.agent;
|
||||
|
||||
import com.alibaba.testable.agent.transformer.TestableClassTransformer;
|
||||
import com.alibaba.testable.agent.util.LogUtil;
|
||||
import com.alibaba.testable.core.util.LogUtil;
|
||||
import com.alibaba.testable.core.model.MockDiagnose;
|
||||
|
||||
import java.lang.instrument.Instrumentation;
|
||||
|
||||
@ -13,6 +14,7 @@ public class PreMain {
|
||||
|
||||
private static final String AND = "&";
|
||||
private static final String DEBUG = "debug";
|
||||
private static final String VERBOSE = "verbose";
|
||||
|
||||
public static void premain(String agentArgs, Instrumentation inst) {
|
||||
parseArgs(agentArgs);
|
||||
@ -25,7 +27,9 @@ public class PreMain {
|
||||
}
|
||||
for (String a : args.split(AND)) {
|
||||
if (a.equals(DEBUG)) {
|
||||
LogUtil.globalDebugEnable = true;
|
||||
LogUtil.setDefaultLevel(LogUtil.LEVEL_DIAGNOSE);
|
||||
} else if (a.equals(VERBOSE)) {
|
||||
LogUtil.setDefaultLevel(LogUtil.LEVEL_VERBOSE);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -4,7 +4,7 @@ import com.alibaba.testable.agent.constant.ConstPool;
|
||||
import com.alibaba.testable.agent.model.MethodInfo;
|
||||
import com.alibaba.testable.agent.util.BytecodeUtil;
|
||||
import com.alibaba.testable.agent.util.ClassUtil;
|
||||
import com.alibaba.testable.agent.util.LogUtil;
|
||||
import com.alibaba.testable.core.util.LogUtil;
|
||||
import org.objectweb.asm.Opcodes;
|
||||
import org.objectweb.asm.tree.*;
|
||||
|
||||
@ -52,7 +52,7 @@ public class SourceClassHandler extends BaseClassHandler {
|
||||
|
||||
private void transformMethod(ClassNode cn, MethodNode mn, Set<MethodInfo> memberInjectMethods,
|
||||
Set<MethodInfo> newOperatorInjectMethods) {
|
||||
LogUtil.debug(" Handling method %s", mn.name);
|
||||
LogUtil.diagnose(" Handling method %s", mn.name);
|
||||
AbstractInsnNode[] instructions = mn.instructions.toArray();
|
||||
List<MethodInfo> memberInjectMethodList = new ArrayList<MethodInfo>(memberInjectMethods);
|
||||
int i = 0;
|
||||
@ -155,7 +155,7 @@ public class SourceClassHandler extends BaseClassHandler {
|
||||
|
||||
private AbstractInsnNode[] replaceNewOps(ClassNode cn, MethodNode mn, String newOperatorInjectMethodName,
|
||||
AbstractInsnNode[] instructions, int start, int end) {
|
||||
LogUtil.debug(" Line %d, mock method %s used", getLineNum(instructions, start),
|
||||
LogUtil.diagnose(" Line %d, mock method %s used", getLineNum(instructions, start),
|
||||
newOperatorInjectMethodName);
|
||||
String classType = ((TypeInsnNode)instructions[start]).desc;
|
||||
String constructorDesc = ((MethodInsnNode)instructions[end]).desc;
|
||||
@ -187,7 +187,7 @@ public class SourceClassHandler extends BaseClassHandler {
|
||||
private AbstractInsnNode[] replaceMemberCallOps(ClassNode cn, MethodNode mn, String substitutionMethod,
|
||||
AbstractInsnNode[] instructions, String ownerClass,
|
||||
int opcode, int start, int end) {
|
||||
LogUtil.debug(" Line %d, mock method %s used", getLineNum(instructions, start), substitutionMethod);
|
||||
LogUtil.diagnose(" Line %d, mock method %s used", getLineNum(instructions, start), substitutionMethod);
|
||||
mn.maxStack++;
|
||||
MethodInsnNode method = (MethodInsnNode)instructions[end];
|
||||
String testClassName = ClassUtil.getTestClassName(cn.name);
|
||||
|
@ -9,7 +9,7 @@ import com.alibaba.testable.agent.model.MethodInfo;
|
||||
import com.alibaba.testable.agent.tool.ComparableWeakRef;
|
||||
import com.alibaba.testable.agent.util.AnnotationUtil;
|
||||
import com.alibaba.testable.agent.util.ClassUtil;
|
||||
import com.alibaba.testable.agent.util.LogUtil;
|
||||
import com.alibaba.testable.core.util.LogUtil;
|
||||
import com.alibaba.testable.core.model.MockDiagnose;
|
||||
import org.objectweb.asm.ClassReader;
|
||||
import org.objectweb.asm.tree.AnnotationNode;
|
||||
@ -43,15 +43,15 @@ public class TestableClassTransformer implements ClassFileTransformer {
|
||||
try {
|
||||
if (shouldTransformAsSourceClass(className)) {
|
||||
// it's a source class with testable enabled
|
||||
LogUtil.debug("Handling source class %s", className);
|
||||
LogUtil.diagnose("Handling source class %s", className);
|
||||
List<MethodInfo> injectMethods = getTestableMockMethods(ClassUtil.getTestClassName(className));
|
||||
return new SourceClassHandler(injectMethods).getBytes(classFileBuffer);
|
||||
} else if (shouldTransformAsTestClass(className)) {
|
||||
// it's a test class with testable enabled
|
||||
LogUtil.debug("Handling test class %s", className);
|
||||
LogUtil.diagnose("Handling test class %s", className);
|
||||
return new TestClassHandler().getBytes(classFileBuffer);
|
||||
}
|
||||
resetMockContext();
|
||||
LogUtil.resetLogLevel();
|
||||
} catch (IOException e) {
|
||||
return null;
|
||||
}
|
||||
@ -79,7 +79,7 @@ public class TestableClassTransformer implements ClassFileTransformer {
|
||||
for (MethodNode mn : cn.methods) {
|
||||
checkMethodAnnotation(cn, methodInfos, mn);
|
||||
}
|
||||
LogUtil.debug(" Found %d mock methods", methodInfos.size());
|
||||
LogUtil.diagnose(" Found %d mock methods", methodInfos.size());
|
||||
return methodInfos;
|
||||
} catch (Exception e) {
|
||||
return new ArrayList<MethodInfo>();
|
||||
@ -149,18 +149,12 @@ public class TestableClassTransformer implements ClassFileTransformer {
|
||||
}
|
||||
|
||||
private void setupMockContext(AnnotationNode an) {
|
||||
MockDiagnose mockDebug = AnnotationUtil.getAnnotationParameter(an, FIELD_DIAGNOSE, null, MockDiagnose.class);
|
||||
if (MockDiagnose.ENABLE.equals(mockDebug)) {
|
||||
LogUtil.enableDebugLog();
|
||||
} else if (MockDiagnose.DISABLE.equals(mockDebug)) {
|
||||
LogUtil.disableDebugLog();
|
||||
MockDiagnose diagnose = AnnotationUtil.getAnnotationParameter(an, FIELD_DIAGNOSE, null, MockDiagnose.class);
|
||||
if (diagnose != null) {
|
||||
LogUtil.enableDiagnose(diagnose == MockDiagnose.ENABLE);
|
||||
}
|
||||
}
|
||||
|
||||
private void resetMockContext() {
|
||||
LogUtil.resetDebugLog();
|
||||
}
|
||||
|
||||
/**
|
||||
* Split desc to "first parameter" and "desc of rest parameters"
|
||||
* @param desc method desc
|
||||
|
@ -1,33 +0,0 @@
|
||||
package com.alibaba.testable.agent.util;
|
||||
|
||||
/**
|
||||
* @author flin
|
||||
*/
|
||||
public class LogUtil {
|
||||
|
||||
private static final int LEVEL_ERROR = 0;
|
||||
private static final int LEVEL_WARN = 1;
|
||||
private static final int LEVEL_DIAGNOSE = 2;
|
||||
private static int level;
|
||||
|
||||
public static boolean globalDebugEnable = false;
|
||||
|
||||
public static void debug(String msg, Object... args) {
|
||||
if (level >= LEVEL_DIAGNOSE) {
|
||||
System.out.println(String.format("[DIAGNOSE] " + msg, args));
|
||||
}
|
||||
}
|
||||
|
||||
public static void enableDebugLog() {
|
||||
level = LEVEL_DIAGNOSE;
|
||||
}
|
||||
|
||||
public static void disableDebugLog() {
|
||||
level = LEVEL_ERROR;
|
||||
}
|
||||
|
||||
public static void resetDebugLog() {
|
||||
level = globalDebugEnable ? LEVEL_DIAGNOSE : LEVEL_WARN;
|
||||
}
|
||||
|
||||
}
|
@ -18,6 +18,6 @@ public @interface MockWith {
|
||||
* switch of mock diagnose information of current test class
|
||||
* @return enable or disable
|
||||
*/
|
||||
MockDiagnose diagnose() default MockDiagnose.WARN_ONLY;
|
||||
MockDiagnose diagnose() default MockDiagnose.DISABLE;
|
||||
|
||||
}
|
||||
|
@ -12,12 +12,7 @@ public enum MockDiagnose {
|
||||
DISABLE,
|
||||
|
||||
/**
|
||||
* Only show warning message
|
||||
*/
|
||||
WARN_ONLY,
|
||||
|
||||
/**
|
||||
* Print detail diagnose logs
|
||||
* Print diagnose logs
|
||||
*/
|
||||
ENABLE
|
||||
|
||||
|
@ -35,8 +35,10 @@ public class InvokeRecordUtil {
|
||||
List<Object[]> records = getInvokeRecord(mockMethodName, testCaseName);
|
||||
if (isConstructor) {
|
||||
records.add(args);
|
||||
LogUtil.verbose("Mock constructor invoked \"%s\"", key);
|
||||
} else {
|
||||
records.add(slice(args, 1));
|
||||
LogUtil.verbose("Mock method invoked \"%s\"", key);
|
||||
}
|
||||
INVOKE_RECORDS.put(key, records);
|
||||
}
|
||||
|
@ -0,0 +1,46 @@
|
||||
package com.alibaba.testable.core.util;
|
||||
|
||||
/**
|
||||
* @author flin
|
||||
*/
|
||||
public class LogUtil {
|
||||
|
||||
public static final int LEVEL_ERROR = 0;
|
||||
public static final int LEVEL_WARN = 1;
|
||||
public static final int LEVEL_DIAGNOSE = 2;
|
||||
public static final int LEVEL_VERBOSE = 3;
|
||||
|
||||
private static int defaultLogLevel = LEVEL_WARN;
|
||||
private static int level;
|
||||
|
||||
public static void verbose(String msg, Object... args) {
|
||||
if (level >= LEVEL_VERBOSE) {
|
||||
System.out.println(String.format("[VERBOSE] " + msg, args));
|
||||
}
|
||||
}
|
||||
|
||||
public static void diagnose(String msg, Object... args) {
|
||||
if (level >= LEVEL_DIAGNOSE) {
|
||||
System.out.println(String.format("[DIAGNOSE] " + msg, args));
|
||||
}
|
||||
}
|
||||
|
||||
public static void warn(String msg, Object... args) {
|
||||
if (level >= LEVEL_WARN) {
|
||||
System.out.println(String.format("[WARN] " + msg, args));
|
||||
}
|
||||
}
|
||||
|
||||
public static void enableDiagnose(boolean enable) {
|
||||
level = enable ? LEVEL_DIAGNOSE : LEVEL_ERROR;
|
||||
}
|
||||
|
||||
public static void setDefaultLevel(int level) {
|
||||
defaultLogLevel = level;
|
||||
}
|
||||
|
||||
public static void resetLogLevel() {
|
||||
level = defaultLogLevel;
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user