mirror of
https://gitee.com/BDWare/MockJava
synced 2025-01-10 01:44:06 +00:00
Merge remote-tracking branch 'origin/master'
# Conflicts: # src/main/java/org/bdware/mockjava/MockGenerator.java # src/main/java/org/bdware/mockjava/ValueGenerator.java # src/main/java/org/bdware/mockjava/generator/IntegerGenerator.java
This commit is contained in:
commit
2ee4e6f7f7
@ -36,7 +36,7 @@ repositories {
|
||||
}
|
||||
|
||||
dependencies {
|
||||
api 'com.google.code.gson:gson:2.8.6'
|
||||
api 'com.google.code.gson:gson:2.8.7'
|
||||
api 'log4j:log4j:1.2.17'
|
||||
testImplementation 'junit:junit:4.12'
|
||||
testImplementation 'junit:junit:4.13.2'
|
||||
}
|
||||
|
@ -8,13 +8,14 @@ import java.util.Set;
|
||||
|
||||
public class JsonUtil {
|
||||
private static Set<Object> recorded = null;
|
||||
|
||||
public static JsonElement toJson(jdk.nashorn.api.scripting.ScriptObjectMirror res) {
|
||||
recorded = new HashSet<>();
|
||||
JsonElement jsonElement = copyInternal(res);
|
||||
recorded.clear();
|
||||
;
|
||||
return jsonElement;
|
||||
}
|
||||
|
||||
private static JsonElement copyInternal(Object obj) {
|
||||
if (recorded.contains(obj)) return JsonNull.INSTANCE;
|
||||
if (obj == null) return JsonNull.INSTANCE;
|
||||
|
@ -6,8 +6,16 @@ import com.google.gson.JsonObject;
|
||||
import com.google.gson.JsonPrimitive;
|
||||
|
||||
public abstract class JsonVisitor {
|
||||
JsonElement result;
|
||||
|
||||
public JsonElement get() {
|
||||
return result;
|
||||
}
|
||||
|
||||
public JsonVisitor visit(JsonElement ele) {
|
||||
if (ele == null) return null;
|
||||
if (null == ele) {
|
||||
return null;
|
||||
}
|
||||
if (ele.isJsonArray()) {
|
||||
return visitJsonArray((JsonArray) ele);
|
||||
} else if (ele.isJsonObject()) {
|
||||
@ -18,7 +26,20 @@ public abstract class JsonVisitor {
|
||||
}
|
||||
}
|
||||
|
||||
public abstract JsonVisitor visitObject(JsonObject asJsonObject);
|
||||
public JsonVisitor visitObject(JsonObject jsonObject) {
|
||||
JsonObject jo = new JsonObject();
|
||||
for (String key : jsonObject.keySet()) {
|
||||
JsonElement origin = jsonObject.get(key);
|
||||
visit(origin);
|
||||
JsonElement je = get();
|
||||
if (origin.isJsonArray()) {
|
||||
key += "|" + Math.min(((JsonArray) origin).size(), 20);
|
||||
}
|
||||
jo.add(key, je);
|
||||
}
|
||||
result = jo;
|
||||
return this;
|
||||
}
|
||||
|
||||
public abstract JsonVisitor visitJsonArray(JsonArray ele);
|
||||
|
||||
|
@ -1,37 +1,14 @@
|
||||
package org.bdware.mockjava;
|
||||
|
||||
import com.google.gson.JsonArray;
|
||||
import com.google.gson.JsonElement;
|
||||
import com.google.gson.JsonObject;
|
||||
import com.google.gson.JsonPrimitive;
|
||||
|
||||
public class MockSchemaParser extends JsonVisitor {
|
||||
public static MockSchemaParser instance = new MockSchemaParser();
|
||||
JsonElement result;
|
||||
|
||||
public JsonElement get() {
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public JsonVisitor visitObject(JsonObject jsonObject) {
|
||||
JsonObject jo = new JsonObject();
|
||||
for (String key : jsonObject.keySet()) {
|
||||
JsonElement origin = jsonObject.get(key);
|
||||
visit(origin);
|
||||
JsonElement je = get();
|
||||
if (origin.isJsonArray()) {
|
||||
key += "|" + Math.min(((JsonArray) origin).size(), 20);
|
||||
}
|
||||
jo.add(key, je);
|
||||
}
|
||||
result = jo;
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public JsonVisitor visitJsonArray(JsonArray array) {
|
||||
JsonArray ret = new JsonArray();
|
||||
JsonArray ret = new JsonArray();
|
||||
if (array.size() > 0) {
|
||||
visit(array.get(0));
|
||||
ret.add(result);
|
||||
@ -46,8 +23,9 @@ public class MockSchemaParser extends JsonVisitor {
|
||||
result = new JsonPrimitive("@boolean");
|
||||
} else if (primitive.isNumber()) {
|
||||
result = new JsonPrimitive("@integer");
|
||||
} else {
|
||||
result = new JsonPrimitive("@string");
|
||||
}
|
||||
result = new JsonPrimitive("@string");
|
||||
return this;
|
||||
}
|
||||
}
|
@ -19,8 +19,8 @@ public class MockUtil {
|
||||
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); ) {
|
||||
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);
|
||||
@ -31,7 +31,7 @@ public class MockUtil {
|
||||
|
||||
private static boolean isJson(String str) {
|
||||
try {
|
||||
JsonObject jsonStr = JsonParser.parseString(str).getAsJsonObject();
|
||||
JsonParser.parseString(str).getAsJsonObject();
|
||||
return true;
|
||||
} catch (Exception e) {
|
||||
return false;
|
||||
@ -39,7 +39,7 @@ public class MockUtil {
|
||||
}
|
||||
|
||||
public static Object mock(String template) {
|
||||
Object result = (ScriptObjectMirror) ScriptObjectMirror.wrap(null, Context.getGlobal());
|
||||
Object result = ScriptObjectMirror.wrap(null, Context.getGlobal());
|
||||
if (template.length() > 0) {
|
||||
try {
|
||||
//
|
||||
|
Loading…
Reference in New Issue
Block a user