Rewrite ThingParser usages.

This commit changes how the ThingParser is used throughout the code base. Instead of blindly filtering out null values, we're now throwing a new InvalidThingInputString exception. This exception is caught in an outer scope that has more context. The exception is then unwrapped and rethrown as a ConfigError with the additional context. In the outermost scope of (re)loading the config-file, the ConfigError is caught and printed, and then (re)loading stops gracefully.

We still need a proper way to handle loads/reloads consistently to get rid of the default command usage message, but this is a good step towards better usability in the face of user errors.

Fixes #478
This commit is contained in:
Andreas Troelsen
2018-08-05 23:14:01 +02:00
parent 924a419b47
commit cd82a89626
10 changed files with 238 additions and 140 deletions
@@ -1,7 +1,6 @@
package com.garbagemule.MobArena.things;
import static org.hamcrest.CoreMatchers.nullValue;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.inOrder;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
@@ -10,13 +9,18 @@ import static org.mockito.Mockito.when;
import com.garbagemule.MobArena.MobArena;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.mockito.InOrder;
public class ThingManagerTest {
private ThingManager subject;
@Rule
public ExpectedException exception = ExpectedException.none();
@Before
public void setup() {
MobArena plugin = mock(MobArena.class);
@@ -27,6 +31,7 @@ public class ThingManagerTest {
public void afterCoreParsersInOrder() {
ThingParser first = mock(ThingParser.class);
ThingParser second = mock(ThingParser.class);
when(second.parse(anyString())).thenReturn(mock(Thing.class));
subject.register(first /*, false */);
subject.register(second /*, false */);
@@ -41,6 +46,7 @@ public class ThingManagerTest {
public void beforeCoreParsersInverseOrder() {
ThingParser first = mock(ThingParser.class);
ThingParser second = mock(ThingParser.class);
when(first.parse(anyString())).thenReturn(mock(Thing.class));
subject.register(first, true);
subject.register(second, true);
@@ -72,7 +78,7 @@ public class ThingManagerTest {
}
@Test
public void returnsNullIfNoParsersSucceed() {
public void throwsIfNoParsersSucceed() {
ThingParser first = mock(ThingParser.class);
ThingParser second = mock(ThingParser.class);
when(first.parse("thing")).thenReturn(null);
@@ -80,9 +86,9 @@ public class ThingManagerTest {
subject.register(first);
subject.register(second);
Thing result = subject.parse("thing");
exception.expect(InvalidThingInputString.class);
assertThat(result, nullValue());
subject.parse("thing");
}
}