JUnit test for control classes

JUnit test case list

No.ModuleDAO classMethodTest case
1Edit roomRoomDAOsearchRoom()not found
2Edit roomRoomDAOsearchRoom()found
3Edit roomRoomDAOupdateRoom()room does not exist
4Edit roomRoomDAOupdateRoom()room exists
5Booking roomRoomDAOsearchFreeRoom()there are available rooms
6Booking roomRoomDAOsearchFreeRoom()no room available
7Booking roomClientDAOsearchClient()found
8Booking roomClientDAOsearchClient()not found
9Booking roomBookingDAOaddBooking()room exists, client exists, room still available
10Booking roomBookingDAOaddBooking()room exists, client does not exist, room still available
11Booking roomBookingDAOaddBooking()room does not exist
12Booking roomBookingDAOaddBooking()room exists, client exists, room unavailable
13View room statisticRoomStatDAOgetRoomStat()at least 2 booked room, a room booked at least 2 times
14View room statisticRoomStatDAOgetRoomStat()only 1 booked room with 2 booked times
15View room statisticRoomStatDAOgetRoomStat()only 1 booked room with only 1 booked time
16View room statisticRoomStatDAOgetRoomStat()no booked room in the interval
17View room statisticBookingDAOgetBookingofRoom()at least 2 booked times
18View room statisticBookingDAOgetBookingofRoom()only 1 booked time: start date < checkin < checkout < end date
19View room statisticBookingDAOgetBookingofRoom()only 1 booked time: checkin < start date < checkout < end date
20View room statisticBookingDAOgetBookingofRoom()only 1 booked time: start date < checkin < end date < checkout
21View room statisticBookingDAOgetBookingofRoom()No booked time

Input (current) database

tblHotel:

tblRoom:

tblClient:

tblBooking:

tblBookedRoom

JUnit test case code for edit room

RoomDaoTest.java:

package test.unit;
import java.sql.Connection;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import org.junit.Assert;
import org.junit.Test;
import dao.DAO;
import dao.RoomDAO;
import model.Room;

public class RoomDaoTest {
	RoomDAO rd = new RoomDAO();
	
	
	@Test
	public void testSearchRoomException1(){
		String key = "xxxxxxxxxx";
		ArrayList<Room> listRoom = rd.searchRoom(key);
		Assert.assertNotNull(listRoom);
		Assert.assertEquals(0, listRoom.size());
		return;
	}
	
	@Test
	public void testSearchRoomException2(){
		String key = "rn";
		ArrayList<Room> listRoom = rd.searchRoom(key);
		Assert.assertNotNull(listRoom);
		Assert.assertEquals(0, listRoom.size());
		return;
	}
	
	@Test
	public void testSearchRoomStandard1(){
		String key = "1";
		ArrayList<Room> listRoom = rd.searchRoom(key);
		Assert.assertNotNull(listRoom);
		Assert.assertEquals(6, listRoom.size());
		for(int i=0; i<listRoom.size(); i++){
			Assert.assertTrue(listRoom.get(i).getName().toLowerCase().
                     contains(key.toLowerCase()));
		}
		return;
	}
	
	@Test
	public void testSearchRoomStandard2(){
		String key = "3";
		ArrayList<Room> listRoom = rd.searchRoom(key);		
		Assert.assertNotNull(listRoom);
		Assert.assertEquals(2, listRoom.size());
		for(int i=0; i<listRoom.size(); i++){
			Assert.assertTrue(listRoom.get(i).getName().toLowerCase().
                     contains(key.toLowerCase()));
		}
		return;
	}
	

	@Test	
	public void testUpdateRoom(){
		Connection con = DAO.con;
		String newType = "test type";
		String newDes = "test des";
		float newPrice = 5f;
		String key = "102";
		
		try{
			con.setAutoCommit(false);
			ArrayList<Room> lr = rd.searchRoom(key);

			lr.get(0).setType(newType);
			lr.get(0).setDes(newDes);
			lr.get(0).setPrice(newPrice);
			rd.updateRoom(lr.get(0));
			

			//test the new updated row
			lr.clear();
			lr = rd.searchRoom(key);
			Assert.assertTrue(lr.get(0).getName().contains(key));
			Assert.assertEquals(newType, lr.get(0).getType());
			Assert.assertEquals(newPrice, lr.get(0).getPrice(),0.000001f);
			Assert.assertEquals(newDes, lr.get(0).getDes());
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			try{
				con.rollback();
				con.setAutoCommit(true);
			}catch(Exception ex){
				ex.printStackTrace();
			}
		}
		return;
	}
}

