Add more tests and refactor

This commit is contained in:
Santiago Lo Coco 2024-03-06 14:52:36 +01:00
parent 8b125d4911
commit 47469b20e7
11 changed files with 143 additions and 27 deletions

View File

@ -1,5 +1,14 @@
package edu.uastw; 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; import java.util.Iterator;
public class App { public class App {

View File

@ -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.ArrayList;
import java.util.Iterator; import java.util.Iterator;
@ -46,21 +49,7 @@ public class Library implements Iterable<LibraryItem> {
@Override @Override
public Iterator<LibraryItem> iterator() { public Iterator<LibraryItem> iterator() {
return new LibraryItemIterator(); return libraryItems.stream().limit(booksCapacity).iterator();
}
private class LibraryItemIterator implements Iterator<LibraryItem> {
private int currentIndex = 0;
@Override
public boolean hasNext() {
return currentIndex < booksCapacity;
}
@Override
public LibraryItem next() {
return libraryItems.get(currentIndex++);
}
} }
public Iterator<LibraryItem> customTypeIterator(ItemType type) { public Iterator<LibraryItem> customTypeIterator(ItemType type) {

View File

@ -1,4 +1,6 @@
package edu.uastw; package edu.uastw.library;
import edu.uastw.library.items.LibraryItem;
public class LibraryBuilder { public class LibraryBuilder {
private final Library library; private final Library library;

View File

@ -1,4 +1,6 @@
package edu.uastw; package edu.uastw.library.decorators;
import edu.uastw.library.Library;
public class DecreaseBooksCapacityDecorator extends LibraryDecorator { public class DecreaseBooksCapacityDecorator extends LibraryDecorator {
private final int reducedCapacity; private final int reducedCapacity;

View File

@ -1,4 +1,6 @@
package edu.uastw; package edu.uastw.library.decorators;
import edu.uastw.library.Library;
public class IncreaseBooksCapacityDecorator extends LibraryDecorator { public class IncreaseBooksCapacityDecorator extends LibraryDecorator {
private final int additionalCapacity; private final int additionalCapacity;

View File

@ -1,4 +1,6 @@
package edu.uastw; package edu.uastw.library.decorators;
import edu.uastw.library.Library;
public abstract class LibraryDecorator { public abstract class LibraryDecorator {
protected Library library; protected Library library;

View File

@ -1,4 +1,4 @@
package edu.uastw; package edu.uastw.library.items;
public class Book implements LibraryItem { public class Book implements LibraryItem {
private final String title; private final String title;

View File

@ -1,4 +1,4 @@
package edu.uastw; package edu.uastw.library.items;
public enum ItemType { public enum ItemType {
BOOK, BOOK,

View File

@ -1,4 +1,4 @@
package edu.uastw; package edu.uastw.library.items;
public interface LibraryItem { public interface LibraryItem {
String getTitle(); String getTitle();

View File

@ -1,4 +1,4 @@
package edu.uastw; package edu.uastw.library.items;
public class Magazine implements LibraryItem { public class Magazine implements LibraryItem {
private final String title; private final String title;

View File

@ -1,21 +1,34 @@
import edu.uastw.Book; import edu.uastw.library.LibraryBuilder;
import edu.uastw.Library; import edu.uastw.library.decorators.DecreaseBooksCapacityDecorator;
import edu.uastw.LibraryItem; 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.After;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import java.lang.reflect.Field; import java.lang.reflect.Field;
import java.util.Iterator;
import java.util.List; import java.util.List;
import static org.junit.Assert.*; import static org.junit.Assert.*;
public class LibraryTest { public class LibraryTest {
private final ByteArrayOutputStream outputStreamCaptor = new ByteArrayOutputStream();
private static int LIBRARY_CAPACITY = 3;
private Library library; private Library library;
@Before @Before
public void setUp() { public void setUp() {
library = Library.getInstance(); library = Library.getInstance();
System.setOut(new PrintStream(outputStreamCaptor));
library.setBooksCapacity(LIBRARY_CAPACITY);
} }
@After @After
@ -25,10 +38,12 @@ public class LibraryTest {
List<LibraryItem> libraryItems = (List<LibraryItem>) libraryItemsField.get(library); List<LibraryItem> libraryItems = (List<LibraryItem>) libraryItemsField.get(library);
libraryItems.clear(); libraryItems.clear();
} }
@Test @Test
public void testAddItem() { public void testAddItem() {
LibraryItem book = new Book("Test Book", "Test Author"); LibraryItem book = new Book("Test Book", "Test Author");
library.addLibraryItem(book); library.addLibraryItem(book);
assertTrue(library.iterator().hasNext()); assertTrue(library.iterator().hasNext());
} }
@ -36,6 +51,7 @@ public class LibraryTest {
public void testIterator() { public void testIterator() {
LibraryItem book = new Book("Test Book", "Test Author"); LibraryItem book = new Book("Test Book", "Test Author");
library.addLibraryItem(book); library.addLibraryItem(book);
assertTrue(library.iterator().hasNext()); assertTrue(library.iterator().hasNext());
assertEquals(book, library.iterator().next()); assertEquals(book, library.iterator().next());
} }
@ -52,4 +68,98 @@ public class LibraryTest {
assertEquals(3, library.getBooksCapacity()); 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<LibraryItem> 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());
}
} }