Reformat with Google code style and enforce Checkstyle.

Fixes #125
This commit is contained in:
Andrew Steinborn
2018-10-27 23:45:35 -04:00
parent 53aa92db92
commit 25b5e00125
208 changed files with 12851 additions and 11620 deletions

View File

@@ -1,84 +1,84 @@
package com.velocitypowered.proxy.plugin.util;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import com.google.common.collect.ImmutableList;
import com.velocitypowered.api.plugin.PluginDescription;
import com.velocitypowered.api.plugin.meta.PluginDependency;
import com.velocitypowered.proxy.plugin.loader.VelocityPluginDescription;
import org.junit.jupiter.api.Test;
import java.util.ArrayList;
import java.util.List;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import org.junit.jupiter.api.Test;
class PluginDependencyUtilsTest {
private static final PluginDescription NO_DEPENDENCY_1_EXAMPLE = new VelocityPluginDescription(
"example", "tuxed", "0.1", null, null, ImmutableList.of("example2"),
ImmutableList.of(), null
);
private static final PluginDescription NO_DEPENDENCY_2_EXAMPLE = new VelocityPluginDescription(
"example2", "tuxed", "0.1", null, null, ImmutableList.of(), ImmutableList.of(), null
);
private static final PluginDescription NEVER_DEPENDED = new VelocityPluginDescription(
"and-again", "tuxed", "0.1", null, null, ImmutableList.of(), ImmutableList.of(), null
);
private static final PluginDescription SOFT_DEPENDENCY_EXISTS = new VelocityPluginDescription(
"soft", "tuxed", "0.1", null, null, ImmutableList.of(),
ImmutableList.of(new PluginDependency("example", "", true)), null
);
private static final PluginDescription SOFT_DEPENDENCY_DOES_NOT_EXIST = new VelocityPluginDescription(
"fluffy", "tuxed", "0.1", null, null, ImmutableList.of(),
ImmutableList.of(new PluginDependency("i-dont-exist", "", false)), null
);
private static final PluginDescription MULTI_DEPENDENCY = new VelocityPluginDescription(
"multi-depend", "tuxed", "0.1", null, null, ImmutableList.of(),
ImmutableList.of(
new PluginDependency("example", "", false),
new PluginDependency("example2", "", false)
), null
);
private static final PluginDescription CIRCULAR_DEPENDENCY_1 = new VelocityPluginDescription(
"circle", "tuxed", "0.1", null, null, ImmutableList.of(),
ImmutableList.of(
new PluginDependency("oval", "", false)
), null
);
private static final PluginDescription CIRCULAR_DEPENDENCY_2 = new VelocityPluginDescription(
"oval", "tuxed", "0.1", null, null, ImmutableList.of(),
ImmutableList.of(
new PluginDependency("circle", "", false)
), null
);
private static final PluginDescription NO_DEPENDENCY_1_EXAMPLE = new VelocityPluginDescription(
"example", "tuxed", "0.1", null, null, ImmutableList.of("example2"),
ImmutableList.of(), null
);
private static final PluginDescription NO_DEPENDENCY_2_EXAMPLE = new VelocityPluginDescription(
"example2", "tuxed", "0.1", null, null, ImmutableList.of(), ImmutableList.of(), null
);
private static final PluginDescription NEVER_DEPENDED = new VelocityPluginDescription(
"and-again", "tuxed", "0.1", null, null, ImmutableList.of(), ImmutableList.of(), null
);
private static final PluginDescription SOFT_DEPENDENCY_EXISTS = new VelocityPluginDescription(
"soft", "tuxed", "0.1", null, null, ImmutableList.of(),
ImmutableList.of(new PluginDependency("example", "", true)), null
);
private static final PluginDescription SOFT_DEPENDENCY_DOES_NOT_EXIST = new VelocityPluginDescription(
"fluffy", "tuxed", "0.1", null, null, ImmutableList.of(),
ImmutableList.of(new PluginDependency("i-dont-exist", "", false)), null
);
private static final PluginDescription MULTI_DEPENDENCY = new VelocityPluginDescription(
"multi-depend", "tuxed", "0.1", null, null, ImmutableList.of(),
ImmutableList.of(
new PluginDependency("example", "", false),
new PluginDependency("example2", "", false)
), null
);
// Note: Kahn's algorithm is non-unique in its return result, although the topological sort will have the correct
// order.
private static final List<PluginDescription> EXPECTED = ImmutableList.of(
NO_DEPENDENCY_1_EXAMPLE,
NO_DEPENDENCY_2_EXAMPLE,
NEVER_DEPENDED,
SOFT_DEPENDENCY_DOES_NOT_EXIST,
SOFT_DEPENDENCY_EXISTS,
MULTI_DEPENDENCY
);
private static final PluginDescription CIRCULAR_DEPENDENCY_1 = new VelocityPluginDescription(
"circle", "tuxed", "0.1", null, null, ImmutableList.of(),
ImmutableList.of(
new PluginDependency("oval", "", false)
), null
);
private static final PluginDescription CIRCULAR_DEPENDENCY_2 = new VelocityPluginDescription(
"oval", "tuxed", "0.1", null, null, ImmutableList.of(),
ImmutableList.of(
new PluginDependency("circle", "", false)
), null
);
@Test
void sortCandidates() throws Exception {
List<PluginDescription> descriptionList = new ArrayList<>();
descriptionList.add(NO_DEPENDENCY_1_EXAMPLE);
descriptionList.add(NO_DEPENDENCY_2_EXAMPLE);
descriptionList.add(NEVER_DEPENDED);
descriptionList.add(SOFT_DEPENDENCY_DOES_NOT_EXIST);
descriptionList.add(SOFT_DEPENDENCY_EXISTS);
descriptionList.add(MULTI_DEPENDENCY);
// Note: Kahn's algorithm is non-unique in its return result, although the topological sort will have the correct
// order.
private static final List<PluginDescription> EXPECTED = ImmutableList.of(
NO_DEPENDENCY_1_EXAMPLE,
NO_DEPENDENCY_2_EXAMPLE,
NEVER_DEPENDED,
SOFT_DEPENDENCY_DOES_NOT_EXIST,
SOFT_DEPENDENCY_EXISTS,
MULTI_DEPENDENCY
);
assertEquals(EXPECTED, PluginDependencyUtils.sortCandidates(descriptionList));
}
@Test
void sortCandidates() throws Exception {
List<PluginDescription> descriptionList = new ArrayList<>();
descriptionList.add(NO_DEPENDENCY_1_EXAMPLE);
descriptionList.add(NO_DEPENDENCY_2_EXAMPLE);
descriptionList.add(NEVER_DEPENDED);
descriptionList.add(SOFT_DEPENDENCY_DOES_NOT_EXIST);
descriptionList.add(SOFT_DEPENDENCY_EXISTS);
descriptionList.add(MULTI_DEPENDENCY);
@Test
void sortCandidatesCircularDependency() throws Exception {
List<PluginDescription> descs = ImmutableList.of(CIRCULAR_DEPENDENCY_1, CIRCULAR_DEPENDENCY_2);
assertThrows(IllegalStateException.class, () -> PluginDependencyUtils.sortCandidates(descs));
}
assertEquals(EXPECTED, PluginDependencyUtils.sortCandidates(descriptionList));
}
@Test
void sortCandidatesCircularDependency() throws Exception {
List<PluginDescription> descs = ImmutableList.of(CIRCULAR_DEPENDENCY_1, CIRCULAR_DEPENDENCY_2);
assertThrows(IllegalStateException.class, () -> PluginDependencyUtils.sortCandidates(descs));
}
}