JUnit test case code for Booking room

RoomDaoTest.java, add following test case:

	@Test
	public void testSearchFreeRoom1(){
		Date checkin = new GregorianCalendar(2020, 
                 Calendar.APRIL, 28).getTime();
		Date checkout = new GregorianCalendar(2020, 
                 Calendar.APRIL, 30).getTime();
		ArrayList<Room> listRoom = rd.searchFreeRoom(checkin, checkout);		
		Assert.assertNotNull(listRoom);
		Assert.assertEquals(10, listRoom.size());
	}
	
	@Test
	public void testSearchFreeRoom2(){
		Date checkin = new GregorianCalendar(2020, 
                 Calendar.APRIL, 28).getTime();
		Date checkout = new GregorianCalendar(2020, Calendar.MAY, 1).getTime();
		ArrayList<Room> listRoom = rd.searchFreeRoom(checkin, checkout);		
		Assert.assertNotNull(listRoom);
		Assert.assertEquals(7, listRoom.size());
		for(int i=0; i<listRoom.size(); i++){
			Assert.assertFalse(listRoom.get(i).getId() == 1);
			Assert.assertFalse(listRoom.get(i).getId() == 3);
			Assert.assertFalse(listRoom.get(i).getId() == 5);
		}
	}
	
	@Test
	public void testSearchFreeRoom3(){
		Date checkin = new GregorianCalendar(2020, Calendar.MAY, 1).getTime();
		Date checkout = new GregorianCalendar(2020, Calendar.MAY, 6).getTime();
		ArrayList<Room> listRoom = rd.searchFreeRoom(checkin, checkout);		
		Assert.assertNotNull(listRoom);
		Assert.assertEquals(7, listRoom.size());
		for(int i=0; i<listRoom.size(); i++){
			Assert.assertFalse(listRoom.get(i).getId() == 1);
			Assert.assertFalse(listRoom.get(i).getId() == 3);
			Assert.assertFalse(listRoom.get(i).getId() == 5);
		}
	}
	
	@Test
	public void testSearchFreeRoom4(){
		Date checkin = new GregorianCalendar(2020, Calendar.MAY, 2).getTime();
		Date checkout = new GregorianCalendar(2020, Calendar.MAY, 5).getTime();
		ArrayList<Room> listRoom = rd.searchFreeRoom(checkin, checkout);		
		Assert.assertNotNull(listRoom);
		Assert.assertEquals(8, listRoom.size());
		for(int i=0; i<listRoom.size(); i++){
			Assert.assertFalse(listRoom.get(i).getId() == 3);
			Assert.assertFalse(listRoom.get(i).getId() == 5);
		}
	}
	
	@Test
	public void testSearchFreeRoom5(){
		Date checkin = new GregorianCalendar(2020, Calendar.MAY, 9).getTime();
		Date checkout = new GregorianCalendar(2020, 
                  Calendar.MAY, 11).getTime();
		ArrayList<Room> listRoom = rd.searchFreeRoom(checkin, checkout);		
		Assert.assertNotNull(listRoom);
		Assert.assertEquals(9, listRoom.size());
		for(int i=0; i<listRoom.size(); i++){
			Assert.assertFalse(listRoom.get(i).getId() == 1);
		}
	}
	
	@Test
	public void testSearchFreeRoom6(){
		Date checkin = new GregorianCalendar(2020, Calendar.MAY, 11).getTime();
		Date checkout = new GregorianCalendar(2020, 
                Calendar.MAY, 13).getTime();
		ArrayList<Room> listRoom = rd.searchFreeRoom(checkin, checkout);		
		Assert.assertNotNull(listRoom);
		Assert.assertEquals(10, listRoom.size());
	}

