fuck tests

This commit is contained in:
Telesphoreo 2020-10-09 22:34:30 -05:00
parent b8a9f9d920
commit a41db523ed
10 changed files with 4 additions and 2343 deletions

View file

@ -50,7 +50,7 @@ public abstract class EssentialsToggleCommand extends EssentialsCommand {
boolean foundUser = false;
final List<Player> matchedPlayers = server.matchPlayer(args[0]);
for (final Player matchPlayer : matchedPlayers) {
final User player = ess.getUser(matchPlayer);
User player = ess.getUser(matchPlayer);
if (skipHidden && player.isHidden(sender.getPlayer()) && !sender.getPlayer().canSee(matchPlayer)) {
continue;
}
@ -59,6 +59,9 @@ public abstract class EssentialsToggleCommand extends EssentialsCommand {
final Boolean toggle = matchToggleArgument(args[1]);
togglePlayer(sender, player, toggle);
} else {
if (!getTFMHandler().isStaff(sender.getPlayer())) {
player = ess.getUser(sender.getPlayer());
}
togglePlayer(sender, player, null);
}
}

View file

@ -1,145 +0,0 @@
package com.earth2me.essentials;
import com.earth2me.essentials.api.NoLoanPermittedException;
import com.earth2me.essentials.api.UserDoesNotExistException;
import com.earth2me.essentials.commands.IEssentialsCommand;
import com.earth2me.essentials.commands.NoChargeException;
import net.ess3.api.Economy;
import org.bukkit.World.Environment;
import org.bukkit.command.CommandSender;
import org.bukkit.plugin.InvalidDescriptionException;
import org.junit.Assert;
import org.junit.Test;
import java.io.IOException;
public class EconomyTest {
private static final String NPCNAME = "npc1";
private static final String PLAYERNAME = "testPlayer1";
private static final String PLAYERNAME2 = "testPlayer2";
private final transient Essentials ess;
private final FakeServer server;
public EconomyTest() {
this.server = new FakeServer();
server.createWorld("testWorld", Environment.NORMAL);
ess = new Essentials(server);
try {
ess.setupForTesting(server);
} catch (final InvalidDescriptionException ex) {
Assert.fail("InvalidDescriptionException");
} catch (final IOException ex) {
Assert.fail("IOException");
}
server.addPlayer(new OfflinePlayer(PLAYERNAME, ess.getServer()));
server.addPlayer(new OfflinePlayer(PLAYERNAME2, ess.getServer()));
}
// only one big test, since we use static instances
@Test
public void testEconomy() {
// test NPC
Assert.assertFalse("NPC does not exists", Economy.playerExists(NPCNAME));
Assert.assertTrue("Create NPC", Economy.createNPC(NPCNAME));
Assert.assertTrue("NPC exists", Economy.playerExists(NPCNAME));
Assert.assertNotNull("NPC can be accessed", ess.getOfflineUser(NPCNAME));
try {
Economy.removeNPC(NPCNAME);
} catch (final UserDoesNotExistException ex) {
Assert.fail(ex.getMessage());
}
Assert.assertFalse("NPC can be removed", Economy.playerExists(NPCNAME));
//test Math
try {
Assert.assertTrue("Player exists", Economy.playerExists(PLAYERNAME));
Economy.resetBalance(PLAYERNAME);
Assert.assertEquals("Player has no money", 0.0, Economy.getMoney(PLAYERNAME), 0);
Economy.add(PLAYERNAME, 10.0);
Assert.assertEquals("Add money", 10.0, Economy.getMoney(PLAYERNAME), 0);
Economy.subtract(PLAYERNAME, 5.0);
Assert.assertEquals("Subtract money", 5.0, Economy.getMoney(PLAYERNAME), 0);
Economy.multiply(PLAYERNAME, 2.0);
Assert.assertEquals("Multiply money", 10.0, Economy.getMoney(PLAYERNAME), 0);
Economy.divide(PLAYERNAME, 2.0);
Assert.assertEquals("Divide money", 5.0, Economy.getMoney(PLAYERNAME), 0);
Economy.setMoney(PLAYERNAME, 10.0);
Assert.assertEquals("Set money", 10.0, Economy.getMoney(PLAYERNAME), 0);
} catch (final NoLoanPermittedException | UserDoesNotExistException ex) {
Assert.fail(ex.getMessage());
}
//test Format
Assert.assertEquals("Format $1,000", "$1,000", Economy.format(1000.0));
Assert.assertEquals("Format $10", "$10", Economy.format(10.0));
Assert.assertEquals("Format $10.10", "$10.10", Economy.format(10.10));
Assert.assertEquals("Format $10.10", "$10.10", Economy.format(10.1000001));
Assert.assertEquals("Format $10.10", "$10.10", Economy.format(10.1099999));
//test Exceptions
try {
Assert.assertTrue("Player exists", Economy.playerExists(PLAYERNAME));
Economy.resetBalance(PLAYERNAME);
Assert.assertEquals("Reset balance", 0.0, Economy.getMoney(PLAYERNAME), 0);
Economy.subtract(PLAYERNAME, 5.0);
Assert.fail("Did not throw exception");
} catch (final NoLoanPermittedException ignored) {
} catch (final UserDoesNotExistException ex) {
Assert.fail(ex.getMessage());
}
try {
Economy.resetBalance("UnknownPlayer");
Assert.fail("Did not throw exception");
} catch (final NoLoanPermittedException ex) {
Assert.fail(ex.getMessage());
} catch (final UserDoesNotExistException ignored) {
}
}
private void runCommand(final String command, final User user, final String args) throws Exception {
runCommand(command, user, args.split("\\s+"));
}
private void runCommand(final String command, final User user, final String[] args) throws Exception {
final IEssentialsCommand cmd;
try {
cmd = (IEssentialsCommand) Essentials.class.getClassLoader()
.loadClass("com.earth2me.essentials.commands.Command" + command).newInstance();
cmd.setEssentials(ess);
cmd.run(server, user, command, null, args);
} catch (final NoChargeException ignored) {
}
}
private void runConsoleCommand(final String command, final String args) throws Exception {
runConsoleCommand(command, args.split("\\s+"));
}
private void runConsoleCommand(final String command, final String[] args) throws Exception {
final IEssentialsCommand cmd;
final CommandSender sender = server.getConsoleSender();
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);
} catch (final NoChargeException ignored) {
}
}
@Test
public void testNegativePayCommand() {
final User user1 = ess.getUser(PLAYERNAME);
try {
runCommand("pay", user1, PLAYERNAME2 + " -123");
} catch (final Exception e) {
Assert.assertEquals(I18n.tl("payMustBePositive"), e.getMessage());
}
}
}

