mirror of
https://gitee.com/BDWare/MockJava
synced 2025-01-10 09:54:05 +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'
|
||||||
|
|
||||||
|
@ -14,22 +14,26 @@ 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,7 +63,8 @@ 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;
|
||||||
|
@ -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()) {
|
||||||
|
@ -10,7 +10,9 @@ 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
|
||||||
|
Loading…
Reference in New Issue
Block a user