/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.apache.shardingsphere.sharding.route.engine.condition;

import org.apache.shardingsphere.infra.binder.statement.SQLStatementContext;
import org.apache.shardingsphere.infra.binder.statement.dml.SelectStatementContext;
import org.apache.shardingsphere.sharding.route.engine.condition.value.ListShardingConditionValue;
import org.apache.shardingsphere.sharding.route.engine.condition.value.ShardingConditionValue;
import org.apache.shardingsphere.sharding.rule.ShardingRule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.junit.MockitoJUnitRunner;

import java.util.Collections;
import java.util.LinkedList;

import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
import static org.mockito.Mockito.mock;

@RunWith(MockitoJUnitRunner.class)
public final class ShardingConditionsTest {

    @Test
    public void assertIsAlwaysFalse() {
        ShardingConditions shardingConditions = new ShardingConditions(Collections.emptyList(), mock(SQLStatementContext.class), mock(ShardingRule.class));
        assertFalse(shardingConditions.isAlwaysFalse());
    }

    @Test
    public void assertIsAlwaysFalseTrue() {
        ShardingConditions shardingConditions = createSingleShardingConditions("t_order");
        assertTrue(shardingConditions.isAlwaysFalse());
    }

    @Test
    public void assertIsNeedMerge() {
        assertFalse(createSingleShardingConditions("t_order").isNeedMerge());
    }

    @Test
    public void isSameShardingConditionTrue() {
        ShardingConditions shardingConditions = createSingleShardingConditions("t_order");
        assertTrue(shardingConditions.isSameShardingCondition());
    }

    @Test
    public void isSameShardingConditionFalse() {
        ShardingConditions shardingConditions = createMultipleShardingConditions("t_order");
        assertFalse(shardingConditions.isSameShardingCondition());
    }

    @Test
    public void assertMerge() {
        createSingleShardingConditions("t_order").merge();
    }

    private ShardingConditions createSingleShardingConditions(final String tableName) {
        LinkedList<ShardingCondition> result = new LinkedList<>();
        ShardingConditionValue shardingConditionValue = new ListShardingConditionValue<>("order_id", tableName, Collections.singleton(1L));
        AlwaysFalseShardingCondition shardingCondition = new AlwaysFalseShardingCondition();
        shardingCondition.getValues().add(shardingConditionValue);
        result.add(shardingCondition);
        SelectStatementContext sqlStatementContext = mock(SelectStatementContext.class, RETURNS_DEEP_STUBS);
        return new ShardingConditions(result, sqlStatementContext, mock(ShardingRule.class));
    }

    private ShardingConditions createMultipleShardingConditions(final String tableName) {
        LinkedList<ShardingCondition> result = new LinkedList<>();
        ShardingConditionValue shardingConditionValue1 = new ListShardingConditionValue<>("user_id", tableName, Collections.singleton(1L));
        ShardingConditionValue shardingConditionValue2 = new ListShardingConditionValue<>("order_id", tableName, Collections.singleton(1L));
        ShardingCondition shardingCondition = new ShardingCondition();
        shardingCondition.getValues().add(shardingConditionValue1);
        ShardingCondition shardingCondition2 = new ShardingCondition();
        shardingCondition2.getValues().add(shardingConditionValue2);
        result.add(shardingCondition);
        result.add(shardingCondition2);
        SelectStatementContext sqlStatementContext = mock(SelectStatementContext.class, RETURNS_DEEP_STUBS);
        return new ShardingConditions(result, sqlStatementContext, mock(ShardingRule.class));
    }
}

@@ -1464,32 +1464,119 @@
 org.
-junit.Assert.assertFalse
+hamcrest.CoreMatchers.is;%0Aimport static org.junit.Assert.assertFalse;%0Aimport static org.junit.Assert.assertThat
 ;%0Aim
@@ -1790,16 +1790,20 @@
 sTest %7B%0A
+    
 %0A    @Te
@@ -2056,32 +2056,36 @@
 False());%0A    %7D%0A
+    
 %0A    @Test%0A    p
@@ -2198,33 +2198,24 @@
 gConditions(
-%22t_order%22
 );%0A        a
@@ -2259,32 +2259,36 @@
 False());%0A    %7D%0A
+    
 %0A    @Test%0A    p
@@ -2371,25 +2371,16 @@
 ditions(
-%22t_order%22
 ).isNeed
@@ -2387,32 +2387,36 @@
 Merge());%0A    %7D%0A
+    
 %0A    @Test%0A    p
@@ -2533,33 +2533,24 @@
 gConditions(
-%22t_order%22
 );%0A        a
@@ -2604,32 +2604,36 @@
 ition());%0A    %7D%0A
+    
 %0A    @Test%0A    p
@@ -2757,25 +2757,16 @@
 ditions(
-%22t_order%22
 );%0A     
@@ -2829,24 +2829,28 @@
 n());%0A    %7D%0A
+    
 %0A    @Test%0A 
@@ -2892,56 +2892,211 @@
     
-createSingleShardingConditions(%22t_order%22).merge(
+ShardingConditions multipleShardingConditions = createMultipleShardingConditions();%0A        multipleShardingConditions.merge();%0A        assertThat(multipleShardingConditions.getConditions().size(), is(2)
 );%0A 
@@ -3092,32 +3092,36 @@
 , is(2));%0A    %7D%0A
+    
 %0A    private Sha
@@ -3167,38 +3167,16 @@
 ditions(
-final String tableName
 ) %7B%0A    
@@ -3335,33 +3335,33 @@
 %22order_id%22, 
-tableName
+%22t_order%22
 , Collection
@@ -3786,16 +3786,20 @@
 ;%0A    %7D%0A
+    
 %0A    pri
@@ -3859,30 +3859,8 @@
 ons(
-final String tableName
 ) %7B%0A
@@ -4023,33 +4023,33 @@
 (%22user_id%22, 
-tableName
+%22t_order%22
 , Collection
@@ -4172,17 +4172,17 @@
 d%22, 
-tableName
+%22t_order%22
 , Co

Revise #15130 (#15372)