From 380e389a14c64d2901d931b82a63a19d3d734ae3 Mon Sep 17 00:00:00 2001 From: jiangML <1060319118@qq.com> Date: Fri, 17 Nov 2023 14:13:27 +0800 Subject: [PATCH] Optimize PropertiesConverter --- .../distsql/handler/query/ShowEncryptRuleExecutorTest.java | 2 +- .../distsql/handler/query/ShowMaskRuleExecutorTest.java | 2 +- .../shadow/distsql/query/ShowShadowRuleExecutorTest.java | 4 ++-- .../distsql/query/ShowShardingTableRuleExecutorTest.java | 2 +- .../query/ShowUnusedShardingKeyGeneratorExecutorTest.java | 2 +- .../shardingsphere/infra/props/PropertiesConverter.java | 6 +++++- .../shardingsphere/infra/props/PropertiesConverterTest.java | 2 +- .../distsql/handler/query/ShowTrafficRuleExecutorTest.java | 6 +++--- 8 files changed, 15 insertions(+), 11 deletions(-) diff --git a/features/encrypt/distsql/handler/src/test/java/org/apache/shardingsphere/encrypt/distsql/handler/query/ShowEncryptRuleExecutorTest.java b/features/encrypt/distsql/handler/src/test/java/org/apache/shardingsphere/encrypt/distsql/handler/query/ShowEncryptRuleExecutorTest.java index a334774dc1d99..02d4d7ff7e317 100644 --- a/features/encrypt/distsql/handler/src/test/java/org/apache/shardingsphere/encrypt/distsql/handler/query/ShowEncryptRuleExecutorTest.java +++ b/features/encrypt/distsql/handler/src/test/java/org/apache/shardingsphere/encrypt/distsql/handler/query/ShowEncryptRuleExecutorTest.java @@ -58,7 +58,7 @@ void assertGetRowData() { assertThat(row.getCell(4), is("user_assisted")); assertThat(row.getCell(5), is("user_like")); assertThat(row.getCell(6), is("md5")); - assertThat(row.getCell(7), is("{}")); + assertThat(row.getCell(7), is("")); assertThat(row.getCell(8), is("")); assertThat(row.getCell(9), is("")); assertThat(row.getCell(10), is("")); diff --git a/features/mask/distsql/handler/src/test/java/org/apache/shardingsphere/mask/distsql/handler/query/ShowMaskRuleExecutorTest.java b/features/mask/distsql/handler/src/test/java/org/apache/shardingsphere/mask/distsql/handler/query/ShowMaskRuleExecutorTest.java index 096704a189fdc..a6e59fc54e54f 100644 --- a/features/mask/distsql/handler/src/test/java/org/apache/shardingsphere/mask/distsql/handler/query/ShowMaskRuleExecutorTest.java +++ b/features/mask/distsql/handler/src/test/java/org/apache/shardingsphere/mask/distsql/handler/query/ShowMaskRuleExecutorTest.java @@ -55,7 +55,7 @@ void assertGetRowData() { assertThat(row.getCell(1), is("t_mask")); assertThat(row.getCell(2), is("user_id")); assertThat(row.getCell(3), is("md5")); - assertThat(row.getCell(4), is("{}")); + assertThat(row.getCell(4), is("")); } @Test diff --git a/features/shadow/distsql/handler/src/test/java/org/apache/shardingsphere/shadow/distsql/query/ShowShadowRuleExecutorTest.java b/features/shadow/distsql/handler/src/test/java/org/apache/shardingsphere/shadow/distsql/query/ShowShadowRuleExecutorTest.java index 91270ddc484db..593cabbd9f8fe 100644 --- a/features/shadow/distsql/handler/src/test/java/org/apache/shardingsphere/shadow/distsql/query/ShowShadowRuleExecutorTest.java +++ b/features/shadow/distsql/handler/src/test/java/org/apache/shardingsphere/shadow/distsql/query/ShowShadowRuleExecutorTest.java @@ -56,14 +56,14 @@ void assertGetRowData() { assertThat(row.getCell(3), is("source")); assertThat(row.getCell(4), is("shadow")); assertThat(row.getCell(5), is("REGEX_MATCH")); - assertThat(row.getCell(6), is("{}")); + assertThat(row.getCell(6), is("")); row = iterator.next(); assertThat(row.getCell(1), is("t_order_item")); assertThat(row.getCell(2), is("shadow_rule")); assertThat(row.getCell(3), is("source")); assertThat(row.getCell(4), is("shadow")); assertThat(row.getCell(5), is("REGEX_MATCH")); - assertThat(row.getCell(6), is("{}")); + assertThat(row.getCell(6), is("")); } @Test diff --git a/features/sharding/distsql/handler/src/test/java/org/apache/shardingsphere/sharding/distsql/query/ShowShardingTableRuleExecutorTest.java b/features/sharding/distsql/handler/src/test/java/org/apache/shardingsphere/sharding/distsql/query/ShowShardingTableRuleExecutorTest.java index 63e737d7de863..353d56c86e958 100644 --- a/features/sharding/distsql/handler/src/test/java/org/apache/shardingsphere/sharding/distsql/query/ShowShardingTableRuleExecutorTest.java +++ b/features/sharding/distsql/handler/src/test/java/org/apache/shardingsphere/sharding/distsql/query/ShowShardingTableRuleExecutorTest.java @@ -69,7 +69,7 @@ void assertGetRowData() { assertThat(row.getCell(11), is("{\"algorithm-expression\":\"t_order_${order_id % 2}\"}")); assertThat(row.getCell(12), is("order_id")); assertThat(row.getCell(13), is("SNOWFLAKE")); - assertThat(row.getCell(14), is("{}")); + assertThat(row.getCell(14), is("")); assertThat(row.getCell(15), is("DML_SHARDING_CONDITIONS")); assertThat(row.getCell(16), is("true")); } diff --git a/features/sharding/distsql/handler/src/test/java/org/apache/shardingsphere/sharding/distsql/query/ShowUnusedShardingKeyGeneratorExecutorTest.java b/features/sharding/distsql/handler/src/test/java/org/apache/shardingsphere/sharding/distsql/query/ShowUnusedShardingKeyGeneratorExecutorTest.java index 40b33b7bce985..ea1b40a2c3a3d 100644 --- a/features/sharding/distsql/handler/src/test/java/org/apache/shardingsphere/sharding/distsql/query/ShowUnusedShardingKeyGeneratorExecutorTest.java +++ b/features/sharding/distsql/handler/src/test/java/org/apache/shardingsphere/sharding/distsql/query/ShowUnusedShardingKeyGeneratorExecutorTest.java @@ -53,7 +53,7 @@ void assertGetRowData() { LocalDataQueryResultRow row = iterator.next(); assertThat(row.getCell(1), is("uuid_key_generator")); assertThat(row.getCell(2), is("UUID")); - assertThat(row.getCell(3), is("{}")); + assertThat(row.getCell(3), is("")); } @Test diff --git a/infra/common/src/main/java/org/apache/shardingsphere/infra/props/PropertiesConverter.java b/infra/common/src/main/java/org/apache/shardingsphere/infra/props/PropertiesConverter.java index b795f47860316..bc3fc0a88f532 100644 --- a/infra/common/src/main/java/org/apache/shardingsphere/infra/props/PropertiesConverter.java +++ b/infra/common/src/main/java/org/apache/shardingsphere/infra/props/PropertiesConverter.java @@ -21,6 +21,8 @@ import lombok.NoArgsConstructor; import org.apache.shardingsphere.infra.util.json.JsonUtils; +import java.util.LinkedHashMap; +import java.util.Map; import java.util.Properties; /** @@ -36,6 +38,8 @@ public final class PropertiesConverter { * @return converted string content */ public static String convert(final Properties props) { - return JsonUtils.toJsonString(props); + Map sortedProps = new LinkedHashMap<>(); + props.keySet().stream().map(Object::toString).sorted().forEach(each -> sortedProps.put(each, props.get(each))); + return sortedProps.isEmpty() ? "" : JsonUtils.toJsonString(sortedProps); } } diff --git a/infra/common/src/test/java/org/apache/shardingsphere/infra/props/PropertiesConverterTest.java b/infra/common/src/test/java/org/apache/shardingsphere/infra/props/PropertiesConverterTest.java index 657899a3290bc..3b52f18f0d170 100644 --- a/infra/common/src/test/java/org/apache/shardingsphere/infra/props/PropertiesConverterTest.java +++ b/infra/common/src/test/java/org/apache/shardingsphere/infra/props/PropertiesConverterTest.java @@ -35,6 +35,6 @@ void assertConvert() { @Test void assertConvertEmptyProperties() { - assertThat(PropertiesConverter.convert(new Properties()), is("{}")); + assertThat(PropertiesConverter.convert(new Properties()), is("")); } } diff --git a/kernel/traffic/distsql/handler/src/test/java/org/apache/shardingsphere/traffic/distsql/handler/query/ShowTrafficRuleExecutorTest.java b/kernel/traffic/distsql/handler/src/test/java/org/apache/shardingsphere/traffic/distsql/handler/query/ShowTrafficRuleExecutorTest.java index 95f8d5aff0e99..0fc8570d3f4ae 100644 --- a/kernel/traffic/distsql/handler/src/test/java/org/apache/shardingsphere/traffic/distsql/handler/query/ShowTrafficRuleExecutorTest.java +++ b/kernel/traffic/distsql/handler/src/test/java/org/apache/shardingsphere/traffic/distsql/handler/query/ShowTrafficRuleExecutorTest.java @@ -58,14 +58,14 @@ void assertExecute() { assertThat(row.getCell(3), is("SQL_MATCH")); assertThat(row.getCell(4), is("{\"sql\":\"select * from t_order\"}")); assertThat(row.getCell(5), is("RANDOM")); - assertThat(row.getCell(6), is("{}")); + assertThat(row.getCell(6), is("")); row = iterator.next(); assertThat(row.getCell(1), is("rule_name_2")); assertThat(row.getCell(2), is("oltp")); assertThat(row.getCell(3), is("SQL_HINT")); - assertThat(row.getCell(4), is("{}")); + assertThat(row.getCell(4), is("")); assertThat(row.getCell(5), is("ROBIN")); - assertThat(row.getCell(6), is("{}")); + assertThat(row.getCell(6), is("")); } @Test