Skip to content

Commit

Permalink
Remove code duplication in unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ADarko22 committed May 14, 2024
1 parent 741a573 commit be677c7
Showing 1 changed file with 52 additions and 54 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.function.Consumer;

import org.apache.maven.execution.MavenSession;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.logging.Log;
Expand Down Expand Up @@ -111,11 +113,11 @@ void testSQBefore56() {
when(scanner.serverVersion()).thenReturn("5.1");

MojoExecutionException exception = assertThrows(MojoExecutionException.class,
() -> scannerBootstrapper.execute());
() -> scannerBootstrapper.execute());

assertThat(exception)
.hasCauseExactlyInstanceOf(UnsupportedOperationException.class)
.hasMessage(UNSUPPORTED_BELOW_SONARQUBE_56_MESSAGE);
.hasCauseExactlyInstanceOf(UnsupportedOperationException.class)
.hasMessage(UNSUPPORTED_BELOW_SONARQUBE_56_MESSAGE);
}

@Test
Expand Down Expand Up @@ -145,78 +147,68 @@ void testNullServerVersion() {
when(scanner.serverVersion()).thenReturn(null);

MojoExecutionException exception = assertThrows(MojoExecutionException.class,
() -> scannerBootstrapper.execute());
() -> scannerBootstrapper.execute());

assertThat(exception)
.hasCauseExactlyInstanceOf(UnsupportedOperationException.class)
.hasMessage(UNSUPPORTED_BELOW_SONARQUBE_56_MESSAGE);
.hasCauseExactlyInstanceOf(UnsupportedOperationException.class)
.hasMessage(UNSUPPORTED_BELOW_SONARQUBE_56_MESSAGE);
}

@Test
void scanAll_property_is_applied_by_default() throws MojoExecutionException {
// When sonar.scanner.scanAll is not set
Map<String, String> collectedProperties = scannerBootstrapper.collectProperties();
assertThat(collectedProperties).containsKey(ScanProperties.PROJECT_SOURCE_DIRS);
String[] sourceDirs = collectedProperties.get(ScanProperties.PROJECT_SOURCE_DIRS).split(",");
assertThat(sourceDirs).hasSize(3);
assertThat(sourceDirs[0]).endsWith(Paths.get("src", "main", "java").toString());
assertThat(sourceDirs[1]).endsWith(Paths.get("pom.xml").toString());
assertThat(sourceDirs[2]).endsWith(Paths.get("src", "main", "resources", "index.js").toString());
verifyCollectedSources(sourceDirs -> {
assertThat(sourceDirs).hasSize(3);
assertThat(sourceDirs[0]).endsWith(Paths.get("src", "main", "java").toString());
assertThat(sourceDirs[1]).endsWith(Paths.get("pom.xml").toString());
assertThat(sourceDirs[2]).endsWith(Paths.get("src", "main", "resources", "index.js").toString());
});

verify(log, times(1)).info("Parameter sonar.maven.scanAll is enabled. The scanner will attempt to collect additional sources.");
verify(scannerBootstrapper, times(1)).collectAllSources(any());
}

@Test
void scanAll_property_is_not_applied_when_set_explicitly() throws MojoExecutionException {
// When sonar.scanner.scanAll is set explicitly to false
Properties withScanAllSetToFalse = new Properties();
withScanAllSetToFalse.put(MavenScannerProperties.PROJECT_SCAN_ALL_SOURCES, "false");
when(session.getUserProperties()).thenReturn(withScanAllSetToFalse);
setSonarScannerScanAllTo("false");

verifyCollectedSources(sourceDirs -> {
assertThat(sourceDirs).hasSize(2);
assertThat(sourceDirs[0]).endsWith(Paths.get("src", "main", "java").toString());
assertThat(sourceDirs[1]).endsWith(Paths.get("pom.xml").toString());
});

Map<String, String> collectedProperties = scannerBootstrapper.collectProperties();
assertThat(collectedProperties).containsKey(ScanProperties.PROJECT_SOURCE_DIRS);
String[] sourceDirs = collectedProperties.get(ScanProperties.PROJECT_SOURCE_DIRS).split(",");
assertThat(sourceDirs).hasSize(2);
assertThat(sourceDirs[0]).endsWith(Paths.get("src", "main", "java").toString());
assertThat(sourceDirs[1]).endsWith(Paths.get("pom.xml").toString());
verify(log, never()).info("Parameter sonar.maven.scanAll is enabled. The scanner will attempt to collect additional sources.");
verify(scannerBootstrapper, never()).collectAllSources(any());
}

@Test
void scanAll_property_is_applied_when_set_explicitly() throws MojoExecutionException {
// When sonar.scanner.scanAll is set explicitly to true
Properties withScanAllSetToTrue = new Properties();
withScanAllSetToTrue.put(MavenScannerProperties.PROJECT_SCAN_ALL_SOURCES, "true");
when(session.getUserProperties()).thenReturn(withScanAllSetToTrue);
setSonarScannerScanAllTo("true");

verifyCollectedSources(sourceDirs -> {
assertThat(sourceDirs).hasSize(3);
assertThat(sourceDirs[0]).endsWith(Paths.get("src", "main", "java").toString());
assertThat(sourceDirs[1]).endsWith(Paths.get("pom.xml").toString());
assertThat(sourceDirs[2]).endsWith(Paths.get("src", "main", "resources", "index.js").toString());
});

Map<String, String> collectedProperties = scannerBootstrapper.collectProperties();
assertThat(collectedProperties).containsKey(ScanProperties.PROJECT_SOURCE_DIRS);
String[] sourceDirs = collectedProperties.get(ScanProperties.PROJECT_SOURCE_DIRS).split(",");
assertThat(sourceDirs).hasSize(3);
assertThat(sourceDirs[0]).endsWith(Paths.get("src", "main", "java").toString());
assertThat(sourceDirs[1]).endsWith(Paths.get("pom.xml").toString());
assertThat(sourceDirs[2]).endsWith(Paths.get("src", "main", "resources", "index.js").toString());
verify(log, times(1)).info("Parameter sonar.maven.scanAll is enabled. The scanner will attempt to collect additional sources.");
verify(scannerBootstrapper, times(1)).collectAllSources(any());
}

