DMLShardingTablesOnlyTest.java 8.7 KB
Newer Older
T
terrymanu 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
/**
 * 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>
 */

package com.dangdang.ddframe.rdb.integrate.tbl;

import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

import org.dbunit.DatabaseUnitException;
import org.junit.Before;
import org.junit.Test;

import com.dangdang.ddframe.rdb.sharding.api.ShardingDataSource;

public final class DMLShardingTablesOnlyTest extends AbstractShardingTablesOnlyDBUnitTest {
    
    private ShardingDataSource shardingDataSource;
    
    @Before
    public void init() throws SQLException {
        shardingDataSource = getShardingDataSource();
    }
    
    @Test
    public void assertInsert() throws SQLException, DatabaseUnitException {
        String sql = "INSERT INTO `t_order` (`order_id`, `user_id`, `status`) VALUES (?, ?, ?)";
        for (int i = 1; i <= 10; i++) {
            try (Connection connection = shardingDataSource.getConnection()) {
T
terrymanu 已提交
47 48 49 50 51
                PreparedStatement preparedStatement = connection.prepareStatement(sql);
                preparedStatement.setInt(1, i);
                preparedStatement.setInt(2, i);
                preparedStatement.setString(3, "insert");
                preparedStatement.executeUpdate();
T
terrymanu 已提交
52 53
            }
        }
T
terrymanu 已提交
54
        assertDataSet("insert", "insert");
T
terrymanu 已提交
55 56 57
    }
    
    @Test
T
terrymanu 已提交
58
    public void assertInsertWithAllPlaceholders() throws SQLException, DatabaseUnitException {
T
terrymanu 已提交
59 60 61
        String sql = "INSERT INTO `t_order` (`order_id`, `user_id`, `status`) VALUES (?, ?, ?)";
        for (int i = 1; i <= 10; i++) {
            try (Connection connection = shardingDataSource.getConnection()) {
T
terrymanu 已提交
62 63 64 65 66
                PreparedStatement preparedStatement = connection.prepareStatement(sql);
                preparedStatement.setInt(1, i);
                preparedStatement.setInt(2, i);
                preparedStatement.setString(3, "insert");
                preparedStatement.executeUpdate();
T
terrymanu 已提交
67 68
            }
        }
T
terrymanu 已提交
69
        assertDataSet("insert", "insert");
T
terrymanu 已提交
70 71 72
    }
    
    @Test
T
terrymanu 已提交
73
    public void assertInsertWithoutPlaceholder() throws SQLException, DatabaseUnitException {
T
terrymanu 已提交
74 75 76
        String sql = "INSERT INTO `t_order` (`order_id`, `user_id`, `status`) VALUES (%s, %s, 'insert')";
        for (int i = 1; i <= 10; i++) {
            try (Connection connection = shardingDataSource.getConnection()) {
T
terrymanu 已提交
77 78
                PreparedStatement preparedStatement = connection.prepareStatement(String.format(sql, i, i));
                preparedStatement.executeUpdate();
T
terrymanu 已提交
79 80
            }
        }
T
terrymanu 已提交
81
        assertDataSet("insert", "insert");
T
terrymanu 已提交
82 83 84
    }
    
    @Test
T
terrymanu 已提交
85
    public void assertInsertWithPlaceholdersForShardingKeys() throws SQLException, DatabaseUnitException {
T
terrymanu 已提交
86 87 88
        String sql = "INSERT INTO `t_order` (`order_id`, `user_id`, `status`) VALUES (%s, %s, ?)";
        for (int i = 1; i <= 10; i++) {
            try (Connection connection = shardingDataSource.getConnection()) {
T
terrymanu 已提交
89 90 91
                PreparedStatement preparedStatement = connection.prepareStatement(String.format(sql, i, i));
                preparedStatement.setString(1, "insert");
                preparedStatement.executeUpdate();
T
terrymanu 已提交
92 93
            }
        }
T
terrymanu 已提交
94
        assertDataSet("insert", "insert");
T
terrymanu 已提交
95 96 97
    }
    
    @Test
T
terrymanu 已提交
98
    public void assertInsertWithPlaceholdersForNotShardingKeys() throws SQLException, DatabaseUnitException {
T
terrymanu 已提交
99 100 101
        String sql = "INSERT INTO `t_order` (`order_id`, `user_id`, `status`) VALUES (%s, %s, ?)";
        for (int i = 1; i <= 10; i++) {
            try (Connection connection = shardingDataSource.getConnection()) {
T
terrymanu 已提交
102 103 104
                PreparedStatement preparedStatement = connection.prepareStatement(String.format(sql, i, i));
                preparedStatement.setString(1, "insert");
                preparedStatement.executeUpdate();
T
terrymanu 已提交
105 106
            }
        }
T
terrymanu 已提交
107
        assertDataSet("insert", "insert");
T
terrymanu 已提交
108 109 110 111 112 113 114 115 116
    }
    
