desislava_topuzakova

01. Database

Nov 26th, 2020
588
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package p01_Database;
  2.  
  3. import org.junit.Assert;
  4. import org.junit.Before;
  5. import org.junit.Test;
  6.  
  7. import javax.naming.OperationNotSupportedException;
  8.  
  9. public class DatabaseTest {
  10.  
  11.     private Database database;
  12.     private static final Integer[] NUMBERS = {5, 9, 29, 45};
  13.  
  14.     @Before
  15.     public void prepareDatabase () throws OperationNotSupportedException {
  16.         database = new Database(NUMBERS);
  17.     }
  18.  
  19.     //Constructor Testing
  20.     //1.връща ли правилен обект -> елементите и брой на елементите
  21.     @Test
  22.     public void testConstructorHasToCreateValidObject () throws OperationNotSupportedException {
  23.         Integer [] databaseNumbers = database.getElements();
  24.         //ЕЛЕМЕНТИТЕ
  25.         //БРОЙ НА ЕЛЕМЕНТИТЕ
  26.         Assert.assertEquals("Count of elements is incorrect", NUMBERS.length, databaseNumbers.length);
  27.         for (int i = 0; i < databaseNumbers.length; i++) {
  28.             Assert.assertEquals(NUMBERS[i], databaseNumbers[i]);
  29.         }
  30.     }
  31.      //2. случай елемените са над 16
  32.     @Test(expected = OperationNotSupportedException.class)
  33.     public void testConstructorThrowWhenUseMoreThanSixteenElements () throws OperationNotSupportedException {
  34.         Integer[] numbers = new Integer[17];
  35.         new Database(numbers);
  36.     }
  37.     //3. случай елементите са под 1
  38.     @Test(expected = OperationNotSupportedException.class)
  39.     public void testConstructorThrowWhenUseLessThanOneElement () throws OperationNotSupportedException {
  40.         Integer[] numbers = new Integer[0];
  41.         new Database(numbers);
  42.     }
  43.  
  44.     //Add method testing
  45.  
  46.     //1. подаване на null елемент
  47.     @Test(expected = OperationNotSupportedException.class)
  48.     public void testAddShouldThrowExWhenParamNull () throws OperationNotSupportedException {
  49.         database.add(null);
  50.     }
  51.  
  52.     //2. правилна работа, добавя елемента в масива
  53.     @Test
  54.     public void testAddShouldAddElement () throws OperationNotSupportedException {
  55.         database.add(17);
  56.         //5, 9, 29, 45, 17
  57.         Assert.assertEquals(5, database.getElements().length);
  58.         Assert.assertEquals(Integer.valueOf(17), database.getElements()[4]);
  59.     }
  60.  
  61.     //Remove method testing
  62.             //1. нямаме елементи
  63.     @Test (expected = OperationNotSupportedException.class)
  64.     public void testRemoveShouldThrowExWithEmptyData () throws OperationNotSupportedException {
  65.         for (int i = 0; i < NUMBERS.length; i++) {
  66.             database.remove();
  67.         }
  68.         //{} празен масив
  69.         database.remove();
  70.     }
  71.     //2. дали премахва последния елемент
  72.     @Test
  73.     public void testRemoveLastElement () throws OperationNotSupportedException {
  74.         database.remove();
  75.         Integer [] elementsInDatabase = database.getElements();
  76.         Assert.assertEquals(NUMBERS.length - 1, elementsInDatabase.length);
  77.         Assert.assertEquals(Integer.valueOf(29), elementsInDatabase[elementsInDatabase.length - 1]);
  78.         for (int i = 0; i < elementsInDatabase.length; i++) {
  79.             Assert.assertEquals(elementsInDatabase[i], NUMBERS[i]);
  80.         }
  81.     }
  82.  
  83. }
  84.  
RAW Paste Data