22
33import static org .assertj .core .api .Assertions .assertThat ;
44import static org .mockito .Mockito .mock ;
5+ import static org .mockito .Mockito .when ;
56
67import dev .dbos .transact .DBOS ;
78import dev .dbos .transact .config .DBOSConfig ;
89
10+ import java .sql .Connection ;
11+ import java .sql .DatabaseMetaData ;
12+ import java .sql .SQLException ;
13+
914import javax .sql .DataSource ;
1015
1116import org .junit .jupiter .api .Test ;
@@ -208,14 +213,10 @@ void customizerThatThrowsFails() {
208213
209214 @ Test
210215 void dataSourceBeanIsUsedWhenNoDatasourceUrlConfigured () {
211- // When a DataSource bean is present and no dbos.datasource.url is set,
212- // the DBOS instance should be created using that DataSource (no databaseUrl required).
213- // We provide a mock DBOSLifecycle to prevent dbos.launch() from running.
214- var mockDs = mock (DataSource .class );
215216 new ApplicationContextRunner ()
216217 .withConfiguration (AutoConfigurations .of (DBOSAutoConfiguration .class ))
217218 .withPropertyValues ("dbos.application.name=test-app" )
218- .withBean (DataSource .class , () -> mockDs )
219+ .withBean (DataSource .class , () -> mockPostgresDataSource () )
219220 .withBean (
220221 DBOSAutoConfiguration .DBOSLifecycle .class ,
221222 () -> mock (DBOSAutoConfiguration .DBOSLifecycle .class ))
@@ -225,4 +226,53 @@ void dataSourceBeanIsUsedWhenNoDatasourceUrlConfigured() {
225226 assertThat (context ).hasSingleBean (DBOS .class );
226227 });
227228 }
229+
230+ @ Test
231+ void nonPostgresSpringDataSourceFails () {
232+ var mockDs = mockDataSource ("MySQL" );
233+ new ApplicationContextRunner ()
234+ .withConfiguration (AutoConfigurations .of (DBOSAutoConfiguration .class ))
235+ .withPropertyValues ("dbos.application.name=test-app" )
236+ .withBean (DataSource .class , () -> mockDs )
237+ .withBean (
238+ DBOSAutoConfiguration .DBOSLifecycle .class ,
239+ () -> mock (DBOSAutoConfiguration .DBOSLifecycle .class ))
240+ .run (
241+ context -> {
242+ assertThat (context ).hasFailed ();
243+ assertThat (context .getStartupFailure ())
244+ .hasMessageContaining ("PostgreSQL" )
245+ .hasMessageContaining ("MySQL" );
246+ });
247+ }
248+
249+ @ Test
250+ void postgresSpringDataSourceSucceeds () {
251+ new ApplicationContextRunner ()
252+ .withConfiguration (AutoConfigurations .of (DBOSAutoConfiguration .class ))
253+ .withPropertyValues ("dbos.application.name=test-app" )
254+ .withBean (DataSource .class , () -> mockPostgresDataSource ())
255+ .withBean (
256+ DBOSAutoConfiguration .DBOSLifecycle .class ,
257+ () -> mock (DBOSAutoConfiguration .DBOSLifecycle .class ))
258+ .run (context -> assertThat (context ).hasNotFailed ());
259+ }
260+
261+ private static DataSource mockPostgresDataSource () {
262+ return mockDataSource ("PostgreSQL" );
263+ }
264+
265+ private static DataSource mockDataSource (String productName ) {
266+ try {
267+ var meta = mock (DatabaseMetaData .class );
268+ when (meta .getDatabaseProductName ()).thenReturn (productName );
269+ var conn = mock (Connection .class );
270+ when (conn .getMetaData ()).thenReturn (meta );
271+ var ds = mock (DataSource .class );
272+ when (ds .getConnection ()).thenReturn (conn );
273+ return ds ;
274+ } catch (SQLException e ) {
275+ throw new RuntimeException (e );
276+ }
277+ }
228278}
0 commit comments