From 61bef1210eb1c757172882b90da13049879e817e Mon Sep 17 00:00:00 2001 From: Santiago Lo Coco Date: Sun, 10 Mar 2024 13:27:06 +0100 Subject: [PATCH] Update tests and refactor --- src/main/java/edu/uastw/App.java | 4 +- src/main/java/edu/uastw/library/Library.java | 13 +- .../library/interfaces/ExceptionRunnable.java | 5 + .../library/interfaces/IgnoreCoverage.java | 4 + .../{items => interfaces}/LibraryItem.java | 4 +- .../java/edu/uastw/library/items/Book.java | 2 + .../edu/uastw/library/items/Magazine.java | 2 + src/test/java/LibraryTest.java | 276 ++++++++++-------- 8 files changed, 185 insertions(+), 125 deletions(-) create mode 100644 src/main/java/edu/uastw/library/interfaces/ExceptionRunnable.java create mode 100644 src/main/java/edu/uastw/library/interfaces/IgnoreCoverage.java rename src/main/java/edu/uastw/library/{items => interfaces}/LibraryItem.java (56%) diff --git a/src/main/java/edu/uastw/App.java b/src/main/java/edu/uastw/App.java index 3da9c87..3c808fc 100644 --- a/src/main/java/edu/uastw/App.java +++ b/src/main/java/edu/uastw/App.java @@ -4,13 +4,15 @@ import edu.uastw.library.*; import edu.uastw.library.decorators.DecreaseBooksCapacityDecorator; import edu.uastw.library.decorators.IncreaseBooksCapacityDecorator; import edu.uastw.library.decorators.LibraryDecorator; +import edu.uastw.library.interfaces.IgnoreCoverage; import edu.uastw.library.items.Book; import edu.uastw.library.items.ItemType; -import edu.uastw.library.items.LibraryItem; +import edu.uastw.library.interfaces.LibraryItem; import edu.uastw.library.items.Magazine; import java.util.Iterator; +@IgnoreCoverage public class App { /***************** Resilience variables *****************/ diff --git a/src/main/java/edu/uastw/library/Library.java b/src/main/java/edu/uastw/library/Library.java index 4590fe3..0386366 100644 --- a/src/main/java/edu/uastw/library/Library.java +++ b/src/main/java/edu/uastw/library/Library.java @@ -2,8 +2,9 @@ package edu.uastw.library; import edu.uastw.library.exceptions.LibraryClosedException; import edu.uastw.library.exceptions.LibraryFullException; +import edu.uastw.library.interfaces.ExceptionRunnable; import edu.uastw.library.items.ItemType; -import edu.uastw.library.items.LibraryItem; +import edu.uastw.library.interfaces.LibraryItem; import java.util.ArrayList; import java.util.Iterator; @@ -15,7 +16,7 @@ public class Library implements Iterable { private final List libraryItems; private int booksCapacity = 3; - /* Resilience variables */ + /**** Resilience variables ****/ private int retryAttempts; private double libraryOpenCondition; private int timeMultiplier; @@ -24,7 +25,7 @@ public class Library implements Iterable { private int timeout; private int tokens; private int interval; - /*************************/ + /******************************/ private Library() { libraryItems = new ArrayList<>(); @@ -49,12 +50,8 @@ public class Library implements Iterable { return false; } - public interface MyRunnable { - void run() throws Exception; - } - // Retry method - private void performWithRetry(MyRunnable action) throws Exception { + private void performWithRetry(ExceptionRunnable action) throws Exception { int attempt = 0; while (attempt < retryAttempts) { System.out.println("Attempt: " + attempt); diff --git a/src/main/java/edu/uastw/library/interfaces/ExceptionRunnable.java b/src/main/java/edu/uastw/library/interfaces/ExceptionRunnable.java new file mode 100644 index 0000000..e8d7680 --- /dev/null +++ b/src/main/java/edu/uastw/library/interfaces/ExceptionRunnable.java @@ -0,0 +1,5 @@ +package edu.uastw.library.interfaces; + +public interface ExceptionRunnable { + void run() throws Exception; +} diff --git a/src/main/java/edu/uastw/library/interfaces/IgnoreCoverage.java b/src/main/java/edu/uastw/library/interfaces/IgnoreCoverage.java new file mode 100644 index 0000000..4f79792 --- /dev/null +++ b/src/main/java/edu/uastw/library/interfaces/IgnoreCoverage.java @@ -0,0 +1,4 @@ +package edu.uastw.library.interfaces; + +public @interface IgnoreCoverage { +} diff --git a/src/main/java/edu/uastw/library/items/LibraryItem.java b/src/main/java/edu/uastw/library/interfaces/LibraryItem.java similarity index 56% rename from src/main/java/edu/uastw/library/items/LibraryItem.java rename to src/main/java/edu/uastw/library/interfaces/LibraryItem.java index 137cdfe..78e39c1 100644 --- a/src/main/java/edu/uastw/library/items/LibraryItem.java +++ b/src/main/java/edu/uastw/library/interfaces/LibraryItem.java @@ -1,4 +1,6 @@ -package edu.uastw.library.items; +package edu.uastw.library.interfaces; + +import edu.uastw.library.items.ItemType; public interface LibraryItem { String getTitle(); diff --git a/src/main/java/edu/uastw/library/items/Book.java b/src/main/java/edu/uastw/library/items/Book.java index 6a04236..701bd62 100644 --- a/src/main/java/edu/uastw/library/items/Book.java +++ b/src/main/java/edu/uastw/library/items/Book.java @@ -1,5 +1,7 @@ package edu.uastw.library.items; +import edu.uastw.library.interfaces.LibraryItem; + public class Book implements LibraryItem { private final String title; private final String author; diff --git a/src/main/java/edu/uastw/library/items/Magazine.java b/src/main/java/edu/uastw/library/items/Magazine.java index 44d38c4..68da5ed 100644 --- a/src/main/java/edu/uastw/library/items/Magazine.java +++ b/src/main/java/edu/uastw/library/items/Magazine.java @@ -1,5 +1,7 @@ package edu.uastw.library.items; +import edu.uastw.library.interfaces.LibraryItem; + public class Magazine implements LibraryItem { private final String title; private final String publisher; diff --git a/src/test/java/LibraryTest.java b/src/test/java/LibraryTest.java index b97cea6..1cb7ec7 100644 --- a/src/test/java/LibraryTest.java +++ b/src/test/java/LibraryTest.java @@ -1,10 +1,11 @@ import edu.uastw.library.decorators.DecreaseBooksCapacityDecorator; import edu.uastw.library.decorators.IncreaseBooksCapacityDecorator; import edu.uastw.library.decorators.LibraryDecorator; +import edu.uastw.library.exceptions.LibraryFullException; import edu.uastw.library.items.Book; import edu.uastw.library.Library; import edu.uastw.library.items.ItemType; -import edu.uastw.library.items.LibraryItem; +import edu.uastw.library.interfaces.LibraryItem; import edu.uastw.library.items.Magazine; import org.junit.After; import org.junit.Before; @@ -19,15 +20,29 @@ import java.util.List; import static org.junit.Assert.*; public class LibraryTest { + private static final int LIBRARY_CAPACITY = 3; + private static final int RETRY_ATTEMPTS = 1; + private static final int LIBRARY_OPEN = -1; + private static final int TIME_MULTIPLIER = 0; + private static final int RATE_LIMIT = 1; + private static final int TIMEOUT = 5; + private static final int INTERVAL = 1; private final ByteArrayOutputStream outputStreamCaptor = new ByteArrayOutputStream(); - private static int LIBRARY_CAPACITY = 3; private Library library; @Before public void setUp() { - library = Library.getInstance(); + library = new Library.Builder() + .setBooksCapacity(LIBRARY_CAPACITY) + .setRetryAttempts(RETRY_ATTEMPTS) + .setLibraryOpenCondition(LIBRARY_OPEN) + .setTimeMultiplier(TIME_MULTIPLIER) + .setRateLimit(RATE_LIMIT) + .setTimeout(TIMEOUT) + .setInterval(INTERVAL) + .build(); + System.setOut(new PrintStream(outputStreamCaptor)); - library.setBooksCapacity(LIBRARY_CAPACITY); } @After @@ -38,142 +53,173 @@ public class LibraryTest { libraryItems.clear(); } - // @Test - // public void testAddItem() { - // LibraryItem book = new Book("Test Book", "Test Author"); - // try { - // library.addLibraryItem(book); - // } catch (Exception e) { - // // TODO Auto-generated catch block - // e.printStackTrace(); - // } + @Test + public void testAddItem() throws Exception { + LibraryItem book = new Book.Builder() + .setTitle("Test Book") + .setAuthor("Test Author") + .build(); - // assertTrue(library.iterator().hasNext()); - // } + library.addLibraryItem(book); - // @Test - // public void testIterator() { - // LibraryItem book = new Book("Test Book", "Test Author"); - // try { - // library.addLibraryItem(book); - // } catch (Exception e) { - // // TODO Auto-generated catch block - // e.printStackTrace(); - // } + assertTrue(library.iterator().hasNext()); + } - // assertTrue(library.iterator().hasNext()); - // assertEquals(book, library.iterator().next()); - // } + @Test + public void testIterator() throws Exception { + LibraryItem book = new Book.Builder() + .setTitle("Test Book") + .setAuthor("Test Author") + .build(); - // @Test - // public void testBooksCapacity() { - // LibraryItem book1 = new Book("Book 1", "Author 1"); - // LibraryItem book2 = new Book("Book 2", "Author 2"); - // LibraryItem book3 = new Book("Book 3", "Author 3"); + library.addLibraryItem(book); - // try { - // library.addLibraryItem(book1); - // library.addLibraryItem(book2); - // library.addLibraryItem(book3); - // } catch (Exception e) { - // // TODO Auto-generated catch block - // e.printStackTrace(); - // } + assertTrue(library.iterator().hasNext()); + assertEquals(book, library.iterator().next()); + } - // assertEquals(3, library.getBooksCapacity()); - // } + @Test + public void testBooksCapacity() throws Exception { + LibraryItem book1 = new Book.Builder() + .setTitle("Book 1") + .setAuthor("Author 1") + .build(); + LibraryItem book2 = new Book.Builder() + .setTitle("Book 2") + .setAuthor("Author 2") + .build(); + LibraryItem book3 = new Book.Builder() + .setTitle("Book 3") + .setAuthor("Author 3") + .build(); - // @Test - // public void testCapacityReached() { - // LibraryItem book1 = new Book("Book 1", "Author 1"); - // LibraryItem book2 = new Book("Book 2", "Author 2"); - // LibraryItem book3 = new Book("Book 3", "Author 3"); - // LibraryItem book4 = new Book("Book 4", "Author 4"); + library.addLibraryItem(book1); + library.addLibraryItem(book2); + library.addLibraryItem(book3); - // library.addLibraryItem(book1); - // library.addLibraryItem(book2); - // library.addLibraryItem(book3); + assertEquals(3, library.getBooksCapacity()); + } - // outputStreamCaptor.reset(); + @Test(expected = LibraryFullException.class) + public void testCapacityReached() throws Exception { + LibraryItem book1 = new Book.Builder() + .setTitle("Book 1") + .setAuthor("Author 1") + .build(); + LibraryItem book2 = new Book.Builder() + .setTitle("Book 2") + .setAuthor("Author 2") + .build(); + LibraryItem book3 = new Book.Builder() + .setTitle("Book 3") + .setAuthor("Author 3") + .build(); + LibraryItem book4 = new Book.Builder() + .setTitle("Book 4") + .setAuthor("Author 4") + .build(); - // library.addLibraryItem(book4); + library.addLibraryItem(book1); + library.addLibraryItem(book2); + library.addLibraryItem(book3); + library.addLibraryItem(book4); - // assertEquals("Library capacity reached. Cannot add more items.\n", outputStreamCaptor.toString()); - // } + assertEquals("Library capacity reached. Cannot add more items.\n", outputStreamCaptor.toString()); + } - // @Test - // public void testCustomIterator() { - // LibraryItem book1 = new Book("Book 1", "Author 1"); - // LibraryItem book2 = new Book("Book 2", "Author 2"); - // LibraryItem magazine = new Magazine("Magazine 1", "Publisher 1"); + @Test + public void testCustomIterator() throws Exception { + LibraryItem book1 = new Book.Builder() + .setTitle("Book 1") + .setAuthor("Author 1") + .build(); + LibraryItem book2 = new Book.Builder() + .setTitle("Book 2") + .setAuthor("Author 2") + .build(); + LibraryItem magazine = new Magazine.Builder() + .setTitle("Magazine 1") + .setPublisher("Publisher 1") + .build(); + library.addLibraryItem(book1); + library.addLibraryItem(book2); + library.addLibraryItem(magazine); - // library.addLibraryItem(book1); - // library.addLibraryItem(book2); - // library.addLibraryItem(magazine); + Iterator bookIterator = library.customTypeIterator(ItemType.BOOK); - // Iterator bookIterator = library.customTypeIterator(ItemType.BOOK); + assertTrue(bookIterator.hasNext()); + assertEquals("Book 1", bookIterator.next().getTitle()); + assertTrue(bookIterator.hasNext()); + assertEquals("Author 2", bookIterator.next().getOwner()); + assertFalse(bookIterator.hasNext()); + } - // assertTrue(bookIterator.hasNext()); - // assertEquals("Book 1", bookIterator.next().getTitle()); - // assertTrue(bookIterator.hasNext()); - // assertEquals("Author 2", bookIterator.next().getOwner()); - // assertFalse(bookIterator.hasNext()); - // } + @Test + public void testIncreaseBooksCapacityDecorator() { + LibraryDecorator decorator = new IncreaseBooksCapacityDecorator(library, 2); + decorator.extendedFunctionality(); - // @Test - // public void testIncreaseBooksCapacityDecorator() { - // LibraryDecorator decorator = new IncreaseBooksCapacityDecorator(library, 2); - // decorator.extendedFunctionality(); + assertEquals(5, library.getBooksCapacity()); + } - // assertEquals(5, library.getBooksCapacity()); - // } + @Test + public void testDecreaseBooksCapacityDecorator() { + LibraryDecorator decorator = new DecreaseBooksCapacityDecorator(library, 2); + decorator.extendedFunctionality(); - // @Test - // public void testDecreaseBooksCapacityDecorator() { - // LibraryDecorator decorator = new DecreaseBooksCapacityDecorator(library, 2); - // decorator.extendedFunctionality(); + assertEquals(1, library.getBooksCapacity()); + } - // assertEquals(1, library.getBooksCapacity()); - // } + @Test + public void testDecreaseBooksCapacityDecoratorMax() { + library.setBooksCapacity(1); + LibraryDecorator decorator = new DecreaseBooksCapacityDecorator(library, 2); + decorator.extendedFunctionality(); - // @Test - // public void testDecreaseBooksCapacityDecoratorMax() { - // library.setBooksCapacity(1); - // LibraryDecorator decorator = new DecreaseBooksCapacityDecorator(library, 2); - // decorator.extendedFunctionality(); + assertEquals(1, library.getBooksCapacity()); + } - // assertEquals(1, library.getBooksCapacity()); - // } + @Test + public void testDisplayLibraryItems() throws Exception { + LibraryItem book1 = new Book.Builder() + .setTitle("Book 1") + .setAuthor("Author 1") + .build(); + LibraryItem book2 = new Book.Builder() + .setTitle("Book 2") + .setAuthor("Author 2") + .build(); + library.addLibraryItem(book1); + library.addLibraryItem(book2); - // @Test - // public void testDisplayLibraryItems() { - // LibraryItem book1 = new Book("Book 1", "Author 1"); - // LibraryItem book2 = new Book("Book 2", "Author 2"); + library.displayLibraryItems(); - // library.addLibraryItem(book1); - // library.addLibraryItem(book2); + String expectedOutput = "Items available in the library:\nBook 1 by Author 1\nBook 2 by Author 2\n"; + assertTrue(outputStreamCaptor.toString().contains(expectedOutput)); + } - // outputStreamCaptor.reset(); + @Test + public void testBuilder() { + Library library = new Library.Builder() + .setBooksCapacity(3) + .build(); - // library.displayLibraryItems(); - // String expectedOutput = "Items available in the library:\nBook 1 by Author 1\nBook 2 by Author 2\n"; + assertEquals(3, library.getBooksCapacity()); + } - // assertEquals(expectedOutput, outputStreamCaptor.toString()); - // } + @Test() + public void testRemoveLibraryItem() throws Exception { + LibraryItem book1 = new Book.Builder() + .setTitle("Book 1") + .setAuthor("Author 1") + .build(); + LibraryItem book2 = new Book.Builder() + .setTitle("Book 2") + .setAuthor("Author 2") + .build(); + library.addLibraryItem(book1); + library.addLibraryItem(book2); - // @Test - // public void testBuilder() { - // LibraryItem book1 = new Book("Book 1", "Author 1"); - // LibraryItem book2 = new Book("Book 2", "Author 2"); - // LibraryItem book3 = new Book("Book 3", "Author 3"); - - // Library library = new LibraryBuilder() - // .setBooksCapacity(3) - // .addLibraryItem(book1) - // .addLibraryItem(book2) - // .addLibraryItem(book3) - // .build(); - - // assertEquals(3, library.getBooksCapacity()); - // } + library.removeLibraryItem(book1); + } }