use of com.buschmais.jqassistant.core.rule.api.model.RuleException in project jqa-core-framework by buschmais.
the class TransactionalVisitorTest method runtimeException.
@Test
void runtimeException() throws RuleException {
Constraint constraint = mock(Constraint.class);
doThrow(new NullPointerException()).when(delegate).visitConstraint(constraint, MAJOR);
try {
visitor.visitConstraint(constraint, MAJOR);
fail("Expecting a " + XOException.class);
} catch (RuleException e) {
verify(delegate).visitConstraint(constraint, MAJOR);
verifyFailedTransaction();
}
}
use of com.buschmais.jqassistant.core.rule.api.model.RuleException in project jqa-core-framework by buschmais.
the class ScriptRuleInterpreterPlugin method execute.
@Override
public <T extends ExecutableRule<?>> Result<T> execute(T executableRule, Map<String, Object> ruleParameters, Severity severity, AnalyzerContext context) throws RuleException {
Executable<String> executable = executableRule.getExecutable();
String language = executable.getLanguage();
ScriptEngine scriptEngine = scriptEngineManager.getEngineByName(language);
if (scriptEngine == null) {
List<String> availableLanguages = new ArrayList<>();
for (ScriptEngineFactory factory : scriptEngineManager.getEngineFactories()) {
availableLanguages.addAll(factory.getNames());
}
throw new RuleException("Cannot resolve scripting engine for '" + language + "', available languages are " + availableLanguages);
}
// Set default variables
scriptEngine.put(ScriptVariable.STORE.getVariableName(), context.getStore());
scriptEngine.put(ScriptVariable.CONTEXT.getVariableName(), context);
scriptEngine.put(ScriptVariable.RULE.getVariableName(), executableRule);
scriptEngine.put(ScriptVariable.SEVERITY.getVariableName(), severity);
// Set rule parameters
for (Map.Entry<String, Object> entry : ruleParameters.entrySet()) {
scriptEngine.put(entry.getKey(), entry.getValue());
}
Object scriptResult;
try {
scriptResult = scriptEngine.eval(executable.getSource());
} catch (ScriptException e) {
throw new RuleException("Cannot execute script.", e);
}
if (!(scriptResult instanceof Result)) {
throw new RuleException("Script returned an invalid result type, expected " + Result.class.getName() + " but got " + scriptResult);
}
return Result.class.cast(scriptResult);
}
use of com.buschmais.jqassistant.core.rule.api.model.RuleException in project jqa-core-framework by buschmais.
the class TransactionalVisitorTest method ruleException.
@Test
void ruleException() throws RuleException {
Constraint constraint = mock(Constraint.class);
doThrow(new RuleException("Test")).when(delegate).visitConstraint(constraint, MAJOR);
try {
visitor.visitConstraint(constraint, MAJOR);
fail("Expecting a " + RuleException.class);
} catch (RuleException e) {
verify(delegate).visitConstraint(constraint, MAJOR);
verifyFailedTransaction();
}
}
Aggregations