From 4282cd3c7715db1c370e4cdf680a84e2794bd4ff Mon Sep 17 00:00:00 2001 From: "antoine.vinot" Date: Wed, 29 Nov 2023 09:17:01 +0100 Subject: [PATCH] SQSCANNER-126 - Refactor test to be more precise in case of failure --- .../sonarsource/scanner/it/ScannerTest.java | 43 +++++++++++-------- 1 file changed, 25 insertions(+), 18 deletions(-) diff --git a/it/src/test/java/com/sonarsource/scanner/it/ScannerTest.java b/it/src/test/java/com/sonarsource/scanner/it/ScannerTest.java index 3241b16..29d6114 100644 --- a/it/src/test/java/com/sonarsource/scanner/it/ScannerTest.java +++ b/it/src/test/java/com/sonarsource/scanner/it/ScannerTest.java @@ -47,9 +47,8 @@ public class ScannerTest extends ScannerTestCase { Map projectMeasures = getMeasures( "java:basedir-with-source", "files", "ncloc"); - assertThat(parseInt(projectMeasures.get("files").getValue())).isEqualTo(1); - assertThat(parseInt(projectMeasures.get("ncloc").getValue())) - .isGreaterThan(1); + + verifyProjectMeasures(projectMeasures, 1); } /** @@ -61,9 +60,9 @@ public class ScannerTest extends ScannerTestCase { .useNative() .setProjectKey("SAMPLE"); orchestrator.executeBuild(build); + Map projectMeasures = getMeasures("SAMPLE", "files", "ncloc"); - assertThat(parseInt(projectMeasures.get("files").getValue())).isEqualTo(1); - assertThat(parseInt(projectMeasures.get("ncloc").getValue())).isGreaterThan(1); + verifyProjectMeasures(projectMeasures, 1); } /** @@ -75,11 +74,19 @@ public class ScannerTest extends ScannerTestCase { .setProjectKey("SAMPLE"); orchestrator.executeBuild(build); - Map projectMeasures = getMeasures("SAMPLE", "files", - "ncloc"); - assertThat(parseInt(projectMeasures.get("files").getValue())).isEqualTo(2); - assertThat(parseInt(projectMeasures.get("ncloc").getValue())) - .isGreaterThan(1); + Map projectMeasures = getMeasures("SAMPLE", "files", "ncloc"); + verifyProjectMeasures(projectMeasures, 2); + } + + private void verifyProjectMeasures(Map projectMeasures, int expectedFiles) { + assertThat(projectMeasures).isNotNull() + .containsKeys("files", "ncloc"); + Measure files = projectMeasures.get("files"); + assertThat(files).isNotNull(); + assertThat(parseInt(files.getValue())).isEqualTo(expectedFiles); + Measure ncloc = projectMeasures.get("ncloc"); + assertThat(ncloc).isNotNull(); + assertThat(parseInt(ncloc.getValue())).isGreaterThan(1); } /** @@ -122,7 +129,7 @@ public class ScannerTest extends ScannerTestCase { SonarScanner build = newScanner(new File("projects/bad-source-dirs")); BuildResult result = orchestrator.executeBuildQuietly(build); - assertThat(result.getStatus()).isNotEqualTo(0); + assertThat(result.getStatus()).isNotZero(); // with the following message assertThat(result.getLogs()) .contains("Invalid value of sonar.sources for bad-source-dirs"); @@ -140,9 +147,9 @@ public class ScannerTest extends ScannerTestCase { // Note: we can't really check the locale value and the charset because the ones used during the Sonar analysis may not be the ones // used to launch the tests. But we can check that the analysis is platform dependent (i.e. "sonar.sourceEncoding" hasn't been set). - assertThat(log).contains("Default locale:"); - assertThat(log).contains(", source code encoding:"); - assertThat(log).contains("(analysis is platform dependent)"); + assertThat(log).contains("Default locale:") + .contains(", source code encoding:") + .contains("(analysis is platform dependent)"); } /** @@ -234,11 +241,11 @@ public class ScannerTest extends ScannerTestCase { SonarScanner build = newScanner(new File("projects/simple-sample")) .setEnvironmentVariable("SONAR_SCANNER_OPTS", "-Xmx1k"); BuildResult executeBuild = orchestrator.executeBuildQuietly(build); - assertThat(executeBuild.getLastStatus()).isNotEqualTo(0); + assertThat(executeBuild.getLastStatus()).isNotZero(); String logs = executeBuild.getLogs(); - assertThat(logs).contains("Error occurred during initialization of VM"); - // Not the same message with JRE 8 and 11 - assertThat(logs).containsPattern("Too small (initial|maximum) heap"); + assertThat(logs).contains("Error occurred during initialization of VM") + // Not the same message with JRE 8 and 11 + .containsPattern("Too small (initial|maximum) heap"); } // SQSCANNER-24 -- 2.39.5