-
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단계] 주디(오은비) 미션 제출합니다. #596
Merged
Merged
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
dc70d1d
패키지 위치 변경 및 코드 정리
kang-hyungu 7182a49
패키지 위치 변경 및 코드 정리
kang-hyungu f8f2c3b
feat: 1단계 구현
eunbii0213 5b17814
feat: 2단계 구현
eunbii0213 b74a465
feat: 2단계 추가 구현
eunbii0213 8263b1d
Merge branch 'eunbii0213' into step2
eunbii0213 c37cf5b
feat: UserDao 중복 코드 제거
eunbii0213 159df88
feat: JdbcTemplate 중복 코드 제거
eunbii0213 d183358
Merge remote-tracking branch 'origin/step2' into step2
eunbii0213 15da493
feat: 커스텀 예외 구현
eunbii0213 034f104
feat: step3 구현
eunbii0213 4669db8
feat: step4 구현
eunbii0213 f4c2f4b
feat: step4 구현
eunbii0213 a9c8274
Merge remote-tracking branch 'origin/step4' into step4
eunbii0213 7b5306c
feat: transaction 적용
eunbii0213 ea2dcd5
feat: SizeException 적용
eunbii0213 c9cd8f8
test: test 수정
eunbii0213 27379b0
feat: findById 트랜잭션 적용
eunbii0213 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 36 additions & 0 deletions
36
app/src/main/java/com/techcourse/service/AppUserService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
@Override | ||
public void insert(final User user) { | ||
userDao.insert(user); | ||
} | ||
|
||
@Override | ||
public void changePassword(final long id, final String newPassword, final String createBy) { | ||
final var user = findById(id); | ||
user.changePassword(newPassword); | ||
userDao.update(user); | ||
userHistoryDao.log(new UserHistory(user, createBy)); | ||
} | ||
|
||
} |
83 changes: 83 additions & 0 deletions
83
app/src/main/java/com/techcourse/service/TxUserService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
package com.techcourse.service; | ||
|
||
import com.techcourse.config.DataSourceConfig; | ||
import com.techcourse.domain.User; | ||
import java.sql.SQLException; | ||
import org.springframework.dao.DataAccessException; | ||
import org.springframework.jdbc.datasource.DataSourceUtils; | ||
|
||
public class TxUserService implements UserService { | ||
|
||
private final UserService userService; | ||
|
||
public TxUserService(final UserService userService) { | ||
this.userService = userService; | ||
} | ||
|
||
@Override | ||
public User findById(final long id) { | ||
User user; | ||
|
||
final var dataSource = DataSourceConfig.getInstance(); | ||
final var connection = DataSourceUtils.getConnection(dataSource); | ||
try { | ||
connection.setAutoCommit(false); | ||
user = userService.findById(id); | ||
|
||
connection.commit(); | ||
} catch (final Exception e) { | ||
try { | ||
connection.rollback(); | ||
} catch (SQLException ex) { | ||
throw new DataAccessException(ex); | ||
} | ||
throw new DataAccessException(e); | ||
} finally { | ||
DataSourceUtils.releaseConnection(connection, dataSource); | ||
} | ||
|
||
return user; | ||
} | ||
|
||
@Override | ||
public void insert(final User user) { | ||
final var dataSource = DataSourceConfig.getInstance(); | ||
final var connection = DataSourceUtils.getConnection(dataSource); | ||
try { | ||
connection.setAutoCommit(false); | ||
userService.insert(user); | ||
|
||
connection.commit(); | ||
} catch (final Exception e) { | ||
try { | ||
connection.rollback(); | ||
} catch (SQLException ex) { | ||
throw new DataAccessException(ex); | ||
} | ||
throw new DataAccessException(e); | ||
} finally { | ||
DataSourceUtils.releaseConnection(connection, dataSource); | ||
} | ||
} | ||
|
||
@Override | ||
public void changePassword(final long id, final String newPassword, final String createBy) { | ||
final var dataSource = DataSourceConfig.getInstance(); | ||
final var connection = DataSourceUtils.getConnection(dataSource); | ||
try { | ||
connection.setAutoCommit(false); | ||
userService.changePassword(id, newPassword, createBy); | ||
|
||
connection.commit(); | ||
} catch (final Exception e) { | ||
try { | ||
connection.rollback(); | ||
} catch (SQLException ex) { | ||
throw new DataAccessException(ex); | ||
} | ||
throw new DataAccessException(e); | ||
} finally { | ||
DataSourceUtils.releaseConnection(connection, dataSource); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,47 +1,10 @@ | ||
package com.techcourse.service; | ||
|
||
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 java.sql.SQLException; | ||
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 User findById(final long id) { | ||
return userDao.findById(id); | ||
} | ||
|
||
public void insert(final User user) { | ||
userDao.insert(user); | ||
} | ||
|
||
public void changePassword(final long id, final String newPassword, final String createBy) throws SQLException { | ||
final var connection = DataSourceConfig.getInstance().getConnection(); | ||
try { | ||
connection.setAutoCommit(false); | ||
final var user = findById(id); | ||
user.changePassword(newPassword); | ||
userDao.update(connection, user); | ||
userHistoryDao.log(connection, new UserHistory(user, createBy)); | ||
|
||
connection.commit(); | ||
} catch (final Exception e) { | ||
connection.rollback(); | ||
throw new DataAccessException(e); | ||
} finally { | ||
connection.close(); | ||
} | ||
} | ||
public interface UserService { | ||
|
||
User findById(final long id); | ||
void insert(final User user); | ||
void changePassword(final long id, final String newPassword, final String createBy); | ||
} |
43 changes: 43 additions & 0 deletions
43
app/src/test/java/com/techcourse/service/AppUserServiceTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package com.techcourse.service; | ||
|
||
import com.techcourse.config.DataSourceConfig; | ||
import com.techcourse.dao.UserDao; | ||
import com.techcourse.dao.UserHistoryDao; | ||
import com.techcourse.domain.User; | ||
import com.techcourse.support.jdbc.init.DatabasePopulatorUtils; | ||
import java.sql.SQLException; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.springframework.jdbc.core.JdbcTemplate; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
class AppUserServiceTest { | ||
|
||
private JdbcTemplate jdbcTemplate; | ||
private UserDao userDao; | ||
|
||
@BeforeEach | ||
void setUp() { | ||
this.jdbcTemplate = new JdbcTemplate(DataSourceConfig.getInstance()); | ||
this.userDao = new UserDao(jdbcTemplate); | ||
|
||
DatabasePopulatorUtils.execute(DataSourceConfig.getInstance()); | ||
final var user = new User("gugu", "password", "[email protected]"); | ||
userDao.insert(user); | ||
} | ||
|
||
@Test | ||
void testChangePassword() throws SQLException { | ||
final var userHistoryDao = new UserHistoryDao(jdbcTemplate); | ||
final var userService = new AppUserService(userDao, userHistoryDao); | ||
|
||
final var newPassword = "qqqqq"; | ||
final var createBy = "gugu"; | ||
userService.changePassword(1L, newPassword, createBy); | ||
|
||
final var actual = userService.findById(1L); | ||
|
||
assertThat(actual.getPassword()).isEqualTo(newPassword); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,7 +2,6 @@ | |
|
||
import com.techcourse.dao.UserHistoryDao; | ||
import com.techcourse.domain.UserHistory; | ||
import java.sql.Connection; | ||
import org.springframework.dao.DataAccessException; | ||
import org.springframework.jdbc.core.JdbcTemplate; | ||
|
||
|
@@ -13,8 +12,8 @@ public MockUserHistoryDao(final JdbcTemplate jdbcTemplate) { | |
} | ||
|
||
@Override | ||
public void log(final Connection connection, final UserHistory userHistory) { | ||
System.out.println("로그뜰까요아닐까용오오오"); | ||
Comment on lines
-16
to
-17
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. ㅋㅋㅋㅋㅋ여기 삭제 기록도 남네요 |
||
public void log(final UserHistory userHistory) { | ||
throw new DataAccessException(); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,23 +2,17 @@ | |
|
||
import com.techcourse.config.DataSourceConfig; | ||
import com.techcourse.dao.UserDao; | ||
import com.techcourse.dao.UserHistoryDao; | ||
import com.techcourse.domain.User; | ||
import com.techcourse.support.jdbc.init.DatabasePopulatorUtils; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Disabled; | ||
import org.junit.jupiter.api.Test; | ||
import org.springframework.dao.DataAccessException; | ||
import org.springframework.jdbc.core.JdbcTemplate; | ||
|
||
import java.sql.SQLException; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.junit.jupiter.api.Assertions.assertAll; | ||
import static org.junit.jupiter.api.Assertions.assertThrows; | ||
|
||
@Disabled | ||
class UserServiceTest { | ||
class TxUserServiceTest { | ||
|
||
private JdbcTemplate jdbcTemplate; | ||
private UserDao userDao; | ||
|
@@ -32,37 +26,24 @@ void setUp() { | |
final var user = new User("gugu", "password", "[email protected]"); | ||
userDao.insert(user); | ||
} | ||
|
||
@Test | ||
void testChangePassword() throws SQLException { | ||
final var userHistoryDao = new UserHistoryDao(jdbcTemplate); | ||
final var userService = new UserService(userDao, userHistoryDao); | ||
|
||
final var newPassword = "qqqqq"; | ||
final var createBy = "gugu"; | ||
userService.changePassword(1L, newPassword, createBy); | ||
|
||
final var actual = userService.findById(1L); | ||
|
||
assertThat(actual.getPassword()).isEqualTo(newPassword); | ||
} | ||
|
||
@Test | ||
void testTransactionRollback() { | ||
// 트랜잭션 롤백 테스트를 위해 mock으로 교체 | ||
// 트랜잭션이 정상 동작하는지 확인하기 위해 의도적으로 MockUserHistoryDao에서 예외를 발생시킨다. | ||
final MockUserHistoryDao mockUserHistoryDao = new MockUserHistoryDao(jdbcTemplate); | ||
final var userService = new UserService(userDao, mockUserHistoryDao); | ||
final var userHistoryDao = new MockUserHistoryDao(jdbcTemplate); | ||
// 애플리케이션 서비스 | ||
final var appUserService = new AppUserService(userDao, userHistoryDao); | ||
// 트랜잭션 서비스 추상화 | ||
final var userService = new TxUserService(appUserService); | ||
|
||
final var newPassword = "newPassword"; | ||
final var createBy = "gugu"; | ||
// 트랜잭션이 정상 동작하는지 확인하기 위해 의도적으로 MockUserHistoryDao에서 예외를 발생시킨다. | ||
assertThrows(DataAccessException.class, | ||
() -> userService.changePassword(1L, newPassword, createBy)); | ||
|
||
final var actual = userService.findById(1L); | ||
|
||
assertAll( | ||
() -> assertThrows(DataAccessException.class, | ||
() -> userService.changePassword(1L, newPassword, createBy)), | ||
() -> assertThat(actual.getPassword()).isNotEqualTo(newPassword) | ||
); | ||
assertThat(actual.getPassword()).isNotEqualTo(newPassword); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
꼼꼼한 테스트 굿굿!! 👍🏻