@Test public void testCreateWithConnection() throws Exception { PreparedStatement preparedStatement = EasyMock.createStrictMock(PreparedStatement.class); ResultSet resultSet = EasyMock.createStrictMock(ResultSet.class); Connection connection = EasyMock.createStrictMock(Connection.class); SavedReviewerSearch search = new SavedReviewerSearch(); Date date = new Date(); SavedReviewerSearchDAO dao = EasyMock.createMockBuilder(SavedReviewerSearchDAO.class) .withConstructor() .addMockedMethod("updatePreparedStatementForCreate") .createStrictMock(); EasyMock.expect(connection.prepareStatement(SavedReviewerSearchDAO.CREATE_SQL)) .andReturn(preparedStatement); dao.updatePreparedStatementForCreate(preparedStatement, search); EasyMock.expectLastCall(); EasyMock.expect(preparedStatement.executeQuery()).andReturn(resultSet); EasyMock.expect(resultSet.next()).andReturn(true); EasyMock.expect(resultSet.getTimestamp(1)).andReturn(new Timestamp(date.getTime())); resultSet.close(); EasyMock.expectLastCall(); preparedStatement.close(); EasyMock.replay(preparedStatement, resultSet, connection, dao); SavedReviewerSearch result = dao.create(connection, search); EasyMock.verify(preparedStatement, resultSet, connection, dao); assertEquals("Wrong created date.", date, result.getCreatedDate()); // failure case EasyMock.reset(preparedStatement, resultSet, connection, dao); EasyMock.expect(connection.prepareStatement(SavedReviewerSearchDAO.CREATE_SQL)) .andReturn(preparedStatement); dao.updatePreparedStatementForCreate(preparedStatement, search); EasyMock.expectLastCall(); EasyMock.expect(preparedStatement.executeQuery()).andReturn(resultSet); EasyMock.expect(resultSet.next()).andReturn(false); resultSet.close(); EasyMock.expectLastCall(); preparedStatement.close(); EasyMock.replay(preparedStatement, resultSet, connection, dao); try { dao.create(connection, search); fail("Should have thrown a sql exception."); } catch (SQLException ex) { // expected } EasyMock.verify(preparedStatement, resultSet, connection, dao); }
@Test public void testResultFromResultSet() throws Exception { SavedReviewerSearchDAO dao = new SavedReviewerSearchDAO(); ResultSet resultSet = EasyMock.createStrictMock(ResultSet.class); Date createdDate = new Date(); int customerId = 56; int userId = 32422; int msgTypeId = 2342; String name = "myName"; String queryType = "myQueryType"; String jsonData = "this is my json data but I don't check that it is json."; EasyMock.expect(resultSet.getString(1)).andReturn(name); EasyMock.expect(resultSet.getInt(2)).andReturn(customerId); EasyMock.expect(resultSet.getInt(3)).andReturn(userId); EasyMock.expect(resultSet.getInt(4)).andReturn(msgTypeId); EasyMock.expect(resultSet.getString(5)).andReturn(queryType); EasyMock.expect(resultSet.getString(6)).andReturn(jsonData); EasyMock.expect(resultSet.getTimestamp(7)).andReturn(new Timestamp(createdDate.getTime())); EasyMock.replay(resultSet); SavedReviewerSearch search = dao.resultFromResultSet(resultSet); EasyMock.verify(resultSet); assertEquals("Wrong created date.", createdDate, search.getCreatedDate()); assertEquals("Wrong customer id.", customerId, search.getCustomerId()); assertEquals("Wrong user id.", userId, search.getSearchUserId()); assertEquals("Wrong msg type id.", msgTypeId, search.getMessageTypeId()); assertEquals("Wrong name.", name, search.getSearchName()); assertEquals("Wrong query type.", queryType, search.getQueryType()); assertEquals("Wrong json data.", jsonData, search.getJsonData()); // now with no date EasyMock.reset(resultSet); EasyMock.expect(resultSet.getString(1)).andReturn(name); EasyMock.expect(resultSet.getInt(2)).andReturn(customerId); EasyMock.expect(resultSet.getInt(3)).andReturn(userId); EasyMock.expect(resultSet.getInt(4)).andReturn(msgTypeId); EasyMock.expect(resultSet.getString(5)).andReturn(queryType); EasyMock.expect(resultSet.getString(6)).andReturn(jsonData); EasyMock.expect(resultSet.getTimestamp(7)).andReturn(null); EasyMock.replay(resultSet); search = dao.resultFromResultSet(resultSet); EasyMock.verify(resultSet); assertNull("Created date should not be set.", search.getCreatedDate()); }
@Test public void testIsUpdate() throws Exception { SavedReviewerSearch search = new SavedReviewerSearch(); search.setSearchUserId(34); search.setSearchName("hello"); SavedReviewerSearchDAO dao = new SavedReviewerSearchDAO(); Connection connection = EasyMock.createStrictMock(Connection.class); PreparedStatement preparedStatement = EasyMock.createStrictMock(PreparedStatement.class); ResultSet resultSet = EasyMock.createStrictMock(ResultSet.class); EasyMock.expect(connection.prepareStatement(SavedReviewerSearchDAO.CHECK_SQL)) .andReturn(preparedStatement); preparedStatement.setInt(1, search.getSearchUserId()); EasyMock.expectLastCall(); preparedStatement.setString(2, search.getSearchName()); EasyMock.expectLastCall(); EasyMock.expect(preparedStatement.executeQuery()).andReturn(resultSet); EasyMock.expect(resultSet.next()).andReturn(true); resultSet.close(); EasyMock.expectLastCall(); preparedStatement.close(); EasyMock.expectLastCall(); EasyMock.replay(connection, preparedStatement, resultSet); assertTrue("Should be an update.", dao.isUpdate(connection, search)); EasyMock.verify(connection, preparedStatement, resultSet); // now not an update EasyMock.reset(connection, preparedStatement, resultSet); EasyMock.expect(connection.prepareStatement(SavedReviewerSearchDAO.CHECK_SQL)) .andReturn(preparedStatement); preparedStatement.setInt(1, search.getSearchUserId()); EasyMock.expectLastCall(); preparedStatement.setString(2, search.getSearchName()); EasyMock.expectLastCall(); EasyMock.expect(preparedStatement.executeQuery()).andReturn(resultSet); EasyMock.expect(resultSet.next()).andReturn(false); resultSet.close(); EasyMock.expectLastCall(); preparedStatement.close(); EasyMock.expectLastCall(); EasyMock.replay(connection, preparedStatement, resultSet); assertFalse("Should not be an update.", dao.isUpdate(connection, search)); EasyMock.verify(connection, preparedStatement, resultSet); }
@Test public void testUpdatePreparedStatementForUpdate() throws Exception { SavedReviewerSearch search = new SavedReviewerSearch(); int customerId = 56; int userId = 32422; int msgTypeId = 2342; String name = "myName"; String queryType = "myQueryType"; String jsonData = "this is my json data but I don't check that it is json."; search.setCustomerId(customerId); search.setSearchUserId(userId); search.setMessageTypeId(msgTypeId); search.setSearchName(name); search.setQueryType(queryType); search.setJsonData(jsonData); SavedReviewerSearchDAO dao = new SavedReviewerSearchDAO(); PreparedStatement preparedStatement = EasyMock.createStrictMock(PreparedStatement.class); preparedStatement.setInt(1, search.getMessageTypeId()); EasyMock.expectLastCall(); preparedStatement.setString(2, search.getQueryType()); EasyMock.expectLastCall(); preparedStatement.setString(3, search.getJsonData()); EasyMock.expectLastCall(); preparedStatement.setInt(4, search.getSearchUserId()); EasyMock.expectLastCall(); preparedStatement.setString(5, search.getSearchName()); EasyMock.expectLastCall(); EasyMock.replay(preparedStatement); dao.updatePreparedStatementForUpdate(preparedStatement, search); EasyMock.verify(preparedStatement); }