BookingDaoTest.java:

package test.unit;

import java.sql.Connection;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import org.junit.Assert;
import org.junit.Test;
import dao.BookingDAO;
import dao.DAO;
import dao.RoomDAO;
import model.BookedRoom;
import model.Booking;
import model.Client;
import model.Room;
import model.User;

public class BookingDaoTest {

	@Test
	public void testAddBooking1() {//exception
		//data preparation
		Booking booking = new Booking();
		booking.setSaleoff(0);
		//booked date
		Date bookedDate = new GregorianCalendar(2020,
                    Calendar.APRIL, 20).getTime();
		booking.setBookedDate(bookedDate);
		//creator
		User creator = new User();
		creator.setId(2);
		booking.setCreator(creator);
		//client
		Client client = new Client();
		client.setId(6);
		booking.setClient(client);
		//room1
		Room room = new Room();
		room.setId(1);
		//booked room1
		BookedRoom br = new BookedRoom();
		br.setRoom(room);
		br.setChecked(false);
		br.setPrice(200);
		br.setSaleoff(0);
		br.setCheckin(new GregorianCalendar(2020, 
               Calendar.APRIL, 28).getTime());		
		br.setCheckout(new GregorianCalendar(2020, 
               Calendar.MAY, 1).getTime());
		booking.getBookedRoom().add(br);
		
		// test		
		BookingDAO bd = new BookingDAO();		
		Connection con = DAO.con;
		try{				
			con.setAutoCommit(false);			
			Assert.assertFalse(bd.addBooking(booking));
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			try{
				if(!con.getAutoCommit()) {
					con.rollback();
					con.setAutoCommit(true);
				}
			}catch(Exception ex){
				ex.printStackTrace();
			}
		}
		return;
	}
	
	
	@Test
	public void testAddBooking2() {//exception
		//data preparation
		Booking booking = new Booking();
		booking.setSaleoff(0);
		//booked date
		Date bookedDate = new GregorianCalendar(2020,
               Calendar.APRIL, 20).getTime();
		booking.setBookedDate(bookedDate);
		//creator
		User creator = new User();
		creator.setId(2);
		booking.setCreator(creator);
		//client
		Client client = new Client();
		client.setId(6);
		booking.setClient(client);
		//room1
		Room room = new Room();
		room.setId(1);
		//booked room1
		BookedRoom br = new BookedRoom();
		br.setRoom(room);
		br.setChecked(false);
		br.setPrice(200);
		br.setSaleoff(0);
		br.setCheckin(new GregorianCalendar(2020, 
               Calendar.APRIL, 25).getTime());		
		br.setCheckout(new GregorianCalendar(2020, 
               Calendar.APRIL, 28).getTime());
		booking.getBookedRoom().add(br);
		//booked room2
		br = new BookedRoom();
		br.setRoom(room);
		br.setChecked(false);
		br.setPrice(200);
		br.setSaleoff(0);
		br.setCheckin(new GregorianCalendar(2020, 
               Calendar.APRIL, 26).getTime());		
		br.setCheckout(new GregorianCalendar(2020, 
                Calendar.APRIL, 28).getTime());
		booking.getBookedRoom().add(br);
		
		// test		
		BookingDAO bd = new BookingDAO();		
		Connection con = DAO.con;
		try{				
			con.setAutoCommit(false);			
			Assert.assertFalse(bd.addBooking(booking));
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			try{
				if(!con.getAutoCommit()) {
					con.rollback();
					con.setAutoCommit(true);
				}
			}catch(Exception ex){
				ex.printStackTrace();
			}
		}
		return;
	}
	
