-
Notifications
You must be signed in to change notification settings - Fork 0
6주차 #6
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
base: main
Are you sure you want to change the base?
The head ref may contain hidden characters: "6\uC8FC\uCC28"
6주차 #6
Changes from all commits
3bffa85
0e57845
3cc7e8d
df80be4
eafa602
7324774
41df1b3
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 |
|---|---|---|
| @@ -1,14 +1,19 @@ | ||
| package springbook.user.service; | ||
|
|
||
| import java.util.List; | ||
| import javax.sql.DataSource; | ||
| import org.springframework.jdbc.datasource.DataSourceTransactionManager; | ||
| import org.springframework.transaction.PlatformTransactionManager; | ||
| import org.springframework.transaction.TransactionStatus; | ||
| import org.springframework.transaction.support.DefaultTransactionDefinition; | ||
| import springbook.user.dao.UserDao; | ||
| import springbook.user.domain.Level; | ||
| import springbook.user.domain.User; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| public class UserService { | ||
| UserDao userDao; | ||
| UserLevelUpgradePolicy userLevelUpgradePolicy; | ||
| PlatformTransactionManager transactionManager; | ||
|
|
||
| public void setUserDao(UserDao userDao) { | ||
| this.userDao = userDao; | ||
|
|
@@ -18,6 +23,10 @@ public void setUserLevelUpgradePolicy(UserLevelUpgradePolicy userLevelUpgradePol | |
| this.userLevelUpgradePolicy = userLevelUpgradePolicy; | ||
| } | ||
|
|
||
| public void setTransactionManager(PlatformTransactionManager transactionManager) { | ||
| this.transactionManager = transactionManager; | ||
| } | ||
|
|
||
| public void add(User user) { | ||
| if (user.getLevel() == null) { | ||
| user.setLevel(Level.BASIC); | ||
|
|
@@ -26,12 +35,23 @@ public void add(User user) { | |
| } | ||
|
|
||
| public void upgradeLevels() { | ||
| List<User> users = userDao.getAll(); | ||
| for (User user : users) { | ||
| if (userLevelUpgradePolicy.canUpgradeLevel(user)) { | ||
| userLevelUpgradePolicy.upgradeLevel(user); | ||
| userDao.update(user); | ||
| TransactionStatus status = transactionManager.getTransaction(new DefaultTransactionDefinition()); | ||
|
Member
Author
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. transaction을 시작한다는 의미라고 생각하자.
Member
Author
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. 트랜잭션이 |
||
| try { | ||
| List<User> users = userDao.getAll(); | ||
| for (User user : users) { | ||
| upgradeLevel(user); | ||
| } | ||
| transactionManager.commit(status); | ||
|
Member
Author
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. 트랜잭션에 대한 조작이 필요할 때 PlatformTransactionManager의 메서드에 트랜잭션을 인자로 전달해주면 된당. |
||
| } catch (Exception e) { | ||
| transactionManager.rollback(status); | ||
| throw e; | ||
| } | ||
| } | ||
|
|
||
| protected void upgradeLevel(User user) { | ||
| if (userLevelUpgradePolicy.canUpgradeLevel(user)) { | ||
| userLevelUpgradePolicy.upgradeLevel(user); | ||
| userDao.update(user); | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,49 +1,77 @@ | ||
| package springbook.user.service; | ||
|
|
||
| import static org.hamcrest.CoreMatchers.is; | ||
| import static org.junit.Assert.assertThat; | ||
| import static org.junit.Assert.fail; | ||
| import static springbook.user.service.UserLevelUpgradePolicyImpl.MIN_LOGCOUNT_FOR_SILVER; | ||
| import static springbook.user.service.UserLevelUpgradePolicyImpl.MIN_RECCOMENT_FOR_GOLD; | ||
|
|
||
| import java.util.Arrays; | ||
| import java.util.List; | ||
| import org.junit.Before; | ||
| import org.junit.Test; | ||
| import org.junit.runner.RunWith; | ||
| import org.springframework.beans.factory.annotation.Autowired; | ||
| import org.springframework.test.context.ContextConfiguration; | ||
| import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; | ||
| import org.springframework.transaction.PlatformTransactionManager; | ||
| import springbook.user.dao.UserDao; | ||
| import springbook.user.domain.Level; | ||
| import springbook.user.domain.User; | ||
|
|
||
| import java.util.Arrays; | ||
| import java.util.List; | ||
|
|
||
| import static org.hamcrest.CoreMatchers.is; | ||
| import static org.junit.Assert.assertThat; | ||
| import static springbook.user.service.UserLevelUpgradePolicyImpl.MIN_LOGCOUNT_FOR_SILVER; | ||
| import static springbook.user.service.UserLevelUpgradePolicyImpl.MIN_RECCOMENT_FOR_GOLD; | ||
|
|
||
| @RunWith(SpringJUnit4ClassRunner.class) | ||
| @ContextConfiguration(locations = "/test-applicationContext.xml") | ||
| public class UserServiceTest { | ||
| static class TestUserService extends UserService { | ||
| private String id; | ||
|
|
||
| private TestUserService(String id) { | ||
| this.id = id; | ||
| } | ||
|
|
||
| @Override | ||
| protected void upgradeLevel(User user) { | ||
| if (user.getId().equals(this.id)) { | ||
| throw new TestUserServiceException(); | ||
| } | ||
| super.upgradeLevel(user); | ||
| } | ||
|
Comment on lines
+33
to
+38
Member
Author
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. UserService에서 |
||
| } | ||
|
|
||
| static class TestUserServiceException extends RuntimeException { | ||
| } | ||
|
|
||
| @Autowired | ||
| UserService userService; | ||
|
|
||
| @Autowired | ||
| UserDao userDao; | ||
|
|
||
| @Autowired | ||
| UserLevelUpgradePolicy userLevelUpgradePolicy; | ||
|
|
||
| @Autowired | ||
| PlatformTransactionManager transactionManager; | ||
|
|
||
| List<User> users; | ||
|
|
||
| @Before | ||
| public void setUp() { | ||
| users = Arrays.asList( | ||
| new User("kayoung", "윤가영", "p1", Level.BASIC, MIN_LOGCOUNT_FOR_SILVER - 1, 0), | ||
| new User("haneul", "이하늘", "p2", Level.BASIC, MIN_LOGCOUNT_FOR_SILVER, 0), | ||
| new User("oieuoa", "윤실버", "p3", Level.SILVER, 60, MIN_RECCOMENT_FOR_GOLD - 1), | ||
| new User("silversky", "스카이", "p4", Level.SILVER, 60, MIN_RECCOMENT_FOR_GOLD), | ||
| new User("blue", "교수님", "p5", Level.GOLD, 100, Integer.MAX_VALUE) | ||
| new User("kayoung", "윤가영", "p1", "kayoung@email.com", Level.BASIC, MIN_LOGCOUNT_FOR_SILVER - 1, 0), | ||
| new User("haneul", "이하늘", "p2", "haneul@email.com", Level.BASIC, MIN_LOGCOUNT_FOR_SILVER, 0), | ||
| new User("oieuoa", "윤실버", "p3", "oieuoa@email.com", Level.SILVER, 60, MIN_RECCOMENT_FOR_GOLD - 1), | ||
| new User("silversky", "스카이", "p4", "silverskey@email.com", Level.SILVER, 60, MIN_RECCOMENT_FOR_GOLD), | ||
| new User("blue", "교수님", "p5", "blue@email.com", Level.GOLD, 100, Integer.MAX_VALUE) | ||
| ); | ||
| } | ||
|
|
||
| @Test | ||
| public void upgradeLevels() { | ||
| public void upgradeLevels() throws Exception { | ||
| userDao.deleteAll(); | ||
| for (User user : users) userDao.add(user); | ||
| for (User user : users) { | ||
| userDao.add(user); | ||
| } | ||
|
|
||
| userService.upgradeLevels(); | ||
|
|
||
|
|
@@ -82,4 +110,22 @@ private void checkLevelUpgraded(User user, Boolean upgraded) { | |
| } | ||
| } | ||
|
|
||
| @Test | ||
| public void upgradeAllOrNothing() { | ||
| // user fixture 사용 | ||
| TestUserService testUserService = new TestUserService(users.get(3).getId()); | ||
| testUserService.setUserDao(this.userDao); // userDao를 수동 DI해줌 | ||
| testUserService.setUserLevelUpgradePolicy(this.userLevelUpgradePolicy); | ||
| testUserService.setTransactionManager(transactionManager); | ||
| userDao.deleteAll(); | ||
| for (User user : users) { | ||
| userDao.add(user); | ||
| } | ||
| try { | ||
| testUserService.upgradeLevels(); | ||
| fail("TetUserSericeException expected"); | ||
| } catch (TestUserServiceException e) { | ||
| } | ||
| checkLevelUpgraded(users.get(1), false); | ||
|
Member
Author
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. p353. 테스트는 실패한다. |
||
| } | ||
| } | ||
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.
구현체를 바꾸고 싶으면 여기에서 클래스를 바꿔주면 된다.
ex) JTA 를 사용하고 싶은 경우
이런식으로 바꾸면 된당.