InventoryTest.java 5.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. import static org.junit.Assert.*;
  2. import java.util.Arrays;
  3. import org.junit.Test;
  4. import org.junit.Before;
  5. public class InventoryTest {
  6. private static final int MAX_QUALITY = 50;
  7. private static final String AGED_BRIE_NAME = "Aged Brie";
  8. private static final String BACKSTAGE_PASS_NAME = "Backstage passes to a TAFKAL80ETC concert";
  9. private static final String NORMAL_ITEM_NAME = "+5 Dexterity Vest";
  10. private Item sulfuras;
  11. private Item normalItem;
  12. @Before
  13. public void setup(){
  14. sulfuras = new Item("Sulfuras, Hand of Ragnaros", 0, 80);
  15. normalItem = new Item(NORMAL_ITEM_NAME, 10, 20);
  16. }
  17. @Test
  18. public void testUpdate_ForSulfurasQuality() throws Exception {
  19. int expectedQuality = sulfuras.getQuality();
  20. Item[] items = {sulfuras};
  21. Inventory sut = new Inventory(items);
  22. sut.updateQuality();
  23. assertEquals(expectedQuality, sulfuras.getQuality());
  24. }
  25. @Test
  26. public void testUpdate_ForSulfurasSellIn() throws Exception {
  27. int expectedSellIn = sulfuras.getSellIn();
  28. Item[] items = {sulfuras};
  29. Inventory sut = new Inventory(items);
  30. sut.updateQuality();
  31. assertEquals(expectedSellIn, sulfuras.getSellIn());
  32. }
  33. @Test
  34. public void testUpdate_ForNormalItemSellIn() throws Exception {
  35. int expectedSellIn = normalItem.getSellIn() - 1;
  36. Item[] items = {normalItem};
  37. Inventory sut = new Inventory(items);
  38. sut.updateQuality();
  39. assertEquals(expectedSellIn, normalItem.getSellIn());
  40. }
  41. @Test
  42. public void testUpdate_ForNormalItemQuantity() throws Exception {
  43. int expectedQuality = normalItem.getQuality() - 1;
  44. Item[] items = {normalItem};
  45. Inventory sut = new Inventory(items);
  46. sut.updateQuality();
  47. assertEquals(expectedQuality, normalItem.getQuality());
  48. }
  49. @Test
  50. public void testUpdate_ForSoldOutItem() throws Exception {
  51. int minimumQuality = 0;
  52. Item normalItemWithMinimumQuality = new Item(NORMAL_ITEM_NAME, 10, minimumQuality);
  53. Item[] items = {normalItemWithMinimumQuality};
  54. Inventory sut = new Inventory(items);
  55. sut.updateQuality();
  56. assertEquals(minimumQuality, normalItemWithMinimumQuality.getQuality());
  57. }
  58. @Test
  59. public void testUpdate_ForNormalItemOnceTheSellInDatePassed() throws Exception {
  60. Item normalItemWithPassedSellDate = new Item(NORMAL_ITEM_NAME, -1, 25);
  61. int expectedQuality = normalItemWithPassedSellDate.getQuality() - 2;
  62. Item[] items = {normalItemWithPassedSellDate};
  63. Inventory sut = new Inventory(items);
  64. sut.updateQuality();
  65. assertEquals(expectedQuality, normalItemWithPassedSellDate.getQuality());
  66. }
  67. @Test
  68. public void testUpdate_AgedBrie() throws Exception {
  69. Item agedBrie = new Item(AGED_BRIE_NAME, 10, 25);
  70. int expectedQuality = agedBrie.getQuality() + 1;
  71. Item[] items = {agedBrie};
  72. Inventory sut = new Inventory(items);
  73. sut.updateQuality();
  74. assertEquals(expectedQuality, agedBrie.getQuality());
  75. }
  76. @Test
  77. public void testUpdate_WhenAgedBrieIsAtMaxQuality() throws Exception {
  78. Item agedBrie = new Item(AGED_BRIE_NAME, 10, MAX_QUALITY);
  79. Item[] items = {agedBrie};
  80. Inventory sut = new Inventory(items);
  81. sut.updateQuality();
  82. assertEquals(MAX_QUALITY, agedBrie.getQuality());
  83. }
  84. @Test
  85. public void testUpdate_ForBackstagePassesAfterTheConcertOccurred() throws Exception {
  86. Item backStagePass = new Item(BACKSTAGE_PASS_NAME, -1, 20);
  87. Item[] items = {backStagePass};
  88. Inventory sut = new Inventory(items);
  89. sut.updateQuality();
  90. assertEquals(0, backStagePass.getQuality());
  91. }
  92. @Test
  93. public void testUpdate_ForBackstagePassesQualityWhenTheConcertIsMoreThan10DaysAway() throws Exception {
  94. Item backStagePass = new Item(BACKSTAGE_PASS_NAME, 11, 20);
  95. int expectedQuality = backStagePass.getQuality() + 1;
  96. Item[] items = {backStagePass};
  97. Inventory sut = new Inventory(items);
  98. sut.updateQuality();
  99. assertEquals(21, backStagePass.getQuality());
  100. }
  101. @Test
  102. public void testUpdate_ForBackstagePassesWhenTheConcertIs10DaysOrLessAway() throws Exception {
  103. Item backStagePass = new Item(BACKSTAGE_PASS_NAME, 10, 27);
  104. int expectedQuality = backStagePass.getQuality() + 2;
  105. Item[] items = {backStagePass};
  106. Inventory sut = new Inventory(items);
  107. sut.updateQuality();
  108. assertEquals(expectedQuality, backStagePass.getQuality());
  109. }
  110. @Test
  111. public void testUpdate_ForBackstagePassesWhenTheConcertIs5DaysOrLessAway() throws Exception {
  112. Item backStagePass = new Item(BACKSTAGE_PASS_NAME, 5, 44);
  113. int expectedQuality = backStagePass.getQuality() + 3;
  114. Item[] items = {backStagePass};
  115. Inventory sut = new Inventory(items);
  116. sut.updateQuality();
  117. assertEquals(expectedQuality, backStagePass.getQuality());
  118. }
  119. @Test
  120. public void testUpdate_WhenBackstagePassesIsAtOrNeaerMaxQuality() throws Exception {
  121. Item backStagePassMoreThan10DaysAway = new Item(BACKSTAGE_PASS_NAME, 15, MAX_QUALITY);
  122. Item backStagePass10DaysAway = new Item(BACKSTAGE_PASS_NAME, 10, MAX_QUALITY - 1);
  123. Item backStagePass5DaysAway = new Item(BACKSTAGE_PASS_NAME, 5, MAX_QUALITY - 2);
  124. Item[] items = {backStagePassMoreThan10DaysAway, backStagePass10DaysAway, backStagePass5DaysAway};
  125. Inventory sut = new Inventory(items);
  126. sut.updateQuality();
  127. assertEquals(MAX_QUALITY, backStagePassMoreThan10DaysAway.getQuality());
  128. assertEquals(MAX_QUALITY, backStagePass10DaysAway.getQuality());
  129. assertEquals(MAX_QUALITY, backStagePass5DaysAway.getQuality());
  130. }
  131. }