desislava_topuzakova

1. Database

Mar 22nd, 2022
1,825
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.58 KB | None | 0 0
  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.     public Database database;
  11.     private static final Integer[] NUMBERS = {7, 45, 34, 12, 98, 23};
  12.  
  13.     @Before
  14.     public void prepareDatabase() throws OperationNotSupportedException {
  15.        database = new Database(NUMBERS);
  16.     }
  17.  
  18.     //1. конструктор
  19.     //1.1. дали създава правилен обект
  20.     @Test
  21.     public void testConstructorHasCreateValidObject () {
  22.         //проверка на елементите -> елементите = зададените
  23.         Integer[] elements = database.getElements();
  24.         //СРАВНЯВАМЕ МАСИВИТЕ
  25.         Assert.assertArrayEquals("Arrays are not the same!", elements, NUMBERS);
  26.  
  27.         //брой елементите
  28.         Assert.assertEquals("Count of elements is incorrect!", elements.length, NUMBERS.length);
  29.         // стойностите на елементите
  30.         for (int index = 0; index < elements.length; index++) {
  31.             Assert.assertEquals("Arrays are not the same!",elements[index], NUMBERS[index]);
  32.         }
  33.     }
  34.     //1.2. елементите > 16
  35.     @Test(expected = OperationNotSupportedException.class)
  36.     public void testConstructorThrowWhenMoreThanSixteenElements () throws OperationNotSupportedException {
  37.         Integer[] numbers = new Integer[17]; //масив със 17 елемента
  38.         new Database(numbers);
  39.     }
  40.     //1.3. елементите < 1
  41.     @Test(expected = OperationNotSupportedException.class)
  42.     public void testConstructorThrowWhenLessThanOneElement () throws OperationNotSupportedException {
  43.         Integer[] numbers = new Integer[0];
  44.         new Database(numbers);
  45.     }
  46.  
  47.     //2. add
  48.     //2.1. успешно добавяме елемент
  49.     @Test
  50.     public void testAddShouldAddElement() throws OperationNotSupportedException {
  51.         //{7, 45, 34, 12, 98, 23} -> 6 елемента
  52.         database.add(67);
  53.         //{7, 45, 34, 12, 98, 23, 67} -> 7 елемента
  54.         Integer[] elements = database.getElements();
  55.         Assert.assertEquals("Invalid add operation!", elements.length, NUMBERS.length + 1);
  56.         Assert.assertEquals(elements[elements.length - 1], Integer.valueOf(67));
  57.     }
  58.     //2.2. добавяне на null
  59.     @Test (expected = OperationNotSupportedException.class)
  60.     public void testAddShouldThrowNullParam() throws OperationNotSupportedException {
  61.         database.add(null);
  62.     }
  63.  
  64.     //3. remove
  65.     //3.1. успешно премахване на елемент
  66.     @Test
  67.     public void testRemoveLastElement () throws OperationNotSupportedException {
  68.         //{7, 45, 34, 12, 98, 23}
  69.         database.remove();
  70.         //{7, 45, 34, 12, 98}
  71.         Integer[] elements = database.getElements();
  72.         Assert.assertEquals(elements.length, NUMBERS.length - 1);
  73.         Assert.assertEquals(elements[elements.length - 1], Integer.valueOf(98));
  74.     }
  75.  
  76.     //3.2. премахване на елемент от празна database (elements = [])
  77.     @Test(expected = OperationNotSupportedException.class)
  78.     public void testRemoveThrowEmptyDatabase () throws OperationNotSupportedException {
  79.         //{7, 45, 34, 12, 98, 23}
  80.         //премахваме всички елементи
  81.         for (int i = 0; i < NUMBERS.length ; i++) {
  82.             database.remove();
  83.         }
  84.         //празна database - 0 елемента
  85.         database.remove();
  86.     }
  87.  
  88. }
  89.  
Advertisement
Add Comment
Please, Sign In to add comment