Skip to content

Commit 9e3be96

Browse files
mergify[bot]yaauie
andauthored
setting: enforce non-nullable (restore 8.15.x behavior) (#17522) (#17527)
(cherry picked from commit 712b37e) Co-authored-by: Rye Biesemeyer <[email protected]>
1 parent e0d5464 commit 9e3be96

File tree

4 files changed

+135
-14
lines changed

4 files changed

+135
-14
lines changed

Diff for: logstash-core/spec/logstash/settings/string_spec.rb

+5
Original file line numberDiff line numberDiff line change
@@ -34,5 +34,10 @@
3434
expect(subject.value).to eq("a")
3535
end
3636
end
37+
context "when a null value is given" do
38+
it "raises an ArgumentError" do
39+
expect { subject.set(nil) }.to raise_error(java.lang.IllegalArgumentException)
40+
end
41+
end
3742
end
3843
end

Diff for: logstash-core/src/main/java/org/logstash/settings/SettingString.java

+3
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,9 @@ public SettingString(String name, String defaultValue, boolean strict) {
4545

4646
@Override
4747
public void validate(String input) throws IllegalArgumentException {
48+
if (input == null) {
49+
throw new IllegalArgumentException(String.format("Setting \"%s\" must be a String. Received: (NilClass)", this.getName()));
50+
}
4851
staticValidate(input, possibleStrings, this.getName());
4952
}
5053

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package org.logstash.settings;
2+
3+
import org.junit.Before;
4+
import org.junit.Test;
5+
import org.junit.experimental.runners.Enclosed;
6+
import org.junit.runner.RunWith;
7+
8+
import java.util.List;
9+
10+
import static org.hamcrest.MatcherAssert.assertThat;
11+
import static org.hamcrest.Matchers.containsString;
12+
import static org.junit.Assert.*;
13+
14+
@RunWith(Enclosed.class)
15+
public class SettingNullableStringTest {
16+
17+
public static class WithValueConstraintCase{
18+
private static final List<String> POSSIBLE_VALUES = List.of("a", "b", "c");
19+
private SettingString sut;
20+
21+
@Before
22+
public void setUp() throws Exception {
23+
sut = new SettingNullableString("mytext", POSSIBLE_VALUES.iterator().next(), true, POSSIBLE_VALUES);
24+
}
25+
26+
@Test
27+
public void whenSetConstrainedToValueNotPresentInPossibleValuesThenThrowAHelpfulError() {
28+
IllegalArgumentException ex = assertThrows(IllegalArgumentException.class, () -> {
29+
sut.set("d");
30+
});
31+
assertThat(ex.getMessage(), containsString("Invalid value \"mytext: d\""));
32+
}
33+
34+
@Test
35+
public void whenSetConstrainedToValuePresentInPossibleValuesThenSetValue() {
36+
sut.set("a");
37+
38+
assertEquals("a", sut.value());
39+
}
40+
41+
@Test
42+
public void whenSetConstrainedToNullThenSetValue() {
43+
sut.set(null);
44+
45+
assertNull(sut.value());
46+
}
47+
}
48+
49+
public static class WithoutValueConstraintCase {
50+
private SettingString sut;
51+
52+
@Before
53+
public void setUp() throws Exception {
54+
sut = new SettingNullableString("mytext", "foo", true);
55+
}
56+
57+
@Test
58+
public void whenSetUnconstrainedToNonNullValueThenSetValue() {
59+
sut.set("a");
60+
61+
assertEquals("a", sut.value());
62+
}
63+
64+
@Test
65+
public void whenSetUnconstrainedToNullThenSetValue() {
66+
sut.set(null);
67+
68+
assertNull(sut.value());
69+
}
70+
}
71+
}

Diff for: logstash-core/src/test/java/org/logstash/settings/SettingStringTest.java

+56-14
Original file line numberDiff line numberDiff line change
@@ -20,31 +20,73 @@
2020

2121
import org.junit.Before;
2222
import org.junit.Test;
23+
import org.junit.experimental.runners.Enclosed;
24+
import org.junit.runner.RunWith;
2325

2426
import java.util.List;
2527

28+
import static org.hamcrest.MatcherAssert.assertThat;
29+
import static org.hamcrest.Matchers.containsString;
2630
import static org.junit.Assert.assertEquals;
31+
import static org.junit.Assert.assertThrows;
2732

28-
// Mirrored from logstash-core/spec/logstash/settings/string_spec.rb
33+
@RunWith(Enclosed.class)
2934
public class SettingStringTest {
3035

31-
private static final List<String> POSSIBLE_VALUES = List.of("a", "b", "c");
32-
private SettingString sut;
36+
public static class WithValueConstraintCase {
37+
private static final List<String> POSSIBLE_VALUES = List.of("a", "b", "c");
38+
private SettingString sut;
3339

34-
@Before
35-
public void setUp() throws Exception {
36-
sut = new SettingString("mytext", POSSIBLE_VALUES.iterator().next(), true, POSSIBLE_VALUES);
37-
}
40+
@Before
41+
public void setUp() throws Exception {
42+
sut = new SettingString("mytext", POSSIBLE_VALUES.iterator().next(), true, POSSIBLE_VALUES);
43+
}
44+
45+
@Test
46+
public void whenSetValueNotPresentInPossibleValuesThenThrowAHelpfulError() {
47+
IllegalArgumentException ex = assertThrows(IllegalArgumentException.class, () -> {
48+
sut.set("d");
49+
});
50+
assertThat(ex.getMessage(), containsString("Invalid value \"mytext: d\""));
51+
}
52+
53+
@Test
54+
public void whenSetConstrainedToValuePresentInPossibleValuesThenSetValue() {
55+
sut.set("a");
56+
57+
assertEquals("a", sut.value());
58+
}
3859

39-
@Test(expected = IllegalArgumentException.class)
40-
public void whenSetValueNotPresentInPossibleValuesThenThrowAnError() {
41-
sut.set("d");
60+
@Test
61+
public void whenSetConstrainedToNullThenThrowAHelpfulError() {
62+
IllegalArgumentException ex = assertThrows(IllegalArgumentException.class, () -> {
63+
sut.set(null);
64+
});
65+
assertThat(ex.getMessage(), containsString("Setting \"mytext\" must be a String"));
66+
}
4267
}
4368

44-
@Test
45-
public void whenSetValuePresentInPossibleValuesThenSetValue() {
46-
sut.set("a");
69+
public static class WithoutValueConstraintCase {
70+
private SettingString sut;
71+
72+
@Before
73+
public void setUp() throws Exception {
74+
sut = new SettingString("mytext", "foo", true);
75+
}
76+
77+
@Test
78+
public void whenSetUnconstrainedToNonNullValueThenSetValue() {
79+
sut.set("a");
80+
81+
assertEquals("a", sut.value());
82+
}
4783

48-
assertEquals("a", sut.value());
84+
@Test
85+
public void whenSetUnconstrainedToNullThenThrowAHelpfulError() {
86+
IllegalArgumentException ex = assertThrows(IllegalArgumentException.class, () -> {
87+
sut.set(null);
88+
});
89+
assertThat(ex.getMessage(), containsString("Setting \"mytext\" must be a String"));
90+
}
4991
}
5092
}

0 commit comments

Comments
 (0)