@SuppressWarnings("deprecation") @Test public void testPsSQL() throws IOException { SensorContextTester ctxTester = SensorContextTester.create(folder.getRoot()); ctxTester.settings().setProperty(Constants.PLUGIN_SQL_DIALECT, "pssql"); File baseFile = folder.newFile("test.sql"); FileUtils.copyURLToFile(getClass().getResource("/pssql/sample1.sql"), baseFile); String contents = new String(Files.readAllBytes(baseFile.toPath())); DefaultInputFile ti = new TestInputFileBuilder("test", "test.sql").initMetadata(contents).setContents(contents) .setLanguage(Constants.languageKey).build(); ctxTester.fileSystem().add(ti); SQLSensor s = new SQLSensor(); s.execute(ctxTester); Assert.assertEquals(2, ctxTester.allIssues().size()); }
@Override public void execute(final SensorContext context) { final Configuration config = context.config(); final String dialect = config.get(Constants.PLUGIN_SQL_DIALECT).orElse("tsql").toUpperCase(); final long timeout = config.getLong(Constants.PLUGIN_SQL_SCA_TIMEOUT) .orElse(Constants.PLUGIN_SQL_SCA_TIMEOUT_DEFAULT); final long maxFileSize = config.getLong(Constants.PLUGIN_SQL_SCA_MAX_FILE_SIZE) .orElse(Constants.PLUGIN_SQL_SCA_MAX_FILE_SIZE_DEFAULT); if (!EnumUtils.isValidEnum(Dialects.class, dialect)) { LOGGER.warn("Undefined dialect was passed: {}. Supported dialects are: {}", dialect, Dialects.values()); return; } final Dialects sqlDialect = Dialects.valueOf(dialect.toUpperCase()); final ExecutorService service = Executors.newWorkStealingPool(); final org.sonar.api.batch.fs.FileSystem fs = context.fileSystem(); final Iterable<InputFile> files = fs.inputFiles(fs.predicates().hasLanguage(Constants.languageKey)); final List<SqlRules> customRules = adhocRulesProvider.getAdhocRules( config.get(Constants.PLUGIN_SQL_EXTERNAL_RULES_SUFFIX) .orElse(Constants.PLUGIN_SQL_EXTERNAL_RULES_SUFFIXES_DEFAULT), context.fileSystem().baseDir().getAbsolutePath(), config.getStringArray(Constants.PLUGIN_SQL_EXTERNAL_RULES_SEARCH_PATH)); LOGGER.debug("Found {} number of custom rules", customRules.size()); files.forEach(inputFile -> { service.execute(new Runnable() { @SuppressWarnings("deprecation") @Override public void run() { try { if (inputFile.file().length() > maxFileSize) { LOGGER.debug("Skipping {} file as its size exceeds {} bytes. You can increase this limit by setting {} property", inputFile, maxFileSize, Constants.PLUGIN_SQL_SCA_MAX_FILE_SIZE); return; } final AntlrContext ctx = sqlDialect.parse(inputFile.contents(), customRules); for (final Filler filler : fillers) { filler.fill(inputFile, context, ctx); } } catch (Throwable e) { LOGGER.warn("Unexpected exception while analyzing file: " + inputFile, 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); } }
SQLSensor implements Sensor { @Override public void execute(final SensorContext context) { final Configuration config = context.config(); final String dialect = config.get(Constants.PLUGIN_SQL_DIALECT).orElse("tsql").toUpperCase(); final long timeout = config.getLong(Constants.PLUGIN_SQL_SCA_TIMEOUT) .orElse(Constants.PLUGIN_SQL_SCA_TIMEOUT_DEFAULT); final long maxFileSize = config.getLong(Constants.PLUGIN_SQL_SCA_MAX_FILE_SIZE) .orElse(Constants.PLUGIN_SQL_SCA_MAX_FILE_SIZE_DEFAULT); if (!EnumUtils.isValidEnum(Dialects.class, dialect)) { LOGGER.warn("Undefined dialect was passed: {}. Supported dialects are: {}", dialect, Dialects.values()); return; } final Dialects sqlDialect = Dialects.valueOf(dialect.toUpperCase()); final ExecutorService service = Executors.newWorkStealingPool(); final org.sonar.api.batch.fs.FileSystem fs = context.fileSystem(); final Iterable<InputFile> files = fs.inputFiles(fs.predicates().hasLanguage(Constants.languageKey)); final List<SqlRules> customRules = adhocRulesProvider.getAdhocRules( config.get(Constants.PLUGIN_SQL_EXTERNAL_RULES_SUFFIX) .orElse(Constants.PLUGIN_SQL_EXTERNAL_RULES_SUFFIXES_DEFAULT), context.fileSystem().baseDir().getAbsolutePath(), config.getStringArray(Constants.PLUGIN_SQL_EXTERNAL_RULES_SEARCH_PATH)); LOGGER.debug("Found {} number of custom rules", customRules.size()); files.forEach(inputFile -> { service.execute(new Runnable() { @SuppressWarnings("deprecation") @Override public void run() { try { if (inputFile.file().length() > maxFileSize) { LOGGER.debug("Skipping {} file as its size exceeds {} bytes. You can increase this limit by setting {} property", inputFile, maxFileSize, Constants.PLUGIN_SQL_SCA_MAX_FILE_SIZE); return; } final AntlrContext ctx = sqlDialect.parse(inputFile.contents(), customRules); for (final Filler filler : fillers) { filler.fill(inputFile, context, ctx); } } catch (Throwable e) { LOGGER.warn("Unexpected exception while analyzing file: " + inputFile, 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); } } }
SQLSensor implements Sensor { @Override public void execute(final SensorContext context) { final Configuration config = context.config(); final String dialect = config.get(Constants.PLUGIN_SQL_DIALECT).orElse("tsql").toUpperCase(); final long timeout = config.getLong(Constants.PLUGIN_SQL_SCA_TIMEOUT) .orElse(Constants.PLUGIN_SQL_SCA_TIMEOUT_DEFAULT); final long maxFileSize = config.getLong(Constants.PLUGIN_SQL_SCA_MAX_FILE_SIZE) .orElse(Constants.PLUGIN_SQL_SCA_MAX_FILE_SIZE_DEFAULT); if (!EnumUtils.isValidEnum(Dialects.class, dialect)) { LOGGER.warn("Undefined dialect was passed: {}. Supported dialects are: {}", dialect, Dialects.values()); return; } final Dialects sqlDialect = Dialects.valueOf(dialect.toUpperCase()); final ExecutorService service = Executors.newWorkStealingPool(); final org.sonar.api.batch.fs.FileSystem fs = context.fileSystem(); final Iterable<InputFile> files = fs.inputFiles(fs.predicates().hasLanguage(Constants.languageKey)); final List<SqlRules> customRules = adhocRulesProvider.getAdhocRules( config.get(Constants.PLUGIN_SQL_EXTERNAL_RULES_SUFFIX) .orElse(Constants.PLUGIN_SQL_EXTERNAL_RULES_SUFFIXES_DEFAULT), context.fileSystem().baseDir().getAbsolutePath(), config.getStringArray(Constants.PLUGIN_SQL_EXTERNAL_RULES_SEARCH_PATH)); LOGGER.debug("Found {} number of custom rules", customRules.size()); files.forEach(inputFile -> { service.execute(new Runnable() { @SuppressWarnings("deprecation") @Override public void run() { try { if (inputFile.file().length() > maxFileSize) { LOGGER.debug("Skipping {} file as its size exceeds {} bytes. You can increase this limit by setting {} property", inputFile, maxFileSize, Constants.PLUGIN_SQL_SCA_MAX_FILE_SIZE); return; } final AntlrContext ctx = sqlDialect.parse(inputFile.contents(), customRules); for (final Filler filler : fillers) { filler.fill(inputFile, context, ctx); } } catch (Throwable e) { LOGGER.warn("Unexpected exception while analyzing file: " + inputFile, 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); } }  }
SQLSensor implements Sensor { @Override public void execute(final SensorContext context) { final Configuration config = context.config(); final String dialect = config.get(Constants.PLUGIN_SQL_DIALECT).orElse("tsql").toUpperCase(); final long timeout = config.getLong(Constants.PLUGIN_SQL_SCA_TIMEOUT) .orElse(Constants.PLUGIN_SQL_SCA_TIMEOUT_DEFAULT); final long maxFileSize = config.getLong(Constants.PLUGIN_SQL_SCA_MAX_FILE_SIZE) .orElse(Constants.PLUGIN_SQL_SCA_MAX_FILE_SIZE_DEFAULT); if (!EnumUtils.isValidEnum(Dialects.class, dialect)) { LOGGER.warn("Undefined dialect was passed: {}. Supported dialects are: {}", dialect, Dialects.values()); return; } final Dialects sqlDialect = Dialects.valueOf(dialect.toUpperCase()); final ExecutorService service = Executors.newWorkStealingPool(); final org.sonar.api.batch.fs.FileSystem fs = context.fileSystem(); final Iterable<InputFile> files = fs.inputFiles(fs.predicates().hasLanguage(Constants.languageKey)); final List<SqlRules> customRules = adhocRulesProvider.getAdhocRules( config.get(Constants.PLUGIN_SQL_EXTERNAL_RULES_SUFFIX) .orElse(Constants.PLUGIN_SQL_EXTERNAL_RULES_SUFFIXES_DEFAULT), context.fileSystem().baseDir().getAbsolutePath(), config.getStringArray(Constants.PLUGIN_SQL_EXTERNAL_RULES_SEARCH_PATH)); LOGGER.debug("Found {} number of custom rules", customRules.size()); files.forEach(inputFile -> { service.execute(new Runnable() { @SuppressWarnings("deprecation") @Override public void run() { try { if (inputFile.file().length() > maxFileSize) { LOGGER.debug("Skipping {} file as its size exceeds {} bytes. You can increase this limit by setting {} property", inputFile, maxFileSize, Constants.PLUGIN_SQL_SCA_MAX_FILE_SIZE); return; } final AntlrContext ctx = sqlDialect.parse(inputFile.contents(), customRules); for (final Filler filler : fillers) { filler.fill(inputFile, context, ctx); } } catch (Throwable e) { LOGGER.warn("Unexpected exception while analyzing file: " + inputFile, 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); } }  @Override void describe(final SensorDescriptor descriptor); @Override void execute(final SensorContext context); }
SQLSensor implements Sensor { @Override public void execute(final SensorContext context) { final Configuration config = context.config(); final String dialect = config.get(Constants.PLUGIN_SQL_DIALECT).orElse("tsql").toUpperCase(); final long timeout = config.getLong(Constants.PLUGIN_SQL_SCA_TIMEOUT) .orElse(Constants.PLUGIN_SQL_SCA_TIMEOUT_DEFAULT); final long maxFileSize = config.getLong(Constants.PLUGIN_SQL_SCA_MAX_FILE_SIZE) .orElse(Constants.PLUGIN_SQL_SCA_MAX_FILE_SIZE_DEFAULT); if (!EnumUtils.isValidEnum(Dialects.class, dialect)) { LOGGER.warn("Undefined dialect was passed: {}. Supported dialects are: {}", dialect, Dialects.values()); return; } final Dialects sqlDialect = Dialects.valueOf(dialect.toUpperCase()); final ExecutorService service = Executors.newWorkStealingPool(); final org.sonar.api.batch.fs.FileSystem fs = context.fileSystem(); final Iterable<InputFile> files = fs.inputFiles(fs.predicates().hasLanguage(Constants.languageKey)); final List<SqlRules> customRules = adhocRulesProvider.getAdhocRules( config.get(Constants.PLUGIN_SQL_EXTERNAL_RULES_SUFFIX) .orElse(Constants.PLUGIN_SQL_EXTERNAL_RULES_SUFFIXES_DEFAULT), context.fileSystem().baseDir().getAbsolutePath(), config.getStringArray(Constants.PLUGIN_SQL_EXTERNAL_RULES_SEARCH_PATH)); LOGGER.debug("Found {} number of custom rules", customRules.size()); files.forEach(inputFile -> { service.execute(new Runnable() { @SuppressWarnings("deprecation") @Override public void run() { try { if (inputFile.file().length() > maxFileSize) { LOGGER.debug("Skipping {} file as its size exceeds {} bytes. You can increase this limit by setting {} property", inputFile, maxFileSize, Constants.PLUGIN_SQL_SCA_MAX_FILE_SIZE); return; } final AntlrContext ctx = sqlDialect.parse(inputFile.contents(), customRules); for (final Filler filler : fillers) { filler.fill(inputFile, context, ctx); } } catch (Throwable e) { LOGGER.warn("Unexpected exception while analyzing file: " + inputFile, 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); } }  @Override void describe(final SensorDescriptor descriptor); @Override void execute(final SensorContext context);  }