mirror of
https://gitee.com/BDWare/MockJava
synced 2025-01-10 09:54:05 +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 {
|
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'
|
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 {
|
public class JsonUtil {
|
||||||
private static Set<Object> recorded = null;
|
private static Set<Object> recorded = null;
|
||||||
|
|
||||||
public static JsonElement toJson(jdk.nashorn.api.scripting.ScriptObjectMirror res) {
|
public static JsonElement toJson(jdk.nashorn.api.scripting.ScriptObjectMirror res) {
|
||||||
recorded = new HashSet<>();
|
recorded = new HashSet<>();
|
||||||
JsonElement jsonElement = copyInternal(res);
|
JsonElement jsonElement = copyInternal(res);
|
||||||
recorded.clear();
|
recorded.clear();
|
||||||
;
|
|
||||||
return jsonElement;
|
return jsonElement;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static JsonElement copyInternal(Object obj) {
|
private static JsonElement copyInternal(Object obj) {
|
||||||
if (recorded.contains(obj)) return JsonNull.INSTANCE;
|
if (recorded.contains(obj)) return JsonNull.INSTANCE;
|
||||||
if (obj == null) return JsonNull.INSTANCE;
|
if (obj == null) return JsonNull.INSTANCE;
|
||||||
|
@ -6,8 +6,16 @@ import com.google.gson.JsonObject;
|
|||||||
import com.google.gson.JsonPrimitive;
|
import com.google.gson.JsonPrimitive;
|
||||||
|
|
||||||
public abstract class JsonVisitor {
|
public abstract class JsonVisitor {
|
||||||
|
JsonElement result;
|
||||||
|
|
||||||
|
public JsonElement get() {
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
public JsonVisitor visit(JsonElement ele) {
|
public JsonVisitor visit(JsonElement ele) {
|
||||||
if (ele == null) return null;
|
if (null == ele) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
if (ele.isJsonArray()) {
|
if (ele.isJsonArray()) {
|
||||||
return visitJsonArray((JsonArray) ele);
|
return visitJsonArray((JsonArray) ele);
|
||||||
} else if (ele.isJsonObject()) {
|
} 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);
|
public abstract JsonVisitor visitJsonArray(JsonArray ele);
|
||||||
|
|
||||||
|
@ -1,33 +1,10 @@
|
|||||||
package org.bdware.mockjava;
|
package org.bdware.mockjava;
|
||||||
|
|
||||||
import com.google.gson.JsonArray;
|
import com.google.gson.JsonArray;
|
||||||
import com.google.gson.JsonElement;
|
|
||||||
import com.google.gson.JsonObject;
|
|
||||||
import com.google.gson.JsonPrimitive;
|
import com.google.gson.JsonPrimitive;
|
||||||
|
|
||||||
public class MockSchemaParser extends JsonVisitor {
|
public class MockSchemaParser extends JsonVisitor {
|
||||||
public static MockSchemaParser instance = new MockSchemaParser();
|
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
|
@Override
|
||||||
public JsonVisitor visitJsonArray(JsonArray array) {
|
public JsonVisitor visitJsonArray(JsonArray array) {
|
||||||
@ -46,8 +23,9 @@ public class MockSchemaParser extends JsonVisitor {
|
|||||||
result = new JsonPrimitive("@boolean");
|
result = new JsonPrimitive("@boolean");
|
||||||
} else if (primitive.isNumber()) {
|
} else if (primitive.isNumber()) {
|
||||||
result = new JsonPrimitive("@integer");
|
result = new JsonPrimitive("@integer");
|
||||||
}
|
} else {
|
||||||
result = new JsonPrimitive("@string");
|
result = new JsonPrimitive("@string");
|
||||||
|
}
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -20,7 +20,7 @@ public class MockUtil {
|
|||||||
// 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) {
|
||||||
// log.error("执行MockJs错误", e);
|
// log.error("执行MockJs错误", e);
|
||||||
@ -31,7 +31,7 @@ public class MockUtil {
|
|||||||
|
|
||||||
private static boolean isJson(String str) {
|
private static boolean isJson(String str) {
|
||||||
try {
|
try {
|
||||||
JsonObject jsonStr = JsonParser.parseString(str).getAsJsonObject();
|
JsonParser.parseString(str).getAsJsonObject();
|
||||||
return true;
|
return true;
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
return false;
|
return false;
|
||||||
@ -39,7 +39,7 @@ public class MockUtil {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public static Object mock(String template) {
|
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) {
|
if (template.length() > 0) {
|
||||||
try {
|
try {
|
||||||
//
|
//
|
||||||
|
Loading…
Reference in New Issue
Block a user