]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-10257 Added ITs
authorGuillaume Jambet <guillaume.jambet@sonarsource.com>
Wed, 17 Jan 2018 16:04:45 +0000 (17:04 +0100)
committerDuarte Meneses <duarte.meneses@sonarsource.com>
Wed, 7 Feb 2018 13:33:55 +0000 (14:33 +0100)
13 files changed:
tests/projects/scm/xoo-sample-with-then-without-scm/scm-repository.zip [new file with mode: 0644]
tests/projects/scm/xoo-sample-with-then-without-scm/sonar-project.properties [new file with mode: 0644]
tests/projects/scm/xoo-sample-with-then-without-scm/src/main/xoo/sample/Sample.xoo [new file with mode: 0644]
tests/projects/scm/xoo-sample-with-then-without-scm/src/main/xoo/sample/Sample.xoo.new [new file with mode: 0644]
tests/projects/scm/xoo-sample-without-then-with-scm/scm-repository.zip [new file with mode: 0644]
tests/projects/scm/xoo-sample-without-then-with-scm/sonar-project.properties [new file with mode: 0644]
tests/projects/scm/xoo-sample-without-then-with-scm/src/main/xoo/sample/Sample.xoo [new file with mode: 0644]
tests/projects/scm/xoo-sample-without-then-with-scm/src/main/xoo/sample/Sample.xoo.new [new file with mode: 0644]
tests/projects/scm/xoo-sample-without-then-with-scm/src/main/xoo/sample/Sample.xoo.updated [new file with mode: 0644]
tests/src/test/java/org/sonarqube/tests/source/NoScmThenScmTest.java [new file with mode: 0644]
tests/src/test/java/org/sonarqube/tests/source/ScmThenNoScmTest.java [new file with mode: 0644]
tests/src/test/java/org/sonarqube/tests/source/SourceSuite.java
tests/src/test/java/org/sonarqube/tests/source/ZipUtils.java [new file with mode: 0644]