View File

@@ -1,51 +1,68 @@
package com.velocitypowered.proxy.protocol;
import static com.velocitypowered.proxy.protocol.ProtocolConstants.MINECRAFT_1_12;
import static com.velocitypowered.proxy.protocol.ProtocolConstants.MINECRAFT_1_12_1;
import static com.velocitypowered.proxy.protocol.ProtocolConstants.MINECRAFT_1_12_2;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import com.velocitypowered.proxy.protocol.packet.Handshake;
import org.junit.jupiter.api.Test;
import static com.velocitypowered.proxy.protocol.ProtocolConstants.*;
import static org.junit.jupiter.api.Assertions.*;
class PacketRegistryTest {
private StateRegistry.PacketRegistry setupRegistry() {
StateRegistry.PacketRegistry registry = new StateRegistry.PacketRegistry(ProtocolConstants.Direction.CLIENTBOUND);
registry.register(Handshake.class, Handshake::new, new StateRegistry.PacketMapping(0x00, MINECRAFT_1_12, false));
return registry;
}
@Test
void packetRegistryWorks() {
StateRegistry.PacketRegistry registry = setupRegistry();
MinecraftPacket packet = registry.getVersion(MINECRAFT_1_12).createPacket(0);
assertNotNull(packet, "Packet was not found in registry");
assertEquals(Handshake.class, packet.getClass(), "Registry returned wrong class");
private StateRegistry.PacketRegistry setupRegistry() {
StateRegistry.PacketRegistry registry = new StateRegistry.PacketRegistry(
ProtocolConstants.Direction.CLIENTBOUND);
registry.register(Handshake.class, Handshake::new,
new StateRegistry.PacketMapping(0x00, MINECRAFT_1_12, false));
return registry;
}
assertEquals(0, registry.getVersion(MINECRAFT_1_12).getPacketId(packet), "Registry did not return the correct packet ID");
}
@Test
void packetRegistryWorks() {
StateRegistry.PacketRegistry registry = setupRegistry();
MinecraftPacket packet = registry.getVersion(MINECRAFT_1_12).createPacket(0);
assertNotNull(packet, "Packet was not found in registry");
assertEquals(Handshake.class, packet.getClass(), "Registry returned wrong class");
@Test
void packetRegistryLinkingWorks() {
StateRegistry.PacketRegistry registry = setupRegistry();
MinecraftPacket packet = registry.getVersion(MINECRAFT_1_12_1).createPacket(0);
assertNotNull(packet, "Packet was not found in registry");
assertEquals(Handshake.class, packet.getClass(), "Registry returned wrong class");
assertEquals(0, registry.getVersion(MINECRAFT_1_12_1).getPacketId(packet), "Registry did not return the correct packet ID");
}
assertEquals(0, registry.getVersion(MINECRAFT_1_12).getPacketId(packet),
"Registry did not return the correct packet ID");
}
@Test
void failOnNoMappings() {
StateRegistry.PacketRegistry registry = new StateRegistry.PacketRegistry(ProtocolConstants.Direction.CLIENTBOUND);
assertThrows(IllegalArgumentException.class, () -> registry.register(Handshake.class, Handshake::new));
assertThrows(IllegalArgumentException.class, () -> registry.getVersion(0).getPacketId(new Handshake()));
}
@Test
void packetRegistryLinkingWorks() {
StateRegistry.PacketRegistry registry = setupRegistry();
MinecraftPacket packet = registry.getVersion(MINECRAFT_1_12_1).createPacket(0);
assertNotNull(packet, "Packet was not found in registry");
assertEquals(Handshake.class, packet.getClass(), "Registry returned wrong class");
assertEquals(0, registry.getVersion(MINECRAFT_1_12_1).getPacketId(packet),
"Registry did not return the correct packet ID");
}
@Test
void registrySuppliesCorrectPacketsByProtocol() {
StateRegistry.PacketRegistry registry = new StateRegistry.PacketRegistry(ProtocolConstants.Direction.CLIENTBOUND);
registry.register(Handshake.class, Handshake::new, new StateRegistry.PacketMapping(0x00, MINECRAFT_1_12, false),
new StateRegistry.PacketMapping(0x01, MINECRAFT_1_12_1, false));
assertEquals(Handshake.class, registry.getVersion(MINECRAFT_1_12).createPacket(0x00).getClass());
assertEquals(Handshake.class, registry.getVersion(MINECRAFT_1_12_1).createPacket(0x01).getClass());
assertEquals(Handshake.class, registry.getVersion(MINECRAFT_1_12_2).createPacket(0x01).getClass());
}
@Test
void failOnNoMappings() {
StateRegistry.PacketRegistry registry = new StateRegistry.PacketRegistry(
ProtocolConstants.Direction.CLIENTBOUND);
assertThrows(IllegalArgumentException.class,
() -> registry.register(Handshake.class, Handshake::new));
assertThrows(IllegalArgumentException.class,
() -> registry.getVersion(0).getPacketId(new Handshake()));
}
@Test
void registrySuppliesCorrectPacketsByProtocol() {
StateRegistry.PacketRegistry registry = new StateRegistry.PacketRegistry(
ProtocolConstants.Direction.CLIENTBOUND);
registry.register(Handshake.class, Handshake::new,
new StateRegistry.PacketMapping(0x00, MINECRAFT_1_12, false),
new StateRegistry.PacketMapping(0x01, MINECRAFT_1_12_1, false));
assertEquals(Handshake.class,
registry.getVersion(MINECRAFT_1_12).createPacket(0x00).getClass());
assertEquals(Handshake.class,
registry.getVersion(MINECRAFT_1_12_1).createPacket(0x01).getClass());
assertEquals(Handshake.class,
registry.getVersion(MINECRAFT_1_12_2).createPacket(0x01).getClass());
}
}

