Skip to content
This repository has been archived by the owner on Apr 16, 2024. It is now read-only.

Commit

Permalink
[ch02] Update UserDaoTest.
Browse files Browse the repository at this point in the history
  • Loading branch information
dhsim86 authored and Dongho Sim committed Jul 29, 2017
1 parent b27d965 commit 1ec2601
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 14 deletions.
19 changes: 14 additions & 5 deletions src/main/java/ch01/springbook/user/dao/UserDao.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

import javax.sql.DataSource;

import org.springframework.dao.EmptyResultDataAccessException;

import ch01.springbook.user.domain.User;

public class UserDao {
Expand Down Expand Up @@ -53,17 +55,24 @@ public User get(String id) throws ClassNotFoundException, SQLException {
ps.setString(1, id);

ResultSet rs = ps.executeQuery();
rs.next();

User user = new User();
user.setId(rs.getString("id"));
user.setName(rs.getString("name"));
user.setPassword(rs.getString("password"));
User user = null;

if (rs.next()) {
user = new User();
user.setId(rs.getString("id"));
user.setName(rs.getString("name"));
user.setPassword(rs.getString("password"));
}

rs.close();
ps.close();
c.close();

if (user == null) {
throw new EmptyResultDataAccessException(1);
}

return user;
}

Expand Down
10 changes: 10 additions & 0 deletions src/main/java/ch01/springbook/user/domain/User.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,16 @@ public class User {
String name;
String password;

public User() {

}

public User(String id, String name, String password) {
this.id = id;
this.name = name;
this.password = password;
}

public String getId() {
return id;
}
Expand Down
55 changes: 46 additions & 9 deletions src/test/java/ch01/springbook/user/UserDaoTest.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
package ch01.springbook.user;

import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertThat;

import java.sql.SQLException;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.GenericXmlApplicationContext;
import org.springframework.dao.EmptyResultDataAccessException;

import ch01.springbook.user.dao.UserDao;
import ch01.springbook.user.domain.User;
Expand All @@ -21,21 +21,58 @@ public void addAndGet() throws SQLException, ClassNotFoundException {
new GenericXmlApplicationContext("applicationContext.xml");
UserDao dao = applicationContext.getBean("userDao", UserDao.class);

User user1 = new User("whiteship", "test", "no1");
User user2 = new User("blackship", "test", "no2");

// Delete Test
dao.deleteAll();
assertThat(dao.getCount(), is(0));

User user = new User();
user.setId("whiteship");
user.setName("심동호");
user.setPassword("1234");
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()));
}

@Test
public void count() throws SQLException, ClassNotFoundException {
ApplicationContext applicationContext =
new GenericXmlApplicationContext("applicationContext.xml");
UserDao dao = applicationContext.getBean("userDao", UserDao.class);

User user1 = new User("test01", "test", "no1");
User user2 = new User("test02", "test", "no2");
User user3 = new User("test03", "test", "no3");

dao.deleteAll();
assertThat(dao.getCount(), is(0));

dao.add(user);
dao.add(user1);
assertThat(dao.getCount(), is(1));

User user2 = dao.get(user.getId());
dao.add(user2);
assertThat(dao.getCount(), is(2));

dao.add(user3);
assertThat(dao.getCount(), is(3));
}

@Test(expected = EmptyResultDataAccessException.class)
public void getUserFailure() throws SQLException, ClassNotFoundException {
ApplicationContext applicationContext =
new GenericXmlApplicationContext("applicationContext.xml");
UserDao dao = applicationContext.getBean("userDao", UserDao.class);

dao.deleteAll();
assertThat(dao.getCount(), is(0));

assertThat(user2.getName(), is(user.getName()));
assertThat(user2.getPassword(), is(user.getPassword()));
dao.get("unknown");
}
}

0 comments on commit 1ec2601

Please sign in to comment.