use of org.sonar.java.se.xproc.MethodYield in project sonar-java by SonarSource.
the class ExplodedGraphWalkerTest method different_exceptions_lead_to_different_program_states_with_catch_exception_block.
@Test
public void different_exceptions_lead_to_different_program_states_with_catch_exception_block() {
Set<Type> encounteredExceptions = new HashSet<>();
int[] tested = { 0 };
JavaCheckVerifier.verifyNoIssue("src/test/files/se/ExceptionalSymbolicValueStacked.java", new SymbolicExecutionVisitor(Collections.emptyList(), new BehaviorCache(new SquidClassLoader(new ArrayList<>()))) {
private ExplodedGraphWalker explodedGraphWalker;
@Override
public void visitNode(Tree tree) {
if (explodedGraphWalker == null) {
explodedGraphWalker = new ExplodedGraphWalker(this.behaviorCache, (SemanticModel) context.getSemanticModel()) {
private ExplodedGraph.Node firstExceptionalNode = null;
@Override
public void enqueue(ProgramPoint newProgramPoint, ProgramState programState, boolean exitPath, MethodYield methodYield) {
SymbolicValue.ExceptionalSymbolicValue exceptionSV = null;
SymbolicValue peekValue = programState.peekValue();
boolean getNode = false;
if (peekValue instanceof SymbolicValue.ExceptionalSymbolicValue) {
exceptionSV = (SymbolicValue.ExceptionalSymbolicValue) peekValue;
Type exceptionType = exceptionSV.exceptionType();
if (exceptionType != null && (exceptionType.is("org.foo.MyException1") || exceptionType.is("org.foo.MyException2"))) {
encounteredExceptions.add(exceptionType);
getNode = true;
}
}
int workListSize = workList.size();
super.enqueue(newProgramPoint, programState, exitPath, methodYield);
if (getNode) {
if (firstExceptionalNode == null) {
firstExceptionalNode = workList.peekFirst();
}
assertThat(workList.size()).as("Should have created a new node in the graph for each of the exceptions").isEqualTo(workListSize + 1);
assertThat(workList.peekFirst().programState.peekValue()).as("Exceptional Symbolic Value should stay on the stack").isEqualTo(exceptionSV);
tested[0]++;
}
}
};
}
MethodTree methodTree = (MethodTree) tree;
if ("foo".equals(methodTree.symbol().name())) {
explodedGraphWalker.visitMethod(methodTree, methodBehaviorForSymbol(methodTree.symbol()));
} else {
super.visitNode(methodTree);
}
}
});
assertThat(encounteredExceptions).hasSize(2);
assertThat(tested[0]).isEqualTo(2);
}
Aggregations