Skip to content

Commit

Permalink
Fixes checkstyle
Browse files Browse the repository at this point in the history
  • Loading branch information
linghengqian committed Nov 12, 2023
1 parent cfa4286 commit b64db8e
Show file tree
Hide file tree
Showing 13 changed files with 194 additions and 74 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,16 @@
*/
public class FileTestUtils {

/**
* read file From file URL string.
* @param fileUrl fileUrl
* @return byte array
*/
public static byte[] readFromFileURLString(final String fileUrl) {
return readInputStream(ClassLoader.getSystemResourceAsStream(fileUrl)).getBytes(StandardCharsets.UTF_8);
}

private static String readInputStream(InputStream is) {
private static String readInputStream(final InputStream is) {
StringBuilder out = new StringBuilder();
try (
InputStreamReader streamReader = new InputStreamReader(is, StandardCharsets.UTF_8);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,18 +49,14 @@ public final class EncryptTest {
private AddressRepository addressRepository;

@Test
public void testEncryptInLocalTransactions() throws SQLException, IOException {
try {
DataSource dataSource = YamlShardingSphereDataSourceFactory.createDataSource(FileTestUtils.readFromFileURLString("yaml/encrypt.yaml"));
orderRepository = new OrderRepository(dataSource);
orderItemRepository = new OrderItemRepository(dataSource);
addressRepository = new AddressRepository(dataSource);
this.initEnvironment();
this.processSuccess();
this.cleanEnvironment();
} finally {
// this.cleanEnvironment();
}
void testEncryptInLocalTransactions() throws SQLException, IOException {
DataSource dataSource = YamlShardingSphereDataSourceFactory.createDataSource(FileTestUtils.readFromFileURLString("yaml/encrypt.yaml"));
orderRepository = new OrderRepository(dataSource);
orderItemRepository = new OrderItemRepository(dataSource);
addressRepository = new AddressRepository(dataSource);
this.initEnvironment();
this.processSuccess();
this.cleanEnvironment();
}

private void initEnvironment() throws SQLException {
Expand All @@ -73,7 +69,7 @@ private void initEnvironment() throws SQLException {
}

private void processSuccess() throws SQLException {
List<Long> orderIds = insertData();
final List<Long> orderIds = insertData();
assertThat(orderRepository.selectAll(),
equalTo(IntStream.range(1, 11).mapToObj(i -> new Order(i, i % 2, i, i, "INSERT_TEST")).collect(Collectors.toList())));
assertThat(orderItemRepository.selectAll(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,18 +48,14 @@ public final class MaskTest {
private AddressRepository addressRepository;

@Test
public void testMaskInLocalTransactions() throws SQLException, IOException {
try {
DataSource dataSource = YamlShardingSphereDataSourceFactory.createDataSource(FileTestUtils.readFromFileURLString("yaml/mask.yaml"));
orderRepository = new OrderRepository(dataSource);
orderItemRepository = new OrderItemRepository(dataSource);
addressRepository = new AddressRepository(dataSource);
this.initEnvironment();
this.processSuccess();
this.cleanEnvironment();
} finally {
// this.cleanEnvironment();
}
void testMaskInLocalTransactions() throws SQLException, IOException {
DataSource dataSource = YamlShardingSphereDataSourceFactory.createDataSource(FileTestUtils.readFromFileURLString("yaml/mask.yaml"));
orderRepository = new OrderRepository(dataSource);
orderItemRepository = new OrderItemRepository(dataSource);
addressRepository = new AddressRepository(dataSource);
this.initEnvironment();
this.processSuccess();
this.cleanEnvironment();
}

private void initEnvironment() throws SQLException {
Expand All @@ -72,7 +68,7 @@ private void initEnvironment() throws SQLException {
}

private void processSuccess() throws SQLException {
List<Long> orderIds = insertData();
final List<Long> orderIds = insertData();
assertThat(orderRepository.selectAll(),
equalTo(IntStream.range(1, 11).mapToObj(i -> new Order(i, i % 2, i, i, "INSERT_TEST")).collect(Collectors.toList())));
assertThat(orderItemRepository.selectAll(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,18 +49,14 @@ public final class ReadWriteSplittingTest {
private AddressRepository addressRepository;

@Test
public void testReadWriteSplittingInLocalTransactions() throws SQLException, IOException {
try {
DataSource dataSource = YamlShardingSphereDataSourceFactory.createDataSource(FileTestUtils.readFromFileURLString("yaml/readwrite-splitting.yaml"));
orderRepository = new OrderRepository(dataSource);
orderItemRepository = new OrderItemRepository(dataSource);
addressRepository = new AddressRepository(dataSource);
this.initEnvironment();
this.processSuccess();
this.cleanEnvironment();
} finally {
// this.cleanEnvironment();
}
void testReadWriteSplittingInLocalTransactions() throws SQLException, IOException {
DataSource dataSource = YamlShardingSphereDataSourceFactory.createDataSource(FileTestUtils.readFromFileURLString("yaml/readwrite-splitting.yaml"));
orderRepository = new OrderRepository(dataSource);
orderItemRepository = new OrderItemRepository(dataSource);
addressRepository = new AddressRepository(dataSource);
this.initEnvironment();
this.processSuccess();
this.cleanEnvironment();
}

private void initEnvironment() throws SQLException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,18 +49,14 @@ public final class ShadowTest {
private AddressRepository addressRepository;

@Test
public void testShadowInLocalTransactions() throws SQLException, IOException {
try {
DataSource dataSource = YamlShardingSphereDataSourceFactory.createDataSource(FileTestUtils.readFromFileURLString("yaml/shadow.yaml"));
orderRepository = new OrderRepository(dataSource);
orderItemRepository = new OrderItemRepository(dataSource);
addressRepository = new AddressRepository(dataSource);
this.initEnvironment();
this.processSuccess();
this.cleanEnvironment();
} finally {
// this.cleanEnvironment();
}
void testShadowInLocalTransactions() throws SQLException, IOException {
DataSource dataSource = YamlShardingSphereDataSourceFactory.createDataSource(FileTestUtils.readFromFileURLString("yaml/shadow.yaml"));
orderRepository = new OrderRepository(dataSource);
orderItemRepository = new OrderItemRepository(dataSource);
addressRepository = new AddressRepository(dataSource);
this.initEnvironment();
this.processSuccess();
this.cleanEnvironment();
}

private void initEnvironment() throws SQLException {
Expand All @@ -75,7 +71,7 @@ private void initEnvironment() throws SQLException {
}

private void processSuccess() throws SQLException {
List<Long> orderIds = insertData();
final List<Long> orderIds = insertData();
assertThat(this.selectAll(), equalTo(Arrays.asList(
new Order(1, 0, 2, 2, "INSERT_TEST"),
new Order(2, 0, 4, 4, "INSERT_TEST"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,18 +49,14 @@ public final class ShardingTest {
private AddressRepository addressRepository;

@Test
public void testShardingInLocalTransactions() throws SQLException, IOException {
try {
DataSource dataSource = YamlShardingSphereDataSourceFactory.createDataSource(FileTestUtils.readFromFileURLString("yaml/sharding.yaml"));
orderRepository = new OrderRepository(dataSource);
orderItemRepository = new OrderItemRepository(dataSource);
addressRepository = new AddressRepository(dataSource);
this.initEnvironment();
this.processSuccess();
this.cleanEnvironment();
} finally {
// this.cleanEnvironment();
}
void testShardingInLocalTransactions() throws SQLException, IOException {
DataSource dataSource = YamlShardingSphereDataSourceFactory.createDataSource(FileTestUtils.readFromFileURLString("yaml/sharding.yaml"));
orderRepository = new OrderRepository(dataSource);
orderItemRepository = new OrderItemRepository(dataSource);
addressRepository = new AddressRepository(dataSource);
this.initEnvironment();
this.processSuccess();
this.cleanEnvironment();
}

private void initEnvironment() throws SQLException {
Expand All @@ -73,7 +69,7 @@ private void initEnvironment() throws SQLException {
}

private void processSuccess() throws SQLException {
List<Long> orderIds = insertData();
final List<Long> orderIds = insertData();

List<Order> orders = orderRepository.selectAll();
assertThat(orders.stream().map(Order::getOrderType).collect(Collectors.toList()),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public String getType() {
}

@Override
public void init(Properties props) {
public void init(final Properties props) {
this.props = props;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@

package org.apache.shardingsphere.infra.nativetest.jdbc.features.entity;

import lombok.*;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@

package org.apache.shardingsphere.infra.nativetest.jdbc.features.entity;

import lombok.*;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import lombok.NoArgsConstructor;

import java.io.Serializable;

@Data
@AllArgsConstructor
@NoArgsConstructor
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import java.util.LinkedList;
import java.util.List;

@SuppressWarnings({"SqlDialectInspection", "SqlNoDataSourceInspection"})
public final class AddressRepository {

private final DataSource dataSource;
Expand All @@ -36,6 +37,10 @@ public AddressRepository(final DataSource dataSource) {
this.dataSource = dataSource;
}

/**
* create table t_address if not exists.
* @throws SQLException SQL exception
*/
public void createTableIfNotExists() throws SQLException {
String sql = "CREATE TABLE IF NOT EXISTS t_address (address_id BIGINT NOT NULL, address_name VARCHAR(100) NOT NULL, PRIMARY KEY (address_id))";
try (
Expand All @@ -45,6 +50,10 @@ public void createTableIfNotExists() throws SQLException {
}
}

/**
* drop table t_address.
* @throws SQLException SQL exception
*/
public void dropTable() throws SQLException {
String sql = "DROP TABLE IF EXISTS t_address";
try (
Expand All @@ -54,6 +63,10 @@ public void dropTable() throws SQLException {
}
}

/**
* truncate table t_address.
* @throws SQLException SQL exception
*/
public void truncateTable() throws SQLException {
String sql = "TRUNCATE TABLE t_address";
try (
Expand All @@ -63,6 +76,12 @@ public void truncateTable() throws SQLException {
}
}

/**
* insert something to table t_address.
* @param address address
* @return addressId of the insert statement
* @throws SQLException SQL exception
*/
public Long insert(final Address address) throws SQLException {
String sql = "INSERT INTO t_address (address_id, address_name) VALUES (?, ?)";
try (
Expand All @@ -75,6 +94,11 @@ public Long insert(final Address address) throws SQLException {
return address.getAddressId();
}

/**
* delete by id.
* @param id id
* @throws SQLException SQL exception
*/
public void delete(final Long id) throws SQLException {
String sql = "DELETE FROM t_address WHERE address_id=?";
try (
Expand All @@ -85,6 +109,11 @@ public void delete(final Long id) throws SQLException {
}
}

/**
* select all.
* @return list of address
* @throws SQLException SQL exception
*/
public List<Address> selectAll() throws SQLException {
String sql = "SELECT * FROM t_address";
List<Address> result = new LinkedList<>();
Expand Down
Loading

0 comments on commit b64db8e

Please sign in to comment.