Skip to content

Commit

Permalink
improve coverage 2
Browse files Browse the repository at this point in the history
  • Loading branch information
alban-auzeill committed May 30, 2024
1 parent 240295d commit c77a72f
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 5 deletions.
11 changes: 6 additions & 5 deletions src/main/java/org/sonarsource/scanner/maven/SonarQubeMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
*/
package org.sonarsource.scanner.maven;

import com.google.common.annotations.VisibleForTesting;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -137,14 +138,14 @@ private void warnAboutNonFixedSonarPluginVersion() {
}
}

private static boolean hasPluginVersionDefinedInTheProject(MavenProject project, String groupId, String artifactId) {
@VisibleForTesting
static boolean hasPluginVersionDefinedInTheProject(MavenProject project, String groupId, String artifactId) {
Stream<Plugin> pluginStream = project.getBuildPlugins().stream();
PluginManagement pluginManagement = project.getPluginManagement();
pluginStream = pluginManagement != null ? Stream.concat(pluginStream, pluginManagement.getPlugins().stream()) : pluginStream;
return pluginStream.anyMatch(plugin ->
(plugin.getGroupId() == null || groupId.equals(plugin.getGroupId())) &&
artifactId.equals(plugin.getArtifactId()) &&
plugin.getVersion() != null);
return pluginStream.anyMatch(plugin -> (plugin.getGroupId() == null || groupId.equals(plugin.getGroupId())) &&
artifactId.equals(plugin.getArtifactId()) &&
(plugin.getVersion() != null && !plugin.getVersion().isBlank()));
}

private static boolean hasASonarGoalMissingVersion(List<String> goals, String groupId, String artifactId) {
Expand Down
34 changes: 34 additions & 0 deletions src/test/java/org/sonarsource/scanner/maven/SonarQubeMojoTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,40 @@ public void sonar_maven_plugin_version_set_in_build_plugins_should_not_display_a
assertThat(logger.warnings()).isEmpty();
}

@Test
public void cover_corner_cases_for_hasPluginVersionDefinedInTheProject() throws Exception {
MavenProject project = new MavenProject();
assertThat(SonarQubeMojo.hasPluginVersionDefinedInTheProject(project, "org.sonarsource.scanner.maven", "sonar-maven-plugin")).isFalse();

Plugin pluginDefinition = new Plugin();
project.getBuild().getPlugins().add(pluginDefinition);

pluginDefinition.setGroupId("org.sonarsource.scanner.maven");
pluginDefinition.setArtifactId("sonar-maven-plugin");
pluginDefinition.setVersion(null);
assertThat(SonarQubeMojo.hasPluginVersionDefinedInTheProject(project, "org.sonarsource.scanner.maven", "sonar-maven-plugin")).isFalse();

pluginDefinition.setGroupId("org.sonarsource.scanner.maven");
pluginDefinition.setArtifactId("sonar-maven-plugin");
pluginDefinition.setVersion(" ");
assertThat(SonarQubeMojo.hasPluginVersionDefinedInTheProject(project, "org.sonarsource.scanner.maven", "sonar-maven-plugin")).isFalse();

pluginDefinition.setGroupId("org.sonarsource.scanner.maven");
pluginDefinition.setArtifactId("sonar-maven-plugin");
pluginDefinition.setVersion("1.2.3.4");
assertThat(SonarQubeMojo.hasPluginVersionDefinedInTheProject(project, "org.sonarsource.scanner.maven", "sonar-maven-plugin")).isTrue();

pluginDefinition.setGroupId("org.other");
pluginDefinition.setArtifactId("sonar-maven-plugin");
pluginDefinition.setVersion("1.2.3.4");
assertThat(SonarQubeMojo.hasPluginVersionDefinedInTheProject(project, "org.sonarsource.scanner.maven", "sonar-maven-plugin")).isFalse();

pluginDefinition.setGroupId("org.sonarsource.scanner.maven");
pluginDefinition.setArtifactId("other-plugin");
pluginDefinition.setVersion("1.2.3.4");
assertThat(SonarQubeMojo.hasPluginVersionDefinedInTheProject(project, "org.sonarsource.scanner.maven", "sonar-maven-plugin")).isFalse();
}

@Test
public void verbose() throws Exception {
logger.setLogLevel(TestLog.LogLevel.DEBUG);
Expand Down

0 comments on commit c77a72f

Please sign in to comment.