From 97ddefab68d747a3accb5c8835691b015a16e6f0 Mon Sep 17 00:00:00 2001 From: "Frank.R.Wu" Date: Thu, 15 Jun 2023 11:08:07 +0800 Subject: [PATCH] build: config spotless plugin and reformat code --- build.gradle | 2 ++ .../org/bdware/mockjava/MockGenerator.java | 31 +++++++++++-------- .../org/bdware/mockjava/MockSchemaParser.java | 2 +- .../java/org/bdware/mockjava/MockUtil.java | 8 ++--- .../mockjava/generator/IntegerGenerator.java | 6 ++-- .../org/bdware/mockjava/MockUtilTest.java | 12 +++---- 6 files changed, 35 insertions(+), 26 deletions(-) diff --git a/build.gradle b/build.gradle index 9e60075..5fb62e1 100644 --- a/build.gradle +++ b/build.gradle @@ -5,6 +5,8 @@ plugins { id 'signing' } +apply from: '../spotless.gradle' + group 'org.bdware.mockjava' version '0.5.0' diff --git a/src/main/java/org/bdware/mockjava/MockGenerator.java b/src/main/java/org/bdware/mockjava/MockGenerator.java index 33190ee..5a3c7f2 100644 --- a/src/main/java/org/bdware/mockjava/MockGenerator.java +++ b/src/main/java/org/bdware/mockjava/MockGenerator.java @@ -12,24 +12,28 @@ import java.util.Map; public class MockGenerator extends JsonVisitor { public static MockGenerator instance = new MockGenerator(); - Map prefixToGenerator; + Map 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); } }; } diff --git a/src/main/java/org/bdware/mockjava/MockSchemaParser.java b/src/main/java/org/bdware/mockjava/MockSchemaParser.java index 82ae1c7..f7d14e6 100644 --- a/src/main/java/org/bdware/mockjava/MockSchemaParser.java +++ b/src/main/java/org/bdware/mockjava/MockSchemaParser.java @@ -28,4 +28,4 @@ public class MockSchemaParser extends JsonVisitor { } return this; } -} \ No newline at end of file +} diff --git a/src/main/java/org/bdware/mockjava/MockUtil.java b/src/main/java/org/bdware/mockjava/MockUtil.java index 68c52a6..a6e5d25 100644 --- a/src/main/java/org/bdware/mockjava/MockUtil.java +++ b/src/main/java/org/bdware/mockjava/MockUtil.java @@ -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()) { diff --git a/src/main/java/org/bdware/mockjava/generator/IntegerGenerator.java b/src/main/java/org/bdware/mockjava/generator/IntegerGenerator.java index 3900a32..54e2fd0 100644 --- a/src/main/java/org/bdware/mockjava/generator/IntegerGenerator.java +++ b/src/main/java/org/bdware/mockjava/generator/IntegerGenerator.java @@ -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 diff --git a/src/test/java/org/bdware/mockjava/MockUtilTest.java b/src/test/java/org/bdware/mockjava/MockUtilTest.java index 890e9fa..9dd6294 100644 --- a/src/test/java/org/bdware/mockjava/MockUtilTest.java +++ b/src/test/java/org/bdware/mockjava/MockUtilTest.java @@ -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'"));