|
| 1 | +package com.codedifferently.lesson16.librarybook; |
| 2 | + |
| 3 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 4 | +import static org.junit.jupiter.api.Assertions.assertFalse; |
| 5 | +import static org.junit.jupiter.api.Assertions.assertThrows; |
| 6 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 7 | +import static org.junit.jupiter.api.Assertions.fail; |
| 8 | + |
| 9 | +import java.util.List; |
| 10 | +import org.junit.jupiter.api.BeforeEach; |
| 11 | +import org.junit.jupiter.api.Test; |
| 12 | + |
| 13 | +public class LibraryBookTest { |
| 14 | + |
| 15 | + private LibraryBook book; |
| 16 | + |
| 17 | + @BeforeEach |
| 18 | + public void setUp() { |
| 19 | + book = new LibraryBook("1984", "George Orwell", LibraryBook.Genre.FICTION, 1949); |
| 20 | + } |
| 21 | + |
| 22 | + @Test |
| 23 | + public void testConstructor() { |
| 24 | + assertEquals("1984", book.getTitle()); |
| 25 | + assertEquals("George Orwell", book.getAuthor()); |
| 26 | + assertEquals(LibraryBook.Genre.FICTION, book.getGenre()); |
| 27 | + assertEquals(1949, book.getPublicationYear()); |
| 28 | + } |
| 29 | + |
| 30 | + @Test |
| 31 | + public void testCheckOut() throws LibraryBook.OverdueException { |
| 32 | + book.checkOut("Alice"); |
| 33 | + assertTrue(book.isCheckedOut()); |
| 34 | + } |
| 35 | + |
| 36 | + @Test |
| 37 | + public void testReturnBook() throws LibraryBook.OverdueException { |
| 38 | + book.checkOut("Alice"); |
| 39 | + book.returnBook(false); |
| 40 | + assertFalse(book.isCheckedOut()); |
| 41 | + } |
| 42 | + |
| 43 | + @Test |
| 44 | + public void testDisplayBorrowerHistory() throws LibraryBook.OverdueException { |
| 45 | + book.checkOut("Alice"); |
| 46 | + book.returnBook(false); |
| 47 | + book.checkOut("Bob"); |
| 48 | + book.returnBook(false); |
| 49 | + |
| 50 | + String expectedHistory = "Borrower History:\n- Alice\n- Bob\n"; |
| 51 | + |
| 52 | + assertEquals(expectedHistory, book.displayBorrowerHistory()); |
| 53 | + } |
| 54 | + |
| 55 | + @Test |
| 56 | + public void testOverdueExceptionOnReturn() throws LibraryBook.OverdueException { |
| 57 | + book.checkOut("Alice"); |
| 58 | + Exception exception = |
| 59 | + assertThrows( |
| 60 | + LibraryBook.OverdueException.class, |
| 61 | + () -> { |
| 62 | + book.returnBook(true); |
| 63 | + }); |
| 64 | + assertEquals("Book is overdue!", exception.getMessage()); |
| 65 | + } |
| 66 | + |
| 67 | + @Test |
| 68 | + public void testCheckOutAlreadyCheckedOut() throws LibraryBook.OverdueException { |
| 69 | + book.checkOut("Alice"); |
| 70 | + Exception exception = |
| 71 | + assertThrows( |
| 72 | + LibraryBook.OverdueException.class, |
| 73 | + () -> { |
| 74 | + book.checkOut("Bob"); |
| 75 | + }); |
| 76 | + assertEquals("Book is already checked out!", exception.getMessage()); |
| 77 | + } |
| 78 | + |
| 79 | + @Test |
| 80 | + public void testReturnBookNotCheckedOut() { |
| 81 | + Exception exception = |
| 82 | + assertThrows( |
| 83 | + LibraryBook.OverdueException.class, |
| 84 | + () -> { |
| 85 | + book.returnBook(false); |
| 86 | + }); |
| 87 | + assertEquals("Book was not checked out!", exception.getMessage()); |
| 88 | + } |
| 89 | + |
| 90 | + @Test |
| 91 | + public void testCheckOutAddsToHistory() throws LibraryBook.OverdueException { |
| 92 | + book.checkOut("Charlie"); |
| 93 | + assertEquals(1, book.getBorrowersHistory().size()); |
| 94 | + assertEquals("Charlie", book.getBorrowersHistory().get(0)); |
| 95 | + } |
| 96 | + |
| 97 | + @Test |
| 98 | + public void testMultipleCheckOutsAndReturns() throws LibraryBook.OverdueException { |
| 99 | + book.checkOut("Alice"); |
| 100 | + book.returnBook(false); |
| 101 | + book.checkOut("Bob"); |
| 102 | + book.returnBook(false); |
| 103 | + book.checkOut("Charlie"); |
| 104 | + |
| 105 | + assertEquals(3, book.getBorrowersHistory().size()); |
| 106 | + assertEquals("Alice", book.getBorrowersHistory().get(0)); |
| 107 | + assertEquals("Bob", book.getBorrowersHistory().get(1)); |
| 108 | + assertEquals("Charlie", book.getBorrowersHistory().get(2)); |
| 109 | + } |
| 110 | + |
| 111 | + @Test |
| 112 | + public void testBorrowerHistoryIsEmptyInitially() { |
| 113 | + assertEquals(0, book.getBorrowersHistory().size()); |
| 114 | + } |
| 115 | + |
| 116 | + @Test |
| 117 | + public void testGenre() { |
| 118 | + assertEquals(LibraryBook.Genre.FICTION, book.getGenre()); |
| 119 | + } |
| 120 | + |
| 121 | + @Test |
| 122 | + public void testSetTitle() { |
| 123 | + book.setTitle("Animal Farm"); |
| 124 | + assertEquals("Animal Farm", book.getTitle(), "Title should be updated to 'Animal Farm'"); |
| 125 | + } |
| 126 | + |
| 127 | + @Test |
| 128 | + public void testSetAuthor() { |
| 129 | + book.setAuthor("Aldous Huxley"); |
| 130 | + assertEquals("Aldous Huxley", book.getAuthor(), "Author should be updated to 'Aldous Huxley'"); |
| 131 | + } |
| 132 | + |
| 133 | + @Test |
| 134 | + public void testSetGenre() { |
| 135 | + book.setGenre(LibraryBook.Genre.NONFICTION); |
| 136 | + assertEquals( |
| 137 | + LibraryBook.Genre.NONFICTION, book.getGenre(), "Genre should be updated to NON_FICTION"); |
| 138 | + } |
| 139 | + |
| 140 | + @Test |
| 141 | + public void testSetPublicationYear() { |
| 142 | + book.setPublicationYear(1932); |
| 143 | + assertEquals(1932, book.getPublicationYear(), "Publication year should be updated to 1932"); |
| 144 | + } |
| 145 | + |
| 146 | + @Test |
| 147 | + public void testSetCheckedOutStatus() { |
| 148 | + book.setCheckedOut(true); |
| 149 | + assertTrue(book.isCheckedOut(), "Checked out status should be updated to true"); |
| 150 | + } |
| 151 | + |
| 152 | + @Test |
| 153 | + public void testSetBorrowersHistory() { |
| 154 | + book.setBorrowersHistory(List.of("Charlie", "Dana")); |
| 155 | + |
| 156 | + assertEquals( |
| 157 | + 2, book.getBorrowersHistory().size(), "Borrowers history should contain 2 entries"); |
| 158 | + assertEquals( |
| 159 | + "Charlie", book.getBorrowersHistory().get(0), "First borrower should be 'Charlie'"); |
| 160 | + assertEquals("Dana", book.getBorrowersHistory().get(1), "Second borrower should be 'Dana'"); |
| 161 | + |
| 162 | + try { |
| 163 | + book.getBorrowersHistory().add("Eve"); |
| 164 | + fail("borrowersHistory should not be modifiable externally"); |
| 165 | + } catch (UnsupportedOperationException e) { |
| 166 | + } |
| 167 | + } |
| 168 | + |
| 169 | + @Test |
| 170 | + public void testSetNewBorrowers() { |
| 171 | + book.setNewBorrowers(List.of("Alice", "Bob")); |
| 172 | + |
| 173 | + assertEquals(2, book.getNewBorrowers().size(), "New borrowers should contain 2 entries"); |
| 174 | + assertEquals("Alice", book.getNewBorrowers().get(0), "First new borrower should be 'Alice'"); |
| 175 | + assertEquals("Bob", book.getNewBorrowers().get(1), "Second new borrower should be 'Bob'"); |
| 176 | + } |
| 177 | + |
| 178 | + @Test |
| 179 | + public void testGetNewBorrowers() { |
| 180 | + book.setNewBorrowers(List.of("Charlie", "Dana")); |
| 181 | + |
| 182 | + List<String> borrowers = book.getNewBorrowers(); |
| 183 | + assertEquals(2, borrowers.size(), "Should return 2 new borrowers"); |
| 184 | + assertEquals("Charlie", borrowers.get(0), "First new borrower should be 'Charlie'"); |
| 185 | + assertEquals("Dana", borrowers.get(1), "Second new borrower should be 'Dana'"); |
| 186 | + } |
| 187 | +} |
0 commit comments