From f7d71ed243c9c964105a00e19f2522e1564b00ab Mon Sep 17 00:00:00 2001 From: Julien HENRY Date: Mon, 1 Sep 2014 18:16:10 +0200 Subject: [PATCH] Improve code coverage --- .../sensor/duplication/DuplicationGroup.java | 6 +- .../duplication/DuplicationGroupTest.java | 63 +++++++++++++++++++ .../DefaultDuplicationBuilderTest.java | 8 ++- 3 files changed, 72 insertions(+), 5 deletions(-) create mode 100644 sonar-plugin-api/src/test/java/org/sonar/api/batch/sensor/duplication/DuplicationGroupTest.java diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/batch/sensor/duplication/DuplicationGroup.java b/sonar-plugin-api/src/main/java/org/sonar/api/batch/sensor/duplication/DuplicationGroup.java index 710ab81c652..5d3d833a906 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/batch/sensor/duplication/DuplicationGroup.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/batch/sensor/duplication/DuplicationGroup.java @@ -146,8 +146,10 @@ public class DuplicationGroup { EqualsBuilder equalsBuilder = new EqualsBuilder() .append(originBlock, rhs.originBlock) .append(duplicates.size(), rhs.duplicates.size()); - for (int i = 0; i < duplicates.size(); i++) { - equalsBuilder.append(duplicates.get(i), rhs.duplicates.get(i)); + if (duplicates.size() == rhs.duplicates.size()) { + for (int i = 0; i < duplicates.size(); i++) { + equalsBuilder.append(duplicates.get(i), rhs.duplicates.get(i)); + } } return equalsBuilder.isEquals(); } diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/batch/sensor/duplication/DuplicationGroupTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/batch/sensor/duplication/DuplicationGroupTest.java new file mode 100644 index 00000000000..98345403912 --- /dev/null +++ b/sonar-plugin-api/src/test/java/org/sonar/api/batch/sensor/duplication/DuplicationGroupTest.java @@ -0,0 +1,63 @@ +/* + * SonarQube, open source software quality management tool. + * Copyright (C) 2008-2014 SonarSource + * mailto:contact AT sonarsource DOT com + * + * SonarQube is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * SonarQube is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ +package org.sonar.api.batch.sensor.duplication; + +import org.junit.Test; + +import java.util.Arrays; + +import static org.fest.assertions.Assertions.assertThat; + +public class DuplicationGroupTest { + + @Test + public void testBlockEqualsAndCo() { + DuplicationGroup.Block b1 = new DuplicationGroup.Block("foo", 1, 10); + DuplicationGroup.Block b2 = new DuplicationGroup.Block("foo", 1, 10); + assertThat(b1).isEqualTo(b1); + assertThat(b1).isEqualTo(b2); + assertThat(b1).isNotEqualTo(""); + assertThat(b1).isNotEqualTo(new DuplicationGroup.Block("foo1", 1, 10)); + assertThat(b1).isNotEqualTo(new DuplicationGroup.Block("foo", 2, 10)); + assertThat(b1).isNotEqualTo(new DuplicationGroup.Block("foo", 1, 11)); + + assertThat(b1.hashCode()).isEqualTo(188843970); + assertThat(b1.toString()).isEqualTo("DuplicationGroup.Block[resourceKey=foo,startLine=1,length=10]"); + } + + @Test + public void testDuplicationGroupEqualsAndCo() { + DuplicationGroup d1 = new DuplicationGroup(new DuplicationGroup.Block("foo", 1, 10)); + d1.setDuplicates(Arrays.asList(new DuplicationGroup.Block("foo", 20, 10), new DuplicationGroup.Block("foo2", 1, 10))); + DuplicationGroup d2 = new DuplicationGroup(new DuplicationGroup.Block("foo", 1, 10)); + d2.setDuplicates(Arrays.asList(new DuplicationGroup.Block("foo", 20, 10), new DuplicationGroup.Block("foo2", 1, 10))); + assertThat(d1).isEqualTo(d1); + assertThat(d1).isEqualTo(d2); + assertThat(d1).isNotEqualTo(""); + assertThat(d1).isNotEqualTo(new DuplicationGroup(new DuplicationGroup.Block("foo", 1, 10))); + + assertThat(d1.hashCode()).isEqualTo(578909124); + assertThat(d1.toString()).contains("[\n" + + " origin=DuplicationGroup.Block[resourceKey=foo,startLine=1,length=10]\n" + + " duplicates=[DuplicationGroup.Block[resourceKey=foo,startLine=20,length=10], DuplicationGroup.Block[resourceKey=foo2,startLine=1,length=10]]\n" + + "]"); + } + +} diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/batch/sensor/duplication/internal/DefaultDuplicationBuilderTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/batch/sensor/duplication/internal/DefaultDuplicationBuilderTest.java index 8da5669729f..17a813dbf35 100644 --- a/sonar-plugin-api/src/test/java/org/sonar/api/batch/sensor/duplication/internal/DefaultDuplicationBuilderTest.java +++ b/sonar-plugin-api/src/test/java/org/sonar/api/batch/sensor/duplication/internal/DefaultDuplicationBuilderTest.java @@ -22,6 +22,7 @@ package org.sonar.api.batch.sensor.duplication.internal; import org.junit.Test; import org.sonar.api.batch.fs.internal.DefaultInputFile; import org.sonar.api.batch.sensor.duplication.DuplicationGroup; +import org.sonar.api.batch.sensor.duplication.DuplicationGroup.Block; import java.util.List; @@ -41,9 +42,10 @@ public class DefaultDuplicationBuilderTest { .build(); assertThat(duplicationGroup).hasSize(2); - assertThat(duplicationGroup.get(0).originBlock().resourceKey()).isEqualTo("foo:foo.php"); - assertThat(duplicationGroup.get(0).originBlock().startLine()).isEqualTo(1); - assertThat(duplicationGroup.get(0).originBlock().length()).isEqualTo(11); + Block originBlock = duplicationGroup.get(0).originBlock(); + assertThat(originBlock.resourceKey()).isEqualTo("foo:foo.php"); + assertThat(originBlock.startLine()).isEqualTo(1); + assertThat(originBlock.length()).isEqualTo(11); assertThat(duplicationGroup.get(0).duplicates()).hasSize(2); } -- 2.39.5