	@Test
	public void testAddBooking3() {//standard
		//data preparation
		Booking booking = new Booking();
		booking.setSaleoff(0);
		//booked date
		Date bookedDate = new GregorianCalendar(2020,
              Calendar.APRIL, 20).getTime();
		booking.setBookedDate(bookedDate);
		//creator
		User creator = new User();
		creator.setId(2);
		booking.setCreator(creator);
		//client
		Client client = new Client();
		client.setId(6);
		booking.setClient(client);
		//room1
		Room room = new Room();
		room.setId(1);
		//booked room1
		BookedRoom br = new BookedRoom();
		br.setRoom(room);
		br.setChecked(false);
		br.setPrice(200);
		br.setSaleoff(0);
		br.setCheckin(new GregorianCalendar(2020, 
              Calendar.APRIL, 25).getTime());		
		br.setCheckout(new GregorianCalendar(2020, 
               Calendar.APRIL, 28).getTime());
		booking.getBookedRoom().add(br);
		
		// test		
		BookingDAO bd = new BookingDAO();		
		Connection con = DAO.con;
		try{				
			con.setAutoCommit(false);			
			Assert.assertTrue(bd.addBooking(booking));
			
			RoomDAO rd = new RoomDAO();
			ArrayList<Room> fr = rd.searchFreeRoom(new
               GregorianCalendar(2020, Calendar.APRIL, 24).getTime(), 
			new GregorianCalendar(2020, Calendar.APRIL,
                   27).getTime());
			Assert.assertNotNull(fr);
			for(Room r:fr) {
				Assert.assertFalse(r.getId() == 1);
			}
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			try{
				if(!con.getAutoCommit()) {
					con.rollback();
					con.setAutoCommit(true);
				}
			}catch(Exception ex){
				ex.printStackTrace();
			}
		}
		return;
	}
	

	@Test
	public void testAddBooking4() {//standard
		//data preparation
		Booking booking = new Booking();
		booking.setSaleoff(0);
		//booked date
		Date bookedDate = new GregorianCalendar(2020,
              Calendar.APRIL, 20).getTime();
		booking.setBookedDate(bookedDate);
		//creator
		User creator = new User();
		creator.setId(2);
		booking.setCreator(creator);
		//client
		Client client = new Client();
		client.setId(6);
		booking.setClient(client);
		//room1
		Room room = new Room();
		room.setId(1);
		//booked room1
		BookedRoom br = new BookedRoom();
		br.setRoom(room);
		br.setChecked(false);
		br.setPrice(200);
		br.setSaleoff(0);
		br.setCheckin(new GregorianCalendar(2020, 
                Calendar.APRIL, 25).getTime());		
		br.setCheckout(new GregorianCalendar(2020, 
                Calendar.APRIL, 28).getTime());
		booking.getBookedRoom().add(br);
		//room2
		room = new Room();
		room.setId(5);
		//booked room2
		br = new BookedRoom();
		br.setRoom(room);
		br.setChecked(false);
		br.setPrice(200);
		br.setSaleoff(0);
		br.setCheckin(new GregorianCalendar(2020, 
              Calendar.APRIL, 25).getTime());		
		br.setCheckout(new GregorianCalendar(2020, 
              Calendar.APRIL, 28).getTime());
		booking.getBookedRoom().add(br);
		
		// test		
		BookingDAO bd = new BookingDAO();		
		Connection con = DAO.con;
		try{				
			con.setAutoCommit(false);			
			Assert.assertTrue(bd.addBooking(booking));
			
			RoomDAO rd = new RoomDAO();
			ArrayList<Room> fr = rd.searchFreeRoom(new
                GregorianCalendar(2020, Calendar.APRIL, 24).getTime(), 
		new GregorianCalendar(2020, Calendar.APRIL, 27).getTime());
			Assert.assertNotNull(fr);
			for(Room r:fr) {
				Assert.assertFalse(r.getId() == 1);
				Assert.assertFalse(r.getId() == 5);
			}
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			try{
				if(!con.getAutoCommit()) {
					con.rollback();
					con.setAutoCommit(true);
				}
			}catch(Exception ex){
				ex.printStackTrace();
			}
		}
		return;
	}
}

JUnit test case code for view room statistic

RoomStatDaoTest.java

package test.unit;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import org.junit.Assert;
import org.junit.Test;
import dao.RoomStatDAO;
import model.RoomStat;

public class RoomStatDaoTest {

