TF-EssentialsX/Essentials/test/com/earth2me/essentials/ToggleTest.java

174 lines
6 KiB
Java
Raw Normal View History

package com.earth2me.essentials;
import com.earth2me.essentials.commands.IEssentialsCommand;
import com.earth2me.essentials.commands.NoChargeException;
import junit.framework.TestCase;
import org.bukkit.World.Environment;
import org.bukkit.command.CommandSender;
import org.bukkit.plugin.InvalidDescriptionException;
2015-04-15 04:06:16 +00:00
import java.io.IOException;
public class ToggleTest extends TestCase {
private final OfflinePlayer base1;
private final Essentials ess;
private final FakeServer server;
2020-10-03 17:46:05 +00:00
public ToggleTest(final String testName) {
2015-04-15 04:06:16 +00:00
super(testName);
server = new FakeServer();
server.createWorld("testWorld", Environment.NORMAL);
ess = new Essentials(server);
try {
ess.setupForTesting(server);
2020-10-03 17:46:05 +00:00
} catch (final InvalidDescriptionException ex) {
2015-04-15 04:06:16 +00:00
fail("InvalidDescriptionException");
2020-10-03 17:46:05 +00:00
} catch (final IOException ex) {
2015-04-15 04:06:16 +00:00
fail("IOException");
}
base1 = server.createPlayer("testPlayer1");
server.addPlayer(base1);
ess.getUser(base1);
}
2020-10-03 17:46:05 +00:00
private void runCommand(final String command, final User user, final String[] args) throws Exception {
final IEssentialsCommand cmd;
2015-04-15 04:06:16 +00:00
try {
cmd = (IEssentialsCommand) Essentials.class.getClassLoader().loadClass("com.earth2me.essentials.commands.Command" + command).newInstance();
cmd.setEssentials(ess);
cmd.run(server, user, command, null, args);
2020-10-03 17:46:05 +00:00
} catch (final NoChargeException ignored) {
}
2015-04-15 04:06:16 +00:00
}
2020-10-03 17:46:05 +00:00
private void runConsoleCommand(final String command, final String[] args) throws Exception {
final IEssentialsCommand cmd;
2015-04-15 04:06:16 +00:00
2020-10-03 17:46:05 +00:00
final CommandSender sender = server.getConsoleSender();
2015-04-15 04:06:16 +00:00
try {
cmd = (IEssentialsCommand) Essentials.class.getClassLoader().loadClass("com.earth2me.essentials.commands.Command" + command).newInstance();
cmd.setEssentials(ess);
cmd.run(server, new CommandSource(sender), command, null, args);
2020-10-03 17:46:05 +00:00
} catch (final NoChargeException ignored) {
}
2015-04-15 04:06:16 +00:00
}
public void testFlyToggle() throws Exception {
2020-10-03 17:46:05 +00:00
final User user = ess.getUser(base1);
2015-04-15 04:06:16 +00:00
assertFalse(user.getBase().getAllowFlight());
2020-10-03 17:46:05 +00:00
runCommand("fly", user, new String[] {"on"});
2015-04-15 04:06:16 +00:00
assertTrue(user.getBase().getAllowFlight());
2020-10-03 17:46:05 +00:00
runCommand("fly", user, new String[] {"on"});
2015-04-15 04:06:16 +00:00
assertTrue(user.getBase().getAllowFlight());
2020-10-03 17:46:05 +00:00
runCommand("fly", user, new String[] {"off"});
2015-04-15 04:06:16 +00:00
assertFalse(user.getBase().getAllowFlight());
2020-10-03 17:46:05 +00:00
runCommand("fly", user, new String[] {"off"});
2015-04-15 04:06:16 +00:00
assertFalse(user.getBase().getAllowFlight());
2020-10-03 17:46:05 +00:00
runCommand("fly", user, new String[] {});
2015-04-15 04:06:16 +00:00
assertTrue(user.getBase().getAllowFlight());
2020-10-03 17:46:05 +00:00
runCommand("fly", user, new String[] {});
2015-04-15 04:06:16 +00:00
assertFalse(user.getBase().getAllowFlight());
}
public void testFlyDisOnToggle() throws Exception {
2020-10-03 17:46:05 +00:00
final User user = ess.getUser(base1);
2015-04-15 04:06:16 +00:00
user.getBase().setAllowFlight(true);
user.getBase().setFlying(true);
assertTrue(user.getBase().isFlying());
2020-10-03 17:46:05 +00:00
runCommand("fly", user, new String[] {});
2015-04-15 04:06:16 +00:00
assertFalse(user.getBase().getAllowFlight());
assertFalse(user.getBase().isFlying());
}
public void testGodToggle() throws Exception {
2020-10-03 17:46:05 +00:00
final User user = ess.getUser(base1);
2015-04-15 04:06:16 +00:00
assertFalse(user.isGodModeEnabled());
2020-10-03 17:46:05 +00:00
runCommand("god", user, new String[] {"on"});
2015-04-15 04:06:16 +00:00
assertTrue(user.isGodModeEnabled());
2020-10-03 17:46:05 +00:00
runCommand("god", user, new String[] {"on"});
2015-04-15 04:06:16 +00:00
assertTrue(user.isGodModeEnabled());
2020-10-03 17:46:05 +00:00
runCommand("god", user, new String[] {"off"});
2015-04-15 04:06:16 +00:00
assertFalse(user.isGodModeEnabled());
2020-10-03 17:46:05 +00:00
runCommand("god", user, new String[] {"off"});
2015-04-15 04:06:16 +00:00
assertFalse(user.isGodModeEnabled());
2020-10-03 17:46:05 +00:00
runCommand("god", user, new String[] {});
2015-04-15 04:06:16 +00:00
assertTrue(user.isGodModeEnabled());
2020-10-03 17:46:05 +00:00
runCommand("god", user, new String[] {});
2015-04-15 04:06:16 +00:00
assertFalse(user.isGodModeEnabled());
}
public void testConsoleToggle() throws Exception {
2020-10-03 17:46:05 +00:00
final User user = ess.getUser(base1);
2015-04-15 04:06:16 +00:00
assertFalse(user.getBase().getAllowFlight());
2020-10-03 17:46:05 +00:00
runConsoleCommand("fly", new String[] {base1.getName(), "on"});
2015-04-15 04:06:16 +00:00
assertTrue(user.getBase().getAllowFlight());
2020-10-03 17:46:05 +00:00
runConsoleCommand("fly", new String[] {base1.getName(), "on"});
2015-04-15 04:06:16 +00:00
assertTrue(user.getBase().getAllowFlight());
2020-10-03 17:46:05 +00:00
runConsoleCommand("fly", new String[] {base1.getName(), "off"});
2015-04-15 04:06:16 +00:00
assertFalse(user.getBase().getAllowFlight());
2020-10-03 17:46:05 +00:00
runConsoleCommand("fly", new String[] {base1.getName(), "off"});
2015-04-15 04:06:16 +00:00
assertFalse(user.getBase().getAllowFlight());
2020-10-03 17:46:05 +00:00
runConsoleCommand("fly", new String[] {base1.getName()});
2015-04-15 04:06:16 +00:00
assertTrue(user.getBase().getAllowFlight());
2020-10-03 17:46:05 +00:00
runConsoleCommand("fly", new String[] {base1.getName()});
2015-04-15 04:06:16 +00:00
assertFalse(user.getBase().getAllowFlight());
}
public void testAliasesToggle() throws Exception {
2020-10-03 17:46:05 +00:00
final User user = ess.getUser(base1);
2015-04-15 04:06:16 +00:00
assertFalse(user.getBase().getAllowFlight());
2020-10-03 17:46:05 +00:00
runConsoleCommand("fly", new String[] {base1.getName(), "enable"});
2015-04-15 04:06:16 +00:00
assertTrue(user.getBase().getAllowFlight());
2020-10-03 17:46:05 +00:00
runConsoleCommand("fly", new String[] {base1.getName(), "enable"});
2015-04-15 04:06:16 +00:00
assertTrue(user.getBase().getAllowFlight());
2020-10-03 17:46:05 +00:00
runConsoleCommand("fly", new String[] {base1.getName(), "disable"});
2015-04-15 04:06:16 +00:00
assertFalse(user.getBase().getAllowFlight());
2020-10-03 17:46:05 +00:00
runConsoleCommand("fly", new String[] {base1.getName(), "disable"});
2015-04-15 04:06:16 +00:00
assertFalse(user.getBase().getAllowFlight());
2020-10-03 17:46:05 +00:00
runConsoleCommand("fly", new String[] {base1.getName(), "1"});
2015-04-15 04:06:16 +00:00
assertTrue(user.getBase().getAllowFlight());
2020-10-03 17:46:05 +00:00
runConsoleCommand("fly", new String[] {base1.getName(), "1"});
2015-04-15 04:06:16 +00:00
assertTrue(user.getBase().getAllowFlight());
2020-10-03 17:46:05 +00:00
runConsoleCommand("fly", new String[] {base1.getName(), "0"});
2015-04-15 04:06:16 +00:00
assertFalse(user.getBase().getAllowFlight());
2020-10-03 17:46:05 +00:00
runConsoleCommand("fly", new String[] {base1.getName(), "0"});
2015-04-15 04:06:16 +00:00
assertFalse(user.getBase().getAllowFlight());
2015-04-15 04:06:16 +00:00
}
}