mirror of
https://github.com/PaperMC/Paper.git
synced 2025-08-14 11:45:52 -07:00
Clarify NamespacedKey error messages.
Add new tests for NamespacedKeys. By: Senmori <thesenmori@gmail.com>
This commit is contained in:
@@ -44,8 +44,8 @@ public final class NamespacedKey {
|
|||||||
*/
|
*/
|
||||||
@Deprecated
|
@Deprecated
|
||||||
public NamespacedKey(String namespace, String key) {
|
public NamespacedKey(String namespace, String key) {
|
||||||
Preconditions.checkArgument(namespace != null && VALID_NAMESPACE.matcher(namespace).matches(), "namespace");
|
Preconditions.checkArgument(namespace != null && VALID_NAMESPACE.matcher(namespace).matches(), "Invalid namespace. Must be [a-z0-9._-]: %s", namespace);
|
||||||
Preconditions.checkArgument(key != null && VALID_KEY.matcher(key).matches(), "key");
|
Preconditions.checkArgument(key != null && VALID_KEY.matcher(key).matches(), "Invalid key. Must be [a-z0-9/._-]: %s", key);
|
||||||
|
|
||||||
this.namespace = namespace;
|
this.namespace = namespace;
|
||||||
this.key = key;
|
this.key = key;
|
||||||
@@ -56,20 +56,26 @@ public final class NamespacedKey {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Create a key in the plugin's namespace.
|
* Create a key in the plugin's namespace.
|
||||||
|
* <p>
|
||||||
|
* Namespaces may only contain lowercase alphanumeric characters, periods,
|
||||||
|
* underscores, and hyphens.
|
||||||
|
* <p>
|
||||||
|
* Keys may only contain lowercase alphanumeric characters, periods,
|
||||||
|
* underscores, hyphens, and forward slashes.
|
||||||
*
|
*
|
||||||
* @param plugin the plugin to use for the namespace
|
* @param plugin the plugin to use for the namespace
|
||||||
* @param key the key to create
|
* @param key the key to create
|
||||||
*/
|
*/
|
||||||
public NamespacedKey(Plugin plugin, String key) {
|
public NamespacedKey(Plugin plugin, String key) {
|
||||||
Preconditions.checkArgument(plugin != null, "plugin");
|
Preconditions.checkArgument(plugin != null, "Plugin cannot be null");
|
||||||
Preconditions.checkArgument(key != null, "key");
|
Preconditions.checkArgument(key != null, "Key cannot be null");
|
||||||
|
|
||||||
this.namespace = plugin.getName().toLowerCase(Locale.ROOT);
|
this.namespace = plugin.getName().toLowerCase(Locale.ROOT);
|
||||||
this.key = key.toLowerCase().toLowerCase(Locale.ROOT);
|
this.key = key.toLowerCase().toLowerCase(Locale.ROOT);
|
||||||
|
|
||||||
// Check validity after normalization
|
// Check validity after normalization
|
||||||
Preconditions.checkArgument(VALID_NAMESPACE.matcher(this.namespace).matches(), "namespace");
|
Preconditions.checkArgument(VALID_NAMESPACE.matcher(this.namespace).matches(), "Invalid namespace. Must be [a-z0-9._-]: %s", this.namespace);
|
||||||
Preconditions.checkArgument(VALID_KEY.matcher(this.key).matches(), "key");
|
Preconditions.checkArgument(VALID_KEY.matcher(this.key).matches(), "Invalid key. Must be [a-z0-9/._-]: %s", this.key);
|
||||||
|
|
||||||
String string = toString();
|
String string = toString();
|
||||||
Preconditions.checkArgument(string.length() < 256, "NamespacedKey must be less than 256 characters (%s)", string);
|
Preconditions.checkArgument(string.length() < 256, "NamespacedKey must be less than 256 characters (%s)", string);
|
||||||
|
@@ -29,6 +29,16 @@ public class NamespacedKeyTest {
|
|||||||
new NamespacedKey("minecraft/test", "foo").toString();
|
new NamespacedKey("minecraft/test", "foo").toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test(expected = IllegalArgumentException.class)
|
||||||
|
public void testInvalidNamespaceCasing() {
|
||||||
|
new NamespacedKey("Minecraft", "foo").toString();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test(expected = IllegalArgumentException.class)
|
||||||
|
public void testInvalidKeyCasing() {
|
||||||
|
new NamespacedKey("minecraft", "Foo").toString();
|
||||||
|
}
|
||||||
|
|
||||||
@Test(expected = IllegalArgumentException.class)
|
@Test(expected = IllegalArgumentException.class)
|
||||||
public void testInvalidKey() {
|
public void testInvalidKey() {
|
||||||
new NamespacedKey("minecraft", "foo!").toString();
|
new NamespacedKey("minecraft", "foo!").toString();
|
||||||
|
Reference in New Issue
Block a user