|
| 1 | +package io.micronaut.management.health.indicator.jdbc; |
| 2 | + |
| 3 | +import io.micronaut.context.ApplicationContext; |
| 4 | +import io.micronaut.context.annotation.Property; |
| 5 | +import io.micronaut.context.annotation.Requires; |
| 6 | +import io.micronaut.core.util.StringUtils; |
| 7 | +import jakarta.inject.Singleton; |
| 8 | +import org.junit.jupiter.api.Test; |
| 9 | + |
| 10 | +import javax.sql.DataSource; |
| 11 | +import java.io.PrintWriter; |
| 12 | +import java.sql.Connection; |
| 13 | +import java.sql.SQLException; |
| 14 | +import java.sql.SQLFeatureNotSupportedException; |
| 15 | +import java.util.Map; |
| 16 | +import java.util.logging.Logger; |
| 17 | + |
| 18 | +import static org.junit.jupiter.api.Assertions.assertFalse; |
| 19 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 20 | + |
| 21 | +class JdbcIndicatorTest { |
| 22 | + |
| 23 | + @Test |
| 24 | + void jdbcHealthIndicatorViaConfiguration() { |
| 25 | + Map<String, Object> configuration = Map.of( |
| 26 | + "endpoints.health.jdbc.enabled", StringUtils.FALSE, |
| 27 | + "spec.name", "JdbcIndicatorTest"); |
| 28 | + try (ApplicationContext context = ApplicationContext.run(configuration)) { |
| 29 | + assertFalse(context.containsBean(JdbcIndicator.class)); |
| 30 | + } |
| 31 | + // enabled by default |
| 32 | + configuration = Map.of("spec.name", "JdbcIndicatorTest"); |
| 33 | + try (ApplicationContext context = ApplicationContext.run(configuration)) { |
| 34 | + assertTrue(context.containsBean(JdbcIndicator.class)); |
| 35 | + } |
| 36 | + } |
| 37 | + |
| 38 | + @Requires(property = "spec.name", value = "JdbcIndicatorTest") |
| 39 | + @Singleton |
| 40 | + static class DataSourceMock implements DataSource { |
| 41 | + |
| 42 | + @Override |
| 43 | + public Connection getConnection() throws SQLException { |
| 44 | + return null; |
| 45 | + } |
| 46 | + |
| 47 | + @Override |
| 48 | + public Connection getConnection(String username, String password) throws SQLException { |
| 49 | + return null; |
| 50 | + } |
| 51 | + |
| 52 | + @Override |
| 53 | + public PrintWriter getLogWriter() throws SQLException { |
| 54 | + return null; |
| 55 | + } |
| 56 | + |
| 57 | + @Override |
| 58 | + public void setLogWriter(PrintWriter out) throws SQLException { |
| 59 | + |
| 60 | + } |
| 61 | + |
| 62 | + @Override |
| 63 | + public void setLoginTimeout(int seconds) throws SQLException { |
| 64 | + |
| 65 | + } |
| 66 | + |
| 67 | + @Override |
| 68 | + public int getLoginTimeout() throws SQLException { |
| 69 | + return 0; |
| 70 | + } |
| 71 | + |
| 72 | + @Override |
| 73 | + public Logger getParentLogger() throws SQLFeatureNotSupportedException { |
| 74 | + return null; |
| 75 | + } |
| 76 | + |
| 77 | + @Override |
| 78 | + public <T> T unwrap(Class<T> iface) throws SQLException { |
| 79 | + return null; |
| 80 | + } |
| 81 | + |
| 82 | + @Override |
| 83 | + public boolean isWrapperFor(Class<?> iface) throws SQLException { |
| 84 | + return false; |
| 85 | + } |
| 86 | + } |
| 87 | +} |
0 commit comments