Skip to content

Commit

Permalink
skip some test method when use jacoco
Browse files Browse the repository at this point in the history
  • Loading branch information
leaderli committed Mar 3, 2024
1 parent e8fc20e commit e704054
Show file tree
Hide file tree
Showing 8 changed files with 91 additions and 43 deletions.
19 changes: 19 additions & 0 deletions .run/commit_jacoco_test.run.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<component name="ProjectRunConfigurationManager">
<configuration default="false" name="commit_jacoco_test" type="JUnit" factoryName="JUnit" folderName="git"
singleton="false">
<extension name="coverage" sample_coverage="false" track_test_folders="true" runner="jacoco"/>
<option name="ALTERNATIVE_JRE_PATH" value="1.8"/>
<option name="PACKAGE_NAME" value=""/>
<option name="MAIN_CLASS_NAME" value=""/>
<option name="METHOD_NAME" value=""/>
<option name="TEST_OBJECT" value="package"/>
<option name="VM_PARAMETERS"
value="-ea -javaagent:$MAVEN_REPOSITORY$/org/jacoco/org.jacoco.agent/0.8.8/org.jacoco.agent-0.8.8-runtime.jar=destfile=$PROJECT_DIR$/litool-test/target/coverage-reports/jacoco.exec "/>
<option name="TEST_SEARCH_SCOPE">
<value defaultName="wholeProject"/>
</option>
<method v="2">
<option name="Make" enabled="true"/>
</method>
</configuration>
</component>
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,7 @@ public AbstractMocker(Class<?> mockClass, boolean detach) {
}
currentMethod = method;
currentArgs = ArrayEqual.of(args);
System.out.println(currentMethod + " currentMethod " + currentArgs);
methodValueMap.put(currentMethod, new MethodValue(currentMethod));
System.out.println(methodValueMap);
return PrimitiveEnum.get(method.getReturnType()).zero_value;
}, false);
}
Expand Down
33 changes: 11 additions & 22 deletions litool-test/src/main/java/io/leaderli/litool/test/LiMock.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,30 +36,28 @@ public class LiMock {
public static final ByteBuddy byteBuddy = new ByteBuddy();
public static Instrumentation instrumentation = ByteBuddyAgent.install();
public static ClassPool classPool = ClassPool.getDefault();
private static boolean jacoco;
public final static boolean jacoco;

static {
checkJacoco();
System.out.println("jacoco:" + jacoco);
jacoco = checkJacoco();
classPool.importPackage("io.leaderli.litool.test.MockMethodInvoker");
classPool.importPackage("io.leaderli.litool.core.meta.Either");
}

private static void checkJacoco() {
private static boolean checkJacoco() {
for (Class<?> loadedClass : instrumentation.getAllLoadedClasses()) {
if (ClassFileTransformer.class.isAssignableFrom(loadedClass)) {
if (loadedClass.getName().startsWith("org.jacoco.agent.rt")) {
jacoco = true;
return;
return true;
}
}
}
return false;
}

public static CtClass getCtClass(Class<?> clazz, boolean retransformClasses) {
public static CtClass getCtClass(Class<?> clazz) {
try {

if (jacoco && retransformClasses && instrumentation.isModifiableClass(clazz)) {
if (jacoco && instrumentation.isModifiableClass(clazz)) {
instrumentation.addTransformer(new TempClassFileTransformer(instrumentation), true);
instrumentation.retransformClasses(clazz);
}
Expand All @@ -68,17 +66,12 @@ public static CtClass getCtClass(Class<?> clazz, boolean retransformClasses) {
} catch (Exception e) {
throw new RuntimeException(clazz + " : " + e);
}

}

public static CtClass getCtClass(Class<?> clazz) {
return getCtClass(clazz, true);
}


private static void backup(Class<?> clazz) throws IOException, CannotCompileException {
if (!originClasses.containsKey(clazz)) {
originClasses.put(clazz, toBytecode(getCtClass(clazz, true)));
originClasses.put(clazz, toBytecode(getCtClass(clazz)));
}
}

Expand All @@ -91,8 +84,9 @@ private static byte[] toBytecode(CtClass ct) throws IOException, CannotCompileEx
public static void detach(Class<?> clazz) throws UnmodifiableClassException, ClassNotFoundException {


getCtClass(clazz, false).detach();
// instrumentation.retransformClasses(clazz);
getCtClass(clazz).detach();
instrumentation.retransformClasses(clazz);

if (originClasses.containsKey(clazz)) {
instrumentation.redefineClasses(new ClassDefinition(clazz, originClasses.get(clazz)));
}
Expand All @@ -109,9 +103,7 @@ static class TempClassFileTransformer implements ClassFileTransformer {
@Override
public byte[] transform(ClassLoader loader, String className, Class<?> classBeingRedefined, ProtectionDomain protectionDomain, byte[] classfileBuffer) {
try {
System.out.println("transform:" + className);
CtClass ctClass = classPool.makeClass(new ByteArrayInputStream(classfileBuffer));
System.out.println("after transform:" + ctClass);
} catch (IOException ignore) {
}
instrumentation.removeTransformer(this);
Expand Down Expand Up @@ -216,10 +208,8 @@ public static void mock(Class<?> mockClass, Function<Method, Boolean> mockMethod
}
CtClass ct = getCtClass(mockClass);

Method method1 = null;
for (Method method : findDeclaredMethods(mockClass, mockMethodFilter)) {

method1 = method;
CtMethod ctMethod = getCtMethod(method, ct);
String uuid = method.getName() + " " + UUID.randomUUID();
MockMethodInvoker.invokers.put(uuid, LiTuple.of(methodProxy, method));
Expand All @@ -234,7 +224,6 @@ public static void mock(Class<?> mockClass, Function<Method, Boolean> mockMethod
ctMethod.insertBefore(src);
}
instrumentation.redefineClasses(new ClassDefinition(mockClass, toBytecode(ct)));

} catch (Exception e) {
throw new RuntimeException(e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ public class MockBean<T> extends AbstractMocker<MockBean<T>> implements MockBean
MockBean(Class<T> mockClass, boolean detach) {
super(mockClass, detach);
instance = BeanCreator.mockBean(mockClass);
System.out.println(mockClass + " " + instance);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ public class MockMethodInvoker {
public static Either<?, ?> invoke(String uuid, Class<?> clazz, String name, Class<?>[] argsType, Object[] args) {

LiTuple<MethodProxy, Method> tuple = invokers.get(uuid);
System.out.println("tuple:" + tuple);
if (tuple == null) {
return Either.none();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package io.leaderli.litool.test;

import org.junit.jupiter.api.extension.ConditionEvaluationResult;
import org.junit.jupiter.api.extension.ExecutionCondition;
import org.junit.jupiter.api.extension.ExtensionContext;

public class SkipWhenJacocoExecutionCondition implements ExecutionCondition {
@Override
public ConditionEvaluationResult evaluateExecutionCondition(ExtensionContext context) {
if (LiMock.jacoco) {
return ConditionEvaluationResult.disabled("skip " + context.getRequiredTestMethod().getName() + " when use jacoco");
}
return ConditionEvaluationResult.enabled("");
}
}
47 changes: 30 additions & 17 deletions litool-test/src/test/java/io/leaderli/litool/test/LiMockTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import io.leaderli.litool.core.type.ModifierUtil;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.opentest4j.AssertionFailedError;

import java.lang.instrument.UnmodifiableClassException;
Expand Down Expand Up @@ -224,34 +225,34 @@ void testWhen3() throws UnmodifiableClassException, ClassNotFoundException {
Assertions.assertEquals(2, Void2.a);
}

@LiTest
void testWhenBean() {

LiMock.getCtClass(Error.class);
// Bean1 foo = new Bean1();
// Assertions.assertEquals(1, foo.m1());
// LiMock.mockerBean(Bean1.class).when(Bean1::m1, 2).build();
// Assertions.assertEquals(2, foo.m1());
System.out.println("-------------------------------------------------------------------------");
// System.out.println(Supplier1.class);
Supplier1 supplier1 = LiMock.mockerBean(Supplier1.class).when(instance -> {
System.out.println("00000000000000000000000000000000000000");
return instance.get();
}, 2).build();
@LiTest
void testWhenBean2() {

// Assertions.assertEquals(2, supplier1.get());
// System.out.println("-------------------------------------------------------------------------");
Bean1 foo = new Bean1();
Assertions.assertEquals(1, foo.m1());
LiMock.mockerBean(Bean1.class).when(Bean1::m1, 2).build();
Assertions.assertEquals(2, foo.m1());
}


@LiTest
void testWhenBean3() {

Supplier<Integer> supplier = LiMock.mockerBean(Supplier1.class).other(Supplier::get, (m, a) -> 100).build();
Assertions.assertEquals(100, supplier.get());
Assertions.assertEquals(100, new Supplier1().get());

}

@Test
@ExtendWith(SkipWhenJacocoExecutionCondition.class)
void testWhenBeans() {

Supplier<Integer> supplier = LiMock.mockerBeans(Supplier1.class, Supplier2.class).other(Supplier::get, (m, a) -> 100).build();
Assertions.assertEquals(100, supplier.get());
Assertions.assertEquals(100, new Supplier1().get());
Assertions.assertEquals(100, new Supplier2().get());

//
}

@SuppressWarnings({"rawtypes", "unchecked"})
Expand Down Expand Up @@ -549,6 +550,18 @@ static class Supplier1 implements Supplier<Integer> {
public Integer get() {
return 1;
}

public void m1() {

}
}

abstract static class Supplier0 implements Supplier<Integer> {
public void m1() {

}


}

static class Supplier2 implements Supplier<Integer> {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package io.leaderli.litool.test;

import net.bytebuddy.asm.Advice;

import java.lang.reflect.Method;

public class Supplier2dAdvice {
@Advice.OnMethodEnter
public static Object enter(
// @Advice.This Object mock, // 静态方法无this
@Advice.Origin Method origin,
@Advice.AllArguments Object[] arguments) {

return 100;
}
}

0 comments on commit e704054

Please sign in to comment.