public void assertEvaluatesTo(Object expectation, String model, boolean validate) { XExpression expression = null; try { expression = expression(model, validate); IEvaluationResult result = interpreter.evaluate(expression); assertNull("Expected no exception. Model was: " + model + ", Exception was: " + result.getException(), result.getException()); if(expectation != null && expectation.getClass().isArray()) assertArrayEquals("Model was: " + model, (Object[]) expectation, (Object[]) result.getResult()); else assertEquals("Model was: " + model, expectation, result.getResult()); } catch (Exception e) { if (e instanceof RuntimeException) throw (RuntimeException) e; throw new RuntimeException(e); } finally { if (expression != null) { cache.clear(expression.eResource()); } } }
@Override public void assertEvaluatesWithException(Class<? extends Throwable> expectatedException, String model) { XExpression expression = null; try { expression = expression(model,true); IEvaluationResult result = interpreter.evaluate(expression); assertTrue("Expected " + expectatedException.getSimpleName() + " but got: " + result.getException(), expectatedException.isInstance(result.getException())); } catch (Exception e) { if (e instanceof RuntimeException) throw (RuntimeException) e; throw new RuntimeException(e); } finally { if (expression != null) { cache.clear(expression.eResource()); } } }
public Object execute(IEvaluationContext evaluationContext) throws ScriptExecutionException { if(xExpression!=null) { try { IEvaluationResult result = interpreter.evaluate(xExpression, evaluationContext, CancelIndicator.NullImpl); if(result==null) { // this can only happen on an InterpreterCancelledException, i.e. NEVER ;-) return null; } if (result.getException() != null) { throw new ScriptExecutionException(result.getException().getMessage(), result.getException()); } return result.getResult(); } catch(Throwable e) { if(e instanceof ScriptExecutionException) { throw (ScriptExecutionException) e; } else { throw new ScriptExecutionException("An error occured during the script execution: " + e.getMessage(), e); } } } else { throw new ScriptExecutionException("Script does not contain any expression"); } }
@SuppressWarnings("unchecked") public <T> T fromString(String xObjectText) throws XObjectsInterpreterException { File xObjectFile = parser.parse(xObjectText); try { List<Issue> issues = validator.getValidationIssues(xObjectFile); if (!issues.isEmpty()) { // copy/paste even ch.vorburger.el.engine.ExpressionParsingException ? Open Bugzilla to have outside of test.. throw new IllegalArgumentException("Invalid XObjects syntax: " + xObjectText + ", " + issues); } XObject xObject = xObjectFile.getRoot(); IEvaluationResult result = interpreter.evaluate(xObject); if (result.getException() != null) { throw new XObjectsInterpreterException("Failed to evaluated", result.getException()); } else { return (T) result.getResult(); } } finally { // This is super important to avoid a memory leak.. if (xObjectFile != null && xObjectFile.eResource() != null) xObjectFile.eResource().unload(); } }
protected IEvaluationResult _doEvaluate(XDoWhileExpression doWhileLoop, IEvaluationContext context, CancelIndicator indicator) { Object condition = null; do { internalEvaluate(doWhileLoop.getBody(), context, indicator); condition = internalEvaluate(doWhileLoop.getPredicate(), context, indicator); } while (Boolean.TRUE.equals(condition)); return null; }
@Override protected Object doInvoke(Method method, Object[] args) throws Throwable { IEvaluationContext forkedContext = context.fork(); if (args != null) { initializeClosureParameters(forkedContext, args); } IEvaluationResult result = interpreter.evaluate(closure.getExpression(), forkedContext, indicator); if (indicator.isCanceled()) throw new InterpreterCanceledException(); if (result.getException() != null) throw result.getException(); return result.getResult(); }
@Override public IEvaluationResult evaluate(XExpression expression) { return evaluate(expression, createContext(), CancelIndicator.NullImpl); }