|
| 1 | +package io.ebean.test.config; |
| 2 | + |
| 3 | + |
| 4 | +import io.ebean.Database; |
| 5 | +import io.ebean.DatabaseFactory; |
| 6 | +import io.ebean.annotation.Platform; |
| 7 | +import io.ebean.config.DatabaseConfig; |
| 8 | +import io.ebean.datasource.DataSourceAlert; |
| 9 | +import io.ebean.datasource.DataSourceInitialiseException; |
| 10 | +import io.ebean.xtest.ForPlatform; |
| 11 | + |
| 12 | +import io.ebean.xtest.base.PlatformCondition; |
| 13 | +import org.junit.jupiter.api.Test; |
| 14 | +import org.junit.jupiter.api.extension.ExtendWith; |
| 15 | +import org.tests.model.basic.EBasicVer; |
| 16 | + |
| 17 | +import java.sql.Connection; |
| 18 | +import java.sql.DriverManager; |
| 19 | +import java.sql.SQLException; |
| 20 | +import java.sql.Statement; |
| 21 | +import java.util.Properties; |
| 22 | + |
| 23 | +import jakarta.persistence.PersistenceException; |
| 24 | +import javax.sql.DataSource; |
| 25 | + |
| 26 | +import static org.assertj.core.api.Assertions.assertThat; |
| 27 | +import static org.assertj.core.api.Assertions.assertThatThrownBy; |
| 28 | + |
| 29 | +@ExtendWith(PlatformCondition.class) |
| 30 | +public class TestServerOffline { |
| 31 | + |
| 32 | + @Test |
| 33 | + @ForPlatform({Platform.H2}) |
| 34 | + public void testOffline_default() throws SQLException { |
| 35 | + |
| 36 | + String url = "jdbc:h2:mem:testoffline1"; |
| 37 | + try (Connection bootup = DriverManager.getConnection(url, "sa", "secret")) { |
| 38 | + Properties props = props(url); |
| 39 | + DatabaseConfig config = config(props); |
| 40 | + |
| 41 | + assertThatThrownBy(() -> DatabaseFactory.create(config)) |
| 42 | + .isInstanceOf(DataSourceInitialiseException.class); |
| 43 | + } |
| 44 | + |
| 45 | + } |
| 46 | + |
| 47 | + private static class LazyDatasourceInitializer implements DataSourceAlert { |
| 48 | + |
| 49 | + public Database server; |
| 50 | + |
| 51 | + private boolean initialized; |
| 52 | + |
| 53 | + @Override |
| 54 | + public void dataSourceUp(DataSource dataSource) { |
| 55 | + if (!initialized) { |
| 56 | + initDatabase(); |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + public synchronized void initDatabase() { |
| 61 | + if (!initialized) { |
| 62 | + server.runDdl(); |
| 63 | + initialized = true; |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + @Override |
| 68 | + public void dataSourceDown(DataSource dataSource, SQLException reason) {} |
| 69 | + |
| 70 | + } |
| 71 | + |
| 72 | + @Test |
| 73 | + @ForPlatform({Platform.H2}) |
| 74 | + public void testOffline_recovery() throws SQLException { |
| 75 | + |
| 76 | + String url = "jdbc:h2:mem:testoffline3"; |
| 77 | + try (Connection bootup = DriverManager.getConnection(url, "sa", "secret")) { |
| 78 | + |
| 79 | + Properties props = props(url); |
| 80 | + |
| 81 | + // to bring up ebean without a database, we must disable various things |
| 82 | + // that happen on startup |
| 83 | + props.setProperty("datasource.h2_offline.failOnStart", "false"); |
| 84 | + props.setProperty("ebean.h2_offline.skipDataSourceCheck", "true"); |
| 85 | + props.setProperty("ebean.h2_offline.ddl.run", "false"); |
| 86 | + DatabaseConfig config = config(props); |
| 87 | + |
| 88 | + LazyDatasourceInitializer alert = new LazyDatasourceInitializer() ; |
| 89 | + config.getDataSourceConfig().alert(alert); |
| 90 | + config.getDataSourceConfig().heartbeatFreqSecs(1); |
| 91 | + |
| 92 | + Database h2Offline = DatabaseFactory.create(config); |
| 93 | + alert.server = h2Offline; |
| 94 | + assertThat(h2Offline).isNotNull(); |
| 95 | + // DB is online now in offline mode |
| 96 | + |
| 97 | + // Accessing the DB will throw a PE |
| 98 | + assertThatThrownBy(() -> alert.initDatabase()) |
| 99 | + .isInstanceOf(PersistenceException.class) |
| 100 | + .hasMessageContaining("Failed to obtain connection to run DDL"); |
| 101 | + |
| 102 | + assertThatThrownBy(() -> h2Offline.find(EBasicVer.class).findCount()).isInstanceOf(PersistenceException.class); |
| 103 | + |
| 104 | + // so - reset the password so that the server can reconnect |
| 105 | + try (Statement stmt = bootup.createStatement()) { |
| 106 | + stmt.execute("alter user sa set password 'sa'"); |
| 107 | + } |
| 108 | + |
| 109 | + assertThat(alert.initialized).isFalse(); |
| 110 | + |
| 111 | + // next access to ebean should bring DS online |
| 112 | + h2Offline.find(EBasicVer.class).findCount(); |
| 113 | + assertThat(alert.initialized).isTrue(); |
| 114 | + |
| 115 | + // check if server is working (ie ddl was run) |
| 116 | + EBasicVer bean = new EBasicVer("foo"); |
| 117 | + h2Offline.save(bean); |
| 118 | + assertThat(h2Offline.find(EBasicVer.class).findCount()).isEqualTo(1); |
| 119 | + h2Offline.delete(bean); |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + private Properties props(String url) { |
| 124 | + |
| 125 | + Properties props = new Properties(); |
| 126 | + |
| 127 | + props.setProperty("datasource.h2_offline.username", "sa"); |
| 128 | + props.setProperty("datasource.h2_offline.password", "sa"); |
| 129 | + props.setProperty("datasource.h2_offline.url", url); |
| 130 | + props.setProperty("datasource.h2_offline.driver", "org.h2.Driver"); |
| 131 | + |
| 132 | + props.setProperty("ebean.h2_offline.databasePlatformName", "h2"); |
| 133 | + props.setProperty("ebean.h2_offline.ddl.extra", "false"); |
| 134 | + |
| 135 | + props.setProperty("ebean.h2_offline.ddl.generate", "true"); |
| 136 | + props.setProperty("ebean.h2_offline.ddl.run", "true"); |
| 137 | + |
| 138 | + return props; |
| 139 | + } |
| 140 | + |
| 141 | + private DatabaseConfig config(Properties props) { |
| 142 | + DatabaseConfig config = new DatabaseConfig(); |
| 143 | + config.setName("h2_offline"); |
| 144 | + config.loadFromProperties(props); |
| 145 | + config.setDefaultServer(false); |
| 146 | + config.setRegister(false); |
| 147 | + config.classes().add(EBasicVer.class); |
| 148 | + return config; |
| 149 | + } |
| 150 | + |
| 151 | +} |
0 commit comments