add enum class test

This commit is contained in:
oppofind 2019-10-31 22:38:20 +08:00
parent a677d2fe29
commit e9b66b13e4
3 changed files with 74 additions and 3 deletions

View File

@ -26,7 +26,7 @@ public class ApiDataDictionary {
private String descField;
public static ApiDataDictionary dictionary() {
public static ApiDataDictionary dict() {
return new ApiDataDictionary();
}

View File

@ -1,12 +1,16 @@
package com.power.doc;
import com.power.common.util.DateTimeUtil;
import com.power.doc.builder.ApiDocBuilder;
import com.power.common.util.StringUtil;
import com.power.doc.enums.OrderEnum;
import com.power.doc.model.ApiConfig;
import com.power.doc.model.ApiDataDictionary;
import com.power.doc.model.RevisionLog;
import com.power.doc.model.SourceCodePath;
import org.junit.Test;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
@ -50,6 +54,9 @@ public class ApiDocTest {
//SourcePath.path().setPath("F:\\Personal\\project\\smart\\src\\main\\java")
//SourcePath.path().setDesc("加载项目外代码").setPath("E:\\ApplicationPower\\ApplicationPower\\Common-util\\src\\main\\java")
);
config.setDataDictionaries(
ApiDataDictionary.dict().setTitle("订单字典").setEnumClass(OrderEnum.class).setValueField("code").setDescField("desc")
);
//设置请求头如果没有请求头可以不用设置
/* config.setRequestHeaders(
ApiReqHeader.header().setName("access_token").setType("string").setDesc("Basic auth credentials"),
@ -71,8 +78,32 @@ public class ApiDocTest {
RevisionLog.getLog().setRevisionTime("2018/12/16").setAuthor("chen2").setRemarks("测试2").setStatus("修改").setVersion("V2.0")
);
List<ApiDataDictionary> apiDataDictionaryList = config.getDataDictionaries();
try {
for (ApiDataDictionary apiDataDictionary : apiDataDictionaryList) {
System.out.println("dictionary" + apiDataDictionary.getTitle());
Class<?> clzz = apiDataDictionary.getEnumClass();
if (!clzz.isEnum()) {
throw new RuntimeException(clzz.getCanonicalName() + " is not an enum class.");
}
Object[] objects = clzz.getEnumConstants();
String valueMethodName = "get" + StringUtil.firstToUpperCase(apiDataDictionary.getValueField());
String descMethodName = "get" + StringUtil.firstToUpperCase(apiDataDictionary.getDescField());
Method valueMethod = clzz.getMethod(valueMethodName);
Method descMethod = clzz.getMethod(descMethodName);
for (Object object : objects) {
Object val = valueMethod.invoke(object);
Object desc = descMethod.invoke(object);
System.out.println("enum value=" + val + "desc=" + desc);
}
}
} catch (NoSuchMethodException | InvocationTargetException | IllegalAccessException e) {
}
long start = System.currentTimeMillis();
ApiDocBuilder.builderControllersApi(config);
// ApiDocBuilder.builderControllersApi(config);
long end = System.currentTimeMillis();
DateTimeUtil.printRunTime(end, start);
}

View File

@ -0,0 +1,40 @@
package com.power.doc.enums;
/**
* @author yu 2019/10/31.
*/
public enum OrderEnum {
SUCCESS("0000", "success"),
PARAM_EMPTY("1001", "必选参数为空"),
PARAM_ERROR("1002", "参数格式错误"),
UNKNOWN_ERROR("9999", "系统繁忙,请稍后再试....");
private String code;
private String desc;
OrderEnum(String code, String desc) {
this.code = code;
this.desc = desc;
}
public String getCode() {
return this.code;
}
public String getDesc() {
return desc;
}
@Override
public String toString() {
return "ErrorCodeEnum{" +
"code='" + code + '\'' +
", desc='" + desc + '\'' +
'}';
}
}