|
| 1 | +/* |
| 2 | + * #%L |
| 3 | + * JSR-223-compliant JavaScript scripting language plugin. |
| 4 | + * %% |
| 5 | + * Copyright (C) 2008 - 2014 Board of Regents of the University of |
| 6 | + * Wisconsin-Madison, Broad Institute of MIT and Harvard, and Max Planck |
| 7 | + * Institute of Molecular Cell Biology and Genetics. |
| 8 | + * %% |
| 9 | + * Redistribution and use in source and binary forms, with or without |
| 10 | + * modification, are permitted provided that the following conditions are met: |
| 11 | + * |
| 12 | + * 1. Redistributions of source code must retain the above copyright notice, |
| 13 | + * this list of conditions and the following disclaimer. |
| 14 | + * 2. Redistributions in binary form must reproduce the above copyright notice, |
| 15 | + * this list of conditions and the following disclaimer in the documentation |
| 16 | + * and/or other materials provided with the distribution. |
| 17 | + * |
| 18 | + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 19 | + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 20 | + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 21 | + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE |
| 22 | + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 23 | + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 24 | + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 25 | + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 26 | + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 27 | + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 28 | + * POSSIBILITY OF SUCH DAMAGE. |
| 29 | + * #L% |
| 30 | + */ |
| 31 | + |
| 32 | +package org.scijava.plugins.scripting.javascript; |
| 33 | + |
| 34 | +import static org.junit.Assert.assertEquals; |
| 35 | +import static org.junit.Assert.assertNull; |
| 36 | +import static org.junit.Assert.assertTrue; |
| 37 | + |
| 38 | +import java.io.IOException; |
| 39 | +import java.util.concurrent.ExecutionException; |
| 40 | + |
| 41 | +import javax.script.Bindings; |
| 42 | +import javax.script.ScriptContext; |
| 43 | +import javax.script.ScriptEngine; |
| 44 | +import javax.script.ScriptException; |
| 45 | + |
| 46 | +import org.junit.Test; |
| 47 | +import org.scijava.Context; |
| 48 | +import org.scijava.script.ScriptLanguage; |
| 49 | +import org.scijava.script.ScriptModule; |
| 50 | +import org.scijava.script.ScriptService; |
| 51 | + |
| 52 | +/** |
| 53 | + * JavaScript unit tests. |
| 54 | + * |
| 55 | + * @author Johannes Schindelin |
| 56 | + * @author Curtis Rueden |
| 57 | + */ |
| 58 | +public class JavaScriptTest { |
| 59 | + |
| 60 | + @Test |
| 61 | + public void testBasic() throws InterruptedException, ExecutionException, |
| 62 | + IOException, ScriptException |
| 63 | + { |
| 64 | + final Context context = new Context(ScriptService.class); |
| 65 | + final ScriptService scriptService = context.getService(ScriptService.class); |
| 66 | + final String script = "$x = 1 + 2;"; |
| 67 | + // NB: Some JVMs return Integer, others Double. Let's be careful here. |
| 68 | + final ScriptModule m = scriptService.run("add.js", script, true).get(); |
| 69 | + final Number result = (Number) m.getReturnValue(); |
| 70 | + assertEquals(3.0, result.doubleValue(), 0.0); |
| 71 | + } |
| 72 | + |
| 73 | + @Test |
| 74 | + public void testLocals() throws ScriptException { |
| 75 | + final Context context = new Context(ScriptService.class); |
| 76 | + final ScriptService scriptService = context.getService(ScriptService.class); |
| 77 | + |
| 78 | + final ScriptLanguage language = scriptService.getLanguageByExtension("js"); |
| 79 | + final ScriptEngine engine = language.getScriptEngine(); |
| 80 | + assertTrue(engine.getClass().getName().endsWith(".RhinoScriptEngine")); |
| 81 | + engine.put("$hello", 17); |
| 82 | + assertEquals("17", engine.eval("$hello").toString()); |
| 83 | + assertEquals("17", engine.get("$hello").toString()); |
| 84 | + |
| 85 | + final Bindings bindings = engine.getBindings(ScriptContext.ENGINE_SCOPE); |
| 86 | + bindings.clear(); |
| 87 | + assertNull(engine.get("$hello")); |
| 88 | + } |
| 89 | + |
| 90 | + @Test |
| 91 | + public void testParameters() throws InterruptedException, ExecutionException, |
| 92 | + IOException, ScriptException |
| 93 | + { |
| 94 | + final Context context = new Context(ScriptService.class); |
| 95 | + final ScriptService scriptService = context.getService(ScriptService.class); |
| 96 | + |
| 97 | + final String script = "" + // |
| 98 | + "// @ScriptService ss\n" + // |
| 99 | + "// @OUTPUT String language\n" + // |
| 100 | + "language = ss.getLanguageByName('JavaScript').getLanguageName()\n"; |
| 101 | + final ScriptModule m = scriptService.run("hello.js", script, true).get(); |
| 102 | + |
| 103 | + final Object actual = m.getOutput("language"); |
| 104 | + final String expected = |
| 105 | + scriptService.getLanguageByName("JavaScript").getLanguageName(); |
| 106 | + assertEquals(expected, actual); |
| 107 | + |
| 108 | + final Object result = m.getReturnValue(); |
| 109 | + assertEquals(expected, result); |
| 110 | + } |
| 111 | + |
| 112 | +} |
0 commit comments