Search in sources :

Example 1 with Result

use of com.buschmais.jqassistant.core.analysis.api.Result in project jqa-core-framework by buschmais.

the class AnalyzerVisitorTest method skipConcept.

@Test
public void skipConcept() throws RuleException {
    analyzerVisitor.skipConcept(concept, Severity.MAJOR);
    verify(store, never()).executeQuery(Mockito.eq(statement), anyMap());
    verify(reportWriter).beginConcept(concept);
    ArgumentCaptor<Result> resultCaptor = ArgumentCaptor.forClass(Result.class);
    verify(reportWriter).setResult(resultCaptor.capture());
    Result result = resultCaptor.getValue();
    assertThat(result.getStatus(), equalTo(Result.Status.SKIPPED));
    assertThat(result.getSeverity(), equalTo(Severity.MAJOR));
    verify(reportWriter).endConcept();
    verify(store, never()).create(ConceptDescriptor.class);
}
Also used : Result(com.buschmais.jqassistant.core.analysis.api.Result) Test(org.junit.Test)

Example 2 with Result

use of com.buschmais.jqassistant.core.analysis.api.Result in project jqa-core-framework by buschmais.

the class AnalyzerVisitorTest method executeConstraint.

@Test
public void executeConstraint() throws RuleException {
    analyzerVisitor.visitConstraint(constraint, Severity.BLOCKER);
    ArgumentCaptor<Map> argumentCaptor = ArgumentCaptor.forClass(Map.class);
    verify(store).executeQuery(Mockito.eq(statement), argumentCaptor.capture());
    Map<String, Object> parameters = argumentCaptor.getValue();
    assertThat(parameters.get(PARAMETER_WITHOUT_DEFAULT), CoreMatchers.<Object>equalTo("value"));
    assertThat(parameters.get(PARAMETER_WITH_DEFAULT), CoreMatchers.<Object>equalTo("defaultValue"));
    verify(reportWriter).beginConstraint(constraint);
    ArgumentCaptor<Result> resultCaptor = ArgumentCaptor.forClass(Result.class);
    verify(reportWriter).setResult(resultCaptor.capture());
    Result result = resultCaptor.getValue();
    assertThat(result.getStatus(), equalTo(Result.Status.FAILURE));
    assertThat(result.getSeverity(), equalTo(Severity.BLOCKER));
    verify(reportWriter).endConstraint();
}
Also used : CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) Matchers.anyMap(org.mockito.Matchers.anyMap) Result(com.buschmais.jqassistant.core.analysis.api.Result) Test(org.junit.Test)

Example 3 with Result

use of com.buschmais.jqassistant.core.analysis.api.Result in project jqa-core-framework by buschmais.

the class AnalyzerVisitorTest method skipConstraint.

@Test
public void skipConstraint() throws RuleException {
    analyzerVisitor.skipConstraint(constraint, Severity.BLOCKER);
    verify(store, never()).executeQuery(Mockito.eq(statement), anyMap());
    verify(reportWriter).beginConstraint(constraint);
    ArgumentCaptor<Result> resultCaptor = ArgumentCaptor.forClass(Result.class);
    verify(reportWriter).setResult(resultCaptor.capture());
    Result result = resultCaptor.getValue();
    assertThat(result.getStatus(), equalTo(Result.Status.SKIPPED));
    assertThat(result.getSeverity(), equalTo(Severity.BLOCKER));
    verify(reportWriter).endConstraint();
}
Also used : Result(com.buschmais.jqassistant.core.analysis.api.Result) Test(org.junit.Test)

Example 4 with Result

use of com.buschmais.jqassistant.core.analysis.api.Result in project jqa-core-framework by buschmais.

the class AnalyzerVisitor method executeScript.

/**
     * Execute an analysis script
     * 
     * @param <T>
     *            The result type.
     * @param scriptExecutable
     *            The script.
     * @param ruleParameters
     * @param severity
     *            The severity. @return The result.
     * @throws RuleExecutorException
     *             If script execution fails.
     */
