Java tutorial
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package cz.PA165.vozovyPark.dao; import cz.PA165.vozovyPark.dao.impl.VehicleDAOImpl; import cz.PA165.vozovyPark.entities.Vehicle; import cz.PA165.vozovyPark.enums.UserClassEnum; import java.util.Collection; import java.util.List; import javax.persistence.EntityManager; import javax.persistence.EntityManagerFactory; import javax.persistence.Persistence; import junit.framework.Assert; import org.junit.After; import org.junit.Before; import org.junit.Test; import org.springframework.beans.factory.annotation.Autowired; /** * * @author Juraj Pleko, 359530 corrected by jzelezny */ public class VehicleDAOTest { private static final String PERSISTENCE_UNIT_NAME = "VozovnaPU"; @Autowired private VehicleDAO vehicleDao; EntityManagerFactory emf; EntityManager em; /** * */ @Before public void setUp() { emf = Persistence.createEntityManagerFactory(PERSISTENCE_UNIT_NAME); em = emf.createEntityManager(); VehicleDAOImpl vehicleDaoImpl = new VehicleDAOImpl(em); this.vehicleDao = vehicleDaoImpl; //cleaning Collection<Vehicle> allVehicles = vehicleDao.findAll(); for (Vehicle v : allVehicles) { em.getTransaction().begin(); vehicleDao.removeVehicle(v); em.getTransaction().commit(); } } /** * */ @After public void tearDown() { Collection<Vehicle> allVehicles = vehicleDao.findAll(); for (Vehicle v : allVehicles) { em.getTransaction().begin(); vehicleDao.removeVehicle(v); em.getTransaction().commit(); } if (em.isOpen()) { em.close(); } if (emf.isOpen()) { emf.close(); } } private static Vehicle getVehicle(String brand, int distanceCount, String engineType, String type, UserClassEnum userClass, String vin, int year, String regPlate) { Vehicle vehicle = new Vehicle(); vehicle.setBrand(brand); vehicle.setMaxDistance(distanceCount); vehicle.setEngineType(engineType); vehicle.setType(type); vehicle.setUserClass(userClass); vehicle.setVin(vin); vehicle.setYearMade(year); vehicle.setRegistrationPlate(regPlate); return vehicle; } /** * Test of getById method, of class VehicleDAO. */ @Test public void testCreateAndGetById() { try { Vehicle mercedes = VehicleDAOTest.getVehicle("Mercedes", 20000, "R4 Diesel", "E", UserClassEnum.PRESIDENT, "2a-447i-882a45", 2009, "UEW6828"); vehicleDao.createVehicle(mercedes); Vehicle loaded = vehicleDao.getById(mercedes.getId()); Assert.assertEquals("Insserted and loaded vehicles should be same.", mercedes, loaded); } catch (Exception ex) { Assert.fail("Unexpected exception was throwed: " + ex + " " + ex.getMessage()); } } /** * Test of remove method, of class VehicleDAO. */ @Test public void testRemove() { try { Vehicle mercedes = VehicleDAOTest.getVehicle("Mercedes", 20000, "R4 Diesel", "E", UserClassEnum.PRESIDENT, "2a-447i-882a45", 2009, "UEW6828"); this.vehicleDao.createVehicle(mercedes); Long id = mercedes.getId(); System.out.println("Log: vehicle created!"); em.getTransaction().begin(); this.vehicleDao.removeVehicle(mercedes); em.getTransaction().commit(); System.out.println("Log: vehicle removed!"); Vehicle loaded = this.vehicleDao.getById(id); System.out.println("Log: vehicle loaded!"); Assert.assertNull("Vehicle was not deleted from database", loaded); } catch (Exception ex) { Assert.fail("Unexpected exception was throwed: " + ex + " " + ex.getMessage()); } } /** * Test of update method, of class VehicleDAO. */ @Test public void testUpdate() { try { Vehicle mercedes = VehicleDAOTest.getVehicle("Mercedes", 20000, "R4 Diesel", "E", UserClassEnum.PRESIDENT, "2a-447i-882a45", 2009, "UEW6828"); this.vehicleDao.createVehicle(mercedes); Long id = mercedes.getId(); mercedes.setType("C"); this.vehicleDao.updateVehicle(mercedes); Vehicle loaded = this.vehicleDao.getById(id); Assert.assertNotSame("Vehicle was not upadted!", "E", loaded.getType()); } catch (Exception ex) { Assert.fail("Unexpected exception was throwed: " + ex + " " + ex.getMessage()); } } /** * Test of findAll method, of class VehicleDAO. */ @Test public void testFindAll() { List<Vehicle> allVehicles = null; try { Vehicle mercedes1 = VehicleDAOTest.getVehicle("Mercedes", 20000, "R4 Diesel", "E", UserClassEnum.PRESIDENT, "2a-447i-882a45", 2009, "UEW6828"); Vehicle mercedes2 = VehicleDAOTest.getVehicle("Mercedes Piano", 20000, "R4 Diesel", "C", UserClassEnum.PRESIDENT, "98-447i-883345", 2009, "UEW6828"); vehicleDao.createVehicle(mercedes1); vehicleDao.createVehicle(mercedes2); em.getTransaction().begin(); allVehicles = vehicleDao.findAll(); em.getTransaction().commit(); if (allVehicles.size() != 2) { Assert.fail("Wrong size of list. Should be 2. List size: " + allVehicles.size()); } Assert.assertTrue("Contains..", allVehicles.contains(mercedes1) && allVehicles.contains(mercedes2)); } catch (Exception ex) { Assert.fail("Unexpected exception was thrown: " + ex + " " + ex.getMessage()); } } /** * Test of findByUserClass method, of class VehicleDAO. */ @Test public void testFindByUserClass() { try { Vehicle mercedes1 = VehicleDAOTest.getVehicle("Mercedes", 20000, "R4 Diesel", "E", UserClassEnum.PRESIDENT, "2a-447i-882a45", 2009, "UEW6828"); Vehicle mercedes2 = VehicleDAOTest.getVehicle("Mercedes", 20000, "R4 Diesel", "C", UserClassEnum.MANAGER, "98-447i-883345", 2009, "UEW6828"); this.vehicleDao.createVehicle(mercedes1); this.vehicleDao.createVehicle(mercedes2); em.getTransaction().begin(); Collection<Vehicle> required = this.vehicleDao.findByUserClass(UserClassEnum.MANAGER); em.getTransaction().commit(); Assert.assertTrue("Vehicle with right user class was not loaded.", required.contains(mercedes2)); Assert.assertFalse("Vehicle with different user class was loaded as well", required.contains(mercedes1)); } catch (Exception ex) { Assert.fail("Unexpected exception was throwed: " + ex + " " + ex.getMessage()); } } }