Java Code Examples for org.sonar.squidbridge.api.SourceFile#getCheckMessages()

The following examples show how to use org.sonar.squidbridge.api.SourceFile#getCheckMessages() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SavePipelineCheckTest.java    From sonar-flow-plugin with GNU Lesser General Public License v3.0 6 votes vote down vote up
@Test
public void savePipelineCheck() {
  List<SquidAstVisitor<Grammar>> metrics = new ArrayList<SquidAstVisitor<Grammar>>();
  metrics.add(new SimpleMetricVisitor());
  List<FlowCheck> checks = new ArrayList<FlowCheck>();
  checks.add(new SavePipelineCheck());

  String invalidPath = "src/test/resources/WmTestPackage/ns/I8cFlowSonarPluginTest"
      + "/pub/checkSavePipelineInvalid/flow.xml";
  String expectedMessage = "Remove service pub.flow:savePipeline";

  SourceFile sfViolation = FlowAstScanner.scanSingleFile(new File(invalidPath), checks, metrics);
  List<CheckMessage> violationMessages = new ArrayList<CheckMessage>(
      sfViolation.getCheckMessages());
  assertEquals(1, violationMessages.size());
  assertTrue("Returned check message not as expected",
      expectedMessage.equals(violationMessages.get(0).getDefaultMessage()));

}
 
Example 2
Source File: LuaSquidSensor.java    From sonar-lua with GNU Lesser General Public License v3.0 6 votes vote down vote up
private void saveViolations(SensorContext context, InputFile inputFile, SourceFile squidFile) {
  Collection<CheckMessage> messages = squidFile.getCheckMessages();
  if (messages != null) {

    for (CheckMessage message : messages) {
      RuleKey ruleKey = checks.ruleKey((SquidCheck<LexerlessGrammar>) message.getCheck());
      NewIssue newIssue = context.newIssue()
        .forRule(ruleKey)
        .gap(message.getCost());
      Integer line = message.getLine();
      NewIssueLocation location = newIssue.newLocation()
        .on(inputFile)
        .message(message.getText(Locale.ENGLISH));
      if (line != null) {
        location.at(inputFile.selectLine(line));
      }
      newIssue.at(location);
      newIssue.save();
    }
  }
}
 
Example 3
Source File: DisabledCheckTest.java    From sonar-flow-plugin with GNU Lesser General Public License v3.0 6 votes vote down vote up
@Test
public void disabledCheck() {
  List<SquidAstVisitor<Grammar>> metrics = new ArrayList<SquidAstVisitor<Grammar>>();
  metrics.add(new SimpleMetricVisitor());
  List<FlowCheck> checks = new ArrayList<FlowCheck>();
  checks.add(new DisabledCheck());

  String invalidPath = "src/test/resources/WmTestPackage/ns/I8cFlowSonarPluginTest"
      + "/pub/checkDisabledInvalid/flow.xml";
  String expectedMessage = "Remove disabled code";

  SourceFile sfViolation = FlowAstScanner.scanSingleFile(new File(invalidPath), checks, metrics);
  List<CheckMessage> violationMessages = new ArrayList<CheckMessage>(
      sfViolation.getCheckMessages());
  assertEquals(1, violationMessages.size());
  assertTrue("Returned check message not as expected",
      expectedMessage.equals(violationMessages.get(0).getDefaultMessage()));
}
 
Example 4
Source File: ExitCheckTest.java    From sonar-flow-plugin with GNU Lesser General Public License v3.0 6 votes vote down vote up
@Test
public void exitCheck() {
  List<SquidAstVisitor<Grammar>> metrics = new ArrayList<SquidAstVisitor<Grammar>>();
  metrics.add(new SimpleMetricVisitor());
  List<FlowCheck> checks = new ArrayList<FlowCheck>();
  checks.add(new ExitCheck());

  String invalidPath = "src/test/resources/WmTestPackage/ns/I8cFlowSonarPluginTest"
      + "/pub/checkExitStepInvalid/flow.xml";

  SourceFile sfViolation = FlowAstScanner.scanSingleFile(new File(invalidPath), checks, metrics);
  List<CheckMessage> violationMessages = new ArrayList<CheckMessage>(
      sfViolation.getCheckMessages());
  assertEquals(2, violationMessages.size());
  // TODO check both violation messages
}
 
