@Test public void testExecute() throws IOException { Assume.assumeTrue("Tool exists", new File(Constants.SQL_SQLCHECK_PATH_DEFAULT).exists()); SensorContextTester ctxTester = SensorContextTester.create(folder.getRoot()); ctxTester.fileSystem().setWorkDir(folder.getRoot().toPath()); File baseFile = folder.newFile("sample2.sql"); FileUtils.copyURLToFile(getClass().getResource("/tsql/sample1.sql"), baseFile); String contents = new String(Files.readAllBytes(baseFile.toPath())); DefaultInputFile ti = new TestInputFileBuilder("test", folder.getRoot(), baseFile).initMetadata(contents) .setLanguage(Constants.languageKey).setContents(contents).setProjectBaseDir(folder.getRoot().toPath()) .build(); ctxTester.fileSystem().add(ti); SqlCheckSensor s = new SqlCheckSensor(temp); s.execute(ctxTester); Assert.assertEquals(3, ctxTester.allExternalIssues().size()); Assert.assertEquals(0, ctxTester.allIssues().size()); Assert.assertEquals(0, ctxTester.allAdHocRules().size()); }
@Override public void execute(SensorContext context) { final String externalTool = context.config().get(Constants.SQL_SQLCHECK_PATH) .orElse(Constants.SQL_SQLCHECK_PATH_DEFAULT).toLowerCase(); try { if (!Files.exists(Paths.get(externalTool))) { LOGGER.info("Skip running external tool as executable not found {}", externalTool); return; } final long timeout = context.config().getLong(Constants.PLUGIN_SQL_SCA_TIMEOUT) .orElse(Constants.PLUGIN_SQL_SCA_TIMEOUT_DEFAULT); final ExecutorService service = Executors.newWorkStealingPool(); for (InputFile file : context.fileSystem() .inputFiles(context.fileSystem().predicates().hasLanguage(Constants.languageKey))) { service.submit(new Runnable() { @Override public void run() { try { execute(context, externalTool, file); } catch (Throwable e) { LOGGER.warn("Unexpected exception while analyzing file: " + file, e); } } }); } service.shutdown(); try { service.awaitTermination(timeout, TimeUnit.SECONDS); service.shutdownNow(); } catch (Throwable e) { LOGGER.warn("Unexpected exception while waiting for executor service to finish.", e); } } catch (Throwable e) { LOGGER.warn("Unexpected error", e); } }
SqlCheckSensor extends BaseSensor implements Sensor { @Override public void execute(SensorContext context) { final String externalTool = context.config().get(Constants.SQL_SQLCHECK_PATH) .orElse(Constants.SQL_SQLCHECK_PATH_DEFAULT).toLowerCase(); try { if (!Files.exists(Paths.get(externalTool))) { LOGGER.info("Skip running external tool as executable not found {}", externalTool); return; } final long timeout = context.config().getLong(Constants.PLUGIN_SQL_SCA_TIMEOUT) .orElse(Constants.PLUGIN_SQL_SCA_TIMEOUT_DEFAULT); final ExecutorService service = Executors.newWorkStealingPool(); for (InputFile file : context.fileSystem() .inputFiles(context.fileSystem().predicates().hasLanguage(Constants.languageKey))) { service.submit(new Runnable() { @Override public void run() { try { execute(context, externalTool, file); } catch (Throwable e) { LOGGER.warn("Unexpected exception while analyzing file: " + file, e); } } }); } service.shutdown(); try { service.awaitTermination(timeout, TimeUnit.SECONDS); service.shutdownNow(); } catch (Throwable e) { LOGGER.warn("Unexpected exception while waiting for executor service to finish.", e); } } catch (Throwable e) { LOGGER.warn("Unexpected error", e); } } }
SqlCheckSensor extends BaseSensor implements Sensor { @Override public void execute(SensorContext context) { final String externalTool = context.config().get(Constants.SQL_SQLCHECK_PATH) .orElse(Constants.SQL_SQLCHECK_PATH_DEFAULT).toLowerCase(); try { if (!Files.exists(Paths.get(externalTool))) { LOGGER.info("Skip running external tool as executable not found {}", externalTool); return; } final long timeout = context.config().getLong(Constants.PLUGIN_SQL_SCA_TIMEOUT) .orElse(Constants.PLUGIN_SQL_SCA_TIMEOUT_DEFAULT); final ExecutorService service = Executors.newWorkStealingPool(); for (InputFile file : context.fileSystem() .inputFiles(context.fileSystem().predicates().hasLanguage(Constants.languageKey))) { service.submit(new Runnable() { @Override public void run() { try { execute(context, externalTool, file); } catch (Throwable e) { LOGGER.warn("Unexpected exception while analyzing file: " + file, e); } } }); } service.shutdown(); try { service.awaitTermination(timeout, TimeUnit.SECONDS); service.shutdownNow(); } catch (Throwable e) { LOGGER.warn("Unexpected exception while waiting for executor service to finish.", e); } } catch (Throwable e) { LOGGER.warn("Unexpected error", e); } } SqlCheckSensor(final TempFolder tempFolder); }
SqlCheckSensor extends BaseSensor implements Sensor { @Override public void execute(SensorContext context) { final String externalTool = context.config().get(Constants.SQL_SQLCHECK_PATH) .orElse(Constants.SQL_SQLCHECK_PATH_DEFAULT).toLowerCase(); try { if (!Files.exists(Paths.get(externalTool))) { LOGGER.info("Skip running external tool as executable not found {}", externalTool); return; } final long timeout = context.config().getLong(Constants.PLUGIN_SQL_SCA_TIMEOUT) .orElse(Constants.PLUGIN_SQL_SCA_TIMEOUT_DEFAULT); final ExecutorService service = Executors.newWorkStealingPool(); for (InputFile file : context.fileSystem() .inputFiles(context.fileSystem().predicates().hasLanguage(Constants.languageKey))) { service.submit(new Runnable() { @Override public void run() { try { execute(context, externalTool, file); } catch (Throwable e) { LOGGER.warn("Unexpected exception while analyzing file: " + file, e); } } }); } service.shutdown(); try { service.awaitTermination(timeout, TimeUnit.SECONDS); service.shutdownNow(); } catch (Throwable e) { LOGGER.warn("Unexpected exception while waiting for executor service to finish.", e); } } catch (Throwable e) { LOGGER.warn("Unexpected error", e); } } SqlCheckSensor(final TempFolder tempFolder); @Override void describe(SensorDescriptor descriptor); @Override void execute(SensorContext context); }
SqlCheckSensor extends BaseSensor implements Sensor { @Override public void execute(SensorContext context) { final String externalTool = context.config().get(Constants.SQL_SQLCHECK_PATH) .orElse(Constants.SQL_SQLCHECK_PATH_DEFAULT).toLowerCase(); try { if (!Files.exists(Paths.get(externalTool))) { LOGGER.info("Skip running external tool as executable not found {}", externalTool); return; } final long timeout = context.config().getLong(Constants.PLUGIN_SQL_SCA_TIMEOUT) .orElse(Constants.PLUGIN_SQL_SCA_TIMEOUT_DEFAULT); final ExecutorService service = Executors.newWorkStealingPool(); for (InputFile file : context.fileSystem() .inputFiles(context.fileSystem().predicates().hasLanguage(Constants.languageKey))) { service.submit(new Runnable() { @Override public void run() { try { execute(context, externalTool, file); } catch (Throwable e) { LOGGER.warn("Unexpected exception while analyzing file: " + file, e); } } }); } service.shutdown(); try { service.awaitTermination(timeout, TimeUnit.SECONDS); service.shutdownNow(); } catch (Throwable e) { LOGGER.warn("Unexpected exception while waiting for executor service to finish.", e); } } catch (Throwable e) { LOGGER.warn("Unexpected error", e); } } SqlCheckSensor(final TempFolder tempFolder); @Override void describe(SensorDescriptor descriptor); @Override void execute(SensorContext context);  }