View File

@@ -1,51 +1,51 @@
package com.velocitypowered.proxy.scheduler;
import static org.junit.jupiter.api.Assertions.assertEquals;
import com.velocitypowered.api.scheduler.ScheduledTask;
import com.velocitypowered.api.scheduler.TaskStatus;
import com.velocitypowered.proxy.testutil.FakePluginManager;
import org.junit.jupiter.api.Test;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import static org.junit.jupiter.api.Assertions.assertEquals;
import org.junit.jupiter.api.Test;
class VelocitySchedulerTest {
// TODO: The timings here will be inaccurate on slow systems.
// TODO: The timings here will be inaccurate on slow systems.
@Test
void buildTask() throws Exception {
VelocityScheduler scheduler = new VelocityScheduler(new FakePluginManager());
CountDownLatch latch = new CountDownLatch(1);
ScheduledTask task = scheduler.buildTask(FakePluginManager.PLUGIN_A, latch::countDown).schedule();
latch.await();
assertEquals(TaskStatus.FINISHED, task.status());
}
@Test
void buildTask() throws Exception {
VelocityScheduler scheduler = new VelocityScheduler(new FakePluginManager());
CountDownLatch latch = new CountDownLatch(1);
ScheduledTask task = scheduler.buildTask(FakePluginManager.PLUGIN_A, latch::countDown)
.schedule();
latch.await();
assertEquals(TaskStatus.FINISHED, task.status());
}
@Test
void cancelWorks() throws Exception {
VelocityScheduler scheduler = new VelocityScheduler(new FakePluginManager());
AtomicInteger i = new AtomicInteger(3);
ScheduledTask task = scheduler.buildTask(FakePluginManager.PLUGIN_A, i::decrementAndGet)
.delay(100, TimeUnit.SECONDS)
.schedule();
task.cancel();
Thread.sleep(200);
assertEquals(3, i.get());
assertEquals(TaskStatus.CANCELLED, task.status());
}
@Test
void cancelWorks() throws Exception {
VelocityScheduler scheduler = new VelocityScheduler(new FakePluginManager());
AtomicInteger i = new AtomicInteger(3);
ScheduledTask task = scheduler.buildTask(FakePluginManager.PLUGIN_A, i::decrementAndGet)
.delay(100, TimeUnit.SECONDS)
.schedule();
task.cancel();
Thread.sleep(200);
assertEquals(3, i.get());
assertEquals(TaskStatus.CANCELLED, task.status());
}
@Test
void repeatTaskWorks() throws Exception {
VelocityScheduler scheduler = new VelocityScheduler(new FakePluginManager());
CountDownLatch latch = new CountDownLatch(3);
ScheduledTask task = scheduler.buildTask(FakePluginManager.PLUGIN_A, latch::countDown)
.delay(100, TimeUnit.MILLISECONDS)
.repeat(100, TimeUnit.MILLISECONDS)
.schedule();
latch.await();
task.cancel();
}
@Test
void repeatTaskWorks() throws Exception {
VelocityScheduler scheduler = new VelocityScheduler(new FakePluginManager());
CountDownLatch latch = new CountDownLatch(3);
ScheduledTask task = scheduler.buildTask(FakePluginManager.PLUGIN_A, latch::countDown)
.delay(100, TimeUnit.MILLISECONDS)
.repeat(100, TimeUnit.MILLISECONDS)
.schedule();
latch.await();
task.cancel();
}
}