	@Test
	public void testGetRoomStat1() {//standard
		RoomStatDAO rsd = new RoomStatDAO();
		Date startDate = new GregorianCalendar(2020, 
              Calendar.MAY, 05).getTime();
		Date endDate = new GregorianCalendar(2020, 
              Calendar.MAY, 11).getTime();
		
		ArrayList<RoomStat> lrs = 
               rsd.getRoomStat(startDate, endDate);
		Assert.assertNotNull(lrs);
		Assert.assertEquals(10, lrs.size());
		Assert.assertTrue(lrs.get(0).getName().equalsIgnoreCase("101"));
		Assert.assertEquals(5, lrs.get(0).getTotalDay(), 0.00001f);
		Assert.assertEquals(950, lrs.get(0).getTotalIncome(),
                     0.00001f);	
		
		Assert.assertTrue(lrs.get(1).getName().equalsIgnoreCase("105"));
		Assert.assertEquals(2, lrs.get(1).getTotalDay(), 0.00001f);
		Assert.assertEquals(300, lrs.get(1).getTotalIncome(),
                 0.00001f);
		
		Assert.assertTrue(lrs.get(2).getName().equalsIgnoreCase("102"));
		Assert.assertEquals(0, lrs.get(2).getTotalDay(), 0.00001f);
		Assert.assertEquals(0, lrs.get(2).getTotalIncome(),
                 0.00001f);
	}
	

	@Test
	public void testGetRoomStat2() {//standard
		RoomStatDAO rsd = new RoomStatDAO();
		Date startDate = new GregorianCalendar(2020,
               Calendar.APRIL, 01).getTime();
		Date endDate = new GregorianCalendar(2020,
               Calendar.DECEMBER, 31).getTime();
		
		ArrayList<RoomStat> lrs = 
                rsd.getRoomStat(startDate, endDate);
		Assert.assertNotNull(lrs);
		Assert.assertEquals(10, lrs.size());
		Assert.assertTrue(lrs.get(0).getName().equalsIgnoreCase("101"));
		Assert.assertEquals(7, lrs.get(0).getTotalDay(), 0.00001f);
		Assert.assertEquals(1350, lrs.get(0).getTotalIncome(),
               0.00001f);	
		
		Assert.assertTrue(lrs.get(1).getName().equalsIgnoreCase("103"));
		Assert.assertEquals(6, lrs.get(1).getTotalDay(), 0.00001f);
		Assert.assertEquals(1200, lrs.get(1).getTotalIncome(),
               0.00001f);
		
		Assert.assertTrue(lrs.get(2).getName().equalsIgnoreCase("105"));
		Assert.assertEquals(7, lrs.get(2).getTotalDay(), 0.00001f);
		Assert.assertEquals(1050, lrs.get(2).getTotalIncome(),
                0.00001f);
		
		Assert.assertTrue(lrs.get(3).getName().equalsIgnoreCase("202"));
		Assert.assertEquals(4, lrs.get(3).getTotalDay(), 0.00001f);
		Assert.assertEquals(600, lrs.get(3).getTotalIncome(),
              0.00001f);
		
		Assert.assertTrue(lrs.get(4).getName().equalsIgnoreCase("102"));
		Assert.assertEquals(0, lrs.get(4).getTotalDay(), 0.00001f);
		Assert.assertEquals(0, lrs.get(4).getTotalIncome(),
              0.00001f);
	}
	

	@Test
	public void testGetRoomStat3() {//exception
		RoomStatDAO rsd = new RoomStatDAO();
		Date startDate = new GregorianCalendar(2020, 
                Calendar.JULY, 01).getTime();
		Date endDate = new GregorianCalendar(2020,
                Calendar.DECEMBER, 31).getTime();
		
		ArrayList<RoomStat> lrs = 
                rsd.getRoomStat(startDate, endDate);
		Assert.assertNotNull(lrs);
		Assert.assertEquals(10, lrs.size());
		Assert.assertTrue(lrs.get(0).getName().equalsIgnoreCase("101"));
		Assert.assertEquals(0, lrs.get(0).getTotalDay(), 0.00001f);
		Assert.assertEquals(0, lrs.get(0).getTotalIncome(),
            0.00001f);
	}
}

