mirror of
https://github.com/alibaba/testable-mock.git
synced 2026-08-19 09:43:29 +08:00
feat: support construct simple interface and abstract class
This commit is contained in:
@@ -1,8 +1,11 @@
|
||||
package com.alibaba.testable.core.util;
|
||||
|
||||
import com.alibaba.testable.core.compile.InMemoryJavaCompiler;
|
||||
import com.alibaba.testable.core.tool.OmniConstructor;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
import java.lang.reflect.Modifier;
|
||||
import java.lang.reflect.Parameter;
|
||||
|
||||
import static com.alibaba.testable.core.constant.ConstPool.DOLLAR;
|
||||
import static com.alibaba.testable.core.constant.ConstPool.DOT;
|
||||
@@ -19,7 +22,23 @@ public class ConstructionUtil {
|
||||
.append(clazz.getName().replace(DOLLAR, DOT))
|
||||
.append(" {\n");
|
||||
for (Method m : clazz.getMethods()) {
|
||||
|
||||
if (!Modifier.isStatic(m.getModifiers()) && !Modifier.isFinal(m.getModifiers())) {
|
||||
sourceCode.append("\tpublic ").append(m.getReturnType().getName().replace(DOLLAR, DOT)).append(" ")
|
||||
.append(m.getName()).append("(");
|
||||
Class<?>[] parameters = m.getParameterTypes();
|
||||
for (int i = 0; i < parameters.length; i++) {
|
||||
sourceCode.append(getParameterName(parameters[i])).append(" p").append(i);
|
||||
if (i < parameters.length - 1) {
|
||||
sourceCode.append(", ");
|
||||
}
|
||||
}
|
||||
sourceCode.append(") {\n");
|
||||
if (!m.getReturnType().equals(void.class)) {
|
||||
sourceCode.append("\t\treturn ").append(OmniConstructor.class.getName().replace(DOLLAR, DOT)).append(".")
|
||||
.append("newInstance(").append(m.getReturnType().getName().replace(DOLLAR, DOT)).append(".class);\n");
|
||||
}
|
||||
sourceCode.append("\t}\n");
|
||||
}
|
||||
}
|
||||
sourceCode.append("}");
|
||||
|
||||
@@ -33,6 +52,13 @@ public class ConstructionUtil {
|
||||
}
|
||||
}
|
||||
|
||||
private static String getParameterName(Class<?> parameter) {
|
||||
if (parameter.isArray()) {
|
||||
return getParameterName(parameter.getComponentType()) + "[]";
|
||||
}
|
||||
return parameter.getName().replace(DOLLAR, DOT);
|
||||
}
|
||||
|
||||
private static String getSubclassName(Class<?> clazz) {
|
||||
return clazz.getSimpleName() + TESTABLE_IMPL;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,41 @@
|
||||
package com.alibaba.testable.core.util;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
class ConstructionUtilTest {
|
||||
|
||||
public interface EmptyInterface {}
|
||||
|
||||
public interface RealInterface {
|
||||
void fun1();
|
||||
int func2(double d, boolean b);
|
||||
String fun3(String s, byte[] b);
|
||||
EmptyInterface fun4(RealInterface i);
|
||||
}
|
||||
|
||||
public static abstract class AbstractClazz implements RealInterface {
|
||||
@Override
|
||||
public void fun1() {}
|
||||
public static <T> T useless() { return null; }
|
||||
}
|
||||
|
||||
@Test
|
||||
void should_generate_empty_interface() throws Exception {
|
||||
EmptyInterface ins = ConstructionUtil.generateSubClassOf(EmptyInterface.class);
|
||||
assertNotNull(ins);
|
||||
}
|
||||
|
||||
@Test
|
||||
void should_generate_real_interface() throws Exception {
|
||||
RealInterface ins = ConstructionUtil.generateSubClassOf(RealInterface.class);
|
||||
assertNotNull(ins);
|
||||
}
|
||||
|
||||
@Test
|
||||
void should_generate_abstract_class() throws Exception {
|
||||
RealInterface ins = ConstructionUtil.generateSubClassOf(AbstractClazz.class);
|
||||
assertNotNull(ins);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user