Advertisement
desislava_topuzakova

02. Extended Database

Nov 26th, 2020
1,051
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.95 KB | None | 0 0
  1. package p02_ExtendedDatabase;
  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.     private Database database;
  11.     private static final Person[] PEOPLE = {new Person(1, "First"), new Person(2, "Second"), new Person(3, "Third")};
  12.  
  13.     @Before
  14.     public void prepareDatabase () throws OperationNotSupportedException {
  15.         database = new Database(PEOPLE);
  16.     }
  17.  
  18.     //Constructor Testing
  19.     //1.връща ли правилен обект -> елементите и брой на елементите
  20.     @Test
  21.     public void testConstructorHasToCreateValidObject () throws OperationNotSupportedException {
  22.         Person [] databaseNumbers = database.getElements();
  23.         //ЕЛЕМЕНТИТЕ
  24.         //БРОЙ НА ЕЛЕМЕНТИТЕ
  25.         Assert.assertEquals("Count of elements is incorrect", PEOPLE.length, databaseNumbers.length);
  26.         for (int i = 0; i < databaseNumbers.length; i++) {
  27.             Assert.assertEquals(PEOPLE[i], databaseNumbers[i]);
  28.         }
  29.     }
  30.     //2. случай елемените са над 16
  31.     @Test(expected = OperationNotSupportedException.class)
  32.     public void testConstructorThrowWhenUseMoreThanSixteenElements () throws OperationNotSupportedException {
  33.         Integer[] numbers = new Integer[17];
  34.         new p01_Database.Database(numbers);
  35.     }
  36.     //3. случай елементите са под 1
  37.     @Test(expected = OperationNotSupportedException.class)
  38.     public void testConstructorThrowWhenUseLessThanOneElement () throws OperationNotSupportedException {
  39.         Person[] numbers = new Person[0];
  40.         new Database(numbers);
  41.     }
  42.  
  43.     //Add method testing
  44.  
  45.     //1. подаване на null елемент
  46.     @Test(expected = OperationNotSupportedException.class)
  47.     public void testAddShouldThrowExWhenParamNull () throws OperationNotSupportedException {
  48.         database.add(null);
  49.     }
  50.  
  51.     //2. правилна работа, добавя елемента в масива
  52.     @Test
  53.     public void testAddShouldAddElement () throws OperationNotSupportedException {
  54.         //p1, p2, p3
  55.         database.add(new Person(4, "Forth"));
  56.         //p1, p2, p3, p4
  57.         Assert.assertEquals(4, database.getElements().length);
  58.         Assert.assertEquals(4, database.getElements()[3].getId());
  59.         Assert.assertEquals("Forth", database.getElements()[3].getUsername());
  60.     }
  61.  
  62.     //Remove method testing
  63.     //1. нямаме елементи
  64.     @Test (expected = OperationNotSupportedException.class)
  65.     public void testRemoveShouldThrowExWithEmptyData () throws OperationNotSupportedException {
  66.         for (int i = 0; i < PEOPLE.length; i++) {
  67.             database.remove();
  68.         }
  69.         //{} празен масив
  70.         database.remove();
  71.     }
  72.     //2. дали премахва последния елемент
  73.     @Test
  74.     public void testRemoveLastElement () throws OperationNotSupportedException {
  75.         //p1, p2, p3
  76.         database.remove();
  77.         //p1,p2
  78.         Person [] elementsInDatabase = database.getElements();
  79.         Assert.assertEquals(PEOPLE.length - 1, elementsInDatabase.length);
  80.         Assert.assertEquals(2, elementsInDatabase[elementsInDatabase.length - 1].getId());
  81.         Assert.assertEquals("Second", elementsInDatabase[elementsInDatabase.length - 1].getUsername());
  82.         for (int i = 0; i < elementsInDatabase.length; i++) {
  83.             Assert.assertEquals(elementsInDatabase[i], PEOPLE[i]);
  84.         }
  85.     }
  86.  
  87.  
  88.     //find by username
  89.     //1. подаваме парарметър null
  90.     @Test(expected = OperationNotSupportedException.class)
  91.     public void testFindByUsernameThrowExWithNullUsername() throws OperationNotSupportedException {
  92.         database.findByUsername(null);
  93.     }
  94.  
  95.      //2. нямаме хора
  96.     @Test(expected = OperationNotSupportedException.class)
  97.     public void testFindByUsernameThrowWithEmptyData () throws OperationNotSupportedException {
  98.         database = new Database(null, null);
  99.         database.findByUsername("First");
  100.     }
  101.  
  102.      //3. ако намерим не намерим  точно 1 име
  103.     @Test (expected = OperationNotSupportedException.class)
  104.     public void testFindByUsernameThrowIfSizeIsNotEqualsOne() throws OperationNotSupportedException {
  105.         database = new Database();
  106.         database.findByUsername("First");
  107.     }
  108.  
  109.     //4. да намерим човек по Username
  110.     @Test
  111.     public void testFindByUsernameReturnCorrectPerson() throws OperationNotSupportedException {
  112.         Person resultPerson = database.findByUsername("First");
  113.         //username = "First", id = 1
  114.         Assert.assertEquals(1, resultPerson.getId());
  115.         Assert.assertEquals("First", resultPerson.getUsername());
  116.  
  117.     }
  118.     //find by id
  119.     //1. нямаме хора
  120.     @Test(expected = OperationNotSupportedException.class)
  121.     public void testFindByIdThrowWithEmptyData () throws OperationNotSupportedException {
  122.         database = new Database(null, null);
  123.         database.findById(1);
  124.     }
  125.  
  126.     //2. ако намерим не намерим  точно 1 id
  127.     @Test (expected = OperationNotSupportedException.class)
  128.     public void testFindByIdThrowIfSizeIsNotEqualsOne() throws OperationNotSupportedException {
  129.         database = new Database();
  130.         database.findById(1);
  131.     }
  132.  
  133.     //3 да намерим човек по Username
  134.     @Test
  135.     public void testFindByIdReturnCorrectPerson() throws OperationNotSupportedException {
  136.         Person resultPerson = database.findById(1);
  137.         //username = "First", id = 1
  138.         Assert.assertEquals(1, resultPerson.getId());
  139.         Assert.assertEquals("First", resultPerson.getUsername());
  140.  
  141.         Person resultPerson2 = database.findById(2);
  142.         Assert.assertEquals(2, resultPerson2.getId());
  143.         Assert.assertEquals("Second", resultPerson2.getUsername());
  144.     }
  145. }
  146.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement