|
| 1 | +package com.bencodez.simpleapi.sql.mysql; |
| 2 | + |
| 3 | +import static org.junit.jupiter.api.Assertions.*; |
| 4 | +import static org.mockito.Mockito.*; |
| 5 | + |
| 6 | +import java.sql.Connection; |
| 7 | +import java.sql.Driver; |
| 8 | +import java.sql.DriverPropertyInfo; |
| 9 | +import java.sql.SQLException; |
| 10 | +import java.util.Properties; |
| 11 | +import java.util.logging.Logger; |
| 12 | + |
| 13 | +import org.junit.jupiter.api.Test; |
| 14 | + |
| 15 | +import com.bencodez.simpleapi.sql.mysql.config.MysqlConfig; |
| 16 | + |
| 17 | +class AbstractSqlTableInitializationTest { |
| 18 | + @Test |
| 19 | + void deferredExistingPoolConstructorDoesNotNeedSubclassState() { |
| 20 | + MySQL mysql = mock(MySQL.class); |
| 21 | + ConnectionManager manager = mock(ConnectionManager.class); |
| 22 | + when(mysql.getConnectionManager()).thenReturn(manager); |
| 23 | + when(manager.getDbType()).thenReturn(DbType.MYSQL); |
| 24 | + DeferredTable table = new DeferredTable("votes", mysql); |
| 25 | + assertFalse(table.initialized); |
| 26 | + table.initialized = true; |
| 27 | + assertEquals("votes", table.getTableName()); |
| 28 | + } |
| 29 | + @Test |
| 30 | + void authenticationFailureStopsConstructorBeforeSubclassHooksOrQueries() { |
| 31 | + RejectingTable.hookCalls = 0; |
| 32 | + RejectingDriver.connectionAttempts = 0; |
| 33 | + MysqlConfig config = config(); |
| 34 | + config.setDriver(RejectingDriver.class.getName()); |
| 35 | + |
| 36 | + IllegalStateException failure = assertThrows(IllegalStateException.class, |
| 37 | + () -> new RejectingTable("votes", config, true)); |
| 38 | + assertTrue(hasCause(failure, AUTH_FAILURE)); |
| 39 | + assertEquals(0, RejectingTable.hookCalls); |
| 40 | + assertTrue(RejectingDriver.connectionAttempts > 0); |
| 41 | + } |
| 42 | + |
| 43 | + @Test void databaseSelectionFailureClosesPoolWithoutCallingSubclassHooks() throws Exception { |
| 44 | + SQLException denied = new SQLException("controlled database selection rejected", "42000"); |
| 45 | + RejectingTable.hookCalls = 0; |
| 46 | + try (var managers = mockConstruction(ConnectionManager.class, (manager, context) -> { |
| 47 | + when(manager.open()).thenReturn(true); |
| 48 | + Connection connection = mock(Connection.class); |
| 49 | + when(manager.getConnectionChecked()).thenReturn(connection); |
| 50 | + when(connection.prepareStatement(anyString())).thenThrow(denied); |
| 51 | + })) { |
| 52 | + IllegalStateException failure = assertThrows(IllegalStateException.class, |
| 53 | + () -> new RejectingTable("votes", config(), true)); |
| 54 | + assertSame(denied, failure.getCause()); |
| 55 | + assertEquals(0, RejectingTable.hookCalls); |
| 56 | + verify(managers.constructed().get(0)).close(); |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + @Test void uncheckedDatabaseSelectionFailuresKeepIdentityAndCleanupFailures() throws Exception { |
| 61 | + for (Throwable failure : new Throwable[] {new IllegalArgumentException("driver rejected selection"), |
| 62 | + new LinkageError("driver linkage failure")}) { |
| 63 | + RuntimeException cleanupFailure = new IllegalStateException("controlled cleanup failure"); |
| 64 | + RejectingTable.hookCalls = 0; |
| 65 | + try (var managers = mockConstruction(ConnectionManager.class, (manager, context) -> { |
| 66 | + when(manager.open()).thenReturn(true); |
| 67 | + Connection connection = mock(Connection.class); |
| 68 | + when(manager.getConnectionChecked()).thenReturn(connection); |
| 69 | + when(connection.prepareStatement(anyString())).thenThrow(failure); |
| 70 | + doThrow(cleanupFailure).when(manager).close(); |
| 71 | + })) { |
| 72 | + Throwable thrown = assertThrows(failure.getClass(), |
| 73 | + () -> new RejectingTable("votes", config(), true)); |
| 74 | + assertSame(failure, thrown); |
| 75 | + assertArrayEquals(new Throwable[] {cleanupFailure}, thrown.getSuppressed()); |
| 76 | + assertEquals(0, RejectingTable.hookCalls); |
| 77 | + verify(managers.constructed().get(0)).close(); |
| 78 | + verify(managers.constructed().get(0).getConnectionChecked()).close(); |
| 79 | + } |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + @Test void databaseSelectionEscapesQuotedDatabaseNames() throws Exception { |
| 84 | + MysqlConfig config = config(); |
| 85 | + config.setDatabase("vote`data"); |
| 86 | + try (var managers = mockConstruction(ConnectionManager.class, (manager, context) -> { |
| 87 | + when(manager.open()).thenReturn(true); |
| 88 | + Connection connection = mock(Connection.class); |
| 89 | + when(manager.getConnectionChecked()).thenReturn(connection); |
| 90 | + when(connection.prepareStatement("USE `vote``data`;")) |
| 91 | + .thenReturn(mock(java.sql.PreparedStatement.class)); |
| 92 | + })) { |
| 93 | + AbstractSqlTable table = new AbstractSqlTable("votes", config, true, true) { |
| 94 | + public String getPrimaryKeyColumn() { return "id"; } |
| 95 | + public String buildCreateTableSql(DbType type) { return "CREATE TABLE votes (id TEXT)"; } |
| 96 | + public void logSevere(String message) { fail("early hook"); } |
| 97 | + public void logInfo(String message) { fail("early hook"); } |
| 98 | + public void debug(Throwable failure) { fail("early hook"); } |
| 99 | + public void debug(String message) { fail("early hook"); } |
| 100 | + }; |
| 101 | + verify(managers.constructed().get(0).getConnectionChecked()) |
| 102 | + .prepareStatement("USE `vote``data`;"); |
| 103 | + table.close(); |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + private static MysqlConfig config() { |
| 108 | + MysqlConfig config = new MysqlConfig(); |
| 109 | + config.setHostName("127.0.0.1"); |
| 110 | + config.setPort(3306); |
| 111 | + config.setUser("user"); |
| 112 | + config.setPass("password"); |
| 113 | + config.setDatabase("database"); |
| 114 | + config.setMinimumIdle(0); |
| 115 | + config.setMaxThreads(1); |
| 116 | + config.setConnectionTimeout(1000); |
| 117 | + return config; |
| 118 | + } |
| 119 | + |
| 120 | + private static boolean hasCause(Throwable failure, String message) { |
| 121 | + for (Throwable current = failure; current != null; current = current.getCause()) { |
| 122 | + if (message.equals(current.getMessage())) return true; |
| 123 | + } |
| 124 | + return false; |
| 125 | + } |
| 126 | + |
| 127 | + private static final String AUTH_FAILURE = "controlled authentication rejected"; |
| 128 | + |
| 129 | + private static final class DeferredTable extends AbstractSqlTable { |
| 130 | + boolean initialized; |
| 131 | + DeferredTable(String name, MySQL mysql) { super(name, mysql, true); } |
| 132 | + public String getPrimaryKeyColumn() { return "id"; } |
| 133 | + public String buildCreateTableSql(DbType type) { return "CREATE TABLE votes (id TEXT)"; } |
| 134 | + public void logSevere(String message) { if (!initialized) fail("early hook"); } |
| 135 | + public void logInfo(String message) { if (!initialized) fail("early hook"); } |
| 136 | + public void debug(Throwable failure) { if (!initialized) fail("early hook"); } |
| 137 | + public void debug(String message) { if (!initialized) fail("early hook"); } |
| 138 | + } |
| 139 | + |
| 140 | + private static final class RejectingTable extends AbstractSqlTable { |
| 141 | + static int hookCalls; |
| 142 | + |
| 143 | + RejectingTable(String name, MysqlConfig config, boolean debug) { |
| 144 | + super(name, config, debug); |
| 145 | + } |
| 146 | + |
| 147 | + public String getPrimaryKeyColumn() { return "id"; } |
| 148 | + public String buildCreateTableSql(DbType type) { hookCalls++; return "CREATE TABLE votes (id TEXT)"; } |
| 149 | + public void logSevere(String message) { hookCalls++; } |
| 150 | + public void logInfo(String message) { hookCalls++; } |
| 151 | + public void debug(Throwable failure) { hookCalls++; } |
| 152 | + public void debug(String message) { hookCalls++; } |
| 153 | + } |
| 154 | + |
| 155 | + public static final class RejectingDriver implements Driver { |
| 156 | + static int connectionAttempts; |
| 157 | + |
| 158 | + public Connection connect(String url, Properties properties) throws SQLException { |
| 159 | + connectionAttempts++; |
| 160 | + throw new SQLException(AUTH_FAILURE); |
| 161 | + } |
| 162 | + public boolean acceptsURL(String url) { return url != null && url.startsWith("jdbc:"); } |
| 163 | + public DriverPropertyInfo[] getPropertyInfo(String url, Properties properties) { return new DriverPropertyInfo[0]; } |
| 164 | + public int getMajorVersion() { return 1; } |
| 165 | + public int getMinorVersion() { return 0; } |
| 166 | + public boolean jdbcCompliant() { return false; } |
| 167 | + public Logger getParentLogger() { return Logger.getGlobal(); } |
| 168 | + } |
| 169 | +} |
0 commit comments