public class RJSTest extends AbstractTest {
@Testpublic void testScript() throws IOException {//this has to be changed e.g. with path on you local disk where r.js is locatedString javascriptRoot = getTestResourceURL("/javascripts").getPath() + "/";try {//initializeContext context = Context.enter();context.setOptimizationLevel(-1);context.setLanguageVersion(Context.VERSION_1_7);Global global = new Global();global.init(context);Scriptable scope = context.initStandardObjects(global);//execute Hello World Java ScriptObject wrappedOut = Context.javaToJS(System.out, scope);ScriptableObject.putProperty(scope, "out", wrappedOut);context.evaluateString(scope, "out.println('Hello World')", null, 1, null);// load r.js, with the following code the test is failing, without the test is successful
global.defineProperty("arguments", new String[]{}, ScriptableObject.DONTENUM);executeJS(context, scope, javascriptRoot + "/common/lib", "r.js");} finally {Context.exit();}assertTrue("Context.getCurrentContext() is not null!", Context.getCurrentContext() == null);}public void executeJS(Context context, Scriptable scope, String jsPath, String jsFileName) {InputStream inputStream = null;try {File jsFile = new File(jsPath + "/" + jsFileName);inputStream = new FileInputStream(jsFile);context.evaluateReader(scope, new InputStreamReader(inputStream), jsFileName, 1, null);} catch (IOException e) {throw new RuntimeException(e);} finally {if (inputStream != null) {try {inputStream.close();} catch (IOException e) {// ignore}}}}
}