-
Notifications
You must be signed in to change notification settings - Fork 300
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[JDBC 라이브러리 구현하기 - 4단계] 가비 미션 제출합니다. #523
Changes from all commits
0b720af
e21617d
1ae2ea5
b05df24
8637110
9c51e1d
b072844
87a8363
f47ee27
06d7f7c
c89797e
faa6f7d
26a140a
42d84c0
f0061ad
a281e05
7ad67af
0077696
245d83e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package com.techcourse.service; | ||
|
||
import com.techcourse.dao.UserDao; | ||
import com.techcourse.dao.UserHistoryDao; | ||
import com.techcourse.domain.User; | ||
import com.techcourse.domain.UserHistory; | ||
|
||
public class AppUserService implements UserService { | ||
|
||
private final UserDao userDao; | ||
private final UserHistoryDao userHistoryDao; | ||
|
||
public AppUserService(final UserDao userDao, final UserHistoryDao userHistoryDao) { | ||
this.userDao = userDao; | ||
this.userHistoryDao = userHistoryDao; | ||
} | ||
|
||
@Override | ||
public User findById(final long id) { | ||
return userDao.findById(id).orElseThrow(); | ||
} | ||
|
||
@Override | ||
public void insert(final User user) { | ||
userDao.insert(user); | ||
} | ||
|
||
@Override | ||
public void changePassword(final long id, final String newPassword, final String createBy) { | ||
final User user = findById(id); | ||
user.changePassword(newPassword); | ||
userDao.update(user); | ||
userHistoryDao.log(new UserHistory(user, createBy)); | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package com.techcourse.service; | ||
|
||
import com.techcourse.domain.User; | ||
import org.springframework.transaction.TransactionManager; | ||
|
||
public class TxUserService implements UserService { | ||
|
||
private final UserService userService; | ||
private final TransactionManager transactionManager; | ||
|
||
public TxUserService(final UserService userService, final TransactionManager transactionManager) { | ||
this.userService = userService; | ||
this.transactionManager = transactionManager; | ||
} | ||
|
||
@Override | ||
public User findById(final long id) { | ||
return transactionManager.runForObject(() -> userService.findById(id)); | ||
} | ||
|
||
@Override | ||
public void insert(final User user) { | ||
transactionManager.run(() -> userService.insert(user)); | ||
} | ||
|
||
@Override | ||
public void changePassword(final long id, final String newPassword, final String createBy) { | ||
transactionManager.run(() -> userService.changePassword(id, newPassword, createBy)); | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,60 +1,13 @@ | ||
package com.techcourse.service; | ||
|
||
import java.sql.Connection; | ||
import java.sql.SQLException; | ||
import java.util.Optional; | ||
|
||
import com.techcourse.config.DataSourceConfig; | ||
import com.techcourse.dao.UserDao; | ||
import com.techcourse.dao.UserHistoryDao; | ||
import com.techcourse.domain.User; | ||
import com.techcourse.domain.UserHistory; | ||
import org.springframework.dao.DataAccessException; | ||
|
||
public class UserService { | ||
|
||
private final UserDao userDao; | ||
private final UserHistoryDao userHistoryDao; | ||
|
||
public UserService(final UserDao userDao, final UserHistoryDao userHistoryDao) { | ||
this.userDao = userDao; | ||
this.userHistoryDao = userHistoryDao; | ||
} | ||
|
||
public Optional<User> findById(final long id) { | ||
return userDao.findById(id); | ||
} | ||
|
||
public void insert(final User user) { | ||
userDao.insert(user); | ||
} | ||
public interface UserService { | ||
|
||
public void changePassword(final long id, final String newPassword, final String createBy) { | ||
Connection connection = null; | ||
try { | ||
connection = DataSourceConfig.getInstance().getConnection(); | ||
connection.setAutoCommit(false); | ||
User findById(final long id); | ||
|
||
final User user = findById(id).orElseThrow(); | ||
user.changePassword(newPassword); | ||
userDao.update(connection, user); | ||
userHistoryDao.log(connection, new UserHistory(user, createBy)); | ||
void insert(final User user); | ||
|
||
connection.commit(); | ||
} catch (final SQLException e) { | ||
try { | ||
connection.rollback(); | ||
} catch (final SQLException rollbackException) { | ||
throw new DataAccessException(rollbackException); | ||
} | ||
throw new DataAccessException(e); | ||
} finally { | ||
try { | ||
connection.close(); | ||
} catch (final SQLException e) { | ||
throw new DataAccessException(e); | ||
} | ||
} | ||
} | ||
void changePassword(final long id, final String newPassword, final String createBy); | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,18 +9,21 @@ | |
import org.junit.jupiter.api.Test; | ||
import org.springframework.dao.DataAccessException; | ||
import org.springframework.jdbc.core.JdbcTemplate; | ||
import org.springframework.transaction.TransactionManager; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.junit.jupiter.api.Assertions.assertThrows; | ||
|
||
class UserServiceTest { | ||
|
||
private JdbcTemplate jdbcTemplate; | ||
private TransactionManager transactionManager; | ||
private UserDao userDao; | ||
|
||
@BeforeEach | ||
void setUp() { | ||
this.jdbcTemplate = new JdbcTemplate(DataSourceConfig.getInstance()); | ||
this.transactionManager = new TransactionManager(DataSourceConfig.getInstance()); | ||
this.userDao = new UserDao(jdbcTemplate); | ||
|
||
DatabasePopulatorUtils.execute(DataSourceConfig.getInstance()); | ||
|
@@ -31,31 +34,35 @@ void setUp() { | |
@Test | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 여기 setUp()에서 사용되는 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 꼼꼼하게 봐주셔서 감사합니다! 반영했습니다~! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ㅎㅎㅎ... 진짜 궁금해서 여쭤본거였는데.... ☺ There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 저는 setUp() 메서드에 DatabasePopulatorUtils라는 다른 종류의 클래스를 사용하는걸 미처 보지 못했어요 놓친 것이 있다면 알려주심 감사하겠습니다.. |
||
void testChangePassword() { | ||
final UserHistoryDao userHistoryDao = new UserHistoryDao(jdbcTemplate); | ||
final UserService userService = new UserService(userDao, userHistoryDao); | ||
final UserService userService = new AppUserService(userDao, userHistoryDao); | ||
|
||
final String newPassword = "qqqqq"; | ||
final String createBy = "gugu"; | ||
userService.changePassword(1L, newPassword, createBy); | ||
|
||
final User actual = userService.findById(1L).orElseThrow(); | ||
final User actual = userService.findById(1L); | ||
|
||
assertThat(actual.getPassword()).isEqualTo(newPassword); | ||
} | ||
|
||
@Test | ||
void testTransactionRollback() { | ||
// 트랜잭션 롤백 테스트를 위해 mock으로 교체 | ||
final UserHistoryDao userHistoryDao = new MockUserHistoryDao(jdbcTemplate); | ||
final UserService userService = new UserService(userDao, userHistoryDao); | ||
final var userHistoryDao = new MockUserHistoryDao(jdbcTemplate); | ||
// 애플리케이션 서비스 | ||
final UserService appUserService = new AppUserService(userDao, userHistoryDao); | ||
// 트랜잭션 서비스 추상화 | ||
final UserService txUserService = new TxUserService(appUserService, transactionManager); | ||
|
||
final String newPassword = "newPassword"; | ||
final String createBy = "gugu"; | ||
final var newPassword = "newPassword"; | ||
final var createBy = "gugu"; | ||
// 트랜잭션이 정상 동작하는지 확인하기 위해 의도적으로 MockUserHistoryDao에서 예외를 발생시킨다. | ||
assertThrows(DataAccessException.class, | ||
() -> userService.changePassword(1L, newPassword, createBy)); | ||
() -> txUserService.changePassword(1L, newPassword, createBy)); | ||
|
||
final User actual = userService.findById(1L).orElseThrow(); | ||
final var actual = txUserService.findById(1L); | ||
|
||
assertThat(actual.getPassword()).isNotEqualTo(newPassword); | ||
} | ||
|
||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
릴리즈까지 꼼꼼!!! 👍👍