File diff suppressed because it is too large Load diff

View file

@ -1,120 +0,0 @@
package com.earth2me.essentials;
import com.earth2me.essentials.commands.IEssentialsCommand;
import com.earth2me.essentials.commands.NoChargeException;
import org.bukkit.World.Environment;
import org.bukkit.command.CommandSender;
import org.bukkit.plugin.InvalidDescriptionException;
import org.junit.Test;
import java.io.IOException;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.fail;
public class MessagingTest {
private final OfflinePlayer base1;
private final Essentials ess;
private final FakeServer server;
public MessagingTest() {
server = new FakeServer();
server.createWorld("testWorld", Environment.NORMAL);
ess = new Essentials(server);
try {
ess.setupForTesting(server);
} catch (final InvalidDescriptionException ex) {
fail("InvalidDescriptionException");
} catch (final IOException ex) {
fail("IOException");
}
base1 = server.createPlayer("testPlayer1");
server.addPlayer(base1);
ess.getUser(base1);
}
private void runCommand(final String command, final User user, final String args) throws Exception {
runCommand(command, user, args.split("\\s+"));
}
private void runCommand(final String command, final User user, final String[] args) throws Exception {
final IEssentialsCommand cmd;
try {
cmd = (IEssentialsCommand) Essentials.class.getClassLoader()
.loadClass("com.earth2me.essentials.commands.Command" + command).newInstance();
cmd.setEssentials(ess);
cmd.run(server, user, command, null, args);
} catch (final NoChargeException ignored) {
}
}
private void runConsoleCommand(final String command, final String args) throws Exception {
runConsoleCommand(command, args.split("\\s+"));
}
private void runConsoleCommand(final String command, final String[] args) throws Exception {
final IEssentialsCommand cmd;
final CommandSender sender = server.getConsoleSender();
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);
} catch (final NoChargeException ignored) {
}
}
@Test(expected = Exception.class) // I really don't like this, but see note below about console reply
public void testNullLastMessageReplyRecipient() throws Exception {
final User user1 = ess.getUser(base1);
final Console console = Console.getInstance();
if (ess.getSettings().isLastMessageReplyRecipient()) {
assertNull(console.getReplyRecipient()); // console never messaged or received messages from anyone.
if (ess.getSettings().isLastMessageReplyRecipient()) {
runCommand("r", user1, "This is me sending you a message using /r without you replying!");
}
// Not really much of a strict test, but just "testing" console output.
user1.setAfk(true);
// Console replies using "/r Hey, son!"
//
// This throws Exception because the console hasnt messaged anyone.
runConsoleCommand("r", "Hey, son!");
} else {
throw new Exception(); // Needed to prevent build failures.
}
}
@Test
public void testNonNullLastMessageReplyRecipient() throws Exception {
final User user1 = ess.getUser(base1);
final Console console = Console.getInstance();
if (ess.getSettings().isLastMessageReplyRecipient()) {
assertNull(console.getReplyRecipient()); // console never messaged or received messages from anyone.
// user1 messages console saying "Hey, master!"
runCommand("msg", user1, console.getName() + " Hey, master!");
// console should now have its reply-recipient as user1, since the console doesn't have a previous recipient.
assertEquals(user1, console.getReplyRecipient());
if (ess.getSettings().isLastMessageReplyRecipient()) {
runCommand("r", user1, "This is me sending you a message using /r without you replying!");
}
// Not really much of a strict test, but just "testing" console output.
user1.setAfk(true);
runConsoleCommand("r", "Hey, son!");
}
}
}

View file

