This commit is contained in:
2026-02-04 00:53:07 +08:00
parent c0acba8180
commit 7d48faca11
570 changed files with 134 additions and 43299 deletions

View File

@@ -1,108 +0,0 @@
package org.springblade.test.handler;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.BeforeEach;
import org.mockito.Mockito;
import org.springblade.common.handler.PostgresLongArrayTypeHandler;
import java.sql.Array;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.List;
import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.Mockito.*;
/**
* 改进版 PostgresLongArrayTypeHandler 测试类
*/
public class PostgresLongArrayTypeHandlerImprovedTest {
private PostgresLongArrayTypeHandler handler;
private PreparedStatement preparedStatement;
private ResultSet resultSet;
@BeforeEach
void setUp() throws Exception {
handler = new PostgresLongArrayTypeHandler();
preparedStatement = Mockito.mock(PreparedStatement.class);
resultSet = Mockito.mock(ResultSet.class);
}
@Test
void testSetNonNullParameter() throws Exception {
List<Long> parameter = List.of(1L, 2L, 3L);
// 模拟数据库连接创建数组
when(preparedStatement.getConnection()).thenReturn(Mockito.mock(java.sql.Connection.class));
java.sql.Connection conn = preparedStatement.getConnection();
when(conn.createArrayOf(anyString(), any())).thenReturn(Mockito.mock(Array.class));
// 执行测试
handler.setNonNullParameter(preparedStatement, 1, parameter, null);
// 验证调用
verify(conn).createArrayOf(eq("bigint"), any());
verify(preparedStatement).setArray(eq(1), any(Array.class));
}
@Test
void testGetNullableResultWithLongList() throws Exception {
List<Long> expected = List.of(1L, 2L, 3L);
when(resultSet.getObject("test_column")).thenReturn(expected);
List<Long> result = handler.getNullableResult(resultSet, "test_column");
assertNotNull(result);
assertEquals(expected, result);
}
@Test
void testGetNullableResultWithStringArray() throws Exception {
String arrayStr = "{1,2,3}";
List<Long> expected = List.of(1L, 2L, 3L);
when(resultSet.getObject("test_column")).thenReturn(arrayStr);
List<Long> result = handler.getNullableResult(resultSet, "test_column");
assertNotNull(result);
assertEquals(expected, result);
}
@Test
void testGetNullableResultWithNull() throws Exception {
when(resultSet.getObject("test_column")).thenReturn(null);
List<Long> result = handler.getNullableResult(resultSet, "test_column");
assertNull(result);
}
@Test
void testGetNullableResultWithEmptyStringArray() throws Exception {
String arrayStr = "{}";
when(resultSet.getObject("test_column")).thenReturn(arrayStr);
List<Long> result = handler.getNullableResult(resultSet, "test_column");
assertNotNull(result);
assertTrue(result.isEmpty());
}
@Test
void testGetNullableResultWithSingleElementStringArray() throws Exception {
String arrayStr = "{42}";
List<Long> expected = List.of(42L);
when(resultSet.getObject("test_column")).thenReturn(arrayStr);
List<Long> result = handler.getNullableResult(resultSet, "test_column");
assertNotNull(result);
assertEquals(expected, result);
assertEquals(1, result.size());
}
}

View File

@@ -1,113 +0,0 @@
package org.springblade.test.handler;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.BeforeEach;
import org.mockito.Mockito;
import org.springblade.common.handler.PostgresLongArrayTypeHandler;
import java.sql.Array;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.List;
import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.Mockito.*;
/**
* PostgresLongArrayTypeHandler 测试类
*/
public class PostgresLongArrayTypeHandlerTest {
private PostgresLongArrayTypeHandler handler;
private PreparedStatement preparedStatement;
private ResultSet resultSet;
private Array array;
@BeforeEach
void setUp() throws Exception {
handler = new PostgresLongArrayTypeHandler();
preparedStatement = Mockito.mock(PreparedStatement.class);
resultSet = Mockito.mock(ResultSet.class);
array = Mockito.mock(Array.class);
}
@Test
void testSetNonNullParameter() throws Exception {
List<Long> parameter = List.of(1L, 2L, 3L);
// 模拟数据库连接创建数组
when(preparedStatement.getConnection()).thenReturn(Mockito.mock(java.sql.Connection.class));
java.sql.Connection conn = preparedStatement.getConnection();
when(conn.createArrayOf(anyString(), any())).thenReturn(array);
// 执行测试
handler.setNonNullParameter(preparedStatement, 1, parameter, null);
// 验证调用
verify(conn).createArrayOf(eq("BIGINT"), any());
verify(preparedStatement).setArray(eq(1), any(Array.class));
}
@Test
void testGetNullableResultWithColumnName() throws Exception {
List<Long> expected = List.of(1L, 2L, 3L);
Object[] arrayData = {1L, 2L, 3L};
when(resultSet.getArray("test_column")).thenReturn(array);
when(array.getArray()).thenReturn(arrayData);
List<Long> result = handler.getNullableResult(resultSet, "test_column");
assertNotNull(result);
assertEquals(expected, result);
}
@Test
void testGetNullableResultWithNullArray() throws Exception {
when(resultSet.getArray("test_column")).thenReturn(null);
List<Long> result = handler.getNullableResult(resultSet, "test_column");
assertNull(result); // 当数据库值为NULL时,应返回null
}
@Test
void testGetNullableResultWithEmptyArray() throws Exception {
when(resultSet.getArray("test_column")).thenReturn(array);
when(array.getArray()).thenReturn(new Object[0]);
List<Long> result = handler.getNullableResult(resultSet, "test_column");
assertNotNull(result); // 空数组应返回空列表而非null
assertTrue(result.isEmpty());
}
@Test
void testGetNullableResultWithSingleElement() throws Exception {
List<Long> expected = List.of(42L);
Object[] arrayData = {42L};
when(resultSet.getArray("test_column")).thenReturn(array);
when(array.getArray()).thenReturn(arrayData);
List<Long> result = handler.getNullableResult(resultSet, "test_column");
assertNotNull(result);
assertEquals(expected, result);
assertEquals(1, result.size());
}
@Test
void testGetNullableResultWithMixedNumberTypes() throws Exception {
Object[] arrayData = {Integer.valueOf(1), Long.valueOf(2L), Short.valueOf((short) 3)};
List<Long> expected = List.of(1L, 2L, 3L);
when(resultSet.getArray("test_column")).thenReturn(array);
when(array.getArray()).thenReturn(arrayData);
List<Long> result = handler.getNullableResult(resultSet, "test_column");
assertNotNull(result);
assertEquals(expected, result);
}
}