@Test
  public void findCustomerWithCar() {
    assertTrue(car1.getAvailable());
    assertTrue(car2.getAvailable());
    assertTrue(car3.getAvailable());

    manager.rentCarToCustomer(
        car1, customer1, Date.valueOf("2012-03-21"), Date.valueOf("2012-03-31"));

    assertEquals(customer1, manager.findCustomerWithCar(car1));
    assertCustomerDeepEquals(customer1, manager.findCustomerWithCar(car1));
    assertTrue(car2.getAvailable());
    assertTrue(car3.getAvailable());

    try {
      manager.findCustomerWithCar(null);
      fail();
    } catch (IllegalArgumentException e) {
    }
    try {
      manager.findCustomerWithCar(carWithoutID);
      fail();
    } catch (IllegalArgumentException e) {
    }
  }
 @Before
 public void setUp() throws SQLException {
   dataSource = prepareDataSource();
   DBUtils.createTables(dataSource);
   manager = new RentManagerImplementation();
   manager.setDataSource(dataSource);
   carManager = new CarManagerImplementation();
   carManager.setDataSource(dataSource);
   customerManager = new CustomerManagerImplementation();
   customerManager.setDataSource(dataSource);
   prepareTestData();
 }
  @Test
  public void getAllCustomerCars() {
    assertFalse(customer1.getActive());
    assertFalse(customer2.getActive());
    assertFalse(customer3.getActive());

    manager.rentCarToCustomer(
        car2, customer1, Date.valueOf("2012-03-21"), Date.valueOf("2012-03-31"));
    manager.rentCarToCustomer(
        car3, customer1, Date.valueOf("2012-03-25"), Date.valueOf("2012-04-02"));
    manager.rentCarToCustomer(
        car1, customer2, Date.valueOf("2012-03-15"), Date.valueOf("2012-03-27"));

    List<Car> carsRetnedtoCustomer1 = Arrays.asList(car2, car3);
    List<Car> carsRetnedtoCustomer2 = Arrays.asList(car1);

    assertCarDeepEquals(carsRetnedtoCustomer1, manager.getAllCustomerCars(customer1));
    assertCarDeepEquals(carsRetnedtoCustomer2, manager.getAllCustomerCars(customer2));
    assertFalse(customer3.getActive());

    try {
      manager.getAllCustomerCars(null);
      fail();
    } catch (IllegalArgumentException e) {
    }

    try {
      manager.getAllCustomerCars(customerWithoutID);
      fail();
    } catch (IllegalArgumentException e) {
    }
  }
  @Test
  public void getCarFromCustomer() {
    assertTrue(car1.getAvailable());
    assertTrue(car2.getAvailable());
    assertTrue(car3.getAvailable());

    manager.rentCarToCustomer(
        car1, customer1, Date.valueOf("2012-03-21"), Date.valueOf("2012-03-31"));
    manager.rentCarToCustomer(
        car2, customer1, Date.valueOf("2012-03-25"), Date.valueOf("2012-04-02"));
    manager.rentCarToCustomer(
        car3, customer2, Date.valueOf("2012-03-15"), Date.valueOf("2012-03-27"));

    assertEquals(customer1, manager.findCustomerWithCar(car1));
    assertCustomerDeepEquals(customer1, manager.findCustomerWithCar(car1));
    assertEquals(customer1, manager.findCustomerWithCar(car2));
    assertCustomerDeepEquals(customer1, manager.findCustomerWithCar(car2));
    assertEquals(customer2, manager.findCustomerWithCar(car3));
    assertCustomerDeepEquals(customer2, manager.findCustomerWithCar(car3));

    manager.getCarFromCustomer(car3, customer2);

    List<Car> carsRetnedtoCustomer1 = Arrays.asList(car1, car2);

    assertCarDeepEquals(carsRetnedtoCustomer1, manager.getAllCustomerCars(customer1));
    assertFalse(customer2.getActive());
    assertFalse(customer3.getActive());

    assertEquals(customer1, manager.findCustomerWithCar(car1));
    assertCustomerDeepEquals(customer1, manager.findCustomerWithCar(car1));
    assertEquals(customer1, manager.findCustomerWithCar(car2));
    assertCustomerDeepEquals(customer1, manager.findCustomerWithCar(car2));
    assertTrue(car3.getAvailable());

    try {
      manager.getCarFromCustomer(car3, customer1);
      fail();
    } catch (TransactionException e) {
    }

    try {
      manager.getCarFromCustomer(car3, customer2);
      fail();
    } catch (IllegalArgumentException e) {
    }

    try {
      manager.getCarFromCustomer(null, customer1);
      fail();
    } catch (IllegalArgumentException e) {
    }

    try {
      manager.getCarFromCustomer(carWithoutID, customer1);
      fail();
    } catch (IllegalArgumentException e) {
    }

    try {
      manager.getCarFromCustomer(carNotInDB, customer1);
      fail();
    } catch (TransactionException e) {
    }

    try {
      manager.getCarFromCustomer(car1, null);
      fail();
    } catch (IllegalArgumentException e) {
    }

    try {
      manager.getCarFromCustomer(car1, customerWithoutID);
      fail();
    } catch (IllegalArgumentException e) {
    }

    try {
      manager.getCarFromCustomer(car1, customerNotInDB);
      fail();
    } catch (TransactionException e) {
    }

    // Check that previous tests didn't affect data in database
    assertCarDeepEquals(carsRetnedtoCustomer1, manager.getAllCustomerCars(customer1));
    assertFalse(customer2.getActive());
    assertFalse(customer3.getActive());

    assertEquals(customer1, manager.findCustomerWithCar(car1));
    assertCustomerDeepEquals(customer1, manager.findCustomerWithCar(car1));
    assertEquals(customer1, manager.findCustomerWithCar(car2));
    assertCustomerDeepEquals(customer1, manager.findCustomerWithCar(car2));
    assertTrue(car3.getAvailable());
  }