From e3ce850eab6e863105bf1aca390c568a8bba8e11 Mon Sep 17 00:00:00 2001 From: Michael Gumowski Date: Fri, 2 Dec 2016 17:32:57 +0100 Subject: [PATCH] Fix quality flaw: complete coverage of MethodYield equals method --- .../java/org/sonar/java/se/MethodYield.java | 3 +- .../org/sonar/java/se/MethodYieldTest.java | 49 +++++++++++++++++++ 2 files changed, 51 insertions(+), 1 deletion(-) diff --git a/java-frontend/src/main/java/org/sonar/java/se/MethodYield.java b/java-frontend/src/main/java/org/sonar/java/se/MethodYield.java index 2fbb26bcae1..e74c278de00 100644 --- a/java-frontend/src/main/java/org/sonar/java/se/MethodYield.java +++ b/java-frontend/src/main/java/org/sonar/java/se/MethodYield.java @@ -157,7 +157,8 @@ public boolean equals(Object obj) { MethodYield other = (MethodYield) obj; if (!Arrays.equals(parametersConstraints, other.parametersConstraints) || exception != other.exception - || resultIndex != other.resultIndex) { + || resultIndex != other.resultIndex + || varArgs != other.varArgs) { return false; } if (resultConstraint != null) { diff --git a/java-frontend/src/test/java/org/sonar/java/se/MethodYieldTest.java b/java-frontend/src/test/java/org/sonar/java/se/MethodYieldTest.java index ec79ebf68b6..55085a9d3b0 100644 --- a/java-frontend/src/test/java/org/sonar/java/se/MethodYieldTest.java +++ b/java-frontend/src/test/java/org/sonar/java/se/MethodYieldTest.java @@ -72,6 +72,13 @@ public void test_creation_of_states() throws Exception { assertThat(generatedStatesFromFirstYield).hasSize(1); } + @Test + public void test_toString() throws Exception { + SymbolicExecutionVisitor sev = createSymbolicExecutionVisitor("src/test/files/se/XProcYields.java"); + MethodYield yield = getMethodBehavior(sev, "bar").getValue().yields().get(0); + assertThat(yield.toString()).isEqualTo("{params: [TRUE, NOT_NULL], result: null (-1), exceptional: false}"); + } + private static enum Status { A, B } @@ -108,6 +115,48 @@ public void all_constraints_should_be_valid_to_generate_a_new_state() throws Exc assertThat(generatedStatesFromFirstYield.iterator().next().getConstraintWithStatus(sv2, Status.B)).isNotNull(); } + @Test + public void test_yield_equality() { + MethodYield yield = new MethodYield(1, false); + MethodYield otherYield; + + assertThat(yield).isNotEqualTo(null); + assertThat(yield).isNotEqualTo(new Object()); + + // same instance + assertThat(yield).isEqualTo(yield); + + // same constraints, same nb of parameters, same exceptional aspect + assertThat(yield).isEqualTo(new MethodYield(1, false)); + + // arity is taken into account + assertThat(yield).isNotEqualTo(new MethodYield(0, false)); + + // varargs is taken into account + assertThat(yield).isNotEqualTo(new MethodYield(1, true)); + + // same arity and constraints but exceptional path + otherYield = new MethodYield(1, false); + otherYield.exception = true; + assertThat(yield).isNotEqualTo(otherYield); + + // same arity and constraints but different return value + otherYield = new MethodYield(1, false); + otherYield.resultIndex = 0; + assertThat(yield).isNotEqualTo(otherYield); + + // same arity but different return constraint + otherYield = new MethodYield(1, false); + otherYield.resultConstraint = ObjectConstraint.NOT_NULL; + assertThat(yield).isNotEqualTo(otherYield); + + // same return constraint + yield.resultConstraint = ObjectConstraint.NOT_NULL; + otherYield = new MethodYield(1, false); + otherYield.resultConstraint = ObjectConstraint.NOT_NULL; + assertThat(yield).isEqualTo(otherYield); + } + @Test public void constraints_on_varargs() throws Exception { SymbolicExecutionVisitor sev = createSymbolicExecutionVisitor("src/test/files/se/VarArgsYields.java");