Example 5
Source File: EmptyMapCheckTest.java    From sonar-flow-plugin with GNU Lesser General Public License v3.0 6 votes vote down vote up
@Test
public void emptyMapCheckValid() {
  List<SquidAstVisitor<Grammar>> metrics = new ArrayList<SquidAstVisitor<Grammar>>();
  metrics.add(new SimpleMetricVisitor());
  List<FlowCheck> checks = new ArrayList<FlowCheck>();
  checks.add(new EmptyMapCheck());

  // Valid
  String invalidPath = "src/test/resources/WmTestPackage/ns/I8cFlowSonarPluginTest"
      + "/pub/checkEmptyMapValid/flow.xml";

  SourceFile sfViolation = FlowAstScanner.scanSingleFile(new File(invalidPath), checks, metrics);
  List<CheckMessage> violationMessages = new ArrayList<CheckMessage>(
      sfViolation.getCheckMessages());
  assertEquals(0, violationMessages.size());
}
 
Example 6
Source File: EmptyMapCheckTest.java    From sonar-flow-plugin with GNU Lesser General Public License v3.0 6 votes vote down vote up
@Test
public void emptyMapCheckInvalid() {
  List<SquidAstVisitor<Grammar>> metrics = new ArrayList<SquidAstVisitor<Grammar>>();
  metrics.add(new SimpleMetricVisitor());
  List<FlowCheck> checks = new ArrayList<FlowCheck>();
  checks.add(new EmptyMapCheck());

  // Invalid
  String invalidPath = "src/test/resources/WmTestPackage/ns/I8cFlowSonarPluginTest"
      + "/pub/checkEmptyMapInvalid/flow.xml";
  String expectedMessage 
      = "This map step in the flow is empty, create content or remove the map.";

  SourceFile sfViolation = FlowAstScanner.scanSingleFile(new File(invalidPath), checks, metrics);
  List<CheckMessage> violationMessages = new ArrayList<CheckMessage>(
      sfViolation.getCheckMessages());
  assertEquals(1, violationMessages.size());
  assertTrue("Returned check message not as expected",
      expectedMessage.equals(violationMessages.get(0).getDefaultMessage()));
}
 
Example 7
Source File: ApexSquidSensor.java    From enforce-sonarqube-plugin with MIT License 6 votes vote down vote up
/**
 * Saves issues form input file and source file.
 *
 * @param sonarFile input file.
 * @param squidFile source file.
 */
private void saveIssues(InputFile sonarFile, SourceFile squidFile) {
    Collection<CheckMessage> messages = squidFile.getCheckMessages();
    messages.forEach(message -> {
        RuleKey ruleKey = checks.ruleKey((SquidAstVisitor<Grammar>) message.getCheck());
        Issuable issuable = resourcePerspectives.as(Issuable.class, sonarFile);

        if (issuable != null) {
            Issue issue = issuable.newIssueBuilder()
                    .ruleKey(ruleKey)
                    .line(message.getLine())
                    .message(message.getText(Locale.ENGLISH))
                    .build();
            issuable.addIssue(issue);
        }
    });
}
 
Example 8
Source File: QualifiedNameCheckTest.java    From sonar-flow-plugin with GNU Lesser General Public License v3.0 6 votes vote down vote up
@Test
public void qualifiedNameCheckInvalid() {
  Configuration cfg = mock(Configuration.class);
  Mockito.when(cfg.get(QualifiedNameCheck.QUALIFIED_NAME_KEY)).thenReturn(Optional.of("test.*"));
  FlowLanguage.setConfig(cfg);
  List<SquidAstVisitor<Grammar>> metrics = new ArrayList<SquidAstVisitor<Grammar>>();
  metrics.add(new SimpleMetricVisitor());
  List<FlowCheck> checks = new ArrayList<FlowCheck>();
  QualifiedNameCheck qnc = new QualifiedNameCheck();
  checks.add(qnc);
  String validPath = "src/test/resources/WmTestPackage/ns/I8cFlowSonarPluginTest"
      + "/pub/checkQualityNameInvalid/flow.xml";

  SourceFile sfCorrect = FlowAstScanner.scanSingleFile(new File(validPath), checks, metrics);
  Set<CheckMessage> scmCorrect = sfCorrect.getCheckMessages();
  assertEquals(1, scmCorrect.size());  
}
 
