This is an automated email from the ASF dual-hosted git repository.
zhangliang pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/shardingsphere.git
The following commit(s) were added to refs/heads/master by this push:
new 42be9fc3223 Refactor AgentTestJDBCRepository (#32198)
42be9fc3223 is described below
commit 42be9fc3223bfd0609a93236f0ed6525cf450fe2
Author: Liang Zhang <[email protected]>
AuthorDate: Sat Jul 20 16:52:05 2024 +0800
Refactor AgentTestJDBCRepository (#32198)
---
.../AgentTestJDBCRepository.java} | 71 ++++++++++------------
.../fixture/request/ProxyRequestExecutor.java | 25 ++++----
2 files changed, 45 insertions(+), 51 deletions(-)
diff --git
a/test/e2e/agent/plugins/common/src/test/java/org/apache/shardingsphere/test/e2e/agent/common/fixture/dao/JDBCAgentTestDAO.java
b/test/e2e/agent/plugins/common/src/test/java/org/apache/shardingsphere/test/e2e/agent/common/fixture/repository/AgentTestJDBCRepository.java
similarity index 65%
rename from
test/e2e/agent/plugins/common/src/test/java/org/apache/shardingsphere/test/e2e/agent/common/fixture/dao/JDBCAgentTestDAO.java
rename to
test/e2e/agent/plugins/common/src/test/java/org/apache/shardingsphere/test/e2e/agent/common/fixture/repository/AgentTestJDBCRepository.java
index 3ebcf58b860..35230fe6f40 100644
---
a/test/e2e/agent/plugins/common/src/test/java/org/apache/shardingsphere/test/e2e/agent/common/fixture/dao/JDBCAgentTestDAO.java
+++
b/test/e2e/agent/plugins/common/src/test/java/org/apache/shardingsphere/test/e2e/agent/common/fixture/repository/AgentTestJDBCRepository.java
@@ -15,10 +15,10 @@
* limitations under the License.
*/
-package org.apache.shardingsphere.test.e2e.agent.common.fixture.dao;
+package org.apache.shardingsphere.test.e2e.agent.common.fixture.repository;
-import lombok.AccessLevel;
-import lombok.NoArgsConstructor;
+import lombok.RequiredArgsConstructor;
+import lombok.SneakyThrows;
import
org.apache.shardingsphere.test.e2e.agent.common.fixture.entity.OrderEntity;
import java.sql.Connection;
@@ -30,18 +30,20 @@ import java.util.Collection;
import java.util.LinkedList;
/**
- * JDBC agent test DAO.
+ * Agent test JDBC repository.
*/
-@NoArgsConstructor(access = AccessLevel.PRIVATE)
-public final class JDBCAgentTestDAO {
+@RequiredArgsConstructor
+public final class AgentTestJDBCRepository {
+
+ private final Connection connection;
/**
* Insert order.
*
* @param orderEntity order entity
- * @param connection connection
*/
- public static void insertOrder(final OrderEntity orderEntity, final
Connection connection) {
+ @SneakyThrows(SQLException.class)
+ public void insertOrder(final OrderEntity orderEntity) {
String sql = "INSERT INTO t_order (order_id, user_id, status) VALUES
(?, ?, ?)";
try (PreparedStatement preparedStatement =
connection.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS)) {
connection.setAutoCommit(false);
@@ -50,17 +52,16 @@ public final class JDBCAgentTestDAO {
preparedStatement.setString(3, orderEntity.getStatus());
preparedStatement.executeUpdate();
connection.commit();
- } catch (final SQLException ignored) {
}
}
/**
- * Insert order rollback.
+ * Insert order and rollback.
*
* @param orderEntity order entity
- * @param connection connection
*/
- public static void insertOrderRollback(final OrderEntity orderEntity,
final Connection connection) {
+ @SneakyThrows(SQLException.class)
+ public void insertOrderAndRollback(final OrderEntity orderEntity) {
String sql = "INSERT INTO t_order (order_id, user_id, status) VALUES
(?, ?, ?)";
try (PreparedStatement preparedStatement =
connection.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS)) {
connection.setAutoCommit(false);
@@ -69,32 +70,30 @@ public final class JDBCAgentTestDAO {
preparedStatement.setString(3, orderEntity.getStatus());
preparedStatement.executeUpdate();
connection.rollback();
- } catch (final SQLException ignored) {
}
}
/**
- * Delete order by order id.
+ * Delete order.
*
- * @param orderId order id
- * @param connection connection
+ * @param orderId to be deleted order ID
*/
- public static void deleteOrderByOrderId(final Long orderId, final
Connection connection) {
+ @SneakyThrows(SQLException.class)
+ public void deleteOrder(final Long orderId) {
String sql = "DELETE FROM t_order WHERE order_id=?";
try (PreparedStatement preparedStatement =
connection.prepareStatement(sql)) {
preparedStatement.setLong(1, orderId);
preparedStatement.executeUpdate();
- } catch (final SQLException ignored) {
}
}
/**
- * Update order status.
+ * Update order.
*
- * @param orderEntity order entity
- * @param connection connection
+ * @param orderEntity to be updated order entity
*/
- public static void updateOrderStatus(final OrderEntity orderEntity, final
Connection connection) {
+ @SneakyThrows(SQLException.class)
+ public void updateOrder(final OrderEntity orderEntity) {
String sql = "UPDATE t_order SET status = ? WHERE order_id =?";
try (PreparedStatement preparedStatement =
connection.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS)) {
connection.setAutoCommit(false);
@@ -102,44 +101,36 @@ public final class JDBCAgentTestDAO {
preparedStatement.setLong(2, orderEntity.getOrderId());
preparedStatement.executeUpdate();
connection.commit();
- } catch (final SQLException ignored) {
}
}
/**
- * Select all orders collection.
+ * Select all orders.
*
- * @param connection connection
- * @return collection
+ * @return all orders
*/
- public static Collection<OrderEntity> selectAllOrders(final Connection
connection) {
+ @SneakyThrows(SQLException.class)
+ public Collection<OrderEntity> selectAllOrders() {
String sql = "SELECT * FROM t_order";
- return getOrders(sql, connection);
- }
-
- private static Collection<OrderEntity> getOrders(final String sql, final
Connection connection) {
Collection<OrderEntity> result = new LinkedList<>();
- try (PreparedStatement preparedStatement =
connection.prepareStatement(sql)) {
- ResultSet resultSet = preparedStatement.executeQuery();
+ try (
+ PreparedStatement preparedStatement =
connection.prepareStatement(sql);
+ ResultSet resultSet = preparedStatement.executeQuery()) {
while (resultSet.next()) {
- OrderEntity orderEntity = new
OrderEntity(resultSet.getLong(1), resultSet.getInt(2), resultSet.getString(3));
- result.add(orderEntity);
+ result.add(new OrderEntity(resultSet.getLong(1),
resultSet.getInt(2), resultSet.getString(3)));
}
- } catch (final SQLException ignored) {
}
return result;
}
/**
* Create execute error.
- *
- * @param connection connection
*/
- public static void createExecuteError(final Connection connection) {
+ @SneakyThrows(SQLException.class)
+ public void createExecuteError() {
String sql = "SELECT * FROM non_existent_table";
try (Statement statement = connection.createStatement()) {
statement.executeQuery(sql);
- } catch (final SQLException ignored) {
}
}
}
diff --git
a/test/e2e/agent/plugins/common/src/test/java/org/apache/shardingsphere/test/e2e/agent/common/fixture/request/ProxyRequestExecutor.java
b/test/e2e/agent/plugins/common/src/test/java/org/apache/shardingsphere/test/e2e/agent/common/fixture/request/ProxyRequestExecutor.java
index f192013e99a..6ae84d9a6c4 100644
---
a/test/e2e/agent/plugins/common/src/test/java/org/apache/shardingsphere/test/e2e/agent/common/fixture/request/ProxyRequestExecutor.java
+++
b/test/e2e/agent/plugins/common/src/test/java/org/apache/shardingsphere/test/e2e/agent/common/fixture/request/ProxyRequestExecutor.java
@@ -17,9 +17,8 @@
package org.apache.shardingsphere.test.e2e.agent.common.fixture.request;
-import lombok.RequiredArgsConstructor;
+import
org.apache.shardingsphere.test.e2e.agent.common.fixture.repository.AgentTestJDBCRepository;
import
org.apache.shardingsphere.test.e2e.agent.common.fixture.entity.OrderEntity;
-import
org.apache.shardingsphere.test.e2e.agent.common.fixture.dao.JDBCAgentTestDAO;
import org.testcontainers.shaded.org.awaitility.Awaitility;
import java.sql.Connection;
@@ -33,12 +32,16 @@ import java.util.concurrent.TimeUnit;
/**
* Proxy request executor.
*/
-@RequiredArgsConstructor
public final class ProxyRequestExecutor implements Runnable {
- private final Connection connection;
+ private final AgentTestJDBCRepository repository;
- private final ExecutorService executor = Executors.newFixedThreadPool(1);
+ private final ExecutorService executor;
+
+ public ProxyRequestExecutor(final Connection connection) {
+ repository = new AgentTestJDBCRepository(connection);
+ executor = Executors.newFixedThreadPool(1);
+ }
/**
* Start.
@@ -69,16 +72,16 @@ public final class ProxyRequestExecutor implements Runnable
{
Collection<Long> results = new LinkedList<>();
for (int i = 1; i <= 10; i++) {
OrderEntity orderEntity = new OrderEntity(i, i, "INSERT_TEST");
- JDBCAgentTestDAO.insertOrder(orderEntity, connection);
+ repository.insertOrder(orderEntity);
results.add(orderEntity.getOrderId());
}
OrderEntity orderEntity = new OrderEntity(1000L, 1000, "ROLL_BACK");
- JDBCAgentTestDAO.insertOrderRollback(orderEntity, connection);
- JDBCAgentTestDAO.updateOrderStatus(orderEntity, connection);
- JDBCAgentTestDAO.selectAllOrders(connection);
+ repository.insertOrderAndRollback(orderEntity);
+ repository.updateOrder(orderEntity);
+ repository.selectAllOrders();
for (Long each : results) {
- JDBCAgentTestDAO.deleteOrderByOrderId(each, connection);
+ repository.deleteOrder(each);
}
- JDBCAgentTestDAO.createExecuteError(connection);
+ repository.createExecuteError();
}
}