Skip to content

Commit

Permalink
add ColaMock
Browse files Browse the repository at this point in the history
  • Loading branch information
fulan.zjf committed Mar 11, 2019
1 parent 40dbe44 commit 5f09791
Show file tree
Hide file tree
Showing 107 changed files with 7,209 additions and 113 deletions.
52 changes: 50 additions & 2 deletions cola-framework/cola-test/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,41 @@
<relativePath>../pom.xml</relativePath>
</parent>
<artifactId>cola-test</artifactId>
<version>1.0.0-SNAPSHOT</version>
<version>${cola.framework.version}</version>
<name>cola-test</name>
<dependencies>
<dependency>
<groupId>org.freemarker</groupId>
<artifactId>freemarker</artifactId>
<version>2.3.28</version>
</dependency>
<dependency>
<groupId>commons-beanutils</groupId>
<artifactId>commons-beanutils</artifactId>
<version>1.8.3</version>
<scope>compile</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.javassist</groupId>
<artifactId>javassist</artifactId>
<version>3.23.1-GA</version>
<optional>true</optional>
</dependency>
<dependency>
<groupId>com.sun</groupId>
<artifactId>tools</artifactId>
<version>1.8</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-all</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
Expand All @@ -24,6 +51,27 @@
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</dependency>
<dependency>
<groupId>uk.co.jemos.podam</groupId>
<artifactId>podam</artifactId>
</dependency>

</dependencies>

<!-- Source attach plugin -->
Expand All @@ -43,5 +91,5 @@
</executions>
</plugin>
</plugins>
</build>
</build>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,12 @@
import java.util.List;
import java.util.Map;

import com.alibaba.cola.mock.listener.ColaNotifier;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.Description;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
Expand All @@ -20,7 +23,8 @@
* @author fulan.zjf 2017年10月27日 下午3:14:21
*/
public class TestExecutor {


private ColaNotifier notifier = new ColaNotifier();
private String className;
private String methodName;

Expand All @@ -35,17 +39,23 @@ public TestExecutor(ApplicationContext context){
public void testClass() throws Exception {
Class<?> testClz = Class.forName(className);
Object testInstance = getTestInstance(testClz);
//刷新
//ColaMockito.g().refreshScanMockFromTest(testClz);
runClassTest(testClz, testInstance);
}

public void testMethod() throws Exception {
Class<?> testClz = Class.forName(className);
Object testInstance = getTestInstance(testClz);
Object testInstance = getTestInstance(testClz);
//刷新
//if(ColaMockito.g().getContext().isRecording()) {
// ColaMockito.g().refreshScanMockFromTest(testClz);
//}
runMethodTest(testClz, testInstance);
}

private void runMethodTest(Class<?> testClz, Object testInstance) throws Exception{
Method[] allMethods = testClz.getDeclaredMethods();
Method[] allMethods = testClz.getMethods();
Method beforeMethod = null;
Method afterMethod = null;
for (Method method : allMethods){
Expand All @@ -61,24 +71,33 @@ private void runMethodTest(Class<?> testClz, Object testInstance) throws Excepti
}
}
}
Method method = testClz.getMethod(methodName);
Description description = Description.createTestDescription(testClz, method.getName());
notifier.fireTestRunStarted(testInstance);
notifier.fireTestRunStarted(description);
//invoke before method
invokeMethod(testInstance, beforeMethod);
notifier.fireTestStarted(method, description);
//invoke test method
invokeMethod(testInstance, testClz.getMethod(methodName));
invokeMethod(testInstance, method);
notifier.fireTestFinished(method, description);
//invoke after method
invokeMethod(testInstance, afterMethod);
invokeMethod(testInstance, afterMethod);
notifier.fireTestRunFinished(description);
}

private Object getTestInstance(Class<?> testClz) throws Exception{
if(testInstanceCache.get(className) != null)
if(testInstanceCache.get(className) != null) {
return testInstanceCache.get(className);
}
Object testInstance = testClz.newInstance();
injectWiredBean(testClz, testInstance);
return testInstance;
}

private void runClassTest(Class<?> testClz, Object testInstance)throws Exception{
Method[] allMethods = testClz.getDeclaredMethods();
Description description = Description.createSuiteDescription(testClz);
Method[] allMethods = testClz.getMethods();
Method beforeMethod = null;
Method afterMethod = null;
List<Method> testMethods = new ArrayList<Method>();
Expand All @@ -95,40 +114,53 @@ private void runClassTest(Class<?> testClz, Object testInstance)throws Exception
}
if(annotation instanceof Test || method.getName().startsWith("test")){
testMethods.add(method);
description.addChild(Description.createTestDescription(testClz, method.getName()));
break;
}
}
}
}
notifier.fireTestRunStarted(testInstance);
notifier.fireTestRunStarted(description);
//invoke before method
invokeMethod(testInstance, beforeMethod);
//invoke test methods
for(Method testMethod: testMethods){
notifier.fireTestStarted(testMethod, description);
//testMethodStarted(testInstance, testMethod);
invokeMethod(testInstance, testMethod);
//testMethodFinished(testInstance, testMethod);
notifier.fireTestFinished(testMethod, description);
}
//invoke after method
invokeMethod(testInstance, afterMethod);
notifier.fireTestRunFinished(description);
}

