Skip to content

Commit

Permalink
aaa
Browse files Browse the repository at this point in the history
  • Loading branch information
leaderli committed Dec 25, 2024
1 parent 328c6e8 commit c815910
Show file tree
Hide file tree
Showing 13 changed files with 64 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ protected LiTypeToken() {
static Type getSuperclassTypeParameter(Class<?> subclass) {
Type superclass = subclass.getGenericSuperclass();
if (superclass instanceof Class) {
throw new RuntimeException("Missing type parameter.");
throw new IllegalArgumentException("Missing type parameter.");
}
ParameterizedType parameterized = (ParameterizedType) superclass;
return TypeUtil.canonicalize(parameterized.getActualTypeArguments()[0]);
Expand All @@ -74,6 +74,7 @@ static Type getSuperclassTypeParameter(Class<?> subclass) {
* @return 如果类型可以分配给泛型数组类型,则返回true;否则返回false。
*/
private static boolean isAssignableFrom(Type from, GenericArrayType to) {

Type toGenericComponentType = to.getGenericComponentType();
if (toGenericComponentType instanceof ParameterizedType) {
Type t = from;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,8 @@ public BaseMocker<T, R> mock(MethodFilter methodFilter, MethodProxy otherFunctio

for (Method declaredMethod : LiMock.findDeclaredMethods(mockClass, methodFilter)) {
methodValueMap.computeIfAbsent(declaredMethod, MethodValue::new).otherFunction(otherFunction);

}
return this;

}

}
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package io.leaderli.litool.test;

import io.leaderli.litool.core.type.BeanCreator;
import io.leaderli.litool.core.type.MethodFilter;
import io.leaderli.litool.core.type.PrimitiveEnum;
import io.leaderli.litool.test.bean.BeanCreator;

public abstract class BaseMockerForBean<T, R> extends BaseMocker<T, R> {
protected final boolean detach;
Expand Down
42 changes: 40 additions & 2 deletions litool-test/src/main/java/io/leaderli/litool/test/LiMock.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ private Skip() {
private static boolean checkJacoco() {
for (Class<?> loadedClass : instrumentation.getAllLoadedClasses()) {
if (ClassFileTransformer.class.isAssignableFrom(loadedClass) && loadedClass.getName().startsWith("org.jacoco.agent.rt")) {
return true;
}
return true;
}

}
return false;
Expand Down Expand Up @@ -485,6 +485,44 @@ public static <T> IRecorder<RecordBeans<T>, T> recordBeans(Class<? extends T>...
return new RecordBeans<>(mockClasses);
}

@SuppressWarnings({"unchecked", "rawtypes"})
public static <T> T simpleInterface(Class<T> mockClass, Object... returns) {
MockInterface<T> mockInterface = new MockInterface<>(mockClass);
for (Method method : mockClass.getMethods()) {
if (mockInterface.methodValueMap.containsKey(method)) {
continue;
}
for (Object returnValue : returns) {
if (ClassUtil.isAssignableFromOrIsWrapper(method.getReturnType(), returnValue.getClass())) {
MethodValue methodValue = new MethodValue(method);
methodValue.other(returnValue);
mockInterface.methodValueMap.put(method, methodValue);
break;
}
}
}
return mockInterface.build();
}

public static void simple(Class<?> mockClass, Object... returns) {
if (mockClass.isInterface()) {
throw new IllegalArgumentException("not support interface " + mockClass);
}
Map<Method, Object> returnMap = new HashMap<>();
for (Method declaredMethod : findDeclaredMethods(mockClass, MethodFilter.isMethod())) {
if (returnMap.containsKey(declaredMethod)) {
continue;
}
for (Object returnValue : returns) {
if (ClassUtil.isAssignableFromOrIsWrapper(declaredMethod.getReturnType(), returnValue.getClass())) {
returnMap.put(declaredMethod, returnValue);
break;
}
}
}
mock(mockClass, MethodFilter.of(returnMap::containsKey), (m, args) -> returnMap.get(m));
}

public static IMocker<Void, Object> mocker(Class<?> mockClass) {
return new Mocker(mockClass, true);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package io.leaderli.litool.test;

import io.leaderli.litool.core.type.BeanCreator;
import io.leaderli.litool.test.bean.BeanCreator;
import org.junit.jupiter.api.extension.ExtensionContext;
import org.junit.jupiter.api.extension.ParameterContext;
import org.junit.jupiter.api.extension.ParameterResolutionException;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package io.leaderli.litool.test;

import io.leaderli.litool.core.type.BeanCreator;
import io.leaderli.litool.core.type.ReflectUtil;
import io.leaderli.litool.test.bean.BeanCreator;
import org.junit.jupiter.api.extension.*;

import java.lang.reflect.Field;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ public abstract class MethodValueRecorder {
protected final Class<?> mockClass;
protected final Map<Method, MethodValue> methodValueMap = new HashMap<>();

public MethodValueRecorder(Class<?> mockClass) {
protected MethodValueRecorder(Class<?> mockClass) {
this.mockClass = mockClass;
}

public static Either<?, ?> invoke(String uuid, Class<?> clazz, String name, Class<?>[] argsType, Object[] args) throws Throwable {
public static Either invoke(String uuid, Class<?> clazz, String name, Class<?>[] argsType, Object[] args) throws Throwable {

LiTuple<MethodProxy<?>, Method> tuple = invokers.get(uuid);
if (tuple == null) {
Expand Down Expand Up @@ -93,7 +93,7 @@ public static void record(String uuid, Object _this, Object[] args, Object _retu
try {
tuple._1.apply(tuple._2, args, _return);
} catch (Throwable throwable) {
Recorder.assertThrow.add(throwable);
AbstractRecorder.assertThrow.add(throwable);
throw throwable;
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package io.leaderli.litool.test;

import io.leaderli.litool.core.type.BeanCreator;
import io.leaderli.litool.test.bean.BeanCreator;

public interface MockBeanBuilderConfig {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package io.leaderli.litool.test;

import io.leaderli.litool.core.exception.LiAssertUtil;
import io.leaderli.litool.core.type.BeanCreator;
import io.leaderli.litool.core.type.MethodFilter;
import io.leaderli.litool.test.bean.BeanCreator;

import java.util.ArrayList;
import java.util.function.Consumer;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
package io.leaderli.litool.core.type;
package io.leaderli.litool.test.bean;

import io.leaderli.litool.core.type.*;

import java.lang.annotation.Annotation;
import java.lang.reflect.Array;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,15 @@ class LiMockTest {
}


@LiTest
void testSimple() {
Assertions.assertEquals(1, new Bean11().m1(1));
LiMock.simple(Bean11.class, 10);
Assertions.assertEquals(10, new Bean11().m1(1));
Supplier supplier = LiMock.simpleInterface(Supplier.class, 10);
Assertions.assertEquals(10, supplier.get());

}
@LiTest
void skipClassConstructors() {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package io.leaderli.litool.test;

import io.leaderli.litool.core.type.BeanCreator;
import io.leaderli.litool.test.bean.BeanCreator;
import org.junit.jupiter.api.Assertions;

@LiTestInstance
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package io.leaderli.litool.core.type;
package io.leaderli.litool.test.bean;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
Expand Down

0 comments on commit c815910

Please sign in to comment.