-
-
Notifications
You must be signed in to change notification settings - Fork 134
/
VisitorKeepInventoryListenerTest.java
223 lines (197 loc) · 8.02 KB
/
VisitorKeepInventoryListenerTest.java
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
package world.bentobox.bentobox.listeners.flags.worldsettings;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.UUID;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.World;
import org.bukkit.entity.Player;
import org.bukkit.event.entity.PlayerDeathEvent;
import org.bukkit.inventory.ItemStack;
import org.bukkit.metadata.FixedMetadataValue;
import org.bukkit.util.Vector;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import org.powermock.reflect.Whitebox;
import com.google.common.collect.ImmutableSet;
import world.bentobox.bentobox.BentoBox;
import world.bentobox.bentobox.api.configuration.WorldSettings;
import world.bentobox.bentobox.api.user.User;
import world.bentobox.bentobox.database.objects.Island;
import world.bentobox.bentobox.lists.Flags;
import world.bentobox.bentobox.managers.IslandWorldManager;
import world.bentobox.bentobox.managers.IslandsManager;
import world.bentobox.bentobox.util.Util;
/**
* @author tastybento
*
*/
@RunWith(PowerMockRunner.class)
@PrepareForTest({BentoBox.class, Util.class})
public class VisitorKeepInventoryListenerTest {
// Class under test
private VisitorKeepInventoryListener l;
@Mock
private Player player;
/* IslandWorldManager */
@Mock
private IslandWorldManager iwm;
/* World */
@Mock
private World world;
/* Islands */
@Mock
private IslandsManager islandsManager;
@Mock
private Island island;
private PlayerDeathEvent e;
@Mock
private Location location;
/**
* @throws java.lang.Exception
*/
@Before
public void setUp() throws Exception {
// Set up plugin
BentoBox plugin = mock(BentoBox.class);
Whitebox.setInternalState(BentoBox.class, "instance", plugin);
/* Island World Manager */
when(plugin.getIWM()).thenReturn(iwm);
// User
User.setPlugin(plugin);
UUID uuid = UUID.randomUUID();
when(player.getUniqueId()).thenReturn(uuid);
when(player.getName()).thenReturn("tastybento");
when(player.getLocation()).thenReturn(location);
when(location.getWorld()).thenReturn(world);
when(location.toVector()).thenReturn(new Vector(1,2,3));
// Turn on why for player
when(player.getMetadata(eq("bskyblock_world_why_debug"))).thenReturn(Collections.singletonList(new FixedMetadataValue(plugin, true)));
when(player.getMetadata(eq("bskyblock_world_why_debug_issuer"))).thenReturn(Collections.singletonList(new FixedMetadataValue(plugin, uuid.toString())));
User.getInstance(player);
// WorldSettings and World Flags
WorldSettings ws = mock(WorldSettings.class);
when(iwm.getWorldSettings(any())).thenReturn(ws);
Map<String, Boolean> worldFlags = new HashMap<>();
when(ws.getWorldFlags()).thenReturn(worldFlags);
// World
when(world.getName()).thenReturn("bskyblock_world");
// By default everything is in world
when(iwm.inWorld(any(World.class))).thenReturn(true);
when(iwm.inWorld(any(Location.class))).thenReturn(true);
when(iwm.getAddon(any())).thenReturn(Optional.empty());
// Default not set
Flags.VISITOR_KEEP_INVENTORY.setSetting(world, false);
/* Islands */
when(plugin.getIslands()).thenReturn(islandsManager);
// Visitor
when(island.getMemberSet()).thenReturn(ImmutableSet.of());
// By default, there should be an island.
when(islandsManager.getProtectedIslandAt(any())).thenReturn(Optional.of(island));
// Util
PowerMockito.mockStatic(Util.class, Mockito.CALLS_REAL_METHODS);
when(Util.getWorld(any())).thenReturn(world);
// Default death event
List<ItemStack> drops = new ArrayList<>();
drops.add(new ItemStack(Material.ACACIA_BOAT));
e = new PlayerDeathEvent(player, drops, 100, 0, 0, 0, "Death message");
// Make new
l = new VisitorKeepInventoryListener();
}
/**
* @throws java.lang.Exception
*/
@After
public void tearDown() throws Exception {
User.clearUsers();
Mockito.framework().clearInlineMocks();
}
/**
* Test method for {@link world.bentobox.bentobox.listeners.flags.worldsettings.VisitorKeepInventoryListener#onVisitorDeath(org.bukkit.event.entity.PlayerDeathEvent)}.
*/
@Test
public void testOnVisitorDeath() {
l.onVisitorDeath(e);
}
/**
* Test method for {@link world.bentobox.bentobox.listeners.flags.worldsettings.VisitorKeepInventoryListener#onVisitorDeath(org.bukkit.event.entity.PlayerDeathEvent)}.
*/
@Test
public void testOnVisitorDeathFalseFlag() {
l.onVisitorDeath(e);
assertFalse(e.getKeepInventory());
assertFalse(e.getKeepLevel());
assertFalse(e.getDrops().isEmpty());
assertEquals(100, e.getDroppedExp());
// Why
verify(player).sendMessage("Why: PlayerDeathEvent in world bskyblock_world at 1,2,3");
verify(player).sendMessage("Why: tastybento VISITOR_KEEP_INVENTORY - SETTING_NOT_ALLOWED_IN_WORLD");
}
/**
* Test method for {@link world.bentobox.bentobox.listeners.flags.worldsettings.VisitorKeepInventoryListener#onVisitorDeath(org.bukkit.event.entity.PlayerDeathEvent)}.
*/
@Test
public void testOnVisitorDeathTrueFlag() {
Flags.VISITOR_KEEP_INVENTORY.setSetting(world, true);
l.onVisitorDeath(e);
assertTrue(e.getKeepInventory());
assertTrue(e.getKeepLevel());
assertTrue(e.getDrops().isEmpty());
assertEquals(0, e.getDroppedExp());
// Why
verify(player).sendMessage("Why: PlayerDeathEvent in world bskyblock_world at 1,2,3");
verify(player).sendMessage("Why: tastybento VISITOR_KEEP_INVENTORY - SETTING_ALLOWED_IN_WORLD");
}
/**
* Test method for {@link world.bentobox.bentobox.listeners.flags.worldsettings.VisitorKeepInventoryListener#onVisitorDeath(org.bukkit.event.entity.PlayerDeathEvent)}.
*/
@Test
public void testOnVisitorDeathNotInWorld() {
when(iwm.inWorld(eq(world))).thenReturn(false);
Flags.VISITOR_KEEP_INVENTORY.setSetting(world, true);
l.onVisitorDeath(e);
assertFalse(e.getKeepInventory());
assertFalse(e.getKeepLevel());
assertFalse(e.getDrops().isEmpty());
assertEquals(100, e.getDroppedExp());
// Why
verify(player).sendMessage("Why: PlayerDeathEvent in world bskyblock_world at 1,2,3");
verify(player).sendMessage("Why: tastybento VISITOR_KEEP_INVENTORY - SETTING_NOT_ALLOWED_IN_WORLD");
}
/**
* Test method for {@link world.bentobox.bentobox.listeners.flags.worldsettings.VisitorKeepInventoryListener#onVisitorDeath(org.bukkit.event.entity.PlayerDeathEvent)}.
*/
@Test
public void testOnVisitorDeathTrueFlagNoIsland() {
when(islandsManager.getProtectedIslandAt(any())).thenReturn(Optional.empty());
Flags.VISITOR_KEEP_INVENTORY.setSetting(world, true);
l.onVisitorDeath(e);
assertFalse(e.getKeepInventory());
assertFalse(e.getKeepLevel());
assertFalse(e.getDrops().isEmpty());
assertEquals(100, e.getDroppedExp());
// Why
verify(player, never()).sendMessage(anyString());
}
}