Skip to content

Commit

Permalink
Take R2DBC MySQL driver into account as of Spring Boot 3.1.x
Browse files Browse the repository at this point in the history
  • Loading branch information
wonwoo authored and snicoll committed Oct 23, 2023
1 parent 909191a commit 90614db
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@
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 List<String> DRIVERS = Arrays.asList("h2", "mysql", "mariadb", "postgresql", "sqlserver",
"oracle");

private static final VersionRange SPRING_BOOT_3_1_0_OR_LATER = VersionParser.DEFAULT.parseRange("3.1.0");

private final boolean mysqlR2dbcIsAsyncerDependency;

private final Build build;

public R2dbcHelpDocumentCustomizer(Build build) {
public R2dbcHelpDocumentCustomizer(Build build, Version platformVersion) {
this.build = build;
this.mysqlR2dbcIsAsyncerDependency = SPRING_BOOT_3_1_0_OR_LATER.match(platformVersion);
}

@Override
public void customize(HelpDocument document) {
if (this.build.dependencies().ids().noneMatch(DRIVERS::contains)) {
if (this.build.dependencies().ids().noneMatch(DRIVERS::contains) || !this.mysqlR2dbcIsAsyncerDependency) {
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 All @@ -35,53 +36,65 @@ class R2dbcHelpDocumentCustomizerTests {

@Test
void r2dbcWithNoMatchingDriver() {
HelpDocument helpDocument = createHelpDocument();
HelpDocument helpDocument = createHelpDocument(Version.parse("3.1.0"));
assertThat(helpDocument.getSections()).hasSize(1);
}

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

@Test
void r2dbcWithMariadb() {
HelpDocument helpDocument = createHelpDocument("mariadb");
HelpDocument helpDocument = createHelpDocument(Version.parse("3.1.0"), "mariadb");
assertThat(helpDocument.getSections()).isEmpty();
}

@Test
void r2dbcWithPostgresql() {
HelpDocument helpDocument = createHelpDocument("postgresql");
HelpDocument helpDocument = createHelpDocument(Version.parse("3.1.0"), "postgresql");
assertThat(helpDocument.getSections()).isEmpty();
}

@Test
void r2dbcWithSqlServer() {
HelpDocument helpDocument = createHelpDocument("sqlserver");
HelpDocument helpDocument = createHelpDocument(Version.parse("3.1.0"), "sqlserver");
assertThat(helpDocument.getSections()).isEmpty();
}

@Test
void r2dbcWithOracle() {
HelpDocument helpDocument = createHelpDocument("oracle");
HelpDocument helpDocument = createHelpDocument(Version.parse("3.1.0"), "oracle");
assertThat(helpDocument.getSections()).isEmpty();
}

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

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

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

private HelpDocument createHelpDocument(String... dependencyIds) {
private HelpDocument createHelpDocument(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 90614db

Please sign in to comment.