use of org.sonar.ucfg.LocationInFile in project sonar-java by SonarSource.
the class UCFGJavaVisitorTest method assertCodeToUCfg.
private UCFG assertCodeToUCfg(String source, UCFG expectedUCFG, boolean testLocations) {
CompilationUnitTree cut = getCompilationUnitTreeWithSemantics(source);
UCFGJavaVisitor UCFGJavaVisitor = new UCFGJavaVisitor(tmp.getRoot());
UCFGJavaVisitor.fileKey = FILE_KEY;
UCFGJavaVisitor.visitCompilationUnit(cut);
UCFG actualUCFG = null;
try {
File java_ucfg_dir = new File(new File(tmp.getRoot(), "ucfg"), "java");
File ucfg = new File(java_ucfg_dir, "ucfg_0.proto");
actualUCFG = UCFGtoProtobuf.fromProtobufFile(ucfg);
} catch (IOException e) {
e.printStackTrace();
}
assertThat(actualUCFG.methodId()).isEqualTo(expectedUCFG.methodId());
assertThat(actualUCFG.basicBlocks()).isEqualTo(expectedUCFG.basicBlocks());
assertThat(actualUCFG.basicBlocks().values().stream().flatMap(b -> b.calls().stream())).containsExactlyElementsOf(expectedUCFG.basicBlocks().values().stream().flatMap(b -> b.calls().stream()).collect(Collectors.toList()));
assertThat(actualUCFG.basicBlocks().values().stream().map(BasicBlock::terminator)).containsExactlyElementsOf(expectedUCFG.basicBlocks().values().stream().map(BasicBlock::terminator).collect(Collectors.toList()));
assertThat(actualUCFG.entryBlocks()).isEqualTo(expectedUCFG.entryBlocks());
assertThat(toLocationStream(actualUCFG).noneMatch(l -> l == UCFGBuilder.LOC)).isTrue();
if (testLocations) {
Stream<LocationInFile> locStream = toLocationStream(actualUCFG);
assertThat(locStream).containsExactlyElementsOf(toLocationStream(expectedUCFG).collect(Collectors.toList()));
}
return actualUCFG;
}
use of org.sonar.ucfg.LocationInFile in project sonar-java by SonarSource.
the class UCFGJavaVisitorTest method visit_java_file.
@Test
public void visit_java_file() {
Expression.Variable arg = UCFGBuilder.variableWithId("arg");
UCFG expectedUCFG = UCFGBuilder.createUCFGForMethod("A#method(Ljava/lang/String;)Ljava/lang/String;").addMethodParam(arg).addBasicBlock(newBasicBlock("1").ret(arg, new LocationInFile(FILE_KEY, 1, 37, 1, 48))).build();
assertCodeToUCfg("class A { String method(String arg) {return arg;}}", expectedUCFG);
}
use of org.sonar.ucfg.LocationInFile in project sonar-java by SonarSource.
the class UCFGJavaVisitorTest method build_two_parameters_annotations.
@Test
public void build_two_parameters_annotations() {
Expression.Variable arg0 = UCFGBuilder.variableWithId("arg0");
Expression.Variable arg1 = UCFGBuilder.variableWithId("arg1");
Expression.Variable aux0 = UCFGBuilder.variableWithId("%0");
Expression.Variable aux1 = UCFGBuilder.variableWithId("%1");
UCFG expectedUCFG = UCFGBuilder.createUCFGForMethod("A#method(Ljava/lang/String;Ljava/lang/String;)Ljava/lang/String;").addMethodParam(arg0).addMethodParam(arg1).addBasicBlock(newBasicBlock("paramAnnotations").assignTo(aux0, call("javax.annotation.Nullable").withArgs(arg0), new LocationInFile(FILE_KEY, 1, 24, 1, 50)).assignTo(arg0, call("__annotation").withArgs(aux0), new LocationInFile(FILE_KEY, 1, 58, 1, 62)).assignTo(aux1, call("javax.annotation.Nullable").withArgs(arg1), new LocationInFile(FILE_KEY, 1, 64, 1, 90)).assignTo(arg1, call("__annotation").withArgs(aux1), new LocationInFile(FILE_KEY, 1, 98, 1, 102)).jumpTo(UCFGBuilder.createLabel("1"))).addBasicBlock(newBasicBlock("1").ret(constant("foo"), new LocationInFile(FILE_KEY, 1, 106, 1, 119))).build();
assertCodeToUCfg("class A { String method(@javax.annotation.Nullable String arg0, @javax.annotation.Nullable String arg1) { return \"foo\";}}", expectedUCFG);
}
use of org.sonar.ucfg.LocationInFile in project sonar-java by SonarSource.
the class UCFGJavaVisitorTest method location_in_source_file_should_be_preserved.
@Test
public void location_in_source_file_should_be_preserved() {
Expression.Variable arg = UCFGBuilder.variableWithId("arg");
Expression.Variable var = UCFGBuilder.variableWithId("%0");
UCFG expectedUCFG = UCFGBuilder.createUCFGForMethod("A#method(Ljava/lang/String;)Ljava/lang/String;").addMethodParam(arg).addBasicBlock(newBasicBlock("1").assignTo(var, call("java.lang.String#toString()Ljava/lang/String;").withArgs(arg), new LocationInFile(FILE_KEY, 2, 0, 2, 14)).ret(arg, new LocationInFile(FILE_KEY, 3, 2, 3, 13))).build();
assertCodeToUCfgAndLocations("class A { String method(String arg) {\narg.toString();\n return arg;\n}}", expectedUCFG);
}
use of org.sonar.ucfg.LocationInFile in project sonar-java by SonarSource.
the class UCFGJavaVisitorTest method static_method_call_without_object.
@Test
public void static_method_call_without_object() {
Expression.Variable arg = UCFGBuilder.variableWithId("arg");
Expression.Variable var0 = UCFGBuilder.variableWithId("%0");
UCFG expectedUCFG = UCFGBuilder.createUCFGForMethod("A#method(Ljava/lang/Integer;)I").addMethodParam(arg).addStartingBlock(newBasicBlock("1").assignTo(var0, call("java.lang.String#valueOf(Ljava/lang/Object;)Ljava/lang/String;").withArgs(arg), new LocationInFile(FILE_KEY, 4, 11, 4, 23)).ret(constant("\"\""), new LocationInFile(FILE_KEY, 4, 4, 4, 24))).build();
assertCodeToUCfg("import static java.lang.String.valueOf; \n" + "class A { \n" + " int method(Integer arg) {\n" + " return valueOf(arg);\n" + " }\n" + "}", expectedUCFG);
}
Aggregations