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'
}
apply from: '../spotless.gradle'
group 'org.bdware.mockjava'
version '0.5.0'

View File

@ -14,22 +14,26 @@ public class MockGenerator extends JsonVisitor {
public static MockGenerator instance = new MockGenerator();
Map<String, ValueGenerator> prefixToGenerator;
JsonElement result;
public JsonElement get() {
return result;
}
public MockGenerator(Class... clzs) {
prefixToGenerator = new HashMap<>();
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,7 +63,8 @@ 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);
return this;

View File

@ -18,8 +18,7 @@ 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);
try (InputStream mockJs = MockUtil.class.getClassLoader().getResourceAsStream(MOCK_JS_PATH);
InputStreamReader reader = new InputStreamReader(mockJs)) {
MOCK_JS_ENGINE.eval(reader);
} catch (ScriptException | IOException 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()) {

View File

@ -10,7 +10,9 @@ import java.util.Random;
@Generator(tag = "@integer")
public class IntegerGenerator extends ValueGenerator {
public IntegerGenerator() {}
static Random secureRandom = new SecureRandom();
@Override
public JsonElement generate(JsonElement previous) {
// TODO parse value here