@Test public void should_analyze_file_with_its_own_encoding() throws IOException { Charset fileSystemCharset = StandardCharsets.UTF_8; Charset fileCharset = StandardCharsets.UTF_16; Path moduleBaseDir = temporaryFolder.newFolder().toPath(); SensorContextTester context = SensorContextTester.create(moduleBaseDir); DefaultFileSystem fileSystem = new DefaultFileSystem(moduleBaseDir); fileSystem.setEncoding(fileSystemCharset); context.setFileSystem(fileSystem); String filename = "utf16.xml"; try (BufferedWriter writer = Files.newBufferedWriter(moduleBaseDir.resolve(filename), fileCharset)) { writer.write("<?xml version=\"1.0\" encoding=\"utf-16\" standalone=\"yes\"?>\n"); writer.write("<tag></tag>"); } String modulekey = "modulekey"; DefaultInputFile defaultInputFile = TestInputFileBuilder.create(modulekey, filename) .setModuleBaseDir(moduleBaseDir) .setType(InputFile.Type.MAIN) .setLanguage(Xml.KEY) .setCharset(fileCharset) .build(); fileSystem.add(defaultInputFile); defaultInputFile.setMetadata(new FileMetadata().readMetadata(new FileInputStream(defaultInputFile.file()), StandardCharsets.UTF_8, defaultInputFile.absolutePath())); ActiveRules activeRules = new ActiveRulesBuilder() .create(NEW_LINE_RULE_KEY) .activate() .build(); CheckFactory checkFactory = new CheckFactory(activeRules); FileLinesContextFactory fileLinesContextFactory = mock(FileLinesContextFactory.class); when(fileLinesContextFactory.createFor(any(InputFile.class))).thenReturn(mock(FileLinesContext.class)); sensor = new XmlSensor(fileSystem, checkFactory, fileLinesContextFactory); sensor.execute(context); String componentKey = modulekey + ":" + filename; assertThat(context.measure(componentKey, CoreMetrics.NCLOC).value()).isEqualTo(2); }
@Override public void execute(SensorContext context) { List<InputFile> inputFiles = new ArrayList<>(); fileSystem.inputFiles(mainFilesPredicate).forEach(inputFiles::add); if (inputFiles.isEmpty()) { return; } boolean isSonarLintContext = context.runtime().getProduct() == SonarProduct.SONARLINT; ProgressReport progressReport = new ProgressReport("Report about progress of XML analyzer", TimeUnit.SECONDS.toMillis(10)); progressReport.start(inputFiles.stream().map(InputFile::toString).collect(Collectors.toList())); boolean cancelled = false; try { for (InputFile inputFile : inputFiles) { if (context.isCancelled()) { cancelled = true; break; } scanFile(context, inputFile, isSonarLintContext); progressReport.nextFile(); } } finally { if (!cancelled) { progressReport.stop(); } else { progressReport.cancel(); } } }
XmlSensor implements Sensor { @Override public void execute(SensorContext context) { List<InputFile> inputFiles = new ArrayList<>(); fileSystem.inputFiles(mainFilesPredicate).forEach(inputFiles::add); if (inputFiles.isEmpty()) { return; } boolean isSonarLintContext = context.runtime().getProduct() == SonarProduct.SONARLINT; ProgressReport progressReport = new ProgressReport("Report about progress of XML analyzer", TimeUnit.SECONDS.toMillis(10)); progressReport.start(inputFiles.stream().map(InputFile::toString).collect(Collectors.toList())); boolean cancelled = false; try { for (InputFile inputFile : inputFiles) { if (context.isCancelled()) { cancelled = true; break; } scanFile(context, inputFile, isSonarLintContext); progressReport.nextFile(); } } finally { if (!cancelled) { progressReport.stop(); } else { progressReport.cancel(); } } } }
XmlSensor implements Sensor { @Override public void execute(SensorContext context) { List<InputFile> inputFiles = new ArrayList<>(); fileSystem.inputFiles(mainFilesPredicate).forEach(inputFiles::add); if (inputFiles.isEmpty()) { return; } boolean isSonarLintContext = context.runtime().getProduct() == SonarProduct.SONARLINT; ProgressReport progressReport = new ProgressReport("Report about progress of XML analyzer", TimeUnit.SECONDS.toMillis(10)); progressReport.start(inputFiles.stream().map(InputFile::toString).collect(Collectors.toList())); boolean cancelled = false; try { for (InputFile inputFile : inputFiles) { if (context.isCancelled()) { cancelled = true; break; } scanFile(context, inputFile, isSonarLintContext); progressReport.nextFile(); } } finally { if (!cancelled) { progressReport.stop(); } else { progressReport.cancel(); } } } XmlSensor(FileSystem fileSystem, CheckFactory checkFactory, FileLinesContextFactory fileLinesContextFactory); }
XmlSensor implements Sensor { @Override public void execute(SensorContext context) { List<InputFile> inputFiles = new ArrayList<>(); fileSystem.inputFiles(mainFilesPredicate).forEach(inputFiles::add); if (inputFiles.isEmpty()) { return; } boolean isSonarLintContext = context.runtime().getProduct() == SonarProduct.SONARLINT; ProgressReport progressReport = new ProgressReport("Report about progress of XML analyzer", TimeUnit.SECONDS.toMillis(10)); progressReport.start(inputFiles.stream().map(InputFile::toString).collect(Collectors.toList())); boolean cancelled = false; try { for (InputFile inputFile : inputFiles) { if (context.isCancelled()) { cancelled = true; break; } scanFile(context, inputFile, isSonarLintContext); progressReport.nextFile(); } } finally { if (!cancelled) { progressReport.stop(); } else { progressReport.cancel(); } } } XmlSensor(FileSystem fileSystem, CheckFactory checkFactory, FileLinesContextFactory fileLinesContextFactory); @Override void execute(SensorContext context); @Override String toString(); @Override void describe(SensorDescriptor descriptor); }
XmlSensor implements Sensor { @Override public void execute(SensorContext context) { List<InputFile> inputFiles = new ArrayList<>(); fileSystem.inputFiles(mainFilesPredicate).forEach(inputFiles::add); if (inputFiles.isEmpty()) { return; } boolean isSonarLintContext = context.runtime().getProduct() == SonarProduct.SONARLINT; ProgressReport progressReport = new ProgressReport("Report about progress of XML analyzer", TimeUnit.SECONDS.toMillis(10)); progressReport.start(inputFiles.stream().map(InputFile::toString).collect(Collectors.toList())); boolean cancelled = false; try { for (InputFile inputFile : inputFiles) { if (context.isCancelled()) { cancelled = true; break; } scanFile(context, inputFile, isSonarLintContext); progressReport.nextFile(); } } finally { if (!cancelled) { progressReport.stop(); } else { progressReport.cancel(); } } } XmlSensor(FileSystem fileSystem, CheckFactory checkFactory, FileLinesContextFactory fileLinesContextFactory); @Override void execute(SensorContext context); @Override String toString(); @Override void describe(SensorDescriptor descriptor);  }