-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
QuitCommandTest.java
199 lines (175 loc) · 6.26 KB
/
QuitCommandTest.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
package world.bentobox.parkour.commands;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Optional;
import java.util.UUID;
import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.entity.Player;
import org.eclipse.jdt.annotation.NonNull;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.powermock.modules.junit4.PowerMockRunner;
import world.bentobox.bentobox.api.commands.CompositeCommand;
import world.bentobox.bentobox.api.user.User;
import world.bentobox.bentobox.managers.CommandsManager;
import world.bentobox.bentobox.managers.LocalesManager;
import world.bentobox.bentobox.managers.RanksManager;
import world.bentobox.parkour.AbstractParkourTest;
import world.bentobox.parkour.ParkourManager;
import world.bentobox.parkour.ParkourRunRecord;
import world.bentobox.parkour.Settings;
/**
* @author tastybento
*
*/
@RunWith(PowerMockRunner.class)
public class QuitCommandTest extends AbstractParkourTest {
@Mock
private CompositeCommand ac;
@Mock
private User user;
@Mock
private LocalesManager lm;
private UUID uuid;
@Mock
private World world;
@Mock
private ParkourManager parkourManager;
private QuitCommand cmd;
@Mock
private @NonNull Location location;
// No mock
private ParkourRunRecord prm;
/**
* @throws java.lang.Exception
*/
@Before
public void setUp() throws Exception {
super.setUp();
// Command manager
CommandsManager cm = mock(CommandsManager.class);
when(plugin.getCommandsManager()).thenReturn(cm);
// Player
Player p = mock(Player.class);
// Sometimes use Mockito.withSettings().verboseLogging()
when(user.isOp()).thenReturn(false);
when(user.getPermissionValue(anyString(), anyInt())).thenReturn(4);
when(user.getWorld()).thenReturn(world);
uuid = UUID.randomUUID();
when(user.getUniqueId()).thenReturn(uuid);
when(user.getPlayer()).thenReturn(p);
when(user.getName()).thenReturn("tastybento");
when(user.getLocation()).thenReturn(location);
when(user.getTranslation(any())).thenAnswer(invocation -> invocation.getArgument(0, String.class));
User.setPlugin(plugin);
// Parent command has no aliases
when(ac.getSubCommandAliases()).thenReturn(new HashMap<>());
when(ac.getWorld()).thenReturn(world);
when(ac.getAddon()).thenReturn(addon);
prm = new ParkourRunRecord(new HashMap<>(), new HashMap<>(), new ArrayList<>());
prm.timers().put(uuid, 20L);
when(addon.getParkourRunRecord()).thenReturn(prm);
// Islands
when(addon.getIslands()).thenReturn(im);
when(plugin.getIslands()).thenReturn(im);
when(im.getIsland(world, user)).thenReturn(island);
when(im.getIslandAt(location)).thenReturn(Optional.of(island));
when(im.hasIsland(world, user)).thenReturn(true);
when(im.inTeam(world, uuid)).thenReturn(true);
when(island.getRankCommand(anyString())).thenReturn(RanksManager.OWNER_RANK);
when(island.getRank(user)).thenReturn(RanksManager.MEMBER_RANK);
when(im.userIsOnIsland(any(), any())).thenReturn(true);
// Parkour Manager
// No warp spot
when(parkourManager.getWarpSpot(island)).thenReturn(Optional.empty());
when(addon.getParkourManager()).thenReturn(parkourManager);
// IWM
when(plugin.getIWM()).thenReturn(iwm);
when(iwm.getPermissionPrefix(any())).thenReturn("parkour.");
when(iwm.inWorld(world)).thenReturn(true);
// Settings
Settings settings = new Settings();
when(addon.getSettings()).thenReturn(settings);
// DUT
cmd = new QuitCommand(ac);
}
/**
* Test method for
* {@link world.bentobox.parkour.commands.QuitCommand#QuitCommand(world.bentobox.bentobox.api.commands.CompositeCommand)}.
*/
@Test
public void testQuitCommand() {
assertNotNull(cmd);
}
/**
* Test method for {@link world.bentobox.parkour.commands.QuitCommand#setup()}.
*/
@Test
public void testSetup() {
assertEquals("quit", cmd.getPermission());
assertEquals("parkour.commands.parkour.quit.description", cmd.getDescription());
assertTrue(cmd.isOnlyPlayer());
}
/**
* Test method for {@link world.bentobox.parkour.commands.QuitCommand#canExecute(world.bentobox.bentobox.api.user.User, java.lang.String, java.util.List)}.
*/
@Test
public void testCanExecuteWrongWorld() {
when(iwm.inWorld(world)).thenReturn(false);
assertFalse(cmd.canExecute(user, "", List.of()));
verify(user).sendMessage("general.errors.wrong-world");
}
/**
* Test method for {@link world.bentobox.parkour.commands.QuitCommand#canExecute(world.bentobox.bentobox.api.user.User, java.lang.String, java.util.List)}.
*/
@Test
public void testCanExecuteNotOnIsland() {
// Not on island
when(im.getIslandAt(location)).thenReturn(Optional.empty());
assertFalse(cmd.canExecute(user, "", List.of()));
verify(user).sendMessage("parkour.errors.not-on-island");
}
/**
* Test method for
* {@link world.bentobox.parkour.commands.QuitCommand#canExecute(world.bentobox.bentobox.api.user.User, java.lang.String, java.util.List)}.
*/
@Test
public void testCanExecuteNotInRun() {
prm.timers().clear();
assertFalse(cmd.canExecute(user, "", List.of()));
verify(user).sendMessage("parkour.errors.not-in-run");
}
/**
* Test method for
* {@link world.bentobox.parkour.commands.QuitCommand#canExecute(world.bentobox.bentobox.api.user.User, java.lang.String, java.util.List)}.
*/
@Test
public void testCanExecuteSuccess() {
assertTrue(cmd.canExecute(user, "", List.of()));
}
/**
* Test method for
* {@link world.bentobox.parkour.commands.QuitCommand#execute(world.bentobox.bentobox.api.user.User, java.lang.String, java.util.List)}.
*/
@Test
public void testExecuteUserStringListOfString() {
assertTrue(cmd.execute(user, "", List.of()));
verify(user).sendMessage("parkour.quit.success");
assertTrue(prm.timers().isEmpty());
assertTrue(prm.checkpoints().isEmpty());
}
}