]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-10260 Added without scm IT
authorGuillaume Jambet <guillaume.jambet@sonarsource.com>
Tue, 16 Jan 2018 15:13:19 +0000 (16:13 +0100)
committerDuarte Meneses <duarte.meneses@sonarsource.com>
Wed, 7 Feb 2018 13:33:55 +0000 (14:33 +0100)
tests/projects/scm/xoo-sample-without-scm/sonar-project.properties [new file with mode: 0644]
tests/projects/scm/xoo-sample-without-scm/src/main/xoo/sample/Sample.xoo [new file with mode: 0644]
tests/projects/scm/xoo-sample-without-scm/src/main/xoo/sample/Sample.xoo.new [new file with mode: 0644]
tests/src/test/java/org/sonarqube/tests/source/LineData.java [new file with mode: 0644]
tests/src/test/java/org/sonarqube/tests/source/NoScmTest.java [new file with mode: 0644]
tests/src/test/java/org/sonarqube/tests/source/SourceScmWS.java [new file with mode: 0644]
tests/src/test/java/org/sonarqube/tests/source/SourceSuite.java

diff --git a/tests/projects/scm/xoo-sample-without-scm/sonar-project.properties b/tests/projects/scm/xoo-sample-without-scm/sonar-project.properties
new file mode 100644 (file)
index 0000000..ff04664
--- /dev/null
@@ -0,0 +1,5 @@
+sonar.projectKey=sample-without-scm
+sonar.projectName=Sample without SCM
+sonar.projectVersion=1.0-SNAPSHOT
+sonar.sources=src/main/xoo
+sonar.language=xoo
diff --git a/tests/projects/scm/xoo-sample-without-scm/src/main/xoo/sample/Sample.xoo b/tests/projects/scm/xoo-sample-without-scm/src/main/xoo/sample/Sample.xoo
new file mode 100644 (file)
index 0000000..f23190c
--- /dev/null
@@ -0,0 +1,7 @@
+package sample;
+
+public class Sample {
+
+    private String myMethod() {
+    }
+}
diff --git a/tests/projects/scm/xoo-sample-without-scm/src/main/xoo/sample/Sample.xoo.new b/tests/projects/scm/xoo-sample-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/src/test/java/org/sonarqube/tests/source/LineData.java b/tests/src/test/java/org/sonarqube/tests/source/LineData.java
new file mode 100644 (file)
index 0000000..d9c0f07
--- /dev/null
@@ -0,0 +1,58 @@
+/*
+ * 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.text.ParseException;
+import java.text.SimpleDateFormat;
+import java.util.Date;
+import org.apache.commons.lang.builder.EqualsBuilder;
+import org.apache.commons.lang.builder.HashCodeBuilder;
+import org.apache.commons.lang.builder.ToStringBuilder;
+import org.apache.commons.lang.builder.ToStringStyle;
+
+public class LineData {
+
+  private static final SimpleDateFormat DATETIME_FORMAT = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");
+
+  final String revision;
+  final Date date;
+  final String author;
+
+  public LineData(String revision, String datetime, String author) throws ParseException {
+    this.revision = revision;
+    this.date = DATETIME_FORMAT.parse(datetime);
+    this.author = author;
+  }
+
+  @Override
+  public boolean equals(Object obj) {
+    return EqualsBuilder.reflectionEquals(this, obj);
+  }
+
+  @Override
+  public int hashCode() {
+    return new HashCodeBuilder().append(revision).append(date).append(author).toHashCode();
+  }
+
+  @Override
+  public String toString() {
+    return ToStringBuilder.reflectionToString(this, ToStringStyle.SIMPLE_STYLE);
+  }
+}
\ No newline at end of file
diff --git a/tests/src/test/java/org/sonarqube/tests/source/NoScmTest.java b/tests/src/test/java/org/sonarqube/tests/source/NoScmTest.java
new file mode 100644 (file)
index 0000000..28ddb4b
--- /dev/null
@@ -0,0 +1,133 @@
+/*
+ * 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 util.ItUtils.projectDir;
+
+public class NoScmTest {
+
+  private final String PROJECT_DIRECTORY = "xoo-sample-without-scm";
+  private final String PROJECT_NAME = "sample-without-scm";
+  private final String PATH_TO_SAMPLE = "src/main/xoo/sample/Sample.xoo";
+  private final String FILE_TO_ANALYSE = PROJECT_NAME + ":" + PATH_TO_SAMPLE;
+  private final String PATH_TO_INACTIVATED_SAMPLE = "src/main/xoo/sample/Sample.xoo.new";
+
+  @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 two_analysis_without_scm_on_same_file() throws ParseException, IOException {
+
+    File source = disposableWorkspaceFor(PROJECT_DIRECTORY);
+
+    // First run
+    SonarScanner scanner = SonarScanner.create(source);
+
+    orchestrator.executeBuild(scanner);
+    Map<Integer, LineData> scmData1 = ws.getScmData(FILE_TO_ANALYSE);
+
+    assertThat(scmData1.size()).isEqualTo(1);
+    assertThat(scmData1.get(1).revision).isEmpty();
+    assertThat(scmData1.get(1).author).isEmpty();
+    assertThat(scmData1.get(1).date).isInSameMinuteWindowAs(new Date());
+
+    // 2nd run
+    scanner = SonarScanner.create(source);
+
+    orchestrator.executeBuild(scanner);
+    Map<Integer, LineData> scmData2 = ws.getScmData(FILE_TO_ANALYSE);
+
+    assertThat(scmData2.size()).isEqualTo(1);
+    assertThat(scmData2.get(1).revision).isEmpty();
+    assertThat(scmData2.get(1).author).isEmpty();
+    assertThat(scmData2.get(1).date).isEqualTo(scmData1.get(1).date);
+
+  }
+
+  @Test
+  public void two_analysis_without_scm_on_modified_file() throws ParseException, IOException {
+
+    File source = disposableWorkspaceFor(PROJECT_DIRECTORY);
+
+    // First run
+    SonarScanner scanner = SonarScanner.create(source);
+
+    orchestrator.executeBuild(scanner);
+    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());
+
+    // Swap analysed fo to a modified one
+    File sample = new File(source, PATH_TO_SAMPLE);
+    sample.delete();
+    moveFile(new File(source, PATH_TO_INACTIVATED_SAMPLE), sample);
+
+    // 2nd run
+    scanner = SonarScanner.create(source);
+
+    orchestrator.executeBuild(scanner);
+    scmData = ws.getScmData(FILE_TO_ANALYSE);
+
+    assertThat(scmData.size()).isEqualTo(4);
+    assertThat(scmData.get(1).revision).isEmpty();
+    assertThat(scmData.get(1).author).isEmpty();
+    assertThat(scmData.get(1).date).isInSameMinuteWindowAs(new Date());
+
+    assertThat(scmData.get(5).revision).isEmpty();
+    assertThat(scmData.get(5).author).isEmpty();
+    assertThat(scmData.get(5).date).isAfter(scmData.get(1).date);
+
+  }
+
+  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/SourceScmWS.java b/tests/src/test/java/org/sonarqube/tests/source/SourceScmWS.java
new file mode 100644 (file)
index 0000000..77472ed
--- /dev/null
@@ -0,0 +1,52 @@
+/*
+ * 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 java.text.ParseException;
+import java.util.HashMap;
+import java.util.Map;
+import org.sonar.wsclient.jsonsimple.JSONArray;
+import org.sonar.wsclient.jsonsimple.JSONObject;
+import org.sonar.wsclient.jsonsimple.JSONValue;
+
+public class SourceScmWS {
+
+  private final Orchestrator orchestrator;
+
+  public SourceScmWS(Orchestrator orchestrator) {
+    this.orchestrator = orchestrator;
+  }
+
+  public Map<Integer, LineData> getScmData(String fileKey) throws ParseException {
+    Map<Integer, LineData> result = new HashMap<>();
+    String json = orchestrator.getServer().adminWsClient().get("api/sources/scm", "key", fileKey);
+    JSONObject obj = (JSONObject) JSONValue.parse(json);
+    JSONArray array = (JSONArray) obj.get("scm");
+    for (Object anArray : array) {
+      JSONArray item = (JSONArray) anArray;
+      String datetime = (String) item.get(2);
+      result.put(((Long) item.get(0)).intValue(), new LineData((String) item.get(3), datetime, (String) item.get(1)));
+    }
+    return result;
+  }
+
+
+}
index 7057e94c6feb56180acb26fb5eba7a3c3c099571..23dd36d49665a2c14709cdaeecd94a23afe0672e 100644 (file)
@@ -30,15 +30,16 @@ import static util.ItUtils.xooPlugin;
 @Suite.SuiteClasses({
   EncodingTest.class,
   ScmTest.class,
+  NoScmTest.class,
   SourceViewerTest.class
 })
 public class SourceSuite {
 
   @ClassRule
   public static final Orchestrator ORCHESTRATOR = Orchestrator.builderEnv()
-    .addPlugin(xooPlugin())
     // reduce memory for Elasticsearch
     .setServerProperty("sonar.search.javaOpts", "-Xms128m -Xmx128m")
+    .addPlugin(xooPlugin())
     .build();
 
 }