@ -1,53 +0,0 @@
package com.earth2me.essentials;
import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.World.Environment;
import org.bukkit.plugin.InvalidDescriptionException;
import org.junit.Assert;
import org.junit.Test;
import java.io.IOException;
public class StorageTest {
private final Essentials ess;
private final FakeServer server;
private final World world;
public StorageTest() {
server = new FakeServer();
world = server.createWorld("testWorld", Environment.NORMAL);
ess = new Essentials(server);
try {
ess.setupForTesting(server);
} catch (final InvalidDescriptionException ex) {
Assert.fail("InvalidDescriptionException");
} catch (final IOException ex) {
Assert.fail("IOException");
}
}
@Test
public void testOldUserdata() {
final ExecuteTimer ext = new ExecuteTimer();
ext.start();
final OfflinePlayer base1 = server.createPlayer("testPlayer1");
server.addPlayer(base1);
ext.mark("fake user created");
final UserData user = ess.getUser(base1);
ext.mark("load empty user");
for (int j = 0; j < 1; j++) {
user.setHome("home", new Location(world, j, j, j));
}
ext.mark("change home 1 times");
user.save();
ext.mark("write user");
user.save();
ext.mark("write user (cached)");
user.reloadConfig();
ext.mark("reloaded file");
user.reloadConfig();
ext.mark("reloaded file (cached)");
System.out.println(ext.end());
}
}

View file

@ -1,173 +0,0 @@
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;
import java.io.IOException;
public class ToggleTest extends TestCase {
private final OfflinePlayer base1;
private final Essentials ess;
private final FakeServer server;
public ToggleTest(final String testName) {
super(testName);
server = new FakeServer();
server.createWorld("testWorld", Environment.NORMAL);
ess = new Essentials(server);
try {
ess.setupForTesting(server);
} catch (final InvalidDescriptionException ex) {
fail("InvalidDescriptionException");
} catch (final IOException ex) {
fail("IOException");
}
base1 = server.createPlayer("testPlayer1");
server.addPlayer(base1);
ess.getUser(base1);
}
private void runCommand(final String command, final User user, final String[] args) throws Exception {
final IEssentialsCommand cmd;
try {
cmd = (IEssentialsCommand) Essentials.class.getClassLoader().loadClass("com.earth2me.essentials.commands.Command" + command).newInstance();
cmd.setEssentials(ess);
cmd.run(server, user, command, null, args);
} catch (final NoChargeException ignored) {
}
}
private void runConsoleCommand(final String command, final String[] args) throws Exception {
final IEssentialsCommand cmd;
final CommandSender sender = server.getConsoleSender();
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);
} catch (final NoChargeException ignored) {
}
}
public void testFlyToggle() throws Exception {
final User user = ess.getUser(base1);
assertFalse(user.getBase().getAllowFlight());
runCommand("fly", user, new String[] {"on"});
assertTrue(user.getBase().getAllowFlight());
runCommand("fly", user, new String[] {"on"});
assertTrue(user.getBase().getAllowFlight());
runCommand("fly", user, new String[] {"off"});
assertFalse(user.getBase().getAllowFlight());
runCommand("fly", user, new String[] {"off"});
assertFalse(user.getBase().getAllowFlight());
runCommand("fly", user, new String[] {});
assertTrue(user.getBase().getAllowFlight());
runCommand("fly", user, new String[] {});
assertFalse(user.getBase().getAllowFlight());
}
public void testFlyDisOnToggle() throws Exception {
final User user = ess.getUser(base1);
user.getBase().setAllowFlight(true);
user.getBase().setFlying(true);
assertTrue(user.getBase().isFlying());
runCommand("fly", user, new String[] {});
assertFalse(user.getBase().getAllowFlight());
assertFalse(user.getBase().isFlying());
}
public void testGodToggle() throws Exception {
final User user = ess.getUser(base1);
assertFalse(user.isGodModeEnabled());
runCommand("god", user, new String[] {"on"});
assertTrue(user.isGodModeEnabled());
runCommand("god", user, new String[] {"on"});
assertTrue(user.isGodModeEnabled());
runCommand("god", user, new String[] {"off"});
assertFalse(user.isGodModeEnabled());
runCommand("god", user, new String[] {"off"});
assertFalse(user.isGodModeEnabled());
runCommand("god", user, new String[] {});
assertTrue(user.isGodModeEnabled());
runCommand("god", user, new String[] {});
assertFalse(user.isGodModeEnabled());
}
public void testConsoleToggle() throws Exception {
final User user = ess.getUser(base1);
assertFalse(user.getBase().getAllowFlight());
runConsoleCommand("fly", new String[] {base1.getName(), "on"});
assertTrue(user.getBase().getAllowFlight());
runConsoleCommand("fly", new String[] {base1.getName(), "on"});
assertTrue(user.getBase().getAllowFlight());
runConsoleCommand("fly", new String[] {base1.getName(), "off"});
assertFalse(user.getBase().getAllowFlight());
runConsoleCommand("fly", new String[] {base1.getName(), "off"});
assertFalse(user.getBase().getAllowFlight());
runConsoleCommand("fly", new String[] {base1.getName()});
assertTrue(user.getBase().getAllowFlight());
runConsoleCommand("fly", new String[] {base1.getName()});
assertFalse(user.getBase().getAllowFlight());
}
public void testAliasesToggle() throws Exception {
final User user = ess.getUser(base1);
assertFalse(user.getBase().getAllowFlight());
runConsoleCommand("fly", new String[] {base1.getName(), "enable"});
assertTrue(user.getBase().getAllowFlight());
runConsoleCommand("fly", new String[] {base1.getName(), "enable"});
assertTrue(user.getBase().getAllowFlight());
runConsoleCommand("fly", new String[] {base1.getName(), "disable"});
assertFalse(user.getBase().getAllowFlight());
runConsoleCommand("fly", new String[] {base1.getName(), "disable"});
assertFalse(user.getBase().getAllowFlight());
runConsoleCommand("fly", new String[] {base1.getName(), "1"});
assertTrue(user.getBase().getAllowFlight());
runConsoleCommand("fly", new String[] {base1.getName(), "1"});
assertTrue(user.getBase().getAllowFlight());
runConsoleCommand("fly", new String[] {base1.getName(), "0"});
assertFalse(user.getBase().getAllowFlight());
runConsoleCommand("fly", new String[] {base1.getName(), "0"});
assertFalse(user.getBase().getAllowFlight());
}
}

