]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-23327 Update tests to Junit5
authorMatteo Mara <matteo.mara@sonarsource.com>
Fri, 11 Oct 2024 13:20:42 +0000 (15:20 +0200)
committersonartech <sonartech@sonarsource.com>
Fri, 11 Oct 2024 20:02:44 +0000 (20:02 +0000)
sonar-scanner-engine/src/test/java/org/sonar/scanner/repository/TelemetryCacheTest.java
sonar-scanner-engine/src/test/java/org/sonar/scanner/sensor/ModuleSensorContextTest.java
sonar-scanner-protocol/build.gradle
sonar-scanner-protocol/src/test/java/org/sonar/scanner/protocol/output/FileStructureTest.java
sonar-scanner-protocol/src/test/java/org/sonar/scanner/protocol/output/ScannerReportWriterTest.java

index cbdb518db7f73bdfcad89acceeab1d3a43e9b3eb..dc6fe24b18beeb37634fe28fb6e31c06ce822823 100644 (file)
@@ -27,57 +27,57 @@ import static org.assertj.core.data.MapEntry.entry;
 
 class TelemetryCacheTest {
 
-    TelemetryCache underTest = new TelemetryCache();
+  TelemetryCache underTest = new TelemetryCache();
 
-    @Test
-    void put_EntryIsAddedToCache() {
-        assertThat(underTest.getAll()).isEmpty();
+  @Test
+  void put_EntryIsAddedToCache() {
+    assertThat(underTest.getAll()).isEmpty();
 
-        underTest.put("key", "value");
-        assertThat(underTest.getAll()).containsOnly(entry("key", "value"));
-    }
+    underTest.put("key", "value");
+    assertThat(underTest.getAll()).containsOnly(entry("key", "value"));
+  }
 
-    @Test
-    void put_whenKeyIsAlreadyThere_EntryOverridesPreviousValue() {
-        underTest.put("key", "value");
-        underTest.put("key", "newValue");
-        assertThat(underTest.getAll()).containsOnly(entry("key", "newValue"));
-    }
+  @Test
+  void put_whenKeyIsAlreadyThere_EntryOverridesPreviousValue() {
+    underTest.put("key", "value");
+    underTest.put("key", "newValue");
+    assertThat(underTest.getAll()).containsOnly(entry("key", "newValue"));
+  }
 
-    @Test
-    void put_whenCacheIsAlreadyFull_newEntryIsNotAdded() {
-        for (int i = 0; i < 1000; i++) {
-            underTest.put("key" + i, "value" + i);
-        }
-        underTest.put("key", "value");
-        assertThat(underTest.getAll()).hasSize(1000);
-        assertThat(underTest.getAll()).doesNotContain(entry("key", "value"));
+  @Test
+  void put_whenCacheIsAlreadyFull_newEntryIsNotAdded() {
+    for (int i = 0; i < 1000; i++) {
+      underTest.put("key" + i, "value" + i);
     }
+    underTest.put("key", "value");
+    assertThat(underTest.getAll()).hasSize(1000);
+    assertThat(underTest.getAll()).doesNotContain(entry("key", "value"));
+  }
 
-    @Test
-    void put_whenCacheIsAlreadyFull_newEntryIsAddedIfKeyAlreadyThere() {
-        for (int i = 0; i < 1000; i++) {
-            underTest.put("key" + i, "value" + i);
-        }
-        underTest.put("key1", "newValue");
-        underTest.put("key", "newValue");
-
-        assertThat(underTest.getAll()).hasSize(1000);
-        assertThat(underTest.getAll()).contains(entry("key1", "newValue"));
+  @Test
+  void put_whenCacheIsAlreadyFull_newEntryIsAddedIfKeyAlreadyThere() {
+    for (int i = 0; i < 1000; i++) {
+      underTest.put("key" + i, "value" + i);
     }
+    underTest.put("key1", "newValue");
+    underTest.put("key", "newValue");
 
-    @Test
-    void put_whenKeyIsNull_IAEIsThrown() {
-        assertThatThrownBy(() -> underTest.put(null, "value"))
-          .isInstanceOf(IllegalArgumentException.class)
-          .hasMessage("Key of the telemetry entry must not be null");
-    }
+    assertThat(underTest.getAll()).hasSize(1000);
+    assertThat(underTest.getAll()).contains(entry("key1", "newValue"));
+  }
 
-    @Test
-    void put_whenValueIsNull_IAEIsThrown() {
-        assertThatThrownBy(() -> underTest.put("key", null))
-          .isInstanceOf(IllegalArgumentException.class)
-          .hasMessage("Value of the telemetry entry must not be null");
-    }
+  @Test
+  void put_whenKeyIsNull_IAEIsThrown() {
+    assertThatThrownBy(() -> underTest.put(null, "value"))
+      .isInstanceOf(IllegalArgumentException.class)
+      .hasMessage("Key of the telemetry entry must not be null");
+  }
+
+  @Test
+  void put_whenValueIsNull_IAEIsThrown() {
+    assertThatThrownBy(() -> underTest.put("key", null))
+      .isInstanceOf(IllegalArgumentException.class)
+      .hasMessage("Value of the telemetry entry must not be null");
+  }
 
 }
index 4ca7d33d6428804e31d4f5d98cb5043d66421d76..feb5c1ddb1782075629a8ee779d232a04617c56c 100644 (file)
  */
 package org.sonar.scanner.sensor;
 
-import org.junit.Before;
-import org.junit.Rule;
-import org.junit.Test;
-import org.junit.rules.TemporaryFolder;
+import java.io.File;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.io.TempDir;
 import org.sonar.api.SonarEdition;
 import org.sonar.api.SonarQubeSide;
 import org.sonar.api.SonarRuntime;
@@ -46,10 +46,10 @@ import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.verify;
 import static org.mockito.Mockito.when;
 
-public class ModuleSensorContextTest {
+class ModuleSensorContextTest {
 
-  @Rule
-  public TemporaryFolder temp = new TemporaryFolder();
+  @TempDir
+  public File temp;
 
   private final ActiveRules activeRules = new ActiveRulesBuilder().build();
   private final MapSettings settings = new MapSettings();
@@ -65,15 +65,15 @@ public class ModuleSensorContextTest {
   private ExecutingSensorContext executingSensorContext = mock(ExecutingSensorContext.class);
   private ScannerPluginRepository pluginRepository = mock(ScannerPluginRepository.class);
 
-  @Before
-  public void prepare() throws Exception {
-    fs = new DefaultFileSystem(temp.newFolder().toPath());
+  @BeforeEach
+  void prepare() {
+    fs = new DefaultFileSystem(temp);
     underTest = new ModuleSensorContext(mock(DefaultInputProject.class), mock(InputModule.class), settings.asConfig(), settings, fs, activeRules, sensorStorage, runtime,
       branchConfiguration, writeCache, readCache, analysisCacheEnabled, unchangedFilesHandler, executingSensorContext, pluginRepository);
   }
 
   @Test
-  public void shouldProvideComponents_returnsNotNull() {
+  void shouldProvideComponents_returnsNotNull() {
     assertThat(underTest.activeRules()).isEqualTo(activeRules);
     assertThat(underTest.fileSystem()).isEqualTo(fs);
     assertThat(underTest.getSonarQubeVersion()).isEqualTo(Version.parse("5.5"));
@@ -93,7 +93,7 @@ public class ModuleSensorContextTest {
   }
 
   @Test
-  public void should_delegate_to_unchanged_files_handler() {
+  void should_delegate_to_unchanged_files_handler() {
     DefaultInputFile defaultInputFile = mock(DefaultInputFile.class);
 
     underTest.markAsUnchanged(defaultInputFile);
@@ -102,7 +102,7 @@ public class ModuleSensorContextTest {
   }
 
   @Test
-  public void pull_request_can_skip_unchanged_files() {
+  void pull_request_can_skip_unchanged_files() {
     when(branchConfiguration.isPullRequest()).thenReturn(true);
     underTest = new ModuleSensorContext(mock(DefaultInputProject.class), mock(InputModule.class), settings.asConfig(), settings, fs, activeRules, sensorStorage, runtime,
       branchConfiguration, writeCache, readCache, analysisCacheEnabled, unchangedFilesHandler, executingSensorContext, pluginRepository);
index f0c1546a5d7b33b9d896b987535721d92858da8b..43ea08b6c59b9d3db9fcf6a69dcc8101b0eb4062 100644 (file)
@@ -17,7 +17,16 @@ dependencies {
 
   testImplementation 'com.google.guava:guava'
   testImplementation 'junit:junit'
+  testImplementation 'org.junit.jupiter:junit-jupiter-api'
+  testImplementation 'org.junit.jupiter:junit-jupiter-params'
   testImplementation 'org.assertj:assertj-core'
+
+  testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine'
+  testRuntimeOnly 'org.junit.vintage:junit-vintage-engine'
+}
+
+test {
+  useJUnitPlatform()
 }
 
 //create a single Jar with all dependencies
index 9c7ada548b35580ae1fd3366327463a9218acad4..e59d2e00798c6d2c8b58fea48098100443252aad 100644 (file)
@@ -22,45 +22,40 @@ package org.sonar.scanner.protocol.output;
 import java.io.File;
 import java.nio.charset.Charset;
 import org.apache.commons.io.FileUtils;
-import org.junit.Rule;
-import org.junit.Test;
-import org.junit.rules.TemporaryFolder;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.io.TempDir;
 
 import static org.assertj.core.api.Assertions.assertThat;
-import static org.junit.Assert.fail;
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
 
-public class FileStructureTest {
+class FileStructureTest {
 
-  @Rule
-  public TemporaryFolder temp = new TemporaryFolder();
+  @TempDir
+  public File temp;
 
   @Test
-  public void fail_if_dir_does_not_exist() throws Exception {
-    File dir = temp.newFolder();
+  void fail_if_dir_does_not_exist() {
+    File dir = temp;
     FileUtils.deleteQuietly(dir);
-    try {
-      new FileStructure(dir);
-      fail();
-    } catch (IllegalArgumentException e) {
-      assertThat(e).hasMessageContaining("Directory of analysis report does not exist");
-    }
+
+    assertThatThrownBy(() -> new FileStructure(dir))
+      .isInstanceOf(IllegalArgumentException.class)
+      .hasMessageContaining("Directory of analysis report does not exist");
   }
 
   @Test
-  public void fail_if_invalid_dir() throws Exception {
+  void fail_if_invalid_dir() {
     // not a dir but a file
-    File dir = temp.newFile();
-    try {
-      new FileStructure(dir);
-      fail();
-    } catch (IllegalArgumentException e) {
-      assertThat(e).hasMessageContaining("Directory of analysis report does not exist");
-    }
+    File dir = new File(temp, "newFile");
+
+    assertThatThrownBy(() -> new FileStructure(dir))
+      .isInstanceOf(IllegalArgumentException.class)
+      .hasMessageContaining("Directory of analysis report does not exist");
   }
 
   @Test
-  public void locate_files() throws Exception {
-    File dir = temp.newFolder();
+  void locate_files() throws Exception {
+    File dir = temp;
     FileUtils.write(new File(dir, "metadata.pb"), "metadata content", Charset.defaultCharset());
     FileUtils.write(new File(dir, "issues-3.pb"), "external issues of component 3", Charset.defaultCharset());
     FileUtils.write(new File(dir, "external-issues-3.pb"), "issues of component 3", Charset.defaultCharset());
@@ -76,8 +71,8 @@ public class FileStructureTest {
   }
 
   @Test
-  public void contextProperties_file() throws Exception {
-    File dir = temp.newFolder();
+  void contextProperties_file() throws Exception {
+    File dir = temp;
     File file = new File(dir, "context-props.pb");
     FileUtils.write(file, "content", Charset.defaultCharset());
 
@@ -86,8 +81,8 @@ public class FileStructureTest {
   }
 
   @Test
-  public void telemetryFile_hasTheCorrectName() throws Exception {
-    File dir = temp.newFolder();
+  void telemetryFile_hasTheCorrectName() throws Exception {
+    File dir = temp;
     File file = new File(dir, "telemetry-entries.pb");
     FileUtils.write(file, "content", Charset.defaultCharset());
 
index 4b89c9a3db57a8b03535dda172cddc4d2eb27784..1dd80d25714ce95fba8cbb69147252334695dd2d 100644 (file)
@@ -23,10 +23,9 @@ import com.google.common.collect.Iterators;
 import java.io.File;
 import java.time.Instant;
 import java.util.List;
-import org.junit.Before;
-import org.junit.Rule;
-import org.junit.Test;
-import org.junit.rules.TemporaryFolder;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.io.TempDir;
 import org.sonar.core.util.CloseableIterator;
 import org.sonar.core.util.Protobuf;
 import org.sonar.scanner.protocol.Constants;
@@ -36,19 +35,19 @@ import org.sonar.scanner.protocol.output.ScannerReport.SyntaxHighlightingRule.Hi
 
 import static org.assertj.core.api.Assertions.assertThat;
 
-public class ScannerReportWriterTest {
+class ScannerReportWriterTest {
 
-  @Rule
-  public TemporaryFolder temp = new TemporaryFolder();
+  @TempDir
+  public File temp;
   private ScannerReportWriter underTest;
 
-  @Before
-  public void setUp() throws Exception {
-    underTest = new ScannerReportWriter(new FileStructure(temp.newFolder()));
+  @BeforeEach
+  void setUp() {
+    underTest = new ScannerReportWriter(new FileStructure(temp));
   }
 
   @Test
-  public void write_metadata() {
+  void write_metadata() {
     ScannerReport.Metadata.Builder metadata = ScannerReport.Metadata.newBuilder()
       .setAnalysisDate(15000000L)
       .setProjectKey("PROJECT_A")
@@ -62,7 +61,7 @@ public class ScannerReportWriterTest {
   }
 
   @Test
-  public void write_component() {
+  void write_component() {
     // no data yet
     assertThat(underTest.hasComponentData(FileStructure.Domain.COMPONENT, 1)).isFalse();
 
@@ -88,7 +87,7 @@ public class ScannerReportWriterTest {
   }
 
   @Test
-  public void write_issues() {
+  void write_issues() {
     // no data yet
     assertThat(underTest.hasComponentData(FileStructure.Domain.ISSUES, 1)).isFalse();
 
@@ -108,7 +107,7 @@ public class ScannerReportWriterTest {
   }
 
   @Test
-  public void write_external_issues() {
+  void write_external_issues() {
     // no data yet
     assertThat(underTest.hasComponentData(FileStructure.Domain.EXTERNAL_ISSUES, 1)).isFalse();
 
@@ -128,7 +127,7 @@ public class ScannerReportWriterTest {
   }
 
   @Test
-  public void write_adhoc_rule() {
+  void write_adhoc_rule() {
 
     // write data
     ScannerReport.AdHocRule rule = ScannerReport.AdHocRule.newBuilder()
@@ -149,7 +148,7 @@ public class ScannerReportWriterTest {
   }
 
   @Test
-  public void write_cve() {
+  void write_cve() {
 
     // write data
     ScannerReport.Cve cve = ScannerReport.Cve.newBuilder()
@@ -173,7 +172,7 @@ public class ScannerReportWriterTest {
   }
 
   @Test
-  public void write_changed_lines() {
+  void write_changed_lines() {
     assertThat(underTest.hasComponentData(FileStructure.Domain.CHANGED_LINES, 1)).isFalse();
 
     ScannerReport.ChangedLines changedLines = ScannerReport.ChangedLines.newBuilder()
@@ -190,7 +189,7 @@ public class ScannerReportWriterTest {
   }
 
   @Test
-  public void write_measures() {
+  void write_measures() {
     assertThat(underTest.hasComponentData(FileStructure.Domain.MEASURES, 1)).isFalse();
 
     ScannerReport.Measure measure = ScannerReport.Measure.newBuilder()
@@ -208,7 +207,7 @@ public class ScannerReportWriterTest {
   }
 
   @Test
-  public void write_scm() {
+  void write_scm() {
     assertThat(underTest.hasComponentData(FileStructure.Domain.CHANGESETS, 1)).isFalse();
 
     ScannerReport.Changesets scm = ScannerReport.Changesets.newBuilder()
@@ -233,7 +232,7 @@ public class ScannerReportWriterTest {
   }
 
   @Test
-  public void write_duplications() {
+  void write_duplications() {
     assertThat(underTest.hasComponentData(FileStructure.Domain.DUPLICATIONS, 1)).isFalse();
 
     ScannerReport.Duplication duplication = ScannerReport.Duplication.newBuilder()
@@ -262,7 +261,7 @@ public class ScannerReportWriterTest {
   }
 
   @Test
-  public void write_duplication_blocks() {
+  void write_duplication_blocks() {
     assertThat(underTest.hasComponentData(FileStructure.Domain.CPD_TEXT_BLOCKS, 1)).isFalse();
 
     ScannerReport.CpdTextBlock duplicationBlock = ScannerReport.CpdTextBlock.newBuilder()
@@ -288,7 +287,7 @@ public class ScannerReportWriterTest {
   }
 
   @Test
-  public void write_symbols() {
+  void write_symbols() {
     // no data yet
     assertThat(underTest.hasComponentData(FileStructure.Domain.SYMBOLS, 1)).isFalse();
 
@@ -320,7 +319,7 @@ public class ScannerReportWriterTest {
   }
 
   @Test
-  public void write_syntax_highlighting() {
+  void write_syntax_highlighting() {
     // no data yet
     assertThat(underTest.hasComponentData(FileStructure.Domain.SYNTAX_HIGHLIGHTINGS, 1)).isFalse();
 
@@ -337,7 +336,7 @@ public class ScannerReportWriterTest {
   }
 
   @Test
-  public void write_line_significant_code() {
+  void write_line_significant_code() {
     // no data yet
     assertThat(underTest.hasComponentData(FileStructure.Domain.SGNIFICANT_CODE, 1)).isFalse();
 
@@ -352,7 +351,7 @@ public class ScannerReportWriterTest {
   }
 
   @Test
-  public void write_coverage() {
+  void write_coverage() {
     // no data yet
     assertThat(underTest.hasComponentData(FileStructure.Domain.COVERAGES, 1)).isFalse();
 
@@ -368,7 +367,7 @@ public class ScannerReportWriterTest {
   }
 
   @Test
-  public void write_telemetry() {
+  void write_telemetry() {
 
     List<ScannerReport.TelemetryEntry> input = List.of(
       ScannerReport.TelemetryEntry.newBuilder()