Skip to content

Commit

Permalink
[update], 支持java使用mockjs生成mock数据源
Browse files Browse the repository at this point in the history
  • Loading branch information
dingpei committed Feb 27, 2024
1 parent 58f8807 commit 5978602
Show file tree
Hide file tree
Showing 6 changed files with 8,727 additions and 0 deletions.
25 changes: 25 additions & 0 deletions jcommon/mockjs/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>jcommon</artifactId>
<groupId>run.mone</groupId>
<version>1.4-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>mockjs</artifactId>

<dependencies>

<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.12.0</version>
</dependency>

</dependencies>


</project>
101 changes: 101 additions & 0 deletions jcommon/mockjs/src/main/java/run/mone/mock/MockJsUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
package run.mone.mock;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Supplier;
import java.util.stream.IntStream;

@Slf4j
public class MockJsUtils {
/**
* Javascript执行引擎
*/
public static final ScriptEngine MOCK_JS_ENGINE;
/**
* mockjs的资源路径
*/
private static final String MOCK_JS_PATH = "js/mock-min.js";

static {
MOCK_JS_ENGINE = new ScriptEngineManager().getEngineByName("js");
try (
InputStream mockJs = MockJsUtils.class.getClassLoader().getResourceAsStream(MOCK_JS_PATH);
InputStreamReader reader = new InputStreamReader(mockJs)
) {
MOCK_JS_ENGINE.eval(reader);
} catch (ScriptException | IOException e) {
log.error("执行MockJs错误", e);
} catch (Throwable e) {
log.error("内部错误", e);
}
}

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

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

return null;
}

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

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

int n = (number / batchNumber) + 1;

for (int j = 0; j <= n; j++) {
int defaultNumber = batchNumber;
IntStream.range(0, defaultNumber)
.parallel() // 将流转换为并行流
.forEach(i -> {
// 这里是要并行执行的操作
String mockData = supplier.get();
res.add(mockData);
});
}

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

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

}
10 changes: 10 additions & 0 deletions jcommon/mockjs/src/main/resources/js/mock-min.js

Large diffs are not rendered by default.

Loading

0 comments on commit 5978602

Please sign in to comment.