BookingDaoTest.java, add following test case:

	@Test
	public void testGetBillofRoom1() {
           //standard - 1 booked sd<ci<co<ed
		BookingDAO bd = new BookingDAO();
		Date startDate = new GregorianCalendar(2020, 
               Calendar.MAY, 04).getTime();
		Date endDate = new GregorianCalendar(2020, 
               Calendar.MAY, 11).getTime();
		
		ArrayList<Booking> lbk = bd.getBookingOfRoom(1, 
               startDate, endDate);
		Assert.assertNotNull(lbk);
		Assert.assertEquals(1, lbk.size());
		Assert.assertEquals(950, lbk.get(0).getTotal(), 0.00001f);
		Assert.assertEquals("Tom",lbk.get(0).getClient().getName());
	}
	
	@Test
	public void testGetBillofRoom2() {
          //standard - 1 booked ci<sd<co<ed
		BookingDAO bd = new BookingDAO();
		Date startDate = new GregorianCalendar(2020, 
              Calendar.MAY, 07).getTime();
		Date endDate = new GregorianCalendar(2020, 
              Calendar.MAY, 11).getTime();
		
		ArrayList<Booking> lbk = bd.getBookingOfRoom(1, 
              startDate, endDate);
		Assert.assertNotNull(lbk);
		Assert.assertEquals(1, lbk.size());
		Assert.assertEquals(570, lbk.get(0).getTotal(), 0.00001f);
		Assert.assertEquals("Tom",lbk.get(0).getClient().getName());
	}
	

	@Test
	public void testGetBillofRoom3() {
          //standard - 1 booked sd<ci<ed<co
		BookingDAO bd = new BookingDAO();
		Date startDate = new GregorianCalendar(2020, 
              Calendar.MAY, 04).getTime();
		Date endDate = new GregorianCalendar(2020, 
              Calendar.MAY, 07).getTime();
		
		ArrayList<Booking> lbk = bd.getBookingOfRoom(1, 
              startDate, endDate);
		Assert.assertNotNull(lbk);
		Assert.assertEquals(1, lbk.size());
		Assert.assertEquals(380, lbk.get(0).getTotal(), 0.00001f);
		Assert.assertEquals("Tom",lbk.get(0).getClient().getName());
	}
	

	@Test
	public void testGetBillofRoom4() {
        //standard - 2 booked sd<ci<co<ed
		BookingDAO bd = new BookingDAO();
		Date startDate = new GregorianCalendar(2020,
                Calendar.APRIL, 28).getTime();
		Date endDate = new GregorianCalendar(2020, 
                Calendar.MAY, 11).getTime();
		
		ArrayList<Booking> lbk = bd.getBookingOfRoom(1, 
              startDate, endDate);
		Assert.assertNotNull(lbk);
		Assert.assertEquals(2, lbk.size());
		Assert.assertEquals(400, lbk.get(0).getTotal(), 0.00001f);
		Assert.assertEquals("Pierre",lbk.get(0).getClient().getName());
		
		Assert.assertEquals(950, lbk.get(1).getTotal(), 0.00001f);
		Assert.assertEquals("Tom",lbk.get(1).getClient().getName());
	}
	
	@Test
	public void testGetBillofRoom5() {
            //exception - 0 booked ci<co<=sd<ed
		BookingDAO bd = new BookingDAO();
		Date startDate = new GregorianCalendar(2020, 
              Calendar.MAY, 10).getTime();
		Date endDate = new GregorianCalendar(2020, 
               Calendar.MAY, 11).getTime();
		
		ArrayList<Booking> lbk = bd.getBookingOfRoom(1, 
                startDate, endDate);
		Assert.assertNotNull(lbk);
		Assert.assertEquals(0, lbk.size());
	}
	

	@Test
	public void testGetBillofRoom6() {
           //exception - 0 booked ci<co<=sd<ed
		BookingDAO bd = new BookingDAO();
		Date startDate = new GregorianCalendar(2020,
              Calendar.APRIL, 01).getTime();
		Date endDate = new GregorianCalendar(2020, 
               Calendar.APRIL, 29).getTime();
		
		ArrayList<Booking> lbk = bd.getBookingOfRoom(1, 
               startDate, endDate);
		Assert.assertNotNull(lbk);
		Assert.assertEquals(0, lbk.size());
	}