build: config spotless plugin and reformat code

This commit is contained in:
Frank.R.Wu 2023-06-15 11:08:07 +08:00
parent 7447d5a079
commit 97ddefab68
6 changed files with 35 additions and 26 deletions

View File

@ -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'

View File

@ -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);
} }
}; };
} }

View File

@ -18,8 +18,7 @@ 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) {
@ -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()) {

View File

@ -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

View File

@ -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}