1
1
/*
2
- * Copyright 2012-2023 the original author or authors.
2
+ * Copyright 2012-2024 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
@@ -457,6 +457,36 @@ void buildWhenDerivedFromCustomTypeWithTypeChange() {
457
457
assertThat (testSource .getPassword ()).isEqualTo ("secret" );
458
458
}
459
459
460
+ @ Test
461
+ void buildWhenDerivedFromCustomTypeDeriveDriverClassNameFromUrl () {
462
+ NoDriverClassNameDataSource dataSource = new NoDriverClassNameDataSource ();
463
+ dataSource .setUsername ("test" );
464
+ dataSource .setPassword ("secret" );
465
+ dataSource .setUrl ("jdbc:postgresql://localhost:5432/postgres" );
466
+ DataSourceBuilder <?> builder = DataSourceBuilder .derivedFrom (dataSource ).type (SimpleDriverDataSource .class );
467
+ SimpleDriverDataSource testSource = (SimpleDriverDataSource ) builder .build ();
468
+ assertThat (testSource .getUsername ()).isEqualTo ("test" );
469
+ assertThat (testSource .getUrl ()).isEqualTo ("jdbc:postgresql://localhost:5432/postgres" );
470
+ assertThat (testSource .getPassword ()).isEqualTo ("secret" );
471
+ assertThat (testSource .getDriver ()).isInstanceOf (org .postgresql .Driver .class );
472
+ }
473
+
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
+
460
490
@ Test // gh-31920
461
491
void buildWhenC3P0TypeSpecifiedReturnsExpectedDataSource () {
462
492
this .dataSource = DataSourceBuilder .create ()
@@ -620,12 +650,10 @@ void setPassword(String password) {
620
650
621
651
}
622
652
623
- static class CustomDataSource extends LimitedCustomDataSource {
653
+ static class NoDriverClassNameDataSource extends LimitedCustomDataSource {
624
654
625
655
private String url ;
626
656
627
- private String driverClassName ;
628
-
629
657
String getUrl () {
630
658
return this .url ;
631
659
}
@@ -634,6 +662,14 @@ void setUrl(String url) {
634
662
this .url = url ;
635
663
}
636
664
665
+ }
666
+
667
+ static class CustomDataSource extends LimitedCustomDataSource {
668
+
669
+ private String driverClassName ;
670
+
671
+ private String url ;
672
+
637
673
String getDriverClassName () {
638
674
return this .driverClassName ;
639
675
}
@@ -642,6 +678,14 @@ void setDriverClassName(String driverClassName) {
642
678
this .driverClassName = driverClassName ;
643
679
}
644
680
681
+ String getUrl () {
682
+ return this .url ;
683
+ }
684
+
685
+ void setUrl (String url ) {
686
+ this .url = url ;
687
+ }
688
+
645
689
}
646
690
647
691
}
0 commit comments