desislava_topuzakova

1. Database

Jul 31st, 2021
1,101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.47 KB | None | 0 0
  1. import org.junit.Assert;
  2. import org.junit.Before;
  3. import org.junit.Test;
  4. import p01_Database.Database;
  5.  
  6. import javax.naming.OperationNotSupportedException;
  7.  
  8. public class DatabaseTest {
  9.  
  10.     private Database database;
  11.     private static final Integer[] NUMBERS = {5, 8, 20, 67, 98};
  12.  
  13.     @Before
  14.     public void prepareDatabase() throws OperationNotSupportedException {
  15.         database = new Database(NUMBERS);
  16.     }
  17.  
  18.     //тестваме конструктора
  19.     //1. дали ми създава правилно обект
  20.     @Test
  21.     public void testConstructorHasToCreateValidObject() {
  22.         //взема елементите на създания обект -> проверка дали са същите, които зададох
  23.         Integer[] databaseElements = database.getElements();
  24.         //проверихме дали броя на елементите е еднакъв
  25.         Assert.assertEquals("Count of elements is incorrect.", databaseElements.length, NUMBERS.length);
  26.         for (int i = 0; i < databaseElements.length; i++) {
  27.             Assert.assertEquals("Arrays are not the same!", NUMBERS[i], databaseElements[i]);
  28.         }
  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.  
  38.     //3. елементите < 1
  39.     @Test(expected = OperationNotSupportedException.class)
  40.     public void testConstructorThrowWhenUseLessThanOneElement() throws OperationNotSupportedException {
  41.         Integer[] numbers = new Integer[0];
  42.         new Database(numbers);
  43.     }
  44.  
  45.     //тестваме add
  46.     //1. ако подадения елемент е null
  47.     @Test(expected = OperationNotSupportedException.class)
  48.     public void testAddShouldThrowWhenParamNull() throws OperationNotSupportedException {
  49.         database.add(null);
  50.     }
  51.     //2. ако подадения елемент не е null -> успешно го добавя към спиксъка
  52.     @Test
  53.     public void testAddShouldAddElement() throws OperationNotSupportedException {
  54.         //{5, 8, 20, 67, 98}
  55.         database.add(7);
  56.         //{5, 8, 20, 67, 98, 7}
  57.         Assert.assertEquals(database.getElements().length, NUMBERS.length + 1);
  58.         Assert.assertEquals(database.getElements()[database.getElements().length - 1], Integer.valueOf(7));
  59.     }
  60.  
  61.     //тестваме remove
  62.     //1. да нямаме елементи в масива
  63.     @Test(expected = OperationNotSupportedException.class)
  64.     public void testRemoveShouldThrowWithEmptyData() throws OperationNotSupportedException {
  65.         // премахнем всички същестуващи елементи
  66.         for (int i = 0; i < NUMBERS.length; i++) {
  67.             database.remove();
  68.         }
  69.         //[]
  70.         //remove
  71.         database.remove();
  72.     }
  73.  
  74.     //2. дали правилно премахва елемент
  75.     @Test
  76.     public void testRemoveLastElement() throws OperationNotSupportedException {
  77.         //{5, 8, 20, 67, 98}
  78.         database.remove();
  79.  
  80.         Integer[] elementsInDatabase = database.getElements();  //5, 8, 20, 67
  81.         Assert.assertEquals(elementsInDatabase.length, NUMBERS.length - 1);
  82.         Assert.assertEquals(elementsInDatabase[elementsInDatabase.length - 1], Integer.valueOf(67));
  83.     }
  84. }
  85.  
Advertisement
Add Comment
Please, Sign In to add comment