@Test public void testRuleImplementationDetailsAreParsedProperly() throws Exception { File baseFile = temp.newFile(); FileUtils.copyURLToFile(getClass().getResource("/external/rule2.customRules"), baseFile); SqlRules rules = AdhocRulesProvider.read(baseFile); Assert.assertEquals(1, rules.getRule().size()); Rule rule = rules.getRule().get(0); Assert.assertNotNull(rule.getRuleImplementation()); RuleImplementation ri = rule.getRuleImplementation(); Assert.assertEquals(2, ri.getNames().getTextItem().size()); Assert.assertEquals(1, ri.getTextToFind().getTextItem().size()); Assert.assertEquals(3, ri.getChildrenRules().getRuleImplementation().size()); Assert.assertEquals(1, ri.getParentRules().getRuleImplementation().size()); Assert.assertEquals(1, ri.getSiblingsRules().getRuleImplementation().size()); Assert.assertEquals(2, ri.getUsesRules().getRuleImplementation().size()); Assert.assertEquals(2, ri.getCompliantRulesCodeExamples().getRuleCodeExample().size()); Assert.assertEquals(1, ri.getViolatingRulesCodeExamples().getRuleCodeExample().size()); Assert.assertEquals(2, ri.getDistance()); Assert.assertEquals(3, ri.getIndex()); Assert.assertEquals(4, ri.getTimes()); Assert.assertEquals(RuleDistanceIndexMatchType.EQUALS, ri.getDistanceCheckType()); Assert.assertEquals(RuleDistanceIndexMatchType.LESS, ri.getIndexCheckType()); Assert.assertEquals(RuleMatchType.TEXT_AND_CLASS, ri.getRuleMatchType()); Assert.assertEquals(RuleMode.GROUP, ri.getRuleMode()); Assert.assertEquals(RuleResultType.FAIL_IF_FOUND, ri.getRuleResultType()); Assert.assertEquals("msg", ri.getRuleViolationMessage()); Assert.assertEquals(TextCheckType.STRICT, ri.getTextCheckType()); Assert.assertEquals("test", ri.getParentRules().getRuleImplementation().get(0).getRuleViolationMessage()); }
public static SqlRules read(final File file) throws Exception { try (BOMInputStream stream = new BOMInputStream(new FileInputStream(file))) { SqlRules rules = new SqlRules(); DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); Document doc = builder.parse(stream); Element root = doc.getDocumentElement(); rules.setDialect(XmlHelper.readAttribute(root, "dialect", "tsql")); rules.setRepoName(XmlHelper.readAttribute(root, "repoName", null)); rules.setRepoKey(XmlHelper.readAttribute(root, "repoKey", null)); rules.setIsAdhoc(true); rules.setIsExternal(true); NodeList list = doc.getElementsByTagName("rule"); for (int i = 0; i < list.getLength(); i++) { try { Node parent = list.item(i); org.sonar.plugins.sql.models.rules.Rule rule = new Rule(); rule.setKey(XmlHelper.getNodeValue(parent, "key")); rule.setName(XmlHelper.getNodeValue(parent, "name")); rule.setInternalKey(XmlHelper.getNodeValue(parent, "internalKey")); rule.setDescription(XmlHelper.getNodeValue(parent, "description")); rule.setSeverity(XmlHelper.getNodeValue(parent, "severity")); rule.setRemediationFunction(XmlHelper.getNodeValue(parent, "remediationFunction")); rule.setDebtRemediationFunctionCoefficient( XmlHelper.getNodeValue(parent, "debtRemediationFunctionCoefficient")); rule.setTag(XmlHelper.getNodeValue(parent, "tag")); rule.setRuleType(XmlHelper.getNodeValue2(parent, "ruleType", v -> v, "CODE_SMELL")); Node imp = XmlHelper.getNode(parent, "ruleImplementation"); List<RuleImplementation> main = new LinkedList<RuleImplementation>(); readNode(main, imp); if (main.isEmpty()) { continue; } rule.setRuleImplementation(main.get(0)); rules.getRule().add(rule); } catch (Exception e) { e.printStackTrace(); } } return rules; } }
AdhocRulesProvider { public static SqlRules read(final File file) throws Exception { try (BOMInputStream stream = new BOMInputStream(new FileInputStream(file))) { SqlRules rules = new SqlRules(); DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); Document doc = builder.parse(stream); Element root = doc.getDocumentElement(); rules.setDialect(XmlHelper.readAttribute(root, "dialect", "tsql")); rules.setRepoName(XmlHelper.readAttribute(root, "repoName", null)); rules.setRepoKey(XmlHelper.readAttribute(root, "repoKey", null)); rules.setIsAdhoc(true); rules.setIsExternal(true); NodeList list = doc.getElementsByTagName("rule"); for (int i = 0; i < list.getLength(); i++) { try { Node parent = list.item(i); org.sonar.plugins.sql.models.rules.Rule rule = new Rule(); rule.setKey(XmlHelper.getNodeValue(parent, "key")); rule.setName(XmlHelper.getNodeValue(parent, "name")); rule.setInternalKey(XmlHelper.getNodeValue(parent, "internalKey")); rule.setDescription(XmlHelper.getNodeValue(parent, "description")); rule.setSeverity(XmlHelper.getNodeValue(parent, "severity")); rule.setRemediationFunction(XmlHelper.getNodeValue(parent, "remediationFunction")); rule.setDebtRemediationFunctionCoefficient( XmlHelper.getNodeValue(parent, "debtRemediationFunctionCoefficient")); rule.setTag(XmlHelper.getNodeValue(parent, "tag")); rule.setRuleType(XmlHelper.getNodeValue2(parent, "ruleType", v -> v, "CODE_SMELL")); Node imp = XmlHelper.getNode(parent, "ruleImplementation"); List<RuleImplementation> main = new LinkedList<RuleImplementation>(); readNode(main, imp); if (main.isEmpty()) { continue; } rule.setRuleImplementation(main.get(0)); rules.getRule().add(rule); } catch (Exception e) { e.printStackTrace(); } } return rules; } } }
AdhocRulesProvider { public static SqlRules read(final File file) throws Exception { try (BOMInputStream stream = new BOMInputStream(new FileInputStream(file))) { SqlRules rules = new SqlRules(); DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); Document doc = builder.parse(stream); Element root = doc.getDocumentElement(); rules.setDialect(XmlHelper.readAttribute(root, "dialect", "tsql")); rules.setRepoName(XmlHelper.readAttribute(root, "repoName", null)); rules.setRepoKey(XmlHelper.readAttribute(root, "repoKey", null)); rules.setIsAdhoc(true); rules.setIsExternal(true); NodeList list = doc.getElementsByTagName("rule"); for (int i = 0; i < list.getLength(); i++) { try { Node parent = list.item(i); org.sonar.plugins.sql.models.rules.Rule rule = new Rule(); rule.setKey(XmlHelper.getNodeValue(parent, "key")); rule.setName(XmlHelper.getNodeValue(parent, "name")); rule.setInternalKey(XmlHelper.getNodeValue(parent, "internalKey")); rule.setDescription(XmlHelper.getNodeValue(parent, "description")); rule.setSeverity(XmlHelper.getNodeValue(parent, "severity")); rule.setRemediationFunction(XmlHelper.getNodeValue(parent, "remediationFunction")); rule.setDebtRemediationFunctionCoefficient( XmlHelper.getNodeValue(parent, "debtRemediationFunctionCoefficient")); rule.setTag(XmlHelper.getNodeValue(parent, "tag")); rule.setRuleType(XmlHelper.getNodeValue2(parent, "ruleType", v -> v, "CODE_SMELL")); Node imp = XmlHelper.getNode(parent, "ruleImplementation"); List<RuleImplementation> main = new LinkedList<RuleImplementation>(); readNode(main, imp); if (main.isEmpty()) { continue; } rule.setRuleImplementation(main.get(0)); rules.getRule().add(rule); } catch (Exception e) { e.printStackTrace(); } } return rules; } }  }
AdhocRulesProvider { public static SqlRules read(final File file) throws Exception { try (BOMInputStream stream = new BOMInputStream(new FileInputStream(file))) { SqlRules rules = new SqlRules(); DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); Document doc = builder.parse(stream); Element root = doc.getDocumentElement(); rules.setDialect(XmlHelper.readAttribute(root, "dialect", "tsql")); rules.setRepoName(XmlHelper.readAttribute(root, "repoName", null)); rules.setRepoKey(XmlHelper.readAttribute(root, "repoKey", null)); rules.setIsAdhoc(true); rules.setIsExternal(true); NodeList list = doc.getElementsByTagName("rule"); for (int i = 0; i < list.getLength(); i++) { try { Node parent = list.item(i); org.sonar.plugins.sql.models.rules.Rule rule = new Rule(); rule.setKey(XmlHelper.getNodeValue(parent, "key")); rule.setName(XmlHelper.getNodeValue(parent, "name")); rule.setInternalKey(XmlHelper.getNodeValue(parent, "internalKey")); rule.setDescription(XmlHelper.getNodeValue(parent, "description")); rule.setSeverity(XmlHelper.getNodeValue(parent, "severity")); rule.setRemediationFunction(XmlHelper.getNodeValue(parent, "remediationFunction")); rule.setDebtRemediationFunctionCoefficient( XmlHelper.getNodeValue(parent, "debtRemediationFunctionCoefficient")); rule.setTag(XmlHelper.getNodeValue(parent, "tag")); rule.setRuleType(XmlHelper.getNodeValue2(parent, "ruleType", v -> v, "CODE_SMELL")); Node imp = XmlHelper.getNode(parent, "ruleImplementation"); List<RuleImplementation> main = new LinkedList<RuleImplementation>(); readNode(main, imp); if (main.isEmpty()) { continue; } rule.setRuleImplementation(main.get(0)); rules.getRule().add(rule); } catch (Exception e) { e.printStackTrace(); } } return rules; } }  static SqlRules read(final File file); List<SqlRules> getAdhocRules(String searchSuffix, String baseDir, String... paths); }
AdhocRulesProvider { public static SqlRules read(final File file) throws Exception { try (BOMInputStream stream = new BOMInputStream(new FileInputStream(file))) { SqlRules rules = new SqlRules(); DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); Document doc = builder.parse(stream); Element root = doc.getDocumentElement(); rules.setDialect(XmlHelper.readAttribute(root, "dialect", "tsql")); rules.setRepoName(XmlHelper.readAttribute(root, "repoName", null)); rules.setRepoKey(XmlHelper.readAttribute(root, "repoKey", null)); rules.setIsAdhoc(true); rules.setIsExternal(true); NodeList list = doc.getElementsByTagName("rule"); for (int i = 0; i < list.getLength(); i++) { try { Node parent = list.item(i); org.sonar.plugins.sql.models.rules.Rule rule = new Rule(); rule.setKey(XmlHelper.getNodeValue(parent, "key")); rule.setName(XmlHelper.getNodeValue(parent, "name")); rule.setInternalKey(XmlHelper.getNodeValue(parent, "internalKey")); rule.setDescription(XmlHelper.getNodeValue(parent, "description")); rule.setSeverity(XmlHelper.getNodeValue(parent, "severity")); rule.setRemediationFunction(XmlHelper.getNodeValue(parent, "remediationFunction")); rule.setDebtRemediationFunctionCoefficient( XmlHelper.getNodeValue(parent, "debtRemediationFunctionCoefficient")); rule.setTag(XmlHelper.getNodeValue(parent, "tag")); rule.setRuleType(XmlHelper.getNodeValue2(parent, "ruleType", v -> v, "CODE_SMELL")); Node imp = XmlHelper.getNode(parent, "ruleImplementation"); List<RuleImplementation> main = new LinkedList<RuleImplementation>(); readNode(main, imp); if (main.isEmpty()) { continue; } rule.setRuleImplementation(main.get(0)); rules.getRule().add(rule); } catch (Exception e) { e.printStackTrace(); } } return rules; } }  static SqlRules read(final File file); List<SqlRules> getAdhocRules(String searchSuffix, String baseDir, String... paths);  }