From dea7c215d79562b3890bcaec284d9e0e7cdc1970 Mon Sep 17 00:00:00 2001 From: Andrew Steinborn Date: Sun, 21 Apr 2019 02:52:30 -0400 Subject: [PATCH] Allow empty channel names (but not namespaces). --- .../messages/MinecraftChannelIdentifier.java | 4 +-- .../MinecraftChannelIdentifierTest.java | 27 +++++++++++++++++++ 2 files changed, 29 insertions(+), 2 deletions(-) create mode 100644 api/src/test/java/com/velocitypowered/api/proxy/messages/MinecraftChannelIdentifierTest.java diff --git a/api/src/main/java/com/velocitypowered/api/proxy/messages/MinecraftChannelIdentifier.java b/api/src/main/java/com/velocitypowered/api/proxy/messages/MinecraftChannelIdentifier.java index 580f321f5..b63b6b1ac 100644 --- a/api/src/main/java/com/velocitypowered/api/proxy/messages/MinecraftChannelIdentifier.java +++ b/api/src/main/java/com/velocitypowered/api/proxy/messages/MinecraftChannelIdentifier.java @@ -12,7 +12,7 @@ import org.checkerframework.checker.nullness.qual.Nullable; */ public final class MinecraftChannelIdentifier implements ChannelIdentifier { - private static final Pattern VALID_IDENTIFIER_REGEX = Pattern.compile("[a-z0-9\\-_]+"); + private static final Pattern VALID_IDENTIFIER_REGEX = Pattern.compile("[a-z0-9\\-_]*"); private final String namespace; private final String name; @@ -42,7 +42,7 @@ public final class MinecraftChannelIdentifier implements ChannelIdentifier { */ public static MinecraftChannelIdentifier create(String namespace, String name) { Preconditions.checkArgument(!Strings.isNullOrEmpty(namespace), "namespace is null or empty"); - Preconditions.checkArgument(!Strings.isNullOrEmpty(name), "namespace is null or empty"); + Preconditions.checkArgument(name != null, "namespace is null or empty"); Preconditions.checkArgument(VALID_IDENTIFIER_REGEX.matcher(namespace).matches(), "namespace is not valid"); Preconditions diff --git a/api/src/test/java/com/velocitypowered/api/proxy/messages/MinecraftChannelIdentifierTest.java b/api/src/test/java/com/velocitypowered/api/proxy/messages/MinecraftChannelIdentifierTest.java new file mode 100644 index 000000000..eba99b6f8 --- /dev/null +++ b/api/src/test/java/com/velocitypowered/api/proxy/messages/MinecraftChannelIdentifierTest.java @@ -0,0 +1,27 @@ +package com.velocitypowered.api.proxy.messages; + +import static org.junit.jupiter.api.Assertions.*; + +import org.junit.jupiter.api.Test; + +class MinecraftChannelIdentifierTest { + + @Test + void createAllowsValidNamespaces() { + MinecraftChannelIdentifier.create("minecraft", "brand"); + } + + @Test + void createAllowsEmptyName() { + MinecraftChannelIdentifier.create("minecraft", ""); + } + + @Test + void createDisallowsNull() { + assertAll( + () -> assertThrows(IllegalArgumentException.class, () -> MinecraftChannelIdentifier.create(null, "")), + () -> assertThrows(IllegalArgumentException.class, () -> MinecraftChannelIdentifier.create("", "")), + () -> assertThrows(IllegalArgumentException.class, () -> MinecraftChannelIdentifier.create("minecraft", null)) + ); + } +} \ No newline at end of file