|
5 | 5 | import static org.junit.Assert.*; |
6 | 6 |
|
7 | 7 |
|
| 8 | + |
| 9 | +import java.io.ByteArrayOutputStream; |
| 10 | +import java.io.PrintStream; |
| 11 | +import java.util.logging.Logger; |
| 12 | + |
| 13 | + |
8 | 14 | public class BookStoreTest { |
9 | 15 |
|
10 | 16 | @Test |
@@ -40,4 +46,68 @@ public void testWithOutOfStock() { |
40 | 46 | assertFalse(bookStoreOutOfStock.isInStock()); |
41 | 47 | } |
42 | 48 |
|
| 49 | + @Test |
| 50 | + public void testPrepareZeroPrice() { |
| 51 | + Book book = new Book("Zero Price Book", 0.00, true, 5); |
| 52 | + BookStore bookStore = DataPreparation.prepareBook(book); |
| 53 | + |
| 54 | + assertNotNull(bookStore); |
| 55 | + assertEquals(0.00, bookStore.getDiscountPrice(), 0.01); |
| 56 | + assertTrue(bookStore.isInStock()); |
| 57 | + } |
| 58 | + |
| 59 | + @Test |
| 60 | + public void testPrepareNegativeStock() { |
| 61 | + Book book = new Book("Negative Stock Book", 10.00, false, -1); |
| 62 | + BookStore bookStore = DataPreparation.prepareBook(book); |
| 63 | + |
| 64 | + assertNotNull(bookStore); |
| 65 | + assertFalse(bookStore.isInStock()); |
| 66 | + } |
| 67 | + |
| 68 | + @Test |
| 69 | + public void testPresentInStock() { |
| 70 | + BookStore bookInStock = new BookStore("Wonder Woman Vol. 1", 15.00, 12.00, true); |
| 71 | + String htmlOutput = Presentation.presentBook(bookInStock); |
| 72 | + |
| 73 | + assertNotNull(htmlOutput); |
| 74 | + assertTrue(htmlOutput.contains("<h1>Wonder Woman Vol. 1</h1>")); |
| 75 | + assertTrue(htmlOutput.contains("<p>Price: $15.0</p>")); |
| 76 | + assertTrue(htmlOutput.contains("<p>Discounted Price: $12.0</p>")); |
| 77 | + assertTrue(htmlOutput.contains("<p>Status: In Stock</p>")); |
| 78 | + } |
| 79 | + |
| 80 | + @Test |
| 81 | + public void testPresentOutOfStock() { |
| 82 | + BookStore bookOutOfStock = new BookStore("Wonder Woman Vol. 1", 15.00, 12.00, false); |
| 83 | + String htmlOutput = Presentation.presentBook(bookOutOfStock); |
| 84 | + |
| 85 | + assertNotNull(htmlOutput); |
| 86 | + assertTrue(htmlOutput.contains("<h1>Wonder Woman Vol. 1</h1>")); |
| 87 | + assertTrue(htmlOutput.contains("<p>Price: $15.0</p>")); |
| 88 | + assertTrue(htmlOutput.contains("<p>Discounted Price: $12.0</p>")); |
| 89 | + assertTrue(htmlOutput.contains("<p>Status: Out of Stock</p>")); |
| 90 | + } |
| 91 | + |
| 92 | + @Test |
| 93 | + public void testMain() { |
| 94 | + // Redirect System.out to capture logger output |
| 95 | + ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); |
| 96 | + PrintStream printStream = new PrintStream(outputStream); |
| 97 | + Logger.getLogger("").addHandler(new java.util.logging.ConsoleHandler() { |
| 98 | + { |
| 99 | + setOutputStream(printStream); |
| 100 | + } |
| 101 | + }); |
| 102 | + |
| 103 | + // Run the main method |
| 104 | + App.main(new String[]{}); |
| 105 | + |
| 106 | + // Check output contains expected HTML |
| 107 | + String loggedOutput = outputStream.toString(); |
| 108 | + assertTrue(loggedOutput.contains("<h1>Batman Vol. 1: The Court of Owls</h1>")); |
| 109 | + assertTrue(loggedOutput.contains("<p>Price: $11.6</p>")); |
| 110 | + assertTrue(loggedOutput.contains("<p>Discounted Price: $8.7</p>")); |
| 111 | + assertTrue(loggedOutput.contains("<p>Status: In Stock</p>")); |
| 112 | + } |
43 | 113 | } |
0 commit comments