|
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(); |
@@ -1528,6 +1543,49 @@ public void testManagedIdentityWithEncryptStrict() { |
1528 | 1543 | } catch (SQLException e) { |
1529 | 1544 | fail("Connection failed: " + e.getMessage()); |
1530 | 1545 | } |
1531 | | - } |
| 1546 | + } |
| 1547 | + |
| 1548 | + public Method mockedConnectionRecoveryCheck() throws Exception { |
| 1549 | + mockConnection = spy(new SQLServerConnection("test")); |
| 1550 | + mockLogger = mock(Logger.class); |
| 1551 | + doReturn(true).when(mockLogger).isLoggable(Level.WARNING); |
| 1552 | + doNothing().when(mockConnection).terminate(anyInt(), anyString()); |
| 1553 | + |
| 1554 | + Method method = SQLServerConnection.class.getDeclaredMethod("connectionReconveryCheck", boolean.class, |
| 1555 | + boolean.class, ServerPortPlaceHolder.class); |
| 1556 | + method.setAccessible(true); |
| 1557 | + return method; |
| 1558 | + } |
| 1559 | + |
| 1560 | + @Test |
| 1561 | + void testConnectionRecoveryCheckThrowsWhenAllConditionsMet() throws Exception { |
| 1562 | + Method method = mockedConnectionRecoveryCheck(); |
| 1563 | + method.invoke(mockConnection, true, false, null); |
| 1564 | + verify(mockConnection, times(1)).terminate(eq(SQLServerException.DRIVER_ERROR_INVALID_TDS), |
| 1565 | + eq(SQLServerException.getErrString("R_crClientNoRecoveryAckFromLogin"))); |
| 1566 | + } |
| 1567 | + |
| 1568 | + @Test |
| 1569 | + void testConnectionRecoveryCheckDoesNotThrowWhenNotReconnectRunning() throws Exception { |
| 1570 | + Method method = mockedConnectionRecoveryCheck(); |
| 1571 | + method.invoke(mockConnection, false, false, null); |
| 1572 | + verify(mockConnection, never()).terminate(anyInt(), anyString()); |
| 1573 | + } |
| 1574 | + |
| 1575 | + @Test |
| 1576 | + void testConnectionRecoveryCheckDoesNotThrowWhenRecoveryPossible() throws Exception { |
| 1577 | + Method method = mockedConnectionRecoveryCheck(); |
| 1578 | + method.invoke(mockConnection, true, true, null); |
| 1579 | + verify(mockConnection, never()).terminate(anyInt(), anyString()); |
| 1580 | + } |
| 1581 | + |
| 1582 | + @Test |
| 1583 | + void testConnectionRecoveryCheckDoesNotThrowWhenRoutingDetailsNotNull() throws Exception { |
| 1584 | + Method method = mockedConnectionRecoveryCheck(); |
| 1585 | + ServerPortPlaceHolder routingDetails = mock(ServerPortPlaceHolder.class); |
| 1586 | + method.setAccessible(true); |
| 1587 | + method.invoke(mockConnection, true, false, routingDetails); |
| 1588 | + verify(mockConnection, never()).terminate(anyInt(), anyString()); |
| 1589 | + } |
1532 | 1590 |
|
1533 | 1591 | } |
0 commit comments