]> source.dussan.org Git - sonarqube.git/commitdiff
Improve UT coverage
authorJulien HENRY <julien.henry@sonarsource.com>
Wed, 27 Aug 2014 16:17:34 +0000 (18:17 +0200)
committerJulien HENRY <julien.henry@sonarsource.com>
Wed, 27 Aug 2014 16:17:59 +0000 (18:17 +0200)
sonar-plugin-api/src/main/java/org/sonar/api/batch/sensor/duplication/DuplicationGroup.java
sonar-plugin-api/src/test/java/org/sonar/api/batch/fs/internal/DefaultInputDirTest.java [new file with mode: 0644]
sonar-plugin-api/src/test/java/org/sonar/api/batch/sensor/duplication/internal/DefaultDuplicationBuilderTest.java [new file with mode: 0644]

index 280e81062469c3194ebebd560decdc259fcf76a2..28e1e7d2041841a81c9c002f67ad99c8ba2a0f16 100644 (file)
@@ -99,10 +99,16 @@ public class DuplicationGroup {
     this.originBlock = originBlock;
   }
 
+  /**
+   * For unit test and internal use only.
+   */
   public void setDuplicates(List<Block> 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 (file)
index 0000000..ca11cd0
--- /dev/null
@@ -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 (file)
index 0000000..8da5669
--- /dev/null
@@ -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> 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);
+  }
+
+}