private static void invokeMethod(Object obj, Method method) throws Exception{
if (method == null)
if (method == null) {
return;
}
method.invoke(obj);
}

private void injectWiredBean(Class<?> testClz, Object testInstance) {
Field[] fields = testClz.getDeclaredFields();
if(fields == null) return;
if(fields == null) {
return;
}
for(Field field : fields) {
String beanName = field.getName();
Annotation autowiredAnn = field.getDeclaredAnnotation(Autowired.class);
if (autowiredAnn == null) {
//System.out.println("Field "+beanName+" is not autowired, just ignore it");
return;
System.out.println("Field "+beanName+" is not autowired, just ignore it");
continue;
}
try {
field.setAccessible(true);
field.set(testInstance, context.getBean(beanName));
} catch (BeansException | IllegalArgumentException | IllegalAccessException e) {
} catch (IllegalArgumentException e){
e.printStackTrace();
}catch (BeansException | IllegalAccessException e) {
//try to use type to get bean
try {
field.set(testInstance, context.getBean(field.getType()));
Expand Down Expand Up @@ -171,6 +203,12 @@ public String getMethodName() {
public void setMethodName(String methodName) {
this.methodName = methodName;
}



public ColaNotifier getNotifier() {
return notifier;
}

public void setNotifier(ColaNotifier notifier) {
this.notifier = notifier;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.concurrent.atomic.AtomicBoolean;

import com.alibaba.cola.mock.ColaTestRecordController;
import com.alibaba.cola.mock.autotest.ColaTestGenerator;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
Expand Down Expand Up @@ -37,13 +42,28 @@ public class TestsContainer implements ApplicationContextAware{
private static final String WELCOME_INPUT = "$Welcome$";

private static final String REPEAT_INPUT = "r";

/** 创建测试类命令*/
private static final String CREATE_TEST_INPUT = "new";
private static final String SPACE = " ";

private static ApplicationContext context;

private static TestExecutor testExecutor;
private static AtomicBoolean initFlag = new AtomicBoolean(false);

private static boolean isInputValid = true;


public static void init(ApplicationContext context){
if(!initFlag.compareAndSet(false, true)) {
return;
}
if(context == null){
testExecutor = new TestExecutor(TestsContainer.context);
}else {
testExecutor = new TestExecutor(context);
}
}

/**
* TestsContainer is optional to be in Spring Container
* @param context ApplicationContext to be provided
Expand All @@ -56,10 +76,8 @@ public static void start(ApplicationContext context) {
/**
* TestsContainer must be within Spring Container
*/
public static void start( ){

testExecutor = new TestExecutor(context);

public static void start(){
init(TestsContainer.context);
BufferedReader bufferRead = new BufferedReader(new InputStreamReader(
System.in));
String input = WELCOME_INPUT;
Expand Down Expand Up @@ -103,6 +121,8 @@ private static void execute(String input) throws Exception{
System.out.println("**** 2.测试整个测试类,请在控制台输入类全称");
System.out.println("**** 例如:com.alibaba.cola.sales.service.test.CustomerServiceTest");
System.out.println("**** 3.重复上一次测试,只需在控制台输入字母 - ‘r’");
System.out.println("**** 4.自动生成ColaTest测试类,请输入‘new 方法全称 参数1 参数2 ...’");
System.out.println("**** 例如:new com.alibaba.crm.sales.domain.customer.entity.CustomerE#addContact");
System.out.println("***********************************************************************************");
return;
}
Expand All @@ -111,6 +131,10 @@ private static void execute(String input) throws Exception{
System.err.println("Your input is not a valid qualified name");
return;
}
if(input.startsWith(CREATE_TEST_INPUT + SPACE)){
createTestClassCmd(input);
return;
}
boolean isMethod = false;
if (isEclipseMethod(input) || isIdeaMethod(input)){
isMethod = true;
Expand Down Expand Up @@ -142,6 +166,17 @@ private static void execute(String input) throws Exception{
System.out.println("===Run "+(isMethod?"Method":"Class")+" end====\n");
}

private static void createTestClassCmd(String cmd){
cmd = cmd.replaceAll(" +", " ");
String[] cmdParams = cmd.split(" ");
if(cmdParams.length < 2){
System.err.println("Your input is not a valid");
return;
}
ColaTestGenerator generator = new ColaTestGenerator(cmdParams[1], ColaTestRecordController.getTemplateSuperClassName());
generator.generate(Arrays.copyOfRange(cmdParams, 2, cmdParams.length));
}

/**
* @param input
* @return
Expand All @@ -154,6 +189,10 @@ private static boolean isIdeaMethod(String input) {
return input.indexOf("#") > 0 ;//to accommodate idea
}

public static TestExecutor getTestExecutor() {
return testExecutor;
}

@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
context = applicationContext;
Expand Down
Loading

0 comments on commit 5f09791

Please sign in to comment.