package com.ubisec.test.plsql;
import com.ubisec.base.FileUtils;
import com.ubisec.base.NoOpFileLinesContextFactory;
import org.junit.Before;
import org.junit.Test;import org.sonar.api.batch.fs.internal.DefaultInputFile;
import org.sonar.api.batch.fs.internal.TestInputFileBuilder;
import org.sonar.api.batch.rule.ActiveRules;
import org.sonar.api.batch.rule.internal.ActiveRulesBuilder;
import org.sonar.api.batch.rule.internal.NewActiveRule;
import org.sonar.api.batch.sensor.internal.SensorContextTester;
import org.sonar.api.batch.sensor.issue.Issue;
import org.sonar.api.config.internal.MapSettings;
import org.sonar.api.issue.NoSonarFilter;
import org.sonar.api.rule.RuleKey;
import org.sonar.plsqlopen.PlSql;
import org.sonar.plsqlopen.PlSqlSquidSensor;
import org.sonar.plsqlopen.checks.UnusedVariableCheck;
import java.io.File;
import java.nio.charset.StandardCharsets;
import java.nio.file.Paths;
import java.util.Collection;
public class PlsqlCheckUnuseVarTest {
private PlSqlSquidSensor sensor;
private SensorContextTester context;
@Before
public void setup(){
ActiveRulesBuilder activeRulesBuilder = new ActiveRulesBuilder();
activeRulesBuilder.addRule(new NewActiveRule.Builder()
.setRuleKey(RuleKey.of("plsql", "EmptyBlock"))
.setName("Print Statement Usage")
.build());
activeRulesBuilder.addRule(new NewActiveRule.Builder() .setRuleKey(RuleKey.of("plsql",this.annotatedEngineKey(UnusedVariableCheck.class)))
.build());
ActiveRules activeRules = activeRulesBuilder.build();
context = SensorContextTester.create(new File("."));
sensor = new PlSqlSquidSensor(activeRules, new MapSettings().asConfig(), new NoSonarFilter(), new NoOpFileLinesContextFactory(), null);
}
@Test
public void check(){
context.fileSystem().add(this.getDefaultInputFile("C:\\hl-projects\\github\\sonar_go_scala_ruby_kotlin\\sonar_go_scala_ruby_kotlin\\src\\test\\resources\\plsql\\checks\\code.sql"));
context.fileSystem().add(this.getDefaultInputFile("C:\\hl-projects\\github\\sonar_go_scala_ruby_kotlin\\sonar_go_scala_ruby_kotlin\\src\\test\\resources\\plsql\\checks\\unused_variable.sql"));
sensor.execute(context);
Collection<Issue> issues = context.allIssues();
System.out.println();
}
public DefaultInputFile getDefaultInputFile(String relativePath){
String content = FileUtils.fileContent(new File(relativePath), StandardCharsets.UTF_8);
DefaultInputFile inputFile = new TestInputFileBuilder("key", relativePath)
.setLanguage(PlSql.KEY)
.setCharset(StandardCharsets.UTF_8)
.initMetadata(content)
.setModuleBaseDir(Paths.get(""))
.build();
return inputFile;
}
public String annotatedEngineKey(Class cls) {
org.sonar.plugins.plsqlopen.api.annotations.Rule rule =(org.sonar.plugins.plsqlopen.api.annotations.Rule)cls.getAnnotation(org.sonar.plugins.plsqlopen.api.annotations.Rule.class);
return rule.key();
}
}