Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package p02_ExtendedDatabase;
- import org.junit.Assert;
- import org.junit.Before;
- import org.junit.Test;
- import javax.naming.OperationNotSupportedException;
- public class DatabaseTest {
- private Database database;
- private static final Person[] PEOPLE = {new Person(1, "Boris"), new Person(2, "Petar"), new Person(3, "Miroslav")};
- @Before
- public void prepareDatabase() throws OperationNotSupportedException {
- database = new Database(PEOPLE);
- }
- //1. конструктор
- //1.1. дали създава правилен обект
- @Test
- public void testConstructorHasCreateValidObject () {
- //проверка на елементите -> елементите = зададените
- Person[] elements = database.getElements();
- //СРАВНЯВАМЕ МАСИВИТЕ
- Assert.assertArrayEquals("Arrays are not the same!", elements, PEOPLE);
- }
- //1.2. елементите > 16
- @Test(expected = OperationNotSupportedException.class)
- public void testConstructorThrowWhenMoreThanSixteenElements () throws OperationNotSupportedException {
- Person[] people = new Person[17]; //масив със 17 елемента
- new Database(people);
- }
- //1.3. елементите < 1
- @Test(expected = OperationNotSupportedException.class)
- public void testConstructorThrowWhenLessThanOneElement () throws OperationNotSupportedException {
- Person[] people = new Person[0];
- new Database(people);
- }
- //2. add
- //2.1. успешно добавяме елемент
- @Test
- public void testAddShouldAddElement() throws OperationNotSupportedException {
- //[1."Boris", 2. "Petar", 3. "Miroslav"]
- Person person = new Person(4, "Desi");
- database.add(person);
- //[1."Boris", 2. "Petar", 3. "Miroslav", 4. "Desi"]
- Person[] people = database.getElements();
- Assert.assertEquals("Invalid add operation!", people.length, PEOPLE.length + 1);
- Assert.assertEquals(Integer.valueOf(people[people.length - 1].getId()), Integer.valueOf(4));
- Assert.assertEquals(people[people.length - 1].getUsername(), "Desi");
- }
- //2.2. добавяне на null
- @Test (expected = OperationNotSupportedException.class)
- public void testAddShouldThrowNullParam() throws OperationNotSupportedException {
- database.add(null);
- }
- //3. remove
- //3.1. успешно премахване
- @Test
- public void testRemoveLastElement () throws OperationNotSupportedException {
- //[1."Boris", 2. "Petar", 3. "Miroslav"]
- database.remove();
- //[1."Boris", 2. "Petar"]
- Person[] people = database.getElements();
- Assert.assertEquals(people.length, PEOPLE.length - 1);
- Assert.assertEquals(Integer.valueOf(people[people.length - 1].getId()), Integer.valueOf(2));
- Assert.assertEquals(people[people.length - 1].getUsername(), "Petar");
- }
- //3.2. премахване от празна database
- @Test(expected = OperationNotSupportedException.class)
- public void testRemoveThrowEmptyDatabase () throws OperationNotSupportedException {
- database = new Database();
- //database => elements = []
- //празна база данни
- database.remove();
- }
- //4. findByUsername
- //4.1. подаваме null
- @Test (expected = OperationNotSupportedException.class)
- public void testFindByUsernameThrowNullParam () throws OperationNotSupportedException {
- database.findByUsername(null);
- }
- //4.2. ако подам валиден username
- @Test
- public void testFindByUsername () throws OperationNotSupportedException {
- //[1."Boris", 2. "Petar", 3. "Miroslav"]
- Person person = database.findByUsername("Petar");
- //username = "Peter", id = 2
- Assert.assertEquals(2, person.getId());
- Assert.assertEquals("Petar", person.getUsername());
- }
- //4.3. ако нямаме хора в database
- @Test (expected = OperationNotSupportedException.class)
- public void testFindByUsernameThrowEmptyDatabase() throws OperationNotSupportedException {
- database = new Database();
- //database => elements = []
- database.findByUsername("Petar");
- }
- //4.4. ако намерим повече от 2 човека с това име
- @Test (expected = OperationNotSupportedException.class)
- public void testFindByUsernameMoreThanOnePerson () throws OperationNotSupportedException {
- //[1."Boris", 2. "Petar", 3. "Miroslav"]
- database.add(new Person(4, "Miroslav"));
- //[1."Boris", 2. "Petar", 3. "Miroslav", 4. "Miroslav"]
- database.findByUsername("Miroslav");
- }
- //4.5. ако нямаме човек с това име
- @Test (expected = OperationNotSupportedException.class)
- public void testFindByUsernameInvalidName () throws OperationNotSupportedException {
- //[1."Boris", 2. "Petar", 3. "Miroslav"]
- database.findByUsername("Pesho");
- }
- //5. findById
- //5.1. ако подадем валидно id
- @Test
- public void testFindById() throws OperationNotSupportedException {
- //[1."Boris", 2. "Petar", 3. "Miroslav"]
- Person person = database.findById(1); //1."Boris"
- Assert.assertEquals(1, person.getId());
- Assert.assertEquals(person.getUsername(), "Boris");
- }
- //5.2. ако намерим повече от 1 човек с това id
- @Test (expected = OperationNotSupportedException.class)
- public void testFindByIdMoreThanOneId () throws OperationNotSupportedException {
- //[1."Boris", 2. "Petar", 3. "Miroslav"]
- database.add(new Person(2, "Pesho"));
- //[1."Boris", 2. "Petar", 3. "Miroslav", 2. "Pesho"]
- database.findById(2);
- }
- //5.3. празен database
- @Test (expected = OperationNotSupportedException.class)
- public void testFindByIdEmptyData () throws OperationNotSupportedException {
- database = new Database();
- database.findById(2);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment