New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Do not permit multiple settings files #13043
Do not permit multiple settings files #13043
Conversation
This commit enforces that at most a single settings file is found. If multiple settings files are found, a SettingsException will be thrown Closes #13042
@@ -42,6 +44,8 @@ | |||
import static org.hamcrest.Matchers.*; | |||
|
|||
public class InternalSettingsPreparerTests extends ESTestCase { | |||
@Rule | |||
public ExpectedException expectedException = ExpectedException.none(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is a matter of taste i suppose, but i still hate all junit support for exceptions like this.
The rule sucks because it has side effects, if we write another test that uses it, it can easily have some bogus leftover state from a previous test method?
Personally i still do it tests like this:
try {
something();
fail("should have hit expected exception");
} catch (SomeException expected) {
assertTrue(expected.getMessage().contains("expected text"));
}
This sucks too, in that its easy to forget the fail() part and have the whole test do nothing.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The rule sucks because it has side effects, if we write another test that uses it, it can easily have some bogus leftover state from a previous test method?
I don't think so. I think that @rule is just like @before in that it's processed once before each test method thus setting the state to empty before each test method runs. I'll research.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here would be something of an alternative maybe for the future, when java 8 is minimal (it would be less annoying due to effectively final and lambda):
public void test() {
assertError(IndexOutOfBoundsException.class, () -> {
int foo[] = new int[5];
System.out.println(foo[6]);
});
}
// test helper for expected exception
// TODO: can we replace Runnable with TestMethod or similar interface that throws Throwable
// so checked exceptions arent annoying?
static void assertError(Class<? extends Throwable> expectedClazz, Runnable foo) {
assertError(expectedClazz, null, foo);
}
// test helper for expected exception, with expected message
static void assertError(Class<? extends Throwable> expectedClazz, String expectedMessage, Runnable foo) {
try {
foo.run();
fail("didnt hit expected exception, expected: " + expectedClazz.getSimpleName());
} catch (Throwable t) {
if (!expectedClazz.isAssignableFrom(t.getClass())) {
throw new IllegalStateException("got the wrong exception, expected: " + expectedClazz.getSimpleName() + ", got: " + t, t);
}
if (t.getMessage() == null && expectedMessage != null) {
throw new IllegalStateException("exception had null message, but expected: " + expectedMessage, t);
}
if (expectedMessage != null && !t.getMessage().contains(expectedMessage)) {
throw new IllegalStateException("expection message did not contain expected text: " + expectedMessage, t);
}
}
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The execptedException
field will be initialized before each test method.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok, then i am fine with it! Maybe we should followup and consider moving it to a base class and we do this stuff in a consistent way everywhere. I bet we will find test bugs.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
And one downside even with being initialized before each test method, is some tests do multiple asserts like this inside a single method. So we'd have to clean up those tests to be separate test methods, but thats still fine.
Similar concerns as Robert for tests, but otherwise LGTM |
Do not permit multiple settings files
This commit enforces that at most a single settings file is found. If
multiple settings files are found, a SettingsException will be thrown
Closes #13042