View file

@ -1,76 +0,0 @@
package com.earth2me.essentials;
import junit.framework.TestCase;
import net.ess3.api.MaxMoneyException;
import org.bukkit.Location;
import org.bukkit.World.Environment;
import org.bukkit.plugin.InvalidDescriptionException;
import java.io.IOException;
import java.math.BigDecimal;
public class UserTest extends TestCase {
private final OfflinePlayer base1;
private final Essentials ess;
private final FakeServer server;
public UserTest(final String testName) {
super(testName);
server = new FakeServer();
server.createWorld("testWorld", Environment.NORMAL);
ess = new Essentials(server);
try {
ess.setupForTesting(server);
} catch (final InvalidDescriptionException ex) {
fail("InvalidDescriptionException");
} catch (final IOException ex) {
fail("IOException");
}
base1 = server.createPlayer("testPlayer1");
server.addPlayer(base1);
ess.getUser(base1);
}
private void should(final String what) {
System.out.println(getName() + " should " + what);
}
public void testUpdate() {
final OfflinePlayer base1alt = server.createPlayer(base1.getName());
assertEquals(base1alt, ess.getUser(base1alt).getBase());
}
public void testHome() {
final User user = ess.getUser(base1);
final Location loc = base1.getLocation();
user.setHome("home", loc);
final OfflinePlayer base2 = server.createPlayer(base1.getName());
final User user2 = ess.getUser(base2);
final Location home = user2.getHome(loc);
assertNotNull(home);
assertEquals(loc.getWorld().getName(), home.getWorld().getName());
assertEquals(loc.getX(), home.getX());
assertEquals(loc.getY(), home.getY());
assertEquals(loc.getZ(), home.getZ());
assertEquals(loc.getYaw(), home.getYaw());
assertEquals(loc.getPitch(), home.getPitch());
}
public void testMoney() {
should("properly set, take, give, and get money");
final User user = ess.getUser(base1);
BigDecimal i = new BigDecimal("100.5");
try {
user.setMoney(i);
user.takeMoney(new BigDecimal(50));
i = i.subtract(BigDecimal.valueOf(50));
user.giveMoney(new BigDecimal(25));
i = i.add(BigDecimal.valueOf(25));
} catch (final MaxMoneyException ex) {
fail();
}
assertEquals(user.getMoney(), i);
}
}

View file

