Allow empty channel names (but not namespaces).

This commit is contained in:
Andrew Steinborn
2019-04-21 02:52:30 -04:00
parent 7a5d8d424a
commit dea7c215d7
2 changed files with 29 additions and 2 deletions

View File

@@ -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

View File

@@ -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))
);
}
}