@Test
  public void should_analyse() {
    ProjectFileSystem fs = mock(ProjectFileSystem.class);
    when(fs.getSourceCharset()).thenReturn(Charset.forName("UTF-8"));
    InputFile inputFile =
        InputFileUtils.create(
            new File("src/test/resources/cpd"), new File("src/test/resources/cpd/Person.js"));
    when(fs.mainFiles(JavaScript.KEY)).thenReturn(ImmutableList.of(inputFile));
    Project project = new Project("key");
    project.setFileSystem(fs);
    SensorContext context = mock(SensorContext.class);

    sensor.analyse(project, context);

    verify(context)
        .saveMeasure(Mockito.any(Resource.class), Mockito.eq(CoreMetrics.FILES), Mockito.eq(1.0));
    verify(context)
        .saveMeasure(Mockito.any(Resource.class), Mockito.eq(CoreMetrics.LINES), Mockito.eq(22.0));
    verify(context)
        .saveMeasure(Mockito.any(Resource.class), Mockito.eq(CoreMetrics.NCLOC), Mockito.eq(10.0));
    verify(context)
        .saveMeasure(
            Mockito.any(Resource.class), Mockito.eq(CoreMetrics.FUNCTIONS), Mockito.eq(2.0));
    verify(context)
        .saveMeasure(
            Mockito.any(Resource.class), Mockito.eq(CoreMetrics.STATEMENTS), Mockito.eq(6.0));
    verify(context)
        .saveMeasure(
            Mockito.any(Resource.class), Mockito.eq(CoreMetrics.COMPLEXITY), Mockito.eq(3.0));
    verify(context)
        .saveMeasure(
            Mockito.any(Resource.class), Mockito.eq(CoreMetrics.COMMENT_LINES), Mockito.eq(2.0));
  }
  public void analyse(Project project, SensorContext context) {
    this.project = project;
    this.context = context;

    Collection<SquidAstVisitor<LexerlessGrammar>> squidChecks = annotationCheckFactory.getChecks();
    List<SquidAstVisitor<LexerlessGrammar>> visitors = Lists.newArrayList(squidChecks);
    visitors.add(new FileLinesVisitor(project, fileLinesContextFactory));
    this.scanner =
        FlexAstScanner.create(
            createConfiguration(project), visitors.toArray(new SquidAstVisitor[visitors.size()]));
    Collection<java.io.File> files =
        InputFileUtils.toFiles(project.getFileSystem().mainFiles(Flex.KEY));
    files = ImmutableList.copyOf(Collections2.filter(files, Predicates.not(MXML_FILTER)));
    scanner.scanFiles(files);

    Collection<SourceCode> squidSourceFiles =
        scanner.getIndex().search(new QueryByType(SourceFile.class));
    save(squidSourceFiles);

    Collection<SourceCode> squidPackages =
        scanner.getIndex().search(new QueryByType(FlexSquidPackage.class));
    for (SourceCode pkg : squidPackages) {
      String packageName = pkg.getKey();
      if (!"".equals(packageName)) {
        Directory directory = resourceBridge.findDirectory(packageName);
        context.saveMeasure(directory, CoreMetrics.PACKAGES, 1.0);
      }
    }
  }
 protected void analyse(ProjectFileSystem fileSystem, SensorContext context) {
   parseDirs(
       context,
       InputFileUtils.toFiles(fileSystem.mainFiles(JavaScript.KEY)),
       fileSystem.getSourceDirs(),
       false,
       fileSystem.getSourceCharset());
 }
  @Test
  public void shouldImportSoure() {
    SensorContext context = mock(SensorContext.class);
    JavaFile javaFile = mock(JavaFile.class);
    InputFile inputFile =
        InputFileUtils.create(
            new File("."),
            new File("./src/test/java/org/sonar/plugins/java/JavaSourceImporterTest.java"));

    sensor.importSource(context, javaFile, inputFile, Charset.forName("UTF-8"));
    verify(context, times(1)).saveSource(any(JavaFile.class), anyString());
  }
  /** SONAR-3804 */
  @Test
  public void shouldThrowCleanMessageIfFailToImportSoure() {
    File file = new File("./src/test/java/org/sonar/plugins/java/JavaSourceImporterTest.java");

    SensorContext context = mock(SensorContext.class);
    JavaFile javaFile = mock(JavaFile.class);
    InputFile inputFile = InputFileUtils.create(new File("."), file);
    doThrow(DataException.class).when(context).saveSource(any(JavaFile.class), anyString());

    thrown.expect(SonarException.class);
    thrown.expectMessage(
        "Unable to read and import the source file : '"
            + file.getAbsolutePath()
            + "' with the charset : 'UTF-8'.");

    sensor.importSource(context, javaFile, inputFile, Charset.forName("UTF-8"));
  }