Skip to content

Commit a12bfd9

Browse files
committed
Polish "Derive driverClassName from URL when otherwise unknown"
See gh-39376
1 parent 4611230 commit a12bfd9

File tree

1 file changed

+30
-5
lines changed

1 file changed

+30
-5
lines changed

spring-boot-project/spring-boot/src/test/java/org/springframework/boot/jdbc/DataSourceBuilderTests.java

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2023 the original author or authors.
2+
* Copyright 2012-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -458,8 +458,8 @@ void buildWhenDerivedFromCustomTypeWithTypeChange() {
458458
}
459459

460460
@Test
461-
void buildWhenDerivedFromCustomTypeDeriveDriverClassNameFromDerivedUrl() {
462-
UrlCapableLimitedCustomDataSource dataSource = new UrlCapableLimitedCustomDataSource();
461+
void buildWhenDerivedFromCustomTypeDeriveDriverClassNameFromUrl() {
462+
NoDriverClassNameDataSource dataSource = new NoDriverClassNameDataSource();
463463
dataSource.setUsername("test");
464464
dataSource.setPassword("secret");
465465
dataSource.setUrl("jdbc:postgresql://localhost:5432/postgres");
@@ -471,6 +471,22 @@ void buildWhenDerivedFromCustomTypeDeriveDriverClassNameFromDerivedUrl() {
471471
assertThat(testSource.getDriver()).isInstanceOf(org.postgresql.Driver.class);
472472
}
473473

474+
@Test
475+
void buildWhenDerivedFromCustomTypeDeriveDriverClassNameFromOverridenUrl() {
476+
NoDriverClassNameDataSource dataSource = new NoDriverClassNameDataSource();
477+
dataSource.setUsername("test");
478+
dataSource.setPassword("secret");
479+
dataSource.setUrl("jdbc:mysql://localhost:5432/mysql");
480+
DataSourceBuilder<?> builder = DataSourceBuilder.derivedFrom(dataSource)
481+
.type(SimpleDriverDataSource.class)
482+
.url("jdbc:mariadb://localhost:5432/mariadb");
483+
SimpleDriverDataSource testSource = (SimpleDriverDataSource) builder.build();
484+
assertThat(testSource.getUsername()).isEqualTo("test");
485+
assertThat(testSource.getUrl()).isEqualTo("jdbc:mariadb://localhost:5432/mariadb");
486+
assertThat(testSource.getPassword()).isEqualTo("secret");
487+
assertThat(testSource.getDriver()).isInstanceOf(org.mariadb.jdbc.Driver.class);
488+
}
489+
474490
@Test // gh-31920
475491
void buildWhenC3P0TypeSpecifiedReturnsExpectedDataSource() {
476492
this.dataSource = DataSourceBuilder.create()
@@ -634,7 +650,7 @@ void setPassword(String password) {
634650

635651
}
636652

637-
static class UrlCapableLimitedCustomDataSource extends LimitedCustomDataSource {
653+
static class NoDriverClassNameDataSource extends LimitedCustomDataSource {
638654

639655
private String url;
640656

@@ -648,10 +664,11 @@ void setUrl(String url) {
648664

649665
}
650666

651-
static class CustomDataSource extends UrlCapableLimitedCustomDataSource {
667+
static class CustomDataSource extends LimitedCustomDataSource {
652668

653669
private String driverClassName;
654670

671+
private String url;
655672

656673
String getDriverClassName() {
657674
return this.driverClassName;
@@ -661,6 +678,14 @@ void setDriverClassName(String driverClassName) {
661678
this.driverClassName = driverClassName;
662679
}
663680

681+
String getUrl() {
682+
return this.url;
683+
}
684+
685+
void setUrl(String url) {
686+
this.url = url;
687+
}
688+
664689
}
665690

666691
}

0 commit comments

Comments
 (0)