Skip to content

Commit 746cdad

Browse files
test: Add nashorn test (#1898)
* test: Add nashorn test Closes #1895 Signed-off-by: unknowIfGuestInDream <liang.tang.cx@gmail.com> * test: Add nashorn test Closes #1895 Signed-off-by: unknowIfGuestInDream <liang.tang.cx@gmail.com> --------- Signed-off-by: unknowIfGuestInDream <liang.tang.cx@gmail.com>
1 parent 517f5ca commit 746cdad

File tree

23 files changed

+1126
-2
lines changed

23 files changed

+1126
-2
lines changed

.gitattributes

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@
2121
*.bat linguist-language=Shell
2222
*.vbs linguist-language=Shell
2323
frame/src/main/resources/com/tlcsdm/frame/static/css/* linguist-vendored
24-
docs/** linguist-documentation
24+
core/src/test/resources/nashorn/*.js linguist-vendored
25+
docs/** linguist-documentation
2526

2627
# Ensure the following are treated as binary.
2728
# # (binary is a macro for -text -diff)

core/pom.xml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -433,6 +433,16 @@
433433
<artifactId>javapoet</artifactId>
434434
<scope>test</scope>
435435
</dependency>
436+
<dependency>
437+
<groupId>org.openjdk.nashorn</groupId>
438+
<artifactId>nashorn-core</artifactId>
439+
<scope>provided</scope>
440+
</dependency>
441+
<dependency>
442+
<groupId>org.jruby</groupId>
443+
<artifactId>jruby-core</artifactId>
444+
<scope>test</scope>
445+
</dependency>
436446
</dependencies>
437447

438448
<build>

core/src/main/java/module-info.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@
8787
requires static net.jonathangiles.tools.teenyhttpd;
8888
requires org.reflections;
8989
requires static org.apache.commons.csv;
90+
requires static org.openjdk.nashorn;
9091

9192
opens com.tlcsdm.core.javafx.controller to javafx.fxml;
9293
opens com.tlcsdm.core.javafx.view to javafx.fxml;

core/src/test/java/com/tlcsdm/core/util/User.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,4 +53,13 @@ public String getName() {
5353
public void setName(String name) {
5454
this.name = name;
5555
}
56+
57+
public static String sayHello(String name) {
58+
System.out.println("Hello " + name);
59+
return "Hi!";
60+
}
61+
62+
public static void fun(Object obj) {
63+
System.out.println(obj.getClass());
64+
}
5665
}
Lines changed: 178 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,178 @@
1+
/*
2+
* Copyright (c) 2024 unknowIfGuestInDream.
3+
* All rights reserved.
4+
*
5+
* Redistribution and use in source and binary forms, with or without
6+
* modification, are permitted provided that the following conditions are met:
7+
* * Redistributions of source code must retain the above copyright
8+
* notice, this list of conditions and the following disclaimer.
9+
* * Redistributions in binary form must reproduce the above copyright
10+
* notice, this list of conditions and the following disclaimer in the
11+
* documentation and/or other materials provided with the distribution.
12+
* * Neither the name of unknowIfGuestInDream, any associated website, nor the
13+
* names of its contributors may be used to endorse or promote products
14+
* derived from this software without specific prior written permission.
15+
*
16+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
17+
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18+
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19+
* DISCLAIMED. IN NO EVENT SHALL UNKNOWIFGUESTINDREAM BE LIABLE FOR ANY
20+
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21+
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22+
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23+
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24+
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25+
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26+
*/
27+
28+
package com.tlcsdm.core.util.nashorn;
29+
30+
import cn.hutool.core.io.resource.ResourceUtil;
31+
import org.junit.jupiter.api.Test;
32+
33+
import javax.script.Compilable;
34+
import javax.script.CompiledScript;
35+
import javax.script.Invocable;
36+
import javax.script.ScriptEngine;
37+
import javax.script.ScriptEngineFactory;
38+
import javax.script.ScriptEngineManager;
39+
import javax.script.ScriptException;
40+
import java.io.IOException;
41+
import java.io.InputStreamReader;
42+
43+
/**
44+
* @author unknowIfGuestInDream
45+
*/
46+
public class NashornDemoTest {
47+
48+
@Test
49+
public void bind() throws ScriptException {
50+
ScriptEngineManager engineManager = new ScriptEngineManager();
51+
ScriptEngine engine = engineManager.getEngineByName("Nashorn");
52+
53+
engine.put("name", "Rahman");
54+
engine.put("surname", "Usta");
55+
56+
String fullName = engine.get("name") + " " + engine.get("surname");
57+
58+
engine.put("fullName", fullName);
59+
60+
engine.eval("var person={};");
61+
engine.eval("person.name=name;");
62+
engine.eval("person.surname=surname;");
63+
64+
engine.eval("print(JSON.stringify(person));");
65+
66+
engine.put("age", 26);
67+
engine.eval("person.age=age; person.fullName=fullName");
68+
69+
engine.eval("print(JSON.stringify(person));");
70+
}
71+
72+
@Test
73+
public void compilable() throws ScriptException {
74+
ScriptEngine engine = new ScriptEngineManager().getEngineByName("Nashorn");
75+
76+
Compilable compilable = (Compilable) engine;
77+
78+
CompiledScript compiled =
79+
compilable.compile("var calculate=function(one,two){ return (one*two); }");
80+
81+
Object mul = null;
82+
try {
83+
mul = engine.eval("calculate(20,30)");
84+
} catch (ScriptException se) {
85+
System.out.println(se.getMessage());
86+
compiled.eval();
87+
88+
mul = engine.eval("calculate(5,10)");
89+
}
90+
91+
System.out.println(mul);
92+
}
93+
94+
@Test
95+
public void engines() throws ScriptException {
96+
ScriptEngineManager mgr = new ScriptEngineManager();
97+
98+
for (ScriptEngineFactory factory : mgr.getEngineFactories()) {
99+
System.out.println("ScriptEngineFactory Info");
100+
System.out.printf("\tScript Engine: %s (%s)\n", factory.getEngineName(), factory.getEngineVersion());
101+
System.out.printf("\tLanguage: %s (%s)\n", factory.getLanguageName(), factory.getLanguageVersion());
102+
for (String name : factory.getNames()) {
103+
System.out.printf("\tEngine Alias: %s\n", name);
104+
}
105+
}
106+
}
107+
108+
@Test
109+
public void eval() throws ScriptException {
110+
ScriptEngineManager engineManager = new ScriptEngineManager();
111+
ScriptEngine engine = engineManager.getEngineByName("Nashorn");
112+
113+
engine.eval("var person= new Object();");
114+
engine.eval("person.name='Rahman';");
115+
engine.eval("person.surname='Usta';");
116+
117+
engine.eval("print(JSON.stringify(person));");
118+
119+
engine.eval("person.age=26;");
120+
121+
engine.eval("print(JSON.stringify(person));");
122+
}
123+
124+
@Test
125+
public void func() throws ScriptException {
126+
ScriptEngine engine = new ScriptEngineManager().getEngineByName("Nashorn");
127+
128+
engine.eval("var person={};");
129+
engine.eval("person.name='Rahman';");
130+
engine.eval("person.surname='Usta';");
131+
engine.eval("person.fullName=function(){return this.name+' '+this.surname};");
132+
133+
engine.eval("print('FullName: '+person.fullName());");
134+
}
135+
136+
@Test
137+
public void geo() throws ScriptException, IOException {
138+
ScriptEngineManager engineManager = new ScriptEngineManager();
139+
ScriptEngine engine = engineManager.getEngineByName("js");
140+
engine.eval(new InputStreamReader(ResourceUtil.getResource("nashorn/geo.js").openStream()));
141+
}
142+
143+
@Test
144+
public void invokable() throws ScriptException, NoSuchMethodException {
145+
ScriptEngine engine = new ScriptEngineManager().getEngineByName("Nashorn");
146+
147+
engine.eval("var person={};");
148+
engine.eval("person.name='Rahman';");
149+
engine.eval("person.surname='Usta';");
150+
151+
engine.eval("person.calculate=function(age){return this.name+':'+this.surname+':'+age};");
152+
engine.eval("calculate=function(one,two){ return (one*two); }");
153+
154+
Invocable inv = (Invocable) engine;
155+
156+
Object person = engine.get("person");
157+
158+
Object calculate = inv.invokeMethod(person, "calculate", 26);
159+
160+
System.out.println(calculate);
161+
162+
System.out.println(inv.invokeFunction("calculate", 5, 4));
163+
}
164+
165+
@Test
166+
public void javatype() throws ScriptException, IOException {
167+
ScriptEngine engine = new ScriptEngineManager().getEngineByName("js");
168+
engine.eval(new InputStreamReader(ResourceUtil.getResource("nashorn/javatype.js").openStream()));
169+
}
170+
171+
@Test
172+
public void jruby() throws ScriptException, IOException {
173+
ScriptEngineManager engineManager = new ScriptEngineManager();
174+
ScriptEngine engine = engineManager.getEngineByName("ruby");
175+
engine.eval(new InputStreamReader(ResourceUtil.getResource("nashorn/jruby.rb").openStream()));
176+
}
177+
178+
}

0 commit comments

Comments
 (0)