Example 9
Source File: QualifiedNameCheckTest.java    From sonar-flow-plugin with GNU Lesser General Public License v3.0 6 votes vote down vote up
@Test
public void qualifiedNameCheckValid() {
  Configuration cfg = mock(Configuration.class);
  Mockito.when(cfg.get(QualifiedNameCheck.QUALIFIED_NAME_KEY)).thenReturn(Optional.of(QualifiedNameCheck.QUALIFIED_NAME_DEFVALUE));
  FlowLanguage.setConfig(cfg);
  List<SquidAstVisitor<Grammar>> metrics = new ArrayList<SquidAstVisitor<Grammar>>();
  metrics.add(new SimpleMetricVisitor());
  List<FlowCheck> checks = new ArrayList<FlowCheck>();
  QualifiedNameCheck qnc = new QualifiedNameCheck();
  checks.add(qnc);
  String validPath = "src/test/resources/WmTestPackage/ns/I8cFlowSonarPluginTest"
      + "/pub/checkQualityNameInvalid/flow.xml";

  SourceFile sfCorrect = FlowAstScanner.scanSingleFile(new File(validPath), checks, metrics);
  Set<CheckMessage> scmCorrect = sfCorrect.getCheckMessages();
  assertEquals(0, scmCorrect.size());  
}
 
Example 10
Source File: EmptyFlowCheckTest.java    From sonar-flow-plugin with GNU Lesser General Public License v3.0 6 votes vote down vote up
@Test
public void emptyFlowCheck() {
  List<SquidAstVisitor<Grammar>> metrics = new ArrayList<SquidAstVisitor<Grammar>>();
  metrics.add(new SimpleMetricVisitor());
  List<FlowCheck> checks = new ArrayList<FlowCheck>();
  checks.add(new EmptyFlowCheck());

  String invalidPath = "src/test/resources/WmTestPackage/ns/I8cFlowSonarPluginTest"
      + "/pub/checkEmptyFlowInvalid/flow.xml";
  String expectedMessage 
      = "Service doesn't contain any flow steps. Remove service or add flow steps.";

  SourceFile sfViolation = FlowAstScanner.scanSingleFile(new File(invalidPath), checks, metrics);
  List<CheckMessage> violationMessages = new ArrayList<CheckMessage>(
      sfViolation.getCheckMessages());
  assertEquals(1, violationMessages.size());
  assertTrue("Returned check message not as expected",
      expectedMessage.equals(violationMessages.get(0).getDefaultMessage()));
}
 
Example 11
Source File: TryCatchCheckTest.java    From sonar-flow-plugin with GNU Lesser General Public License v3.0 6 votes vote down vote up
@Test
public void tryCatchCheckInvalid() {
  List<SquidAstVisitor<Grammar>> metrics = new ArrayList<SquidAstVisitor<Grammar>>();
  metrics.add(new SimpleMetricVisitor());
  List<FlowCheck> checks = new ArrayList<FlowCheck>();
  checks.add(new TryCatchCheck());

  // check invalid flow
  String invalidFlowPath = "src/test/resources/WmTestPackage/ns/I8cFlowSonarPluginTest"
      + "/pub/checkTryCatchInvalid/flow.xml";
  String expectedMessage = "Create try-catch sequence";

  SourceFile sfViolation = FlowAstScanner.scanSingleFile(new File(invalidFlowPath), checks,
      metrics);
  List<CheckMessage> violationMessages = new ArrayList<CheckMessage>(
      sfViolation.getCheckMessages());
  assertEquals(1, violationMessages.size());
  assertTrue("Returned check message not as expected",
      expectedMessage.equals(violationMessages.get(0).getDefaultMessage()));

}
 
