Nhu Nguyen преди 6 години
родител
ревизия
07d4978942
променени са 1 файла, в които са добавени 167 реда и са изтрити 173 реда
  1. 167
    173
      InventoryTest.java

+ 167
- 173
InventoryTest.java Целия файл

@@ -1,179 +1,173 @@
1 1
 import static org.junit.Assert.*;
2 2
 import java.util.Arrays;
3 3
 import org.junit.Test;
4
+import org.junit.Before;
4 5
 
5 6
 public class InventoryTest {
6
-	
7
-	@Test
8
-	public void should_never_changes_quailty_of_Sulfuras() throws Exception {
9
-		Item sulfuras = new Item("Sulfuras, Hand of Ragnaros", 0, 80);
10
-		
11
-		Inventory sut = new Inventory((Item[]) Arrays.asList(new Item("Sulfuras, Hand of Ragnaros", 0, 80)).toArray());
12
-		
13
-		sut.updateQuality();
14
-		
15
-		assertEquals(80, sulfuras.getQuality());
16
-		
17
-		
18
-	}
19
-	@Test
20
-	public void should_never_changes_sellIn_of_Sulfuras() throws Exception {
21
-		Item sulfuras = new Item("Sulfuras, Hand of Ragnaros", 0, 80);
22
-		
23
-		Inventory sut = new Inventory((Item[]) Arrays.asList(sulfuras).toArray());
24
-		
25
-		sut.updateQuality();
26
-		
27
-		assertEquals(0, sulfuras.getSellIn());
28
-		
29
-		
30
-	}
31
-	
32
-	
33
-	
34
-	@Test
35
-	public void should_lower_the_sellIn_by_one_for_normal_items() throws Exception {
36
-		Item normalItem = new Item("+5 Dexterity Vest", 10, 20);
37
-		
38
-		Inventory sut = new Inventory((Item[]) Arrays.asList(normalItem).toArray());
39
-		
40
-		sut.updateQuality();
41
-		
42
-		
43
-		assertEquals(9, normalItem.getSellIn());
44
-	}
45
-	
46
-	@Test
47
-	public void should_lower_the_quality_by_one_for_normal_items() throws Exception {
48
-		Item normalItem = new Item("+5 Dexterity Vest", 10, 20);
49
-		
50
-		Inventory sut = new Inventory((Item[]) Arrays.asList(normalItem).toArray());
51
-		
52
-		sut.updateQuality();
53
-		
54
-		
55
-		assertEquals(19, normalItem.getQuality());
56
-	}
57
-	
58
-	@Test
59
-	public void should_not_lower_the_quality_below_zero() throws Exception {
60
-		Item normalItem = new Item("+5 Dexterity Vest", 10, 0);
61
-		
62
-		Inventory sut = new Inventory((Item[]) Arrays.asList(normalItem).toArray());
63
-		
64
-		sut.updateQuality();
65
-		
66
-		
67
-		assertEquals(0, normalItem.getQuality());
68
-	}
69
-	
70
-	@Test
71
-	public void should_lower_the_quality_twice_as_fast_once_the_sell_in_date_has_passed() throws Exception {
72
-		Item normalItem = new Item("+5 Dexterity Vest", -1, 25);
73
-		
74
-		Inventory sut = new Inventory((Item[]) Arrays.asList(normalItem).toArray());
75
-		
76
-		sut.updateQuality();
77
-		
78
-		
79
-		assertEquals(23, normalItem.getQuality());
80
-	}
81
-	
82
-	
83
-	@Test
84
-	public void should_increase_the_quality_of_aged_brie_as_it_gets_older() throws Exception {
85
-		Item agedBrie = new Item("Aged Brie", 10, 25);
86
-		
87
-		Inventory sut = new Inventory((Item[]) Arrays.asList(agedBrie).toArray());
88
-		
89
-		sut.updateQuality();
90
-		
91
-		
92
-		assertEquals(26, agedBrie.getQuality());
93
-	}
94
-	
95
-	
96
-	@Test
97
-	public void should_not_increase_the_quality_of_aged_brie_over_50() throws Exception {
98
-		Item agedBrie = new Item("Aged Brie", 10, 50);
99
-		
100
-		Inventory sut = new Inventory((Item[]) Arrays.asList(agedBrie).toArray());
101
-		
102
-		sut.updateQuality();
103
-		
104
-		
105
-		assertEquals(50, agedBrie.getQuality());
106
-	}
107
-	
108
-	@Test
109
-	public void should_lower_backstage_passes_to_zero_quality_once_concert_has_happened() throws Exception {
110
-		Item backStagePass = new Item("Backstage passes to a TAFKAL80ETC concert", -1, 20);
111
-		
112
-		Inventory sut = new Inventory((Item[]) Arrays.asList(backStagePass).toArray());
113
-		
114
-		sut.updateQuality();
115
-		
116
-		
117
-		assertEquals(0, backStagePass.getQuality());
118
-	}
119
-	
120
-	@Test
121
-	public void should_increase_backstage_passes_quality_by_1_when_the_concert_is_more_than_10_days_away() throws Exception {
122
-		Item backStagePass = new Item("Backstage passes to a TAFKAL80ETC concert", 11, 20);
123
-		
124
-		Inventory sut = new Inventory((Item[]) Arrays.asList(backStagePass).toArray());
125
-		
126
-		sut.updateQuality();
127
-		
128
-		
129
-		assertEquals(21, backStagePass.getQuality());
130
-	}
131
-	
132
-	@Test
133
-	public void should_increase_backstage_passes_quality_by_2_when_the_concert_is_10_days_or_less_away() throws Exception {
134
-		Item backStagePass = new Item("Backstage passes to a TAFKAL80ETC concert", 10, 27);
135
-		
136
-		Inventory sut = new Inventory((Item[]) Arrays.asList(backStagePass).toArray());
137
-		
138
-		sut.updateQuality();
139
-		
140
-		
141
-		assertEquals(29, backStagePass.getQuality());
142
-	}
143
-	
144
-	@Test
145
-	public void should_increase_backstage_passes_quality_by_3_when_the_concert_is_5_days_or_less_away() throws Exception {
146
-		Item backStagePass = new Item("Backstage passes to a TAFKAL80ETC concert", 5, 44);
147
-		
148
-		Inventory sut = new Inventory((Item[]) Arrays.asList(backStagePass).toArray());
149
-		
150
-		sut.updateQuality();
151
-		
152
-		
153
-		assertEquals(47, backStagePass.getQuality());
154
-	}
155
-	
156
-	@Test
157
-	public void should_not_increase_backstage_passes_above_a_quality_of_50() throws Exception {
158
-		Item backStagePassMoreThan10DaysAway = new Item("Backstage passes to a TAFKAL80ETC concert", 15, 50);
159
-		
160
-		Item backStagePass10DaysAway = new Item("Backstage passes to a TAFKAL80ETC concert", 5, 49);
161
-		Item backStagePass5DaysAway = new Item("Backstage passes to a TAFKAL80ETC concert", 5, 48);
162
-		
163
-		Inventory sut = new Inventory((Item[]) Arrays.asList(backStagePassMoreThan10DaysAway, backStagePass10DaysAway, backStagePass5DaysAway).toArray());
164
-		
165
-		sut.updateQuality();
166
-		
167
-		
168
-		assertEquals(50, backStagePassMoreThan10DaysAway.getQuality());
169
-		assertEquals(50, backStagePass10DaysAway.getQuality());
170
-		assertEquals(50, backStagePass5DaysAway.getQuality());
171
-	}
172
-	
173
-	
174
-	
175
-	
176
-	
177
-	
178
-	
7
+    private static final int MAX_QUALITY = 50;
8
+    private static final String AGED_BRIE_NAME = "Aged Brie";
9
+    private static final String BACKSTAGE_PASS_NAME = "Backstage passes to a TAFKAL80ETC concert";
10
+    private static final String NORMAL_ITEM_NAME = "+5 Dexterity Vest";
11
+
12
+    private Item sulfuras;
13
+    private Item normalItem;
14
+
15
+    @Before
16
+    public void setup(){
17
+        sulfuras = new Item("Sulfuras, Hand of Ragnaros", 0, 80);
18
+        normalItem = new Item(NORMAL_ITEM_NAME, 10, 20);
19
+    }
20
+
21
+    @Test
22
+    public void testUpdate_ForSulfurasQuality() throws Exception {
23
+        int expectedQuality = sulfuras.getQuality();
24
+        Item[] items = {sulfuras};
25
+        Inventory sut = new Inventory(items);
26
+
27
+        sut.updateQuality();
28
+
29
+        assertEquals(expectedQuality, sulfuras.getQuality());
30
+    }
31
+    @Test
32
+    public void testUpdate_ForSulfurasSellIn() throws Exception {
33
+        int expectedSellIn = sulfuras.getSellIn();
34
+        Item[] items = {sulfuras};
35
+        Inventory sut = new Inventory(items);
36
+
37
+        sut.updateQuality();
38
+
39
+        assertEquals(expectedSellIn, sulfuras.getSellIn());
40
+    }
41
+
42
+    @Test
43
+    public void testUpdate_ForNormalItemSellIn() throws Exception {
44
+        int expectedSellIn = normalItem.getSellIn() - 1;
45
+        Item[] items = {normalItem};
46
+        Inventory sut = new Inventory(items);
47
+
48
+        sut.updateQuality();
49
+
50
+        assertEquals(expectedSellIn, normalItem.getSellIn());
51
+    }
52
+
53
+    @Test
54
+    public void testUpdate_ForNormalItemQuantity() throws Exception {
55
+        int expectedQuality = normalItem.getQuality() - 1;
56
+        Item[] items = {normalItem};
57
+        Inventory sut = new Inventory(items);
58
+
59
+        sut.updateQuality();
60
+
61
+        assertEquals(expectedQuality, normalItem.getQuality());
62
+    }
63
+
64
+    @Test
65
+    public void testUpdate_ForSoldOutItem() throws Exception {
66
+        int minimumQuality = 0;
67
+        Item normalItemWithMinimumQuality = new Item(NORMAL_ITEM_NAME, 10, minimumQuality);
68
+        Item[] items = {normalItemWithMinimumQuality};
69
+        Inventory sut = new Inventory(items);
70
+
71
+        sut.updateQuality();
72
+
73
+        assertEquals(minimumQuality, normalItemWithMinimumQuality.getQuality());
74
+    }
75
+
76
+    @Test
77
+    public void testUpdate_ForNormalItemOnceTheSellInDatePassed() throws Exception {
78
+        Item normalItemWithPassedSellDate = new Item(NORMAL_ITEM_NAME, -1, 25);
79
+        int expectedQuality = normalItemWithPassedSellDate.getQuality() - 2;
80
+        Item[] items = {normalItemWithPassedSellDate};
81
+        Inventory sut = new Inventory(items);
82
+
83
+        sut.updateQuality();
84
+
85
+        assertEquals(expectedQuality, normalItemWithPassedSellDate.getQuality());
86
+    }
87
+
88
+    @Test
89
+    public void testUpdate_AgedBrie() throws Exception {
90
+        Item agedBrie = new Item(AGED_BRIE_NAME, 10, 25);
91
+        int expectedQuality = agedBrie.getQuality() + 1;
92
+        Item[] items = {agedBrie};
93
+        Inventory sut = new Inventory(items);
94
+
95
+        sut.updateQuality();
96
+
97
+        assertEquals(expectedQuality, agedBrie.getQuality());
98
+    }
99
+
100
+    @Test
101
+    public void testUpdate_WhenAgedBrieIsAtMaxQuality() throws Exception {
102
+        Item agedBrie = new Item(AGED_BRIE_NAME, 10, MAX_QUALITY);
103
+        Item[] items = {agedBrie};
104
+        Inventory sut = new Inventory(items);
105
+
106
+        sut.updateQuality();
107
+
108
+        assertEquals(MAX_QUALITY, agedBrie.getQuality());
109
+    }
110
+
111
+    @Test
112
+    public void testUpdate_ForBackstagePassesAfterTheConcertOccurred() throws Exception {
113
+        Item backStagePass = new Item(BACKSTAGE_PASS_NAME, -1, 20);
114
+        Item[] items = {backStagePass};
115
+        Inventory sut = new Inventory(items);
116
+
117
+        sut.updateQuality();
118
+
119
+        assertEquals(0, backStagePass.getQuality());
120
+    }
121
+
122
+    @Test
123
+    public void testUpdate_ForBackstagePassesQualityWhenTheConcertIsMoreThan10DaysAway() throws Exception {
124
+        Item backStagePass = new Item(BACKSTAGE_PASS_NAME, 11, 20);
125
+        int expectedQuality = backStagePass.getQuality() + 1;
126
+        Item[] items = {backStagePass};
127
+        Inventory sut = new Inventory(items);
128
+
129
+        sut.updateQuality();
130
+
131
+        assertEquals(21, backStagePass.getQuality());
132
+    }
133
+
134
+    @Test
135
+    public void testUpdate_ForBackstagePassesWhenTheConcertIs10DaysOrLessAway() throws Exception {
136
+        Item backStagePass = new Item(BACKSTAGE_PASS_NAME, 10, 27);
137
+        int expectedQuality = backStagePass.getQuality() + 2;
138
+        Item[] items = {backStagePass};
139
+        Inventory sut = new Inventory(items);
140
+
141
+        sut.updateQuality();
142
+
143
+        assertEquals(expectedQuality, backStagePass.getQuality());
144
+    }
145
+
146
+    @Test
147
+    public void testUpdate_ForBackstagePassesWhenTheConcertIs5DaysOrLessAway() throws Exception {
148
+        Item backStagePass = new Item(BACKSTAGE_PASS_NAME, 5, 44);
149
+        int expectedQuality = backStagePass.getQuality() + 3;
150
+        Item[] items = {backStagePass};
151
+        Inventory sut = new Inventory(items);
152
+
153
+        sut.updateQuality();
154
+
155
+        assertEquals(expectedQuality, backStagePass.getQuality());
156
+    }
157
+
158
+    @Test
159
+    public void testUpdate_WhenBackstagePassesIsAtOrNeaerMaxQuality() throws Exception {
160
+        Item backStagePassMoreThan10DaysAway = new Item(BACKSTAGE_PASS_NAME, 15, MAX_QUALITY);
161
+        Item backStagePass10DaysAway = new Item(BACKSTAGE_PASS_NAME, 10, MAX_QUALITY - 1);
162
+        Item backStagePass5DaysAway = new Item(BACKSTAGE_PASS_NAME, 5, MAX_QUALITY - 2);
163
+
164
+        Item[] items = {backStagePassMoreThan10DaysAway, backStagePass10DaysAway, backStagePass5DaysAway};
165
+        Inventory sut = new Inventory(items);
166
+
167
+        sut.updateQuality();
168
+
169
+        assertEquals(MAX_QUALITY, backStagePassMoreThan10DaysAway.getQuality());
170
+        assertEquals(MAX_QUALITY, backStagePass10DaysAway.getQuality());
171
+        assertEquals(MAX_QUALITY, backStagePass5DaysAway.getQuality());
172
+    }
179 173
 }