From ced9efa29176a03fb48490ade09e73e8205a35a3 Mon Sep 17 00:00:00 2001 From: wonyongChoi05 Date: Mon, 9 Oct 2023 06:46:36 +0900 Subject: [PATCH 1/5] =?UTF-8?q?refactor:=20Service=20=EA=B3=84=EC=B8=B5=20?= =?UTF-8?q?=EB=B6=84=EB=A6=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../techcourse/service/AppUserService.java | 37 ++++++++++++++++ .../com/techcourse/service/TxUserService.java | 42 +++++++++++++++++++ .../com/techcourse/service/UserService.java | 40 +++--------------- ...rviceTest.java => AppUserServiceTest.java} | 10 +++-- .../jdbc/datasource/DataSourceUtils.java | 1 - 5 files changed, 91 insertions(+), 39 deletions(-) create mode 100644 app/src/main/java/com/techcourse/service/AppUserService.java create mode 100644 app/src/main/java/com/techcourse/service/TxUserService.java rename app/src/test/java/com/techcourse/service/{UserServiceTest.java => AppUserServiceTest.java} (86%) diff --git a/app/src/main/java/com/techcourse/service/AppUserService.java b/app/src/main/java/com/techcourse/service/AppUserService.java new file mode 100644 index 0000000000..65e93e0aa1 --- /dev/null +++ b/app/src/main/java/com/techcourse/service/AppUserService.java @@ -0,0 +1,37 @@ +package com.techcourse.service; + +import com.techcourse.dao.UserDao; +import com.techcourse.dao.JdbcUserHistoryDao; +import com.techcourse.domain.User; +import com.techcourse.domain.UserHistory; +import com.techcourse.repository.UserRepository; + +public class AppUserService implements UserService { + + private final UserRepository userRepository; + private final JdbcUserHistoryDao jdbcUserHistoryDao; + + public AppUserService(final UserDao userDao, final JdbcUserHistoryDao jdbcUserHistoryDao) { + this.userRepository = new UserRepository(userDao); + this.jdbcUserHistoryDao = jdbcUserHistoryDao; + } + + @Override + public User findById(final long id) { + return userRepository.findById(id); + } + + @Override + public void insert(final User user) { + userRepository.save(user); + } + + @Override + public void changePassword(final long id, final String newPassword, final String createBy) { + final var user = findById(id); + user.changePassword(newPassword); + userRepository.update(user); + jdbcUserHistoryDao.log(new UserHistory(user, createBy)); + } + +} diff --git a/app/src/main/java/com/techcourse/service/TxUserService.java b/app/src/main/java/com/techcourse/service/TxUserService.java new file mode 100644 index 0000000000..98cb8143f4 --- /dev/null +++ b/app/src/main/java/com/techcourse/service/TxUserService.java @@ -0,0 +1,42 @@ +package com.techcourse.service; + +import com.techcourse.config.DataSourceConfig; +import com.techcourse.dao.JdbcUserHistoryDao; +import com.techcourse.domain.User; +import org.springframework.jdbc.transaction.TransactionManager; + +import static com.techcourse.config.DataSourceConfig.getInstance; + +public class TxUserService implements UserService { + + private final UserService userService; + private final TransactionManager transactionManager; + + public TxUserService(UserService userService) { + this.userService = userService; + this.transactionManager = new TransactionManager(getInstance()); + } + + @Override + public User findById(final long id) { + return transactionManager.execute(connection -> userService.findById(id)); + } + + @Override + public void insert(final User user) { + transactionManager.execute(connection -> { + userService.insert(user); + return null; + } + ); + } + + @Override + public void changePassword(long id, String newPassword, String createBy) { + transactionManager.execute(connection -> { + userService.changePassword(id, newPassword, createBy); + return null; + }); + } + +} diff --git a/app/src/main/java/com/techcourse/service/UserService.java b/app/src/main/java/com/techcourse/service/UserService.java index b20446a516..563ecd2c1e 100644 --- a/app/src/main/java/com/techcourse/service/UserService.java +++ b/app/src/main/java/com/techcourse/service/UserService.java @@ -1,41 +1,11 @@ package com.techcourse.service; -import com.techcourse.dao.UserDao; -import com.techcourse.dao.JdbcUserHistoryDao; import com.techcourse.domain.User; -import com.techcourse.domain.UserHistory; -import com.techcourse.repository.UserRepository; -import org.springframework.jdbc.transaction.TransactionManager; -import javax.sql.DataSource; +public interface UserService { -public class UserService { + User findById(final long id); + void insert(final User user); + void changePassword(final long id, final String newPassword, final String createBy); - private final TransactionManager transactionManager; - private final UserRepository userRepository; - private final JdbcUserHistoryDao jdbcUserHistoryDao; - - public UserService(final DataSource dataSource, final UserDao userDao, final JdbcUserHistoryDao jdbcUserHistoryDao) { - this.transactionManager = new TransactionManager(dataSource); - this.userRepository = new UserRepository(userDao); - this.jdbcUserHistoryDao = jdbcUserHistoryDao; - } - - public User findById(final long id) { - return userRepository.findById(id); - } - - public void insert(final User user) { - userRepository.save(user); - } - - public void changePassword(final long id, final String newPassword, final String createBy) { - transactionManager.execute(connection -> { - final var user = findById(id); - user.changePassword(newPassword); - userRepository.update(user); - jdbcUserHistoryDao.log(new UserHistory(user, createBy)); - return null; - }); - } -} +} \ No newline at end of file diff --git a/app/src/test/java/com/techcourse/service/UserServiceTest.java b/app/src/test/java/com/techcourse/service/AppUserServiceTest.java similarity index 86% rename from app/src/test/java/com/techcourse/service/UserServiceTest.java rename to app/src/test/java/com/techcourse/service/AppUserServiceTest.java index 8caca2c38b..7b6b46939b 100644 --- a/app/src/test/java/com/techcourse/service/UserServiceTest.java +++ b/app/src/test/java/com/techcourse/service/AppUserServiceTest.java @@ -14,7 +14,7 @@ import static org.assertj.core.api.Assertions.assertThat; import static org.junit.jupiter.api.Assertions.assertThrows; -class UserServiceTest { +class AppUserServiceTest { private JdbcTemplate jdbcTemplate; private UserDao userDao; @@ -32,7 +32,7 @@ void setUp() { @Test void testChangePassword() { final var userHistoryDao = new JdbcUserHistoryDao(jdbcTemplate); - final var userService = new UserService(DataSourceConfig.getInstance(), userDao, userHistoryDao); + final var userService = new AppUserService(userDao, userHistoryDao); final var newPassword = "qqqqq"; final var createBy = "gugu"; @@ -47,7 +47,10 @@ void testChangePassword() { void testTransactionRollback() { // 트랜잭션 롤백 테스트를 위해 mock으로 교체 final var userHistoryDao = new MockJdbcUserHistoryDao(jdbcTemplate); - final var userService = new UserService(DataSourceConfig.getInstance(), userDao, userHistoryDao); + // 애플리케이션 서비스 + final var appUserService = new AppUserService(userDao, userHistoryDao); + // 트랜잭션 서비스 추상화 + final var userService = new TxUserService(appUserService); final var newPassword = "newPassword"; final var createBy = "gugu"; @@ -59,4 +62,5 @@ void testTransactionRollback() { assertThat(actual.getPassword()).isNotEqualTo(newPassword); } + } diff --git a/jdbc/src/main/java/org/springframework/jdbc/datasource/DataSourceUtils.java b/jdbc/src/main/java/org/springframework/jdbc/datasource/DataSourceUtils.java index 3c40bfec52..6ce4b00507 100644 --- a/jdbc/src/main/java/org/springframework/jdbc/datasource/DataSourceUtils.java +++ b/jdbc/src/main/java/org/springframework/jdbc/datasource/DataSourceUtils.java @@ -7,7 +7,6 @@ import java.sql.Connection; import java.sql.SQLException; -// 4단계 미션에서 사용할 것 public abstract class DataSourceUtils { private DataSourceUtils() {} From 4045c214770f387fb6774e2f909df92a238fda69 Mon Sep 17 00:00:00 2001 From: wonyongChoi05 Date: Mon, 9 Oct 2023 07:21:20 +0900 Subject: [PATCH 2/5] =?UTF-8?q?refactor:=20Connection=EC=9D=84=20Transacti?= =?UTF-8?q?onSynchronizationManager=EB=A1=9C=20=EA=B4=80=EB=A6=AC=ED=95=9C?= =?UTF-8?q?=EB=8B=A4.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../techcourse/dao/JdbcUserHistoryDao.java | 7 ---- .../com/techcourse/domain/UserHistory.java | 1 - .../techcourse/repository/UserRepository.java | 1 - .../techcourse/service/AppUserService.java | 2 +- .../com/techcourse/service/TxUserService.java | 2 - .../jdbc/core/JdbcTemplate.java | 10 +++-- .../jdbc/datasource/DataSourceUtils.java | 1 + .../jdbc/transaction/TransactionManager.java | 40 ++++++------------- .../TransactionSynchronizationManager.java | 17 +++++++- 9 files changed, 36 insertions(+), 45 deletions(-) diff --git a/app/src/main/java/com/techcourse/dao/JdbcUserHistoryDao.java b/app/src/main/java/com/techcourse/dao/JdbcUserHistoryDao.java index 3e7ab14333..123be14454 100644 --- a/app/src/main/java/com/techcourse/dao/JdbcUserHistoryDao.java +++ b/app/src/main/java/com/techcourse/dao/JdbcUserHistoryDao.java @@ -1,15 +1,8 @@ package com.techcourse.dao; import com.techcourse.domain.UserHistory; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; import org.springframework.jdbc.core.JdbcTemplate; -import javax.sql.DataSource; -import java.sql.Connection; -import java.sql.PreparedStatement; -import java.sql.SQLException; - public class JdbcUserHistoryDao implements UserHistoryDao { private final JdbcTemplate jdbcTemplate; diff --git a/app/src/main/java/com/techcourse/domain/UserHistory.java b/app/src/main/java/com/techcourse/domain/UserHistory.java index 82c92ea649..7ae4fdb813 100644 --- a/app/src/main/java/com/techcourse/domain/UserHistory.java +++ b/app/src/main/java/com/techcourse/domain/UserHistory.java @@ -1,6 +1,5 @@ package com.techcourse.domain; -import java.sql.Connection; import java.time.LocalDateTime; public class UserHistory { diff --git a/app/src/main/java/com/techcourse/repository/UserRepository.java b/app/src/main/java/com/techcourse/repository/UserRepository.java index d3cee31a41..41549085df 100644 --- a/app/src/main/java/com/techcourse/repository/UserRepository.java +++ b/app/src/main/java/com/techcourse/repository/UserRepository.java @@ -3,7 +3,6 @@ import com.techcourse.dao.UserDao; import com.techcourse.domain.User; -import java.sql.Connection; import java.util.List; public class UserRepository { diff --git a/app/src/main/java/com/techcourse/service/AppUserService.java b/app/src/main/java/com/techcourse/service/AppUserService.java index 65e93e0aa1..2ae7914975 100644 --- a/app/src/main/java/com/techcourse/service/AppUserService.java +++ b/app/src/main/java/com/techcourse/service/AppUserService.java @@ -1,7 +1,7 @@ package com.techcourse.service; -import com.techcourse.dao.UserDao; import com.techcourse.dao.JdbcUserHistoryDao; +import com.techcourse.dao.UserDao; import com.techcourse.domain.User; import com.techcourse.domain.UserHistory; import com.techcourse.repository.UserRepository; diff --git a/app/src/main/java/com/techcourse/service/TxUserService.java b/app/src/main/java/com/techcourse/service/TxUserService.java index 98cb8143f4..161435993a 100644 --- a/app/src/main/java/com/techcourse/service/TxUserService.java +++ b/app/src/main/java/com/techcourse/service/TxUserService.java @@ -1,7 +1,5 @@ package com.techcourse.service; -import com.techcourse.config.DataSourceConfig; -import com.techcourse.dao.JdbcUserHistoryDao; import com.techcourse.domain.User; import org.springframework.jdbc.transaction.TransactionManager; diff --git a/jdbc/src/main/java/org/springframework/jdbc/core/JdbcTemplate.java b/jdbc/src/main/java/org/springframework/jdbc/core/JdbcTemplate.java index 3c18d92ec7..02066afc81 100644 --- a/jdbc/src/main/java/org/springframework/jdbc/core/JdbcTemplate.java +++ b/jdbc/src/main/java/org/springframework/jdbc/core/JdbcTemplate.java @@ -1,6 +1,8 @@ package org.springframework.jdbc.core; +import org.springframework.jdbc.datasource.DataSourceUtils; import org.springframework.jdbc.transaction.TransactionManager; +import org.springframework.transaction.support.TransactionSynchronizationManager; import javax.sql.DataSource; import java.sql.Connection; @@ -11,13 +13,15 @@ import java.util.Optional; import static java.util.Collections.singletonList; +import static org.springframework.jdbc.datasource.DataSourceUtils.*; +import static org.springframework.transaction.support.TransactionSynchronizationManager.*; public class JdbcTemplate { - private final TransactionManager transactionManager; + private final DataSource dataSource; public JdbcTemplate(final DataSource dataSource) { - this.transactionManager = new TransactionManager(dataSource); + this.dataSource = dataSource; } public int update(final String sql, final Object... conditions) { @@ -43,7 +47,7 @@ private T getRowByQuery(final PreparedStatement preparedStatement, final Row } private T getResult(final PreparedStatementExecutor executor, final String sql, final Object... conditions) { - Connection connection = transactionManager.getConnection(); + Connection connection = getConnection(dataSource); try ( PreparedStatement preparedStatement = connection.prepareStatement(sql); ) { diff --git a/jdbc/src/main/java/org/springframework/jdbc/datasource/DataSourceUtils.java b/jdbc/src/main/java/org/springframework/jdbc/datasource/DataSourceUtils.java index 6ce4b00507..a1b7ce13ca 100644 --- a/jdbc/src/main/java/org/springframework/jdbc/datasource/DataSourceUtils.java +++ b/jdbc/src/main/java/org/springframework/jdbc/datasource/DataSourceUtils.java @@ -29,6 +29,7 @@ public static Connection getConnection(DataSource dataSource) throws CannotGetJd public static void releaseConnection(Connection connection, DataSource dataSource) { try { connection.close(); + TransactionSynchronizationManager.unbindResource(dataSource); } catch (SQLException ex) { throw new CannotGetJdbcConnectionException("Failed to close JDBC Connection"); } diff --git a/jdbc/src/main/java/org/springframework/jdbc/transaction/TransactionManager.java b/jdbc/src/main/java/org/springframework/jdbc/transaction/TransactionManager.java index bf6d52d8b6..f160e9ab7c 100644 --- a/jdbc/src/main/java/org/springframework/jdbc/transaction/TransactionManager.java +++ b/jdbc/src/main/java/org/springframework/jdbc/transaction/TransactionManager.java @@ -1,12 +1,17 @@ package org.springframework.jdbc.transaction; +import org.springframework.jdbc.datasource.DataSourceUtils; +import org.springframework.transaction.support.TransactionSynchronizationManager; + import javax.sql.DataSource; import java.sql.Connection; import java.sql.SQLException; -public class TransactionManager { +import static org.springframework.jdbc.datasource.DataSourceUtils.*; +import static org.springframework.jdbc.datasource.DataSourceUtils.releaseConnection; +import static org.springframework.transaction.support.TransactionSynchronizationManager.*; - private static final ThreadLocal connectionInThread = new ThreadLocal<>(); +public class TransactionManager { private final DataSource dataSource; @@ -27,54 +32,33 @@ public T execute(final TransactionExecutor transactionExecutor) { } public Connection begin() { - Connection connection = getConnection(); + Connection connection = getConnection(dataSource); try { connection.setAutoCommit(false); } catch (SQLException e) { throw new RuntimeException(e); } - connectionInThread.set(connection); return connection; } public void commit() { - Connection connection = getConnection(); + Connection connection = getConnection(dataSource); try { connection.commit(); } catch (SQLException e) { throw new RuntimeException(e); } - init(connection); + releaseConnection(connection, dataSource); } public void rollback() { - Connection connection = getConnection(); + Connection connection = getConnection(dataSource); try { connection.rollback(); } catch (SQLException e) { throw new RuntimeException(e); } - init(connection); - } - - private void init(final Connection connection) { - try { - connection.close(); - } catch (SQLException e) { - throw new RuntimeException(e); - } - connectionInThread.remove(); - } - - public Connection getConnection() { - if (connectionInThread.get() == null) { - try { - return dataSource.getConnection(); - } catch (SQLException e) { - throw new RuntimeException(e); - } - } - return connectionInThread.get(); + releaseConnection(connection, dataSource); } } diff --git a/jdbc/src/main/java/org/springframework/transaction/support/TransactionSynchronizationManager.java b/jdbc/src/main/java/org/springframework/transaction/support/TransactionSynchronizationManager.java index 715557fc66..a6c2ce35bf 100644 --- a/jdbc/src/main/java/org/springframework/transaction/support/TransactionSynchronizationManager.java +++ b/jdbc/src/main/java/org/springframework/transaction/support/TransactionSynchronizationManager.java @@ -2,6 +2,7 @@ import javax.sql.DataSource; import java.sql.Connection; +import java.util.HashMap; import java.util.Map; public abstract class TransactionSynchronizationManager { @@ -11,13 +12,25 @@ public abstract class TransactionSynchronizationManager { private TransactionSynchronizationManager() {} public static Connection getResource(DataSource key) { - return null; + final Map resource = resources.get(); + if (resource == null) { + return null; + } + return resource.get(key); } public static void bindResource(DataSource key, Connection value) { + Map resource = resources.get(); + if (resource == null) { + resource = new HashMap<>(); + resources.set(resource); + } + resource.put(key, value); } public static Connection unbindResource(DataSource key) { - return null; + final Map resource = resources.get(); + return resource.remove(key); } + } From 5c9cd082060b7a76932d18e0cd56cc624fc765a8 Mon Sep 17 00:00:00 2001 From: wonyongChoi05 Date: Thu, 12 Oct 2023 09:31:19 +0900 Subject: [PATCH 3/5] =?UTF-8?q?refactor:=20=EC=99=80=EC=9D=BC=EB=93=9C?= =?UTF-8?q?=EC=B9=B4=EB=93=9C=20=EC=A0=9C=EA=B1=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/build.gradle | 1 + .../java/org/springframework/jdbc/core/JdbcTemplate.java | 7 +------ .../jdbc/transaction/TransactionManager.java | 6 +----- 3 files changed, 3 insertions(+), 11 deletions(-) diff --git a/app/build.gradle b/app/build.gradle index 00444e6f91..9d36e34567 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -27,6 +27,7 @@ dependencies { implementation "org.apache.commons:commons-lang3:3.13.0" implementation "com.fasterxml.jackson.core:jackson-databind:2.15.2" implementation "com.h2database:h2:2.2.220" + implementation project(path: ':study') testImplementation "org.assertj:assertj-core:3.24.2" testImplementation "org.junit.jupiter:junit-jupiter-api:5.7.2" diff --git a/jdbc/src/main/java/org/springframework/jdbc/core/JdbcTemplate.java b/jdbc/src/main/java/org/springframework/jdbc/core/JdbcTemplate.java index 02066afc81..a08f7129f0 100644 --- a/jdbc/src/main/java/org/springframework/jdbc/core/JdbcTemplate.java +++ b/jdbc/src/main/java/org/springframework/jdbc/core/JdbcTemplate.java @@ -1,9 +1,5 @@ package org.springframework.jdbc.core; -import org.springframework.jdbc.datasource.DataSourceUtils; -import org.springframework.jdbc.transaction.TransactionManager; -import org.springframework.transaction.support.TransactionSynchronizationManager; - import javax.sql.DataSource; import java.sql.Connection; import java.sql.PreparedStatement; @@ -13,8 +9,7 @@ import java.util.Optional; import static java.util.Collections.singletonList; -import static org.springframework.jdbc.datasource.DataSourceUtils.*; -import static org.springframework.transaction.support.TransactionSynchronizationManager.*; +import static org.springframework.jdbc.datasource.DataSourceUtils.getConnection; public class JdbcTemplate { diff --git a/jdbc/src/main/java/org/springframework/jdbc/transaction/TransactionManager.java b/jdbc/src/main/java/org/springframework/jdbc/transaction/TransactionManager.java index f160e9ab7c..4a110268e2 100644 --- a/jdbc/src/main/java/org/springframework/jdbc/transaction/TransactionManager.java +++ b/jdbc/src/main/java/org/springframework/jdbc/transaction/TransactionManager.java @@ -1,15 +1,11 @@ package org.springframework.jdbc.transaction; -import org.springframework.jdbc.datasource.DataSourceUtils; -import org.springframework.transaction.support.TransactionSynchronizationManager; - import javax.sql.DataSource; import java.sql.Connection; import java.sql.SQLException; -import static org.springframework.jdbc.datasource.DataSourceUtils.*; +import static org.springframework.jdbc.datasource.DataSourceUtils.getConnection; import static org.springframework.jdbc.datasource.DataSourceUtils.releaseConnection; -import static org.springframework.transaction.support.TransactionSynchronizationManager.*; public class TransactionManager { From da70f23fa039fc61c328b15f56f4ceeeec155fee Mon Sep 17 00:00:00 2001 From: wonyongChoi05 Date: Thu, 12 Oct 2023 09:35:03 +0900 Subject: [PATCH 4/5] =?UTF-8?q?refactor:=20unbindResource=20=EB=A9=94?= =?UTF-8?q?=EC=84=9C=EB=93=9C=EB=8A=94=20=EB=B0=98=ED=99=98=EA=B0=92?= =?UTF-8?q?=EC=9D=B4=20=EC=A1=B4=EC=9E=AC=ED=95=98=EC=A7=80=20=EC=95=8A?= =?UTF-8?q?=EB=8A=94=EB=8B=A4.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../support/TransactionSynchronizationManager.java | 4 ++-- .../support/TransactionSynchronizationManagerTest.java | 6 ++++++ 2 files changed, 8 insertions(+), 2 deletions(-) create mode 100644 jdbc/src/test/java/org/springframework/transaction/support/TransactionSynchronizationManagerTest.java diff --git a/jdbc/src/main/java/org/springframework/transaction/support/TransactionSynchronizationManager.java b/jdbc/src/main/java/org/springframework/transaction/support/TransactionSynchronizationManager.java index a6c2ce35bf..c0c48350f7 100644 --- a/jdbc/src/main/java/org/springframework/transaction/support/TransactionSynchronizationManager.java +++ b/jdbc/src/main/java/org/springframework/transaction/support/TransactionSynchronizationManager.java @@ -28,9 +28,9 @@ public static void bindResource(DataSource key, Connection value) { resource.put(key, value); } - public static Connection unbindResource(DataSource key) { + public static void unbindResource(DataSource key) { final Map resource = resources.get(); - return resource.remove(key); + resource.remove(key); } } diff --git a/jdbc/src/test/java/org/springframework/transaction/support/TransactionSynchronizationManagerTest.java b/jdbc/src/test/java/org/springframework/transaction/support/TransactionSynchronizationManagerTest.java new file mode 100644 index 0000000000..b2b28c7d8c --- /dev/null +++ b/jdbc/src/test/java/org/springframework/transaction/support/TransactionSynchronizationManagerTest.java @@ -0,0 +1,6 @@ +package org.springframework.transaction.support; + +class TransactionSynchronizationManagerTest { + + +} \ No newline at end of file From 9f3ff2af34d4094af1485bd4afea872c0f0b250f Mon Sep 17 00:00:00 2001 From: wonyongChoi05 Date: Thu, 12 Oct 2023 09:55:02 +0900 Subject: [PATCH 5/5] =?UTF-8?q?feat:=20TSM=20=ED=85=8C=EC=8A=A4=ED=8A=B8?= =?UTF-8?q?=EC=BD=94=EB=93=9C=20=EC=9E=91=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...TransactionSynchronizationManagerTest.java | 31 +++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/jdbc/src/test/java/org/springframework/transaction/support/TransactionSynchronizationManagerTest.java b/jdbc/src/test/java/org/springframework/transaction/support/TransactionSynchronizationManagerTest.java index b2b28c7d8c..b7a5784fa6 100644 --- a/jdbc/src/test/java/org/springframework/transaction/support/TransactionSynchronizationManagerTest.java +++ b/jdbc/src/test/java/org/springframework/transaction/support/TransactionSynchronizationManagerTest.java @@ -1,6 +1,37 @@ package org.springframework.transaction.support; +import org.assertj.core.api.Assertions; +import org.junit.jupiter.api.Test; + +import javax.sql.DataSource; +import java.sql.Connection; + +import static org.mockito.Mockito.mock; +import static org.springframework.transaction.support.TransactionSynchronizationManager.*; + class TransactionSynchronizationManagerTest { + DataSource dataSource = mock(DataSource.class); + Connection connection = mock(Connection.class); + + @Test + void 커넥션이_null이라면_null을_반환한다() { + Connection resource = getResource(dataSource); + Assertions.assertThat(resource).isNull(); + } + + @Test + void 커넥션을_반환한다() { + bindResource(dataSource, connection); + Connection resource = getResource(dataSource); + Assertions.assertThat(resource).isNotNull(); + } + + @Test + void 커넥션을_바인딩한다() { + bindResource(dataSource, connection); + Connection resource = getResource(dataSource); + Assertions.assertThat(resource).isNotNull(); + } } \ No newline at end of file