JavaScripPredicate extends AbstractAdviserPredicate { @Override public void init() throws Exception { javaScripExpression = checkNotNull(getParameterValue(PARAMETER_JAVASCRIPT_EXPRESSION)); ScriptEngine engine = createJavaScriptEngine(); engine.getContext().getBindings(ScriptContext.ENGINE_SCOPE) .put(BINDING_NAME_CONTEXT, predicateExecutionContext); try { engine.eval(javaScripExpression); } catch (Exception e) { throw new Exception( String.format("Error [%s] while parsing javascript expression [%s].", e.getMessage(), javaScripExpression), e); } this.scriptEngine = (Invocable) engine; try { scriptEngine.invokeFunction(FUNCTION_INIT); } catch (NoSuchMethodException e) { } catch (ScriptException e) { throw new Exception( String.format("Error [%s] while calling initializing script with init().", e.getMessage()), e); } } JavaScripPredicate(PredicateExecutionContext predicateExecutionContext); @Override boolean apply(final PriceTick priceTick); @Override void init(); static final String PARAMETER_JAVASCRIPT_EXPRESSION; }
@Test(expected = Exception.class) public void testApplyScriptBadSyntax() throws Exception { String script = "this syntax is invalid!"; AdviserPredicate adviserPredicate = createAdviserPredicate(script); adviserPredicate.init(); }
@Test public void testApplyScriptWithoutInit() throws Exception { String script = "apply = function(priceTick) {};"; AdviserPredicate adviserPredicate = createAdviserPredicate(script); adviserPredicate.init(); }