public void add(final User user) { this.jdbcTemplate.update( "insert into users(id, name, password) values(?,?,?)", user.getId(), user.getName(), user.getPassword()); }
public User mapRow(ResultSet rs, int i) throws SQLException { User user = new User(); user.setId(rs.getString("id")); user.setName(rs.getString("name")); user.setPassword(rs.getString("password")); return user; }
@Test(expected = IllegalStateException.class) public void cannotUpgradeLevel() { Level[] levels = Level.values(); for (Level level : levels) { if (level.nextLevel() != null) continue; user.setLevel(level); user.upgradeLevel(); } }
private void sendUpgradeEMail(User user) { SimpleMailMessage mailMessage = new SimpleMailMessage(); mailMessage.setTo(user.getEmail()); mailMessage.setFrom("*****@*****.**"); mailMessage.setSubject("Upgrade 안내"); mailMessage.setText("사용자님의 등급이 " + user.getLevel().name()); this.mailSender.send(mailMessage); }
@Test public void upgradeLevel() { Level[] levels = Level.values(); for (Level level : levels) { if (level.nextLevel() == null) continue; user.setLevel(level); user.upgradeLevel(); assertThat(user.getLevel(), is(level.nextLevel())); } }
private boolean canUpgradeLevel(User user) { Level currentLevel = user.getLevel(); switch (currentLevel) { case BASIC: return (user.getLogin() >= MIN_LOGCOUNT_FOR_SILVER); case SILVER: return (user.getRecommend() >= MIN_RECCOMEND_FOR_GOLD); case GOLD: return false; default: throw new IllegalArgumentException("Unknown Level: " + currentLevel); } }
@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)); }
public User mapRow(ResultSet rs, int rowNum) throws SQLException { User user = new User(); user.setId(rs.getString("id")); user.setName(rs.getString("name")); user.setPassword(rs.getString("password")); user.setLevel(Level.valueOf(rs.getInt("level"))); user.setLogin(rs.getInt("login")); user.setRecommend(rs.getInt("recommend")); user.setEmail(rs.getString("email")); return user; }
public void update(User user) { this.jdbcTemplate.update( this.sqlService.getSql("userUpdate"), user.getName(), user.getPassword(), user.getLevel().intValue(), user.getLogin(), user.getRecommend(), user.getEmail(), user.getId()); }
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())); } }
public void add(final User user) throws DuplicateUserIdException { try { this.jdbcTemplate.update( this.sqlService.getSql("userAdd"), user.getId(), user.getName(), user.getPassword(), user.getLevel().intValue(), user.getLogin(), user.getRecommend(), user.getEmail()); } catch (DuplicateKeyException e) { throw new DuplicateUserIdException(e); // Wrapping of Exception } }
@Test public void andAndGet() throws SQLException { dao.deleteAll(); assertThat(dao.getCount(), is(0)); dao.add(user1); dao.add(user2); assertThat(dao.getCount(), is(2)); User userget1 = dao.get(user1.getId()); assertThat(userget1.getName(), is(user1.getName())); assertThat(userget1.getPassword(), is(user1.getPassword())); User userget2 = dao.get(user2.getId()); assertThat(userget2.getName(), is(user2.getName())); assertThat(userget2.getPassword(), is(user2.getPassword())); }
protected void upgradeLevel(User user) { if (user.getId().equals(this.id)) throw new TestUserServiceException(); super.upgradeLevel(user); }
private void checkUserAndLevel(User updated, String expectedId, Level expectedLevel) { assertThat(updated.getId(), is(expectedId)); assertThat(updated.getLevel(), is(expectedLevel)); }
public void add(User user) { if (user.getLevel() == null) { user.setLevel(Level.BASIC); } userDao.add(user); }
protected void upgradeLevel(User user) { user.upgradeLevel(); userDao.update(user); sendUpgradeEMail(user); }
private void checkSameUser(User user1, User user2) { assertThat(user1.getId(), is(user2.getId())); assertThat(user1.getName(), is(user2.getName())); assertThat(user1.getPassword(), is(user2.getPassword())); }
public static void main(String[] args) throws ClassNotFoundException, SQLException { ApplicationContext context = new GenericXmlApplicationContext("applicationContext.xml"); // ApplicationContext context = new AnnotationConfigApplicationContext(DaoFactory.class); UserDaoJdbc dao = context.getBean("userDao", UserDaoJdbc.class); User user = new User(); user.setId("whiteship2"); user.setName("백기선2"); user.setPassword("married"); dao.add(user); System.out.println(user.getId() + "등록성공"); User user2 = dao.get(user.getId()); if (!user.getName().equals(user2.getName())) { System.out.println("테스트 실패 (name)"); } else if (!user.getPassword().equals(user2.getPassword())) { System.out.println("테스트 실패 (password)"); } else { System.out.println("조회 테스트 성공"); } }