Skip to content
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

Merged
merged 1 commit into from Aug 21, 2015
Merged

Do not permit multiple settings files #13043

merged 1 commit into from Aug 21, 2015

Conversation

jasontedor
Copy link
Member

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

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();
Copy link
Contributor

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.

Copy link
Member Author

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.

Copy link
Contributor

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);
        }
    }
}

Copy link
Member Author

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.

Copy link
Contributor

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.

Copy link
Contributor

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.

@jpountz
Copy link
Contributor

jpountz commented Aug 21, 2015

Similar concerns as Robert for tests, but otherwise LGTM

jasontedor added a commit that referenced this pull request Aug 21, 2015
@jasontedor jasontedor merged commit 907f648 into elastic:master Aug 21, 2015
@jasontedor jasontedor deleted the fix/no-multiple-settings branch August 21, 2015 16:28
jasontedor added a commit that referenced this pull request Aug 21, 2015
This commit backports the fix #13043 for #13042 to 2.0 from master.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants