cz.muni.fi.pa165.rentalofconstructionmachinery.dao.RentalDAOTest.java Source code

Java tutorial

Introduction

Here is the source code for cz.muni.fi.pa165.rentalofconstructionmachinery.dao.RentalDAOTest.java

Source

package cz.muni.fi.pa165.rentalofconstructionmachinery.dao;

import cz.muni.fi.pa165.rentalofconstructionmachinery.CustomerType;
import cz.muni.fi.pa165.rentalofconstructionmachinery.entity.Customer;
import cz.muni.fi.pa165.rentalofconstructionmachinery.entity.Machine;
import cz.muni.fi.pa165.rentalofconstructionmachinery.entity.Rental;
import java.util.Date;
import java.util.List;
import static org.junit.Assert.*;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.transaction.annotation.Transactional;

/**
 *
 * @author Zuzana Krejcova
 */
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:/testContext.xml")
@Transactional
public class RentalDAOTest {

    private Customer customer;
    private Customer customer2;
    private Machine machine;
    private Rental rental;

    @Autowired
    private MachineDAO machineDAO;

    @Autowired
    private RentalDAO rentalDAO;

    @Autowired
    CustomerDAO cdao;

    public void setMachineDAO(MachineDAO machineDAO) {
        this.machineDAO = machineDAO;
    }

    public RentalDAOTest() {
    }

    @Before
    public void setUpTest() {
        customer = new Customer();
        customer2 = new Customer();
        machine = new Machine();
        customer.setName("name1");
        customer.setType(CustomerType.LEGAL);
        customer.setName("name2");
        customer.setType(CustomerType.PHYSICAL);
        machine.setLicensePlate("lp");
        machine.setType("type1");
        cdao.createCustomer(customer);
        cdao.createCustomer(customer2);
        machineDAO.createMachine(machine);
        rental = new Rental();
        rental.setCustomer(customer);
        rental.setDuration(5);
        rental.setMachine(machine);
        rental.setSince(new Date());
    }

    @Test
    public void testCreateRental() {
        rentalDAO.createRental(rental);
    }

    @Test
    public void testCreateNullRental() {
        try {
            rentalDAO.createRental(null);
            fail("Should not be able to create/persist a null rental.");
        } catch (Exception e) {
        }
    }

    @Test
    public void testCreateRentalWithID() {
        Long id = new Long(666);
        rental.setId(id);
        boolean failed = false;
        try {
            rentalDAO.createRental(rental);
        } catch (Exception e) {
            failed = true;
        }
        if (!failed) {
            assertFalse(rental.getId().equals(id));
        }
    }

    @Test
    public void testDeleteRental() {
        rentalDAO.createRental(rental);
        List r1 = rentalDAO.getAllRentals();
        System.out.println(rentalDAO.getAllRentals().size());
        rentalDAO.deleteRental(rental);
        List r2 = rentalDAO.getAllRentals();
        assertFalse(r1.size() == r2.size());
    }

    @Test
    public void testDeleteNullRental() {
        try {
            rentalDAO.deleteRental(null);
            fail("Should not be able to delete null rental.");
        } catch (Exception e) {
        }
    }

    @Test
    public void testDeleteNonesixtentRental() {
        rental.setId(new Long(999));
        List rentals = rentalDAO.getAllRentals();
        try {
            rentalDAO.deleteRental(rental);
        } catch (Exception e) {
            List rentals2 = rentalDAO.getAllRentals();
            assertFalse(rentals.size() == rentals2.size());
        }
    }

    @Test
    public void testUpdateRental() {
        rentalDAO.createRental(rental);
        rental.setCustomer(customer2);
        rentalDAO.updateRental(rental);
        assertTrue(rentalDAO.getRentalById(rental.getId()).getCustomer().getId().equals(customer2.getId()));
    }

    @Test
    public void testUpdateNullRental() {
        try {
            rentalDAO.updateRental(null);
            fail("Should not be able to update a null rental.");
        } catch (Exception e) {
        }
    }

    @Test
    public void testUpdateNonexistentRental() {
        try {
            rentalDAO.updateRental(rental);
            fail("Should not be able to update a rental that is not persisted yet.");
        } catch (Exception e) {
        }
    }

    @Test
    public void testGetAllRentals() {
        Rental r1 = new Rental();
        Rental r2 = new Rental();
        r1.setCustomer(customer);
        r1.setDuration(6);
        r1.setMachine(machine);
        r1.setSince(new Date());
        r2.setCustomer(customer);
        r2.setDuration(8);
        r2.setMachine(machine);
        r2.setSince(new Date());
        rentalDAO.createRental(r1);
        rentalDAO.createRental(r2);
        List result = rentalDAO.getAllRentals();
        assertEquals(2, result.size());
    }

    @Test
    public void testGetRentalById() {
        rentalDAO.createRental(rental);
        Rental result = rentalDAO.getRentalById(rental.getId());
        assertEquals(rental.getId(), result.getId());
    }

    @Test
    public void testGetRentalByNullId() {
        try {
            Rental result = rentalDAO.getRentalById(null);
            fail("Should not be able to get a rental instance with null id.");
        } catch (Exception e) {
        }
    }
}