private <T extends ExecutableRule> Result<T> executeScript(T executable, ScriptExecutable scriptExecutable, Map<String, Object> ruleParameters, Severity severity) throws RuleExecutorException {
    String language = scriptExecutable.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 RuleExecutorException("Cannot resolve scripting engine for '" + language + "', available languages are " + availableLanguages);
    }
    // Set default variables
    scriptEngine.put(ScriptVariable.STORE.getVariableName(), store);
    scriptEngine.put(ScriptVariable.RULE.getVariableName(), executable);
    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(scriptExecutable.getSource());
    } catch (ScriptException e) {
        throw new RuleExecutorException("Cannot execute script.", e);
    }
    if (!(scriptResult instanceof Result)) {
        throw new RuleExecutorException("Script returned an invalid result type, expected " + Result.class.getName() + " but got " + scriptResult);
    }
    return Result.class.cast(scriptResult);
}
Also used : ScriptException(javax.script.ScriptException) RuleExecutorException(com.buschmais.jqassistant.core.rule.api.executor.RuleExecutorException) ScriptEngineFactory(javax.script.ScriptEngineFactory) ScriptEngine(javax.script.ScriptEngine) Result(com.buschmais.jqassistant.core.analysis.api.Result)

Example 5 with Result

use of com.buschmais.jqassistant.core.analysis.api.Result in project jqa-core-framework by buschmais.

the class AnalyzerVisitor method visitConcept.

@Override
public boolean visitConcept(Concept concept, Severity effectiveSeverity) throws RuleExecutorException {
    try {
        store.beginTransaction();
        ConceptDescriptor conceptDescriptor = store.find(ConceptDescriptor.class, concept.getId());
        Result.Status status;
        if (conceptDescriptor == null || configuration.isExecuteAppliedConcepts()) {
            logger.info("Applying concept '" + concept.getId() + "' with severity: '" + concept.getSeverity().getInfo(effectiveSeverity) + "'.");
            reportPlugin.beginConcept(concept);
            Result<Concept> result = execute(concept, effectiveSeverity);
            reportPlugin.setResult(result);
            status = result.getStatus();
            if (conceptDescriptor == null) {
                conceptDescriptor = store.create(ConceptDescriptor.class);
                conceptDescriptor.setId(concept.getId());
                conceptDescriptor.setStatus(result.getStatus());
            }
            reportPlugin.endConcept();
        } else {
            status = conceptDescriptor.getStatus();
        }
        store.commitTransaction();
        return Result.Status.SUCCESS.equals(status);
    } catch (XOException e) {
        store.rollbackTransaction();
        throw new RuleExecutorException("Cannot apply concept " + concept.getId(), e);
    }
}
Also used : XOException(com.buschmais.xo.api.XOException) RuleExecutorException(com.buschmais.jqassistant.core.rule.api.executor.RuleExecutorException) ConceptDescriptor(com.buschmais.jqassistant.core.analysis.api.model.ConceptDescriptor) Result(com.buschmais.jqassistant.core.analysis.api.Result)

Aggregations

Result (com.buschmais.jqassistant.core.analysis.api.Result)8 Test (org.junit.Test)5 RuleExecutorException (com.buschmais.jqassistant.core.rule.api.executor.RuleExecutorException)3 CoreMatchers.containsString (org.hamcrest.CoreMatchers.containsString)3 Matchers.anyMap (org.mockito.Matchers.anyMap)3 XOException (com.buschmais.xo.api.XOException)2 ScriptException (javax.script.ScriptException)2 ConceptDescriptor (com.buschmais.jqassistant.core.analysis.api.model.ConceptDescriptor)1 Query (com.buschmais.xo.api.Query)1 ScriptEngine (javax.script.ScriptEngine)1 ScriptEngineFactory (javax.script.ScriptEngineFactory)1