User.java
package com.bean;
public class User {
private String id;
private String name;
private 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;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
UserDao.java
package com.dao;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.sql.DataSource;
import com.bean.User;
public class UserDao {
private Connection getConnection() throws Exception {
Connection con = null;
// // 커넥션 풀링 사용 코드
// Context context = new InitialContext();
// DataSource ds = (DataSource) context.lookup("java:comp/env/jdbc/oracle");
// con = ds.getConnection(); // 커넥션을 빌려온다.
// 1 드라이버 로딩
Class.forName("oracle.jdbc.driver.OracleDriver");
// 2 DB연결
con = DriverManager.getConnection("jdbc:oracle:thin:@127.0.0.1:1521:xe", "jspid", "jsppass");
return con;
}
// import 단축키 ctrl shift o
public void add(User user) throws Exception {
Connection c = this.getConnection();
PreparedStatement ps = c.prepareStatement(
"insert into users(id, name, password) values(?,?,?)");
ps.setString(1, user.getId());
ps.setString(2, user.getName());
ps.setString(3, user.getPassword());
ps.executeUpdate();
ps.close();
c.close();
}
public User get(String id) throws Exception {
Connection c = this.getConnection();
PreparedStatement ps = c.prepareStatement("select * from users where id = ?");
ps.setString(1, id);
ResultSet rs = ps.executeQuery();
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();
return user;
}
public User get2(String id) throws Exception {
Connection c = this.getConnection();
PreparedStatement ps = c.prepareStatement("select * from users where id = ?");
ps.setString(1, id);
ResultSet rs = ps.executeQuery();
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 SQLException("해당되는 user 정보없음");
}
return user;
}
public void deleteAll() throws Exception {
Connection c = getConnection();
PreparedStatement ps = c.prepareStatement("delete from users");
ps.executeUpdate();
ps.close();
c.close();
}
public int getCount() throws Exception {
Connection c = 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;
}
}
UserDaoTest.java
package com.dao;
import static org.junit.Assert.*;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.JUnitCore;
import com.bean.User;
public class UserDaoTest {
// 픽스처
UserDao userDao;
User user1;
User user2;
User user3;
@Before
public void setup() {
// 테스트를 위한 준비작업
// 주로 픽스처를 준비함
userDao = new UserDao();
user1 = new User("gyumee", "박성철", "springno1");
user2 = new User("leegw700", "이길원", "springno2");
user3 = new User("bumjin", "박범진", "springno3");
}
@Test
public void addAndGet() throws Exception {
userDao.deleteAll();
assertEquals(0, userDao.getCount());
userDao.add(user1); // 검증
userDao.add(user2);
assertEquals(2, userDao.getCount());
User userget1 = userDao.get(user1.getId());
assertEquals(user1.getName(), userget1.getName());
assertEquals(user1.getPassword(), userget1.getPassword());
User userget2 = userDao.get(user2.getId());
assertEquals(user2.getName(), userget2.getName());
assertEquals(user2.getPassword(), userget2.getPassword());
}
@Test
public void count() throws Exception {
userDao.deleteAll();
assertEquals(0, userDao.getCount());
userDao.add(user1);
assertEquals(1, userDao.getCount());
userDao.add(user2);
assertEquals(2, userDao.getCount());
userDao.add(user3);
assertEquals(3, userDao.getCount());
}
@Test
public void getUserNull() throws Exception {
userDao.deleteAll();
assertEquals(0, userDao.getCount());
assertNull(userDao.get("unknown_id"));
}
@Test(expected=Exception.class)
public void getUserException() throws Exception {
userDao.deleteAll();
assertEquals(0, userDao.getCount());
userDao.get2("unknown_id");
}
public static void main(String[] args) {
JUnitCore.main("com.dao.UserDaoTest");
}
}
DateUtil.java
package com.gilbut;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
public class DateUtil {
// 20181114
public static String getCurDateByCalendar() {
StringBuffer sb = new StringBuffer();
Calendar cal = Calendar.getInstance();
sb.append(cal.get(Calendar.YEAR));
sb.append(String.format("%02d", cal.get(Calendar.MONTH) + 1));
sb.append(String.format("%02d", cal.get(Calendar.DATE)));
return sb.toString();
}
public String getCurDateByDate() {
Date date = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
String str = sdf.format(date);
return str;
}
}
DateUtilTest.java
package com.gilbut;
import org.junit.runner.JUnitCore;
import junit.framework.TestCase;
public class DateUtilTest extends TestCase {
DateUtil dateUtil;
@Override
protected void setUp() throws Exception {
super.setUp();
// 테스트메소드가 실행되기 전마다 매번 실행됨
dateUtil = new DateUtil();
}
@Override
protected void tearDown() throws Exception {
// 테스트 메소드가 실행된 이후마다 매번 실행됨
// 테스트 정리작업 메소드
}
// 테스트케이스 클래스의 테스트 메소드 형식
// - public void
// - 매개변수도 없어야 함
// - test라는 이름으로 메소드명이 시작해야 함
public void testGetCurDateByCalendar() {
assertEquals("20181114", dateUtil.getCurDateByCalendar());
}
public void testGetCurDateByDate () {
assertEquals("20181114", dateUtil.getCurDateByDate());
}
public static void main(String[] args) {
JUnitCore.main("com.gilbut.DateUtilTest");
}
}
DateUtilTest2.java
package com.gilbut;
import static org.junit.Assert.*;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Assert;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
// 애노테이션 기반으로 테스트케이스 구현
public class DateUtilTest2 {
// 테스트 대상(target) 객체 준비
DateUtil dateUtil;
@BeforeClass
public static void init() {
System.out.println("init() 호출 - @BeforeClass");
}
@AfterClass
public static void destroy() {
System.out.println("destroy() 호출 - @AfterClass");
}
@Before
public void setUp() {
System.out.println("setup() 호출");
dateUtil = new DateUtil();
}
@After
public void tearDown() {
System.out.println("tearDown() 호출");
System.out.println();
}
@Test
public void getCurDateByCalendar() {
System.out.println("getCurDateByCalendar() 호출");
String strDate = dateUtil.getCurDateByCalendar();
assertNotNull(strDate);
assertEquals(dateUtil.getCurDateByDate(), strDate);
}
@Test
public void getCurDateByDate () {
System.out.println("getCurDateByDate 호출");
assertEquals("20181114", dateUtil.getCurDateByDate());
}
}