Skip to content

Commit

Permalink
Merge pull request #1314 from wonwoo
Browse files Browse the repository at this point in the history
* pr/1314:
  Polish "Take R2DBC MySQL driver into account as of Spring Boot 3.1.x"
  Take R2DBC MySQL driver into account as of Spring Boot 3.1.x

Closes gh-1314
  • Loading branch information
snicoll committed Oct 23, 2023
2 parents 909191a + 4dfcba9 commit 11e3ed3
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,15 @@

package io.spring.start.site.extension.dependency.springdata;

import java.util.Arrays;
import java.util.ArrayList;
import java.util.List;

import io.spring.initializr.generator.buildsystem.Build;
import io.spring.initializr.generator.spring.documentation.HelpDocument;
import io.spring.initializr.generator.spring.documentation.HelpDocumentCustomizer;
import io.spring.initializr.generator.version.Version;
import io.spring.initializr.generator.version.VersionParser;
import io.spring.initializr.generator.version.VersionRange;

/**
* A {@link HelpDocumentCustomizer} that adds a section when R2DBC is selected but no
Expand All @@ -31,17 +34,23 @@
*/
public class R2dbcHelpDocumentCustomizer implements HelpDocumentCustomizer {

private static final List<String> DRIVERS = Arrays.asList("h2", "mariadb", "postgresql", "sqlserver", "oracle");
private static final VersionRange SPRING_BOOT_3_1_0_OR_LATER = VersionParser.DEFAULT.parseRange("3.1.0");

private final Build build;

public R2dbcHelpDocumentCustomizer(Build build) {
private final List<String> drivers;

public R2dbcHelpDocumentCustomizer(Build build, Version platformVersion) {
this.build = build;
this.drivers = new ArrayList<>(List.of("h2", "mariadb", "postgresql", "sqlserver", "oracle"));
if (SPRING_BOOT_3_1_0_OR_LATER.match(platformVersion)) {
this.drivers.add("mysql");
}
}

@Override
public void customize(HelpDocument document) {
if (this.build.dependencies().ids().noneMatch(DRIVERS::contains)) {
if (this.build.dependencies().ids().noneMatch(this.drivers::contains)) {
document.addSection((writer) -> {
writer.println("## Missing R2DBC Driver");
writer.println();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ public R2dbcBuildCustomizer r2dbcBuildCustomizer(ProjectDescription description)

@Bean
@ConditionalOnRequestedDependency("data-r2dbc")
public R2dbcHelpDocumentCustomizer r2dbcHelpDocumentCustomizer(Build build) {
return new R2dbcHelpDocumentCustomizer(build);
public R2dbcHelpDocumentCustomizer r2dbcHelpDocumentCustomizer(Build build, ProjectDescription description) {
return new R2dbcHelpDocumentCustomizer(build, description.getPlatformVersion());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import io.spring.initializr.generator.buildsystem.gradle.GradleBuild;
import io.spring.initializr.generator.io.template.MustacheTemplateRenderer;
import io.spring.initializr.generator.spring.documentation.HelpDocument;
import io.spring.initializr.generator.version.Version;
import org.junit.jupiter.api.Test;

import static org.assertj.core.api.Assertions.assertThat;
Expand Down Expand Up @@ -69,19 +70,35 @@ void r2dbcWithOracle() {
assertThat(helpDocument.getSections()).isEmpty();
}

@Test
void r2dbcWithMysql() {
HelpDocument helpDocument = createHelpDocument("mysql");
assertThat(helpDocument.getSections()).isEmpty();
}

@Test
void r2dbcWithMysqlBeforeVersion() {
HelpDocument helpDocument = createHelpDocumentForVersion(Version.parse("2.7.0.M1"), "mysql");
assertThat(helpDocument.getSections()).hasSize(1);
}

@Test
void r2dbcWithSeveralDrivers() {
HelpDocument helpDocument = createHelpDocument("mysql", "h2");
HelpDocument helpDocument = createHelpDocumentForVersion(Version.parse("3.1.0"), "mysql", "h2");
assertThat(helpDocument.getSections()).isEmpty();
}

private HelpDocument createHelpDocument(String... dependencyIds) {
return createHelpDocumentForVersion(Version.parse("3.1.0"), dependencyIds);
}

private HelpDocument createHelpDocumentForVersion(Version platformVersion, String... dependencyIds) {
Build build = new GradleBuild();
for (String dependencyId : dependencyIds) {
build.dependencies().add(dependencyId, Dependency.withCoordinates(dependencyId, dependencyId));
}
HelpDocument document = new HelpDocument(mock(MustacheTemplateRenderer.class));
new R2dbcHelpDocumentCustomizer(build).customize(document);
new R2dbcHelpDocumentCustomizer(build, platformVersion).customize(document);
return document;
}

Expand Down

0 comments on commit 11e3ed3

Please sign in to comment.