    @Test
    public void assertUpdateWithoutAlias() throws SQLException, DatabaseUnitException {
        ShardingDataSource shardingDataSource = getShardingDataSource();
        String sql = "UPDATE `t_order` SET `status` = ? WHERE `order_id` = ? AND `user_id` = ?";
        for (int i = 10; i < 12; i++) {
            for (int j = 0; j < 10; j++) {
                try (Connection connection = shardingDataSource.getConnection()) {
T
terrymanu 已提交
117 118 119 120 121
                    PreparedStatement preparedStatement = connection.prepareStatement(sql);
                    preparedStatement.setString(1, "updated");
                    preparedStatement.setInt(2, i * 100 + j);
                    preparedStatement.setInt(3, i);
                    assertThat(preparedStatement.executeUpdate(), is(1));
T
terrymanu 已提交
122 123 124
                }
            }
        }
T
terrymanu 已提交
125
        assertDataSet("update", "updated");
T
terrymanu 已提交
126 127 128 129 130 131 132 133 134
    }
    
    @Test
    public void assertUpdateWithAlias() throws SQLException, DatabaseUnitException {
        ShardingDataSource shardingDataSource = getShardingDataSource();
        String sql = "UPDATE `t_order` as o SET o.`status` = ? WHERE o.`order_id` = ? AND o.`user_id` = ?";
        for (int i = 10; i < 12; i++) {
            for (int j = 0; j < 10; j++) {
                try (Connection connection = shardingDataSource.getConnection()) {
T
terrymanu 已提交
135 136 137 138 139
                    PreparedStatement preparedStatement = connection.prepareStatement(sql);
                    preparedStatement.setString(1, "updated");
                    preparedStatement.setInt(2, i * 100 + j);
                    preparedStatement.setInt(3, i);
                    assertThat(preparedStatement.executeUpdate(), is(1));
T
terrymanu 已提交
140 141 142
                }
            }
        }
T
terrymanu 已提交
143
        assertDataSet("update", "updated");
T
terrymanu 已提交
144 145 146 147 148 149 150
    }
    
    @Test
    public void assertUpdateWithoutShardingValue() throws SQLException, DatabaseUnitException {
        ShardingDataSource shardingDataSource = getShardingDataSource();
        String sql = "UPDATE `t_order` SET `status` = ? WHERE `status` = ?";
        try (Connection connection = shardingDataSource.getConnection()) {
T
terrymanu 已提交
151 152 153 154
            PreparedStatement preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1, "updated");
            preparedStatement.setString(2, "init");
            assertThat(preparedStatement.executeUpdate(), is(20));
T
terrymanu 已提交
155
        }
T
terrymanu 已提交
156
        assertDataSet("update", "updated");
T
terrymanu 已提交
157 158 159 160 161 162 163 164 165
    }
    
    @Test
    public void assertDeleteWithoutAlias() throws SQLException, DatabaseUnitException {
        ShardingDataSource shardingDataSource = getShardingDataSource();
        String sql = "DELETE `t_order` WHERE `order_id` = ? AND `user_id` = ? AND `status` = ?";
        for (int i = 10; i < 12; i++) {
            for (int j = 0; j < 10; j++) {
                try (Connection connection = shardingDataSource.getConnection()) {
T
terrymanu 已提交
166 167 168 169 170
                    PreparedStatement preparedStatement = connection.prepareStatement(sql);
                    preparedStatement.setInt(1, i * 100 + j);
                    preparedStatement.setInt(2, i);
                    preparedStatement.setString(3, "init");
                    assertThat(preparedStatement.executeUpdate(), is(1));
T
terrymanu 已提交
171 172 173
                }
            }   
        }
T
terrymanu 已提交
174
        assertDataSet("delete", "init");
T
terrymanu 已提交
175 176 177 178 179 180 181
    }
    
    @Test
    public void assertDeleteWithoutShardingValue() throws SQLException, DatabaseUnitException {
        ShardingDataSource shardingDataSource = getShardingDataSource();
        String sql = "DELETE `t_order` WHERE `status` = ?";
        try (Connection connection = shardingDataSource.getConnection()) {
T
terrymanu 已提交
182 183 184
            PreparedStatement preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1, "init");
            assertThat(preparedStatement.executeUpdate(), is(20));
T
terrymanu 已提交
185
        }
T
terrymanu 已提交
186
        assertDataSet("delete", "init");
T
terrymanu 已提交
187 188
    }
    
T
terrymanu 已提交
189
    private void assertDataSet(final String expectedDataSetPattern, final String status) throws SQLException, DatabaseUnitException {
T
terrymanu 已提交
190
        for (int i = 0; i < 10; i++) {
T
terrymanu 已提交
191
            assertDataSet(String.format("integrate/dataset/tbl/expect/%s/db_single.xml", expectedDataSetPattern), 
T
terrymanu 已提交
192 193 194 195 196
                    shardingDataSource.getConnection().getConnection("dataSource_db_single"), 
                    String.format("t_order_%s", i), String.format("SELECT * FROM `t_order_%s` WHERE `status`=?", i), status);
        }
    }
}