| 18 | import static org.junit.Assert.*; |
| 19 | |
| 20 | @RunWith(SpringJUnit4ClassRunner.class) |
| 21 | @ContextConfiguration(locations = {"file:src/main/webapp/WEB-INF/spring/**/root-context.xml"}) |
| 22 | public class DBConnectionTest2Test { |
| 23 | @Autowired |
| 24 | DataSource ds; |
| 25 | |
| 26 | @Test |
| 27 | public void insertUserTest() throws Exception { |
| 28 | User user = new User("asdf2", "1234", "abc", "aaaa@aaa.com", new Date(), "fb", new Date()); |
| 29 | deleteAll(); |
| 30 | int rowCnt = insertUser(user); |
| 31 | |
| 32 | assertTrue(rowCnt==1); |
| 33 | } |
| 34 | |
| 35 | @Test |
| 36 | public void selectUserTest() throws Exception { |
| 37 | deleteAll(); |
| 38 | User user = new User("asdf2", "1234", "abc", "aaaa@aaa.com", new Date(), "fb", new Date()); |
| 39 | int rowCnt = insertUser(user); |
| 40 | User user2 = selectUser("asdf2"); |
| 41 | |
| 42 | assertTrue(user.getId().equals("asdf2")); |
| 43 | } |
| 44 | |
| 45 | @Test |
| 46 | public void deleteUserTest() throws Exception { |
| 47 | deleteAll(); |
| 48 | int rowCnt = deleteUser("asdf"); |
| 49 | |
| 50 | assertTrue(rowCnt==0); |
| 51 | |
| 52 | User user = new User("asdf2", "1234", "abc", "aaaa@aaa.com", new Date(), "fb", new Date()); |
| 53 | rowCnt = insertUser(user); |
| 54 | assertTrue(rowCnt==1); |
| 55 | |
| 56 | rowCnt = deleteUser(user.getId()); |
| 57 | assertTrue(rowCnt==1); |
| 58 | |
| 59 | assertTrue(selectUser(user.getId())==null); |
| 60 | |
| 61 | } |
| 62 | |
| 63 | // 매개변수로 받은 사용자 정보로 user_info테이블을 update하는 메서드 |
| 64 | public int updateUser(User user) throws Exception { |
| 65 | return 0; |
| 66 | } |
| 67 | |
| 68 | public int deleteUser(String id) throws Exception { |
| 69 | Connection conn = ds.getConnection(); |
| 70 | |
| 71 | String sql = "delete from user_info where id= ? "; |
| 72 | |
| 73 | PreparedStatement pstmt = conn.prepareStatement(sql); // SQL Injection공격, 성능향상 |
| 74 | pstmt.setString(1, id); |
| 75 | // int rowCnt = pstmt.executeUpdate(); // insert, delete, update |
| 76 | // return rowCnt; |
| 77 | return pstmt.executeUpdate(); // insert, delete, update |
nothing calls this directly
no outgoing calls
no test coverage detected