|
1 | 1 | /*
|
2 |
| - * Copyright 2012-2023 the original author or authors. |
| 2 | + * Copyright 2012-2025 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.
|
|
16 | 16 |
|
17 | 17 | package org.springframework.boot.autoconfigure.jdbc;
|
18 | 18 |
|
| 19 | +import java.io.PrintWriter; |
| 20 | +import java.sql.Connection; |
| 21 | +import java.sql.SQLException; |
| 22 | +import java.sql.SQLFeatureNotSupportedException; |
| 23 | +import java.util.logging.Logger; |
| 24 | + |
19 | 25 | import javax.sql.DataSource;
|
20 | 26 |
|
21 | 27 | import com.zaxxer.hikari.HikariDataSource;
|
|
28 | 34 | import org.springframework.boot.jdbc.DataSourceBuilder;
|
29 | 35 | import org.springframework.boot.jdbc.HikariCheckpointRestoreLifecycle;
|
30 | 36 | import org.springframework.boot.test.context.runner.ApplicationContextRunner;
|
| 37 | +import org.springframework.boot.testsupport.classpath.ClassPathExclusions; |
31 | 38 | import org.springframework.boot.testsupport.classpath.ClassPathOverrides;
|
32 | 39 | import org.springframework.context.annotation.Bean;
|
33 | 40 | import org.springframework.context.annotation.Configuration;
|
34 | 41 | import org.springframework.jdbc.datasource.DelegatingDataSource;
|
35 | 42 |
|
36 | 43 | import static org.assertj.core.api.Assertions.assertThat;
|
| 44 | +import static org.assertj.core.api.Assertions.assertThatNoException; |
| 45 | +import static org.mockito.Mockito.mock; |
37 | 46 |
|
38 | 47 | /**
|
39 | 48 | * Tests for {@link DataSourceAutoConfiguration} with Hikari.
|
@@ -80,7 +89,33 @@ void testDataSourceGenericPropertiesOverridden() {
|
80 | 89 | HikariDataSource ds = context.getBean(HikariDataSource.class);
|
81 | 90 | assertThat(ds.getDataSourceProperties().getProperty("dataSourceClassName"))
|
82 | 91 | .isEqualTo("org.h2.JDBCDataSource");
|
| 92 | + }); |
| 93 | + } |
| 94 | + |
| 95 | + @Test |
| 96 | + @SuppressWarnings("resource") |
| 97 | + @ClassPathExclusions({ "h2-*.jar", "hsqldb-*.jar" }) |
| 98 | + void configureDataSourceClassNameWithNoEmbeddedDatabaseAvailable() { |
| 99 | + this.contextRunner |
| 100 | + .withPropertyValues("spring.datasource.url=jdbc:example//", |
| 101 | + "spring.datasource.hikari.data-source-class-name=" + MockDataSource.class.getName()) |
| 102 | + .run((context) -> { |
| 103 | + HikariDataSource ds = context.getBean(HikariDataSource.class); |
| 104 | + assertThat(ds.getDataSourceClassName()).isEqualTo(MockDataSource.class.getName()); |
| 105 | + assertThatNoException().isThrownBy(() -> ds.getConnection().close()); |
| 106 | + }); |
| 107 | + } |
83 | 108 |
|
| 109 | + @Test |
| 110 | + @SuppressWarnings("resource") |
| 111 | + void configureDataSourceClassNameToOverrideUseOfAnEmbeddedDatabase() { |
| 112 | + this.contextRunner |
| 113 | + .withPropertyValues("spring.datasource.url=jdbc:example//", |
| 114 | + "spring.datasource.hikari.data-source-class-name=" + MockDataSource.class.getName()) |
| 115 | + .run((context) -> { |
| 116 | + HikariDataSource ds = context.getBean(HikariDataSource.class); |
| 117 | + assertThat(ds.getDataSourceClassName()).isEqualTo(MockDataSource.class.getName()); |
| 118 | + assertThatNoException().isThrownBy(() -> ds.getConnection().close()); |
84 | 119 | });
|
85 | 120 | }
|
86 | 121 |
|
@@ -201,4 +236,51 @@ DataSource dataSource() {
|
201 | 236 |
|
202 | 237 | }
|
203 | 238 |
|
| 239 | + public static class MockDataSource implements DataSource { |
| 240 | + |
| 241 | + @Override |
| 242 | + public Logger getParentLogger() throws SQLFeatureNotSupportedException { |
| 243 | + return null; |
| 244 | + } |
| 245 | + |
| 246 | + @Override |
| 247 | + public <T> T unwrap(Class<T> iface) throws SQLException { |
| 248 | + return null; |
| 249 | + } |
| 250 | + |
| 251 | + @Override |
| 252 | + public boolean isWrapperFor(Class<?> iface) throws SQLException { |
| 253 | + return false; |
| 254 | + } |
| 255 | + |
| 256 | + @Override |
| 257 | + public Connection getConnection() throws SQLException { |
| 258 | + return mock(Connection.class); |
| 259 | + } |
| 260 | + |
| 261 | + @Override |
| 262 | + public Connection getConnection(String username, String password) throws SQLException { |
| 263 | + return getConnection(); |
| 264 | + } |
| 265 | + |
| 266 | + @Override |
| 267 | + public PrintWriter getLogWriter() throws SQLException { |
| 268 | + return null; |
| 269 | + } |
| 270 | + |
| 271 | + @Override |
| 272 | + public void setLogWriter(PrintWriter out) throws SQLException { |
| 273 | + } |
| 274 | + |
| 275 | + @Override |
| 276 | + public void setLoginTimeout(int seconds) throws SQLException { |
| 277 | + } |
| 278 | + |
| 279 | + @Override |
| 280 | + public int getLoginTimeout() throws SQLException { |
| 281 | + return -1; |
| 282 | + } |
| 283 | + |
| 284 | + } |
| 285 | + |
204 | 286 | }
|
0 commit comments