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