Example 12
Source File: BranchPropertiesCheckTest.java    From sonar-flow-plugin with GNU Lesser General Public License v3.0 6 votes vote down vote up
@Test
public void branchPropertiesCheckInvalidB() {
  List<SquidAstVisitor<Grammar>> metrics = new ArrayList<SquidAstVisitor<Grammar>>();
  metrics.add(new SimpleMetricVisitor());
  List<FlowCheck> checks = new ArrayList<FlowCheck>();
  checks.add(new BranchPropertiesCheck());

  final String expectedMessageB 
      = "Evaluate labels must be true when no switch parameter is defined in BRANCH";

  // Check violation flow B: neither switch nor evaluate labels defined
  SourceFile sfViolationB = FlowAstScanner
      .scanSingleFile(new File("src/test/resources/WmTestPackage/ns/I8cFlowSonarPluginTest"
          + "/pub/checkBranchPropertiesInvalidB/flow.xml"), checks, metrics);
  List<CheckMessage> violationBMessages = new ArrayList<CheckMessage>(
      sfViolationB.getCheckMessages());
  assertEquals(1, violationBMessages.size());
  assertTrue("Returned check message not as expected",
      expectedMessageB.equals(violationBMessages.get(0).getDefaultMessage()));
}
 
Example 13
Source File: BranchPropertiesCheckTest.java    From sonar-flow-plugin with GNU Lesser General Public License v3.0 6 votes vote down vote up
@Test
public void branchPropertiesCheckInvalidA() {
  List<SquidAstVisitor<Grammar>> metrics = new ArrayList<SquidAstVisitor<Grammar>>();
  metrics.add(new SimpleMetricVisitor());
  List<FlowCheck> checks = new ArrayList<FlowCheck>();
  checks.add(new BranchPropertiesCheck());

  final String expectedMessageA 
      = "Both switch and evaluate labels are defined in properties of BRANCH";

  // Check violation flow A: both switch and evaluate labels defined
  SourceFile sfViolationA = FlowAstScanner
      .scanSingleFile(new File("src/test/resources/WmTestPackage/ns/I8cFlowSonarPluginTest"
          + "/pub/checkBranchPropertiesInvalidA/flow.xml"), checks, metrics);
  List<CheckMessage> violationAMessages = new ArrayList<CheckMessage>(
      sfViolationA.getCheckMessages());
  assertEquals(1, violationAMessages.size());
  assertTrue("Returned check message not as expected",
      expectedMessageA.equals(violationAMessages.get(0).getDefaultMessage()));
}
 
Example 14
Source File: FlowSquidSensor.java    From sonar-flow-plugin with GNU Lesser General Public License v3.0 6 votes vote down vote up
private void saveViolations(SensorContext context, InputFile inputFile, SourceFile squidFile) {
  Collection<CheckMessage> messages = squidFile.getCheckMessages();
  logger.debug("+++Nr of violations found: " + messages.size());
  if (messages != null) {
    for (CheckMessage message : messages) {
      Object c = message.getCheck();
      if (c instanceof FlowCheck) {
        FlowCheck fc = (FlowCheck) c;
        if (squidFile.getInt(FlowMetric.IS_TOP_LEVEL) != 1 && fc.isTopLevelCheck()) {
          logger.debug("+++Ignoring toplevelCheck for file: " + squidFile.getKey());
        } else {
          RuleKey ruleKey;
          if (fc.isNodeCheck()) {
            ruleKey = nodeChecks.ruleKey(fc);
          } else {
            ruleKey = flowChecks.ruleKey(fc);
          }
          FlowIssue.create(context, ruleKey, message.getCost())
              .setPrimaryLocation(inputFile, message).save();
        }
      }
    }
  }
}
 
Example 15
Source File: InterfaceCommentsCheckTest.java    From sonar-flow-plugin with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Test
public void interfaceCommentsCheckInvalid() {
  List<SquidAstVisitor<Grammar>> metrics = new ArrayList<SquidAstVisitor<Grammar>>();
  metrics.add(new SimpleMetricVisitor());
  List<FlowCheck> checks = new ArrayList<FlowCheck>();
  checks.add(new InterfaceCommentsCheck());

  String invalidPath = "src/test/resources/WmTestPackage/ns/I8cFlowSonarPluginTest"
      + "/pub/checkInterfaceCommentsInvalid/node.ndf";

  SourceFile sfCorrect = NodeAstScanner.scanSingleFile(new File(invalidPath), checks, metrics);
  Set<CheckMessage> scmCorrect = sfCorrect.getCheckMessages();
  assertEquals(2, scmCorrect.size());
}
 
