]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-2501 Add API to add test and covering
authorJulien Lancelot <julien.lancelot@gmail.com>
Tue, 15 Jan 2013 13:21:48 +0000 (14:21 +0100)
committerJulien Lancelot <julien.lancelot@gmail.com>
Tue, 15 Jan 2013 13:22:03 +0000 (14:22 +0100)
pom.xml
sonar-batch/src/main/java/org/sonar/batch/bootstrap/ProjectModule.java
sonar-plugin-api/src/main/java/org/sonar/api/tests/FileTest.java [new file with mode: 0644]
sonar-plugin-api/src/main/java/org/sonar/api/tests/ProjectTests.java [new file with mode: 0644]
sonar-plugin-api/src/main/java/org/sonar/api/tests/ProjectTestsImpl.java [new file with mode: 0644]
sonar-plugin-api/src/main/java/org/sonar/api/tests/Test.java [new file with mode: 0644]
sonar-plugin-api/src/test/java/org/sonar/api/tests/ProjectTestsImplTest.java [new file with mode: 0644]

diff --git a/pom.xml b/pom.xml
index ffaa383dbe9e53b139516fb83c0e254f2aa36d46..643aa644654abcb798640fd16078c6d57fd4fb5c 100644 (file)
--- a/pom.xml
+++ b/pom.xml
@@ -69,7 +69,7 @@
   </prerequisites>
 
   <properties>
-    <sonarJava.version>1.1</sonarJava.version>
+    <sonarJava.version>1.2-SNAPSHOT</sonarJava.version>
     <sonarGwt.version>3.3.1</sonarGwt.version>
     <h2.version>1.3.167</h2.version>
     <jetty.version>6.1.25</jetty.version>
index bb7df9a5ed0bfcf7ca2181e01cfae27d5e838009..a966646be5f22cc4e3f851ab6b9323a9326d1b2c 100644 (file)
@@ -27,6 +27,7 @@ import org.sonar.api.batch.bootstrap.ProjectDefinition;
 import org.sonar.api.profiles.RulesProfile;
 import org.sonar.api.resources.Languages;
 import org.sonar.api.resources.Project;
+import org.sonar.api.tests.ProjectTestsImpl;
 import org.sonar.batch.DefaultProfileLoader;
 import org.sonar.batch.DefaultProjectClasspath;
 import org.sonar.batch.DefaultProjectFileSystem2;
@@ -102,6 +103,7 @@ public class ProjectModule extends Module {
     container.addSingleton(DefaultModelFinder.class);
     container.addSingleton(DefaultProfileLoader.class);
     container.addSingleton(DryRunExporter.class);
+    container.addSingleton(ProjectTestsImpl.class);
     container.addPicoAdapter(new ProfileProvider());
   }
 
diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/tests/FileTest.java b/sonar-plugin-api/src/main/java/org/sonar/api/tests/FileTest.java
new file mode 100644 (file)
index 0000000..1d31426
--- /dev/null
@@ -0,0 +1,83 @@
+/*
+ * Sonar, open source software quality management tool.
+ * Copyright (C) 2008-2012 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * Sonar 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.
+ *
+ * Sonar 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 Sonar; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02
+ */
+
+package org.sonar.api.tests;
+
+import org.apache.commons.lang.builder.ToStringBuilder;
+import org.apache.commons.lang.builder.ToStringStyle;
+
+import java.util.List;
+
+import static com.google.common.collect.Lists.newArrayList;
+
+public class FileTest {
+
+  private String key;
+
+  private List<Test> tests;
+
+  public FileTest(String key) {
+    this.key = key;
+    tests = newArrayList();
+  }
+
+  public String getKey() {
+    return key;
+  }
+
+  public List<Test> getTests() {
+    return tests;
+  }
+
+  public void addTest(Test test) {
+    tests.add(test);
+  }
+
+  @Override
+  public boolean equals(Object o) {
+    if (this == o) {
+      return true;
+    }
+    if (o == null || getClass() != o.getClass()) {
+      return false;
+    }
+
+    FileTest fileTest = (FileTest) o;
+
+    if (!key.equals(fileTest.key)) {
+      return false;
+    }
+
+    return true;
+  }
+
+  @Override
+  public int hashCode() {
+    return key.hashCode();
+  }
+
+  @Override
+  public String toString() {
+    return new ToStringBuilder(this, ToStringStyle.SHORT_PREFIX_STYLE)
+        .append("key", key)
+        .append("tests", tests)
+        .toString();
+  }
+}
diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/tests/ProjectTests.java b/sonar-plugin-api/src/main/java/org/sonar/api/tests/ProjectTests.java
new file mode 100644 (file)
index 0000000..b09e582
--- /dev/null
@@ -0,0 +1,35 @@
+/*
+ * Sonar, open source software quality management tool.
+ * Copyright (C) 2008-2012 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * Sonar 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.
+ *
+ * Sonar 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 Sonar; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02
+ */
+
+package org.sonar.api.tests;
+
+import java.util.Collection;
+
+/**
+ *
+ * @since 3.5
+ */
+public interface ProjectTests {
+
+  void addTest(String fileTestKey, Test test);
+
+  void cover(String fileTestKey, String test, String mainFile, Collection<Integer> lines);
+
+}
diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/tests/ProjectTestsImpl.java b/sonar-plugin-api/src/main/java/org/sonar/api/tests/ProjectTestsImpl.java
new file mode 100644 (file)
index 0000000..dd36e78
--- /dev/null
@@ -0,0 +1,84 @@
+/*
+ * Sonar, open source software quality management tool.
+ * Copyright (C) 2008-2012 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * Sonar 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.
+ *
+ * Sonar 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 Sonar; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02
+ */
+
+package org.sonar.api.tests;
+
+import com.google.common.base.Predicate;
+import com.google.common.collect.Iterables;
+import org.apache.commons.lang.builder.ToStringBuilder;
+import org.apache.commons.lang.builder.ToStringStyle;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.sonar.api.BatchExtension;
+
+import java.util.Collection;
+import java.util.List;
+
+import static com.google.common.collect.Lists.newArrayList;
+
+public class ProjectTestsImpl implements ProjectTests, BatchExtension {
+
+  private static final Logger LOG = LoggerFactory.getLogger(ProjectTestsImpl.class);
+  private List<FileTest> fileTests;
+
+  public ProjectTestsImpl() {
+    fileTests = newArrayList();
+  }
+
+  public void addTest(String fileTestKey, Test test) {
+    FileTest fileTest = getFileTest(fileTestKey);
+    fileTest.addTest(test);
+
+    LOG.info("Added a new test : " + toString());
+  }
+
+  public List<FileTest> getFileTests() {
+    return fileTests;
+  }
+
+  public void cover(String fileTestKey, String test, String mainFile, Collection<Integer> lines){
+    FileTest fileTest = find(fileTestKey);
+    LOG.info("Covering - File test :" + toString() + ", test:" + test + ", file:" + mainFile+ ", lines:"+ Iterables.toString(lines));
+  }
+
+  private FileTest getFileTest(final String key) {
+    FileTest fileTest = find(key);
+    if (fileTest == null) {
+      fileTest = new FileTest(key);
+      fileTests.add(fileTest);
+    }
+    return fileTest;
+  }
+
+  private FileTest find(final String key){
+    return Iterables.find(fileTests, new Predicate<FileTest>() {
+      public boolean apply(FileTest fileTest) {
+        return fileTest.getKey().equals(key);
+      }
+    }, null);
+  }
+
+  @Override
+  public String toString() {
+    return new ToStringBuilder(this, ToStringStyle.SHORT_PREFIX_STYLE)
+        .append("fileTests", fileTests)
+        .toString();
+  }
+}
diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/tests/Test.java b/sonar-plugin-api/src/main/java/org/sonar/api/tests/Test.java
new file mode 100644 (file)
index 0000000..513bc2f
--- /dev/null
@@ -0,0 +1,105 @@
+/*
+ * Sonar, open source software quality management tool.
+ * Copyright (C) 2008-2012 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * Sonar 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.
+ *
+ * Sonar 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 Sonar; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02
+ */
+
+package org.sonar.api.tests;
+
+import org.apache.commons.lang.builder.ToStringBuilder;
+import org.apache.commons.lang.builder.ToStringStyle;
+
+public class Test {
+
+  private String name;
+  private String stackTrace;
+  private String message;
+  private long durationMilliseconds;
+  private String status;
+
+  public Test(String name) {
+    this.name = name;
+  }
+
+  public long getDurationMilliseconds() {
+    return durationMilliseconds;
+  }
+
+  public Test setDurationMilliseconds(long durationMilliseconds) {
+    this.durationMilliseconds = durationMilliseconds;
+    return this;
+  }
+
+  public String getStatus() {
+    return status;
+  }
+
+  public Test setStatus(String status) {
+    this.status = status;
+    return this;
+  }
+
+  public String getStackTrace() {
+    return stackTrace;
+  }
+
+  public Test setStackTrace(String stackTrace) {
+    this.stackTrace = stackTrace;
+    return this;
+  }
+
+  public String getMessage() {
+    return message;
+  }
+
+  public Test setMessage(String message) {
+    this.message = message;
+    return this;
+  }
+
+  @Override
+  public boolean equals(Object o) {
+    if (this == o) {
+      return true;
+    }
+    if (o == null || getClass() != o.getClass()) {
+      return false;
+    }
+
+    Test test = (Test) o;
+
+    if (!name.equals(test.name)) {
+      return false;
+    }
+
+    return true;
+  }
+
+  @Override
+  public int hashCode() {
+    return name.hashCode();
+  }
+
+  @Override
+  public String toString() {
+    return new ToStringBuilder(this, ToStringStyle.SHORT_PREFIX_STYLE)
+        .append("name", name)
+        .append("durationMilliseconds", durationMilliseconds)
+        .append("status", status)
+        .toString();
+  }
+}
diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/tests/ProjectTestsImplTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/tests/ProjectTestsImplTest.java
new file mode 100644 (file)
index 0000000..dc0585c
--- /dev/null
@@ -0,0 +1,56 @@
+/*
+ * Sonar, open source software quality management tool.
+ * Copyright (C) 2008-2012 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * Sonar 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.
+ *
+ * Sonar 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 Sonar; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02
+ */
+
+package org.sonar.api.tests;
+
+import org.junit.Before;
+import org.junit.Test;
+
+import static org.fest.assertions.Assertions.assertThat;
+
+public class ProjectTestsImplTest {
+
+  private ProjectTestsImpl projectTests;
+
+  @Before
+  public void before(){
+    projectTests = new ProjectTestsImpl();
+  }
+
+  @Test
+  public void should_add_new_test() throws Exception {
+    org.sonar.api.tests.Test test = new org.sonar.api.tests.Test("test").setStatus("ok");
+    projectTests.addTest("key", test);
+    assertThat(projectTests.getFileTests()).hasSize(1);
+    assertThat(projectTests.getFileTests().get(0).getTests()).hasSize(1);
+  }
+
+  @Test
+  public void should_add_new_test_on_existing_file_test() throws Exception {
+    org.sonar.api.tests.Test test = new org.sonar.api.tests.Test("test").setStatus("ok");
+    projectTests.addTest("key", test);
+
+    org.sonar.api.tests.Test test2 = new org.sonar.api.tests.Test("test2").setStatus("ok");
+    projectTests.addTest("key", test2);
+
+    assertThat(projectTests.getFileTests()).hasSize(1);
+    assertThat(projectTests.getFileTests().get(0).getTests()).hasSize(2);
+  }
+}