@Test public void testSetTransactionManager() throws Exception { SavedReviewerSearchDAO dao = new SavedReviewerSearchDAO(); ITransactionManager mgr = EasyMock.createStrictMock(ITransactionManager.class); dao.setTransactionManager(mgr); assertEquals("Wrong tx manager returned.", mgr, dao.getTransactionManager()); }
@Test public void testReadWithConnection() throws Exception { PreparedStatement preparedStatement = EasyMock.createStrictMock(PreparedStatement.class); ResultSet resultSet = EasyMock.createStrictMock(ResultSet.class); Connection connection = EasyMock.createStrictMock(Connection.class); SavedReviewerSearch search = new SavedReviewerSearch(); int id = 34; String name = "myName"; SavedReviewerSearchDAO dao = EasyMock.createMockBuilder(SavedReviewerSearchDAO.class) .withConstructor() .addMockedMethod("resultFromResultSet") .createStrictMock(); EasyMock.expect(connection.prepareStatement(SavedReviewerSearchDAO.READ_SQL)) .andReturn(preparedStatement); preparedStatement.setInt(1, id); EasyMock.expectLastCall(); preparedStatement.setString(2, name); EasyMock.expectLastCall(); EasyMock.expect(preparedStatement.executeQuery()).andReturn(resultSet); EasyMock.expect(resultSet.next()).andReturn(true); EasyMock.expect(dao.resultFromResultSet(resultSet)).andReturn(search); resultSet.close(); EasyMock.expectLastCall(); preparedStatement.close(); EasyMock.replay(preparedStatement, resultSet, connection, dao); SavedReviewerSearch result = dao.read(connection, id, name); assertEquals("Wrong search returned", search, result); EasyMock.verify(preparedStatement, resultSet, connection, dao); // failure case EasyMock.reset(preparedStatement, resultSet, connection, dao); EasyMock.expect(connection.prepareStatement(SavedReviewerSearchDAO.READ_SQL)) .andReturn(preparedStatement); preparedStatement.setInt(1, id); EasyMock.expectLastCall(); preparedStatement.setString(2, name); 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.read(connection, id, name); fail("Should have thrown a sql exception."); } catch (SQLException ex) { // expected } EasyMock.verify(preparedStatement, resultSet, connection, dao); }
@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 testUpsertWithConnection() throws Exception { SavedReviewerSearch search = new SavedReviewerSearch(); SavedReviewerSearch newSearch = new SavedReviewerSearch(); Connection connection = EasyMock.createStrictMock(Connection.class); SavedReviewerSearchDAO dao = EasyMock.createMockBuilder(SavedReviewerSearchDAO.class) .withConstructor() .addMockedMethod("update", Connection.class, SavedReviewerSearch.class) .addMockedMethod("create", Connection.class, SavedReviewerSearch.class) .addMockedMethod("isUpdate") .createStrictMock(); EasyMock.expect(dao.isUpdate(connection, search)).andReturn(false); EasyMock.expect(dao.create(connection, search)).andReturn(newSearch); EasyMock.replay(dao); assertEquals( "Wrong search returned in create case.", newSearch, dao.upsert(connection, search)); EasyMock.verify(dao); EasyMock.reset(dao); EasyMock.expect(dao.isUpdate(connection, search)).andReturn(true); EasyMock.expect(dao.update(connection, search)).andReturn(true); EasyMock.replay(dao); assertEquals("Wrong search returned in update case.", search, dao.upsert(connection, search)); EasyMock.verify(dao); }
@Test public void testDeleteByCustomerId() throws Exception { int id = 34; SavedReviewerSearchDAO dao = EasyMock.createMockBuilder(SavedReviewerSearchDAO.class) .withConstructor() .addMockedMethod("deleteByCustomerId", Connection.class, Integer.TYPE) .createStrictMock(); ITransactionManager txMgr = EasyMock.createStrictMock(ITransactionManager.class); dao.setTransactionManager(txMgr); Connection mockConn = EasyMock.createStrictMock(Connection.class); EasyMock.expect(dao.deleteByCustomerId(mockConn, id)).andReturn(34); MockExecutionWithThrow.<Boolean>execute(txMgr, mockConn); EasyMock.replay(txMgr, mockConn, dao); assertEquals("Wrong delete result.", 34, dao.deleteByCustomerId(id)); EasyMock.verify(txMgr, mockConn, dao); }
@Test public void testUpsert() throws Exception { SavedReviewerSearch search = new SavedReviewerSearch(); SavedReviewerSearchDAO dao = EasyMock.createMockBuilder(SavedReviewerSearchDAO.class) .withConstructor() .addMockedMethod("upsert", Connection.class, SavedReviewerSearch.class) .createStrictMock(); ITransactionManager txMgr = EasyMock.createStrictMock(ITransactionManager.class); dao.setTransactionManager(txMgr); Connection mockConn = EasyMock.createStrictMock(Connection.class); EasyMock.expect(dao.upsert(mockConn, search)).andReturn(search); MockExecutionWithThrow.<Boolean>execute(txMgr, mockConn); EasyMock.replay(txMgr, mockConn, dao); assertEquals("Wrong search result.", search, dao.upsert(search)); EasyMock.verify(txMgr, mockConn, dao); }
@Test public void testFindByUserId() throws Exception { List<String> results = new LinkedList<String>(); SavedReviewerSearchQuery constraints = new SavedReviewerSearchQuery(); SavedReviewerSearchDAO dao = EasyMock.createMockBuilder(SavedReviewerSearchDAO.class) .withConstructor() .addMockedMethod("find", Connection.class, SavedReviewerSearchQuery.class) .createStrictMock(); ITransactionManager txMgr = EasyMock.createStrictMock(ITransactionManager.class); dao.setTransactionManager(txMgr); Connection mockConn = EasyMock.createStrictMock(Connection.class); EasyMock.expect(dao.find(mockConn, constraints)).andReturn(results); MockExecutionWithThrow.<Boolean>execute(txMgr, mockConn); EasyMock.replay(txMgr, mockConn, dao); assertEquals("Wrong searches returned.", results, dao.find(constraints)); EasyMock.verify(txMgr, mockConn, dao); }
@Test public void testUpdateWithConnection() throws Exception { PreparedStatement preparedStatement = EasyMock.createStrictMock(PreparedStatement.class); Connection connection = EasyMock.createStrictMock(Connection.class); SavedReviewerSearch search = new SavedReviewerSearch(); SavedReviewerSearchDAO dao = EasyMock.createMockBuilder(SavedReviewerSearchDAO.class) .withConstructor() .addMockedMethod("updatePreparedStatementForUpdate") .createStrictMock(); EasyMock.expect(connection.prepareStatement(SavedReviewerSearchDAO.UPDATE_SQL)) .andReturn(preparedStatement); dao.updatePreparedStatementForUpdate(preparedStatement, search); EasyMock.expectLastCall(); EasyMock.expect(preparedStatement.executeUpdate()).andReturn(1); EasyMock.expectLastCall(); preparedStatement.close(); EasyMock.replay(preparedStatement, connection, dao); assertEquals("Wrong result returned.", true, dao.update(connection, search)); EasyMock.verify(preparedStatement, connection, dao); // failure case EasyMock.reset(preparedStatement, connection, dao); EasyMock.expect(connection.prepareStatement(SavedReviewerSearchDAO.UPDATE_SQL)) .andReturn(preparedStatement); dao.updatePreparedStatementForUpdate(preparedStatement, search); EasyMock.expectLastCall(); EasyMock.expect(preparedStatement.executeUpdate()).andThrow(new SQLException("blah")); EasyMock.expectLastCall(); preparedStatement.close(); EasyMock.replay(preparedStatement, connection, dao); try { dao.update(connection, search); fail("Should have thrown a sql exception."); } catch (SQLException ex) { // expected } EasyMock.verify(preparedStatement, connection, dao); }
@Test public void testRead() throws Exception { SavedReviewerSearch search = new SavedReviewerSearch(); int id = 34; String name = "mySearch"; SavedReviewerSearchDAO dao = EasyMock.createMockBuilder(SavedReviewerSearchDAO.class) .withConstructor() .addMockedMethod("read", Connection.class, Integer.TYPE, String.class) .createStrictMock(); ITransactionManager txMgr = EasyMock.createStrictMock(ITransactionManager.class); dao.setTransactionManager(txMgr); Connection mockConn = EasyMock.createStrictMock(Connection.class); EasyMock.expect(dao.read(mockConn, id, name)).andReturn(search); MockExecutionWithThrow.<Boolean>execute(txMgr, mockConn); EasyMock.replay(txMgr, mockConn, dao); assertEquals("Wrong search item returned.", search, dao.read(id, name)); EasyMock.verify(txMgr, mockConn, dao); }
@Test public void testDeleteByCustomerIdWithConnection() throws Exception { PreparedStatement preparedStatement = EasyMock.createStrictMock(PreparedStatement.class); Connection connection = EasyMock.createStrictMock(Connection.class); int id = 34; int count = 342; SavedReviewerSearchDAO dao = new SavedReviewerSearchDAO(); EasyMock.expect(connection.prepareStatement(SavedReviewerSearchDAO.DELETE_BY_CUSTOMER_SQL)) .andReturn(preparedStatement); preparedStatement.setInt(1, id); EasyMock.expectLastCall(); EasyMock.expect(preparedStatement.executeUpdate()).andReturn(count); preparedStatement.close(); EasyMock.expectLastCall(); EasyMock.replay(preparedStatement, connection); assertEquals( "Should return count when item is deleted.", count, dao.deleteByCustomerId(connection, id)); EasyMock.verify(preparedStatement, connection); }
@Test public void testDeleteWithConnection() throws Exception { PreparedStatement preparedStatement = EasyMock.createStrictMock(PreparedStatement.class); Connection connection = EasyMock.createStrictMock(Connection.class); int id = 34; String name = "myName"; int count = 342; SavedReviewerSearchDAO dao = new SavedReviewerSearchDAO(); EasyMock.expect(connection.prepareStatement(SavedReviewerSearchDAO.DELETE_SQL)) .andReturn(preparedStatement); preparedStatement.setInt(1, id); EasyMock.expectLastCall(); preparedStatement.setString(2, name); EasyMock.expectLastCall(); EasyMock.expect(preparedStatement.executeUpdate()).andReturn(count); preparedStatement.close(); EasyMock.expectLastCall(); EasyMock.replay(preparedStatement, connection); assertTrue("Should return true when item is deleted.", dao.delete(connection, id, name)); EasyMock.verify(preparedStatement, connection); // failure case count = 0; EasyMock.reset(preparedStatement, connection); EasyMock.expect(connection.prepareStatement(SavedReviewerSearchDAO.DELETE_SQL)) .andReturn(preparedStatement); preparedStatement.setInt(1, id); EasyMock.expectLastCall(); preparedStatement.setString(2, name); EasyMock.expectLastCall(); EasyMock.expect(preparedStatement.executeUpdate()).andReturn(count); preparedStatement.close(); EasyMock.expectLastCall(); EasyMock.replay(preparedStatement, connection); assertFalse("Should return false when item is not deleted.", dao.delete(connection, id, name)); EasyMock.verify(preparedStatement, connection); }
@Test public void testUpdatePreparedStatementForCreate() 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.setString(1, search.getSearchName()); EasyMock.expectLastCall(); preparedStatement.setInt(2, search.getCustomerId()); EasyMock.expectLastCall(); preparedStatement.setInt(3, search.getSearchUserId()); EasyMock.expectLastCall(); preparedStatement.setInt(4, search.getMessageTypeId()); EasyMock.expectLastCall(); preparedStatement.setString(5, search.getQueryType()); EasyMock.expectLastCall(); preparedStatement.setString(6, search.getJsonData()); EasyMock.expectLastCall(); EasyMock.replay(preparedStatement); dao.updatePreparedStatementForCreate(preparedStatement, search); EasyMock.verify(preparedStatement); }
@Test public void testDeleteSavedSearchesByCustomerIds() throws Exception { SavedReviewerSearchDAO dao = new SavedReviewerSearchDAO(); Connection connection = EasyMock.createStrictMock(Connection.class); Statement statement = EasyMock.createStrictMock(Statement.class); int count = 34; List<Integer> customerIds = new LinkedList<Integer>(); customerIds.add(1); customerIds.add(2); customerIds.add(3); String inClause = QueryUtils.literal(customerIds); EasyMock.expect(connection.createStatement()).andReturn(statement); EasyMock.expect( statement.executeUpdate(SavedReviewerSearchDAO.DELETE_BY_CUSTOMER_IDS_SQL + inClause)) .andReturn(count); statement.close(); EasyMock.expectLastCall(); EasyMock.replay(connection, statement); assertEquals( "Wrong count of deleted rows.", count, dao.deleteByCustomerIds(connection, customerIds)); EasyMock.verify(connection, statement); // null customer list EasyMock.reset(connection, statement); EasyMock.replay(connection, statement); assertEquals("Wrong return value.", 0, dao.deleteByCustomerIds(connection, null)); EasyMock.verify(connection, statement); // empty customer list customerIds.clear(); EasyMock.reset(connection, statement); EasyMock.replay(connection, statement); assertEquals("Wrong return value.", 0, dao.deleteByCustomerIds(connection, customerIds)); EasyMock.verify(connection, statement); }
@Test public void testFinddWithConnection() throws Exception { PreparedStatement preparedStatement = EasyMock.createStrictMock(PreparedStatement.class); ResultSet resultSet = EasyMock.createStrictMock(ResultSet.class); Connection connection = EasyMock.createStrictMock(Connection.class); String search1 = "search1"; String search2 = "search2"; int userId = 34; int messageTypeId = 1423; String queryType = "myQueryType"; SavedReviewerSearchQuery constraints = new SavedReviewerSearchQuery(); constraints.setQueryType(queryType); constraints.setMessageTypeId(messageTypeId); constraints.setSearchUserId(userId); SavedReviewerSearchDAO dao = EasyMock.createMockBuilder(SavedReviewerSearchDAO.class) .withConstructor() .addMockedMethod("resultFromResultSet") .createStrictMock(); EasyMock.expect(connection.prepareStatement(SavedReviewerSearchDAO.FIND_SQL)) .andReturn(preparedStatement); preparedStatement.setInt(1, userId); EasyMock.expectLastCall(); preparedStatement.setInt(2, messageTypeId); EasyMock.expectLastCall(); preparedStatement.setString(3, queryType); EasyMock.expectLastCall(); EasyMock.expect(preparedStatement.executeQuery()).andReturn(resultSet); EasyMock.expect(resultSet.next()).andReturn(true); EasyMock.expect(resultSet.getString(1)).andReturn(search1); EasyMock.expect(resultSet.next()).andReturn(true); EasyMock.expect(resultSet.getString(1)).andReturn(search2); EasyMock.expect(resultSet.next()).andReturn(false); resultSet.close(); EasyMock.expectLastCall(); preparedStatement.close(); EasyMock.replay(preparedStatement, resultSet, connection, dao); List<String> results = dao.find(connection, constraints); EasyMock.verify(preparedStatement, resultSet, connection, dao); assertEquals("Wrong # of items in list.", 2, results.size()); assertTrue("Did not find result 1 in list.", results.contains(search1)); assertTrue("Did not find result 2 in list.", results.contains(search2)); // no items EasyMock.reset(preparedStatement, resultSet, connection, dao); EasyMock.expect(connection.prepareStatement(SavedReviewerSearchDAO.FIND_SQL)) .andReturn(preparedStatement); preparedStatement.setInt(1, userId); EasyMock.expectLastCall(); preparedStatement.setInt(2, messageTypeId); EasyMock.expectLastCall(); preparedStatement.setString(3, queryType); 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); results = dao.find(connection, constraints); EasyMock.verify(preparedStatement, resultSet, connection, dao); assertEquals("Wrong # of items in list.", 0, results.size()); }
@Test public void testConstructor() throws Exception { SavedReviewerSearchDAO dao = new SavedReviewerSearchDAO(); assertNull("Tx Manager should be null.", dao.getTransactionManager()); }