SingleRouterUtilTest.java 2.8 KB
Newer Older
T
terrymanu 已提交
1
/*
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
 * Copyright 1999-2015 dangdang.com.
 * <p>
 * Licensed 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.
 * </p>
 */

T
terrymanu 已提交
18
package com.dangdang.ddframe.rdb.sharding.routing.type.single;
19 20

import com.dangdang.ddframe.rdb.sharding.api.ShardingValue;
T
terrymanu 已提交
21
import com.dangdang.ddframe.rdb.sharding.constant.ShardingOperator;
T
terrymanu 已提交
22
import com.dangdang.ddframe.rdb.sharding.parsing.parser.context.ConditionContext;
23
import com.dangdang.ddframe.rdb.sharding.parsing.parser.context.ShardingColumn;
24 25
import org.junit.Test;

T
terrymanu 已提交
26
import java.util.Collections;
27 28
import java.util.Iterator;

29 30 31 32 33 34 35
import static org.hamcrest.core.Is.is;
import static org.junit.Assert.assertThat;

public class SingleRouterUtilTest {
    
    @Test
    public void testConvertConditionToShardingValue() throws Exception {
36
        ConditionContext.Condition condition = new ConditionContext.Condition(new ShardingColumn("test", "test"), ShardingOperator.EQUAL);
37
        condition.getValues().add(1);
T
terrymanu 已提交
38
        ShardingValue<?> shardingValue = SingleRouterUtil.convertConditionToShardingValue(condition, Collections.emptyList());
39 40
        assertThat(shardingValue.getType(), is(ShardingValue.ShardingValueType.SINGLE));
        assertThat((Integer) shardingValue.getValue(), is(1));
41
        condition = new ConditionContext.Condition(new ShardingColumn("test", "test"), ShardingOperator.IN);
42 43
        condition.getValues().add(1);
        condition.getValues().add(2);
T
terrymanu 已提交
44
        shardingValue = SingleRouterUtil.convertConditionToShardingValue(condition, Collections.emptyList());
45
        assertThat(shardingValue.getType(), is(ShardingValue.ShardingValueType.LIST));
T
terrymanu 已提交
46 47 48
        Iterator<?> iterator = shardingValue.getValues().iterator();
        assertThat((Integer) iterator.next(), is(1));
        assertThat((Integer) iterator.next(), is(2));
49
        condition = new ConditionContext.Condition(new ShardingColumn("test", "test"), ShardingOperator.BETWEEN);
50 51
        condition.getValues().add(1);
        condition.getValues().add(2);
T
terrymanu 已提交
52
        shardingValue = SingleRouterUtil.convertConditionToShardingValue(condition, Collections.emptyList());
53 54 55 56
        assertThat(shardingValue.getType(), is(ShardingValue.ShardingValueType.RANGE));
        assertThat((Integer) shardingValue.getValueRange().lowerEndpoint(), is(1));
        assertThat((Integer) shardingValue.getValueRange().upperEndpoint(), is(2));
    }
T
terrymanu 已提交
57
}