210 likes | 395 Views
고급자바프로그래밍 (Advanced Java Programming). 강원대학교 컴퓨터학부 2012 년 가을학기 담당교수 정충교. 5 장 서비스 추상화. 5.1 사용자 레벨 관리 기능 추가. package springbook.user.service ; public class UserService { public void upgradeLevels () private boolean canUpgradeLevel (User user) private void upgradeLevel (User user)
E N D
고급자바프로그래밍(Advanced Java Programming) 강원대학교컴퓨터학부 2012년 가을학기 담당교수정충교
5.1 사용자 레벨 관리 기능 추가 packagespringbook.user.service; publicclassUserService { public void upgradeLevels() private booleancanUpgradeLevel(User user) private void upgradeLevel(User user) public void add(User user) }
UserDao package springbook.user.dao; public interface UserDao { void add(User user); User get(String user); List<User> getAll(); void deleteAll(); intgetCount(); void update(User user); }
@RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations="/test-applicationContext.xml") public class UserServiceTest { @AutowiredUserServiceuserService; @AutowiredUserDaouserDao; List<User> users; // test fixture @Before public void setUp() { users = Arrays.asList( new User("bumjin", "박범진", "p1", Level.BASIC, MIN_LOGCOUNT_FOR_SILVER-1, 0), new User("joytouch", "강명성", "p2", Level.BASIC, MIN_LOGCOUNT_FOR_SILVER, 0), new User("erwins", "신승한", "p3", Level.SILVER, 60, MIN_RECCOMEND_FOR_GOLD-1), new User("madnite1", "이상호", "p4", Level.SILVER, 60, MIN_RECCOMEND_FOR_GOLD), new User("green", "오민규", "p5", Level.GOLD, 100, Integer.MAX_VALUE) ); }
@Test public void upgradeLevels() { userDao.deleteAll(); for(User user : users) userDao.add(user); userService.upgradeLevels(); checkLevelUpgraded(users.get(0), false); checkLevelUpgraded(users.get(1), true); checkLevelUpgraded(users.get(2), false); checkLevelUpgraded(users.get(3), true); checkLevelUpgraded(users.get(4), false); }
private void checkLevelUpgraded(User user, boolean upgraded) { User userUpdate = userDao.get(user.getId()); if (upgraded) { assertThat(userUpdate.getLevel(), is(user.getLevel().nextLevel())); } else { assertThat(userUpdate.getLevel(), is(user.getLevel())); } }
@Test public void add() { userDao.deleteAll(); User userWithLevel = users.get(4); // GOLD 레벨 User userWithoutLevel = users.get(0); userWithoutLevel.setLevel(null); userService.add(userWithLevel); userService.add(userWithoutLevel); User userWithLevelRead = userDao.get(userWithLevel.getId()); User userWithoutLevelRead = userDao.get(userWithoutLevel.getId()); assertThat(userWithLevelRead.getLevel(), is(userWithLevel.getLevel())); assertThat(userWithoutLevelRead.getLevel(), is(Level.BASIC)); }
Transaction • 리스트 5-38
Local Transaction • 리스트 5-41
JdbcTemplate의 기능 • try/catch/finally 작업 흐름 지원 • SQLException예외 변환 • transaction
추상화 • 리스트 5-45