* #1497 Throw dedicated exception for invalid YAML files and handle it on startup - Wrap SnakeYAML exceptions when loading config.yml and commands.yml on startup into own exception type - Handle exception type on startup with specific error message * #1497 Fix inaccurate JavaDoc comment
This commit is contained in:
@@ -0,0 +1,48 @@
|
||||
package fr.xephi.authme.service.yaml;
|
||||
|
||||
import ch.jalu.configme.resource.YamlFileResource;
|
||||
import fr.xephi.authme.TestHelper;
|
||||
import org.junit.Test;
|
||||
import org.yaml.snakeyaml.parser.ParserException;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
import static org.hamcrest.Matchers.instanceOf;
|
||||
import static org.junit.Assert.assertThat;
|
||||
import static org.junit.Assert.fail;
|
||||
|
||||
/**
|
||||
* Test for {@link YamlFileResourceProvider}.
|
||||
*/
|
||||
public class YamlFileResourceProviderTest {
|
||||
|
||||
@Test
|
||||
public void shouldLoadValidFile() {
|
||||
// given
|
||||
File yamlFile = TestHelper.getJarFile(TestHelper.PROJECT_ROOT + "service/yaml/validYaml.yml");
|
||||
|
||||
// when
|
||||
YamlFileResource resource = YamlFileResourceProvider.loadFromFile(yamlFile);
|
||||
|
||||
// then
|
||||
assertThat(resource.getString("test.jkl"), equalTo("Test test"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldThrowForInvalidFile() {
|
||||
// given
|
||||
File yamlFile = TestHelper.getJarFile(TestHelper.PROJECT_ROOT + "service/yaml/invalidYaml.yml");
|
||||
|
||||
// when
|
||||
try {
|
||||
YamlFileResourceProvider.loadFromFile(yamlFile);
|
||||
|
||||
// then
|
||||
fail("Expected exception to be thrown");
|
||||
} catch (YamlParseException e) {
|
||||
assertThat(e.getFile(), equalTo(yamlFile.getPath()));
|
||||
assertThat(e.getCause(), instanceOf(ParserException.class));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
package fr.xephi.authme.util;
|
||||
|
||||
import fr.xephi.authme.ReflectionTestUtils;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.ConcurrentModificationException;
|
||||
|
||||
import static org.hamcrest.Matchers.nullValue;
|
||||
import static org.hamcrest.Matchers.sameInstance;
|
||||
import static org.junit.Assert.assertThat;
|
||||
|
||||
/**
|
||||
* Test for {@link ExceptionUtils}.
|
||||
*/
|
||||
public class ExceptionUtilsTest {
|
||||
|
||||
@Test
|
||||
public void shouldFindWantedThrowable() {
|
||||
// given
|
||||
ConcurrentModificationException initialCme = new ConcurrentModificationException();
|
||||
Throwable th = new Throwable(initialCme);
|
||||
ConcurrentModificationException cme = new ConcurrentModificationException(th);
|
||||
IllegalStateException ise = new IllegalStateException(cme);
|
||||
UnsupportedOperationException uoe = new UnsupportedOperationException(ise);
|
||||
ReflectiveOperationException roe = new ReflectiveOperationException(uoe);
|
||||
|
||||
// when
|
||||
IllegalStateException resultIse = ExceptionUtils.findThrowableInCause(IllegalStateException.class, roe);
|
||||
ConcurrentModificationException resultCme = ExceptionUtils.findThrowableInCause(ConcurrentModificationException.class, cme);
|
||||
StackOverflowError resultSoe = ExceptionUtils.findThrowableInCause(StackOverflowError.class, cme);
|
||||
|
||||
// then
|
||||
assertThat(resultIse, sameInstance(ise));
|
||||
assertThat(resultCme, sameInstance(cme));
|
||||
assertThat(resultSoe, nullValue());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldHandleCircularCausesGracefully() {
|
||||
// given
|
||||
IllegalStateException ise = new IllegalStateException();
|
||||
UnsupportedOperationException uoe = new UnsupportedOperationException(ise);
|
||||
ReflectiveOperationException roe = new ReflectiveOperationException(uoe);
|
||||
ReflectionTestUtils.setField(Throwable.class, ise, "cause", roe);
|
||||
|
||||
// when
|
||||
NullPointerException resultNpe = ExceptionUtils.findThrowableInCause(NullPointerException.class, uoe);
|
||||
UnsupportedOperationException resultUoe = ExceptionUtils.findThrowableInCause(UnsupportedOperationException.class, uoe);
|
||||
|
||||
// then
|
||||
assertThat(resultNpe, nullValue());
|
||||
assertThat(resultUoe, sameInstance(uoe));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
# File with invalid YAML
|
||||
test:
|
||||
abc: 'test'
|
||||
def: 'Going to forget a quote here
|
||||
jkl: 'Test test'
|
||||
@@ -0,0 +1,4 @@
|
||||
test:
|
||||
abc: 'test'
|
||||
def: 'All quotes are good here'
|
||||
jkl: 'Test test'
|
||||
Reference in New Issue
Block a user