From 4d5a32120b9028622e8c0e5fb24814633634653a Mon Sep 17 00:00:00 2001 From: Guillaume Jambet Date: Tue, 16 Jan 2018 16:13:19 +0100 Subject: [PATCH] SONAR-10260 Added without scm IT --- .../sonar-project.properties | 5 + .../src/main/xoo/sample/Sample.xoo | 7 + .../src/main/xoo/sample/Sample.xoo.new | 13 ++ .../org/sonarqube/tests/source/LineData.java | 58 ++++++++ .../org/sonarqube/tests/source/NoScmTest.java | 133 ++++++++++++++++++ .../sonarqube/tests/source/SourceScmWS.java | 52 +++++++ .../sonarqube/tests/source/SourceSuite.java | 3 +- 7 files changed, 270 insertions(+), 1 deletion(-) create mode 100644 tests/projects/scm/xoo-sample-without-scm/sonar-project.properties create mode 100644 tests/projects/scm/xoo-sample-without-scm/src/main/xoo/sample/Sample.xoo create mode 100644 tests/projects/scm/xoo-sample-without-scm/src/main/xoo/sample/Sample.xoo.new create mode 100644 tests/src/test/java/org/sonarqube/tests/source/LineData.java create mode 100644 tests/src/test/java/org/sonarqube/tests/source/NoScmTest.java create mode 100644 tests/src/test/java/org/sonarqube/tests/source/SourceScmWS.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 index 00000000000..ff04664a7ce --- /dev/null +++ b/tests/projects/scm/xoo-sample-without-scm/sonar-project.properties @@ -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 index 00000000000..f23190c4e3a --- /dev/null +++ b/tests/projects/scm/xoo-sample-without-scm/src/main/xoo/sample/Sample.xoo @@ -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 index 00000000000..bed00ccc65e --- /dev/null +++ b/tests/projects/scm/xoo-sample-without-scm/src/main/xoo/sample/Sample.xoo.new @@ -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 index 00000000000..d9c0f07f8a0 --- /dev/null +++ b/tests/src/test/java/org/sonarqube/tests/source/LineData.java @@ -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 index 00000000000..28ddb4b046e --- /dev/null +++ b/tests/src/test/java/org/sonarqube/tests/source/NoScmTest.java @@ -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 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 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 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 index 00000000000..77472ed4be0 --- /dev/null +++ b/tests/src/test/java/org/sonarqube/tests/source/SourceScmWS.java @@ -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 getScmData(String fileKey) throws ParseException { + Map 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; + } + + +} diff --git a/tests/src/test/java/org/sonarqube/tests/source/SourceSuite.java b/tests/src/test/java/org/sonarqube/tests/source/SourceSuite.java index 7057e94c6fe..23dd36d4966 100644 --- a/tests/src/test/java/org/sonarqube/tests/source/SourceSuite.java +++ b/tests/src/test/java/org/sonarqube/tests/source/SourceSuite.java @@ -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(); } -- 2.39.5