mirror of
https://github.com/wahyd4/Martian.git
synced 2026-08-08 21:06:40 +10:00
补充core模块的单测
This commit is contained in:
@@ -20,11 +20,11 @@ public class StartAfter {
|
||||
*/
|
||||
public static void after() throws Exception {
|
||||
try {
|
||||
List<Class> easyLoads = LoadHelper.getMarsAfterList();
|
||||
List<Class> afterList = LoadHelper.getMarsAfterList();
|
||||
|
||||
Map<String, MarsBeanModel> beanModelMap = LoadHelper.getBeanObjectMap();
|
||||
|
||||
for (Class cls : easyLoads) {
|
||||
for (Class cls : afterList) {
|
||||
Object obj = cls.getDeclaredConstructor().newInstance();
|
||||
|
||||
/* 注入属性 */
|
||||
|
||||
@@ -0,0 +1,26 @@
|
||||
package com.mars.core.test.aop.aop;
|
||||
|
||||
import com.alibaba.fastjson.JSON;
|
||||
import com.mars.aop.base.BaseAop;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
public class AopTest implements BaseAop {
|
||||
|
||||
private Logger logger = LoggerFactory.getLogger(AopTest.class);
|
||||
|
||||
@Override
|
||||
public void startMethod(Object[] args) {
|
||||
logger.info("AOP单测开始执行startMethod,方法参数:{}",JSON.toJSONString(args));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void endMethod(Object[] args, Object result) {
|
||||
logger.info("AOP单测开始执行endMethod,方法参数:{},方法返回值:{}",JSON.toJSONString(args), result);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void exp(Throwable e) {
|
||||
logger.error("AOP单测出异常",e);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
package com.mars.core.test.aop.aop;
|
||||
|
||||
import com.mars.aop.model.AopModel;
|
||||
import com.mars.aop.proxy.exec.ExecAop;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
* 测试AOP执行类
|
||||
*/
|
||||
public class ExecAopTest {
|
||||
|
||||
@Test
|
||||
public void testStartMethod(){
|
||||
try {
|
||||
AopModel aopModel = new AopModel();
|
||||
aopModel.setCls(AopTest.class);
|
||||
aopModel.setObj(new AopTest());
|
||||
ExecAop.startMethod(new Object[]{"a","b"},aopModel);
|
||||
} catch (Exception e){
|
||||
Assert.fail(e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testEndMethod(){
|
||||
try {
|
||||
AopModel aopModel = new AopModel();
|
||||
aopModel.setCls(AopTest.class);
|
||||
aopModel.setObj(new AopTest());
|
||||
ExecAop.endMethod(new Object[]{"a","b"},"返回值",aopModel);
|
||||
} catch (Exception e){
|
||||
Assert.fail(e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testExp(){
|
||||
try {
|
||||
AopModel aopModel = new AopModel();
|
||||
aopModel.setCls(AopTest.class);
|
||||
aopModel.setObj(new AopTest());
|
||||
ExecAop.exp(aopModel,new Exception("单测看一下aop异常"));
|
||||
} catch (Exception e){
|
||||
Assert.fail(e.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
package com.mars.core.test.aop.traction;
|
||||
|
||||
import com.mars.aop.model.AopModel;
|
||||
import com.mars.aop.proxy.exec.ExecTraction;
|
||||
import com.mars.common.annotation.enums.TractionLevel;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
* 测试事务执行类
|
||||
*/
|
||||
public class ExecTractionTest {
|
||||
|
||||
@Test
|
||||
public void testBeginTraction(){
|
||||
try {
|
||||
AopModel aopModel = new AopModel();
|
||||
aopModel.setCls(TractionTest.class);
|
||||
aopModel.setObj(new TractionTest());
|
||||
aopModel.setTractionLevel(TractionLevel.READ_UNCOMMITTED);
|
||||
ExecTraction.beginTraction(aopModel);
|
||||
|
||||
aopModel.setTractionLevel(TractionLevel.READ_COMMITTED);
|
||||
ExecTraction.beginTraction(aopModel);
|
||||
|
||||
aopModel.setTractionLevel(TractionLevel.SERIALIZABLE);
|
||||
ExecTraction.beginTraction(aopModel);
|
||||
} catch (Exception e){
|
||||
Assert.fail(e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCommit(){
|
||||
try {
|
||||
AopModel aopModel = new AopModel();
|
||||
aopModel.setCls(TractionTest.class);
|
||||
aopModel.setObj(new TractionTest());
|
||||
ExecTraction.commit(aopModel);
|
||||
} catch (Exception e){
|
||||
Assert.fail(e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRollback(){
|
||||
try {
|
||||
AopModel aopModel = new AopModel();
|
||||
aopModel.setCls(TractionTest.class);
|
||||
aopModel.setObj(new TractionTest());
|
||||
ExecTraction.rollback(aopModel,new Exception("单测看一下事务异常"));
|
||||
} catch (Exception e){
|
||||
Assert.fail(e.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
package com.mars.core.test.aop.traction;
|
||||
|
||||
import com.alibaba.fastjson.JSON;
|
||||
import com.mars.aop.model.AopModel;
|
||||
import com.mars.core.test.aop.aop.AopTest;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
public class TractionTest {
|
||||
|
||||
private Logger logger = LoggerFactory.getLogger(TractionTest.class);
|
||||
|
||||
public void startMethod(AopModel aopModel) {
|
||||
logger.info("单测开始执行事务开启方法,事务级别:{}", JSON.toJSONString(aopModel.getTractionLevel()));
|
||||
}
|
||||
|
||||
public void endMethod() {
|
||||
logger.info("单测开始执行事务提交方法");
|
||||
}
|
||||
|
||||
public void exp(Throwable e) {
|
||||
logger.info("事务单测出异常",e);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
package com.mars.core.test.core.after;
|
||||
|
||||
import com.mars.common.annotation.bean.MarsWrite;
|
||||
import com.mars.common.base.BaseAfter;
|
||||
import org.junit.Assert;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
public class JunitAfter implements BaseAfter {
|
||||
|
||||
private Logger logger = LoggerFactory.getLogger(JunitAfter.class);
|
||||
|
||||
@MarsWrite("junitTestBean")
|
||||
private TestBean testBean;
|
||||
|
||||
@Override
|
||||
public void after() throws Exception {
|
||||
logger.info("执行了After方法,testBean的值:{}",testBean);
|
||||
Assert.assertNotNull(testBean);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
package com.mars.core.test.core.after;
|
||||
|
||||
import com.mars.common.constant.MarsConstant;
|
||||
import com.mars.common.constant.MarsSpace;
|
||||
import com.mars.core.after.StartAfter;
|
||||
import com.mars.core.load.LoadHelper;
|
||||
import com.mars.core.model.MarsBeanModel;
|
||||
import com.mars.ioc.factory.BeanFactory;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* 测试after
|
||||
*/
|
||||
public class StartAfterTest {
|
||||
|
||||
@Test
|
||||
public void testAfter(){
|
||||
try {
|
||||
|
||||
MarsSpace marsSpace = MarsSpace.getEasySpace();
|
||||
|
||||
marsSpace.remove(MarsConstant.HAS_START);
|
||||
|
||||
Map<String, MarsBeanModel> beanModelMap = LoadHelper.getBeanObjectMap();
|
||||
MarsBeanModel marsBeanModel = new MarsBeanModel();
|
||||
marsBeanModel.setCls(TestBean.class);
|
||||
marsBeanModel.setName("junitTestBean");
|
||||
marsBeanModel.setObj(BeanFactory.createBean(TestBean.class));
|
||||
beanModelMap.put("junitTestBean",marsBeanModel);
|
||||
marsSpace.setAttr(MarsConstant.MARS_BEAN_OBJECTS,beanModelMap);
|
||||
|
||||
|
||||
List<Class> afterList = LoadHelper.getMarsAfterList();
|
||||
afterList.add(JunitAfter.class);
|
||||
marsSpace.setAttr(MarsConstant.MARS_AFTERS,afterList);
|
||||
|
||||
StartAfter.after();
|
||||
|
||||
marsSpace.remove(MarsConstant.MARS_AFTERS);
|
||||
marsSpace.remove(MarsConstant.MARS_BEAN_OBJECTS);
|
||||
} catch (Exception e){
|
||||
Assert.fail(e.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
package com.mars.core.test.core.after;
|
||||
|
||||
import com.mars.common.annotation.bean.MarsBean;
|
||||
|
||||
@MarsBean("junitTestBean")
|
||||
public class TestBean {
|
||||
|
||||
}
|
||||
@@ -0,0 +1,86 @@
|
||||
package com.mars.core.test.core.load;
|
||||
|
||||
import com.mars.common.annotation.api.MarsApi;
|
||||
import com.mars.common.constant.MarsConstant;
|
||||
import com.mars.common.constant.MarsSpace;
|
||||
import com.mars.core.load.LoadBeans;
|
||||
import com.mars.core.load.LoadClass;
|
||||
import com.mars.core.load.LoadHelper;
|
||||
import com.mars.core.model.MarsBeanClassModel;
|
||||
import com.mars.core.test.core.load.junitbean.*;
|
||||
import com.mars.redis.lock.MarsRedisLock;
|
||||
import com.mars.redis.template.MarsRedisTemplate;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* bean扫描测试
|
||||
*/
|
||||
public class LoadTest {
|
||||
|
||||
@Test
|
||||
public void testLoad(){
|
||||
try {
|
||||
|
||||
MarsSpace marsSpace = MarsSpace.getEasySpace();
|
||||
|
||||
List<String> packageList = new ArrayList<>();
|
||||
packageList.add("com.mars.core.test.core.load.junitbean");
|
||||
LoadClass.scanClass(packageList);
|
||||
|
||||
LoadBeans.loadBeans();
|
||||
|
||||
List<MarsBeanClassModel> marsApiList = LoadHelper.getMarsApiList();
|
||||
for(MarsBeanClassModel api : marsApiList){
|
||||
Assert.assertEquals(api.getClassName(), JunitMarsApi.class);
|
||||
Assert.assertNotNull(api.getAnnotation());
|
||||
}
|
||||
marsSpace.remove(MarsConstant.CONTROLLERS);
|
||||
|
||||
List<Class> afterList = LoadHelper.getMarsAfterList();
|
||||
for(Class afterCls : afterList){
|
||||
Assert.assertEquals(afterCls, JunitMarsAfter.class);
|
||||
}
|
||||
marsSpace.remove(MarsConstant.MARS_AFTERS);
|
||||
|
||||
List<MarsBeanClassModel> marsBeanClassModelList = LoadHelper.getBeanList();
|
||||
for(MarsBeanClassModel bean : marsBeanClassModelList){
|
||||
if(bean.getClassName().equals(MarsRedisTemplate.class)){
|
||||
continue;
|
||||
}
|
||||
if(bean.getClassName().equals(MarsRedisLock.class)){
|
||||
continue;
|
||||
}
|
||||
if(bean.getClassName().equals(JunitMarsBean.class)){
|
||||
Assert.assertEquals(bean.getClassName(), JunitMarsBean.class);
|
||||
Assert.assertNotNull(bean.getAnnotation());
|
||||
}
|
||||
if(bean.getClassName().equals(JunitIocMarsBean.class)){
|
||||
Assert.assertEquals(bean.getClassName(), JunitIocMarsBean.class);
|
||||
Assert.assertNotNull(bean.getAnnotation());
|
||||
}
|
||||
}
|
||||
marsSpace.remove(MarsConstant.MARS_BEANS);
|
||||
|
||||
List<MarsBeanClassModel> daoList = LoadHelper.getDaoList();
|
||||
for(MarsBeanClassModel dao : daoList){
|
||||
Assert.assertEquals(dao.getClassName(), JunitMarsDao.class);
|
||||
Assert.assertNotNull(dao.getAnnotation());
|
||||
}
|
||||
marsSpace.remove(MarsConstant.MARS_DAOS);
|
||||
|
||||
List<MarsBeanClassModel> interList = LoadHelper.getInterceptorList();
|
||||
for(MarsBeanClassModel inter : interList){
|
||||
Assert.assertEquals(inter.getClassName(), JunitMarsInterceptor.class);
|
||||
Assert.assertNotNull(inter.getAnnotation());
|
||||
}
|
||||
marsSpace.remove(MarsConstant.INTERCEPTORS);
|
||||
|
||||
} catch (Exception e){
|
||||
Assert.fail(e.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
package com.mars.core.test.core.load.junitbean;
|
||||
|
||||
import com.mars.common.annotation.bean.MarsBean;
|
||||
|
||||
@MarsBean
|
||||
public class JunitIocMarsBean {
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
package com.mars.core.test.core.load.junitbean;
|
||||
|
||||
import com.mars.common.annotation.bean.MarsAfter;
|
||||
import com.mars.common.base.BaseAfter;
|
||||
|
||||
@MarsAfter
|
||||
public class JunitMarsAfter implements BaseAfter {
|
||||
|
||||
@Override
|
||||
public void after() throws Exception {
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
package com.mars.core.test.core.load.junitbean;
|
||||
|
||||
import com.mars.common.annotation.api.MarsApi;
|
||||
|
||||
@MarsApi
|
||||
public interface JunitMarsApi {
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
package com.mars.core.test.core.load.junitbean;
|
||||
|
||||
import com.mars.common.annotation.bean.MarsBean;
|
||||
import com.mars.common.annotation.bean.MarsWrite;
|
||||
|
||||
@MarsBean("junitMarsBean")
|
||||
public class JunitMarsBean {
|
||||
|
||||
@MarsWrite
|
||||
private JunitIocMarsBean junitIocMarsBean;
|
||||
|
||||
public JunitIocMarsBean getJunitIocMarsBean() {
|
||||
return junitIocMarsBean;
|
||||
}
|
||||
|
||||
public void setJunitIocMarsBean(JunitIocMarsBean junitIocMarsBean) {
|
||||
this.junitIocMarsBean = junitIocMarsBean;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
package com.mars.core.test.core.load.junitbean;
|
||||
|
||||
import com.mars.common.annotation.jdbc.MarsDao;
|
||||
|
||||
@MarsDao
|
||||
public abstract class JunitMarsDao {
|
||||
}
|
||||
+19
@@ -0,0 +1,19 @@
|
||||
package com.mars.core.test.core.load.junitbean;
|
||||
|
||||
import com.mars.common.annotation.api.MarsInterceptor;
|
||||
import com.mars.common.annotation.bean.MarsWrite;
|
||||
|
||||
@MarsInterceptor
|
||||
public class JunitMarsInterceptor {
|
||||
|
||||
@MarsWrite
|
||||
private JunitMarsBean junitMarsBean;
|
||||
|
||||
public JunitMarsBean getJunitMarsBean() {
|
||||
return junitMarsBean;
|
||||
}
|
||||
|
||||
public void setJunitMarsBean(JunitMarsBean junitMarsBean) {
|
||||
this.junitMarsBean = junitMarsBean;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,71 @@
|
||||
package com.mars.core.test.ioc;
|
||||
|
||||
import com.mars.common.annotation.bean.MarsBean;
|
||||
import com.mars.common.constant.MarsConstant;
|
||||
import com.mars.common.constant.MarsSpace;
|
||||
import com.mars.core.load.LoadBeans;
|
||||
import com.mars.core.load.LoadClass;
|
||||
import com.mars.core.load.LoadHelper;
|
||||
import com.mars.core.model.MarsBeanModel;
|
||||
import com.mars.core.test.core.load.junitbean.JunitIocMarsBean;
|
||||
import com.mars.core.test.core.load.junitbean.JunitMarsBean;
|
||||
import com.mars.ioc.load.LoadMarsBean;
|
||||
import com.mars.redis.lock.MarsRedisLock;
|
||||
import com.mars.redis.template.MarsRedisTemplate;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* 测试bean创建
|
||||
*/
|
||||
public class LoadMarsBeanTest {
|
||||
|
||||
@Test
|
||||
public void testLoadBean(){
|
||||
try {
|
||||
MarsSpace marsSpace = MarsSpace.getEasySpace();
|
||||
|
||||
List<String> packageList = new ArrayList<>();
|
||||
packageList.add("com.mars.core.test.core.load.junitbean");
|
||||
LoadClass.scanClass(packageList);
|
||||
|
||||
LoadBeans.loadBeans();
|
||||
|
||||
LoadMarsBean.loadBean();
|
||||
|
||||
Map<String, MarsBeanModel> marsBeanObjects = LoadHelper.getBeanObjectMap();
|
||||
|
||||
MarsBeanModel junitIocMarsBean = marsBeanObjects.get("junitIocMarsBean");
|
||||
Assert.assertNotNull(junitIocMarsBean);
|
||||
Assert.assertEquals(junitIocMarsBean.getCls(), JunitIocMarsBean.class);
|
||||
Assert.assertEquals(junitIocMarsBean.getName(),"junitIocMarsBean");
|
||||
Assert.assertNotNull(junitIocMarsBean.getObj());
|
||||
|
||||
MarsBeanModel junitMarsBean = marsBeanObjects.get("junitMarsBean");
|
||||
Assert.assertNotNull(junitMarsBean);
|
||||
|
||||
Assert.assertEquals(junitMarsBean.getCls(),JunitMarsBean.class);
|
||||
Assert.assertEquals(junitMarsBean.getName(),"junitMarsBean");
|
||||
Assert.assertNotNull(junitMarsBean.getObj());
|
||||
|
||||
/* 校验属性是否注入成功 */
|
||||
JunitMarsBean junitMarsBeanObj = (JunitMarsBean)junitMarsBean.getObj();
|
||||
Assert.assertNotNull(junitMarsBeanObj.getJunitIocMarsBean());
|
||||
|
||||
marsSpace.remove(MarsConstant.CONTROLLERS);
|
||||
marsSpace.remove(MarsConstant.MARS_AFTERS);
|
||||
marsSpace.remove(MarsConstant.MARS_BEANS);
|
||||
marsSpace.remove(MarsConstant.MARS_DAOS);
|
||||
marsSpace.remove(MarsConstant.INTERCEPTORS);
|
||||
|
||||
} catch (Exception e){
|
||||
e.printStackTrace();
|
||||
Assert.fail(e.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user