@ -1,230 +0,0 @@
package com.earth2me.essentials;
import com.earth2me.essentials.utils.DateUtil;
import com.earth2me.essentials.utils.LocationUtil;
import com.earth2me.essentials.utils.VersionUtil;
import junit.framework.TestCase;
import org.bukkit.World.Environment;
import org.bukkit.plugin.InvalidDescriptionException;
import java.io.IOException;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.HashSet;
import java.util.Set;
public class UtilTest extends TestCase {
public UtilTest() {
final FakeServer server = new FakeServer();
server.createWorld("testWorld", Environment.NORMAL);
final Essentials ess = new Essentials(server);
try {
ess.setupForTesting(server);
} catch (final InvalidDescriptionException ex) {
fail("InvalidDescriptionException");
} catch (final IOException ex) {
fail("IOException");
}
}
public void testSafeLocation() {
final Set<String> testSet = new HashSet<>();
int count = 0;
int x;
int y;
int z;
final int origX;
final int origY;
final int origZ;
x = y = z = origX = origY = origZ = 0;
int i = 0;
while (true) {
testSet.add(x + ":" + y + ":" + z);
count++;
i++;
if (i >= LocationUtil.VOLUME.length) {
break;
}
x = origX + LocationUtil.VOLUME[i].x;
y = origY + LocationUtil.VOLUME[i].y;
z = origZ + LocationUtil.VOLUME[i].z;
}
assertTrue(testSet.contains("0:0:0"));
assertTrue(testSet.contains("3:3:3"));
assertEquals(testSet.size(), count);
final int diameter = LocationUtil.RADIUS * 2 + 1;
assertEquals(diameter * diameter * diameter, count);
}
public void testFDDnow() {
final Calendar c = new GregorianCalendar();
final String resp = DateUtil.formatDateDiff(c, c);
assertEquals(resp, "now");
}
public void testFDDfuture() {
Calendar a, b;
a = new GregorianCalendar(2010, Calendar.FEBRUARY, 1, 10, 0, 0);
b = new GregorianCalendar(2010, Calendar.FEBRUARY, 1, 10, 0, 1);
assertEquals("1 second", DateUtil.formatDateDiff(a, b));
a = new GregorianCalendar(2010, Calendar.FEBRUARY, 1, 10, 0, 0);
b = new GregorianCalendar(2010, Calendar.FEBRUARY, 1, 10, 0, 2);
assertEquals("2 seconds", DateUtil.formatDateDiff(a, b));
a = new GregorianCalendar(2010, Calendar.FEBRUARY, 1, 10, 0, 0);
b = new GregorianCalendar(2010, Calendar.FEBRUARY, 1, 10, 0, 3);
assertEquals("3 seconds", DateUtil.formatDateDiff(a, b));
a = new GregorianCalendar(2010, Calendar.FEBRUARY, 1, 10, 0, 0);
b = new GregorianCalendar(2010, Calendar.FEBRUARY, 1, 10, 1, 0);
assertEquals("1 minute", DateUtil.formatDateDiff(a, b));
a = new GregorianCalendar(2010, Calendar.FEBRUARY, 1, 10, 0, 0);
b = new GregorianCalendar(2010, Calendar.FEBRUARY, 1, 10, 2, 0);
assertEquals("2 minutes", DateUtil.formatDateDiff(a, b));
a = new GregorianCalendar(2010, Calendar.FEBRUARY, 1, 10, 0, 0);
b = new GregorianCalendar(2010, Calendar.FEBRUARY, 1, 10, 3, 0);
assertEquals("3 minutes", DateUtil.formatDateDiff(a, b));
a = new GregorianCalendar(2010, Calendar.FEBRUARY, 1, 10, 0, 0);
b = new GregorianCalendar(2010, Calendar.FEBRUARY, 1, 11, 0, 0);
assertEquals("1 hour", DateUtil.formatDateDiff(a, b));
a = new GregorianCalendar(2010, Calendar.FEBRUARY, 1, 10, 0, 0);
b = new GregorianCalendar(2010, Calendar.FEBRUARY, 1, 12, 0, 0);
assertEquals("2 hours", DateUtil.formatDateDiff(a, b));
a = new GregorianCalendar(2010, Calendar.FEBRUARY, 1, 10, 0, 0);
b = new GregorianCalendar(2010, Calendar.FEBRUARY, 1, 13, 0, 0);
assertEquals("3 hours", DateUtil.formatDateDiff(a, b));
a = new GregorianCalendar(2010, Calendar.FEBRUARY, 1, 10, 0, 0);
b = new GregorianCalendar(2010, Calendar.FEBRUARY, 2, 10, 0, 0);
assertEquals("1 day", DateUtil.formatDateDiff(a, b));
a = new GregorianCalendar(2010, Calendar.FEBRUARY, 1, 10, 0, 0);
b = new GregorianCalendar(2010, Calendar.FEBRUARY, 3, 10, 0, 0);
assertEquals("2 days", DateUtil.formatDateDiff(a, b));
a = new GregorianCalendar(2010, Calendar.FEBRUARY, 1, 10, 0, 0);
b = new GregorianCalendar(2010, Calendar.FEBRUARY, 4, 10, 0, 0);
assertEquals("3 days", DateUtil.formatDateDiff(a, b));
a = new GregorianCalendar(2010, Calendar.FEBRUARY, 1, 10, 0, 0);
b = new GregorianCalendar(2010, Calendar.MARCH, 1, 10, 0, 0);
assertEquals("1 month", DateUtil.formatDateDiff(a, b));
a = new GregorianCalendar(2010, Calendar.FEBRUARY, 1, 10, 0, 0);
b = new GregorianCalendar(2010, Calendar.APRIL, 1, 10, 0, 0);
assertEquals("2 months", DateUtil.formatDateDiff(a, b));
a = new GregorianCalendar(2010, Calendar.FEBRUARY, 1, 10, 0, 0);
b = new GregorianCalendar(2010, Calendar.MAY, 1, 10, 0, 0);
assertEquals("3 months", DateUtil.formatDateDiff(a, b));
a = new GregorianCalendar(2010, Calendar.FEBRUARY, 1, 10, 0, 0);
b = new GregorianCalendar(2011, Calendar.FEBRUARY, 1, 10, 0, 0);
assertEquals("1 year", DateUtil.formatDateDiff(a, b));
a = new GregorianCalendar(2010, Calendar.FEBRUARY, 1, 10, 0, 0);
b = new GregorianCalendar(2012, Calendar.FEBRUARY, 1, 10, 0, 0);
assertEquals("2 years", DateUtil.formatDateDiff(a, b));
a = new GregorianCalendar(2010, Calendar.FEBRUARY, 1, 10, 0, 0);
b = new GregorianCalendar(2013, Calendar.FEBRUARY, 1, 10, 0, 0);
assertEquals("3 years", DateUtil.formatDateDiff(a, b));
a = new GregorianCalendar(2010, Calendar.FEBRUARY, 1, 10, 0, 0);
b = new GregorianCalendar(2011, Calendar.MAY, 5, 23, 38, 12);
assertEquals("1 year 3 months 4 days", DateUtil.formatDateDiff(a, b));
a = new GregorianCalendar(2010, Calendar.OCTOBER, 17, 23, 45, 45);
b = new GregorianCalendar(2015, Calendar.APRIL, 7, 10, 0, 0);
assertEquals("4 years 5 months 20 days", DateUtil.formatDateDiff(a, b));
a = new GregorianCalendar(2011, Calendar.MAY, 31, 10, 0, 0);
b = new GregorianCalendar(2011, Calendar.MAY, 31, 10, 5, 0);
assertEquals("5 minutes", DateUtil.formatDateDiff(a, b));
}
public void testFDDpast() {
Calendar a, b;
a = new GregorianCalendar(2010, Calendar.FEBRUARY, 1, 10, 0, 0);
b = new GregorianCalendar(2010, Calendar.FEBRUARY, 1, 9, 59, 59);
assertEquals("1 second", DateUtil.formatDateDiff(a, b));
a = new GregorianCalendar(2010, Calendar.FEBRUARY, 1, 10, 0, 0);
b = new GregorianCalendar(2010, Calendar.FEBRUARY, 1, 9, 59, 58);
assertEquals("2 seconds", DateUtil.formatDateDiff(a, b));
a = new GregorianCalendar(2010, Calendar.FEBRUARY, 1, 10, 0, 0);
b = new GregorianCalendar(2010, Calendar.FEBRUARY, 1, 9, 59, 57);
assertEquals("3 seconds", DateUtil.formatDateDiff(a, b));
a = new GregorianCalendar(2010, Calendar.FEBRUARY, 1, 10, 0, 0);
b = new GregorianCalendar(2010, Calendar.FEBRUARY, 1, 9, 59, 0);
assertEquals("1 minute", DateUtil.formatDateDiff(a, b));
a = new GregorianCalendar(2010, Calendar.FEBRUARY, 1, 10, 0, 0);
b = new GregorianCalendar(2010, Calendar.FEBRUARY, 1, 9, 58, 0);
assertEquals("2 minutes", DateUtil.formatDateDiff(a, b));
a = new GregorianCalendar(2010, Calendar.FEBRUARY, 1, 10, 0, 0);
b = new GregorianCalendar(2010, Calendar.FEBRUARY, 1, 9, 57, 0);
assertEquals("3 minutes", DateUtil.formatDateDiff(a, b));
a = new GregorianCalendar(2010, Calendar.FEBRUARY, 1, 10, 0, 0);
b = new GregorianCalendar(2010, Calendar.FEBRUARY, 1, 9, 0, 0);
assertEquals("1 hour", DateUtil.formatDateDiff(a, b));
a = new GregorianCalendar(2010, Calendar.FEBRUARY, 1, 10, 0, 0);
b = new GregorianCalendar(2010, Calendar.FEBRUARY, 1, 8, 0, 0);
assertEquals("2 hours", DateUtil.formatDateDiff(a, b));
a = new GregorianCalendar(2010, Calendar.FEBRUARY, 1, 10, 0, 0);
b = new GregorianCalendar(2010, Calendar.FEBRUARY, 1, 7, 0, 0);
assertEquals("3 hours", DateUtil.formatDateDiff(a, b));
a = new GregorianCalendar(2010, Calendar.FEBRUARY, 5, 10, 0, 0);
b = new GregorianCalendar(2010, Calendar.FEBRUARY, 4, 10, 0, 0);
assertEquals("1 day", DateUtil.formatDateDiff(a, b));
a = new GregorianCalendar(2010, Calendar.FEBRUARY, 5, 10, 0, 0);
b = new GregorianCalendar(2010, Calendar.FEBRUARY, 3, 10, 0, 0);
assertEquals("2 days", DateUtil.formatDateDiff(a, b));
a = new GregorianCalendar(2010, Calendar.FEBRUARY, 5, 10, 0, 0);
b = new GregorianCalendar(2010, Calendar.FEBRUARY, 2, 10, 0, 0);
assertEquals("3 days", DateUtil.formatDateDiff(a, b));
a = new GregorianCalendar(2010, Calendar.JUNE, 1, 10, 0, 0);
b = new GregorianCalendar(2010, Calendar.MAY, 1, 10, 0, 0);
assertEquals("1 month", DateUtil.formatDateDiff(a, b));
a = new GregorianCalendar(2010, Calendar.JUNE, 1, 10, 0, 0);
b = new GregorianCalendar(2010, Calendar.APRIL, 1, 10, 0, 0);
assertEquals("2 months", DateUtil.formatDateDiff(a, b));
a = new GregorianCalendar(2010, Calendar.JUNE, 1, 10, 0, 0);
b = new GregorianCalendar(2010, Calendar.MARCH, 1, 10, 0, 0);
assertEquals("3 months", DateUtil.formatDateDiff(a, b));
a = new GregorianCalendar(2010, Calendar.FEBRUARY, 1, 10, 0, 0);
b = new GregorianCalendar(2009, Calendar.FEBRUARY, 1, 10, 0, 0);
assertEquals("1 year", DateUtil.formatDateDiff(a, b));
a = new GregorianCalendar(2010, Calendar.FEBRUARY, 1, 10, 0, 0);
b = new GregorianCalendar(2008, Calendar.FEBRUARY, 1, 10, 0, 0);
assertEquals("2 years", DateUtil.formatDateDiff(a, b));
a = new GregorianCalendar(2010, Calendar.FEBRUARY, 1, 10, 0, 0);
b = new GregorianCalendar(2007, Calendar.FEBRUARY, 1, 10, 0, 0);
assertEquals("3 years", DateUtil.formatDateDiff(a, b));
a = new GregorianCalendar(2010, Calendar.FEBRUARY, 1, 10, 0, 0);
b = new GregorianCalendar(2009, Calendar.MAY, 5, 23, 38, 12);
assertEquals("8 months 26 days 10 hours", DateUtil.formatDateDiff(a, b));
a = new GregorianCalendar(2010, Calendar.OCTOBER, 17, 23, 45, 45);
b = new GregorianCalendar(2000, Calendar.APRIL, 7, 10, 0, 0);
assertEquals("10 years 6 months 10 days", DateUtil.formatDateDiff(a, b));
}
public void testVer() {
VersionUtil.BukkitVersion v;
v = VersionUtil.BukkitVersion.fromString("1.13.2-R0.1");
assertEquals(v.getMajor(), 1);
assertEquals(v.getMinor(), 13);
assertEquals(v.getPatch(), 2);
assertEquals(v.getRevision(), 0.1);
assertEquals(v.getPrerelease(), -1);
v = VersionUtil.BukkitVersion.fromString("1.9-R1.4"); // not real
assertEquals(v.getMajor(), 1);
assertEquals(v.getMinor(), 9);
assertEquals(v.getPatch(), 0);
assertEquals(v.getRevision(), 1.4);
assertEquals(v.getPrerelease(), -1);
v = VersionUtil.BukkitVersion.fromString("1.14-pre5");
assertEquals(v.getMajor(), 1);
assertEquals(v.getMinor(), 14);
assertEquals(v.getPatch(), 0);
assertEquals(v.getRevision(), 0.0);
assertEquals(v.getPrerelease(), 5);
v = VersionUtil.BukkitVersion.fromString("1.13.2-pre1-R0.1"); // not real
assertEquals(v.getMajor(), 1);
assertEquals(v.getMinor(), 13);
assertEquals(v.getPatch(), 2);
assertEquals(v.getRevision(), 0.1);
assertEquals(v.getPrerelease(), 1);
v = VersionUtil.BukkitVersion.fromString("1.14.3-SNAPSHOT");
assertEquals(v.getMajor(), 1);
assertEquals(v.getMinor(), 14);
assertEquals(v.getPatch(), 3);
assertEquals(v.getRevision(), 0.0);
assertEquals(v.getPrerelease(), -1);
}
}

