Merge pull request 'feat : 删除测试类' (#108) from portal into master
Reviewed-on: http://git.feashow.cn/clay/fateverse/pulls/108
This commit is contained in:
@@ -1,32 +0,0 @@
|
||||
package cn.fateverse.query;
|
||||
|
||||
import java.io.*;
|
||||
|
||||
public class ConsoleCapture {
|
||||
public static String captureOutput(Runnable codeToRun) throws IOException {
|
||||
PrintStream originalOut = System.out;
|
||||
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
||||
PrintStream capturingOut = new PrintStream(baos);
|
||||
|
||||
try {
|
||||
System.setOut(capturingOut);
|
||||
codeToRun.run();
|
||||
} finally {
|
||||
System.setOut(originalOut);
|
||||
}
|
||||
|
||||
return baos.toString();
|
||||
}
|
||||
|
||||
// 使用示例
|
||||
public static void main(String[] args) throws IOException {
|
||||
String capturedText = captureOutput(() -> {
|
||||
System.out.println("Hello, Console!");
|
||||
System.out.println("This is being captured.");
|
||||
});
|
||||
|
||||
System.out.println("Captured output:");
|
||||
System.out.println(capturedText);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,16 +0,0 @@
|
||||
package cn.fateverse.query;
|
||||
|
||||
/**
|
||||
* @author Clay
|
||||
* @date 2024/4/22 18:59
|
||||
*/
|
||||
public class DataAdapter1100 {
|
||||
|
||||
public static Object execute(String data) {
|
||||
for (int i = 0; i < 1000; i++) {
|
||||
System.out.println(i);
|
||||
}
|
||||
return "qwewe";
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,77 +0,0 @@
|
||||
package cn.fateverse.query;
|
||||
|
||||
import cn.fateverse.common.code.engine.JavaScriptEngine;
|
||||
import cn.fateverse.common.code.model.EngineResult;
|
||||
import com.alibaba.fastjson2.JSONObject;
|
||||
import org.graalvm.polyglot.Context;
|
||||
import org.graalvm.polyglot.Value;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import javax.script.*;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.function.Predicate;
|
||||
|
||||
/**
|
||||
* @author Clay
|
||||
* @date 2024/4/23 9:42
|
||||
*/
|
||||
public class JavaScriptParamTest {
|
||||
|
||||
|
||||
static String finalCode = "function execute1(data) {\n" +
|
||||
" let res = [data.length];\n" +
|
||||
" for (let i = 0; i < data.length; i++) {\n" +
|
||||
" console.log(data[i])\n" +
|
||||
" console.log(data[i].foo)\n" +
|
||||
" res[i] = data[i].foo\n" +
|
||||
" }\n" +
|
||||
" return res;\n" +
|
||||
"}";
|
||||
|
||||
@Test
|
||||
public void testParam() {
|
||||
List<Object> list = new ArrayList<>();
|
||||
for (int i = 0; i < 10; i++) {
|
||||
|
||||
Map<String, Object> param = new HashMap<>();
|
||||
param.put("foo", "sdsdsdsds");
|
||||
list.add(param);
|
||||
}
|
||||
|
||||
ScriptEngineManager engineManager = new ScriptEngineManager();
|
||||
ScriptEngine jsEngine = engineManager.getEngineByName("graal.js");
|
||||
Context context = Context.newBuilder()
|
||||
.allowAllAccess(true)
|
||||
.build();
|
||||
// Context context = Context.create();
|
||||
context.eval("js", finalCode);
|
||||
|
||||
Value executeFunction = context.getBindings("js").getMember("execute1");
|
||||
Value javaObjectAsValue = Value.asValue(list);
|
||||
Value result = executeFunction.execute(javaObjectAsValue);
|
||||
System.out.println(result);
|
||||
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void testParam2() throws Exception {
|
||||
|
||||
ScriptEngineManager engineManager = new ScriptEngineManager();
|
||||
ScriptEngine jsEngine = engineManager.getEngineByName("graal.js");
|
||||
Invocable funcCall = (Invocable) jsEngine;
|
||||
Bindings bindings = jsEngine.getBindings(ScriptContext.ENGINE_SCOPE);
|
||||
bindings.put("polyglot.js.allowHostAccess", true);
|
||||
bindings.put("polyglot.js.allowHostClassLookup", (Predicate<String>) s -> true);
|
||||
|
||||
jsEngine.eval(finalCode);
|
||||
JSONObject jsonObject = new JSONObject();
|
||||
jsonObject.put("foo", "sdsdsdsds");
|
||||
Object res= funcCall.invokeFunction("execute1", jsonObject);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,122 +0,0 @@
|
||||
//package cn.fateverse.query;
|
||||
//
|
||||
//import cn.fateverse.common.code.config.JavaCodeProperties;
|
||||
//import cn.fateverse.common.code.engine.JavaCodeEngine;
|
||||
//import cn.fateverse.common.code.engine.JavaScriptEngine;
|
||||
//import cn.fateverse.common.code.model.EngineResult;
|
||||
//
|
||||
//import java.util.concurrent.FutureTask;
|
||||
//
|
||||
//
|
||||
///**
|
||||
// * @author Clay
|
||||
// * @date 2024/4/22 16:04
|
||||
// */
|
||||
//public class JavaScriptTest {
|
||||
// public static void main(String[] args) throws Exception {
|
||||
// String code = "function execute(data) {\n" +
|
||||
// " console.log(data)\n" +
|
||||
// " console.log(\"test console!\")\n" +
|
||||
// " let add = (a, b) => a + b; // 使用箭头函数\n" +
|
||||
// " console.log(\"Adding 5 and 3:\", add(5, 3));\n\n" +
|
||||
// " for (let i = 0; i < 1000; i++) {\n" +
|
||||
// " console.log(i)\n" +
|
||||
// " }" +
|
||||
// " return \"success\";\n" +
|
||||
// "}";
|
||||
//
|
||||
//
|
||||
// JavaCodeProperties properties = new JavaCodeProperties();
|
||||
// properties.setClassPath("C:\\home\\clay\\code\\");
|
||||
// JavaCodeEngine javaCodeEngine = new JavaCodeEngine(properties);
|
||||
//
|
||||
//
|
||||
//// EngineResult<Object> executeScript = JavaScriptEngine.executeScript(code, "execute", "测试控制台输出");
|
||||
//
|
||||
// String finalCode = "function execute(data) {\n" +
|
||||
// " console.log(data)\n" +
|
||||
// " console.log(\"test console!\")\n" +
|
||||
// " let add = (a, b) => a + b; // 使用箭头函数\n" +
|
||||
// " console.log(\"Adding 5 and 3:\", add(5, 3));\n\n" +
|
||||
// " for (let i = 0; i < 1000; i++) {\n" +
|
||||
// " console.log(i)\n" +
|
||||
// " }" +
|
||||
// " return \"success\";\n" +
|
||||
// "}";
|
||||
// FutureTask<EngineResult> engineResultFutureTask = new FutureTask<>(() -> {
|
||||
// return JavaScriptEngine.execute(finalCode, "execute", false, "测试控制台输出");
|
||||
// });
|
||||
//
|
||||
//
|
||||
// String finalCode1 = "import java.util.*;\n" +
|
||||
// "import java.util.stream.*;\n" +
|
||||
// "\n" +
|
||||
// "public class DataAdapter100 {" +
|
||||
// "\n" +
|
||||
// " public static Object execute(String data) {\n" +
|
||||
// " for (int i = 0; i < 1000; i++) {\n" +
|
||||
// " System.out.println(i);\n" +
|
||||
// " }\n" +
|
||||
// " return \"qwewe\";\n" +
|
||||
// " }" +
|
||||
// "}\n";
|
||||
// FutureTask<EngineResult> engineResultFutureTask1 = new FutureTask<>(() -> {
|
||||
// return javaCodeEngine.execute(finalCode1, "DataAdapter100", "execute", new Class[]{String.class}, new Object[]{"test"}, true);
|
||||
// });
|
||||
// new Thread(engineResultFutureTask).start();
|
||||
// new Thread(engineResultFutureTask1).start();
|
||||
//// EngineResult<Object> execute = javaCodeEngine.execute(code, "DataAdapter100", "execute", new Class[]{String.class}, new Object[]{"test"}, true);
|
||||
//
|
||||
//
|
||||
// EngineResult x = engineResultFutureTask.get();
|
||||
// EngineResult x1 = engineResultFutureTask1.get();
|
||||
// System.out.println(x);
|
||||
// System.out.println(x1);
|
||||
//
|
||||
//
|
||||
//// EngineResult<Object> capture = MultiThreadedCapture.capture(() -> {
|
||||
//// ScriptEngine engine = new ScriptEngineManager().getEngineByName("graal.js");
|
||||
//// Invocable inv = (Invocable) engine;
|
||||
//// try {
|
||||
//// engine.eval(finalCode);
|
||||
//// return inv.invokeFunction("execute", "args");
|
||||
//// } catch (ScriptException | NoSuchMethodException e) {
|
||||
//// throw new RuntimeException(e);
|
||||
//// }
|
||||
//// });
|
||||
//// System.out.println(capture);
|
||||
//// EngineResult<Object> objectEngineResult = JavaScriptEngine.executeScript(finalCode, "execute", "测试控制台输出");
|
||||
////
|
||||
//// ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
||||
//// PrintStream oldOut = System.out;
|
||||
//// System.setOut(new PrintStream(baos));
|
||||
//
|
||||
//
|
||||
//// EngineResult<Object> capture = MultiThreadedCapture.capture(() -> {
|
||||
//// int someNumber = 42;
|
||||
//// System.out.println(someNumber);
|
||||
//// return null;
|
||||
//// });
|
||||
//
|
||||
//
|
||||
// // 执行产生数值输出的代码
|
||||
//// System.out.println(objectEngineResult);
|
||||
//// ScriptEngine engine = JavaScriptEngine.getEngine();
|
||||
//// Invocable inv = (Invocable) engine;
|
||||
//// engine.eval(finalCode);
|
||||
//// inv.invokeFunction("execute", "args");
|
||||
//// System.out.println(capture);
|
||||
//
|
||||
//// MultiThreadedCapture.executor.shutdown();
|
||||
// // 恢复标准输出流
|
||||
//// System.setOut(oldOut);
|
||||
////
|
||||
//// System.setOut(oldOut);
|
||||
//// // 从捕获的字节数组输出流中获取打印的文本
|
||||
//// String capturedOutput = baos.toString();
|
||||
//// System.out.println("Captured output: " + capturedOutput);
|
||||
//
|
||||
// }
|
||||
//
|
||||
//
|
||||
//}
|
||||
@@ -1,38 +0,0 @@
|
||||
package cn.fateverse.query;
|
||||
|
||||
import org.graalvm.polyglot.Context;
|
||||
import org.graalvm.polyglot.Value;
|
||||
|
||||
public class Main {
|
||||
public static void main(String[] args) {
|
||||
// 创建 GraalVM 上下文
|
||||
Context context = Context.create();
|
||||
|
||||
// 定义一个 JavaScript 函数
|
||||
String script = "function execute(data) {\n" +
|
||||
" console.log(data)\n" +
|
||||
" console.log(\"test console!\")\n" +
|
||||
" let add = (a, b) => a + b; // 使用箭头函数\n" +
|
||||
" console.log(\"Adding 5 and 3:\", add(5, 3));\n\n" +
|
||||
" for (let i = 0; i < 1000; i++) {\n" +
|
||||
" console.log(i)\n" +
|
||||
" }" +
|
||||
" return \"success\";\n" +
|
||||
"}";
|
||||
|
||||
// 编译 JavaScript 函数
|
||||
context.eval("js", script);
|
||||
|
||||
// 获取函数对象
|
||||
Value addFunction = context.getBindings("js").getMember("execute");
|
||||
|
||||
// 执行函数并传入参数
|
||||
Value result = addFunction.execute("测试");
|
||||
Value result2 = addFunction.execute("测试");
|
||||
// 获取返回值
|
||||
// int sum = result.asInt();
|
||||
|
||||
// 输出结果
|
||||
System.out.println("Sum: "); // 输出:Sum: 5
|
||||
}
|
||||
}
|
||||
@@ -1,45 +0,0 @@
|
||||
package cn.fateverse.query;
|
||||
|
||||
import cn.fateverse.common.code.engine.JavaScriptEngine;
|
||||
import cn.fateverse.common.code.model.EngineResult;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.PrintStream;
|
||||
|
||||
public class SystemOut {
|
||||
public static void outTest() {
|
||||
ByteArrayOutputStream baoStream = new ByteArrayOutputStream(1024);
|
||||
PrintStream cacheStream = new PrintStream(baoStream);//临时输出
|
||||
PrintStream oldStream = System.out;//缓存系统输出
|
||||
System.setOut(cacheStream);
|
||||
System.out.print("...");//不会打印到控制台
|
||||
String result = baoStream.toString();//存放控制台输出的字符串
|
||||
System.setOut(oldStream);//还原到系统输出
|
||||
System.out.println("result");
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
// outTest();
|
||||
|
||||
String finalCode = "function execute1(data) {\n" +
|
||||
" console.log(data)\n" +
|
||||
" console.log(\"test console!\")\n" +
|
||||
" let add = (a, b) => a + b; // 使用箭头函数\n" +
|
||||
" console.log(\"Adding 5 and 3:\", add(5, 3));\n\n" +
|
||||
" for (let i = 0; i < 1000; i++) {\n" +
|
||||
" console.log(i)\n" +
|
||||
" }" +
|
||||
" return \"success\";\n" +
|
||||
"}";
|
||||
// String finalCode = "function myFunction() { return 'Hello, world!'; }";
|
||||
long start1 = System.currentTimeMillis();
|
||||
EngineResult execute = JavaScriptEngine.execute(finalCode, "execute1", false, "测试控制台输出");
|
||||
long start2 = System.currentTimeMillis();
|
||||
EngineResult execute1 = JavaScriptEngine.execute(finalCode, "execute1", false, "测试控制台输出");
|
||||
long start3 = System.currentTimeMillis();
|
||||
System.out.println("耗时1:" + (start2 - start1) + "ms");
|
||||
System.out.println("耗时2:" + (start3 - start2) + "ms");
|
||||
System.out.println(execute);
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user