|
| 1 | +/* |
| 2 | + * #%L |
| 3 | + * SciJava Common shared library for SciJava software. |
| 4 | + * %% |
| 5 | + * Copyright (C) 2009 - 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.script; |
| 33 | + |
| 34 | +import static org.junit.Assert.assertEquals; |
| 35 | +import static org.junit.Assert.fail; |
| 36 | + |
| 37 | +import java.io.Reader; |
| 38 | +import java.io.StringReader; |
| 39 | +import java.util.Arrays; |
| 40 | +import java.util.HashMap; |
| 41 | +import java.util.List; |
| 42 | + |
| 43 | +import javax.script.Bindings; |
| 44 | +import javax.script.ScriptContext; |
| 45 | +import javax.script.ScriptEngine; |
| 46 | +import javax.script.ScriptException; |
| 47 | + |
| 48 | +import org.junit.AfterClass; |
| 49 | +import org.junit.BeforeClass; |
| 50 | +import org.junit.Test; |
| 51 | +import org.scijava.Context; |
| 52 | +import org.scijava.plugin.Plugin; |
| 53 | + |
| 54 | +public class ScriptInfoTest { |
| 55 | + |
| 56 | + private static Context context; |
| 57 | + private static ScriptService scriptService; |
| 58 | + |
| 59 | + // -- Test setup -- |
| 60 | + |
| 61 | + @BeforeClass |
| 62 | + public static void setUp() { |
| 63 | + context = new Context(); |
| 64 | + scriptService = context.getService(ScriptService.class); |
| 65 | + } |
| 66 | + |
| 67 | + @AfterClass |
| 68 | + public static void tearDown() { |
| 69 | + context.dispose(); |
| 70 | + } |
| 71 | + |
| 72 | + // -- Tests -- |
| 73 | + |
| 74 | + /** |
| 75 | + * Ensures parameters are parsed correctly from scripts, even in the presence |
| 76 | + * of noise like e-mail addresses. |
| 77 | + */ |
| 78 | + @Test |
| 79 | + public void testParameterParsing() throws Exception { |
| 80 | + final String script = |
| 81 | + "" + "% @LogService log\n" |
| 82 | + + "% @OUTPUT Integer output" |
| 83 | + + "% kraken@blah.net\n"; |
| 84 | + ScriptModule scriptModule = scriptService.run("hello.bsizes", script, true).get(); |
| 85 | + |
| 86 | + Object output = scriptModule.getOutput("result"); |
| 87 | + |
| 88 | + if (output == null || !(output instanceof Integer)) fail(); |
| 89 | + assertEquals(3, ((Integer)output).intValue()); |
| 90 | + } |
| 91 | + |
| 92 | + @Plugin(type = ScriptLanguage.class) |
| 93 | + public static class BindingSizes extends AbstractScriptLanguage { |
| 94 | + |
| 95 | + @Override |
| 96 | + public ScriptEngine getScriptEngine() { |
| 97 | + return new BindingSizesEngine(); |
| 98 | + } |
| 99 | + |
| 100 | + @Override |
| 101 | + public List<String> getNames() { |
| 102 | + return Arrays.asList("BindingSizes"); |
| 103 | + } |
| 104 | + |
| 105 | + @Override |
| 106 | + public List<String> getExtensions() { |
| 107 | + return Arrays.asList("bsizes"); |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + // -- Test script langauge -- |
| 112 | + |
| 113 | + private static class BindingSizesEngine extends AbstractScriptEngine { |
| 114 | + |
| 115 | + { |
| 116 | + engineScopeBindings = new BindingSizesBindings(); |
| 117 | + } |
| 118 | + |
| 119 | + @Override |
| 120 | + public Object eval(String script) throws ScriptException { |
| 121 | + return eval(new StringReader(script)); |
| 122 | + } |
| 123 | + |
| 124 | + @Override |
| 125 | + public Object eval(Reader reader) throws ScriptException { |
| 126 | + Bindings bindings = getBindings(ScriptContext.ENGINE_SCOPE); |
| 127 | + return bindings.size(); |
| 128 | + } |
| 129 | + } |
| 130 | + |
| 131 | + private static class BindingSizesBindings extends HashMap<String, Object> |
| 132 | + implements Bindings |
| 133 | + {} |
| 134 | +} |
0 commit comments