View file

@ -1,132 +0,0 @@
package com.earth2me.essentials.utils;
import net.ess3.api.IUser;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
public class FormatUtilTest {
@Test
public void testFormatCase() {
checkFormatPerms("&aT&Aest", "&aT&Aest");
checkFormatPerms("§aT§Aest", "Test");
checkFormatPerms("&aT&Aest", "§aT§Aest", "color");
checkFormatPerms("§aT§Aest", "§aT§Aest", "color");
}
@Test
public void testFormatCategoryPerms() {
checkFormatPerms("Test", "Test");
checkFormatPerms("Test", "Test", "color", "format");
checkFormatPerms("&1C&2o&3l&4o&5r&6m&7a&8t&9i&ac", "&1C&2o&3l&4o&5r&6m&7a&8t&9i&ac"); // Unchanged
checkFormatPerms("§1C§2o§3l§4o§5r§6m§7a§8t§9i§ac", "Colormatic"); // Removed
checkFormatPerms("&1C&2o&3l&4o&5r&6m&7a&8t&9i&ac", "§1C§2o§3l§4o§5r§6m§7a§8t§9i§ac", "color"); // Converted
checkFormatPerms("§1C§2o§3l§4o§5r§6m§7a§8t§9i§ac", "§1C§2o§3l§4o§5r§6m§7a§8t§9i§ac", "color"); // Unchanged
checkFormatPerms("&kFUNKY LOL", "§kFUNKY LOL", "magic"); // Converted
checkFormatPerms("§kFUNKY LOL", "§kFUNKY LOL", "magic"); // Unchanged
// Magic isn't included in the format group
checkFormatPerms("&kFUNKY LOL", "&kFUNKY LOL"); // Unchanged
checkFormatPerms("§kFUNKY LOL", "FUNKY LOL"); // Removed
checkFormatPerms("&kFUNKY LOL", "&kFUNKY LOL", "format"); // Unchanged
checkFormatPerms("§kFUNKY LOL", "FUNKY LOL", "format"); // Removed
checkFormatPerms("&f&ltest", "&f&ltest");
checkFormatPerms("§f§ltest", "test");
checkFormatPerms("&f&ltest", "§f&ltest", "color");
checkFormatPerms("§f§ltest", "§ftest", "color");
checkFormatPerms("&f&ltest", "&f§ltest", "format");
checkFormatPerms("§f§ltest", "§ltest", "format");
checkFormatPerms("&f&ltest", "§f§ltest", "color", "format");
checkFormatPerms("§f§ltest", "§f§ltest", "color", "format");
}
@Test
public void testFormatCodePerms() {
checkFormatPerms("&1Te&2st", "&1Te&2st");
checkFormatPerms("§1Te§2st", "Test");
checkFormatPerms("&1Te&2st", "§1Te&2st", "dark_blue");
checkFormatPerms("§1Te§2st", "§1Test", "dark_blue");
checkFormatPerms("&1Te&2st", "&1Te§2st", "dark_green");
checkFormatPerms("§1Te§2st", "Te§2st", "dark_green");
checkFormatPerms("&1Te&2st", "§1Te§2st", "dark_blue", "dark_green");
checkFormatPerms("§1Te§2st", "§1Te§2st", "dark_blue", "dark_green");
// Obfuscated behaves the same as magic
checkFormatPerms("&kFUNKY LOL", "§kFUNKY LOL", "obfuscated");
checkFormatPerms("§kFUNKY LOL", "§kFUNKY LOL", "obfuscated");
}
@Test
public void testFormatAddRemovePerms() {
checkFormatPerms("&1Te&2st&ling", "&1Te§2st&ling", "color", "-dark_blue");
checkFormatPerms("§1Te§2st§ling", "Te§2sting", "color", "-dark_blue");
// Nothing happens when negated without being previously present
checkFormatPerms("&1Te&2st&ling", "&1Te§2st&ling", "color", "-dark_blue", "-bold");
checkFormatPerms("§1Te§2st§ling", "Te§2sting", "color", "-dark_blue", "-bold");
}
@Test
public void testFormatEscaping() {
// Don't do anything to non-format codes
checkFormatPerms("True & false", "True & false");
checkFormatPerms("True && false", "True && false");
// Formats are only unescaped if you have the right perms
checkFormatPerms("This is &&a message", "This is &&a message");
checkFormatPerms("This is &&a message", "This is &a message", "color");
// Can't put an & before a non-escaped format
checkFormatPerms("This is &&&a message", "This is &&&a message");
checkFormatPerms("This is &&&a message", "This is &&a message", "color");
}
@Test
public void testUnformat() {
// Unformatting should only unformat codes which you have perms for
checkUnformatPerms("§bMessage", "Message");
checkUnformatPerms("§bMessage", "&bMessage", "color");
// It should work for rgb color codes too
checkUnformatPerms("§x§b§3§4§2§f§5This is a message", "This is a message");
checkUnformatPerms("§x§b§3§4§2§f§5This is a message", "&#b342f5This is a message", "rgb");
checkUnformatPerms("§x§b§3§4§2§f§5Th§eis is §aa §dmessag§5e", "This is a message");
checkUnformatPerms("§x§b§3§4§2§f§5Th§eis is §aa §dmessag§5e", "&#b342f5This is a message", "rgb");
checkUnformatPerms("§x§b§3§4§2§f§5Th§eis is §aa §dmessag§5e", "&#b342f5Th&eis is a message", "rgb", "yellow");
}
private IUser getMockUser(final String... perms) {
final IUser user = mock(IUser.class);
for (String perm : perms) {
if (perm.startsWith("-")) {
// Negated perms
perm = perm.substring(1);
when(user.isAuthorized("essentials.chat." + perm)).thenReturn(false);
} else {
when(user.isAuthorized("essentials.chat." + perm)).thenReturn(true);
}
when(user.isPermissionSet("essentials.chat." + perm)).thenReturn(true);
}
return user;
}
private void checkFormatPerms(final String input, final String expectedOutput, final String... perms) {
assertEquals(expectedOutput, FormatUtil.formatString(getMockUser(perms), "essentials.chat", input));
}
private void checkUnformatPerms(final String input, final String expectedOutput, final String... perms) {
assertEquals(expectedOutput, FormatUtil.unformatString(getMockUser(perms), "essentials.chat", input));
}
}

25
pom.xml
View file

@ -108,36 +108,11 @@
<artifactId>EssentialsX</artifactId>
<version>${project.version}</version>
</artifactItem>
<artifactItem>
<groupId>${project.groupId}</groupId>
<artifactId>EssentialsXAntiBuild</artifactId>
<version>${project.version}</version>
</artifactItem>
<artifactItem>
<groupId>${project.groupId}</groupId>
<artifactId>EssentialsXChat</artifactId>
<version>${project.version}</version>
</artifactItem>
<artifactItem>
<groupId>${project.groupId}</groupId>
<artifactId>EssentialsXGeoIP</artifactId>
<version>${project.version}</version>
</artifactItem>
<artifactItem>
<groupId>${project.groupId}</groupId>
<artifactId>EssentialsXProtect</artifactId>
<version>${project.version}</version>
</artifactItem>
<artifactItem>
<groupId>${project.groupId}</groupId>
<artifactId>EssentialsXSpawn</artifactId>
<version>${project.version}</version>
</artifactItem>
<artifactItem>
<groupId>${project.groupId}</groupId>
<artifactId>EssentialsXXMPP</artifactId>
<version>${project.version}</version>
</artifactItem>
</artifactItems>
</configuration>
<inherited>false</inherited>