|
| 1 | +package com.codingapi.springboot.authorization.handler; |
| 2 | + |
| 3 | +import com.codingapi.springboot.authorization.interceptor.SQLExecuteState; |
| 4 | +import org.junit.jupiter.api.AfterEach; |
| 5 | +import org.junit.jupiter.api.BeforeEach; |
| 6 | +import org.junit.jupiter.api.Test; |
| 7 | + |
| 8 | +import java.io.InputStream; |
| 9 | +import java.io.Reader; |
| 10 | +import java.math.BigDecimal; |
| 11 | +import java.net.URL; |
| 12 | +import java.sql.Array; |
| 13 | +import java.sql.Blob; |
| 14 | +import java.sql.Clob; |
| 15 | +import java.sql.Date; |
| 16 | +import java.sql.NClob; |
| 17 | +import java.sql.Ref; |
| 18 | +import java.sql.RowId; |
| 19 | +import java.sql.SQLXML; |
| 20 | +import java.sql.Time; |
| 21 | +import java.sql.Timestamp; |
| 22 | + |
| 23 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 24 | +import static org.junit.jupiter.api.Assertions.assertSame; |
| 25 | +import static org.mockito.ArgumentMatchers.any; |
| 26 | +import static org.mockito.ArgumentMatchers.anyBoolean; |
| 27 | +import static org.mockito.ArgumentMatchers.anyByte; |
| 28 | +import static org.mockito.ArgumentMatchers.anyDouble; |
| 29 | +import static org.mockito.ArgumentMatchers.anyFloat; |
| 30 | +import static org.mockito.ArgumentMatchers.anyInt; |
| 31 | +import static org.mockito.ArgumentMatchers.anyLong; |
| 32 | +import static org.mockito.ArgumentMatchers.anyShort; |
| 33 | +import static org.mockito.ArgumentMatchers.anyString; |
| 34 | +import static org.mockito.ArgumentMatchers.eq; |
| 35 | +import static org.mockito.Mockito.mock; |
| 36 | +import static org.mockito.Mockito.verify; |
| 37 | +import static org.mockito.Mockito.when; |
| 38 | + |
| 39 | +/** |
| 40 | + * ColumnHandlerContext 单元测试 |
| 41 | + * 验证上下文将各类 get 请求委托给注册的 ColumnHandler |
| 42 | + */ |
| 43 | +class ColumnHandlerContextTest { |
| 44 | + |
| 45 | + private ColumnHandler columnHandler; |
| 46 | + private SQLExecuteState state; |
| 47 | + |
| 48 | + @BeforeEach |
| 49 | + void setUp() { |
| 50 | + columnHandler = mock(ColumnHandler.class); |
| 51 | + ColumnHandlerContext.getInstance().setColumnHandler(columnHandler); |
| 52 | + state = SQLExecuteState.unIntercept("select 1"); |
| 53 | + } |
| 54 | + |
| 55 | + @AfterEach |
| 56 | + void tearDown() { |
| 57 | + // 恢复默认处理器,避免影响其他测试 |
| 58 | + ColumnHandlerContext.getInstance().setColumnHandler(new DefaultColumnHandler()); |
| 59 | + } |
| 60 | + |
| 61 | + @Test |
| 62 | + void testDelegatesToColumnHandler() throws Exception { |
| 63 | + when(columnHandler.getString(any(), anyInt(), anyString(), anyString(), anyString())).thenReturn("s"); |
| 64 | + when(columnHandler.getShort(any(), anyInt(), anyString(), anyString(), anyShort())).thenReturn((short) 1); |
| 65 | + when(columnHandler.getBoolean(any(), anyInt(), anyString(), anyString(), anyBoolean())).thenReturn(true); |
| 66 | + when(columnHandler.getByte(any(), anyInt(), anyString(), anyString(), anyByte())).thenReturn((byte) 2); |
| 67 | + when(columnHandler.getInt(any(), anyInt(), anyString(), anyString(), anyInt())).thenReturn(3); |
| 68 | + when(columnHandler.getLong(any(), anyInt(), anyString(), anyString(), anyLong())).thenReturn(4L); |
| 69 | + when(columnHandler.getFloat(any(), anyInt(), anyString(), anyString(), anyFloat())).thenReturn(5.0f); |
| 70 | + when(columnHandler.getDouble(any(), anyInt(), anyString(), anyString(), anyDouble())).thenReturn(6.0d); |
| 71 | + when(columnHandler.getBigDecimal(any(), anyInt(), anyString(), anyString(), any(BigDecimal.class))) |
| 72 | + .thenReturn(BigDecimal.ONE); |
| 73 | + when(columnHandler.getBytes(any(), anyInt(), anyString(), anyString(), any())) |
| 74 | + .thenReturn(new byte[]{1}); |
| 75 | + Timestamp timestamp = new Timestamp(1000L); |
| 76 | + when(columnHandler.getTimestamp(any(), anyInt(), anyString(), anyString(), any())) |
| 77 | + .thenReturn(timestamp); |
| 78 | + Time time = new Time(1000L); |
| 79 | + when(columnHandler.getTime(any(), anyInt(), anyString(), anyString(), any())).thenReturn(time); |
| 80 | + Date date = new Date(1000L); |
| 81 | + when(columnHandler.getDate(any(), anyInt(), anyString(), anyString(), any())).thenReturn(date); |
| 82 | + InputStream inputStream = mock(InputStream.class); |
| 83 | + when(columnHandler.getAsciiStream(any(), anyInt(), anyString(), anyString(), any())) |
| 84 | + .thenReturn(inputStream); |
| 85 | + when(columnHandler.getUnicodeStream(any(), anyInt(), anyString(), anyString(), any())) |
| 86 | + .thenReturn(inputStream); |
| 87 | + when(columnHandler.getBinaryStream(any(), anyInt(), anyString(), anyString(), any())) |
| 88 | + .thenReturn(inputStream); |
| 89 | + when(columnHandler.getObject(any(), anyInt(), anyString(), anyString(), any())).thenReturn("obj"); |
| 90 | + Reader reader = mock(Reader.class); |
| 91 | + when(columnHandler.getCharacterStream(any(), anyInt(), anyString(), anyString(), any())) |
| 92 | + .thenReturn(reader); |
| 93 | + when(columnHandler.getNCharacterStream(any(), anyInt(), anyString(), anyString(), any())) |
| 94 | + .thenReturn(reader); |
| 95 | + Ref ref = mock(Ref.class); |
| 96 | + when(columnHandler.getRef(any(), anyInt(), anyString(), anyString(), any())).thenReturn(ref); |
| 97 | + Blob blob = mock(Blob.class); |
| 98 | + when(columnHandler.getBlob(any(), anyInt(), anyString(), anyString(), any())).thenReturn(blob); |
| 99 | + Clob clob = mock(Clob.class); |
| 100 | + when(columnHandler.getClob(any(), anyInt(), anyString(), anyString(), any())).thenReturn(clob); |
| 101 | + Array array = mock(Array.class); |
| 102 | + when(columnHandler.getArray(any(), anyInt(), anyString(), anyString(), any())).thenReturn(array); |
| 103 | + URL url = mock(URL.class); |
| 104 | + when(columnHandler.getURL(any(), anyInt(), anyString(), anyString(), any())).thenReturn(url); |
| 105 | + NClob nClob = mock(NClob.class); |
| 106 | + when(columnHandler.getNClob(any(), anyInt(), anyString(), anyString(), any())).thenReturn(nClob); |
| 107 | + SQLXML sqlxml = mock(SQLXML.class); |
| 108 | + when(columnHandler.getSQLXML(any(), anyInt(), anyString(), anyString(), any())) |
| 109 | + .thenReturn(sqlxml); |
| 110 | + when(columnHandler.getNString(any(), anyInt(), anyString(), anyString(), anyString())).thenReturn("ns"); |
| 111 | + RowId rowId = mock(RowId.class); |
| 112 | + when(columnHandler.getRowId(any(), anyInt(), anyString(), anyString(), any())).thenReturn(rowId); |
| 113 | + when(columnHandler.getObject(any(), anyInt(), anyString(), anyString(), any(), eq(String.class))) |
| 114 | + .thenReturn("typed"); |
| 115 | + |
| 116 | + ColumnHandlerContext context = ColumnHandlerContext.getInstance(); |
| 117 | + |
| 118 | + assertEquals("s", context.getString(state, 1, "t", "c", "v")); |
| 119 | + assertEquals((short) 1, context.getShort(state, 1, "t", "c", (short) 0)); |
| 120 | + assertEquals(true, context.getBoolean(state, 1, "t", "c", false)); |
| 121 | + assertEquals((byte) 2, context.getByte(state, 1, "t", "c", (byte) 0)); |
| 122 | + assertEquals(3, context.getInt(state, 1, "t", "c", 0)); |
| 123 | + assertEquals(4L, context.getLong(state, 1, "t", "c", 0L)); |
| 124 | + assertEquals(5.0f, context.getFloat(state, 1, "t", "c", 0f)); |
| 125 | + assertEquals(6.0d, context.getDouble(state, 1, "t", "c", 0d)); |
| 126 | + assertEquals(BigDecimal.ONE, context.getBigDecimal(state, 1, "t", "c", BigDecimal.ZERO)); |
| 127 | + assertSame(timestamp, context.getTimestamp(state, 1, "t", "c", null)); |
| 128 | + assertSame(time, context.getTime(state, 1, "t", "c", null)); |
| 129 | + assertSame(date, context.getDate(state, 1, "t", "c", null)); |
| 130 | + assertSame(inputStream, context.getAsciiStream(state, 1, "t", "c", null)); |
| 131 | + assertSame(inputStream, context.getUnicodeStream(state, 1, "t", "c", null)); |
| 132 | + assertSame(inputStream, context.getBinaryStream(state, 1, "t", "c", null)); |
| 133 | + assertEquals("obj", context.getObject(state, 1, "t", "c", null)); |
| 134 | + assertSame(reader, context.getCharacterStream(state, 1, "t", "c", null)); |
| 135 | + assertSame(reader, context.getNCharacterStream(state, 1, "t", "c", null)); |
| 136 | + assertSame(ref, context.getRef(state, 1, "t", "c", null)); |
| 137 | + assertSame(blob, context.getBlob(state, 1, "t", "c", null)); |
| 138 | + assertSame(clob, context.getClob(state, 1, "t", "c", null)); |
| 139 | + assertSame(array, context.getArray(state, 1, "t", "c", null)); |
| 140 | + assertSame(url, context.getURL(state, 1, "t", "c", null)); |
| 141 | + assertSame(nClob, context.getNClob(state, 1, "t", "c", null)); |
| 142 | + assertSame(sqlxml, context.getSQLXML(state, 1, "t", "c", null)); |
| 143 | + assertEquals("ns", context.getNString(state, 1, "t", "c", "v")); |
| 144 | + assertSame(rowId, context.getRowId(state, 1, "t", "c", null)); |
| 145 | + assertEquals("typed", context.getObject(state, 1, "t", "c", "v", String.class)); |
| 146 | + assertEquals(1, context.getBytes(state, 1, "t", "c", null).length); |
| 147 | + |
| 148 | + // 验证委托时参数透传 |
| 149 | + verify(columnHandler).getString(state, 1, "t", "c", "v"); |
| 150 | + verify(columnHandler).getInt(state, 1, "t", "c", 0); |
| 151 | + verify(columnHandler).getObject(state, 1, "t", "c", "v", String.class); |
| 152 | + } |
| 153 | +} |
0 commit comments