Search in sources :

Example 11 with Result

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

the class ReportHelperTest method failedConcepts.

@Test
public void failedConcepts() {
    Result<Concept> infoConceptResult = mockResult("test:infoConcept", Concept.class, Result.Status.FAILURE, Severity.INFO);
    Result<Concept> minorConceptResult = mockResult("test:minorConcept", Concept.class, Result.Status.FAILURE, Severity.MINOR);
    Result<Concept> majorConceptResult = mockResult("test:majorConcept", Concept.class, Result.Status.FAILURE, Severity.MAJOR);
    Map<String, Result<Concept>> conceptResults = new HashMap<>();
    conceptResults.put("test:infoConcept", infoConceptResult);
    conceptResults.put("test:minorConcept", minorConceptResult);
    conceptResults.put("test:majorConcept", majorConceptResult);
    when(inMemoryReportWriter.getConceptResults()).thenReturn(conceptResults);
    int violations = reportHelper.verifyConceptResults(Severity.MINOR, Severity.MAJOR, inMemoryReportWriter);
    assertThat(violations, equalTo(1));
    verifyMessages(debugMessages, ReportHelper.CONCEPT_FAILED_HEADER, "Concept: test:infoConcept", "Severity: INFO");
    verifyMessages(warnMessages, ReportHelper.CONCEPT_FAILED_HEADER, "Concept: test:minorConcept", "Severity: MINOR");
    verifyMessages(errorMessages, ReportHelper.CONCEPT_FAILED_HEADER, "Concept: test:majorConcept", "Severity: MAJOR");
}
Also used : Concept(com.buschmais.jqassistant.core.analysis.api.rule.Concept) StringContains.containsString(org.hamcrest.core.StringContains.containsString) Constraint(com.buschmais.jqassistant.core.analysis.api.rule.Constraint) Result(com.buschmais.jqassistant.core.analysis.api.Result) Test(org.junit.Test)

Example 12 with Result

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

the class XmlReportTestHelper method createXmlWithUmlauts.

public static String createXmlWithUmlauts(String description) throws ReportException {
    StringWriter writer = new StringWriter();
    XmlReportWriter xmlReportWriter = new XmlReportWriter(writer);
    xmlReportWriter.begin();
    Concept concept = Concept.Builder.newConcept().id("mein:Konzept").description(description).severity(Severity.MAJOR).executable(new CypherExecutable("match...")).verification(ROW_COUNT_VERIFICATION).report(Report.Builder.newInstance().primaryColumn("c2").get()).get();
    Map<String, Severity> concepts = new HashMap<>();
    concepts.put("mein:Konzept", Severity.INFO);
    Group group = Group.Builder.newGroup().id("default").description("Meine Gruppe").conceptIds(concepts).get();
    xmlReportWriter.beginGroup(group);
    xmlReportWriter.beginConcept(concept);
    List<Map<String, Object>> rows = new ArrayList<>();
    rows.add(createRow());
    Result<Concept> result = new Result<>(concept, Result.Status.SUCCESS, Severity.CRITICAL, Arrays.asList(C1, C2), rows);
    xmlReportWriter.setResult(result);
    xmlReportWriter.endConcept();
    xmlReportWriter.endGroup();
    xmlReportWriter.end();
    return writer.toString();
}
Also used : XmlReportWriter(com.buschmais.jqassistant.core.report.impl.XmlReportWriter) Result(com.buschmais.jqassistant.core.analysis.api.Result) StringWriter(java.io.StringWriter)

Example 13 with Result

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

the class ReportHelperTest method failedConstraintsWithOverriddenSeverity.

@Test
public void failedConstraintsWithOverriddenSeverity() {
    Map<String, Object> infoRow = new HashMap<>();
    infoRow.put("InfoElement", "InfoValue");
    Result<Constraint> infoConstraintResult = mockResult("test:infoConstraint", Constraint.class, Result.Status.FAILURE, Severity.INFO, Severity.MINOR, Collections.singletonList(infoRow));
    Map<String, Object> minorRow = new HashMap<>();
    minorRow.put("MinorElement", "MinorValue");
    Result<Constraint> minorConstraintResult = mockResult("test:minorConstraint", Constraint.class, Result.Status.FAILURE, Severity.MINOR, Severity.MAJOR, Collections.singletonList(minorRow));
    Map<String, Object> majorRow = new HashMap<>();
    majorRow.put("MajorElement", "MajorValue");
    Result<Constraint> majorConstraintResult = mockResult("test:majorConstraint", Constraint.class, Result.Status.FAILURE, Severity.MAJOR, Severity.CRITICAL, Collections.singletonList(majorRow));
    Map<String, Result<Constraint>> constraintResults = new HashMap<>();
    constraintResults.put("test:infoConstraint", infoConstraintResult);
    constraintResults.put("test:minorConstraint", minorConstraintResult);
    constraintResults.put("test:majorConstraint", majorConstraintResult);
    when(inMemoryReportWriter.getConstraintResults()).thenReturn(constraintResults);
    int violations = reportHelper.verifyConstraintResults(Severity.MAJOR, Severity.CRITICAL, inMemoryReportWriter);
    assertThat(violations, equalTo(1));
    verifyMessages(debugMessages, ReportHelper.CONSTRAINT_VIOLATION_HEADER, "Constraint: test:infoConstraint", "Severity: MINOR (from INFO)", "InfoElement=InfoValue");
    verifyMessages(warnMessages, ReportHelper.CONSTRAINT_VIOLATION_HEADER, "Constraint: test:minorConstraint", "Severity: MAJOR (from MINOR)", "MinorElement=MinorValue");
    verifyMessages(errorMessages, ReportHelper.CONSTRAINT_VIOLATION_HEADER, "Constraint: test:majorConstraint", "Severity: CRITICAL (from MAJOR)", "MajorElement=MajorValue");
}
Also used : Constraint(com.buschmais.jqassistant.core.analysis.api.rule.Constraint) StringContains.containsString(org.hamcrest.core.StringContains.containsString) Constraint(com.buschmais.jqassistant.core.analysis.api.rule.Constraint) Result(com.buschmais.jqassistant.core.analysis.api.Result) Test(org.junit.Test)

Example 14 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 15 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)19 Test (org.junit.Test)12 Constraint (com.buschmais.jqassistant.core.analysis.api.rule.Constraint)7 StringContains.containsString (org.hamcrest.core.StringContains.containsString)6 RuleExecutorException (com.buschmais.jqassistant.core.rule.api.executor.RuleExecutorException)4 Concept (com.buschmais.jqassistant.core.analysis.api.rule.Concept)3 XmlReportWriter (com.buschmais.jqassistant.core.report.impl.XmlReportWriter)3 StringWriter (java.io.StringWriter)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 PackageDescriptor (com.buschmais.jqassistant.plugin.java.api.model.PackageDescriptor)1 A (com.buschmais.jqassistant.plugin.java.test.set.rules.dependency.packages.a.A)1 Query (com.buschmais.xo.api.Query)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 ScriptEngine (javax.script.ScriptEngine)1 ScriptEngineFactory (javax.script.ScriptEngineFactory)1