diff --git a/tests/projects/scm/xoo-sample-with-then-without-scm/scm-repository.zip b/tests/projects/scm/xoo-sample-with-then-without-scm/scm-repository.zip
new file mode 100644 (file)
index 0000000..f0dada5
Binary files /dev/null and b/tests/projects/scm/xoo-sample-with-then-without-scm/scm-repository.zip differ
diff --git a/tests/projects/scm/xoo-sample-with-then-without-scm/sonar-project.properties b/tests/projects/scm/xoo-sample-with-then-without-scm/sonar-project.properties
new file mode 100644 (file)
index 0000000..ead950e
--- /dev/null
@@ -0,0 +1,5 @@
+sonar.projectKey=sample-with-then-without-scm
+sonar.projectName=Sample with then without SCM
+sonar.projectVersion=1.0-SNAPSHOT
+sonar.sources=src/main/xoo
+sonar.language=xoo
diff --git a/tests/projects/scm/xoo-sample-with-then-without-scm/src/main/xoo/sample/Sample.xoo b/tests/projects/scm/xoo-sample-with-then-without-scm/src/main/xoo/sample/Sample.xoo
new file mode 100644 (file)
index 0000000..4d8ceed
--- /dev/null
@@ -0,0 +1,7 @@
+package sample;
+
+public class Sample {
+
+    private String myMethod() {
+    }
+}
\ No newline at end of file
diff --git a/tests/projects/scm/xoo-sample-with-then-without-scm/src/main/xoo/sample/Sample.xoo.new b/tests/projects/scm/xoo-sample-with-then-without-scm/src/main/xoo/sample/Sample.xoo.new
new file mode 100644 (file)
index 0000000..bed00cc
--- /dev/null
@@ -0,0 +1,13 @@
+package sample;
+
+public class Sample {
+
+    private String attr;
+
+    public Sample(String attr) {
+        this.attr = attr;
+    }
+
+    private String myMethod() {
+    }
+}
diff --git a/tests/projects/scm/xoo-sample-without-then-with-scm/scm-repository.zip b/tests/projects/scm/xoo-sample-without-then-with-scm/scm-repository.zip
new file mode 100644 (file)
index 0000000..14c3545
Binary files /dev/null and b/tests/projects/scm/xoo-sample-without-then-with-scm/scm-repository.zip differ
diff --git a/tests/projects/scm/xoo-sample-without-then-with-scm/sonar-project.properties b/tests/projects/scm/xoo-sample-without-then-with-scm/sonar-project.properties
new file mode 100644 (file)
index 0000000..ef74a8d
--- /dev/null
@@ -0,0 +1,5 @@
+sonar.projectKey=sample-without-then-with-scm
+sonar.projectName=Sample without then with SCM
+sonar.projectVersion=1.0-SNAPSHOT
+sonar.sources=src/main/xoo
+sonar.language=xoo
diff --git a/tests/projects/scm/xoo-sample-without-then-with-scm/src/main/xoo/sample/Sample.xoo b/tests/projects/scm/xoo-sample-without-then-with-scm/src/main/xoo/sample/Sample.xoo
new file mode 100644 (file)
index 0000000..4d8ceed
--- /dev/null
@@ -0,0 +1,7 @@
+package sample;
+
+public class Sample {
+
+    private String myMethod() {
+    }
+}
\ No newline at end of file
diff --git a/tests/projects/scm/xoo-sample-without-then-with-scm/src/main/xoo/sample/Sample.xoo.new b/tests/projects/scm/xoo-sample-without-then-with-scm/src/main/xoo/sample/Sample.xoo.new
new file mode 100644 (file)
index 0000000..bed00cc
--- /dev/null
@@ -0,0 +1,13 @@
+package sample;
+
+public class Sample {
+
+    private String attr;
+
+    public Sample(String attr) {
+        this.attr = attr;
+    }
+
+    private String myMethod() {
+    }
+}
diff --git a/tests/projects/scm/xoo-sample-without-then-with-scm/src/main/xoo/sample/Sample.xoo.updated b/tests/projects/scm/xoo-sample-without-then-with-scm/src/main/xoo/sample/Sample.xoo.updated
new file mode 100644 (file)
index 0000000..c34188e
--- /dev/null
@@ -0,0 +1,7 @@
+package sample;
+
+public class Sample {
+  // changed convention
+  private String method() {
+  }
+}
diff --git a/tests/src/test/java/org/sonarqube/tests/source/NoScmThenScmTest.java b/tests/src/test/java/org/sonarqube/tests/source/NoScmThenScmTest.java
new file mode 100644 (file)
index 0000000..8da6f58
--- /dev/null
@@ -0,0 +1,174 @@
+/*
+ * SonarQube
+ * Copyright (C) 2009-2018 SonarSource SA
+ * mailto:info AT sonarsource DOT com
+ *
+ * This program 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.
+ *
+ * This program 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.sonarqube.tests.source;
+
+import com.sonar.orchestrator.Orchestrator;
+import com.sonar.orchestrator.build.SonarScanner;
+import java.io.File;
+import java.io.IOException;
+import java.text.ParseException;
+import java.util.Date;
+import java.util.Map;
+import org.junit.ClassRule;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.TemporaryFolder;
+import org.sonarqube.qa.util.Tester;
+
+import static org.apache.commons.io.FileUtils.copyDirectory;
+import static org.apache.commons.io.FileUtils.moveFile;
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.sonarqube.tests.source.SourceSuite.ORCHESTRATOR;
+import static org.sonarqube.tests.source.ZipUtils.unzip;
+import static util.ItUtils.projectDir;
+
+public class NoScmThenScmTest {
+
+  private static final String PROJECT_DIRECTORY = "xoo-sample-without-then-with-scm";
+  private static final String PROJECT_NAME = "sample-without-then-with-scm";
+  private static final String PATH_TO_SAMPLE = "src/main/xoo/sample/Sample.xoo";
+  private static final String FILE_TO_ANALYSE = PROJECT_NAME + ":" + PATH_TO_SAMPLE;
+
+  @ClassRule
+  public static Orchestrator orchestrator = ORCHESTRATOR;
+  private SourceScmWS ws = new SourceScmWS(orchestrator);
+
+  @Rule
+  public TemporaryFolder temporaryFolder = new TemporaryFolder();
+
+  @Rule
+  public Tester tester = new Tester(orchestrator);
+
+  @Test
+  public void without_and_then_with_scm_on_same_file() throws ParseException, IOException {
+
+    File source = disposableWorkspaceFor(PROJECT_DIRECTORY);
+
+    // First run
+    orchestrator.executeBuild(newScannerWithoutSCM(source));
+
+    Map<Integer, LineData> scmData = ws.getScmData(FILE_TO_ANALYSE);
+    assertThat(scmData.size()).isEqualTo(1);
+    assertThat(scmData.get(1).revision).isEmpty();
+    assertThat(scmData.get(1).author).isEmpty();
+    assertThat(scmData.get(1).date).isInSameMinuteWindowAs(new Date());
+
+    // Restore SCM Repository
+    unzip(new File(source, "scm-repository.zip"), source.getAbsolutePath());
+
+    // 2nd run
+    orchestrator.executeBuild(newScannerWithSCM(source));
+
+    scmData = ws.getScmData(FILE_TO_ANALYSE);
+    assertThat(scmData.size()).isEqualTo(1);
+    assertThat(scmData.get(1).revision).isEqualTo("6664564f3358c9abf5f595eca31f228b3a95c886");
+    assertThat(scmData.get(1).author).isEqualTo("guillaume.jambet@sonarsource.com");
+    assertThat(scmData.get(1).date).isEqualToIgnoringMillis("2018-01-18T09:56:01");
+  }
+
+  @Test
+  public void without_and_then_with_scm_on_modified_file() throws ParseException, IOException {
+
+    File source = disposableWorkspaceFor(PROJECT_DIRECTORY);
+
+    // First run
+    orchestrator.executeBuild(newScannerWithoutSCM(source));
+
+    Map<Integer, LineData> scmData = ws.getScmData(FILE_TO_ANALYSE);
+    assertThat(scmData.size()).isEqualTo(1);
+    assertThat(scmData.get(1).revision).isEmpty();
+    assertThat(scmData.get(1).author).isEmpty();
+    assertThat(scmData.get(1).date).isInSameMinuteWindowAs(new Date());
+
+    // Restore SCM Repository
+    unzip(new File(source, "scm-repository.zip"), source.getAbsolutePath());
+
+    // Swap analysed fo to a modified one
+    File sample = new File(source, PATH_TO_SAMPLE);
+    sample.delete();
+    moveFile(new File(source, "src/main/xoo/sample/Sample.xoo.updated"), sample);
+
+    // 2nd run
+    orchestrator.executeBuild(newScannerWithSCM(source));
+
+    scmData = ws.getScmData(FILE_TO_ANALYSE);
+    assertThat(scmData.get(1).revision).isEmpty();
+    assertThat(scmData.get(1).author).isEmpty();
+    assertThat(scmData.get(1).date).isEqualTo(scmData.get(1).date);
+
+    assertThat(scmData.get(4).revision).isEmpty();
+    assertThat(scmData.get(4).author).isEmpty();
+    assertThat(scmData.get(4).date).isInSameMinuteWindowAs(new Date());
+
+  }
+
+  @Test
+  public void without_and_then_with_scm_on_modified_file_with_new_lines() throws ParseException, IOException {
+
+    File source = disposableWorkspaceFor(PROJECT_DIRECTORY);
+
+    // First run
+    orchestrator.executeBuild(newScannerWithoutSCM(source));
+
+    Map<Integer, LineData> scmData = ws.getScmData(FILE_TO_ANALYSE);
+    assertThat(scmData.size()).isEqualTo(1);
+    assertThat(scmData.get(1).revision).isEmpty();
+    assertThat(scmData.get(1).author).isEmpty();
+    assertThat(scmData.get(1).date).isInSameMinuteWindowAs(new Date());
+
+    // Restore SCM Repository
+    unzip(new File(source, "scm-repository.zip"), source.getAbsolutePath());
+
+    // Swap analysed fo to a modified one
+    File sample = new File(source, PATH_TO_SAMPLE);
+    sample.delete();
+    moveFile(new File(source, "src/main/xoo/sample/Sample.xoo.new"), sample);
+
+    // 2nd run
+    orchestrator.executeBuild(newScannerWithSCM(source));
+
+    scmData = ws.getScmData(FILE_TO_ANALYSE);
+    assertThat(scmData.get(1).revision).isEmpty();
+    assertThat(scmData.get(1).author).isEmpty();
+    assertThat(scmData.get(1).date).isEqualToIgnoringMillis("2018-01-17T10:35:23");
+
+    assertThat(scmData.get(5).revision).isEmpty();
+    assertThat(scmData.get(5).author).isEmpty();
+    assertThat(scmData.get(5).date).isInSameMinuteWindowAs(new Date());
+
+  }
+
+  private SonarScanner newScannerWithoutSCM(File source) {
+    return SonarScanner.create(source);
+  }
+
+  private SonarScanner newScannerWithSCM(File source) {
+    return SonarScanner.create(source)
+      .setProperty("sonar.scm.provider", "git")
+      .setProperty("sonar.scm.disabled", "false");
+  }
+
+  private File disposableWorkspaceFor(String project) throws IOException {
+    File origin = projectDir("scm/" + project);
+    copyDirectory(origin.getParentFile(), temporaryFolder.getRoot());
+    return new File(temporaryFolder.getRoot(), project);
+  }
+
+}
diff --git a/tests/src/test/java/org/sonarqube/tests/source/ScmThenNoScmTest.java b/tests/src/test/java/org/sonarqube/tests/source/ScmThenNoScmTest.java
new file mode 100644 (file)
index 0000000..0437a48
--- /dev/null
@@ -0,0 +1,137 @@
+/*
+ * SonarQube
+ * Copyright (C) 2009-2018 SonarSource SA
+ * mailto:info AT sonarsource DOT com
+ *
+ * This program 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.
+ *
+ * This program 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.sonarqube.tests.source;
+
+import com.sonar.orchestrator.Orchestrator;
+import com.sonar.orchestrator.build.SonarScanner;
+import java.io.File;
+import java.io.IOException;
+import java.text.ParseException;
+import java.util.Date;
+import java.util.Map;
+import org.junit.ClassRule;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.TemporaryFolder;
+import org.sonarqube.qa.util.Tester;
+
+import static org.apache.commons.io.FileUtils.copyDirectory;
+import static org.apache.commons.io.FileUtils.deleteDirectory;
+import static org.apache.commons.io.FileUtils.moveFile;
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.sonarqube.tests.source.SourceSuite.ORCHESTRATOR;
+import static org.sonarqube.tests.source.ZipUtils.unzip;
+import static util.ItUtils.projectDir;
+
+public class ScmThenNoScmTest {
+
+  @ClassRule
+  public static Orchestrator orchestrator = ORCHESTRATOR;
+  private SourceScmWS ws = new SourceScmWS(orchestrator);
+
+  @Rule
+  public TemporaryFolder temporaryFolder = new TemporaryFolder();
+
+  @Rule
+  public Tester tester = new Tester(orchestrator);
+
+  @Test
+  public void with_and_then_without_scm_on_same_file() throws ParseException, IOException {
+
+    File source = disposableWorkspaceFor("xoo-sample-with-then-without-scm");
+    unzip(new File(source, "scm-repository.zip"), source.getAbsolutePath());
+
+    // First run
+    SonarScanner scanner = SonarScanner.create(source)
+      .setProperty("sonar.scm.provider", "git")
+      .setProperty("sonar.scm.disabled", "false");
+
+    orchestrator.executeBuild(scanner);
+
+    Map<Integer, LineData> scmData = ws.getScmData("sample-with-then-without-scm:src/main/xoo/sample/Sample.xoo");
+    assertThat(scmData.size()).isEqualTo(1);
+    assertThat(scmData.get(1).revision).isEqualTo("036fcddbf771b54d7c5f7c8125a493d7d03a9d9d");
+    assertThat(scmData.get(1).author).isEqualTo("guillaume.jambet@sonarsource.com");
+    assertThat(scmData.get(1).date).isEqualToIgnoringMillis("2018-01-17T10:35:23");
+
+    // Drop SCM
+    deleteDirectory(new File(source, ".git"));
+
+    // 2nd run
+    scanner = SonarScanner.create(source);
+
+    orchestrator.executeBuild(scanner);
+    scmData = ws.getScmData("sample-with-then-without-scm:src/main/xoo/sample/Sample.xoo");
+    assertThat(scmData.size()).isEqualTo(1);
+    assertThat(scmData.get(1).revision).isEmpty();
+    assertThat(scmData.get(1).author).isEmpty();
+    assertThat(scmData.get(1).date).isEqualToIgnoringMillis("2018-01-17T10:35:23");
+
+  }
+
+  @Test
+  public void with_and_then_without_scm_on_modified_file() throws ParseException, IOException {
+
+    File source = disposableWorkspaceFor("xoo-sample-with-then-without-scm");
+    unzip(new File(source, "scm-repository.zip"), source.getAbsolutePath());
+
+    // First run
+    SonarScanner scanner = SonarScanner.create(source)
+      .setProperty("sonar.scm.provider", "git")
+      .setProperty("sonar.scm.disabled", "false");
+
+    orchestrator.executeBuild(scanner);
+
+    Map<Integer, LineData> scmData = ws.getScmData("sample-with-then-without-scm:src/main/xoo/sample/Sample.xoo");
+    assertThat(scmData.size()).isEqualTo(1);
+    assertThat(scmData.get(1).revision).isEqualTo("036fcddbf771b54d7c5f7c8125a493d7d03a9d9d");
+    assertThat(scmData.get(1).author).isEqualTo("guillaume.jambet@sonarsource.com");
+    assertThat(scmData.get(1).date).isEqualToIgnoringMillis("2018-01-17T10:35:23");
+
+    // Drop SCM
+    deleteDirectory(new File(source, ".git"));
+
+    // Swap analysed fo to a modified one
+    File sample = new File(source, "src/main/xoo/sample/Sample.xoo");
+    sample.delete();
+    moveFile(new File(source, "src/main/xoo/sample/Sample.xoo.new"), sample);
+
+    // 2nd run
+    scanner = SonarScanner.create(source);
+
+    orchestrator.executeBuild(scanner);
+    scmData = ws.getScmData("sample-with-then-without-scm:src/main/xoo/sample/Sample.xoo");
+    assertThat(scmData.get(1).revision).isEmpty();
+    assertThat(scmData.get(1).author).isEmpty();
+    assertThat(scmData.get(1).date).isEqualToIgnoringMillis("2018-01-17T10:35:23");
+
+    assertThat(scmData.get(5).revision).isEmpty();
+    assertThat(scmData.get(5).author).isEmpty();
+    assertThat(scmData.get(5).date).isInSameMinuteWindowAs(new Date());
+
+  }
+
+  private File disposableWorkspaceFor(String project) throws IOException {
+    File origin = projectDir("scm/" + project);
+    copyDirectory(origin.getParentFile(), temporaryFolder.getRoot());
+    return new File(temporaryFolder.getRoot(), project);
+  }
+
+}
index 23dd36d49665a2c14709cdaeecd94a23afe0672e..e658a52d90c2463c3fcf54ea8db8b4de10b0a248 100644 (file)
@@ -31,15 +31,19 @@ import static util.ItUtils.xooPlugin;
   EncodingTest.class,
   ScmTest.class,
   NoScmTest.class,
+  ScmThenNoScmTest.class,
   SourceViewerTest.class
 })
 public class SourceSuite {
 
+
   @ClassRule
-  public static final Orchestrator ORCHESTRATOR = Orchestrator.builderEnv()
-    // reduce memory for Elasticsearch
+  public static final Orchestrator ORCHESTRATOR = Orchestrator
+    .builderEnv()
     .setServerProperty("sonar.search.javaOpts", "-Xms128m -Xmx128m")
+    .setOrchestratorProperty("sonar-scm-git-plugin-version", "1.3.0.869")
     .addPlugin(xooPlugin())
+    .addMavenPlugin("org.sonarsource.scm.git", "sonar-scm-git-plugin", "sonar-scm-git-plugin-version")
     .build();
 
 }
diff --git a/tests/src/test/java/org/sonarqube/tests/source/ZipUtils.java b/tests/src/test/java/org/sonarqube/tests/source/ZipUtils.java
new file mode 100644 (file)
index 0000000..258cc38
--- /dev/null
@@ -0,0 +1,70 @@
+/*
+ * SonarQube
+ * Copyright (C) 2009-2018 SonarSource SA
+ * mailto:info AT sonarsource DOT com
+ *
+ * This program 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.
+ *
+ * This program 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.sonarqube.tests.source;
+
+import java.io.BufferedOutputStream;
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.util.zip.ZipEntry;
+import java.util.zip.ZipInputStream;
+
+public class ZipUtils {
+
+  // I see that we have apache commons compress in the path so it should be possible to reduce the boiler plate
+  // -- but nothing out of the box : still have to walk the tree and uncompress file by file.
+
+  static void unzip(File archive, String directory) throws IOException {
+    try (ZipInputStream zis = new ZipInputStream(new FileInputStream(archive))) {
+
+      ZipEntry entry = zis.getNextEntry();
+      while (entry != null) {
+
+        File file = new File(directory, entry.getName());
+
+        if (entry.isDirectory()) {
+          file.mkdirs();
+        } else {
+
+          File parent = file.getParentFile();
+          if (!parent.exists()) {
+            parent.mkdirs();
+          }
+
+          try (BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(file))) {
+            long size = entry.getSize();
+            byte[] buffer = new byte[Math.toIntExact(size)];
+            if (size > 0) {
+              int location;
+              while ((location = zis.read(buffer)) != -1) {
+                bos.write(buffer, 0, location);
+              }
+            }
+          }
+
+        }
+
+        entry = zis.getNextEntry();
+      }
+    }
+  }
+
+}