From f13497a099dcc006b1a12b66150a7cb42c74d2c1 Mon Sep 17 00:00:00 2001 From: Zhengqiang Duan Date: Thu, 20 Oct 2022 20:15:40 +0800 Subject: [PATCH] Revise pr 21603 (#21658) * Revise pr 21603 * fix unit test --- .../ShardingAutoTableAlgorithmUtilTest.java | 50 ++++++++----------- 1 file changed, 20 insertions(+), 30 deletions(-) diff --git a/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/algorithm/sharding/ShardingAutoTableAlgorithmUtilTest.java b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/algorithm/sharding/ShardingAutoTableAlgorithmUtilTest.java index 272be69b13bf8..815f341a46757 100644 --- a/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/algorithm/sharding/ShardingAutoTableAlgorithmUtilTest.java +++ b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/algorithm/sharding/ShardingAutoTableAlgorithmUtilTest.java @@ -17,52 +17,42 @@ package org.apache.shardingsphere.sharding.algorithm.sharding; -import static org.hamcrest.CoreMatchers.is; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertThat; -import static org.junit.Assert.assertTrue; - -import java.util.ArrayList; -import java.util.Collection; -import java.util.Optional; import org.apache.shardingsphere.infra.datanode.DataNodeInfo; import org.junit.Before; import org.junit.Test; +import java.util.Collection; +import java.util.LinkedList; +import java.util.Optional; + +import static org.hamcrest.CoreMatchers.is; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + public final class ShardingAutoTableAlgorithmUtilTest { - private Collection collection; + private final Collection availableTargetNames = new LinkedList<>(); - private DataNodeInfo dataNodeInfo; + private final DataNodeInfo dataNodeInfo = new DataNodeInfo("t_order_", 2, '0'); @Before public void setup() { - collection = new ArrayList<>(); - collection.add("PREFIX----SUFFIX"); - collection.add("PREFIXSUFFIXSTRING"); - collection.add("PREFIX----------"); - String prefix = "PREFIX"; - int suffixMinLength = 10; - char paddingChar = '-'; - dataNodeInfo = new DataNodeInfo(prefix, suffixMinLength, paddingChar); + availableTargetNames.add("t_order_00"); + availableTargetNames.add("t_order_01"); + availableTargetNames.add("t_order_02"); } @Test - public void assertFindMatchedTargetNameForValidInputs() { - Optional output = ShardingAutoTableAlgorithmUtil.findMatchedTargetName(collection, "SUFFIX", dataNodeInfo); - assertTrue(output.isPresent()); - assertThat("PREFIX----SUFFIX", is(output.get())); - Optional output1 = ShardingAutoTableAlgorithmUtil.findMatchedTargetName(collection, "SUFFIXSTRING", dataNodeInfo); - assertTrue(output1.isPresent()); - assertThat("PREFIXSUFFIXSTRING", is(output1.get())); - Optional output2 = ShardingAutoTableAlgorithmUtil.findMatchedTargetName(collection, "", dataNodeInfo); - assertTrue(output2.isPresent()); - assertThat("PREFIX----------", is(output2.get())); + public void assertFindMatchedTargetNameWhenTableExist() { + Optional actual = ShardingAutoTableAlgorithmUtil.findMatchedTargetName(availableTargetNames, "2", dataNodeInfo); + assertTrue(actual.isPresent()); + assertThat(actual.get(), is("t_order_02")); } @Test - public void assertFindMatchedTargetNameNonExistingInput() { - Optional output = ShardingAutoTableAlgorithmUtil.findMatchedTargetName(collection, "NONEXISTINGSUFFIX", dataNodeInfo); + public void assertFindMatchedTargetNameWhenTableNotExist() { + Optional output = ShardingAutoTableAlgorithmUtil.findMatchedTargetName(availableTargetNames, "3", dataNodeInfo); assertFalse(output.isPresent()); } }