org.sonar.squidbridge.SquidAstVisitor Java Examples

The following examples show how to use org.sonar.squidbridge.SquidAstVisitor. 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: 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 #2
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 #3
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 #4
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 #5
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 #6
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 #7
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 #8
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 #9
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 #10
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 #11
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 #12
Source File: NodeAstScanner.java    From sonar-flow-plugin with GNU Lesser General Public License v3.0 6 votes vote down vote up
/**
 * Helper method for testing checks without having to deploy them on a Sonar instance.
 */
public static SourceFile scanSingleFile(File file, Collection<FlowCheck> nodeChecks,
    List<SquidAstVisitor<Grammar>> metrics) {
  if (!file.isFile()) {
    throw new IllegalArgumentException("File '" + file + "' not found.");
  }

  AstScanner<Grammar> scanner = create(new FlowConfiguration(Charsets.UTF_8), nodeChecks,
      metrics);
  scanner.scanFile(file);
  Collection<SourceCode> sources = scanner.getIndex().search(new QueryByType(SourceFile.class));
  if (sources.size() != 1) {
    throw new IllegalStateException(
        "Only one SourceFile was expected whereas " + sources.size() + " has been returned.");
  }
  return (SourceFile) sources.iterator().next();
}
 
Example #13
Source File: NodeAstScanner.java    From sonar-flow-plugin with GNU Lesser General Public License v3.0 6 votes vote down vote up
/**
 * Creates the AstScanner. It will set flow and metric checks, next to the default ones.
 * 
 * @param conf
 *          Configuration
 * @param checks
 *          Additional checks to set
 * @param metrics
 *          Additional metrics to set
 * @return
 */
public static AstScanner<Grammar> create(FlowConfiguration conf, Collection<FlowCheck> checks,
    @Nullable List<SquidAstVisitor<Grammar>> metrics) {
  final SquidAstVisitorContextImpl<Grammar> context = new SquidAstVisitorContextImpl<Grammar>(
      new SourceProject("Flow Project"));
  final Parser<Grammar> parser = NodeParser.create(conf);

  AstScanner.Builder<Grammar> builder = AstScanner.<Grammar>builder(context)
      .setBaseParser(parser);
  /* Required commentAnalyzer */
  builder.setCommentAnalyser(new CommentAnalyser());
  /* Metrics */
  ArrayList<SquidAstVisitor<Grammar>> metricList = new ArrayList<SquidAstVisitor<Grammar>>();
  metricList.addAll(MetricList.getDefaultNodeVisitors());
  if (metrics != null) {
    metricList.addAll(metrics);
  }
  setMetrics(builder, metricList);
  /* Checks */
  for (FlowCheck flowCheck : checks) {
    builder.withSquidAstVisitor(flowCheck);
  }
  return builder.build();
}
 
Example #14
Source File: FlowAstScanner.java    From sonar-flow-plugin with GNU Lesser General Public License v3.0 6 votes vote down vote up
/**
 * Creates the AstScanner. It will set flow and metric checks, next to the default ones.
 * 
 * @param conf
 *          Configuration
 * @param checks
 *          Additional checks to set
 * @param metrics
 *          Additional metrics to set
 * @return
 */
public static AstScanner<Grammar> create(FlowConfiguration conf, Collection<FlowCheck> checks,
    @Nullable List<SquidAstVisitor<Grammar>> metrics) {
  final SquidAstVisitorContextImpl<Grammar> context = new SquidAstVisitorContextImpl<Grammar>(
      new SourceProject("Flow Project"));
  final Parser<Grammar> parser = FlowParser.create(conf);

  AstScanner.Builder<Grammar> builder = AstScanner.<Grammar>builder(context)
      .setBaseParser(parser);
  /* Required commentAnalyzer */
  builder.setCommentAnalyser(new CommentAnalyser());
  /* Metrics */
  ArrayList<SquidAstVisitor<Grammar>> metricList = new ArrayList<SquidAstVisitor<Grammar>>();
  metricList.addAll(MetricList.getFlowVisitors());
  if (metrics != null) {
    metricList.addAll(metrics);
  }
  setMetrics(builder, metricList);
  /* Checks */
  for (FlowCheck flowCheck : checks) {
    builder.withSquidAstVisitor(flowCheck);
  }
  return builder.build();
}
 
Example #15
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 #16
Source File: LuaSquidSensor.java    From sonar-lua with GNU Lesser General Public License v3.0 6 votes vote down vote up
@Override
public void execute(SensorContext context) {
  FileSystem fileSystem = context.fileSystem();
  FilePredicates predicates = fileSystem.predicates();
  List<SquidAstVisitor<LexerlessGrammar>> visitors = new ArrayList<>(checks.all());
  visitors.add(new FileLinesVisitor(fileLinesContextFactory, fileSystem));
  LuaConfiguration configuration = new LuaConfiguration(fileSystem.encoding());

  scanner = LuaAstScanner.create(configuration, visitors);

  Iterable<java.io.File> files = fileSystem.files(
    predicates.and(
      predicates.hasType(InputFile.Type.MAIN),
      predicates.hasLanguage(Lua.KEY),
      inputFile -> !inputFile.absolutePath().endsWith("mxml")
    ));
  scanner.scanFiles(ImmutableList.copyOf(files));

  Collection<SourceCode> squidSourceFiles = scanner.getIndex().search(new QueryByType(SourceFile.class));
  save(context, squidSourceFiles);
}
 
