@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);
  }
Example #3
0
  @SuppressWarnings("unchecked")
  @Test
  public void testFindCustomersWithConnection() throws Exception {
    CustomerDAO dao =
        EasyMock.createMockBuilder(CustomerDAO.class)
            .addMockedMethod("readNextCustomer")
            .addMockedMethod("getCustomerQuery")
            .createStrictMock();
    ResultSet resultSet = EasyMock.createStrictMock(ResultSet.class);
    Connection connection = EasyMock.createStrictMock(Connection.class);
    Statement statement = EasyMock.createStrictMock(Statement.class);
    List<SearchConstraint> constraints = new LinkedList<SearchConstraint>();

    EasyMock.expect(dao.getCustomerQuery(constraints)).andReturn("aQuery");
    EasyMock.expect(connection.createStatement()).andReturn(statement);
    EasyMock.expect(statement.executeQuery("aQuery")).andReturn(resultSet);

    EasyMock.expect(resultSet.next()).andReturn(true);
    EasyMock.expect(dao.readNextCustomer(EasyMock.eq(resultSet), EasyMock.isA(List.class)))
        .andReturn(true);
    EasyMock.expect(dao.readNextCustomer(EasyMock.eq(resultSet), EasyMock.isA(List.class)))
        .andReturn(true);
    EasyMock.expect(dao.readNextCustomer(EasyMock.eq(resultSet), EasyMock.isA(List.class)))
        .andReturn(false);
    resultSet.close();
    EasyMock.expectLastCall();
    statement.close();
  }
Example #4
0
 private int createMailRoutingPolicy(int custId, String name, boolean isDefault) throws Exception {
   PreparedStatement statement = null;
   ResultSet rs = null;
   Connection conn = getDBConnection();
   try {
     String sql =
         "insert into mail_routing_policies (customer_id, routing_policy_name, is_default) values (?,?,?) returning mail_routing_policy_id";
     statement = conn.prepareStatement(sql);
     statement.setInt(1, custId);
     statement.setString(2, name);
     statement.setBoolean(3, isDefault);
     rs = statement.executeQuery();
     assertTrue("Did not insert mail routing policy.", rs.next());
     return rs.getInt(1);
   } finally {
     if (rs != null) {
       rs.close();
     }
     if (statement != null) {
       statement.close();
     }
     if (conn != null) {
       conn.close();
     }
   }
 }
  @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 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());
  }