forked from EngineHub/CraftBook
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathItemUtilTest.java
More file actions
175 lines (142 loc) · 6.33 KB
/
ItemUtilTest.java
File metadata and controls
175 lines (142 loc) · 6.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
package com.sk89q.craftbook.util;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.assertFalse;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import org.bukkit.Material;
import org.bukkit.entity.Item;
import org.bukkit.inventory.ItemStack;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
@Ignore
@RunWith(PowerMockRunner.class)
@PrepareForTest({ItemUtil.class, ItemSyntax.class})
public class ItemUtilTest {
@Test
public void testAddToStack() {
ItemStack mockStack = newMockItemStack(Material.SAND,(byte) 0, 38);
ItemStack mockStack2 = newMockItemStack(Material.SAND, (byte) 0, 10);
when(mockStack.getMaxStackSize()).thenReturn(48);
assertNull(ItemUtil.addToStack(mockStack, mockStack2));
verify(mockStack).setAmount(48);
when(mockStack2.getAmount()).thenReturn(11);
ItemUtil.addToStack(mockStack, mockStack2);
verify(mockStack, times(2)).setAmount(48);
verify(mockStack2).setAmount(1);
}
@Test
public void testFilterItems() {
ArrayList<ItemStack> items = new ArrayList<>();
items.add(newMockItemStack(Material.GRASS_BLOCK,(byte) 0,1));
items.add(newMockItemStack(Material.GRAVEL,(byte) 0,1));
items.add(newMockItemStack(Material.STONE,(byte) 0,1));
HashSet<ItemStack> inclusions = new HashSet<>();
inclusions.add(newMockItemStack(Material.GRASS_BLOCK,(byte) 0,1));
List<ItemStack> filtered = ItemUtil.filterItems(items, inclusions, null);
assertEquals(1, filtered.size());
HashSet<ItemStack> exclusions = new HashSet<>();
exclusions.add(newMockItemStack(Material.GRAVEL,(byte) 0,1));
filtered = ItemUtil.filterItems(items, null, exclusions);
assertEquals(2, filtered.size());
}
@Test
public void testAreItemsSimilar() {
ItemStack test1 = newMockItemStack(Material.OAK_PLANKS,(byte) 4,1);
ItemStack test2 = newMockItemStack(Material.OAK_PLANKS,(byte) 8,1);
assertTrue(ItemUtil.areItemsSimilar(test1, test2));
test2 = newMockItemStack(Material.GRASS_BLOCK,(byte) 8,1);
assertTrue(!ItemUtil.areItemsSimilar(test1, test2));
}
@Test
public void testAreItemsIdentical() {
ItemStack test1 = newMockItemStack(Material.OAK_PLANKS,(byte) 4,1);
ItemStack test2 = newMockItemStack(Material.OAK_PLANKS,(byte) 8,1);
assertTrue(!ItemUtil.areItemsIdentical(test1, test2));
test2 = newMockItemStack(Material.GRASS_BLOCK,(byte) 8,1);
assertTrue(!ItemUtil.areItemsIdentical(test1, test2));
test2 = newMockItemStack(Material.OAK_PLANKS,(byte) 4,1);
assertTrue(ItemUtil.areItemsIdentical(test1, test2));
}
@Test
public void testIsStackValid() {
ItemStack mockStack = newMockItemStack(Material.AIR,(byte) 0,5);
assertTrue(!ItemUtil.isStackValid(mockStack));
when(mockStack.getType()).thenReturn(Material.BEETROOT);
when(mockStack.getAmount()).thenReturn(5);
assertTrue(ItemUtil.isStackValid(mockStack));
when(mockStack.getAmount()).thenReturn(-60);
assertTrue(!ItemUtil.isStackValid(mockStack));
assertTrue(!ItemUtil.isStackValid(null));
}
@Test
public void testTakeFromEntity() {
Item entity = mock(Item.class);
when(entity.isDead()).thenReturn(true);
assertTrue(!ItemUtil.takeFromItemEntity(null, 1));
assertTrue(!ItemUtil.takeFromItemEntity(entity, 1));
when(entity.isDead()).thenReturn(false);
when(entity.getItemStack()).thenReturn(ItemSyntax.getItem("2:0*20"));
assertTrue(!ItemUtil.takeFromItemEntity(entity, 21));
assertTrue(ItemUtil.takeFromItemEntity(entity, 2));
assertTrue(ItemUtil.takeFromItemEntity(entity, 18));
verify(entity).remove();
}
@Test
public void testIsCookable() {
ItemStack ingredient = newMockItemStack(Material.CHICKEN, (byte) 0, 1);
assertTrue(ItemUtil.isCookable(ingredient));
ItemStack ingredient2 = newMockItemStack(Material.CLAY, (byte) 0, 1);
assertFalse(ItemUtil.isCookable(ingredient2));
}
@Test
public void testIsSmeltable() {
ItemStack ingredient = newMockItemStack(Material.CLAY, (byte) 0, 1);
assertTrue(ItemUtil.isSmeltable(ingredient));
ItemStack ingredient2 = newMockItemStack(Material.BEDROCK, (byte) 0, 1);
assertFalse(ItemUtil.isCookable(ingredient2));
}
@Test
public void testIsBlastSmeltable() {
ItemStack ingredient = newMockItemStack(Material.IRON_ORE, (byte) 0, 1);
assertTrue(ItemUtil.isBlastSmeltable(ingredient));
ItemStack ingredient2 = newMockItemStack(Material.CHICKEN, (byte) 0, 1);
assertFalse(ItemUtil.isCookable(ingredient2));
}
@Test
public void testGetItem() {
ItemStack ret1 = ItemSyntax.getItem("2");
assertSame(ret1.getType(), Material.GRASS_BLOCK);
assertEquals(ret1.getData().getData(), -1);
assertEquals(1, ret1.getAmount());
ret1 = ItemSyntax.getItem("2:5");
assertSame(ret1.getType(), Material.GRASS_BLOCK);
assertEquals(5, ret1.getData().getData());
assertEquals(1, ret1.getAmount());
ret1 = ItemSyntax.getItem("2:5*4");
assertSame(ret1.getType(), Material.GRASS_BLOCK);
assertEquals(5, ret1.getData().getData());
assertEquals(4, ret1.getAmount());
ret1 = ItemSyntax.getItem("2*4");
assertSame(ret1.getType(), Material.GRASS_BLOCK);
assertEquals(ret1.getData().getData(), -1);
assertEquals(4, ret1.getAmount());
}
public ItemStack newMockItemStack(Material id, byte data, int amount) {
ItemStack mockStack = mock(ItemStack.class);
when(mockStack.getAmount()).thenReturn(amount);
when(mockStack.getType()).thenReturn(id);
when(mockStack.getDurability()).thenReturn((short) data);
return mockStack;
}
}