1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
|
package org.python.jsr223;
import javax.script.Bindings;
import javax.script.Invocable;
import javax.script.ScriptContext;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;
import javax.script.ScriptEngineFactory;
import javax.script.SimpleBindings;
import junit.framework.TestCase;
import java.io.FileReader;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.Reader;
import java.io.StringReader;
import java.io.StringWriter;
public class ScriptEngineIOTest extends TestCase
{
ScriptEngineFactory pythonEngineFactory;
ScriptEngine pythonEngine;
public void setUp() throws ScriptException
{
pythonEngineFactory = new PyScriptEngineFactory();
pythonEngine = new PyScriptEngine(pythonEngineFactory);
}
public void testEvalString() throws ScriptException
{
assertNull(pythonEngine.eval("x = 5"));
assertEquals(Integer.valueOf(5), pythonEngine.eval("x"));
}
public void testReadline() throws ScriptException
{
final String testString = "Shazaam Batman!\n";
pythonEngine.getContext().setReader(new StringReader(testString));
assertNull(pythonEngine.eval("import sys"));
assertEquals(testString, pythonEngine.eval("sys.stdin.readline()"));
}
public void testReadlines() throws ScriptException
{
final String testString = "Holy Smokes Batman!\nBIF!\r\n\nKAPOW!!!\rTHE END.";
pythonEngine.getContext().setReader(new StringReader(testString));
assertNull(pythonEngine.eval("import sys"));
final Object o = pythonEngine.eval("''.join(sys.stdin.readlines())");
assertEquals("Holy Smokes Batman!\nBIF!\n\nKAPOW!!!\nTHE END.\n", o);
}
public void testWriter() throws ScriptException
{
final StringWriter sw = new StringWriter();
pythonEngine.getContext().setWriter(sw);
final String testString = "It is a wonderful world.";
assertNull(pythonEngine.eval("print '" + testString + "',"));
assertEquals(testString, sw.toString());
}
public void testErrorWriter() throws ScriptException
{
final StringWriter stdout = new StringWriter();
final StringWriter stderr = new StringWriter();
pythonEngine.getContext().setWriter(stdout);
pythonEngine.getContext().setErrorWriter(stderr);
final String testString1 = "It is a wonderful world.";
final String testString2 = "Stuff happens!";
assertNull(pythonEngine.eval("import sys"));
assertNull(pythonEngine.eval("sys.stdout.write('" + testString1 + "')"));
assertNull(pythonEngine.eval("sys.stderr.write('" + testString2 + "')"));
assertEquals(testString1, stdout.toString());
assertEquals(testString2, stderr.toString());
}
public void testEvalWithReader() throws ScriptException, FileNotFoundException
{
//Check that multiple evals don't cause an NPE.
//See issue http://bugs.jython.org/issue1536
final ScriptEngineManager manager = new ScriptEngineManager();
final String engineType = "jython";
final ScriptEngine engine = manager.getEngineByName(engineType);
final StringWriter stdout = new StringWriter();
final StringWriter stderr = new StringWriter();
engine.getContext().setWriter(stdout);
engine.getContext().setErrorWriter(stderr);
final Bindings bindings = new SimpleBindings();
bindings.put("firstLevelNodes", 10);
bindings.put("secondLevelNodes", 5);
engine.setBindings(bindings, ScriptContext.ENGINE_SCOPE);
engine.setBindings(bindings, ScriptContext.ENGINE_SCOPE);
final Reader dfsScript = new FileReader("tests/python/dfs.py");
for (int i = 1; i <= 10; i++) {
engine.eval(dfsScript);
assertEquals(61, engine.get("result"));
}
}
public void testGetInterfaceCharSequence1() throws ScriptException, IOException {
ScriptEngineManager manager = new ScriptEngineManager();
ScriptEngine engine = manager.getEngineByName("python");
Invocable invocableEngine = (Invocable) engine;
assertNull(engine.eval(
"from java.lang import CharSequence\n" +
"class MyString(CharSequence):\n" +
" def length(self): return 3\n" +
" def charAt(self, index): return 'a'\n" +
" def subSequence(self, start, end): return \"\"\n" +
" def toString(self): return \"aaa\"\n" +
"c = MyString()"));
CharSequence seq = invocableEngine.getInterface(engine.get("c"), CharSequence.class);
assertEquals("aaa", seq.toString());
}
public void testGetInterfaceCharSequence2() throws ScriptException, IOException {
ScriptEngineManager manager = new ScriptEngineManager();
ScriptEngine pythonEngine = manager.getEngineByName("python");
Invocable invocableEngine = (Invocable) pythonEngine;
assertNull(pythonEngine.eval(
"from java.lang import StringBuilder\r\n" +
"c = StringBuilder(\"abc\")\r\n"));
CharSequence seq = invocableEngine.getInterface(pythonEngine.get("c"), CharSequence.class);
assertEquals("abc", seq.toString());
}
}
|