From 47469b20e705b404bca6a768fe17aef2c1804584 Mon Sep 17 00:00:00 2001 From: Santiago Lo Coco Date: Wed, 6 Mar 2024 14:52:36 +0100 Subject: [PATCH] Add more tests and refactor --- src/main/java/edu/uastw/App.java | 9 ++ .../java/edu/uastw/{ => library}/Library.java | 21 +--- .../uastw/{ => library}/LibraryBuilder.java | 4 +- .../DecreaseBooksCapacityDecorator.java | 4 +- .../IncreaseBooksCapacityDecorator.java | 4 +- .../decorators}/LibraryDecorator.java | 4 +- .../edu/uastw/{ => library/items}/Book.java | 2 +- .../uastw/{ => library/items}/ItemType.java | 2 +- .../{ => library/items}/LibraryItem.java | 2 +- .../uastw/{ => library/items}/Magazine.java | 2 +- src/test/java/LibraryTest.java | 116 +++++++++++++++++- 11 files changed, 143 insertions(+), 27 deletions(-) rename src/main/java/edu/uastw/{ => library}/Library.java (80%) rename src/main/java/edu/uastw/{ => library}/LibraryBuilder.java (87%) rename src/main/java/edu/uastw/{ => library/decorators}/DecreaseBooksCapacityDecorator.java (90%) rename src/main/java/edu/uastw/{ => library/decorators}/IncreaseBooksCapacityDecorator.java (87%) rename src/main/java/edu/uastw/{ => library/decorators}/LibraryDecorator.java (74%) rename src/main/java/edu/uastw/{ => library/items}/Book.java (93%) rename src/main/java/edu/uastw/{ => library/items}/ItemType.java (58%) rename src/main/java/edu/uastw/{ => library/items}/LibraryItem.java (75%) rename src/main/java/edu/uastw/{ => library/items}/Magazine.java (93%) diff --git a/src/main/java/edu/uastw/App.java b/src/main/java/edu/uastw/App.java index d7913eb..d3bac17 100644 --- a/src/main/java/edu/uastw/App.java +++ b/src/main/java/edu/uastw/App.java @@ -1,5 +1,14 @@ package edu.uastw; +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.items.Book; +import edu.uastw.library.items.ItemType; +import edu.uastw.library.items.LibraryItem; +import edu.uastw.library.items.Magazine; + import java.util.Iterator; public class App { diff --git a/src/main/java/edu/uastw/Library.java b/src/main/java/edu/uastw/library/Library.java similarity index 80% rename from src/main/java/edu/uastw/Library.java rename to src/main/java/edu/uastw/library/Library.java index 8d1da39..06adae4 100644 --- a/src/main/java/edu/uastw/Library.java +++ b/src/main/java/edu/uastw/library/Library.java @@ -1,4 +1,7 @@ -package edu.uastw; +package edu.uastw.library; + +import edu.uastw.library.items.ItemType; +import edu.uastw.library.items.LibraryItem; import java.util.ArrayList; import java.util.Iterator; @@ -46,21 +49,7 @@ public class Library implements Iterable { @Override public Iterator iterator() { - return new LibraryItemIterator(); - } - - private class LibraryItemIterator implements Iterator { - private int currentIndex = 0; - - @Override - public boolean hasNext() { - return currentIndex < booksCapacity; - } - - @Override - public LibraryItem next() { - return libraryItems.get(currentIndex++); - } + return libraryItems.stream().limit(booksCapacity).iterator(); } public Iterator customTypeIterator(ItemType type) { diff --git a/src/main/java/edu/uastw/LibraryBuilder.java b/src/main/java/edu/uastw/library/LibraryBuilder.java similarity index 87% rename from src/main/java/edu/uastw/LibraryBuilder.java rename to src/main/java/edu/uastw/library/LibraryBuilder.java index 3ad9669..56fb077 100644 --- a/src/main/java/edu/uastw/LibraryBuilder.java +++ b/src/main/java/edu/uastw/library/LibraryBuilder.java @@ -1,4 +1,6 @@ -package edu.uastw; +package edu.uastw.library; + +import edu.uastw.library.items.LibraryItem; public class LibraryBuilder { private final Library library; diff --git a/src/main/java/edu/uastw/DecreaseBooksCapacityDecorator.java b/src/main/java/edu/uastw/library/decorators/DecreaseBooksCapacityDecorator.java similarity index 90% rename from src/main/java/edu/uastw/DecreaseBooksCapacityDecorator.java rename to src/main/java/edu/uastw/library/decorators/DecreaseBooksCapacityDecorator.java index 84fe2b2..87a3236 100644 --- a/src/main/java/edu/uastw/DecreaseBooksCapacityDecorator.java +++ b/src/main/java/edu/uastw/library/decorators/DecreaseBooksCapacityDecorator.java @@ -1,4 +1,6 @@ -package edu.uastw; +package edu.uastw.library.decorators; + +import edu.uastw.library.Library; public class DecreaseBooksCapacityDecorator extends LibraryDecorator { private final int reducedCapacity; diff --git a/src/main/java/edu/uastw/IncreaseBooksCapacityDecorator.java b/src/main/java/edu/uastw/library/decorators/IncreaseBooksCapacityDecorator.java similarity index 87% rename from src/main/java/edu/uastw/IncreaseBooksCapacityDecorator.java rename to src/main/java/edu/uastw/library/decorators/IncreaseBooksCapacityDecorator.java index 1580142..e859c69 100644 --- a/src/main/java/edu/uastw/IncreaseBooksCapacityDecorator.java +++ b/src/main/java/edu/uastw/library/decorators/IncreaseBooksCapacityDecorator.java @@ -1,4 +1,6 @@ -package edu.uastw; +package edu.uastw.library.decorators; + +import edu.uastw.library.Library; public class IncreaseBooksCapacityDecorator extends LibraryDecorator { private final int additionalCapacity; diff --git a/src/main/java/edu/uastw/LibraryDecorator.java b/src/main/java/edu/uastw/library/decorators/LibraryDecorator.java similarity index 74% rename from src/main/java/edu/uastw/LibraryDecorator.java rename to src/main/java/edu/uastw/library/decorators/LibraryDecorator.java index affd9f5..63b20a2 100644 --- a/src/main/java/edu/uastw/LibraryDecorator.java +++ b/src/main/java/edu/uastw/library/decorators/LibraryDecorator.java @@ -1,4 +1,6 @@ -package edu.uastw; +package edu.uastw.library.decorators; + +import edu.uastw.library.Library; public abstract class LibraryDecorator { protected Library library; diff --git a/src/main/java/edu/uastw/Book.java b/src/main/java/edu/uastw/library/items/Book.java similarity index 93% rename from src/main/java/edu/uastw/Book.java rename to src/main/java/edu/uastw/library/items/Book.java index 552cecd..0f13ceb 100644 --- a/src/main/java/edu/uastw/Book.java +++ b/src/main/java/edu/uastw/library/items/Book.java @@ -1,4 +1,4 @@ -package edu.uastw; +package edu.uastw.library.items; public class Book implements LibraryItem { private final String title; diff --git a/src/main/java/edu/uastw/ItemType.java b/src/main/java/edu/uastw/library/items/ItemType.java similarity index 58% rename from src/main/java/edu/uastw/ItemType.java rename to src/main/java/edu/uastw/library/items/ItemType.java index 52556f5..2dec3f2 100644 --- a/src/main/java/edu/uastw/ItemType.java +++ b/src/main/java/edu/uastw/library/items/ItemType.java @@ -1,4 +1,4 @@ -package edu.uastw; +package edu.uastw.library.items; public enum ItemType { BOOK, diff --git a/src/main/java/edu/uastw/LibraryItem.java b/src/main/java/edu/uastw/library/items/LibraryItem.java similarity index 75% rename from src/main/java/edu/uastw/LibraryItem.java rename to src/main/java/edu/uastw/library/items/LibraryItem.java index adcce09..137cdfe 100644 --- a/src/main/java/edu/uastw/LibraryItem.java +++ b/src/main/java/edu/uastw/library/items/LibraryItem.java @@ -1,4 +1,4 @@ -package edu.uastw; +package edu.uastw.library.items; public interface LibraryItem { String getTitle(); diff --git a/src/main/java/edu/uastw/Magazine.java b/src/main/java/edu/uastw/library/items/Magazine.java similarity index 93% rename from src/main/java/edu/uastw/Magazine.java rename to src/main/java/edu/uastw/library/items/Magazine.java index a8a79d8..340640e 100644 --- a/src/main/java/edu/uastw/Magazine.java +++ b/src/main/java/edu/uastw/library/items/Magazine.java @@ -1,4 +1,4 @@ -package edu.uastw; +package edu.uastw.library.items; public class Magazine implements LibraryItem { private final String title; diff --git a/src/test/java/LibraryTest.java b/src/test/java/LibraryTest.java index bcb10c1..6e35648 100644 --- a/src/test/java/LibraryTest.java +++ b/src/test/java/LibraryTest.java @@ -1,21 +1,34 @@ -import edu.uastw.Book; -import edu.uastw.Library; -import edu.uastw.LibraryItem; +import edu.uastw.library.LibraryBuilder; +import edu.uastw.library.decorators.DecreaseBooksCapacityDecorator; +import edu.uastw.library.decorators.IncreaseBooksCapacityDecorator; +import edu.uastw.library.decorators.LibraryDecorator; +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.items.Magazine; import org.junit.After; import org.junit.Before; import org.junit.Test; +import java.io.ByteArrayOutputStream; +import java.io.PrintStream; import java.lang.reflect.Field; +import java.util.Iterator; import java.util.List; import static org.junit.Assert.*; public class LibraryTest { + private final ByteArrayOutputStream outputStreamCaptor = new ByteArrayOutputStream(); + private static int LIBRARY_CAPACITY = 3; private Library library; @Before public void setUp() { library = Library.getInstance(); + System.setOut(new PrintStream(outputStreamCaptor)); + library.setBooksCapacity(LIBRARY_CAPACITY); } @After @@ -25,10 +38,12 @@ public class LibraryTest { List libraryItems = (List) libraryItemsField.get(library); libraryItems.clear(); } + @Test public void testAddItem() { LibraryItem book = new Book("Test Book", "Test Author"); library.addLibraryItem(book); + assertTrue(library.iterator().hasNext()); } @@ -36,6 +51,7 @@ public class LibraryTest { public void testIterator() { LibraryItem book = new Book("Test Book", "Test Author"); library.addLibraryItem(book); + assertTrue(library.iterator().hasNext()); assertEquals(book, library.iterator().next()); } @@ -52,4 +68,98 @@ public class LibraryTest { assertEquals(3, library.getBooksCapacity()); } + + @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); + + outputStreamCaptor.reset(); + + library.addLibraryItem(book4); + + 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"); + + library.addLibraryItem(book1); + library.addLibraryItem(book2); + library.addLibraryItem(magazine); + + 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()); + } + + @Test + public void testIncreaseBooksCapacityDecorator() { + LibraryDecorator decorator = new IncreaseBooksCapacityDecorator(library, 2); + decorator.extendedFunctionality(); + + assertEquals(5, library.getBooksCapacity()); + } + + @Test + public void testDecreaseBooksCapacityDecorator() { + LibraryDecorator decorator = new DecreaseBooksCapacityDecorator(library, 2); + decorator.extendedFunctionality(); + + assertEquals(1, library.getBooksCapacity()); + } + + @Test + public void testDecreaseBooksCapacityDecoratorMax() { + library.setBooksCapacity(1); + LibraryDecorator decorator = new DecreaseBooksCapacityDecorator(library, 2); + decorator.extendedFunctionality(); + + assertEquals(1, library.getBooksCapacity()); + } + + @Test + public void testDisplayLibraryItems() { + LibraryItem book1 = new Book("Book 1", "Author 1"); + LibraryItem book2 = new Book("Book 2", "Author 2"); + + library.addLibraryItem(book1); + library.addLibraryItem(book2); + + outputStreamCaptor.reset(); + + library.displayLibraryItems(); + String expectedOutput = "Items available in the library:\nBook 1 by Author 1\nBook 2 by Author 2\n"; + + assertEquals(expectedOutput, outputStreamCaptor.toString()); + } + + @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()); + } }