mirror of
https://gitee.com/BDWare/MockJava
synced 2025-01-09 17:34:06 +00:00
build: config spotless plugin and reformat code
This commit is contained in:
parent
7447d5a079
commit
97ddefab68
@ -5,6 +5,8 @@ plugins {
|
||||
id 'signing'
|
||||
}
|
||||
|
||||
apply from: '../spotless.gradle'
|
||||
|
||||
group 'org.bdware.mockjava'
|
||||
version '0.5.0'
|
||||
|
||||
|
@ -12,24 +12,28 @@ import java.util.Map;
|
||||
public class MockGenerator extends JsonVisitor {
|
||||
|
||||
public static MockGenerator instance = new MockGenerator();
|
||||
Map<String,ValueGenerator> prefixToGenerator;
|
||||
Map<String, ValueGenerator> prefixToGenerator;
|
||||
JsonElement result;
|
||||
|
||||
public JsonElement get() {
|
||||
return result;
|
||||
}
|
||||
public MockGenerator(Class ...clzs){
|
||||
|
||||
public MockGenerator(Class... clzs) {
|
||||
prefixToGenerator = new HashMap<>();
|
||||
for (Class clz:clzs){
|
||||
Generator generator =(Generator) clz.getAnnotation(Generator.class);
|
||||
if (generator!=null){
|
||||
for (Class clz : clzs) {
|
||||
Generator generator = (Generator) clz.getAnnotation(Generator.class);
|
||||
if (generator != null) {
|
||||
try {
|
||||
prefixToGenerator.put(generator.tag(),(ValueGenerator)clz.getConstructor().newInstance());
|
||||
prefixToGenerator.put(generator.tag(),
|
||||
(ValueGenerator) clz.getConstructor().newInstance());
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public JsonVisitor visitObject(JsonObject jsonObject) {
|
||||
JsonObject jo = new JsonObject();
|
||||
@ -59,9 +63,10 @@ public class MockGenerator extends JsonVisitor {
|
||||
|
||||
@Override
|
||||
public JsonVisitor visitPrimitive(JsonPrimitive primitive) {
|
||||
if (!primitive.isString()) result = primitive.deepCopy();
|
||||
if (!primitive.isString())
|
||||
result = primitive.deepCopy();
|
||||
ValueGenerator generator = lookupGenerator(primitive.getAsString());
|
||||
result = generator.generate( primitive);
|
||||
result = generator.generate(primitive);
|
||||
return this;
|
||||
}
|
||||
|
||||
@ -69,19 +74,19 @@ public class MockGenerator extends JsonVisitor {
|
||||
if (!str.startsWith("@")) {
|
||||
return new ValueGenerator() {
|
||||
@Override
|
||||
public JsonElement generate( JsonElement previous) {
|
||||
public JsonElement generate(JsonElement previous) {
|
||||
return new JsonPrimitive(str);
|
||||
}
|
||||
};
|
||||
}
|
||||
int from = str.indexOf("(");
|
||||
String prefix = from>=0?str.substring(0,from):str;
|
||||
ValueGenerator generator = prefixToGenerator.get(prefix);
|
||||
if (generator==null){
|
||||
String prefix = from >= 0 ? str.substring(0, from) : str;
|
||||
ValueGenerator generator = prefixToGenerator.get(prefix);
|
||||
if (generator == null) {
|
||||
return new ValueGenerator() {
|
||||
@Override
|
||||
public JsonElement generate(JsonElement previous) {
|
||||
return new JsonPrimitive("unknown type, prefix:"+prefix+" all:"+str);
|
||||
return new JsonPrimitive("unknown type, prefix:" + prefix + " all:" + str);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
@ -28,4 +28,4 @@ public class MockSchemaParser extends JsonVisitor {
|
||||
}
|
||||
return this;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -18,9 +18,8 @@ public class MockUtil {
|
||||
static {
|
||||
MOCK_JS_ENGINE = new ScriptEngineManager().getEngineByName("JavaScript");
|
||||
// System.out.println(MOCK_JS_ENGINE);
|
||||
try (InputStream mockJs =
|
||||
MockUtil.class.getClassLoader().getResourceAsStream(MOCK_JS_PATH);
|
||||
InputStreamReader reader = new InputStreamReader(mockJs)) {
|
||||
try (InputStream mockJs = MockUtil.class.getClassLoader().getResourceAsStream(MOCK_JS_PATH);
|
||||
InputStreamReader reader = new InputStreamReader(mockJs)) {
|
||||
MOCK_JS_ENGINE.eval(reader);
|
||||
} catch (ScriptException | IOException e) {
|
||||
// log.error("执行MockJs错误", e);
|
||||
@ -58,7 +57,8 @@ public class MockUtil {
|
||||
}
|
||||
|
||||
public static JsonElement parseJsonSchema(JsonElement ele) {
|
||||
if (ele == null) return null;
|
||||
if (ele == null)
|
||||
return null;
|
||||
if (ele.isJsonPrimitive()) {
|
||||
JsonPrimitive primitive = ele.getAsJsonPrimitive();
|
||||
if (primitive.isBoolean()) {
|
||||
|
@ -9,8 +9,10 @@ import java.util.Random;
|
||||
|
||||
@Generator(tag = "@integer")
|
||||
public class IntegerGenerator extends ValueGenerator {
|
||||
public IntegerGenerator(){}
|
||||
static Random secureRandom = new SecureRandom();
|
||||
public IntegerGenerator() {}
|
||||
|
||||
static Random secureRandom = new SecureRandom();
|
||||
|
||||
@Override
|
||||
public JsonElement generate(JsonElement previous) {
|
||||
// TODO parse value here
|
||||
|
@ -5,7 +5,7 @@ import com.google.gson.JsonParser;
|
||||
import org.junit.Test;
|
||||
|
||||
public class MockUtilTest {
|
||||
//TODO @zhanghongwei 把这个测试用例拆多几个
|
||||
// TODO @zhanghongwei 把这个测试用例拆多几个
|
||||
@Test
|
||||
public void main() {
|
||||
// "{'result|min-max':1}":min-max之间的一个数字 如:"{'result|1-100':1}" 返回: {"result":21}
|
||||
@ -14,13 +14,13 @@ public class MockUtilTest {
|
||||
// "{'result':'@datetime'}" "{'result':'@date'}" "{'result':'@time'}"
|
||||
// "{'result':'@cname'}" "{'result':'@name'}" "{'result':'@first'}" "{'result':'@last'}"
|
||||
// "{'result':'@email'}" "{'result':'@ip'}" {'result':'@url'}"
|
||||
// "{'result':'@province'}" "{'result':'@city'}" "{'result':'@county'}"
|
||||
// "{'result':'@province'}" "{'result':'@city'}" "{'result':'@county'}"
|
||||
// {'id':'@integer','email':'@email','password':'@string','name':'@name'}
|
||||
// String template="{'result|1-100':1}";
|
||||
///
|
||||
// Object res = mock("'@email'");
|
||||
// Object res = mock("'@email'");
|
||||
// if (res instanceof ScriptObjectMirror)
|
||||
// System.out.println(JSONTool.copy((ScriptObjectMirror) res));
|
||||
// System.out.println(JSONTool.copy((ScriptObjectMirror) res));
|
||||
// else System.out.println(res);
|
||||
// System.out.println(res);
|
||||
// "{'list|1-5':[{'id|+1':1,'data':'@datetime','nickname': '@cname','email':'@email'}]}
|
||||
@ -35,8 +35,8 @@ public class MockUtilTest {
|
||||
System.out.println(ele2);
|
||||
Object ret = MockUtil.mock(ele2.toString());
|
||||
|
||||
// System.out.println(
|
||||
// (JSONTool.copy((jdk.nashorn.api.scripting.ScriptObjectMirror)
|
||||
// System.out.println(
|
||||
// (JSONTool.copy((jdk.nashorn.api.scripting.ScriptObjectMirror)
|
||||
// mock(ele2.toString()))).toString());
|
||||
|
||||
System.out.println(MockUtil.mock("'@string'"));
|
||||
|
Loading…
Reference in New Issue
Block a user