use of org.sonar.java.se.xproc.MethodBehavior 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();
}
use of org.sonar.java.se.xproc.MethodBehavior in project sonar-java by SonarSource.
the class BytecodeEGWalkerExecuteTest method athrow_should_not_be_linked_to_next_label.
@Test
public void athrow_should_not_be_linked_to_next_label() throws Exception {
CompilationUnitTree tree = (CompilationUnitTree) JavaParser.createParser().parse("class A {int field;}");
SquidClassLoader classLoader = new SquidClassLoader(Collections.singletonList(new File("src/test/JsrRet")));
SemanticModel semanticModel = SemanticModel.createFor(tree, classLoader);
BehaviorCache behaviorCache = new BehaviorCache(classLoader);
behaviorCache.setFileContext(null, semanticModel);
BytecodeEGWalker walker = new BytecodeEGWalker(behaviorCache, semanticModel);
MethodBehavior methodBehavior = walker.getMethodBehavior("org.apache.commons.io.FileUtils#readFileToString(Ljava/io/File;)Ljava/lang/String;", classLoader);
assertThat(methodBehavior.happyPathYields().collect(Collectors.toList())).hasSize(1);
assertThat(methodBehavior.exceptionalPathYields().collect(Collectors.toList())).hasSize(2);
}
use of org.sonar.java.se.xproc.MethodBehavior in project sonar-java by SonarSource.
the class BytecodeEGWalkerExecuteTest method behavior_should_have_declared_exceptions.
@Test
public void behavior_should_have_declared_exceptions() {
MethodBehavior mb = walker.getMethodBehavior(BytecodeEGWalkerExecuteTest.class.getCanonicalName() + "#throwing()V", squidClassLoader);
assertThat(mb.isComplete()).isFalse();
assertThat(mb.getDeclaredExceptions()).containsExactly("java.io.IOException");
}
use of org.sonar.java.se.xproc.MethodBehavior in project sonar-java by SonarSource.
the class BytecodeEGWalkerExecuteTest method method_returning_new_should_have_not_null_result.
@Test
public void method_returning_new_should_have_not_null_result() {
MethodBehavior mb = walker.getMethodBehavior(BytecodeEGWalkerExecuteTest.class.getCanonicalName() + "#newObject()Ljava/lang/Object;", squidClassLoader);
List<MethodYield> yields = mb.yields();
assertThat(yields).hasSize(1);
MethodYield yield = yields.get(0);
assertThat(yield).isInstanceOf(HappyPathYield.class);
ConstraintsByDomain resultConstraint = ((HappyPathYield) yield).resultConstraint();
assertThat(resultConstraint).isNotNull();
assertThat(resultConstraint.get(ObjectConstraint.class)).isEqualTo(ObjectConstraint.NOT_NULL);
TypedConstraint typeConstraint = (TypedConstraint) resultConstraint.get(TypedConstraint.class);
assertThat(typeConstraint.type.equals("java.lang.String")).isTrue();
}
use of org.sonar.java.se.xproc.MethodBehavior in project sonar-java by SonarSource.
the class BytecodeEGWalkerTest method max_step_exception_should_log_warning_and_generate_behavior.
@Test
public void max_step_exception_should_log_warning_and_generate_behavior() {
BytecodeEGWalker bytecodeEGWalker = new BytecodeEGWalker(new BehaviorCache(squidClassLoader), semanticModel) {
@Override
int maxSteps() {
return 2;
}
};
MethodBehavior methodBehavior = getMethodBehavior(BytecodeTestClass.class, "fun(ZLjava/lang/Object;)Ljava/lang/Object;", bytecodeEGWalker);
assertThat(logTester.logs(LoggerLevel.DEBUG)).contains("Dataflow analysis is incomplete for method org.sonar.java.bytecode.se.testdata.BytecodeTestClass#fun(ZLjava/lang/Object;)Ljava/lang/Object;" + " : Too many steps resolving org.sonar.java.bytecode.se.testdata.BytecodeTestClass#fun(ZLjava/lang/Object;)Ljava/lang/Object;");
assertThat(methodBehavior.isComplete()).isFalse();
assertThat(methodBehavior.isVisited()).isTrue();
}
Aggregations