Esempio n. 1
0
  @Test
  public void testGetCustomerWithConnection() throws Exception {
    CustomerDAO dao =
        EasyMock.createMockBuilder(CustomerDAO.class)
            .addMockedMethod("findCustomers", List.class, Connection.class)
            .createStrictMock();
    Customer customer = EasyMock.createStrictMock(Customer.class);
    Connection connection = EasyMock.createStrictMock(Connection.class);
    int custId = 322;
    List<ICustomer> customerList = new LinkedList<ICustomer>();
    customerList.add(customer);

    Capture<List<SearchConstraint>> capture = new Capture<List<SearchConstraint>>();
    EasyMock.expect(dao.findCustomers(EasyMock.capture(capture), EasyMock.eq(connection)))
        .andReturn(customerList);

    EasyMock.replay(dao, customer, connection);
    assertEquals("Wrong customer returned.", customer, dao.getCustomer(custId, connection));
    EasyMock.verify(dao, customer, connection);
    assertEquals("Wrong # of constraints.", 1, capture.getValue().size());
    SearchConstraint constraint = capture.getValue().get(0);
    assertEquals(
        "Wrong property in constraint.",
        ICustomerService.PROP_CUSTOMER_ID,
        constraint.getProperty());
    assertEquals(
        "Wrong operator in constraint.",
        SearchConstraintOperator.CONSTRAINT_EQUALS,
        constraint.getOperator());
    assertEquals("Wrong value in constraint.", custId, constraint.getValue());

    // no customers
    customerList.clear();
    EasyMock.reset(dao, customer, connection);
    capture = new Capture<List<SearchConstraint>>();
    EasyMock.expect(dao.findCustomers(EasyMock.capture(capture), EasyMock.eq(connection)))
        .andReturn(customerList);

    EasyMock.replay(dao, customer, connection);
    assertEquals(
        "Should not have found a customer returned.", null, dao.getCustomer(custId, connection));
    EasyMock.verify(dao, customer, connection);
  }
Esempio n. 2
0
  @Test
  public void testFindCustomerByGuid() throws Exception {
    CustomerDAO dao =
        EasyMock.createMockBuilder(CustomerDAO.class)
            .addMockedMethod("findCustomers", List.class)
            .createStrictMock();

    // null guid
    EasyMock.replay(dao);
    try {
      dao.findCustomerByGuid(null, true);
      fail("Should have thrown an exception when null guid passed in.");
    } catch (IllegalArgumentException ex) {
      // expected
    }
    EasyMock.verify(dao);

    // no customer found
    EasyMock.reset(dao);
    boolean allowDeleted = true;
    String guid = "myGuid";
    Capture<List<SearchConstraint>> capture = new Capture<List<SearchConstraint>>();
    List<ICustomer> customerList = new LinkedList<ICustomer>();
    EasyMock.expect(dao.findCustomers(EasyMock.capture(capture))).andReturn(customerList);
    EasyMock.replay(dao);
    assertNull("Should not have found a customer.", dao.findCustomerByGuid(guid, allowDeleted));
    EasyMock.verify(dao);
    assertEquals("Wrong # of constraints.", 1, capture.getValue().size());
    SearchConstraint constraint = capture.getValue().get(0);
    assertEquals(
        "Wrong property in constraint.",
        ICustomerService.PROP_CLOUD_SERVICE_ANY_GUID,
        constraint.getProperty());
    assertEquals(
        "Wrong operator in constraint.",
        SearchConstraintOperator.CONSTRAINT_EQUALS,
        constraint.getOperator());
    assertEquals("Wrong value in constraint.", guid, constraint.getValue());

    // customer found and don't allow deleted
    EasyMock.reset(dao);
    allowDeleted = false;
    capture = new Capture<List<SearchConstraint>>();
    Customer customer = EasyMock.createStrictMock(Customer.class);
    customerList.add(customer);
    EasyMock.expect(dao.findCustomers(EasyMock.capture(capture))).andReturn(customerList);
    EasyMock.replay(dao);
    assertEquals(
        "Should have found customer.", customer, dao.findCustomerByGuid(guid, allowDeleted));
    EasyMock.verify(dao);
    assertEquals("Wrong # of constraints.", 1, capture.getValue().size());
    constraint = capture.getValue().get(0);
    assertEquals(
        "Wrong property in constraint.",
        ICustomerService.PROP_CLOUD_SERVICE_GUID,
        constraint.getProperty());
    assertEquals(
        "Wrong operator in constraint.",
        SearchConstraintOperator.CONSTRAINT_EQUALS,
        constraint.getOperator());
    assertEquals("Wrong value in constraint.", guid, constraint.getValue());
  }