OpenEdgeWarningsSensor implements Sensor { @Override public void execute(SensorContext context) { if (context.runtime().getProduct() == SonarProduct.SONARLINT) return; settings.init(); int warningsImportNum = 0; final RuleKey defaultWarningRuleKey = RuleKey.of(Constants.STD_REPOSITORY_KEY, OpenEdgeRulesDefinition.COMPILER_WARNING_RULEKEY); if (context.activeRules().find(defaultWarningRuleKey) == null) { LOG.info("'Compiler warning' rule is not activated in your profile - Warning files analysis skipped"); return; } FilePredicates predicates = context.fileSystem().predicates(); for (InputFile file : context.fileSystem().inputFiles(predicates.and( predicates.hasLanguage(Constants.LANGUAGE_KEY), predicates.hasType(Type.MAIN)))) { LOG.debug("Looking for warnings of {}", file); processFile(context, file); warningsImportNum++; if (context.isCancelled()) { LOG.info("Analysis cancelled..."); return; } } LOG.info("{} warning files imported", warningsImportNum); } OpenEdgeWarningsSensor(OpenEdgeSettings settings); @Override void describe(SensorDescriptor descriptor); @Override void execute(SensorContext context);  }
@Test public void testWarnings() throws IOException { SensorContextTester context = TestProjectSensorContext.createContext(); context.setActiveRules(createRules1()); OpenEdgeSettings oeSettings = new OpenEdgeSettings(context.config(), context.fileSystem(), OpenEdgePluginTest.SONARQUBE_RUNTIME, OpenEdgePluginTest.SERVER); OpenEdgeWarningsSensor sensor = new OpenEdgeWarningsSensor(oeSettings); sensor.execute(context); Assert.assertEquals(context.allIssues().size(), 4); Iterator<Issue> issues = context.allIssues().iterator(); Issue issue; issue = issues.next(); assertEquals(issue.primaryLocation().inputComponent().key(), TestProjectSensorContext.BASEDIR + ":" + TestProjectSensorContext.FILE4); assertEquals(issue.primaryLocation().textRange().start().line(), 2); issue = issues.next(); assertEquals(issue.primaryLocation().inputComponent().key(), TestProjectSensorContext.BASEDIR + ":" + TestProjectSensorContext.FILE4); assertEquals(issue.primaryLocation().textRange().start().line(), 3); assertEquals(issue.ruleKey(), RuleKey.of(Constants.STD_REPOSITORY_KEY, OpenEdgeRulesDefinition.COMPILER_WARNING_1688_RULEKEY)); issue = issues.next(); assertEquals(issue.primaryLocation().inputComponent().key(), TestProjectSensorContext.BASEDIR + ":" + TestProjectSensorContext.FILE4); assertEquals(issue.primaryLocation().textRange().start().line(), 3); assertTrue(issue.primaryLocation().message().endsWith("Another one (1689)")); assertEquals(issue.ruleKey(), RuleKey.of(Constants.STD_REPOSITORY_KEY, OpenEdgeRulesDefinition.COMPILER_WARNING_RULEKEY)); issue = issues.next(); assertEquals(issue.primaryLocation().inputComponent().key(), TestProjectSensorContext.BASEDIR + ":" + TestProjectSensorContext.FILE1); assertEquals(issue.primaryLocation().textRange().start().line(), 1); assertEquals(issue.primaryLocation().message(), "Program src\\procedures\\sample\\inc\\test.i, Line 1 is an expression statement that evaluates to a constant."); }
@Test public void testWarnings02() throws IOException { SensorContextTester context = TestProjectSensorContext.createContext(); context.setActiveRules(createRules2()); OpenEdgeSettings oeSettings = new OpenEdgeSettings(context.config(), context.fileSystem(), OpenEdgePluginTest.SONARQUBE_RUNTIME, OpenEdgePluginTest.SERVER); OpenEdgeWarningsSensor sensor = new OpenEdgeWarningsSensor(oeSettings); sensor.execute(context); Assert.assertEquals(context.allIssues().size(), 3); Iterator<Issue> issues = context.allIssues().iterator(); Issue issue; issue = issues.next(); assertEquals(issue.primaryLocation().inputComponent().key(), TestProjectSensorContext.BASEDIR + ":" + TestProjectSensorContext.FILE4); assertEquals(issue.primaryLocation().textRange().start().line(), 2); issue = issues.next(); assertEquals(issue.primaryLocation().inputComponent().key(), TestProjectSensorContext.BASEDIR + ":" + TestProjectSensorContext.FILE4); assertEquals(issue.primaryLocation().textRange().start().line(), 3); assertTrue(issue.primaryLocation().message().endsWith("Another one (1689)")); assertEquals(issue.ruleKey(), RuleKey.of(Constants.STD_REPOSITORY_KEY, OpenEdgeRulesDefinition.COMPILER_WARNING_RULEKEY)); issue = issues.next(); assertEquals(issue.primaryLocation().inputComponent().key(), TestProjectSensorContext.BASEDIR + ":" + TestProjectSensorContext.FILE1); assertEquals(issue.primaryLocation().textRange().start().line(), 1); assertEquals(issue.primaryLocation().message(), "Program src\\procedures\\sample\\inc\\test.i, Line 1 is an expression statement that evaluates to a constant."); }
@Test public void testWarningsDeactivated() throws IOException { SensorContextTester context = TestProjectSensorContext.createContext(); context.setActiveRules(createRules0()); OpenEdgeSettings oeSettings = new OpenEdgeSettings(context.config(), context.fileSystem(), OpenEdgePluginTest.SONARQUBE_RUNTIME, OpenEdgePluginTest.SERVER); OpenEdgeWarningsSensor sensor = new OpenEdgeWarningsSensor(oeSettings); sensor.execute(context); Assert.assertEquals(context.allIssues().size(), 0); }