From 8c5338a6bdac7eba17ef7e023661384308ae0ab0 Mon Sep 17 00:00:00 2001 From: Nathan Adams Date: Thu, 2 Oct 2014 12:56:51 +0200 Subject: [PATCH] Start of new exception system --- build.gradle | 1 + .../minecraft/commands/CommandDispatcher.java | 13 ++--- .../arguments/CommandArgumentType.java | 5 +- .../arguments/IntegerArgumentType.java | 16 +++-- .../ArgumentValidationException.java | 4 -- .../commands/exceptions/CommandException.java | 22 +++++++ .../exceptions/CommandExceptionType.java | 6 ++ .../IllegalArgumentSyntaxException.java | 4 -- .../ParameterizedCommandExceptionType.java | 58 +++++++++++++++++++ .../SimpleCommandExceptionType.java | 42 ++++++++++++++ .../exceptions/UnknownCommandException.java | 4 -- .../commands/tree/ArgumentCommandNode.java | 5 +- .../minecraft/commands/tree/CommandNode.java | 5 +- .../commands/tree/LiteralCommandNode.java | 10 ++-- .../commands/tree/RootCommandNode.java | 5 +- .../commands/CommandDispatcherTest.java | 9 ++- .../arguments/IntegerArgumentTypeTest.java | 40 ++++++++++--- ...ParameterizedCommandExceptionTypeTest.java | 47 +++++++++++++++ .../SimpleCommandExceptionTypeTest.java | 28 +++++++++ .../tree/ArgumentCommandNodeTest.java | 4 +- .../commands/tree/LiteralCommandNodeTest.java | 6 +- 21 files changed, 274 insertions(+), 60 deletions(-) delete mode 100644 src/main/java/net/minecraft/commands/exceptions/ArgumentValidationException.java create mode 100644 src/main/java/net/minecraft/commands/exceptions/CommandExceptionType.java delete mode 100644 src/main/java/net/minecraft/commands/exceptions/IllegalArgumentSyntaxException.java create mode 100644 src/main/java/net/minecraft/commands/exceptions/ParameterizedCommandExceptionType.java create mode 100644 src/main/java/net/minecraft/commands/exceptions/SimpleCommandExceptionType.java delete mode 100644 src/main/java/net/minecraft/commands/exceptions/UnknownCommandException.java create mode 100644 src/test/java/net/minecraft/commands/exceptions/ParameterizedCommandExceptionTypeTest.java create mode 100644 src/test/java/net/minecraft/commands/exceptions/SimpleCommandExceptionTypeTest.java diff --git a/build.gradle b/build.gradle index f778a99..acac085 100644 --- a/build.gradle +++ b/build.gradle @@ -27,6 +27,7 @@ repositories { dependencies { compile 'com.google.code.findbugs:jsr305:2.0.1' compile 'com.google.guava:guava:17.0' + compile 'org.apache.commons:commons-lang3:3.3.2' testCompile 'junit:junit-dep:4.10' testCompile 'org.hamcrest:hamcrest-library:1.2.1' testCompile 'org.mockito:mockito-core:1.8.5' diff --git a/src/main/java/net/minecraft/commands/CommandDispatcher.java b/src/main/java/net/minecraft/commands/CommandDispatcher.java index 812e9ed..cbd1f4d 100644 --- a/src/main/java/net/minecraft/commands/CommandDispatcher.java +++ b/src/main/java/net/minecraft/commands/CommandDispatcher.java @@ -3,14 +3,13 @@ package net.minecraft.commands; import net.minecraft.commands.builder.LiteralArgumentBuilder; import net.minecraft.commands.context.CommandContext; import net.minecraft.commands.context.CommandContextBuilder; -import net.minecraft.commands.exceptions.ArgumentValidationException; import net.minecraft.commands.exceptions.CommandException; -import net.minecraft.commands.exceptions.IllegalArgumentSyntaxException; -import net.minecraft.commands.exceptions.UnknownCommandException; +import net.minecraft.commands.exceptions.SimpleCommandExceptionType; import net.minecraft.commands.tree.CommandNode; import net.minecraft.commands.tree.RootCommandNode; public class CommandDispatcher { + public static final SimpleCommandExceptionType ERROR_UNKNOWN_COMMAND = new SimpleCommandExceptionType("unknown_command", "Unknown command"); public static final String ARGUMENT_SEPARATOR = " "; private final RootCommandNode root = new RootCommandNode(); @@ -24,8 +23,8 @@ public class CommandDispatcher { context.getCommand().run(context); } - protected CommandContext parseNodes(CommandNode node, String command, CommandContextBuilder contextBuilder) throws IllegalArgumentSyntaxException, ArgumentValidationException, UnknownCommandException { - IllegalArgumentSyntaxException exception = null; + protected CommandContext parseNodes(CommandNode node, String command, CommandContextBuilder contextBuilder) throws CommandException { + CommandException exception = null; for (CommandNode child : node.getChildren()) { try { @@ -35,7 +34,7 @@ public class CommandDispatcher { context.withCommand(child.getCommand()); } return parseNodes(child, remaining, context); - } catch (IllegalArgumentSyntaxException ex) { + } catch (CommandException ex) { exception = ex; } } @@ -44,7 +43,7 @@ public class CommandDispatcher { throw exception; } if (command.length() > 0) { - throw new UnknownCommandException(); + throw ERROR_UNKNOWN_COMMAND.create(); } return contextBuilder.build(); diff --git a/src/main/java/net/minecraft/commands/arguments/CommandArgumentType.java b/src/main/java/net/minecraft/commands/arguments/CommandArgumentType.java index 108f493..7e608fa 100644 --- a/src/main/java/net/minecraft/commands/arguments/CommandArgumentType.java +++ b/src/main/java/net/minecraft/commands/arguments/CommandArgumentType.java @@ -1,9 +1,8 @@ package net.minecraft.commands.arguments; import net.minecraft.commands.context.ParsedArgument; -import net.minecraft.commands.exceptions.ArgumentValidationException; -import net.minecraft.commands.exceptions.IllegalArgumentSyntaxException; +import net.minecraft.commands.exceptions.CommandException; public interface CommandArgumentType { - ParsedArgument parse(String command) throws IllegalArgumentSyntaxException, ArgumentValidationException; + ParsedArgument parse(String command) throws CommandException; } diff --git a/src/main/java/net/minecraft/commands/arguments/IntegerArgumentType.java b/src/main/java/net/minecraft/commands/arguments/IntegerArgumentType.java index aad25ca..7291548 100644 --- a/src/main/java/net/minecraft/commands/arguments/IntegerArgumentType.java +++ b/src/main/java/net/minecraft/commands/arguments/IntegerArgumentType.java @@ -4,10 +4,14 @@ import com.google.common.base.Splitter; import net.minecraft.commands.CommandDispatcher; import net.minecraft.commands.context.CommandContext; import net.minecraft.commands.context.ParsedArgument; -import net.minecraft.commands.exceptions.ArgumentValidationException; -import net.minecraft.commands.exceptions.IllegalArgumentSyntaxException; +import net.minecraft.commands.exceptions.CommandException; +import net.minecraft.commands.exceptions.ParameterizedCommandExceptionType; public class IntegerArgumentType implements CommandArgumentType { + public static final ParameterizedCommandExceptionType ERROR_NOT_A_NUMBER = new ParameterizedCommandExceptionType("argument-integer-invalid", "Expected an integer, found '${found}'", "found"); + public static final ParameterizedCommandExceptionType ERROR_TOO_SMALL = new ParameterizedCommandExceptionType("argument-integer-low", "Integer must not be less than ${minimum}, found ${found}", "found", "minimum"); + public static final ParameterizedCommandExceptionType ERROR_TOO_BIG = new ParameterizedCommandExceptionType("argument-integer-big", "Integer must not be more than ${maximum}, found ${found}", "found", "maximum"); + private static final Splitter SPLITTER = Splitter.on(CommandDispatcher.ARGUMENT_SEPARATOR).limit(2); private final int minimum; @@ -35,22 +39,22 @@ public class IntegerArgumentType implements CommandArgumentType { } @Override - public ParsedArgument parse(String command) throws IllegalArgumentSyntaxException, ArgumentValidationException { + public ParsedArgument parse(String command) throws CommandException { String raw = SPLITTER.split(command).iterator().next(); try { int value = Integer.parseInt(raw); if (value < minimum) { - throw new ArgumentValidationException(); + throw ERROR_TOO_SMALL.create(value, minimum); } if (value > maximum) { - throw new ArgumentValidationException(); + throw ERROR_TOO_BIG.create(value, maximum); } return new ParsedArgument(raw, value); } catch (NumberFormatException ignored) { - throw new IllegalArgumentSyntaxException(); + throw ERROR_NOT_A_NUMBER.create(raw); } } diff --git a/src/main/java/net/minecraft/commands/exceptions/ArgumentValidationException.java b/src/main/java/net/minecraft/commands/exceptions/ArgumentValidationException.java deleted file mode 100644 index 8fd83a9..0000000 --- a/src/main/java/net/minecraft/commands/exceptions/ArgumentValidationException.java +++ /dev/null @@ -1,4 +0,0 @@ -package net.minecraft.commands.exceptions; - -public class ArgumentValidationException extends CommandException { -} diff --git a/src/main/java/net/minecraft/commands/exceptions/CommandException.java b/src/main/java/net/minecraft/commands/exceptions/CommandException.java index 44f70c1..6295144 100644 --- a/src/main/java/net/minecraft/commands/exceptions/CommandException.java +++ b/src/main/java/net/minecraft/commands/exceptions/CommandException.java @@ -1,4 +1,26 @@ package net.minecraft.commands.exceptions; +import java.util.Map; + public class CommandException extends Exception { + private final CommandExceptionType type; + private final Map data; + + public CommandException(CommandExceptionType type, Map data) { + this.type = type; + this.data = data; + } + + @Override + public String getMessage() { + return type.getErrorMessage(this); + } + + public CommandExceptionType getType() { + return type; + } + + public Map getData() { + return data; + } } diff --git a/src/main/java/net/minecraft/commands/exceptions/CommandExceptionType.java b/src/main/java/net/minecraft/commands/exceptions/CommandExceptionType.java new file mode 100644 index 0000000..b8b4d98 --- /dev/null +++ b/src/main/java/net/minecraft/commands/exceptions/CommandExceptionType.java @@ -0,0 +1,6 @@ +package net.minecraft.commands.exceptions; + +public interface CommandExceptionType { + String getTypeName(); + String getErrorMessage(CommandException exception); +} diff --git a/src/main/java/net/minecraft/commands/exceptions/IllegalArgumentSyntaxException.java b/src/main/java/net/minecraft/commands/exceptions/IllegalArgumentSyntaxException.java deleted file mode 100644 index e65efb5..0000000 --- a/src/main/java/net/minecraft/commands/exceptions/IllegalArgumentSyntaxException.java +++ /dev/null @@ -1,4 +0,0 @@ -package net.minecraft.commands.exceptions; - -public class IllegalArgumentSyntaxException extends CommandException { -} diff --git a/src/main/java/net/minecraft/commands/exceptions/ParameterizedCommandExceptionType.java b/src/main/java/net/minecraft/commands/exceptions/ParameterizedCommandExceptionType.java new file mode 100644 index 0000000..ca52421 --- /dev/null +++ b/src/main/java/net/minecraft/commands/exceptions/ParameterizedCommandExceptionType.java @@ -0,0 +1,58 @@ +package net.minecraft.commands.exceptions; + +import com.google.common.base.Joiner; +import com.google.common.collect.ImmutableMap; +import org.apache.commons.lang3.text.StrSubstitutor; + +public class ParameterizedCommandExceptionType implements CommandExceptionType { + private static final Joiner JOINER = Joiner.on(", "); + + private final String name; + private final String message; + private final String[] keys; + + public ParameterizedCommandExceptionType(String name, String message, String... keys) { + this.name = name; + this.message = message; + this.keys = keys; + } + + @Override + public String getTypeName() { + return name; + } + + @Override + public String getErrorMessage(CommandException exception) { + return new StrSubstitutor(exception.getData()).replace(message); + } + + public CommandException create(Object... values) { + if (values.length != keys.length) { + throw new IllegalArgumentException("Invalid values! (Expected: " + JOINER.join(keys) + ")"); + } + + ImmutableMap.Builder builder = ImmutableMap.builder(); + + for (int i = 0; i < keys.length; i++) { + builder = builder.put(keys[i], values[i]); + } + + return new CommandException(this, builder.build()); + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (!(o instanceof CommandExceptionType)) return false; + + CommandExceptionType that = (CommandExceptionType) o; + + return getTypeName().equals(that.getTypeName()); + } + + @Override + public int hashCode() { + return getTypeName().hashCode(); + } +} diff --git a/src/main/java/net/minecraft/commands/exceptions/SimpleCommandExceptionType.java b/src/main/java/net/minecraft/commands/exceptions/SimpleCommandExceptionType.java new file mode 100644 index 0000000..beed886 --- /dev/null +++ b/src/main/java/net/minecraft/commands/exceptions/SimpleCommandExceptionType.java @@ -0,0 +1,42 @@ +package net.minecraft.commands.exceptions; + +import com.google.common.collect.ImmutableMap; + +public class SimpleCommandExceptionType implements CommandExceptionType { + private final String name; + private final String message; + + public SimpleCommandExceptionType(String name, String message) { + this.name = name; + this.message = message; + } + + @Override + public String getTypeName() { + return name; + } + + @Override + public String getErrorMessage(CommandException exception) { + return message; + } + + public CommandException create() { + return new CommandException(this, ImmutableMap.of()); + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (!(o instanceof CommandExceptionType)) return false; + + CommandExceptionType that = (CommandExceptionType) o; + + return getTypeName().equals(that.getTypeName()); + } + + @Override + public int hashCode() { + return getTypeName().hashCode(); + } +} diff --git a/src/main/java/net/minecraft/commands/exceptions/UnknownCommandException.java b/src/main/java/net/minecraft/commands/exceptions/UnknownCommandException.java deleted file mode 100644 index 58f844e..0000000 --- a/src/main/java/net/minecraft/commands/exceptions/UnknownCommandException.java +++ /dev/null @@ -1,4 +0,0 @@ -package net.minecraft.commands.exceptions; - -public class UnknownCommandException extends CommandException { -} diff --git a/src/main/java/net/minecraft/commands/tree/ArgumentCommandNode.java b/src/main/java/net/minecraft/commands/tree/ArgumentCommandNode.java index 329cdbf..bee71b1 100644 --- a/src/main/java/net/minecraft/commands/tree/ArgumentCommandNode.java +++ b/src/main/java/net/minecraft/commands/tree/ArgumentCommandNode.java @@ -4,8 +4,7 @@ import net.minecraft.commands.Command; import net.minecraft.commands.arguments.CommandArgumentType; import net.minecraft.commands.context.CommandContextBuilder; import net.minecraft.commands.context.ParsedArgument; -import net.minecraft.commands.exceptions.ArgumentValidationException; -import net.minecraft.commands.exceptions.IllegalArgumentSyntaxException; +import net.minecraft.commands.exceptions.CommandException; public class ArgumentCommandNode extends CommandNode { private final String name; @@ -31,7 +30,7 @@ public class ArgumentCommandNode extends CommandNode { } @Override - public String parse(String command, CommandContextBuilder contextBuilder) throws IllegalArgumentSyntaxException, ArgumentValidationException { + public String parse(String command, CommandContextBuilder contextBuilder) throws CommandException { ParsedArgument parsed = type.parse(command); int start = parsed.getRaw().length(); diff --git a/src/main/java/net/minecraft/commands/tree/CommandNode.java b/src/main/java/net/minecraft/commands/tree/CommandNode.java index 2a7d459..d323e5a 100644 --- a/src/main/java/net/minecraft/commands/tree/CommandNode.java +++ b/src/main/java/net/minecraft/commands/tree/CommandNode.java @@ -3,8 +3,7 @@ package net.minecraft.commands.tree; import com.google.common.collect.Maps; import net.minecraft.commands.Command; import net.minecraft.commands.context.CommandContextBuilder; -import net.minecraft.commands.exceptions.ArgumentValidationException; -import net.minecraft.commands.exceptions.IllegalArgumentSyntaxException; +import net.minecraft.commands.exceptions.CommandException; import java.util.Collection; import java.util.Map; @@ -60,5 +59,5 @@ public abstract class CommandNode { protected abstract Object getMergeKey(); - public abstract String parse(String command, CommandContextBuilder contextBuilder) throws IllegalArgumentSyntaxException, ArgumentValidationException; + public abstract String parse(String command, CommandContextBuilder contextBuilder) throws CommandException; } diff --git a/src/main/java/net/minecraft/commands/tree/LiteralCommandNode.java b/src/main/java/net/minecraft/commands/tree/LiteralCommandNode.java index 6859ac4..6eb7659 100644 --- a/src/main/java/net/minecraft/commands/tree/LiteralCommandNode.java +++ b/src/main/java/net/minecraft/commands/tree/LiteralCommandNode.java @@ -3,10 +3,12 @@ package net.minecraft.commands.tree; import net.minecraft.commands.Command; import net.minecraft.commands.CommandDispatcher; import net.minecraft.commands.context.CommandContextBuilder; -import net.minecraft.commands.exceptions.ArgumentValidationException; -import net.minecraft.commands.exceptions.IllegalArgumentSyntaxException; +import net.minecraft.commands.exceptions.CommandException; +import net.minecraft.commands.exceptions.ParameterizedCommandExceptionType; public class LiteralCommandNode extends CommandNode { + public static final ParameterizedCommandExceptionType ERROR_INCORRECT_LITERAL = new ParameterizedCommandExceptionType("incorrect_literal", "Expected literal ${expected}", "expected"); + private final String literal; public LiteralCommandNode(String literal, Command command) { @@ -24,11 +26,11 @@ public class LiteralCommandNode extends CommandNode { } @Override - public String parse(String command, CommandContextBuilder contextBuilder) throws IllegalArgumentSyntaxException, ArgumentValidationException { + public String parse(String command, CommandContextBuilder contextBuilder) throws CommandException { String expected = literal + (command.length() > literal.length() ? CommandDispatcher.ARGUMENT_SEPARATOR : ""); if (!command.startsWith(expected)) { - throw new IllegalArgumentSyntaxException(); + throw ERROR_INCORRECT_LITERAL.create(expected); } int start = expected.length(); diff --git a/src/main/java/net/minecraft/commands/tree/RootCommandNode.java b/src/main/java/net/minecraft/commands/tree/RootCommandNode.java index 1fb008a..4bb9838 100644 --- a/src/main/java/net/minecraft/commands/tree/RootCommandNode.java +++ b/src/main/java/net/minecraft/commands/tree/RootCommandNode.java @@ -1,8 +1,7 @@ package net.minecraft.commands.tree; import net.minecraft.commands.context.CommandContextBuilder; -import net.minecraft.commands.exceptions.ArgumentValidationException; -import net.minecraft.commands.exceptions.IllegalArgumentSyntaxException; +import net.minecraft.commands.exceptions.CommandException; public class RootCommandNode extends CommandNode { public RootCommandNode() { @@ -15,7 +14,7 @@ public class RootCommandNode extends CommandNode { } @Override - public String parse(String command, CommandContextBuilder contextBuilder) throws IllegalArgumentSyntaxException, ArgumentValidationException { + public String parse(String command, CommandContextBuilder contextBuilder) throws CommandException { return command; } diff --git a/src/test/java/net/minecraft/commands/CommandDispatcherTest.java b/src/test/java/net/minecraft/commands/CommandDispatcherTest.java index 9262ed1..91c5f1c 100644 --- a/src/test/java/net/minecraft/commands/CommandDispatcherTest.java +++ b/src/test/java/net/minecraft/commands/CommandDispatcherTest.java @@ -1,8 +1,7 @@ package net.minecraft.commands; import net.minecraft.commands.context.CommandContext; -import net.minecraft.commands.exceptions.IllegalArgumentSyntaxException; -import net.minecraft.commands.exceptions.UnknownCommandException; +import net.minecraft.commands.exceptions.CommandException; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; @@ -78,12 +77,12 @@ public class CommandDispatcherTest { verify(three).run(any(CommandContext.class)); } - @Test(expected = UnknownCommandException.class) + @Test(expected = CommandException.class) public void testExecuteUnknownCommand() throws Exception { subject.execute("foo", source); } - @Test(expected = UnknownCommandException.class) + @Test(expected = CommandException.class) public void testExecuteUnknownSubcommand() throws Exception { subject.register(literal("foo").executes(command)); subject.execute("foo bar", source); @@ -105,7 +104,7 @@ public class CommandDispatcherTest { verify(subCommand).run(any(CommandContext.class)); } - @Test(expected = IllegalArgumentSyntaxException.class) + @Test(expected = CommandException.class) public void testExecuteInvalidSubcommand() throws Exception { subject.register(literal("foo").then( argument("bar", integer()) diff --git a/src/test/java/net/minecraft/commands/arguments/IntegerArgumentTypeTest.java b/src/test/java/net/minecraft/commands/arguments/IntegerArgumentTypeTest.java index 62e4ab0..06f026f 100644 --- a/src/test/java/net/minecraft/commands/arguments/IntegerArgumentTypeTest.java +++ b/src/test/java/net/minecraft/commands/arguments/IntegerArgumentTypeTest.java @@ -1,18 +1,22 @@ package net.minecraft.commands.arguments; +import com.google.common.collect.ImmutableMap; +import com.google.common.testing.EqualsTester; import net.minecraft.commands.context.CommandContext; import net.minecraft.commands.context.CommandContextBuilder; import net.minecraft.commands.context.ParsedArgument; -import net.minecraft.commands.exceptions.ArgumentValidationException; -import net.minecraft.commands.exceptions.IllegalArgumentSyntaxException; -import com.google.common.testing.EqualsTester; +import net.minecraft.commands.exceptions.CommandException; +import net.minecraft.commands.exceptions.CommandExceptionType; import org.junit.Before; import org.junit.Test; +import java.util.Map; + import static net.minecraft.commands.arguments.IntegerArgumentType.integer; import static org.hamcrest.Matchers.hasToString; import static org.hamcrest.Matchers.is; import static org.junit.Assert.assertThat; +import static org.junit.Assert.fail; public class IntegerArgumentTypeTest { IntegerArgumentType type; @@ -30,14 +34,26 @@ public class IntegerArgumentTypeTest { assertThat(result.getResult(), is(50)); } - @Test(expected = IllegalArgumentSyntaxException.class) + @Test public void testParseInvalid() throws Exception { - type.parse("fifty"); + try { + type.parse("fifty"); + fail(); + } catch (CommandException ex) { + assertThat(ex.getType(), is((CommandExceptionType) IntegerArgumentType.ERROR_NOT_A_NUMBER)); + assertThat(ex.getData(), is((Map) ImmutableMap.of("found", "fifty"))); + } } - @Test(expected = ArgumentValidationException.class) + @Test public void testParseTooLow() throws Exception { - type.parse("-101"); + try { + type.parse("-101"); + fail(); + } catch (CommandException ex) { + assertThat(ex.getType(), is((CommandExceptionType) IntegerArgumentType.ERROR_TOO_SMALL)); + assertThat(ex.getData(), is((Map) ImmutableMap.of("found", -101, "minimum", -100))); + } } @Test @@ -48,9 +64,15 @@ public class IntegerArgumentTypeTest { assertThat(result.getResult(), is(-100)); } - @Test(expected = ArgumentValidationException.class) + @Test public void testParseTooHigh() throws Exception { - type.parse("101"); + try { + type.parse("101"); + fail(); + } catch (CommandException ex) { + assertThat(ex.getType(), is((CommandExceptionType) IntegerArgumentType.ERROR_TOO_BIG)); + assertThat(ex.getData(), is((Map) ImmutableMap.of("found", 101, "maximum", 100))); + } } @Test diff --git a/src/test/java/net/minecraft/commands/exceptions/ParameterizedCommandExceptionTypeTest.java b/src/test/java/net/minecraft/commands/exceptions/ParameterizedCommandExceptionTypeTest.java new file mode 100644 index 0000000..cac7518 --- /dev/null +++ b/src/test/java/net/minecraft/commands/exceptions/ParameterizedCommandExceptionTypeTest.java @@ -0,0 +1,47 @@ +package net.minecraft.commands.exceptions; + +import com.google.common.collect.ImmutableMap; +import com.google.common.testing.EqualsTester; +import org.junit.Before; +import org.junit.Test; + +import java.util.Map; + +import static org.hamcrest.Matchers.is; +import static org.junit.Assert.assertThat; + +@SuppressWarnings("ThrowableResultOfMethodCallIgnored") +public class ParameterizedCommandExceptionTypeTest { + ParameterizedCommandExceptionType type; + + @Before + public void setUp() throws Exception { + type = new ParameterizedCommandExceptionType("foo", "Hello, ${name}!", "name"); + } + + @Test(expected = IllegalArgumentException.class) + public void testCreateTooFewArguments() throws Exception { + type.create(); + } + + @Test(expected = IllegalArgumentException.class) + public void testCreateTooManyArguments() throws Exception { + type.create("World", "Universe"); + } + + @Test + public void testCreate() throws Exception { + CommandException exception = type.create("World"); + assertThat(exception.getType(), is((CommandExceptionType) type)); + assertThat(exception.getData(), is((Map) ImmutableMap.of("name", "World"))); + assertThat(exception.getMessage(), is("Hello, World!")); + } + + @Test + public void testEquals() throws Exception { + new EqualsTester() + .addEqualityGroup(new ParameterizedCommandExceptionType("foo", "Hello, world!"), new ParameterizedCommandExceptionType("foo", "Hello, universe!"), new ParameterizedCommandExceptionType("foo", "Hello, world!", "bar")) + .addEqualityGroup(new ParameterizedCommandExceptionType("bar", "Hello, world!"), new ParameterizedCommandExceptionType("bar", "Hello, universe!"), new ParameterizedCommandExceptionType("bar", "Hello, world!", "bar")) + .testEquals(); + } +} \ No newline at end of file diff --git a/src/test/java/net/minecraft/commands/exceptions/SimpleCommandExceptionTypeTest.java b/src/test/java/net/minecraft/commands/exceptions/SimpleCommandExceptionTypeTest.java new file mode 100644 index 0000000..756eb35 --- /dev/null +++ b/src/test/java/net/minecraft/commands/exceptions/SimpleCommandExceptionTypeTest.java @@ -0,0 +1,28 @@ +package net.minecraft.commands.exceptions; + +import com.google.common.testing.EqualsTester; +import org.junit.Test; + +import static org.hamcrest.Matchers.empty; +import static org.hamcrest.Matchers.is; +import static org.junit.Assert.assertThat; + +@SuppressWarnings("ThrowableResultOfMethodCallIgnored") +public class SimpleCommandExceptionTypeTest { + @Test + public void testCreate() throws Exception { + SimpleCommandExceptionType type = new SimpleCommandExceptionType("foo", "bar"); + CommandException exception = type.create(); + assertThat(exception.getType(), is((CommandExceptionType) type)); + assertThat(exception.getMessage(), is("bar")); + assertThat(exception.getData().values(), empty()); + } + + @Test + public void testEquals() throws Exception { + new EqualsTester() + .addEqualityGroup(new SimpleCommandExceptionType("foo", "Hello, world!"), new SimpleCommandExceptionType("foo", "Hello, universe!")) + .addEqualityGroup(new SimpleCommandExceptionType("bar", "Hello, world!"), new SimpleCommandExceptionType("bar", "Hello, universe!")) + .testEquals(); + } +} \ No newline at end of file diff --git a/src/test/java/net/minecraft/commands/tree/ArgumentCommandNodeTest.java b/src/test/java/net/minecraft/commands/tree/ArgumentCommandNodeTest.java index 66607a5..3f76ca8 100644 --- a/src/test/java/net/minecraft/commands/tree/ArgumentCommandNodeTest.java +++ b/src/test/java/net/minecraft/commands/tree/ArgumentCommandNodeTest.java @@ -3,7 +3,7 @@ package net.minecraft.commands.tree; import com.google.common.testing.EqualsTester; import net.minecraft.commands.Command; import net.minecraft.commands.context.CommandContextBuilder; -import net.minecraft.commands.exceptions.IllegalArgumentSyntaxException; +import net.minecraft.commands.exceptions.CommandException; import org.junit.Before; import org.junit.Test; @@ -44,7 +44,7 @@ public class ArgumentCommandNodeTest extends AbstractCommandNodeTest { assertThat(contextBuilder.getArguments().get("foo").getResult(), is((Object) 123)); } - @Test(expected = IllegalArgumentSyntaxException.class) + @Test(expected = CommandException.class) public void testParseInvalid() throws Exception { node.parse("foo", contextBuilder); } diff --git a/src/test/java/net/minecraft/commands/tree/LiteralCommandNodeTest.java b/src/test/java/net/minecraft/commands/tree/LiteralCommandNodeTest.java index ea982e8..79d7b6a 100644 --- a/src/test/java/net/minecraft/commands/tree/LiteralCommandNodeTest.java +++ b/src/test/java/net/minecraft/commands/tree/LiteralCommandNodeTest.java @@ -3,7 +3,7 @@ package net.minecraft.commands.tree; import com.google.common.testing.EqualsTester; import net.minecraft.commands.Command; import net.minecraft.commands.context.CommandContextBuilder; -import net.minecraft.commands.exceptions.IllegalArgumentSyntaxException; +import net.minecraft.commands.exceptions.CommandException; import org.junit.Before; import org.junit.Test; @@ -37,12 +37,12 @@ public class LiteralCommandNodeTest extends AbstractCommandNodeTest { assertThat(node.parse("foo", contextBuilder), is("")); } - @Test(expected = IllegalArgumentSyntaxException.class) + @Test(expected = CommandException.class) public void testParseSimilar() throws Exception { node.parse("foobar", contextBuilder); } - @Test(expected = IllegalArgumentSyntaxException.class) + @Test(expected = CommandException.class) public void testParseInvalid() throws Exception { node.parse("bar", contextBuilder); }