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");
+ }
}
*/
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;
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();
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"));
}
@Test
- public void should_delegate_to_unchanged_files_handler() {
+ void should_delegate_to_unchanged_files_handler() {
DefaultInputFile defaultInputFile = mock(DefaultInputFile.class);
underTest.markAsUnchanged(defaultInputFile);
}
@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);
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
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());
}
@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());
}
@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());
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;
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")
}
@Test
- public void write_component() {
+ void write_component() {
// no data yet
assertThat(underTest.hasComponentData(FileStructure.Domain.COMPONENT, 1)).isFalse();
}
@Test
- public void write_issues() {
+ void write_issues() {
// no data yet
assertThat(underTest.hasComponentData(FileStructure.Domain.ISSUES, 1)).isFalse();
}
@Test
- public void write_external_issues() {
+ void write_external_issues() {
// no data yet
assertThat(underTest.hasComponentData(FileStructure.Domain.EXTERNAL_ISSUES, 1)).isFalse();
}
@Test
- public void write_adhoc_rule() {
+ void write_adhoc_rule() {
// write data
ScannerReport.AdHocRule rule = ScannerReport.AdHocRule.newBuilder()
}
@Test
- public void write_cve() {
+ void write_cve() {
// write data
ScannerReport.Cve cve = ScannerReport.Cve.newBuilder()
}
@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()
}
@Test
- public void write_measures() {
+ void write_measures() {
assertThat(underTest.hasComponentData(FileStructure.Domain.MEASURES, 1)).isFalse();
ScannerReport.Measure measure = ScannerReport.Measure.newBuilder()
}
@Test
- public void write_scm() {
+ void write_scm() {
assertThat(underTest.hasComponentData(FileStructure.Domain.CHANGESETS, 1)).isFalse();
ScannerReport.Changesets scm = ScannerReport.Changesets.newBuilder()
}
@Test
- public void write_duplications() {
+ void write_duplications() {
assertThat(underTest.hasComponentData(FileStructure.Domain.DUPLICATIONS, 1)).isFalse();
ScannerReport.Duplication duplication = ScannerReport.Duplication.newBuilder()
}
@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()
}
@Test
- public void write_symbols() {
+ void write_symbols() {
// no data yet
assertThat(underTest.hasComponentData(FileStructure.Domain.SYMBOLS, 1)).isFalse();
}
@Test
- public void write_syntax_highlighting() {
+ void write_syntax_highlighting() {
// no data yet
assertThat(underTest.hasComponentData(FileStructure.Domain.SYNTAX_HIGHLIGHTINGS, 1)).isFalse();
}
@Test
- public void write_line_significant_code() {
+ void write_line_significant_code() {
// no data yet
assertThat(underTest.hasComponentData(FileStructure.Domain.SGNIFICANT_CODE, 1)).isFalse();
}
@Test
- public void write_coverage() {
+ void write_coverage() {
// no data yet
assertThat(underTest.hasComponentData(FileStructure.Domain.COVERAGES, 1)).isFalse();
}
@Test
- public void write_telemetry() {
+ void write_telemetry() {
List<ScannerReport.TelemetryEntry> input = List.of(
ScannerReport.TelemetryEntry.newBuilder()