UserServiceTest
UserService
UserDao
UserDaoJdbc
UserDaoJpa
public void upgradeLevels() {
List<User> users = userDao.getAll();
for(User user : users) {
Boolean changed = null;
if (user.getLevel() == Level.BASIC && user.getLogin() >= 50) {
user.setLevel(Level.SILVER);
changed = true;
} else if (user.getLevel() == Level.SILVER && user.getRecommend() >= 30) {
user.setLevel(Level.GOLD);
changed = true;
} else if (user.getLevel() == Level.GOLD) {
changed = false;
} else {
changed = false;
}
if (changed) { userDao.update(user);
}
}
337 page 참고
public void upgradeLevels() {
List<User> users = userDao.getAll();
for(User user : users) {
if (canUpgradeLevel(user)) {
upgradeLevel(user);
}
}
}
private boolean canUpgradeLevel(User user) {
Level currentLevel = user.getLevel();
switch(currentLevel) {
case BASIC: return (user.getLogin() >= 50);
case SILVER: return (user.getRecomment() >= 30);
case GOLD: return false;
default: throw new IllegalArgumentException("Unknown Level : " + currentLevel);
}
}
private void upgradeLevel(User user) {
// 객체지향적인 코드는 다른 오브젝트의 데이터를 가져와서 작업하는 대신
// 데이터를 갖고 있는 다른 오브젝트에게 작업을 해달라고 요청한다.
user.upgradeLevel();
userDao.update(user);
}
// User의 레벨 업그레이드 작업용 메소드
public void upgradeLevel() {
Level nextLevel = this.level.nextLevel();
if (nextLevel == null) {
throw new IllegalStateException(this.level + "은 업그레이드가 불가능합니다.");
} else {
this.level = nextLevel;
}
}
public void upgradeLevels() throws Exception {
// (1) DB Connection 생성
// (2) 트랜잭션 시작
try {
// (3) DAO 메소드 호출
// (4) 트랜잭션 커밋
}
catch (Exception e) {
// (5) 트랜잭션 롤백
throw e;
}
finally {
// (6) DB Connection 종료
}
}
Connection 오브젝트를 파라미터로 전달해줘야 한다.
359 페이지 참고
public void upgradeLevels() throws Exception {
TransactionSynchronizationManager.initSynchronization();
Connection c = DataSourceUtils.getConnection(dataSource);
c.setAutoCommit(false);
try {
List<User> users = userDao.getAll();
for (User user : users) {
if (canUpgradeLevel(user)) {
upgradeLevel(user);
}
}
c.commit();
} catch (Exception e) {
c.rollback();
throw e;
} finally {
DataSourceUtils.releaseConnection(c, dataSource);
TransactionSynchronizationManager.unbindResource(this.dataSource);
TransactionSynchronizationManager.clearSynchronization();
}
}
// 스프링의 트랜잭션 추상화 API를 적용한 upgradeLevels()
public void upgradeLevels() {
PlatformTansactionManager transactionManager =
new DataSourceTransactionManager(datasource);
TransactionStatus status =
transactionManager.getTransaction(new DefaultTransactionDefinition());
try {
List<User> users = userDao.getAll();
for (User user : users) {
if (canUpgradeLevel(user)) {
upgradeLevel(user);
}
}
transactionManager.commit(status);
} catch (RuntimeException e) [
transactionManager.rollback(status);
throw e;
}
}
public class DummyMailSender implements MailSender {
public void send(SimpleMailMessage mailMessage) throws MailException {
}
public void send(SimpleMailMessage[] mailMessage) throws MailException {
}
}
static class MockMailSender implements MailSender {
private List<String> requests = new ArrayList<String>();
public List<String> getRequests() }
return requests;
}
public void send(SimpleMailMessage mailMessage) throws MailException {
requests.add(mailMessage.getTo()[0]);
}
public void send(SimpleMailMessage[] mailMessage) throws MailException {
}
}