Example #17
Source File: FlowLinesOfCodeVisitorTest.java    From sonar-flow-plugin with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Test
public void linesOfCodeTest() {
  logger.debug("Scanning file");
  List<SquidAstVisitor<Grammar>> metrics = new ArrayList<SquidAstVisitor<Grammar>>();
  List<FlowCheck> checks = new ArrayList<FlowCheck>();

  SourceFile sfCorrect = FlowAstScanner.scanSingleFile(new File(
      "src/test/resources/WmTestPackage/ns/I8cFlowSonarPluginTest"
      + "/pub/checkLinesOfCode/flow.xml"),
      checks, metrics);
  int lines = (int) sfCorrect.getDouble(FlowMetric.LINES_OF_CODE);
  assertEquals(13, lines);
}
 
Example #18
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 #19
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 #20
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 #21
Source File: FlowCommentLinesVisitorTest.java    From sonar-flow-plugin with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Test
public void commentLinesTest() {
  logger.debug("Scanning file");
  List<SquidAstVisitor<Grammar>> metrics = new ArrayList<SquidAstVisitor<Grammar>>();
  List<FlowCheck> checks = new ArrayList<FlowCheck>();

  SourceFile sfCorrect = FlowAstScanner.scanSingleFile(new File(
      "src/test/resources/WmTestPackage/ns/I8cFlowSonarPluginTest"
      + "/pub/commentLinesVisitor/flow.xml"),
      checks, metrics);
  int lines = (int) sfCorrect.getDouble(FlowMetric.COMMENT_LINES);
  assertEquals(2, lines);
}
 
Example #22
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 #23
Source File: FlowCommentLinesVisitorTest.java    From sonar-flow-plugin with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Test
public void commentLinesTest2() {
  logger.debug("Scanning file");
  List<SquidAstVisitor<Grammar>> metrics = new ArrayList<SquidAstVisitor<Grammar>>();
  metrics.add(new FlowCommentLinesVisitor<Grammar>(FlowMetric.COMMENT_LINES));
  List<FlowCheck> checks = new ArrayList<FlowCheck>();

  SourceFile sfCorrect = FlowAstScanner.scanSingleFile(new File(
      "src/test/resources/WmTestPackage/ns/I8cFlowSonarPluginTest"
      + "/pub/commentLinesVisitor2/flow.xml"),
      checks, metrics);
  int lines = (int) sfCorrect.getDouble(FlowMetric.COMMENT_LINES);
  assertEquals(0, lines);
}
 
Example #24
Source File: LuaAstScanner.java    From sonar-lua with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * Helper method for testing checks without having to deploy them on a Sonar instance.
 */
public static SourceFile scanSingleFile(File file, SquidAstVisitor<LexerlessGrammar>... visitors) {
  if (!file.isFile()) {
    throw new IllegalArgumentException("File '" + file + "' not found.");
  }

  AstScanner<LexerlessGrammar> scanner = create(new LuaConfiguration(Charsets.UTF_8), Arrays.asList(visitors));
  scanner.scanFile(file);
  Collection<SourceCode> sources = scanner.getIndex().search(new QueryByType(SourceFile.class));
  if (sources.size() != 1) {
    throw new IllegalStateException("Only one SourceFile was expected whereas " + sources.size() + " has been returned.");
  }
  return (SourceFile) sources.iterator().next();
}
 
Example #25
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 #26
Source File: ApexSquidSensor.java    From enforce-sonarqube-plugin with MIT License 5 votes vote down vote up
/**
 * Default construct to initialize the variables.
 *
 * @param fileSystem source files.
 * @param perspectives perspective from resources.
 * @param checkFactory factory to create a check.
 */
public ApexSquidSensor(FileSystem fileSystem, ResourcePerspectives perspectives, CheckFactory checkFactory) {
    this.checks = checkFactory
            .<SquidAstVisitor<Grammar>>create(CheckList.REPOSITORY_KEY)
            .addAnnotatedChecks(CheckList.getChecks());
    this.fileSystem = fileSystem;
    this.resourcePerspectives = perspectives;

    FilePredicates predicates = fileSystem.predicates();
    filePredicate = predicates.and(
            predicates.hasType(InputFile.Type.MAIN),
            predicates.hasLanguage(Apex.KEY));
}
 
Example #27
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());
}
 
Example #28
Source File: NodeAstScannerTest.java    From sonar-flow-plugin with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Test
public void scanFile() {
  logger.debug("Scanning file");
  List<SquidAstVisitor<Grammar>> metrics = new ArrayList<SquidAstVisitor<Grammar>>();
  metrics.add(new SimpleMetricVisitor());
  List<FlowCheck> checks = new ArrayList<FlowCheck>();
  NodeAstScanner.scanSingleFile(nodeFile, checks, metrics);
}
 
Example #29
Source File: FlowAstScannerTest.java    From sonar-flow-plugin with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Test
public void gitFolderTest() {
  logger.debug("Scanning file");
  List<SquidAstVisitor<Grammar>> metrics = new ArrayList<SquidAstVisitor<Grammar>>();
  metrics.add(new SimpleMetricVisitor());
  List<FlowCheck> checks = new ArrayList<FlowCheck>();
  FlowAstScanner.scanSingleFile(new File("src/test/resources/WmTestPackage/.gitx/someFile"), checks,
      metrics);

}
 
Example #30
Source File: MetricList.java    From sonar-flow-plugin with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * This static method returns the list of the required default node visitors.
 * @return List of available required default node visitors
 */
public static List<SquidAstVisitor<Grammar>> getDefaultNodeVisitors() {
  return ImmutableList.<SquidAstVisitor<Grammar>>builder()
      .add(ComplexityVisitor.<Grammar>builder().setMetricDef(FlowMetric.COMPLEXITY)
          .subscribeTo(complexityAstNodeType).build())
      .add(new LinesVisitor<Grammar>(FlowMetric.LINES)).build();
}