Skip to content

Commit

Permalink
update jcommon mockjs (#799)
Browse files Browse the repository at this point in the history
  • Loading branch information
IiiinSea authored Mar 1, 2024
2 parents e4b866e + 394e232 commit b422e6c
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 40 deletions.
32 changes: 7 additions & 25 deletions jcommon/mockjs/src/main/java/run/mone/mock/MockJsUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public static String mock(String template) {
template = StringUtils.trimToEmpty(template);

try {
String result = MOCK_JS_ENGINE.eval("JSON.stringify(Mock.mock(" + template + "))").toString();
String result = MOCK_JS_ENGINE.eval("JSON.stringify(" + template + ")").toString();
return result;
} catch (Throwable e) {
log.error("执行Mock.mock错误", e);
Expand All @@ -53,14 +53,12 @@ public static String mock(String template) {
}

public static List<String> batchMock(String template, int number) {
return batchOperation(() -> mock(template), number);
return batchOperation(new ArrayList<>(), () -> mock(template), number);
}

public static List<String> batchOperation(Supplier<String> supplier, int number) {
public static List<String> batchOperation(List<String> res, Supplier<String> supplier, int number) {
int batchNumber = 1000;
List<String> res = new ArrayList<>();

int n = (number / batchNumber) + 1;
int n = ((number - res.size()) / batchNumber) + 1;

for (int j = 0; j <= n; j++) {
int defaultNumber = batchNumber;
Expand All @@ -73,29 +71,13 @@ public static List<String> batchOperation(Supplier<String> supplier, int number)
});
}

if (res.size() > number) {
if (res.size() >= number) {
List<String> res1 = res.subList(0, number);
return res1;
} else {
return batchOperation(res, supplier, number);
}

return res;
}

public static String random(String template) {
template = StringUtils.trimToEmpty(template);

try {
String result = MOCK_JS_ENGINE.eval("JSON.stringify(Mock." + template + ")").toString();
return result;
} catch (Throwable e) {
log.error("执行Mock.mock错误", e);
}

return null;
}

public static List<String> batchRandom(String template, int number) {
return batchOperation(() -> random(template), number);
}

}
34 changes: 20 additions & 14 deletions jcommon/mockjs/src/test/java/run/mone/mock/test/MockTest.java
Original file line number Diff line number Diff line change
@@ -1,45 +1,51 @@
package run.mone.mock.test;

import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test;
import run.mone.mock.MockJsUtils;

import javax.script.ScriptEngine;
import java.util.List;

@Ignore
public class MockTest {

@Test
public void mockjsTest() {
String input = "{\n" +
" \"boolean|1\": true\n" +
"}";
String input = "Mock.mock({\n" +
" \"number|1-100.1-10\": 1\n" +
"})";
long begin = System.currentTimeMillis();
String output = MockJsUtils.mock(input);
System.out.println(output);
long costtime = System.currentTimeMillis() - begin;
System.out.println(output + " ====== cost time " + costtime);
}

@Test
public void mockjsTestBatch() {
String input = "{\n" +
" \"boolean|1\": true\n" +
"}";
List<String> output = MockJsUtils.batchMock(input, 10000);
String input = "Mock.mock({\n" +
" \"number|1-100.1-10\": 1\n" +
"})";
long begin = System.currentTimeMillis();
List<String> output = MockJsUtils.batchMock(input, 50000);
long costtime = System.currentTimeMillis() - begin;
System.out.println(output);
}

@Test
public void mockjsTestRandom() {
String input = "Random.title(3, 5)";
String output = MockJsUtils.random(input);
String input = "Mock.Random.title(3, 5)";
long begin = System.currentTimeMillis();
String output = MockJsUtils.mock(input);
long costtime = System.currentTimeMillis() - begin;
System.out.println(output);
}

@Test
public void mockjsTestRandomBatch() {
String input = "Random.title(3, 5)";
List<String> output = MockJsUtils.batchRandom(input, 9999);
String input = "Mock.Random.title(3, 5)";
long begin = System.currentTimeMillis();
List<String> output = MockJsUtils.batchMock(input, 9999);
long costtime = System.currentTimeMillis() - begin;
System.out.println(output);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ public enum SysFuncEnum {
//报警级别
SUBSTRING(FUNC_NAME_SUBSTRING, "common", "截取字符串", "${java.substring(ceshi, 1, 3)}",
"返回从指定开始索引到结束索引之间的子字符串,但不包括结束索引位置的字符"),
UUID(FUNC_NAME_UUID, "common", "唯一标识符", "{java.uuid()}",
UUID(FUNC_NAME_UUID, "common", "唯一标识符", "${java.uuid()}",
"标准化的唯一标识符"),
RANDOM_NUMBER(FUNC_NAME_RANDOM_NUMBER, "common", "随机数", "${java.randomNumber(2, 11)}",
"生成指定范围的整型数字");
Expand Down

0 comments on commit b422e6c

Please sign in to comment.