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:
root 2021-08-04 10:52:03 +08:00
commit 2ee4e6f7f7
5 changed files with 34 additions and 34 deletions

View File

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

View File

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

View File

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

View File

@ -1,37 +1,14 @@
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) {
JsonArray ret = new JsonArray(); JsonArray ret = new JsonArray();
if (array.size() > 0) { if (array.size() > 0) {
visit(array.get(0)); visit(array.get(0));
ret.add(result); ret.add(result);
@ -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;
} }
} }

View File

@ -19,8 +19,8 @@ public class MockUtil {
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) {
// 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 {
// //