Search in sources :

Example 16 with LocationInFile

use of org.sonar.ucfg.LocationInFile in project sonar-java by SonarSource.

the class UCFGJavaVisitorTest method unknown_method.

@Test
public void unknown_method() {
    Expression.Variable arg = UCFGBuilder.variableWithId("arg");
    UCFG expectedUCFG = UCFGBuilder.createUCFGForMethod("A#method(Ljava/util/Set;)V").addMethodParam(arg).addBasicBlock(newBasicBlock("0").ret(constant("implicit return"), new LocationInFile(FILE_KEY, 9, 2, 9, 3))).build();
    String source = "import java.util.Set;\n" + "import java.util.Collection;\n" + "import java.util.stream.Collectors;\n" + "public class A {\n" + "  void method(Set<String> arg) { \n" + "    arg.stream()\n" + "      .flatMap(Collection::stream)\n" + "      .collect(Collectors.toCollection(LinkedHashSet::new)); \n" + "  }\n" + "}";
    // Semantic model creates 2 kinds of "unknown" symbols: "Symbols.unknownSymbol" and "JavaSymbolNotFound"
    // We need to test first case
    // To make sure that code contains "Symbols.unknownSymbol" this assertion is there
    // Note that if Semantic model is improved somehow that "Symbols.unknownSymbol" is not anymore generated in this case
    // this test might be removed
    assertUnknownMethodCalled(source);
    assertCodeToUCfg(source, expectedUCFG);
}
Also used : Expression(org.sonar.ucfg.Expression) UCFG(org.sonar.ucfg.UCFG) LocationInFile(org.sonar.ucfg.LocationInFile) Test(org.junit.Test)

Example 17 with LocationInFile

use of org.sonar.ucfg.LocationInFile in project sonar-java by SonarSource.

the class UCFGJavaVisitorTest method basic_block_location.

@Test
public void basic_block_location() {
    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, 3, 5, 3, 16))).build();
    UCFG ucfg = assertCodeToUCfg("class A {\n  String method(String arg) {\n     return arg;\n}}", expectedUCFG);
    assertThat(ucfg.entryBlocks()).hasSize(1);
    assertThat(ucfg.entryBlocks().iterator().next().locationInFile()).isEqualTo(new LocationInFile(FILE_KEY, 3, 12, 3, 15));
}
Also used : Expression(org.sonar.ucfg.Expression) UCFG(org.sonar.ucfg.UCFG) LocationInFile(org.sonar.ucfg.LocationInFile) Test(org.junit.Test)

Example 18 with LocationInFile

use of org.sonar.ucfg.LocationInFile in project sonar-java by SonarSource.

the class UCFGJavaVisitorTest method build_assignment_for_string.

@Test
public void build_assignment_for_string() {
    Expression.Variable arg = UCFGBuilder.variableWithId("arg");
    Expression.Variable arg2 = UCFGBuilder.variableWithId("arg2");
    Expression.Variable var1 = UCFGBuilder.variableWithId("var1");
    UCFG expectedUCFG = UCFGBuilder.createUCFGForMethod("A#method(Ljava/lang/String;Ljava/lang/String;)Ljava/lang/String;").addMethodParam(arg).addBasicBlock(newBasicBlock("1").assignTo(var1, call("__id").withArgs(arg), new LocationInFile(FILE_KEY, 1, 49, 1, 67)).assignTo(var1, call("__id").withArgs(arg2), new LocationInFile(FILE_KEY, 1, 88, 1, 99)).ret(var1, new LocationInFile(FILE_KEY, 1, 114, 1, 126))).build();
    assertCodeToUCfg("class A {String method(String arg, String arg2) {String var1 = arg; int var2; int var3; var1 = arg2; var2 = var3; return var1;}}", expectedUCFG);
}
Also used : Expression(org.sonar.ucfg.Expression) UCFG(org.sonar.ucfg.UCFG) LocationInFile(org.sonar.ucfg.LocationInFile) Test(org.junit.Test)

Aggregations

LocationInFile (org.sonar.ucfg.LocationInFile)18 UCFG (org.sonar.ucfg.UCFG)17 Test (org.junit.Test)16 Expression (org.sonar.ucfg.Expression)16 File (java.io.File)1 IOException (java.io.IOException)1 CompilationUnitTree (org.sonar.plugins.java.api.tree.CompilationUnitTree)1 BasicBlock (org.sonar.ucfg.BasicBlock)1 Label (org.sonar.ucfg.Label)1 UCFGBuilder (org.sonar.ucfg.UCFGBuilder)1 BlockBuilder (org.sonar.ucfg.UCFGBuilder.BlockBuilder)1 UCFGBuilder.newBasicBlock (org.sonar.ucfg.UCFGBuilder.newBasicBlock)1