View File

@@ -4,74 +4,75 @@ import com.google.common.collect.ImmutableList;
import com.velocitypowered.api.plugin.PluginContainer;
import com.velocitypowered.api.plugin.PluginDescription;
import com.velocitypowered.api.plugin.PluginManager;
import org.checkerframework.checker.nullness.qual.NonNull;
import java.nio.file.Path;
import java.util.Collection;
import java.util.Optional;
import org.checkerframework.checker.nullness.qual.NonNull;
public class FakePluginManager implements PluginManager {
public static final Object PLUGIN_A = new Object();
public static final Object PLUGIN_B = new Object();
public static final PluginContainer PC_A = new FakePluginContainer("a", PLUGIN_A);
public static final PluginContainer PC_B = new FakePluginContainer("b", PLUGIN_B);
public static final Object PLUGIN_A = new Object();
public static final Object PLUGIN_B = new Object();
@Override
public @NonNull Optional<PluginContainer> fromInstance(@NonNull Object instance) {
if (instance == PLUGIN_A) {
return Optional.of(PC_A);
} else if (instance == PLUGIN_B) {
return Optional.of(PC_B);
} else {
return Optional.empty();
}
public static final PluginContainer PC_A = new FakePluginContainer("a", PLUGIN_A);
public static final PluginContainer PC_B = new FakePluginContainer("b", PLUGIN_B);
@Override
public @NonNull Optional<PluginContainer> fromInstance(@NonNull Object instance) {
if (instance == PLUGIN_A) {
return Optional.of(PC_A);
} else if (instance == PLUGIN_B) {
return Optional.of(PC_B);
} else {
return Optional.empty();
}
}
@Override
public @NonNull Optional<PluginContainer> getPlugin(@NonNull String id) {
switch (id) {
case "a":
return Optional.of(PC_A);
case "b":
return Optional.of(PC_B);
default:
return Optional.empty();
}
}
@Override
public @NonNull Collection<PluginContainer> getPlugins() {
return ImmutableList.of(PC_A, PC_B);
}
@Override
public boolean isLoaded(@NonNull String id) {
return id.equals("a") || id.equals("b");
}
@Override
public void addToClasspath(@NonNull Object plugin, @NonNull Path path) {
throw new UnsupportedOperationException();
}
private static class FakePluginContainer implements PluginContainer {
private final String id;
private final Object instance;
private FakePluginContainer(String id, Object instance) {
this.id = id;
this.instance = instance;
}
@Override
public @NonNull Optional<PluginContainer> getPlugin(@NonNull String id) {
switch (id) {
case "a":
return Optional.of(PC_A);
case "b":
return Optional.of(PC_B);
default:
return Optional.empty();
}
public @NonNull PluginDescription getDescription() {
return () -> id;
}
@Override
public @NonNull Collection<PluginContainer> getPlugins() {
return ImmutableList.of(PC_A, PC_B);
}
@Override
public boolean isLoaded(@NonNull String id) {
return id.equals("a") || id.equals("b");
}
@Override
public void addToClasspath(@NonNull Object plugin, @NonNull Path path) {
throw new UnsupportedOperationException();
}
private static class FakePluginContainer implements PluginContainer {
private final String id;
private final Object instance;
private FakePluginContainer(String id, Object instance) {
this.id = id;
this.instance = instance;
}
@Override
public @NonNull PluginDescription getDescription() {
return () -> id;
}
@Override
public Optional<?> getInstance() {
return Optional.of(instance);
}
public Optional<?> getInstance() {
return Optional.of(instance);
}
}
}

View File

@@ -1,26 +1,26 @@
package com.velocitypowered.proxy.util;
import org.junit.jupiter.api.Test;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import static org.junit.jupiter.api.Assertions.assertEquals;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import org.junit.jupiter.api.Test;
class EncryptionUtilsTest {
@Test
void twosComplementHexdigest() throws Exception {
String notchHash = mojangLoginSha1("Notch");
assertEquals("4ed1f46bbe04bc756bcb17c0c7ce3e4632f06a48", notchHash);
String jebHash = mojangLoginSha1("jeb_");
assertEquals("-7c9d5b0044c130109a5d7b5fb5c317c02b4e28c1", jebHash);
}
@Test
void twosComplementHexdigest() throws Exception {
String notchHash = mojangLoginSha1("Notch");
assertEquals("4ed1f46bbe04bc756bcb17c0c7ce3e4632f06a48", notchHash);
private String mojangLoginSha1(String str) throws Exception {
MessageDigest digest = MessageDigest.getInstance("SHA-1");
digest.update(str.getBytes(StandardCharsets.UTF_8));
byte[] digested = digest.digest();
return EncryptionUtils.twosComplementHexdigest(digested);
}
String jebHash = mojangLoginSha1("jeb_");
assertEquals("-7c9d5b0044c130109a5d7b5fb5c317c02b4e28c1", jebHash);
}
private String mojangLoginSha1(String str) throws Exception {
MessageDigest digest = MessageDigest.getInstance("SHA-1");
digest.update(str.getBytes(StandardCharsets.UTF_8));
byte[] digested = digest.digest();
return EncryptionUtils.twosComplementHexdigest(digested);
}
}

View File

@@ -1,39 +1,38 @@
package com.velocitypowered.proxy.util;
import com.google.common.base.Ticker;
import org.junit.jupiter.api.Test;
import java.net.InetAddress;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicLong;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
import com.google.common.base.Ticker;
import java.net.InetAddress;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicLong;
import org.junit.jupiter.api.Test;
class RatelimiterTest {
@Test
void attemptZero() {
Ratelimiter noRatelimiter = new Ratelimiter(0);
assertTrue(noRatelimiter.attempt(InetAddress.getLoopbackAddress()));
assertTrue(noRatelimiter.attempt(InetAddress.getLoopbackAddress()));
}
@Test
void attemptZero() {
Ratelimiter noRatelimiter = new Ratelimiter(0);
assertTrue(noRatelimiter.attempt(InetAddress.getLoopbackAddress()));
assertTrue(noRatelimiter.attempt(InetAddress.getLoopbackAddress()));
}
@Test
void attemptOne() {
long base = System.nanoTime();
AtomicLong extra = new AtomicLong();
Ticker testTicker = new Ticker() {
@Override
public long read() {
return base + extra.get();
}
};
Ratelimiter ratelimiter = new Ratelimiter(1000, testTicker);
assertTrue(ratelimiter.attempt(InetAddress.getLoopbackAddress()));
assertFalse(ratelimiter.attempt(InetAddress.getLoopbackAddress()));
extra.addAndGet(TimeUnit.SECONDS.toNanos(2));
assertTrue(ratelimiter.attempt(InetAddress.getLoopbackAddress()));
}
@Test
void attemptOne() {
long base = System.nanoTime();
AtomicLong extra = new AtomicLong();
Ticker testTicker = new Ticker() {
@Override
public long read() {
return base + extra.get();
}
};
Ratelimiter ratelimiter = new Ratelimiter(1000, testTicker);
assertTrue(ratelimiter.attempt(InetAddress.getLoopbackAddress()));
assertFalse(ratelimiter.attempt(InetAddress.getLoopbackAddress()));
extra.addAndGet(TimeUnit.SECONDS.toNanos(2));
assertTrue(ratelimiter.attempt(InetAddress.getLoopbackAddress()));
}
}

View File

@@ -1,46 +1,47 @@
package com.velocitypowered.proxy.util;
import com.velocitypowered.api.proxy.server.RegisteredServer;
import com.velocitypowered.api.proxy.server.ServerInfo;
import com.velocitypowered.proxy.server.ServerMap;
import org.junit.jupiter.api.Test;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.util.Optional;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import com.velocitypowered.api.proxy.server.RegisteredServer;
import com.velocitypowered.api.proxy.server.ServerInfo;
import com.velocitypowered.proxy.server.ServerMap;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.util.Optional;
import org.junit.jupiter.api.Test;
class ServerMapTest {
private static final InetSocketAddress TEST_ADDRESS = new InetSocketAddress(InetAddress.getLoopbackAddress(), 25565);
@Test
void respectsCaseInsensitivity() {
ServerMap map = new ServerMap(null);
ServerInfo info = new ServerInfo("TestServer", TEST_ADDRESS);
RegisteredServer connection = map.register(info);
private static final InetSocketAddress TEST_ADDRESS = new InetSocketAddress(
InetAddress.getLoopbackAddress(), 25565);
assertEquals(Optional.of(connection), map.getServer("TestServer"));
assertEquals(Optional.of(connection), map.getServer("testserver"));
assertEquals(Optional.of(connection), map.getServer("TESTSERVER"));
}
@Test
void respectsCaseInsensitivity() {
ServerMap map = new ServerMap(null);
ServerInfo info = new ServerInfo("TestServer", TEST_ADDRESS);
RegisteredServer connection = map.register(info);
@Test
void rejectsRepeatedRegisterAttempts() {
ServerMap map = new ServerMap(null);
ServerInfo info = new ServerInfo("TestServer", TEST_ADDRESS);
map.register(info);
assertEquals(Optional.of(connection), map.getServer("TestServer"));
assertEquals(Optional.of(connection), map.getServer("testserver"));
assertEquals(Optional.of(connection), map.getServer("TESTSERVER"));
}
ServerInfo willReject = new ServerInfo("TESTSERVER", TEST_ADDRESS);
assertThrows(IllegalArgumentException.class, () -> map.register(willReject));
}
@Test
void rejectsRepeatedRegisterAttempts() {
ServerMap map = new ServerMap(null);
ServerInfo info = new ServerInfo("TestServer", TEST_ADDRESS);
map.register(info);
@Test
void allowsSameServerLaxRegistrationCheck() {
ServerMap map = new ServerMap(null);
ServerInfo info = new ServerInfo("TestServer", TEST_ADDRESS);
RegisteredServer connection = map.register(info);
assertEquals(connection, map.register(info));
}
ServerInfo willReject = new ServerInfo("TESTSERVER", TEST_ADDRESS);
assertThrows(IllegalArgumentException.class, () -> map.register(willReject));
}
@Test
void allowsSameServerLaxRegistrationCheck() {
ServerMap map = new ServerMap(null);
ServerInfo info = new ServerInfo("TestServer", TEST_ADDRESS);
RegisteredServer connection = map.register(info);
assertEquals(connection, map.register(info));
}
}