Created
September 3, 2012 16:08
-
-
Save volgar1x/3610352 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package org.shivas.server; | |
import javax.script.ScriptEngine; | |
import javax.script.ScriptEngineFactory; | |
import javax.script.ScriptEngineManager; | |
import javax.script.ScriptException; | |
import junit.framework.TestCase; | |
public class ScriptingTest extends TestCase { | |
private ScriptEngineManager mgr; | |
protected void setUp() throws Exception { | |
super.setUp(); | |
this.mgr = new ScriptEngineManager(); | |
} | |
protected void tearDown() throws Exception { | |
super.tearDown(); | |
} | |
public void testEngineExists() { | |
int count = 0; | |
for (ScriptEngineFactory sef : mgr.getEngineFactories()) { | |
if (sef.getLanguageName().equalsIgnoreCase("ruby") || | |
sef.getLanguageName().equalsIgnoreCase("python") || | |
sef.getLanguageName().equalsIgnoreCase("groovy")) | |
{ | |
++count; | |
} | |
} | |
assertEquals(3, count); | |
} | |
private void testLanguage(String name, String script) { | |
ScriptEngine engine = mgr.getEngineByName(name); | |
try { | |
engine.eval(script); | |
Greetable greetable = (Greetable) engine.get("x"); | |
greetable.greet("Blackrush (" + name + ")"); | |
} catch (ScriptException e) { | |
assertTrue(false); | |
e.printStackTrace(); | |
} | |
} | |
public void testRuby() { | |
String script = | |
"import \"org.shivas.server.Greetable\"\n" + | |
"class MyGreeter\n" + | |
" include Greetable\n" + | |
"" + | |
" def greet(name)\n" + | |
" puts 'hello ' + name\n" + | |
" end\n" + | |
"end\n" + | |
"x = MyGreeter.new\n" + | |
"x.greet 'from ruby'"; | |
testLanguage("ruby", script); | |
} | |
public void testPython() { | |
String script = | |
"import org.shivas.server.Greetable\n" + | |
"class MyGreeter(org.shivas.server.Greetable):\n" + | |
" def greet(self, name):\n" + | |
" print 'hello ' + name\n" + | |
"\n" + | |
"x = MyGreeter()\n" + | |
"x.greet('from python')"; | |
testLanguage("python", script); | |
} | |
public void testGroovy() { | |
String script = | |
"import org.shivas.server.Greetable\n" + | |
"class MyGreeter implements Greetable {\n" + | |
" void greet(String name) {\n" + | |
" System.out.println 'hello ' + name\n" + | |
" }\n" + | |
"}\n" + | |
"x = new MyGreeter()\n" + | |
"x.greet 'from groovy'"; | |
testLanguage("groovy", script); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment