Skip to content

Commit

Permalink
Use java.util.Collection instead of java.util.List in unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
linghengqian committed Nov 14, 2023
1 parent 2e570f5 commit 7ca5dbc
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@

import org.apache.shardingsphere.driver.api.yaml.YamlShardingSphereDataSourceFactory;
import org.apache.shardingsphere.infra.nativetest.FileTestUtils;

import org.apache.shardingsphere.infra.nativetest.jdbc.features.entity.Address;
import org.apache.shardingsphere.infra.nativetest.jdbc.features.entity.Order;
import org.apache.shardingsphere.infra.nativetest.jdbc.features.entity.OrderItem;
Expand All @@ -32,7 +31,7 @@
import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Collection;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import java.util.stream.LongStream;
Expand Down Expand Up @@ -69,7 +68,7 @@ private void initEnvironment() throws SQLException {
}

private void processSuccess() throws SQLException {
final List<Long> orderIds = insertData();
final Collection<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 All @@ -82,8 +81,8 @@ private void processSuccess() throws SQLException {
assertThat(addressRepository.selectAll(), equalTo(new ArrayList<>()));
}

private List<Long> insertData() throws SQLException {
List<Long> result = new ArrayList<>(10);
private Collection<Long> insertData() throws SQLException {
Collection<Long> result = new ArrayList<>(10);
for (int i = 1; i <= 10; i++) {
Order order = new Order();
order.setUserId(i);
Expand All @@ -104,7 +103,7 @@ private List<Long> insertData() throws SQLException {
return result;
}

private void deleteData(final List<Long> orderIds) throws SQLException {
private void deleteData(final Collection<Long> orderIds) throws SQLException {
long count = 1;
for (Long each : orderIds) {
orderRepository.delete(each);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Collection;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import java.util.stream.LongStream;
Expand Down Expand Up @@ -68,7 +68,7 @@ private void initEnvironment() throws SQLException {
}

private void processSuccess() throws SQLException {
final List<Long> orderIds = insertData();
final Collection<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 All @@ -81,8 +81,8 @@ private void processSuccess() throws SQLException {
assertThat(addressRepository.selectAll(), equalTo(new ArrayList<>()));
}

private List<Long> insertData() throws SQLException {
List<Long> result = new ArrayList<>(10);
private Collection<Long> insertData() throws SQLException {
Collection<Long> result = new ArrayList<>(10);
for (int i = 1; i <= 10; i++) {
Order order = new Order();
order.setUserId(i);
Expand All @@ -103,7 +103,7 @@ private List<Long> insertData() throws SQLException {
return result;
}

private void deleteData(final List<Long> orderIds) throws SQLException {
private void deleteData(final Collection<Long> orderIds) throws SQLException {
long count = 1;
for (Long each : orderIds) {
orderRepository.delete(each);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Collection;

import static org.junit.jupiter.api.Assertions.assertThrows;

Expand Down Expand Up @@ -65,16 +65,16 @@ private void initEnvironment() throws SQLException {
}

private void processSuccess() throws SQLException {
List<Long> orderIds = insertData();
Collection<Long> orderIds = insertData();
// This is intentional because the read operation is in the slave database and the corresponding table does not exist.
assertThrows(JdbcSQLSyntaxErrorException.class, this::printData);
deleteData(orderIds);
// This is intentional because the read operation is in the slave database and the corresponding table does not exist.
assertThrows(JdbcSQLSyntaxErrorException.class, this::printData);
}

private List<Long> insertData() throws SQLException {
List<Long> result = new ArrayList<>(10);
private Collection<Long> insertData() throws SQLException {
Collection<Long> result = new ArrayList<>(10);
for (int i = 1; i <= 10; i++) {
Order order = new Order();
order.setUserId(i);
Expand All @@ -95,7 +95,7 @@ private List<Long> insertData() throws SQLException {
return result;
}

private void deleteData(final List<Long> orderIds) throws SQLException {
private void deleteData(final Collection<Long> orderIds) throws SQLException {
long count = 1;
for (Long each : orderIds) {
orderRepository.delete(each);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.LongStream;

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

private void processSuccess() throws SQLException {
final List<Long> orderIds = insertData();
final Collection<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 Expand Up @@ -102,8 +102,8 @@ private void processSuccess() throws SQLException {
assertThat(addressRepository.selectAll(), equalTo(new ArrayList<>()));
}

private List<Long> insertData() throws SQLException {
List<Long> result = new ArrayList<>(10);
private Collection<Long> insertData() throws SQLException {
Collection<Long> result = new ArrayList<>(10);
for (int i = 1; i <= 10; i++) {
Order order = new Order();
order.setUserId(i);
Expand All @@ -124,7 +124,7 @@ private List<Long> insertData() throws SQLException {
return result;
}

private void deleteData(final List<Long> orderIds) throws SQLException {
private void deleteData(final Collection<Long> orderIds) throws SQLException {
long count = 1;
for (Long each : orderIds) {
orderRepository.deleteShadow(each);
Expand All @@ -134,8 +134,8 @@ private void deleteData(final List<Long> orderIds) throws SQLException {
}
}

private List<Order> selectAll() throws SQLException {
List<Order> result = orderRepository.selectAll();
private Collection<Order> selectAll() throws SQLException {
Collection<Order> result = orderRepository.selectAll();
result.addAll(orderRepository.selectShadowOrder());
return result;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Collection;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import java.util.stream.LongStream;
Expand Down Expand Up @@ -68,8 +68,8 @@ private void initEnvironment() throws SQLException {
}

private void processSuccess() throws SQLException {
final List<Long> orderIds = insertData();
List<Order> orders = orderRepository.selectAll();
final Collection<Long> orderIds = insertData();
Collection<Order> orders = orderRepository.selectAll();
assertThat(orders.stream().map(Order::getOrderType).collect(Collectors.toList()),
equalTo(Arrays.asList(1, 1, 1, 1, 1, 0, 0, 0, 0, 0)));
assertThat(orders.stream().map(Order::getUserId).collect(Collectors.toList()),
Expand All @@ -78,7 +78,7 @@ private void processSuccess() throws SQLException {
equalTo(new ArrayList<>(Arrays.asList(1L, 3L, 5L, 7L, 9L, 2L, 4L, 6L, 8L, 10L))));
assertThat(orders.stream().map(Order::getStatus).collect(Collectors.toList()),
equalTo(IntStream.range(1, 11).mapToObj(i -> "INSERT_TEST").collect(Collectors.toList())));
List<OrderItem> orderItems = orderItemRepository.selectAll();
Collection<OrderItem> orderItems = orderItemRepository.selectAll();
assertThat(orderItems.stream().map(OrderItem::getUserId).collect(Collectors.toList()),
equalTo(new ArrayList<>(Arrays.asList(1, 3, 5, 7, 9, 2, 4, 6, 8, 10))));
assertThat(orderItems.stream().map(OrderItem::getPhone).collect(Collectors.toList()),
Expand All @@ -93,8 +93,8 @@ private void processSuccess() throws SQLException {
assertThat(addressRepository.selectAll(), equalTo(new ArrayList<>()));
}

private List<Long> insertData() throws SQLException {
List<Long> result = new ArrayList<>(10);
private Collection<Long> insertData() throws SQLException {
Collection<Long> result = new ArrayList<>(10);
for (int i = 1; i <= 10; i++) {
Order order = new Order();
order.setUserId(i);
Expand All @@ -115,7 +115,7 @@ private List<Long> insertData() throws SQLException {
return result;
}

private void deleteData(final List<Long> orderIds) throws SQLException {
private void deleteData(final Collection<Long> orderIds) throws SQLException {
long count = 1;
for (Long each : orderIds) {
orderRepository.delete(each);
Expand Down

0 comments on commit 7ca5dbc

Please sign in to comment.