@Override public void initialize() throws RuleException { try { tempDir = Files.createTempDirectory("jQA").toFile(); } catch (IOException e) { throw new RuleException("Cannot create temporary directory."); } }
private JavaRule getJavaRule(String className) throws RuleException { try { Class<?> javaRuleClass = Class.forName(className); if (!JavaRule.class.isAssignableFrom(JavaRule.class)) { throw new RuleException("Java '" + javaRuleClass.getName() + "' does not implement interface '" + JavaRule.class.getName() + "'."); } return JavaRule.class.cast(javaRuleClass.newInstance()); } catch (ReflectiveOperationException e) { throw new RuleException("Cannot create instance of '" + className + "'.", e); } } }
@Override public <T extends ExecutableRule<?>> Result.Status verify(T executable, List<String> columnNames, List<Map<String, Object>> rows, Verification verification) throws RuleException { VerificationStrategy strategy = verificationStrategies.get(verification.getClass()); if (strategy == null) { throw new RuleException("Result verification not supported: " + verification.getClass().getName()); } return strategy.verify(executable, verification, columnNames, rows); }
@Override public void destroy() throws RuleException { if (asciidoctor != null) { asciidoctor.shutdown(); } try { FileUtils.deleteDirectory(tempDir); } catch (IOException e) { throw new RuleException("Cannot delete temporary directory: " + tempDir); } }
/** * Retrieves severity based on string representation. * * @param value * string representation; {@code null} if no matching severity * found. * @return {@link Severity} */ public static Severity fromValue(String value) throws RuleException { if (value == null) { return null; } for (Severity severity : EnumSet.allOf(Severity.class)) { if (severity.value.equals(value.toLowerCase())) { return severity; } } throw new RuleException("Unknown severity '" + value + "'"); }
private Type toType(String value, RuleContext context) throws RuleException { try { return Type.valueOf(value.toUpperCase()); } catch (IllegalArgumentException e) { throw new RuleException("'" + value + "' is not a supported type for a parameter"); } }
@Override public boolean accepts(RuleSource ruleSource) throws RuleException { try { boolean acceptable = ruleSource.getURL().toExternalForm().toLowerCase().endsWith(YAML_EXTENSION_LONG) || ruleSource.getURL().toExternalForm().toLowerCase().endsWith(YAML_EXTENSION_SHORT); return acceptable; } catch (IOException e) { throw new RuleException("Unable to get the URL of the rule source.", e); } }
private void marshal(Writer writer, JqassistantRules rules) throws RuleException { XMLOutputFactory xof = XMLOutputFactory.newInstance(); XMLStreamWriter streamWriter; try { streamWriter = xof.createXMLStreamWriter(writer); } catch (XMLStreamException e) { throw new RuleException("Cannot create stream writer.", e); } XMLStreamWriter indentingStreamWriter = new IndentingXMLStreamWriter(new CDataXMLStreamWriter(streamWriter)); try { Marshaller marshaller = jaxbContext.createMarshaller(); marshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8"); marshaller.marshal(rules, indentingStreamWriter); } catch (JAXBException e) { throw new RuleException("Cannot write rules to " + writer, e); } }
public Object parse(String value) throws RuleException { try { return value != null ? parseString(value) : null; } catch (NumberFormatException e) { throw new RuleException("Cannot parse value " + value + " for type " + name()); } };
private Map<String, Object> getRuleParameters(ExecutableRule executableRule) throws RuleException { Map<String, Object> ruleParameters = new HashMap<>(); Map<String, Parameter> parameters = executableRule.getParameters(); for (Map.Entry<String, Parameter> entry : parameters.entrySet()) { String parameterName = entry.getKey(); Parameter parameter = entry.getValue(); Object parameterValue; String parameterValueAsString = this.ruleParameters.get(parameterName); if (parameterValueAsString != null) { try { parameterValue = parameter.getType().parse(parameterValueAsString); } catch (RuleException e) { throw new RuleException("Cannot determine value for parameter " + parameterName + "' of rule '" + executableRule + "'."); } } else { parameterValue = parameter.getDefaultValue(); } if (parameterValue == null) { throw new RuleException("No value or default value defined for required parameter '" + parameterName + "' of rule '" + executableRule + "'."); } ruleParameters.put(parameterName, parameterValue); } return ruleParameters; }
missingKeys.removeAll(givenKeys); throw new RuleException("Rule source '" + context.getSource().getId() + "' contains the group '" + containingRuleId + "' with an " + "included " + role + " without the following required " + .collect(toList()); throw new RuleException("Rule source '" + context.getSource().getId() + "' contains the group '" + containingRuleId + "' with an included " + role + throw new RuleException("Rule source '" + context.getSource().getId() + "' contains the group '" + containingRuleId + "' with an " + "included " + role + " without specified value for its " +
throw new RuleException("Result contains no columns, at least one with a numeric value is expected."); Object value = row.get(column); if (value == null) { throw new RuleException("The result does not contain a column '" + column); } else if (!Number.class.isAssignableFrom(value.getClass())) { throw new RuleException("The value in column '" + column + "' must be a numeric value but was '" + value + "'");
missing.removeAll(givenKeys); throw new RuleException("The concept '" + conceptId + "' in rule source '" + context.getSource().getId() + "' has an invalid parameter. The " + "following keys are missing: " + String.join(", ", missing)); } else if (unsupportedKeysGiven) { throw new RuleException("The concept '" + conceptId + "' in rule source '" + context.getSource().getId() + "' has an invalid parameter. The " + "following keys are not supported: " + throw new RuleException("A parameter of concept '" + conceptId + "' " + "in rule source '" + context.getSource().getId() + "' has " + "no name"); } else if (typeVal == null || typeVal.isEmpty()) { throw new RuleException("The parameter '" + nameVal + "' of concept '" + conceptId + "' " + "in rule source '" + context.getSource().getId() + "' has no " + "parameter type specified"); } else if (defaultVal == null && parameterSpec.containsKey(PARAMETER_DEFAULT_VALUE)) { throw new RuleException("The parameter '" + nameVal + "' of concept '" + conceptId + "' " + "in rule source '" + context.getSource().getId() + "' has " + "no default value");
availableLanguages.addAll(factory.getNames()); throw new RuleException("Cannot resolve scripting engine for '" + language + "', available languages are " + availableLanguages); scriptResult = scriptEngine.eval(executable.getSource()); } catch (ScriptException e) { throw new RuleException("Cannot execute script.", e); throw new RuleException("Script returned an invalid result type, expected " + Result.class.getName() + " but got " + scriptResult);
private <T extends ExecutableRule> Result<T> execute(T executableRule, Severity severity) throws RuleException { Map<String, Object> ruleParameters = getRuleParameters(executableRule); Executable<?> executable = executableRule.getExecutable(); Collection<RuleInterpreterPlugin> languagePlugins = ruleInterpreterPlugins.get(executable.getLanguage()); if (languagePlugins == null) { throw new RuleException("Could not determine plugin to execute " + executableRule); } for (RuleInterpreterPlugin languagePlugin : languagePlugins) { if (languagePlugin.accepts(executableRule)) { Result<T> result = languagePlugin.execute(executableRule, ruleParameters, severity, analyzerContext); if (result != null) { return result; } } } throw new RuleException("No plugin for language '" + executable.getLanguage() + "' returned a result for " + executableRule); }
givenKeys.removeAll(TOPLEVEL_KEYS); throw new RuleException("Rule source '" + context.getSource().getId() + "' contains the " + "following unsupported keys: " + String.join(", ", givenKeys));
.collect(toList()); throw new RuleException("Rule source with id '" + context.getSource().getId() + "' " + "contains a group with the following missing keys: " + String.join(", ", missing)); givenKeys.removeAll(GROUP_KEYS); throw new RuleException("Rule source with id '" + context.getSource().getId() + "' contains " + "a group containing the following unsupported keys: " + String.join(", ", givenKeys));
protected void doParse(RuleSource ruleSource, RuleSetBuilder ruleSetBuilder) throws RuleException { RuleContext context = new RuleContext(ruleSource, ruleSetBuilder); try (InputStream inputStream = ruleSource.getInputStream(); Reader reader = new YamlUnicodeReader(inputStream)) { LoadSettings settings = new LoadSettingsBuilder().build(); Load load = new Load(settings); Iterable<Object> objects = load.loadAllFromReader(reader); for (Object object : objects) { // if read document is empty, object might be null if (null == object) { continue; } if (Map.class.isAssignableFrom(object.getClass())) { processDocument((Map<String, Object>)object, context); } else { throw new RuleException("Cannot process rules from '" + ruleSource.getId() + "'."); } } } catch (IOException e) { throw new RuleException("Cannot read rules from '" + ruleSource.getId() + "'.", e); } catch (ClassCastException e) { throw new RuleException("Cannot process rules from '" + ruleSource.getId() + "' " + "because of an invalid YAML datastructure"); } }
protected <T extends ExecutableRule<?>> Result<T> execute(String cypher, T executableRule, Map<String, Object> parameters, Severity severity, AnalyzerContext context) throws RuleException { List<Map<String, Object>> rows = new ArrayList<>(); context.getLogger().debug("Executing query '" + cypher + "' with parameters [" + parameters + "]"); List<String> columnNames = null; try (Query.Result<Query.Result.CompositeRowObject> compositeRowObjects = context.getStore().executeQuery(cypher, parameters)) { for (Query.Result.CompositeRowObject rowObject : compositeRowObjects) { if (columnNames == null) { columnNames = new ArrayList<>(rowObject.getColumns()); } Map<String, Object> row = new LinkedHashMap<>(); for (String columnName : columnNames) { row.put(columnName, rowObject.get(columnName, Object.class)); } rows.add(row); } } catch (Exception e) { throw new RuleException("Cannot execute query for rule '" + executableRule + "'.", e); } Status status = getStatus(executableRule, columnNames, rows, context); return Result.<T>builder().rule(executableRule).status(status).severity(severity).columnNames(columnNames).rows(rows).build(); }
@Override public <T extends ExecutableRule<?>> Result<T> execute(T executableRule, Map<String, Object> ruleParameters, Severity severity, AnalyzerContext context) throws RuleException { SourceExecutable<String> executable = (SourceExecutable<String>) executableRule.getExecutable(); String source = executable.getSource(); Constructor c = new Constructor(YamlRuleSource.class); c.setPropertyUtils(new PropertyUtils() { @Override public Property getProperty(Class<? extends Object> type, String name) { return super.getProperty(type, LOWER_HYPHEN.to(LOWER_CAMEL, name)); } }); YamlRuleSource yamlRuleSource = new Yaml(c).loadAs(source, YamlRuleSource.class); String javaRuleClass = yamlRuleSource.getJavaRule(); if (javaRuleClass == null) { throw new RuleException("'java-rule-class' is required."); } JavaRule javaRule = getJavaRule(javaRuleClass); return javaRule.execute(executableRule, yamlRuleSource.getConfiguration(), ruleParameters, severity, context); }