OrchestrationSpringBootMasterSlaveTest.java 3.9 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/*
 * 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.
 */

18
package org.apache.shardingsphere.spring.boot.orchestration.type;
19 20 21

import lombok.SneakyThrows;
import org.apache.commons.dbcp2.BasicDataSource;
22 23
import org.apache.shardingsphere.driver.jdbc.core.datasource.ShardingSphereDataSource;
import org.apache.shardingsphere.driver.orchestration.internal.datasource.OrchestrationShardingSphereDataSource;
24
import org.apache.shardingsphere.spring.boot.orchestration.util.EmbedTestingServer;
25
import org.apache.shardingsphere.infra.rule.ShardingSphereRule;
L
Liang Zhang 已提交
26 27
import org.apache.shardingsphere.masterslave.rule.MasterSlaveDataSourceRule;
import org.apache.shardingsphere.masterslave.rule.MasterSlaveRule;
28 29 30 31 32 33 34 35
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

36 37 38
import javax.annotation.Resource;
import javax.sql.DataSource;
import java.lang.reflect.Field;
39
import java.util.Collection;
40

41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;

@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = OrchestrationSpringBootMasterSlaveTest.class)
@SpringBootApplication
@ActiveProfiles("masterslave")
public class OrchestrationSpringBootMasterSlaveTest {
    
    @Resource
    private DataSource dataSource;
    
    @BeforeClass
    public static void init() {
        EmbedTestingServer.start();
    }
    
    @Test
60
    @SneakyThrows(ReflectiveOperationException.class)
61
    public void assertDataSource() {
62 63
        assertTrue(dataSource instanceof OrchestrationShardingSphereDataSource);
        Field field = OrchestrationShardingSphereDataSource.class.getDeclaredField("dataSource");
64
        field.setAccessible(true);
65 66
        ShardingSphereDataSource shardingSphereDataSource = (ShardingSphereDataSource) field.get(dataSource);
        for (DataSource each : shardingSphereDataSource.getDataSourceMap().values()) {
67
            assertThat(((BasicDataSource) each).getMaxTotal(), is(16));
M
menghaoranss 已提交
68
            assertThat(((BasicDataSource) each).getUsername(), is("sa"));
69
        }
J
Juan Pan(Trista) 已提交
70
        Collection<ShardingSphereRule> rules = shardingSphereDataSource.getSchemaContexts().getDefaultSchemaContext().getSchema().getRules();
71 72 73 74 75
        assertThat(rules.size(), is(1));
        assertMasterSlaveRule((MasterSlaveRule) rules.iterator().next());
    }
    
    private void assertMasterSlaveRule(final MasterSlaveRule rule) {
76 77 78 79 80 81 82
        MasterSlaveDataSourceRule dataSourceRule = rule.getSingleDataSourceRule();
        assertThat(dataSourceRule.getName(), is("ds_ms"));
        assertThat(dataSourceRule.getName(), is("ds_ms"));
        assertThat(dataSourceRule.getMasterDataSourceName(), is("ds_master"));
        assertThat(dataSourceRule.getSlaveDataSourceNames().size(), is(2));
        assertThat(dataSourceRule.getSlaveDataSourceNames().get(0), is("ds_slave_0"));
        assertThat(dataSourceRule.getSlaveDataSourceNames().get(1), is("ds_slave_1"));
83 84
    }
}