├── .bowerrc ├── .editorconfig ├── .gitignore ├── LICENSE ├── README.md ├── bower.json ├── pom.xml └── src ├── main ├── java │ └── de │ │ └── bripkens │ │ └── nashorn │ │ └── Runner.java ├── javascript │ ├── env.nashorn.1.2.js │ └── event_loop.js └── resources │ └── logback.xml └── test ├── java └── de │ └── bripkens │ └── nashorn │ ├── AbstractEventLoopTest.java │ ├── AbstractNashornTest.java │ ├── AsyncHttpClientTest.java │ ├── EngineTest.java │ ├── EnvJsTest.java │ ├── EventLoopTest.java │ ├── StrengthCalculatorWithoutEventLoopTest.java │ └── XmlHttpRequestTest.java ├── javascript ├── shouldClearTimeout.js ├── shouldCompileCode.js ├── shouldExecuteAsPartOfEventLoop.js ├── shouldHandleSimpleGetRequests.js ├── shouldInterleaveExecutions.js └── shouldSupportIntervals.js └── resources └── logback-test.xml /.bowerrc: -------------------------------------------------------------------------------- 1 | { 2 | "directory": "src/main/javascript/bower_components" 3 | } -------------------------------------------------------------------------------- /.editorconfig: -------------------------------------------------------------------------------- 1 | root = true 2 | 3 | [*] 4 | end_of_line = lf 5 | insert_final_newline = true 6 | indent_style = space 7 | indent_size = 2 8 | trim_trailing_whitespace = true 9 | charset = utf-8 10 | 11 | [*.properties] 12 | charset = latin1 13 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | target/ 2 | bower_components/ 3 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Java 8 Nashorn Examples 2 | 3 | This repository contains some Java 8 Nashorn examples, tests and a small 4 | event loop polyfill. Many of the examples in this repository were the basis 5 | for my [enterJS talk](http://www.enterjs.de/abstracts#javascript-und-java-kombinieren-polyglotte-programmierung-auf-jvm). 6 | 7 | You can learn more about Java 8 and project Nashorn in my 8 | [upcoming article](https://blog.codecentric.de/en/2014/06/project-nashorn-javascript-jvm-polyglott/) 9 | about code sharing between JavaScript and Java. 10 | 11 | ## Setup 12 | 13 | ``` 14 | npm install -g bower 15 | bower install 16 | mvn clean package 17 | ``` -------------------------------------------------------------------------------- /bower.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "nashorn-examples", 3 | "version": "0.1.0-SNAPSHOT", 4 | "homepage": "https://github.com/bripkens/java-with-javascript", 5 | "authors": [ 6 | "Ben Ripkens " 7 | ], 8 | "moduleType": [ 9 | "globals" 10 | ], 11 | "license": "MIT", 12 | "private": true, 13 | "ignore": [ 14 | "**/.*", 15 | "node_modules", 16 | "bower_components", 17 | "test", 18 | "tests" 19 | ], 20 | "dependencies": { 21 | "zxcvbn": "dropbox/zxcvbn" 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 4 | 4.0.0 5 | 6 | de.bripkens 7 | nashorn-examples 8 | 0.1.0-SNAPSHOT 9 | jar 10 | 11 | nashorn-examples 12 | http://example.com 13 | 14 | 15 | UTF-8 16 | 1.8 17 | 1.3 18 | 19 | 20 | 21 | 22 | com.google.guava 23 | guava 24 | 17.0 25 | 26 | 27 | commons-io 28 | commons-io 29 | 2.4 30 | 31 | 32 | com.ning 33 | async-http-client 34 | 1.8.9 35 | 36 | 37 | 38 | ch.qos.logback 39 | logback-classic 40 | 1.1.2 41 | 42 | 43 | 44 | junit 45 | junit 46 | 4.11 47 | test 48 | 49 | 50 | org.hamcrest 51 | hamcrest-core 52 | 53 | 54 | 55 | 56 | org.hamcrest 57 | hamcrest-core 58 | ${hamcrest-version} 59 | test 60 | 61 | 62 | org.hamcrest 63 | hamcrest-library 64 | ${hamcrest-version} 65 | test 66 | 67 | 68 | 69 | 70 | 71 | 72 | src/main/javascript 73 | 74 | 75 | src/main/resources 76 | 77 | 78 | 79 | 80 | src/test/javascript 81 | 82 | 83 | src/test/resources 84 | 85 | 86 | 87 | 88 | 89 | org.apache.maven.plugins 90 | maven-compiler-plugin 91 | 3.1 92 | 93 | ${java.version} 94 | ${java.version} 95 | 96 | 97 | 98 | 99 | 100 | -------------------------------------------------------------------------------- /src/main/java/de/bripkens/nashorn/Runner.java: -------------------------------------------------------------------------------- 1 | package de.bripkens.nashorn; 2 | 3 | import org.apache.commons.io.IOUtils; 4 | 5 | import javax.script.ScriptContext; 6 | import javax.script.ScriptEngine; 7 | import javax.script.ScriptEngineManager; 8 | import javax.script.ScriptException; 9 | import java.io.IOException; 10 | import java.io.InputStream; 11 | import java.util.HashMap; 12 | import java.util.Map; 13 | import java.util.regex.Pattern; 14 | 15 | /** 16 | * @author Ben Ripkens 17 | */ 18 | public class Runner { 19 | 20 | private static final String WRAPPER_PRE = "main(function() {'use strict';\n"; 21 | private static final String WRAPPER_POST = "\n});"; 22 | 23 | private static final Pattern KEY_ACCESS_PATTERN = Pattern.compile("^[a-z_][a-z_0-9]*(\\.[a-z_][a-z_0-9]*)*$", Pattern.CASE_INSENSITIVE); 24 | 25 | private ScriptEngine engine; 26 | 27 | public Runner() { 28 | ScriptEngineManager manager = new ScriptEngineManager(); 29 | engine = manager.getEngineByName("nashorn"); 30 | 31 | Map output = new HashMap<>(); 32 | engine.getBindings(ScriptContext.ENGINE_SCOPE).put("output", output); 33 | 34 | // execFile(...) and execCode(...) cannot be used as both would apply 35 | // the wrapper. The wrapper cannot be used at this point because it 36 | // relies on the global main(...) function which is added by the 37 | // event loop file. 38 | exec(slurp("/event_loop.js")); 39 | } 40 | 41 | public void execFile(String path) { 42 | execCode(slurp(path)); 43 | } 44 | 45 | public void execCode(String code) { 46 | exec(WRAPPER_PRE + code + WRAPPER_POST); 47 | } 48 | 49 | private void exec(String code) { 50 | try { 51 | engine.eval(code); 52 | } catch (ScriptException e) { 53 | throw new RuntimeException(e); 54 | } 55 | } 56 | 57 | private static String slurp(String filename) { 58 | InputStream in = Runner.class.getResourceAsStream(filename); 59 | try { 60 | return IOUtils.toString(in, "UTF-8"); 61 | } catch (IOException e) { 62 | throw new RuntimeException(e); 63 | } finally { 64 | IOUtils.closeQuietly(in); 65 | } 66 | } 67 | 68 | public void shutdown() { 69 | try { 70 | engine.eval("shutdown()"); 71 | } catch (ScriptException e) { 72 | throw new RuntimeException(e); 73 | } 74 | } 75 | 76 | public Object get(String key) { 77 | if (!KEY_ACCESS_PATTERN.matcher(key).matches()) { 78 | throw new IllegalArgumentException("You may only access global state via the get() method."); 79 | } 80 | try { 81 | return engine.eval(key); 82 | } catch (ScriptException e) { 83 | throw new RuntimeException(e); 84 | } 85 | } 86 | } 87 | -------------------------------------------------------------------------------- /src/main/javascript/event_loop.js: -------------------------------------------------------------------------------- 1 | (function(context) { 2 | 'use strict'; 3 | 4 | var Timer = Java.type('java.util.Timer'); 5 | var Phaser = Java.type('java.util.concurrent.Phaser'); 6 | var TimeUnit = Java.type('java.util.concurrent.TimeUnit'); 7 | var AsyncHttpClient = Java.type('com.ning.http.client.AsyncHttpClient'); 8 | 9 | var timer = new Timer('jsEventLoop', false); 10 | var phaser = new Phaser(); 11 | 12 | var onTaskFinished = function() { 13 | phaser.arriveAndDeregister(); 14 | }; 15 | 16 | // simulate the global window object which is the same as the global scope 17 | // when running in browsers 18 | context.window = context; 19 | 20 | context.setTimeout = function(fn, millis /* [, args...] */) { 21 | var args = [].slice.call(arguments, 2, arguments.length); 22 | 23 | var phase = phaser.register(); 24 | var canceled = false; 25 | timer.schedule(function() { 26 | if (canceled) { 27 | return; 28 | } 29 | 30 | try { 31 | fn.apply(context, args); 32 | } catch (e) { 33 | print(e); 34 | } finally { 35 | onTaskFinished(); 36 | } 37 | }, millis); 38 | 39 | return function() { 40 | onTaskFinished(); 41 | canceled = true; 42 | }; 43 | }; 44 | 45 | context.clearTimeout = function(cancel) { 46 | cancel(); 47 | }; 48 | 49 | context.setInterval = function(fn, delay /* [, args...] */) { 50 | var args = [].slice.call(arguments, 2, arguments.length); 51 | 52 | var cancel = null; 53 | 54 | var loop = function() { 55 | cancel = context.setTimeout(loop, delay); 56 | fn.apply(context, args); 57 | }; 58 | 59 | cancel = context.setTimeout(loop, delay); 60 | return function() { 61 | cancel(); 62 | }; 63 | }; 64 | 65 | context.clearInterval = function(cancel) { 66 | cancel(); 67 | }; 68 | 69 | context.main = function(fn, waitTimeMillis) { 70 | if (!waitTimeMillis) { 71 | waitTimeMillis = 60 * 1000; 72 | } 73 | 74 | if (phaser.isTerminated()) { 75 | phaser = new Phaser(); 76 | } 77 | 78 | // we register the main(...) function with the phaser so that we 79 | // can be notified of all cases. If we wouldn't do this, we would have a 80 | // race condition as `fn` could be finished before we call `await(...)` 81 | // on the phaser. 82 | phaser.register(); 83 | setTimeout(fn, 0); 84 | 85 | // timeout is handled via TimeoutException. This is good enough for us. 86 | phaser.awaitAdvanceInterruptibly(phaser.arrive(), 87 | waitTimeMillis, 88 | TimeUnit.MILLISECONDS); 89 | 90 | // a new phase will have started, so we need to arrive and deregister 91 | // to make sure that following executions of main(...) will work as well. 92 | phaser.arriveAndDeregister(); 93 | }; 94 | 95 | context.shutdown = function() { 96 | timer.cancel(); 97 | phaser.forceTermination(); 98 | }; 99 | 100 | context.XMLHttpRequest = function() { 101 | var method, url, async, user, password, headers = {}; 102 | 103 | this.onreadystatechange = function(){}; 104 | this.readyState = 0; 105 | this.response = null; 106 | this.responseText = null; 107 | this.responseType = ''; 108 | this.status = null; 109 | this.statusText = null; 110 | this.timeout = 0; // no timeout by default 111 | this.ontimeout = function(){}; 112 | this.withCredentials = false; 113 | 114 | this.abort = function() { 115 | 116 | }; 117 | 118 | this.getAllResponseHeaders = function() { 119 | 120 | }; 121 | 122 | this.getResponseHeader = function(key) { 123 | 124 | }; 125 | 126 | this.setRequestHeader = function(key, value) { 127 | headers[key] = value; 128 | }; 129 | 130 | this.open = function(_method, _url, _async, _user, _password) { 131 | this.readyState = 1; 132 | 133 | method = _method; 134 | url = _url; 135 | 136 | async = _async === false ? false : true; 137 | 138 | user = _user || ''; 139 | password = _password || ''; 140 | 141 | setTimeout(this.onreadystatechange, 0); 142 | }; 143 | 144 | this.send = function(data) { 145 | phaser.register(); 146 | 147 | var that = this; 148 | var client = new AsyncHttpClient(); 149 | 150 | var methodPascalCase = method.replace(/^([a-z])(.*)$/i, function(_, firstChar, rest) { 151 | return firstChar.toUpperCase() + rest.toLowerCase() 152 | }); 153 | var requestBuilder = client['prepare' + methodPascalCase](url); 154 | 155 | Object.keys(headers) 156 | .forEach(function(header) { 157 | var value = headers[header]; 158 | requestBuilder.addHeader(header, value); 159 | }); 160 | 161 | // TODO configure timeouts on AsyncHttpClientConfig 162 | // TODO handle errors 163 | requestBuilder.execute(new com.ning.http.client.AsyncCompletionHandler({ 164 | onCompleted: function(response) { 165 | that.readyState = 4; 166 | that.responseText = that.response = response.getResponseBody('UTF-8'); 167 | that.status = response.getStatusCode(); 168 | that.statusText = response.getStatusCode() + ' ' + response.getStatusText(); 169 | 170 | if (that.responseType === 'json') { 171 | that.response = JSON.parse(that.response); 172 | } 173 | 174 | context.setTimeout(that.onreadystatechange, 0); 175 | phaser.arriveAndDeregister(); 176 | } 177 | })); 178 | }; 179 | }; 180 | 181 | })(this); 182 | -------------------------------------------------------------------------------- /src/main/resources/logback.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | %d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | -------------------------------------------------------------------------------- /src/test/java/de/bripkens/nashorn/AbstractEventLoopTest.java: -------------------------------------------------------------------------------- 1 | package de.bripkens.nashorn; 2 | 3 | import org.junit.After; 4 | import org.junit.Before; 5 | 6 | import javax.script.ScriptContext; 7 | import javax.script.ScriptException; 8 | import java.util.HashMap; 9 | 10 | /** 11 | * @author Ben Ripkens 12 | */ 13 | public abstract class AbstractEventLoopTest extends AbstractNashornTest { 14 | 15 | @Override 16 | @Before 17 | public void before() throws ScriptException { 18 | super.before(); 19 | engine.eval(slurp("/event_loop.js")); 20 | } 21 | 22 | @After 23 | public void after() throws ScriptException { 24 | engine.eval("shutdown()"); 25 | } 26 | 27 | public String inEventLoop(String code) { 28 | return "main(function() {'use strict';" + code + "\n});"; 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /src/test/java/de/bripkens/nashorn/AbstractNashornTest.java: -------------------------------------------------------------------------------- 1 | package de.bripkens.nashorn; 2 | 3 | import org.apache.commons.io.IOUtils; 4 | import org.junit.Before; 5 | 6 | import javax.script.ScriptContext; 7 | import javax.script.ScriptEngine; 8 | import javax.script.ScriptEngineManager; 9 | import javax.script.ScriptException; 10 | import java.io.IOException; 11 | import java.io.InputStream; 12 | import java.util.HashMap; 13 | import java.util.Map; 14 | 15 | /** 16 | * @author Ben Ripkens 17 | */ 18 | public abstract class AbstractNashornTest { 19 | 20 | protected ScriptEngine engine; 21 | 22 | protected HashMap output; 23 | 24 | @Before 25 | public void before() throws ScriptException { 26 | engine = newEngine(); 27 | output = new HashMap<>(); 28 | engine.getBindings(ScriptContext.ENGINE_SCOPE).put("output", output); 29 | } 30 | 31 | public ScriptEngine newEngine() { 32 | ScriptEngineManager manager = new ScriptEngineManager(); 33 | return manager.getEngineByName("nashorn"); 34 | } 35 | 36 | public static String slurp(String filename) { 37 | InputStream in = AbstractNashornTest.class.getResourceAsStream(filename); 38 | try { 39 | return IOUtils.toString(in, "UTF-8"); 40 | } catch (IOException e) { 41 | throw new RuntimeException(e); 42 | } finally { 43 | IOUtils.closeQuietly(in); 44 | } 45 | } 46 | 47 | public static String join(String... parts) { 48 | StringBuilder builder = new StringBuilder(); 49 | 50 | for (int i = 0; i < parts.length; i++) { 51 | if (i > 0) { 52 | builder.append('\n'); 53 | } 54 | builder.append(parts[i]); 55 | } 56 | 57 | return builder.toString(); 58 | } 59 | 60 | } 61 | -------------------------------------------------------------------------------- /src/test/java/de/bripkens/nashorn/AsyncHttpClientTest.java: -------------------------------------------------------------------------------- 1 | package de.bripkens.nashorn; 2 | 3 | import org.apache.commons.io.IOUtils; 4 | import org.junit.Test; 5 | 6 | import com.ning.http.client.AsyncCompletionHandler; 7 | import com.ning.http.client.AsyncHttpClient; 8 | import com.ning.http.client.Response; 9 | 10 | public class AsyncHttpClientTest { 11 | 12 | @Test 13 | public void shouldSendRequest() throws Exception { 14 | AsyncHttpClient client = null; 15 | try { 16 | client = new AsyncHttpClient(); 17 | client.prepareGet("http://movie-database.herokuapp.com/movies")// 18 | .addHeader("Accept", "application/json")// 19 | .execute(new AsyncCompletionHandler() { 20 | 21 | @Override 22 | public Response onCompleted(Response response) throws Exception { 23 | System.out.println(response.getStatusCode()); 24 | System.out.println(response.getResponseBody("UTF-8")); 25 | return response; 26 | } 27 | 28 | @Override 29 | public void onThrowable(Throwable t) { 30 | System.out.println(t); 31 | super.onThrowable(t); 32 | } 33 | 34 | }).get(); 35 | } finally { 36 | IOUtils.closeQuietly(client); 37 | } 38 | } 39 | 40 | } 41 | -------------------------------------------------------------------------------- /src/test/java/de/bripkens/nashorn/EngineTest.java: -------------------------------------------------------------------------------- 1 | package de.bripkens.nashorn; 2 | 3 | import static org.hamcrest.MatcherAssert.assertThat; 4 | import static org.hamcrest.Matchers.*; 5 | import static org.junit.Assert.fail; 6 | 7 | import javax.script.*; 8 | 9 | import com.google.common.collect.Lists; 10 | import jdk.nashorn.api.scripting.ScriptObjectMirror; 11 | 12 | import org.hamcrest.Matchers; 13 | import org.junit.Test; 14 | 15 | import java.util.ArrayList; 16 | import java.util.Collections; 17 | 18 | /** 19 | * @author Ben Ripkens 20 | */ 21 | @SuppressWarnings("restriction") 22 | public class EngineTest extends AbstractNashornTest { 23 | 24 | @Test 25 | public void shouldProvideEngineDetails() { 26 | ScriptEngineFactory factory = engine.getFactory(); 27 | 28 | assertThat(factory.getEngineName(), is("Oracle Nashorn")); 29 | assertThat(factory.getEngineVersion(), is("1.8.0_05")); 30 | assertThat(factory.getLanguageName(), is("ECMAScript")); 31 | assertThat(factory.getLanguageVersion(), is("ECMA - 262 Edition 5.1")); 32 | } 33 | 34 | @Test 35 | public void shouldHaveAGlobalObject() throws Exception { 36 | Object globalContext = engine.eval("this"); 37 | assertThat(globalContext, is(not(nullValue()))); 38 | assertThat(globalContext, is(instanceOf(ScriptObjectMirror.class))); 39 | } 40 | 41 | @Test 42 | public void shouldPermitGlobalMutation() throws Exception { 43 | engine.eval("this.foo = 'bar';"); 44 | assertThat(engine.eval("this.foo;"), is("bar")); 45 | } 46 | 47 | @Test 48 | public void shouldNotShareGlobalsBetweenEngines() throws Exception { 49 | engine.eval("this.foo = 'bar';"); 50 | assertThat(newEngine().eval("this.foo"), is(nullValue())); 51 | } 52 | 53 | @Test 54 | public void shouldBeCapableOfRetrievingTheGlobalScope() throws Exception { 55 | ScriptContext context = engine.getContext(); 56 | Bindings bindings = context.getBindings(ScriptContext.ENGINE_SCOPE); 57 | assertThat(bindings.get("Math"), is(not(nullValue()))); 58 | assertThat(bindings.get("Object"), is(not(nullValue()))); 59 | assertThat(bindings.get("JSON"), is(not(nullValue()))); 60 | 61 | // Danger: no setTimeout etc. available! 62 | assertThat(bindings.get("setTimeout"), is(nullValue())); 63 | assertThat(bindings.get("setInterval"), is(nullValue())); 64 | } 65 | 66 | @Test 67 | public void shouldSupportCompilation() { 68 | assertThat(engine, is(Matchers. instanceOf(Compilable.class))); 69 | } 70 | 71 | @Test 72 | public void shouldCompileCode() throws Exception { 73 | Compilable compilable = (Compilable) engine; 74 | engine.eval(slurp("/shouldCompileCode.js")); 75 | CompiledScript compiledScript = compilable.compile("doIt()"); 76 | Object result = compiledScript.eval(); 77 | assertThat(result, is(instanceOf(String.class))); 78 | } 79 | 80 | @Test 81 | public void shouldPrintHelloWorld() throws Exception { 82 | engine.eval("print('Hello World');"); 83 | } 84 | 85 | @Test 86 | public void shouldHaveLimitedNumberOfGlobals() { 87 | Bindings bindings = engine.getContext().getBindings(ScriptContext.ENGINE_SCOPE); 88 | 89 | ArrayList keys = Lists.newArrayList(bindings.keySet()); 90 | Collections.sort(keys); 91 | 92 | for (String key : keys) { 93 | System.out.println(key); 94 | } 95 | } 96 | 97 | @Test 98 | public void shouldNotExit() { 99 | try { 100 | engine.eval("System.exit(1);"); 101 | fail("Shouldn't get here."); 102 | } catch (ScriptException ex) { 103 | assertThat(ex.getMessage(), containsString("\"System\" is not defined")); 104 | } 105 | } 106 | } 107 | -------------------------------------------------------------------------------- /src/test/java/de/bripkens/nashorn/EnvJsTest.java: -------------------------------------------------------------------------------- 1 | package de.bripkens.nashorn; 2 | 3 | import org.junit.Before; 4 | import org.junit.Test; 5 | 6 | import javax.script.ScriptException; 7 | 8 | /** 9 | * @author Ben Ripkens 10 | */ 11 | public class EnvJsTest extends AbstractNashornTest { 12 | 13 | @Before 14 | public void before() throws ScriptException { 15 | super.before(); 16 | engine.eval(slurp("/env.nashorn.1.2.js")); 17 | } 18 | 19 | @Test 20 | public void shouldHandleSimpleGetRequests() throws ScriptException { 21 | engine.eval(slurp("/shouldHandleSimpleGetRequests.js")); 22 | } 23 | 24 | } 25 | -------------------------------------------------------------------------------- /src/test/java/de/bripkens/nashorn/EventLoopTest.java: -------------------------------------------------------------------------------- 1 | package de.bripkens.nashorn; 2 | 3 | import static org.hamcrest.MatcherAssert.assertThat; 4 | import static org.hamcrest.Matchers.instanceOf; 5 | import static org.hamcrest.Matchers.is; 6 | import static org.hamcrest.Matchers.not; 7 | import static org.hamcrest.Matchers.nullValue; 8 | 9 | import java.util.HashMap; 10 | import java.util.Map; 11 | 12 | import javax.script.Bindings; 13 | import javax.script.ScriptContext; 14 | import javax.script.ScriptException; 15 | 16 | import jdk.nashorn.internal.objects.NativeArray; 17 | 18 | import org.junit.After; 19 | import org.junit.Before; 20 | import org.junit.Test; 21 | 22 | /** 23 | * @author Ben Ripkens 24 | */ 25 | @SuppressWarnings("restriction") 26 | public class EventLoopTest extends AbstractEventLoopTest { 27 | 28 | @Test 29 | public void shouldRegisterMissingSetXFunctions() { 30 | ScriptContext context = engine.getContext(); 31 | Bindings globals = context.getBindings(ScriptContext.ENGINE_SCOPE); 32 | 33 | assertThat(globals.get("setTimeout"), is(not(nullValue()))); 34 | assertThat(globals.get("clearTimeout"), is(not(nullValue()))); 35 | assertThat(globals.get("setInterval"), is(not(nullValue()))); 36 | assertThat(globals.get("clearInterval"), is(not(nullValue()))); 37 | 38 | assertThat(globals.get("main"), is(not(nullValue()))); 39 | } 40 | 41 | @Test 42 | public void shouldExecuteAsPartOfEventLoop() throws ScriptException { 43 | engine.eval(slurp("/shouldExecuteAsPartOfEventLoop.js")); 44 | assertThat(output.get("foo"), is("bar")); 45 | } 46 | 47 | @Test 48 | public void shouldInterleaveExecutions() throws Exception { 49 | engine.eval(slurp("/shouldInterleaveExecutions.js")); 50 | 51 | Object numbers = output.get("numbers"); 52 | assertThat(numbers, is(instanceOf(NativeArray.class))); 53 | 54 | NativeArray arr = (NativeArray) numbers; 55 | assertThat(arr.getLength(), is(5L)); // take care - long value! 56 | assertThat(arr.get(0), is(10)); 57 | assertThat(arr.get(1), is(20)); 58 | assertThat(arr.get(2), is(30)); 59 | assertThat(arr.get(3), is(40)); 60 | assertThat(arr.get(4), is(50)); 61 | } 62 | 63 | @Test 64 | public void shouldClearTimeout() throws Exception { 65 | engine.eval(slurp("/shouldClearTimeout.js")); 66 | 67 | Object numbers = output.get("numbers"); 68 | assertThat(numbers, is(instanceOf(NativeArray.class))); 69 | 70 | NativeArray arr = (NativeArray) numbers; 71 | assertThat(arr.getLength(), is(2L)); // take care - long value! 72 | assertThat(arr.get(0), is(1)); 73 | assertThat(arr.get(1), is(2)); 74 | } 75 | 76 | @Test 77 | public void shouldSupportIntervals() throws Exception { 78 | engine.eval(slurp("/shouldSupportIntervals.js")); 79 | assertThat((double) output.get("iterationCount"), is(5.0)); 80 | } 81 | 82 | @Test 83 | public void shouldSupportMultipleMainCallsInSameEngine() throws Exception { 84 | engine.eval(join(// 85 | "main(function() {", // 86 | " output.numbers = [];", // 87 | " output.numbers.push('a');", // 88 | "});"// 89 | )); 90 | 91 | NativeArray arr = (NativeArray) output.get("numbers"); 92 | assertThat(arr.getLength(), is(1L)); 93 | assertThat(arr.get(0), is("a")); 94 | 95 | engine.eval(join(// 96 | "main(function() {", // 97 | " output.numbers.push('b');", // 98 | "});")); 99 | 100 | assertThat(arr.getLength(), is(2L)); 101 | assertThat(arr.get(0), is("a")); 102 | assertThat(arr.get(1), is("b")); 103 | } 104 | 105 | @Test 106 | public void shouldHandleNestedAsyncOperations() throws Exception { 107 | engine.eval(join(// 108 | "main(function() {", // 109 | " setTimeout(function() {", // 110 | " setTimeout(function() {", // 111 | " output.val = 'done';", // 112 | " }, 20);", // 113 | " }, 20);", "});"// 114 | )); 115 | 116 | assertThat(output.get("val"), is("done")); 117 | } 118 | } 119 | -------------------------------------------------------------------------------- /src/test/java/de/bripkens/nashorn/StrengthCalculatorWithoutEventLoopTest.java: -------------------------------------------------------------------------------- 1 | package de.bripkens.nashorn; 2 | 3 | import org.junit.Test; 4 | 5 | import javax.script.Bindings; 6 | import javax.script.ScriptContext; 7 | import javax.script.ScriptEngine; 8 | import javax.script.ScriptEngineManager; 9 | import java.util.Map; 10 | 11 | import static org.hamcrest.MatcherAssert.assertThat; 12 | import static org.hamcrest.Matchers.is; 13 | 14 | /** 15 | * @author Ben Ripkens 16 | */ 17 | public class StrengthCalculatorWithoutEventLoopTest extends AbstractNashornTest { 18 | 19 | @Test 20 | public void shouldCalculatePasswordStength() throws Exception { 21 | ScriptEngineManager manager = new ScriptEngineManager(); 22 | ScriptEngine engine = manager.getEngineByName("nashorn"); 23 | 24 | // register global scope as 'window' 25 | Bindings globalScope = engine.getBindings(ScriptContext.ENGINE_SCOPE); 26 | globalScope.put("window", globalScope); 27 | 28 | // load dependency which registers global zxcvbn function 29 | engine.eval(slurp("/bower_components/zxcvbn/zxcvbn.js")); 30 | 31 | // you should _never_ pass user provided values via string concatenation 32 | // to Nashorn. Check out my other repository to see a better way of doing 33 | // this with Nashorn: 34 | // https://github.com/bripkens/nashorn-pw-check-example/blob/master/src/main/java/de/codecentric/StrengthChecker.java 35 | Map result; 36 | result = (Map) engine.eval("zxcvbn('myPasswordIsRatherLong');"); 37 | 38 | assertThat(result.get("score"), is(2)); 39 | } 40 | 41 | } 42 | -------------------------------------------------------------------------------- /src/test/java/de/bripkens/nashorn/XmlHttpRequestTest.java: -------------------------------------------------------------------------------- 1 | package de.bripkens.nashorn; 2 | 3 | import org.junit.Test; 4 | 5 | import javax.script.ScriptException; 6 | 7 | import static org.hamcrest.MatcherAssert.assertThat; 8 | import static org.hamcrest.Matchers.is; 9 | 10 | /** 11 | * @author Ben Ripkens 12 | */ 13 | public class XmlHttpRequestTest extends AbstractEventLoopTest { 14 | 15 | @Test 16 | public void shouldHandleSimpleGetRequests() throws ScriptException { 17 | engine.eval(inEventLoop(slurp("/shouldHandleSimpleGetRequests.js"))); 18 | 19 | assertThat(output.get("responseArrived"), is(true)); 20 | } 21 | 22 | 23 | 24 | } 25 | -------------------------------------------------------------------------------- /src/test/javascript/shouldClearTimeout.js: -------------------------------------------------------------------------------- 1 | main(function() { 2 | output.numbers = []; 3 | 4 | setTimeout(function() { 5 | output.numbers.push(1); 6 | }, 10); 7 | 8 | var token = setTimeout(function() { 9 | output.numbers.push(3); 10 | }, 30); 11 | 12 | setTimeout(function() { 13 | output.numbers.push(2); 14 | clearTimeout(token); 15 | }, 20); 16 | 17 | 18 | }); -------------------------------------------------------------------------------- /src/test/javascript/shouldCompileCode.js: -------------------------------------------------------------------------------- 1 | this.doIt = function() { 2 | var numbers = []; 3 | 4 | for (var i = 0; i < 5000; i++) { 5 | numbers.push(i); 6 | } 7 | 8 | return numbers.join(', '); 9 | }; -------------------------------------------------------------------------------- /src/test/javascript/shouldExecuteAsPartOfEventLoop.js: -------------------------------------------------------------------------------- 1 | main(function() { 2 | output.foo = 'bar'; 3 | }); -------------------------------------------------------------------------------- /src/test/javascript/shouldHandleSimpleGetRequests.js: -------------------------------------------------------------------------------- 1 | var request = new XMLHttpRequest(); 2 | 3 | request.onreadystatechange = function() { 4 | if (request.readyState === 4) { 5 | print(request.responseText); 6 | output.responseArrived = true; 7 | } 8 | }; 9 | 10 | request.open('GET', 'http://movie-database.herokuapp.com/movies'); 11 | request.setRequestHeader('Accept', 'application/json'); 12 | request.send(); 13 | -------------------------------------------------------------------------------- /src/test/javascript/shouldInterleaveExecutions.js: -------------------------------------------------------------------------------- 1 | main(function() { 2 | output.numbers = []; 3 | 4 | function add(n) { 5 | setTimeout(function() { 6 | output.numbers.push(n); 7 | }, n); 8 | } 9 | 10 | add(50); 11 | add(20); 12 | add(10); 13 | add(30); 14 | add(40); 15 | }); 16 | -------------------------------------------------------------------------------- /src/test/javascript/shouldSupportIntervals.js: -------------------------------------------------------------------------------- 1 | main(function() { 2 | var stopAfter = 5; 3 | output.iterationCount = 0; 4 | 5 | var token = setInterval(function() { 6 | output.iterationCount++; 7 | 8 | if (output.iterationCount >= stopAfter) { 9 | clearInterval(token); 10 | } 11 | }, 10); 12 | }); -------------------------------------------------------------------------------- /src/test/resources/logback-test.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | %d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | --------------------------------------------------------------------------------