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);
}
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();
}
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();
}
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);
}
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);
}
}
Aggregations