Search in sources :

Example 6 with RuleException

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();
    }
}
Also used : XOException(com.buschmais.xo.api.XOException) Constraint(com.buschmais.jqassistant.core.rule.api.model.Constraint) RuleException(com.buschmais.jqassistant.core.rule.api.model.RuleException) Test(org.junit.jupiter.api.Test)

Example 7 with RuleException

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);
}
Also used : ScriptException(javax.script.ScriptException) ScriptEngineFactory(javax.script.ScriptEngineFactory) ArrayList(java.util.ArrayList) RuleException(com.buschmais.jqassistant.core.rule.api.model.RuleException) Map(java.util.Map) ScriptEngine(javax.script.ScriptEngine) Result(com.buschmais.jqassistant.core.report.api.model.Result)

Example 8 with RuleException

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();
    }
}
Also used : Constraint(com.buschmais.jqassistant.core.rule.api.model.Constraint) RuleException(com.buschmais.jqassistant.core.rule.api.model.RuleException) Test(org.junit.jupiter.api.Test)

Aggregations

RuleException (com.buschmais.jqassistant.core.rule.api.model.RuleException)8 Result (com.buschmais.jqassistant.core.report.api.model.Result)3 Map (java.util.Map)3 Constraint (com.buschmais.jqassistant.core.rule.api.model.Constraint)2 Test (org.junit.jupiter.api.Test)2 RuleInterpreterPlugin (com.buschmais.jqassistant.core.analysis.api.RuleInterpreterPlugin)1 PluginRepositoryException (com.buschmais.jqassistant.core.plugin.api.PluginRepositoryException)1 Status (com.buschmais.jqassistant.core.report.api.model.Result.Status)1 Parameter (com.buschmais.jqassistant.core.rule.api.model.Parameter)1 RuleParserPlugin (com.buschmais.jqassistant.core.rule.api.reader.RuleParserPlugin)1 Query (com.buschmais.xo.api.Query)1 XOException (com.buschmais.xo.api.XOException)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 LinkedHashMap (java.util.LinkedHashMap)1 LinkedList (java.util.LinkedList)1 ScriptEngine (javax.script.ScriptEngine)1 ScriptEngineFactory (javax.script.ScriptEngineFactory)1 ScriptException (javax.script.ScriptException)1 IdClassListType (org.jqassistant.schema.plugin.v1.IdClassListType)1