Search in sources :

Example 1 with ExceptionalYield

use of org.sonar.java.se.xproc.ExceptionalYield in project sonar-java by SonarSource.

the class BehaviorCacheTest method clear_stack_when_taking_exceptional_path_from_method_invocation.

@Test
public void clear_stack_when_taking_exceptional_path_from_method_invocation() throws Exception {
    Pair<SymbolicExecutionVisitor, SemanticModel> sevAndSemantic = createSymbolicExecutionVisitorAndSemantic("src/test/files/se/CleanStackWhenRaisingException.java");
    SymbolicExecutionVisitor sev = sevAndSemantic.a;
    SemanticModel semanticModel = sevAndSemantic.b;
    MethodBehavior behavior = getMethodBehavior(sev, "foo");
    assertThat(behavior.yields()).hasSize(4);
    behavior.happyPathYields().forEach(y -> assertThat(y.resultConstraint()).isNull());
    assertThat(behavior.happyPathYields().count()).isEqualTo(1);
    List<ExceptionalYield> exceptionalYields = behavior.exceptionalPathYields().collect(Collectors.toList());
    assertThat(exceptionalYields).hasSize(3);
    assertThat(exceptionalYields.stream().filter(y -> y.exceptionType(semanticModel).isUnknown())).hasSize(1);
}
Also used : ExceptionalYield(org.sonar.java.se.xproc.ExceptionalYield) SemanticModel(org.sonar.java.resolve.SemanticModel) SETestUtils.getMethodBehavior(org.sonar.java.se.SETestUtils.getMethodBehavior) MethodBehavior(org.sonar.java.se.xproc.MethodBehavior) SETestUtils.createSymbolicExecutionVisitor(org.sonar.java.se.SETestUtils.createSymbolicExecutionVisitor) Test(org.junit.Test)

Example 2 with ExceptionalYield

use of org.sonar.java.se.xproc.ExceptionalYield in project sonar-java by SonarSource.

the class BytecodeEGWalkerTest method test_enqueueing_of_catch_blocks.

@Test
public void test_enqueueing_of_catch_blocks() {
    MethodBehavior mb = getMethodBehavior(ExceptionEnqueue.class, "testCatchBlockEnqueue(Lorg/sonar/java/bytecode/se/testdata/ExceptionEnqueue;)Z");
    List<HappyPathYield> happyPathYields = mb.happyPathYields().collect(Collectors.toList());
    assertThat(happyPathYields).hasSize(1);
    assertThat(happyPathYields.get(0).resultConstraint()).isNull();
    List<ExceptionalYield> exceptionalYields = mb.exceptionalPathYields().collect(Collectors.toList());
    assertThat(exceptionalYields).hasSize(1);
    assertThat(exceptionalYields.get(0).exceptionType(semanticModel).is("java.lang.RuntimeException")).isTrue();
}
Also used : HappyPathYield(org.sonar.java.se.xproc.HappyPathYield) ExceptionalYield(org.sonar.java.se.xproc.ExceptionalYield) MethodBehavior(org.sonar.java.se.xproc.MethodBehavior) Test(org.junit.Test)

Example 3 with ExceptionalYield

use of org.sonar.java.se.xproc.ExceptionalYield in project sonar-java by SonarSource.

the class BytecodeEGWalkerTest method test_enqueueing_of_exit_block3.

@Test
public void test_enqueueing_of_exit_block3() {
    MethodBehavior mb = getMethodBehavior(ExceptionEnqueue.class, "enqueueExitBlock3()Z");
    assertThat(mb.happyPathYields().findFirst().isPresent()).isFalse();
    List<ExceptionalYield> exceptionalYields = mb.exceptionalPathYields().collect(Collectors.toList());
    assertThat(exceptionalYields).hasSize(1);
    assertThat(exceptionalYields.get(0).exceptionType(semanticModel).is("java.io.FileNotFoundException")).isTrue();
}
Also used : ExceptionalYield(org.sonar.java.se.xproc.ExceptionalYield) MethodBehavior(org.sonar.java.se.xproc.MethodBehavior) Test(org.junit.Test)

Example 4 with ExceptionalYield

use of org.sonar.java.se.xproc.ExceptionalYield in project sonar-java by SonarSource.

the class BytecodeEGWalkerTest method test_enqueueing_of_exit_block.

@Test
public void test_enqueueing_of_exit_block() {
    MethodBehavior mb = getMethodBehavior(ExceptionEnqueue.class, "enqueueExitBlock()Z");
    List<MethodYield> yields = mb.yields();
    assertThat(yields).hasSize(1);
    assertThat(mb.happyPathYields().findFirst().isPresent()).isFalse();
    ExceptionalYield exceptionalYield = mb.exceptionalPathYields().findFirst().get();
    Type exceptionType = exceptionalYield.exceptionType(semanticModel);
    assertThat(exceptionType.is("java.io.FileNotFoundException")).isTrue();
}
Also used : Type(org.sonar.plugins.java.api.semantic.Type) MethodYield(org.sonar.java.se.xproc.MethodYield) ExceptionalYield(org.sonar.java.se.xproc.ExceptionalYield) MethodBehavior(org.sonar.java.se.xproc.MethodBehavior) Test(org.junit.Test)

Example 5 with ExceptionalYield

use of org.sonar.java.se.xproc.ExceptionalYield in project sonar-java by SonarSource.

the class BytecodeEGWalkerTest method test_enqueueing_of_exit_block2.

@Test
public void test_enqueueing_of_exit_block2() {
    MethodBehavior mb = getMethodBehavior(ExceptionEnqueue.class, "enqueueExitBlock2(Lorg/sonar/java/bytecode/se/testdata/ExceptionEnqueue;)Z");
    List<HappyPathYield> happyPathYields = mb.happyPathYields().collect(Collectors.toList());
    assertThat(happyPathYields).hasSize(1);
    assertThat(happyPathYields.get(0).resultConstraint()).isNull();
    List<ExceptionalYield> exceptionalYields = mb.exceptionalPathYields().collect(Collectors.toList());
    assertThat(exceptionalYields).hasSize(1);
    assertThat(exceptionalYields.get(0).exceptionType(semanticModel).is("java.io.IOException")).isTrue();
}
Also used : HappyPathYield(org.sonar.java.se.xproc.HappyPathYield) ExceptionalYield(org.sonar.java.se.xproc.ExceptionalYield) MethodBehavior(org.sonar.java.se.xproc.MethodBehavior) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)5 ExceptionalYield (org.sonar.java.se.xproc.ExceptionalYield)5 MethodBehavior (org.sonar.java.se.xproc.MethodBehavior)5 HappyPathYield (org.sonar.java.se.xproc.HappyPathYield)2 SemanticModel (org.sonar.java.resolve.SemanticModel)1 SETestUtils.createSymbolicExecutionVisitor (org.sonar.java.se.SETestUtils.createSymbolicExecutionVisitor)1 SETestUtils.getMethodBehavior (org.sonar.java.se.SETestUtils.getMethodBehavior)1 MethodYield (org.sonar.java.se.xproc.MethodYield)1 Type (org.sonar.plugins.java.api.semantic.Type)1