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

Java tutorial

Introduction

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

Source

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

import cz.muni.fi.pa165.rentalofconstructionmachinery.entity.Machine;
import java.util.ArrayList;
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.beans.factory.annotation.Qualifier;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.transaction.annotation.Transactional;

/**
 *
 * @author Adam Zmrzl
 * @version Murcilago (20121103)
 */
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:/testContext.xml")
@Transactional
public class MachineDAOTest {

    @Autowired
    @Qualifier(value = "machineDao")
    private MachineDAO mDAO;

    private Machine machine;

    @Before
    public void setUpTest() {
        machine = new Machine();
    }

    @Test
    public void testCreateNull() {
        try {
            mDAO.createMachine(null);
            fail("Null machine should not be created.");
        } catch (Exception e) {
        }
    }

    @Test
    public void testCreateMachineWithoutPlate() {
        try {
            mDAO.createMachine(machine);
            fail("Machine without license plate should not be created.");
        } catch (Exception e) {
        }
    }

    @Test
    public void testCreateMachineWithId() {
        Long id = new Long(3141592);
        machine.setId(id);
        try {
            mDAO.createMachine(machine);
            fail("Machine with artifical Id should not be created.");
        } catch (Exception e) {
        }
    }

    @Test
    public void testDeleteNull() {
        try {
            mDAO.deleteMachine(null);
            fail("Null machine should not be deleted.");
        } catch (Exception e) {
        }
    }

    @Test
    public void testDeleteMachine() {
        machine.setLicensePlate("5U2 1234");
        mDAO.createMachine(machine);
        Machine machine2 = new Machine();
        machine2.setLicensePlate("5U2 4321");
        mDAO.createMachine(machine2);
        Machine machine3 = new Machine();
        machine3.setLicensePlate("5U2 4123");
        mDAO.createMachine(machine3);

        int sizeBeforeDelete = mDAO.getAllMachines().size();
        mDAO.deleteMachine(machine);
        assertTrue(mDAO.getAllMachines().size() == sizeBeforeDelete - 1);
    }

    @Test
    public void testUpdateNull() {
        try {
            mDAO.updateMachine(null);
            fail("Null machine should not be updated.");
        } catch (Exception e) {
        }
    }

    @Test
    public void testUpdateMachine() {
        machine.setLicensePlate("5U2 3257");
        mDAO.createMachine(machine);
        machine.setLicensePlate("4L1 4114");
        mDAO.updateMachine(machine);
        assertTrue(mDAO.getMachineById(machine.getId()).getLicensePlate().equals("4L1 4114"));
    }

    @Test
    public void testGetMachineByNullId() {
        try {
            machine = mDAO.getMachineById(null);
            fail("Should not allow query for machine with null id.");
        } catch (Exception e) {
        }
    }

    @Test
    public void testGetMachineById() {
        machine.setLicensePlate("5U2 2222");
        mDAO.createMachine(machine);
        Machine mFetched = mDAO.getMachineById(machine.getId());
        assertTrue(machine.getId().equals(mFetched.getId())
                && machine.getLicensePlate().equals(mFetched.getLicensePlate()));
    }

    @Test
    public void testGetAllMachines() {
        List<Machine> machines = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            machine = new Machine();
            machine.setLicensePlate("5U2 100" + i);
            mDAO.createMachine(machine);
            machines.add(machine);
        }

        List<Machine> mFetched = mDAO.getAllMachines();

        assertTrue(machines.size() == mFetched.size());
        for (int i = 0; i < 10; i++) {
            assertTrue(machines.get(i).getId().equals(mFetched.get(i).getId())
                    && machines.get(i).getLicensePlate().equals(mFetched.get(i).getLicensePlate()));
        }
    }
}