Skip to content

Commit

Permalink
Feat: 토비의 스프링 2장 학습 마무리
Browse files Browse the repository at this point in the history
  • Loading branch information
happysubin committed Feb 14, 2023
1 parent a437869 commit d31fcf6
Show file tree
Hide file tree
Showing 4 changed files with 127 additions and 33 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package tobyspring.vol1.chapter_1.user.dao;

import org.springframework.dao.EmptyResultDataAccessException;
import tobyspring.vol1.chapter_1.user.domain.User;

import javax.sql.DataSource;
Expand Down Expand Up @@ -37,16 +38,47 @@ 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;
}

public void deleteAll() throws SQLException {
Connection c = dataSource.getConnection();
PreparedStatement ps = c.prepareStatement("delete from users");
ps.executeUpdate();

ps.close();;
c.close();
}

public int getCount() throws SQLException {
Connection c = dataSource.getConnection();

PreparedStatement ps = c.prepareStatement("select count(*) from users");

ResultSet rs = ps.executeQuery();
rs.next();
int count = rs.getInt(1);

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

return count;
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,12 +1,20 @@
package tobyspring.vol1.chapter_1.user.domain;

import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

@Getter
@Setter
@NoArgsConstructor
public class User {
private String id;
private String name;
private String password;

public User(String id, String name, String password) {
this.id = id;
this.name = name;
this.password = password;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package tobyspring.vol1.chapter_2;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.dao.EmptyResultDataAccessException;
import tobyspring.vol1.chapter_1.user.dao.DaoFactory;
import tobyspring.vol1.chapter_1.user.dao.UserDao;
import tobyspring.vol1.chapter_1.user.domain.User;

import java.sql.SQLException;

import static org.assertj.core.api.Assertions.*;

@SpringBootTest
public class UserDaoTest {

@Autowired
ApplicationContext ac;

UserDao userDao;
User user;
User user1;
User user2;

@BeforeEach
void beforeEach() throws SQLException {
userDao = ac.getBean(UserDao.class);
userDao.deleteAll();
user = new User("id1", "subin", "1234");
user1 = new User("id2", "yebin", "2234");
user2 = new User("id3", "bean", "3234");

System.out.println("ac = " + ac); //동일한 주소가 출력됨.
System.out.println("this = " + this); //다른 주소가 출력됨. 테스므 메서드 마다 테스트 클래스 인스턴스를 만들어 실행하기 때문이다.
}


@Test
void count() throws SQLException, ClassNotFoundException {

userDao.add(user);
assertThat(userDao.getCount()).isEqualTo(1);

userDao.add(user1);
assertThat(userDao.getCount()).isEqualTo(2);

userDao.add(user2);
assertThat(userDao.getCount()).isEqualTo(3);
}


@Test
void addAndGet() throws SQLException, ClassNotFoundException {

userDao.add(user);
userDao.add(user1);

assertThat(userDao.getCount()).isEqualTo(2);

User getUser = userDao.get("id1");

assertThat(getUser.getName()).isEqualTo("subin");
assertThat(getUser.getPassword()).isEqualTo("1234");

User getUser2 = userDao.get("id2");

assertThat(getUser2.getName()).isEqualTo("yebin");
assertThat(getUser2.getPassword()).isEqualTo("2234");

}

@Test
void getUserFailure(){
assertThatThrownBy(()->{
userDao.get("hello");
}).isInstanceOf(EmptyResultDataAccessException.class);
}
}

0 comments on commit d31fcf6

Please sign in to comment.