|
9 | 9 | import static org.junit.jupiter.api.Assertions.assertThrows;
|
10 | 10 | import static org.junit.jupiter.api.Assertions.assertTrue;
|
11 | 11 | import static org.junit.jupiter.api.Assertions.fail;
|
| 12 | +import static org.mockito.ArgumentMatchers.anyInt; |
| 13 | +import static org.mockito.ArgumentMatchers.anyString; |
| 14 | +import static org.mockito.ArgumentMatchers.eq; |
| 15 | +import static org.mockito.Mockito.doNothing; |
| 16 | +import static org.mockito.Mockito.doReturn; |
| 17 | +import static org.mockito.Mockito.mock; |
| 18 | +import static org.mockito.Mockito.never; |
| 19 | +import static org.mockito.Mockito.spy; |
| 20 | +import static org.mockito.Mockito.times; |
| 21 | +import static org.mockito.Mockito.verify; |
12 | 22 |
|
13 | 23 | import java.io.IOException;
|
14 | 24 | import java.io.Reader;
|
15 | 25 | import java.lang.management.ManagementFactory;
|
| 26 | +import java.lang.reflect.Method; |
16 | 27 | import java.sql.Clob;
|
17 | 28 | import java.sql.Connection;
|
18 | 29 | import java.sql.DriverManager;
|
|
29 | 40 | import java.util.concurrent.ExecutorService;
|
30 | 41 | import java.util.concurrent.Executors;
|
31 | 42 | import java.util.concurrent.Future;
|
| 43 | +import java.util.logging.Level; |
32 | 44 | import java.util.logging.Logger;
|
33 | 45 |
|
34 | 46 | import javax.sql.ConnectionEvent;
|
@@ -60,6 +72,9 @@ public class SQLServerConnectionTest extends AbstractTest {
|
60 | 72 |
|
61 | 73 | String randomServer = RandomUtil.getIdentifier("Server");
|
62 | 74 |
|
| 75 | + SQLServerConnection mockConnection; |
| 76 | + Logger mockLogger; |
| 77 | + |
63 | 78 | @BeforeAll
|
64 | 79 | public static void setupTests() throws Exception {
|
65 | 80 | setConnection();
|
@@ -1400,6 +1415,49 @@ public void testManagedIdentityWithEncryptStrict() {
|
1400 | 1415 | } catch (SQLException e) {
|
1401 | 1416 | fail("Connection failed: " + e.getMessage());
|
1402 | 1417 | }
|
1403 |
| - } |
| 1418 | + } |
| 1419 | + |
| 1420 | + public Method mockedConnectionRecoveryCheck() throws Exception { |
| 1421 | + mockConnection = spy(new SQLServerConnection("test")); |
| 1422 | + mockLogger = mock(Logger.class); |
| 1423 | + doReturn(true).when(mockLogger).isLoggable(Level.WARNING); |
| 1424 | + doNothing().when(mockConnection).terminate(anyInt(), anyString()); |
| 1425 | + |
| 1426 | + Method method = SQLServerConnection.class.getDeclaredMethod("connectionReconveryCheck", boolean.class, |
| 1427 | + boolean.class, ServerPortPlaceHolder.class); |
| 1428 | + method.setAccessible(true); |
| 1429 | + return method; |
| 1430 | + } |
| 1431 | + |
| 1432 | + @Test |
| 1433 | + void testConnectionRecoveryCheckThrowsWhenAllConditionsMet() throws Exception { |
| 1434 | + Method method = mockedConnectionRecoveryCheck(); |
| 1435 | + method.invoke(mockConnection, true, false, null); |
| 1436 | + verify(mockConnection, times(1)).terminate(eq(SQLServerException.DRIVER_ERROR_INVALID_TDS), |
| 1437 | + eq(SQLServerException.getErrString("R_crClientNoRecoveryAckFromLogin"))); |
| 1438 | + } |
| 1439 | + |
| 1440 | + @Test |
| 1441 | + void testConnectionRecoveryCheckDoesNotThrowWhenNotReconnectRunning() throws Exception { |
| 1442 | + Method method = mockedConnectionRecoveryCheck(); |
| 1443 | + method.invoke(mockConnection, false, false, null); |
| 1444 | + verify(mockConnection, never()).terminate(anyInt(), anyString()); |
| 1445 | + } |
| 1446 | + |
| 1447 | + @Test |
| 1448 | + void testConnectionRecoveryCheckDoesNotThrowWhenRecoveryPossible() throws Exception { |
| 1449 | + Method method = mockedConnectionRecoveryCheck(); |
| 1450 | + method.invoke(mockConnection, true, true, null); |
| 1451 | + verify(mockConnection, never()).terminate(anyInt(), anyString()); |
| 1452 | + } |
| 1453 | + |
| 1454 | + @Test |
| 1455 | + void testConnectionRecoveryCheckDoesNotThrowWhenRoutingDetailsNotNull() throws Exception { |
| 1456 | + Method method = mockedConnectionRecoveryCheck(); |
| 1457 | + ServerPortPlaceHolder routingDetails = mock(ServerPortPlaceHolder.class); |
| 1458 | + method.setAccessible(true); |
| 1459 | + method.invoke(mockConnection, true, false, routingDetails); |
| 1460 | + verify(mockConnection, never()).terminate(anyInt(), anyString()); |
| 1461 | + } |
1404 | 1462 |
|
1405 | 1463 | }
|
0 commit comments