Search in sources :

Example 21 with MethodBehavior

use of org.sonar.java.se.xproc.MethodBehavior 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)

Example 22 with MethodBehavior

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

the class BytecodeEGWalkerTest method test_int_comparator.

@Test
public void test_int_comparator() throws Exception {
    MethodBehavior methodBehavior = getMethodBehavior("int_comparison(II)Ljava/lang/Object;");
    assertThat(methodBehavior.yields()).hasSize(1);
    HappyPathYield methodYield = ((HappyPathYield) methodBehavior.yields().get(0));
    assertThat(methodYield.resultConstraint().get(ObjectConstraint.class)).isSameAs(ObjectConstraint.NULL);
}
Also used : HappyPathYield(org.sonar.java.se.xproc.HappyPathYield) MethodBehavior(org.sonar.java.se.xproc.MethodBehavior) Test(org.junit.Test)

Example 23 with MethodBehavior

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

the class BytecodeEGWalkerTest method method_array.

@Test
public void method_array() throws Exception {
    BytecodeEGWalker walker = getBytecodeEGWalker(squidClassLoader);
    MethodBehavior behavior = walker.getMethodBehavior("java.lang.Class[]#clone()Ljava/lang/Object;", squidClassLoader);
    assertThat(behavior).isNull();
}
Also used : MethodBehavior(org.sonar.java.se.xproc.MethodBehavior) Test(org.junit.Test)

Example 24 with MethodBehavior

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

the class BytecodeEGWalkerTest method unchecked_exceptions_should_be_enqueued.

@Test
public void unchecked_exceptions_should_be_enqueued() {
    MethodBehavior mb = getMethodBehavior(ExceptionEnqueue.class, "test(Lorg/sonar/java/bytecode/se/testdata/ExceptionEnqueue;)Ljava/lang/Object;");
    List<Constraint> resultConstraint = mb.happyPathYields().map(y -> y.resultConstraint().get(ObjectConstraint.class)).collect(Collectors.toList());
    assertThat(resultConstraint).contains(ObjectConstraint.NOT_NULL, ObjectConstraint.NULL);
    List<String> exceptions = mb.exceptionalPathYields().map(y -> y.exceptionType(semanticModel).fullyQualifiedName()).collect(Collectors.toList());
    assertThat(exceptions).contains("org.sonar.java.bytecode.se.testdata.ExceptionEnqueue$ExceptionCatch", "org.sonar.java.bytecode.se.testdata.ExceptionEnqueue$ThrowableCatch", "org.sonar.java.bytecode.se.testdata.ExceptionEnqueue$ErrorCatch");
}
Also used : BytecodeTestClass(org.sonar.java.bytecode.se.testdata.BytecodeTestClass) ObjectConstraint(org.sonar.java.se.constraint.ObjectConstraint) Iterables(com.google.common.collect.Iterables) BeforeClass(org.junit.BeforeClass) ProgramState(org.sonar.java.se.ProgramState) MethodYield(org.sonar.java.se.xproc.MethodYield) CompilationUnitTree(org.sonar.plugins.java.api.tree.CompilationUnitTree) ExceptionEnqueue(org.sonar.java.bytecode.se.testdata.ExceptionEnqueue) DivisionByZeroCheck(org.sonar.java.se.checks.DivisionByZeroCheck) Lists(com.google.common.collect.Lists) Assertions.assertThat(org.fest.assertions.Assertions.assertThat) FinalBytecodeTestClass(org.sonar.java.bytecode.se.testdata.FinalBytecodeTestClass) SymbolicValue(org.sonar.java.se.symbolicvalues.SymbolicValue) MethodBehavior(org.sonar.java.se.xproc.MethodBehavior) BooleanConstraint(org.sonar.java.se.constraint.BooleanConstraint) JavaParser(org.sonar.java.ast.parser.JavaParser) Collection(java.util.Collection) SquidClassLoader(org.sonar.java.bytecode.loader.SquidClassLoader) MaxRelationBytecode(org.sonar.java.bytecode.se.testdata.MaxRelationBytecode) FileUtils(org.apache.commons.io.FileUtils) Test(org.junit.Test) Type(org.sonar.plugins.java.api.semantic.Type) Collectors(java.util.stream.Collectors) File(java.io.File) List(java.util.List) Rule(org.junit.Rule) LogTester(org.sonar.api.utils.log.LogTester) SemanticModel(org.sonar.java.resolve.SemanticModel) ByteStreams(com.google.common.io.ByteStreams) BehaviorCache(org.sonar.java.se.xproc.BehaviorCache) Constraint(org.sonar.java.se.constraint.Constraint) ExceptionalYield(org.sonar.java.se.xproc.ExceptionalYield) HappyPathYield(org.sonar.java.se.xproc.HappyPathYield) LoggerLevel(org.sonar.api.utils.log.LoggerLevel) ObjectConstraint(org.sonar.java.se.constraint.ObjectConstraint) BooleanConstraint(org.sonar.java.se.constraint.BooleanConstraint) Constraint(org.sonar.java.se.constraint.Constraint) MethodBehavior(org.sonar.java.se.xproc.MethodBehavior) Test(org.junit.Test)

Example 25 with MethodBehavior

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

the class BytecodeEGWalkerTest method test_enqueueing_of_exit_block4.

@Test
public void test_enqueueing_of_exit_block4() {
    MethodBehavior mb = getMethodBehavior(ExceptionEnqueue.class, "enqueueExitBlock4()Z");
    List<HappyPathYield> happyPathYields = mb.happyPathYields().collect(Collectors.toList());
    assertThat(happyPathYields.get(0).resultConstraint().hasConstraint(BooleanConstraint.TRUE)).isTrue();
    assertThat(mb.exceptionalPathYields().findFirst().isPresent()).isFalse();
}
Also used : HappyPathYield(org.sonar.java.se.xproc.HappyPathYield) MethodBehavior(org.sonar.java.se.xproc.MethodBehavior) Test(org.junit.Test)

Aggregations

MethodBehavior (org.sonar.java.se.xproc.MethodBehavior)36 Test (org.junit.Test)30 SemanticModel (org.sonar.java.resolve.SemanticModel)10 BehaviorCache (org.sonar.java.se.xproc.BehaviorCache)10 HappyPathYield (org.sonar.java.se.xproc.HappyPathYield)10 MethodYield (org.sonar.java.se.xproc.MethodYield)10 List (java.util.List)8 Collectors (java.util.stream.Collectors)8 SymbolicValue (org.sonar.java.se.symbolicvalues.SymbolicValue)8 Type (org.sonar.plugins.java.api.semantic.Type)8 Lists (com.google.common.collect.Lists)6 SquidClassLoader (org.sonar.java.bytecode.loader.SquidClassLoader)6 ProgramState (org.sonar.java.se.ProgramState)6 DivisionByZeroCheck (org.sonar.java.se.checks.DivisionByZeroCheck)6 BooleanConstraint (org.sonar.java.se.constraint.BooleanConstraint)6 ObjectConstraint (org.sonar.java.se.constraint.ObjectConstraint)6 ExceptionalYield (org.sonar.java.se.xproc.ExceptionalYield)6 File (java.io.File)5 Collection (java.util.Collection)5 Set (java.util.Set)5