mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-05-09 23:51:34 +08:00
add test
This commit is contained in:
parent
e05a3b19e0
commit
34704bd326
@ -82,13 +82,13 @@ public class MetaUtil {
|
|||||||
conn = ds.getConnection();
|
conn = ds.getConnection();
|
||||||
|
|
||||||
// catalog和schema获取失败默认使用null代替
|
// catalog和schema获取失败默认使用null代替
|
||||||
String catalog = getCatalog(conn);
|
final String catalog = getCatalog(conn);
|
||||||
if (null == schema) {
|
if (null == schema) {
|
||||||
schema = getSchema(conn);
|
schema = getSchema(conn);
|
||||||
}
|
}
|
||||||
|
|
||||||
final DatabaseMetaData metaData = conn.getMetaData();
|
final DatabaseMetaData metaData = conn.getMetaData();
|
||||||
try (ResultSet rs = metaData.getTables(catalog, schema, tableName, Convert.toStrArray(types))) {
|
try (final ResultSet rs = metaData.getTables(catalog, schema, tableName, Convert.toStrArray(types))) {
|
||||||
if (null != rs) {
|
if (null != rs) {
|
||||||
String table;
|
String table;
|
||||||
while (rs.next()) {
|
while (rs.next()) {
|
||||||
@ -116,9 +116,9 @@ public class MetaUtil {
|
|||||||
*/
|
*/
|
||||||
public static String[] getColumnNames(ResultSet rs) throws DbRuntimeException {
|
public static String[] getColumnNames(ResultSet rs) throws DbRuntimeException {
|
||||||
try {
|
try {
|
||||||
ResultSetMetaData rsmd = rs.getMetaData();
|
final ResultSetMetaData rsmd = rs.getMetaData();
|
||||||
int columnCount = rsmd.getColumnCount();
|
final int columnCount = rsmd.getColumnCount();
|
||||||
String[] labelNames = new String[columnCount];
|
final String[] labelNames = new String[columnCount];
|
||||||
for (int i = 0; i < labelNames.length; i++) {
|
for (int i = 0; i < labelNames.length; i++) {
|
||||||
labelNames[i] = rsmd.getColumnLabel(i + 1);
|
labelNames[i] = rsmd.getColumnLabel(i + 1);
|
||||||
}
|
}
|
||||||
@ -137,17 +137,17 @@ public class MetaUtil {
|
|||||||
* @throws DbRuntimeException SQL执行异常
|
* @throws DbRuntimeException SQL执行异常
|
||||||
*/
|
*/
|
||||||
public static String[] getColumnNames(DataSource ds, String tableName) {
|
public static String[] getColumnNames(DataSource ds, String tableName) {
|
||||||
List<String> columnNames = new ArrayList<>();
|
final List<String> columnNames = new ArrayList<>();
|
||||||
Connection conn = null;
|
Connection conn = null;
|
||||||
try {
|
try {
|
||||||
conn = ds.getConnection();
|
conn = ds.getConnection();
|
||||||
|
|
||||||
// catalog和schema获取失败默认使用null代替
|
// catalog和schema获取失败默认使用null代替
|
||||||
String catalog = getCatalog(conn);
|
final String catalog = getCatalog(conn);
|
||||||
String schema = getSchema(conn);
|
final String schema = getSchema(conn);
|
||||||
|
|
||||||
final DatabaseMetaData metaData = conn.getMetaData();
|
final DatabaseMetaData metaData = conn.getMetaData();
|
||||||
try (ResultSet rs = metaData.getColumns(catalog, schema, tableName, null)) {
|
try (final ResultSet rs = metaData.getColumns(catalog, schema, tableName, null)) {
|
||||||
if (null != rs) {
|
if (null != rs) {
|
||||||
while (rs.next()) {
|
while (rs.next()) {
|
||||||
columnNames.add(rs.getString("COLUMN_NAME"));
|
columnNames.add(rs.getString("COLUMN_NAME"));
|
||||||
@ -225,7 +225,7 @@ public class MetaUtil {
|
|||||||
final DatabaseMetaData metaData = conn.getMetaData();
|
final DatabaseMetaData metaData = conn.getMetaData();
|
||||||
|
|
||||||
// 获得表元数据(表注释)
|
// 获得表元数据(表注释)
|
||||||
try (ResultSet rs = metaData.getTables(catalog, schema, tableName, new String[]{TableType.TABLE.value()})) {
|
try (final ResultSet rs = metaData.getTables(catalog, schema, tableName, new String[]{TableType.TABLE.value()})) {
|
||||||
if (null != rs) {
|
if (null != rs) {
|
||||||
if (rs.next()) {
|
if (rs.next()) {
|
||||||
table.setComment(rs.getString("REMARKS"));
|
table.setComment(rs.getString("REMARKS"));
|
||||||
@ -234,7 +234,7 @@ public class MetaUtil {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 获得主键
|
// 获得主键
|
||||||
try (ResultSet rs = metaData.getPrimaryKeys(catalog, schema, tableName)) {
|
try (final ResultSet rs = metaData.getPrimaryKeys(catalog, schema, tableName)) {
|
||||||
if (null != rs) {
|
if (null != rs) {
|
||||||
while (rs.next()) {
|
while (rs.next()) {
|
||||||
table.addPk(rs.getString("COLUMN_NAME"));
|
table.addPk(rs.getString("COLUMN_NAME"));
|
||||||
@ -243,7 +243,7 @@ public class MetaUtil {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 获得列
|
// 获得列
|
||||||
try (ResultSet rs = metaData.getColumns(catalog, schema, tableName, null)) {
|
try (final ResultSet rs = metaData.getColumns(catalog, schema, tableName, null)) {
|
||||||
if (null != rs) {
|
if (null != rs) {
|
||||||
while (rs.next()) {
|
while (rs.next()) {
|
||||||
table.setColumn(Column.create(table, rs));
|
table.setColumn(Column.create(table, rs));
|
||||||
@ -252,7 +252,7 @@ public class MetaUtil {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 获得索引信息(since 5.7.23)
|
// 获得索引信息(since 5.7.23)
|
||||||
try (ResultSet rs = metaData.getIndexInfo(catalog, schema, tableName, false, false)) {
|
try (final ResultSet rs = metaData.getIndexInfo(catalog, schema, tableName, false, false)) {
|
||||||
final Map<String, IndexInfo> indexInfoMap = new LinkedHashMap<>();
|
final Map<String, IndexInfo> indexInfoMap = new LinkedHashMap<>();
|
||||||
if (null != rs) {
|
if (null != rs) {
|
||||||
while (rs.next()) {
|
while (rs.next()) {
|
||||||
|
@ -2,6 +2,7 @@ package cn.hutool.db.meta;
|
|||||||
|
|
||||||
import cn.hutool.core.collection.CollectionUtil;
|
import cn.hutool.core.collection.CollectionUtil;
|
||||||
import cn.hutool.core.util.StrUtil;
|
import cn.hutool.core.util.StrUtil;
|
||||||
|
import cn.hutool.db.DbRuntimeException;
|
||||||
import cn.hutool.db.ds.DSFactory;
|
import cn.hutool.db.ds.DSFactory;
|
||||||
import org.junit.Assert;
|
import org.junit.Assert;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
@ -20,25 +21,34 @@ public class MetaUtilTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void getTablesTest() {
|
public void getTablesTest() {
|
||||||
List<String> tables = MetaUtil.getTables(ds);
|
final List<String> tables = MetaUtil.getTables(ds);
|
||||||
Assert.assertEquals("user", tables.get(0));
|
Assert.assertEquals("user", tables.get(0));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void getTableMetaTest() {
|
public void getTableMetaTest() {
|
||||||
Table table = MetaUtil.getTableMeta(ds, "user");
|
final Table table = MetaUtil.getTableMeta(ds, "user");
|
||||||
Assert.assertEquals(CollectionUtil.newHashSet("id"), table.getPkNames());
|
Assert.assertEquals(CollectionUtil.newHashSet("id"), table.getPkNames());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void getColumnNamesTest() {
|
public void getColumnNamesTest() {
|
||||||
String[] names = MetaUtil.getColumnNames(ds, "user");
|
final String[] names = MetaUtil.getColumnNames(ds, "user");
|
||||||
Assert.assertArrayEquals(StrUtil.splitToArray("id,name,age,birthday,gender", ','), names);
|
Assert.assertArrayEquals(StrUtil.splitToArray("id,name,age,birthday,gender", ','), names);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void getTableIndexInfoTest() {
|
public void getTableIndexInfoTest() {
|
||||||
Table table = MetaUtil.getTableMeta(ds, "user_1");
|
final Table table = MetaUtil.getTableMeta(ds, "user_1");
|
||||||
|
Assert.assertEquals(table.getIndexInfoList().size(), 2);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 表不存在抛出异常。
|
||||||
|
*/
|
||||||
|
@Test(expected = DbRuntimeException.class)
|
||||||
|
public void getTableNotExistTest() {
|
||||||
|
final Table table = MetaUtil.getTableMeta(ds, "user_not_exist");
|
||||||
Assert.assertEquals(table.getIndexInfoList().size(), 2);
|
Assert.assertEquals(table.getIndexInfoList().size(), 2);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -194,7 +194,7 @@ public final class SensitiveUtil {
|
|||||||
* @return 敏感词过滤处理后的bean对象
|
* @return 敏感词过滤处理后的bean对象
|
||||||
*/
|
*/
|
||||||
public static <T> T sensitiveFilter(T bean, boolean isGreedMatch, SensitiveProcessor sensitiveProcessor) {
|
public static <T> T sensitiveFilter(T bean, boolean isGreedMatch, SensitiveProcessor sensitiveProcessor) {
|
||||||
String jsonText = JSONUtil.toJsonStr(bean);
|
final String jsonText = JSONUtil.toJsonStr(bean);
|
||||||
@SuppressWarnings("unchecked") final Class<T> c = (Class<T>) bean.getClass();
|
@SuppressWarnings("unchecked") final Class<T> c = (Class<T>) bean.getClass();
|
||||||
return JSONUtil.toBean(sensitiveFilter(jsonText, isGreedMatch, sensitiveProcessor), c);
|
return JSONUtil.toBean(sensitiveFilter(jsonText, isGreedMatch, sensitiveProcessor), c);
|
||||||
}
|
}
|
||||||
@ -224,7 +224,7 @@ public final class SensitiveUtil {
|
|||||||
}
|
}
|
||||||
|
|
||||||
//敏感词过滤场景下,不需要密集匹配
|
//敏感词过滤场景下,不需要密集匹配
|
||||||
List<FoundWord> foundWordList = getFoundAllSensitive(text, true, isGreedMatch);
|
final List<FoundWord> foundWordList = getFoundAllSensitive(text, true, isGreedMatch);
|
||||||
if (CollUtil.isEmpty(foundWordList)) {
|
if (CollUtil.isEmpty(foundWordList)) {
|
||||||
return text;
|
return text;
|
||||||
}
|
}
|
||||||
@ -233,10 +233,10 @@ public final class SensitiveUtil {
|
|||||||
|
|
||||||
final Map<Integer, FoundWord> foundWordMap = new HashMap<>(foundWordList.size(), 1);
|
final Map<Integer, FoundWord> foundWordMap = new HashMap<>(foundWordList.size(), 1);
|
||||||
foundWordList.forEach(foundWord -> foundWordMap.put(foundWord.getStartIndex(), foundWord));
|
foundWordList.forEach(foundWord -> foundWordMap.put(foundWord.getStartIndex(), foundWord));
|
||||||
int length = text.length();
|
final int length = text.length();
|
||||||
StringBuilder textStringBuilder = new StringBuilder();
|
final StringBuilder textStringBuilder = new StringBuilder();
|
||||||
for (int i = 0; i < length; i++) {
|
for (int i = 0; i < length; i++) {
|
||||||
FoundWord fw = foundWordMap.get(i);
|
final FoundWord fw = foundWordMap.get(i);
|
||||||
if (fw != null) {
|
if (fw != null) {
|
||||||
textStringBuilder.append(sensitiveProcessor.process(fw));
|
textStringBuilder.append(sensitiveProcessor.process(fw));
|
||||||
i = fw.getEndIndex();
|
i = fw.getEndIndex();
|
||||||
|
Loading…
x
Reference in New Issue
Block a user