Example 16
Source File: InterfaceCommentsCheckTest.java    From sonar-flow-plugin with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Test
public void interfaceCommentsCheckInvalid2() {
  List<SquidAstVisitor<Grammar>> metrics = new ArrayList<SquidAstVisitor<Grammar>>();
  metrics.add(new SimpleMetricVisitor());
  List<FlowCheck> checks = new ArrayList<FlowCheck>();
  checks.add(new InterfaceCommentsCheck());

  String invalidPath = "src/test/resources/WmTestPackage/ns/I8cFlowSonarPluginTest"
      + "/pub/checkInterfaceCommentsInvalid2/node.ndf";

  SourceFile sfCorrect = NodeAstScanner.scanSingleFile(new File(invalidPath), checks, metrics);
  Set<CheckMessage> scmCorrect = sfCorrect.getCheckMessages();
  assertEquals(4, scmCorrect.size());
}
 
Example 17
Source File: InterfaceCommentsCheckTest.java    From sonar-flow-plugin with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Test
public void interfaceCommentsCheckWs() {
  List<SquidAstVisitor<Grammar>> metrics = new ArrayList<SquidAstVisitor<Grammar>>();
  metrics.add(new SimpleMetricVisitor());
  List<FlowCheck> checks = new ArrayList<FlowCheck>();
  checks.add(new InterfaceCommentsCheck());

  String invalidPath = "src/test/resources/WmTestPackage/ns/I8cFlowSonarPluginTest"
      + "/pub/checkInterfaceCommentsWs/node.ndf";

  SourceFile sfCorrect = NodeAstScanner.scanSingleFile(new File(invalidPath), checks, metrics);
  Set<CheckMessage> scmCorrect = sfCorrect.getCheckMessages();
  assertEquals(0, scmCorrect.size());
}
 
Example 18
Source File: InterfaceCommentsCheckTest.java    From sonar-flow-plugin with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Test
public void interfaceCommentsCheckValid() {
  List<SquidAstVisitor<Grammar>> metrics = new ArrayList<SquidAstVisitor<Grammar>>();
  metrics.add(new SimpleMetricVisitor());
  List<FlowCheck> checks = new ArrayList<FlowCheck>();
  checks.add(new InterfaceCommentsCheck());

  String invalidPath = "src/test/resources/WmTestPackage/ns/I8cFlowSonarPluginTest"
      + "/pub/checkInterfaceCommentsValid/node.ndf";

  SourceFile sfCorrect = NodeAstScanner.scanSingleFile(new File(invalidPath), checks, metrics);
  Set<CheckMessage> scmCorrect = sfCorrect.getCheckMessages();
  assertEquals(0, scmCorrect.size());
}
 
Example 19
Source File: TryCatchCheckTest.java    From sonar-flow-plugin with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Test
public void tryCatchCheckValid() {
  List<SquidAstVisitor<Grammar>> metrics = new ArrayList<SquidAstVisitor<Grammar>>();
  metrics.add(new SimpleMetricVisitor());
  List<FlowCheck> checks = new ArrayList<FlowCheck>();
  checks.add(new TryCatchCheck());

  // check valid flow
  String validFlowPath 
      = "src/test/resources/WmTestPackage/ns/I8cFlowSonarPluginTest/pub/checkTryCatchValid/flow.xml";
  SourceFile sfCorrect = FlowAstScanner.scanSingleFile(new File(validFlowPath), checks, metrics);
  Set<CheckMessage> scmCorrect = sfCorrect.getCheckMessages();
  assertEquals(0, scmCorrect.size());

}
 
Example 20
Source File: BranchPropertiesCheckTest.java    From sonar-flow-plugin with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Test
public void branchPropertiesCheckValid() {
  List<SquidAstVisitor<Grammar>> metrics = new ArrayList<SquidAstVisitor<Grammar>>();
  metrics.add(new SimpleMetricVisitor());
  List<FlowCheck> checks = new ArrayList<FlowCheck>();
  checks.add(new BranchPropertiesCheck());

  // Check correct flow
  SourceFile sfCorrect = FlowAstScanner
      .scanSingleFile(new File("src/test/resources/WmTestPackage/ns/I8cFlowSonarPluginTest"
          + "/pub/checkBranchPropertiesValid/flow.xml"), checks, metrics);
  Set<CheckMessage> scmCorrect = sfCorrect.getCheckMessages();
  assertEquals(0, scmCorrect.size());
}