@Test
void should_not_collect_all_sources_when_sonar_sources_is_overridden() throws MojoExecutionException {
// When sonar.scanner.scanAll is set explicitly to true
Properties withScanAllSetToTrue = new Properties();
withScanAllSetToTrue.put(MavenScannerProperties.PROJECT_SCAN_ALL_SOURCES, "true");
when(session.getUserProperties()).thenReturn(withScanAllSetToTrue);
setSonarScannerScanAllTo("true");

// Return the expected directory and notify of overriding
projectProperties.put(ScanProperties.PROJECT_SOURCE_DIRS, Paths.get("src", "main", "resources").toFile().toString());
when(mavenProjectConverter.isSourceDirsOverridden()).thenReturn(true);

Map<String, String> collectedProperties = scannerBootstrapper.collectProperties();
assertThat(collectedProperties).containsKey(ScanProperties.PROJECT_SOURCE_DIRS);
String[] sourceDirs = collectedProperties.get(ScanProperties.PROJECT_SOURCE_DIRS).split(",");
assertThat(sourceDirs).hasSize(1);
assertThat(sourceDirs[0]).endsWith(Paths.get("src", "main", "resources").toString());
verifyCollectedSources(sourceDirs -> {
assertThat(sourceDirs).hasSize(1);
assertThat(sourceDirs[0]).endsWith(Paths.get("src", "main", "resources").toString());
});

verify(log, times(1)).info("Parameter sonar.maven.scanAll is enabled. The scanner will attempt to collect additional sources.");
verify(log, times(1)).warn("Parameter sonar.maven.scanAll is enabled but the scanner will not collect additional sources because sonar.sources has been overridden.");
Expand All @@ -225,10 +217,8 @@ void should_not_collect_all_sources_when_sonar_sources_is_overridden() throws Mo

@Test
void should_not_collect_all_sources_when_sonar_tests_is_overridden() throws MojoExecutionException {
// When sonar.scanner.scanAll is set explicitly to true
Properties withScanAllSetToTrue = new Properties();
withScanAllSetToTrue.put(MavenScannerProperties.PROJECT_SCAN_ALL_SOURCES, "true");
when(session.getUserProperties()).thenReturn(withScanAllSetToTrue);
setSonarScannerScanAllTo("true");

// Return the expected directory and notify of overriding
projectProperties.put(ScanProperties.PROJECT_TEST_DIRS, Paths.get("src", "test", "resources").toFile().toString());
when(mavenProjectConverter.isTestDirsOverridden()).thenReturn(true);
Expand All @@ -246,10 +236,7 @@ void should_not_collect_all_sources_when_sonar_tests_is_overridden() throws Mojo

@Test
void an_exception_is_logged_at_warning_level_when_failing_to_crawl_the_filesystem_to_scan_all_sources() throws MojoExecutionException, IOException {
// Enabling the scanAll option explicitly as a scanner option
Properties withScanAllSetToTrue = new Properties();
withScanAllSetToTrue.put(MavenScannerProperties.PROJECT_SCAN_ALL_SOURCES, "true");
when(session.getUserProperties()).thenReturn(withScanAllSetToTrue);
setSonarScannerScanAllTo("true");

IOException expectedException = new IOException("This is what we expected");
try (MockedStatic<Files> mockedFiles = Mockito.mockStatic(Files.class)) {
Expand All @@ -261,9 +248,7 @@ void an_exception_is_logged_at_warning_level_when_failing_to_crawl_the_filesyste

@Test
void can_collect_sources_with_commas_in_paths() throws MojoExecutionException, IOException {
Properties withScanAllSetToTrue = new Properties();
withScanAllSetToTrue.put(MavenScannerProperties.PROJECT_SCAN_ALL_SOURCES, "true");
when(session.getUserProperties()).thenReturn(withScanAllSetToTrue);
setSonarScannerScanAllTo("true");

// Create paths with commas in them
Path root = tmpFolder.toAbsolutePath();
Expand Down Expand Up @@ -343,6 +328,19 @@ void maven_opts_is_not_logged_at_info_level_when_not_absent_from_environment_var
}
}

private void setSonarScannerScanAllTo(String value) {
Properties withScanAllSet = new Properties();
withScanAllSet.put(MavenScannerProperties.PROJECT_SCAN_ALL_SOURCES, value);
when(session.getUserProperties()).thenReturn(withScanAllSet);
}

private void verifyCollectedSources(Consumer<String[]> sourceDirsAssertions) throws MojoExecutionException {
Map<String, String> collectedProperties = scannerBootstrapper.collectProperties();
assertThat(collectedProperties).containsKey(ScanProperties.PROJECT_SOURCE_DIRS);
String[] sourceDirs = collectedProperties.get(ScanProperties.PROJECT_SOURCE_DIRS).split(",");
sourceDirsAssertions.accept(sourceDirs);
}

private void verifyCommonCalls() {
verify(scanner).start();
verify(scanner).serverVersion();
Expand Down

0 comments on commit be677c7

Please sign in to comment.