From 612d0fce39ffe0be40b37c6e4de87fc69294d1f1 Mon Sep 17 00:00:00 2001 From: Julien HENRY Date: Wed, 27 Aug 2014 18:17:34 +0200 Subject: [PATCH] Improve UT coverage --- .../sensor/duplication/DuplicationGroup.java | 6 ++ .../fs/internal/DefaultInputDirTest.java | 72 +++++++++++++++++++ .../DefaultDuplicationBuilderTest.java | 50 +++++++++++++ 3 files changed, 128 insertions(+) create mode 100644 sonar-plugin-api/src/test/java/org/sonar/api/batch/fs/internal/DefaultInputDirTest.java create mode 100644 sonar-plugin-api/src/test/java/org/sonar/api/batch/sensor/duplication/internal/DefaultDuplicationBuilderTest.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 280e8106246..28e1e7d2041 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 @@ -99,10 +99,16 @@ public class DuplicationGroup { this.originBlock = originBlock; } + /** + * For unit test and internal use only. + */ public void setDuplicates(List duplicates) { this.duplicates = duplicates; } + /** + * For unit test and internal use only. + */ public DuplicationGroup addDuplicate(Block anotherBlock) { this.duplicates.add(anotherBlock); return this; diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/batch/fs/internal/DefaultInputDirTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/batch/fs/internal/DefaultInputDirTest.java new file mode 100644 index 00000000000..ca11cd02bcc --- /dev/null +++ b/sonar-plugin-api/src/test/java/org/sonar/api/batch/fs/internal/DefaultInputDirTest.java @@ -0,0 +1,72 @@ +/* + * 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.fs.internal; + +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.TemporaryFolder; + +import java.io.File; + +import static org.fest.assertions.Assertions.assertThat; + +public class DefaultInputDirTest { + + @Rule + public TemporaryFolder temp = new TemporaryFolder(); + + @Test + public void test() throws Exception { + File dir = temp.newFolder("src"); + DefaultInputDir inputDir = new DefaultInputDir("src") + .setFile(dir) + .setKey("ABCDE"); + + assertThat(inputDir.key()).isEqualTo("ABCDE"); + assertThat(inputDir.file().getAbsolutePath()).isEqualTo(dir.getAbsolutePath()); + assertThat(inputDir.relativePath()).isEqualTo("src"); + assertThat(new File(inputDir.relativePath())).isRelative(); + assertThat(inputDir.absolutePath()).endsWith("src"); + assertThat(new File(inputDir.absolutePath())).isAbsolute(); + } + + @Test + public void testEqualsAndHashCode() throws Exception { + File dir1 = temp.newFolder("src"); + DefaultInputDir inputDir1 = new DefaultInputDir("src") + .setFile(dir1) + .setKey("ABCDE"); + + File dir2 = temp.newFolder("src2"); + DefaultInputDir inputDir2 = new DefaultInputDir("src") + .setFile(dir2) + .setKey("ABCDE"); + + assertThat(inputDir1.equals(inputDir1)).isTrue(); + assertThat(inputDir1.equals(inputDir2)).isTrue(); + assertThat(inputDir1.equals("foo")).isFalse(); + + assertThat(inputDir1.hashCode()).isEqualTo(114148); + + assertThat(inputDir1.toString()).contains("[relative=src, abs="); + + } + +} 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 new file mode 100644 index 00000000000..8da5669729f --- /dev/null +++ b/sonar-plugin-api/src/test/java/org/sonar/api/batch/sensor/duplication/internal/DefaultDuplicationBuilderTest.java @@ -0,0 +1,50 @@ +/* + * 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.internal; + +import org.junit.Test; +import org.sonar.api.batch.fs.internal.DefaultInputFile; +import org.sonar.api.batch.sensor.duplication.DuplicationGroup; + +import java.util.List; + +import static org.fest.assertions.Assertions.assertThat; + +public class DefaultDuplicationBuilderTest { + + @Test + public void test() { + DefaultDuplicationBuilder builder = new DefaultDuplicationBuilder(new DefaultInputFile("foo.php").setKey("foo:foo.php")); + + List duplicationGroup = builder.originBlock(1, 11) + .isDuplicatedBy(new DefaultInputFile("foo.php"), 40, 50) + .isDuplicatedBy(new DefaultInputFile("foo2.php"), 1, 10) + .originBlock(20, 30) + .isDuplicatedBy(new DefaultInputFile("foo3.php"), 30, 40) + .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); + assertThat(duplicationGroup.get(0).duplicates()).hasSize(2); + } + +} -- 2.39.5