Skip to content

Commit 25cacdb

Browse files
committed
Added tests for iterator pattern
1 parent 323e4c8 commit 25cacdb

File tree

1 file changed

+108
-0
lines changed

1 file changed

+108
-0
lines changed
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
package com.iluwatar.iterator;
2+
3+
import org.junit.Test;
4+
import org.junit.runner.RunWith;
5+
import org.junit.runners.Parameterized;
6+
7+
import java.util.ArrayList;
8+
import java.util.List;
9+
10+
import static org.junit.Assert.assertEquals;
11+
import static org.junit.Assert.assertNotNull;
12+
import static org.junit.Assert.fail;
13+
14+
/**
15+
* Date: 12/14/15 - 2:58 PM
16+
*
17+
* @author Jeroen Meulemeester
18+
*/
19+
@RunWith(Parameterized.class)
20+
public class TreasureChestTest {
21+
22+
/**
23+
* Create a list of all expected items in the chest.
24+
*
25+
* @return The set of all expected items in the chest
26+
*/
27+
@Parameterized.Parameters
28+
public static List<Object[]> data() {
29+
final List<Object[]> parameters = new ArrayList<>();
30+
parameters.add(new Object[]{new Item(ItemType.POTION, "Potion of courage")});
31+
parameters.add(new Object[]{new Item(ItemType.RING, "Ring of shadows")});
32+
parameters.add(new Object[]{new Item(ItemType.POTION, "Potion of wisdom")});
33+
parameters.add(new Object[]{new Item(ItemType.POTION, "Potion of blood")});
34+
parameters.add(new Object[]{new Item(ItemType.WEAPON, "Sword of silver +1")});
35+
parameters.add(new Object[]{new Item(ItemType.POTION, "Potion of rust")});
36+
parameters.add(new Object[]{new Item(ItemType.POTION, "Potion of healing")});
37+
parameters.add(new Object[]{new Item(ItemType.RING, "Ring of armor")});
38+
parameters.add(new Object[]{new Item(ItemType.WEAPON, "Steel halberd")});
39+
parameters.add(new Object[]{new Item(ItemType.WEAPON, "Dagger of poison")});
40+
return parameters;
41+
}
42+
43+
/**
44+
* One of the expected items in the chest
45+
*/
46+
private final Item expectedItem;
47+
48+
/**
49+
* Create a new test instance, test if the given expected item can be retrieved from the chest
50+
*
51+
* @param expectedItem One of the items that should be in the chest
52+
*/
53+
public TreasureChestTest(final Item expectedItem) {
54+
this.expectedItem = expectedItem;
55+
}
56+
57+
/**
58+
* Test if the expected item can be retrieved from the chest using the {@link ItemIterator}
59+
*/
60+
@Test
61+
public void testIterator() {
62+
final TreasureChest chest = new TreasureChest();
63+
final ItemIterator iterator = chest.Iterator(expectedItem.getType());
64+
assertNotNull(iterator);
65+
66+
while (iterator.hasNext()) {
67+
final Item item = iterator.next();
68+
assertNotNull(item);
69+
assertEquals(this.expectedItem.getType(), item.getType());
70+
71+
final String name = item.toString();
72+
assertNotNull(name);
73+
if (this.expectedItem.toString().equals(name)) {
74+
return;
75+
}
76+
}
77+
78+
fail("Expected to find item [" + this.expectedItem + "] using iterator, but we didn't.");
79+
80+
}
81+
82+
/**
83+
* Test if the expected item can be retrieved from the chest using the {@link
84+
* TreasureChest#getItems()} method
85+
*/
86+
@Test
87+
public void testGetItems() throws Exception {
88+
final TreasureChest chest = new TreasureChest();
89+
final List<Item> items = chest.getItems();
90+
assertNotNull(items);
91+
92+
for (final Item item : items) {
93+
assertNotNull(item);
94+
assertNotNull(item.getType());
95+
assertNotNull(item.toString());
96+
97+
final boolean sameType = this.expectedItem.getType() == item.getType();
98+
final boolean sameName = this.expectedItem.toString().equals(item.toString());
99+
if (sameType && sameName) {
100+
return;
101+
}
102+
}
103+
104+
fail("Expected to find item [" + this.expectedItem + "] in the item list, but we didn't.");
105+
106+
}
107+
108+
}

0 commit comments

Comments
 (0)