mirror of
https://github.com/elastic/logstash.git
synced 2025-06-28 09:46:03 -04:00
setting: enforce non-nullable (restore 8.15.x behavior) (#17522)
This commit is contained in:
parent
815fa8be1c
commit
712b37e1df
4 changed files with 135 additions and 14 deletions
|
@ -34,5 +34,10 @@ describe LogStash::Setting::SettingString do
|
||||||
expect(subject.value).to eq("a")
|
expect(subject.value).to eq("a")
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
context "when a null value is given" do
|
||||||
|
it "raises an ArgumentError" do
|
||||||
|
expect { subject.set(nil) }.to raise_error(java.lang.IllegalArgumentException)
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -45,6 +45,9 @@ public class SettingString extends BaseSetting<String> {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void validate(String input) throws IllegalArgumentException {
|
public void validate(String input) throws IllegalArgumentException {
|
||||||
|
if (input == null) {
|
||||||
|
throw new IllegalArgumentException(String.format("Setting \"%s\" must be a String. Received: (NilClass)", this.getName()));
|
||||||
|
}
|
||||||
staticValidate(input, possibleStrings, this.getName());
|
staticValidate(input, possibleStrings, this.getName());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,71 @@
|
||||||
|
package org.logstash.settings;
|
||||||
|
|
||||||
|
import org.junit.Before;
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.junit.experimental.runners.Enclosed;
|
||||||
|
import org.junit.runner.RunWith;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import static org.hamcrest.MatcherAssert.assertThat;
|
||||||
|
import static org.hamcrest.Matchers.containsString;
|
||||||
|
import static org.junit.Assert.*;
|
||||||
|
|
||||||
|
@RunWith(Enclosed.class)
|
||||||
|
public class SettingNullableStringTest {
|
||||||
|
|
||||||
|
public static class WithValueConstraintCase{
|
||||||
|
private static final List<String> POSSIBLE_VALUES = List.of("a", "b", "c");
|
||||||
|
private SettingString sut;
|
||||||
|
|
||||||
|
@Before
|
||||||
|
public void setUp() throws Exception {
|
||||||
|
sut = new SettingNullableString("mytext", POSSIBLE_VALUES.iterator().next(), true, POSSIBLE_VALUES);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenSetConstrainedToValueNotPresentInPossibleValuesThenThrowAHelpfulError() {
|
||||||
|
IllegalArgumentException ex = assertThrows(IllegalArgumentException.class, () -> {
|
||||||
|
sut.set("d");
|
||||||
|
});
|
||||||
|
assertThat(ex.getMessage(), containsString("Invalid value \"mytext: d\""));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenSetConstrainedToValuePresentInPossibleValuesThenSetValue() {
|
||||||
|
sut.set("a");
|
||||||
|
|
||||||
|
assertEquals("a", sut.value());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenSetConstrainedToNullThenSetValue() {
|
||||||
|
sut.set(null);
|
||||||
|
|
||||||
|
assertNull(sut.value());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static class WithoutValueConstraintCase {
|
||||||
|
private SettingString sut;
|
||||||
|
|
||||||
|
@Before
|
||||||
|
public void setUp() throws Exception {
|
||||||
|
sut = new SettingNullableString("mytext", "foo", true);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenSetUnconstrainedToNonNullValueThenSetValue() {
|
||||||
|
sut.set("a");
|
||||||
|
|
||||||
|
assertEquals("a", sut.value());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenSetUnconstrainedToNullThenSetValue() {
|
||||||
|
sut.set(null);
|
||||||
|
|
||||||
|
assertNull(sut.value());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -20,31 +20,73 @@ package org.logstash.settings;
|
||||||
|
|
||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
import org.junit.experimental.runners.Enclosed;
|
||||||
|
import org.junit.runner.RunWith;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
|
import static org.hamcrest.MatcherAssert.assertThat;
|
||||||
|
import static org.hamcrest.Matchers.containsString;
|
||||||
import static org.junit.Assert.assertEquals;
|
import static org.junit.Assert.assertEquals;
|
||||||
|
import static org.junit.Assert.assertThrows;
|
||||||
|
|
||||||
// Mirrored from logstash-core/spec/logstash/settings/string_spec.rb
|
@RunWith(Enclosed.class)
|
||||||
public class SettingStringTest {
|
public class SettingStringTest {
|
||||||
|
|
||||||
private static final List<String> POSSIBLE_VALUES = List.of("a", "b", "c");
|
public static class WithValueConstraintCase {
|
||||||
private SettingString sut;
|
private static final List<String> POSSIBLE_VALUES = List.of("a", "b", "c");
|
||||||
|
private SettingString sut;
|
||||||
|
|
||||||
@Before
|
@Before
|
||||||
public void setUp() throws Exception {
|
public void setUp() throws Exception {
|
||||||
sut = new SettingString("mytext", POSSIBLE_VALUES.iterator().next(), true, POSSIBLE_VALUES);
|
sut = new SettingString("mytext", POSSIBLE_VALUES.iterator().next(), true, POSSIBLE_VALUES);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenSetValueNotPresentInPossibleValuesThenThrowAHelpfulError() {
|
||||||
|
IllegalArgumentException ex = assertThrows(IllegalArgumentException.class, () -> {
|
||||||
|
sut.set("d");
|
||||||
|
});
|
||||||
|
assertThat(ex.getMessage(), containsString("Invalid value \"mytext: d\""));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenSetConstrainedToValuePresentInPossibleValuesThenSetValue() {
|
||||||
|
sut.set("a");
|
||||||
|
|
||||||
|
assertEquals("a", sut.value());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenSetConstrainedToNullThenThrowAHelpfulError() {
|
||||||
|
IllegalArgumentException ex = assertThrows(IllegalArgumentException.class, () -> {
|
||||||
|
sut.set(null);
|
||||||
|
});
|
||||||
|
assertThat(ex.getMessage(), containsString("Setting \"mytext\" must be a String"));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test(expected = IllegalArgumentException.class)
|
public static class WithoutValueConstraintCase {
|
||||||
public void whenSetValueNotPresentInPossibleValuesThenThrowAnError() {
|
private SettingString sut;
|
||||||
sut.set("d");
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
@Before
|
||||||
public void whenSetValuePresentInPossibleValuesThenSetValue() {
|
public void setUp() throws Exception {
|
||||||
sut.set("a");
|
sut = new SettingString("mytext", "foo", true);
|
||||||
|
}
|
||||||
|
|
||||||
assertEquals("a", sut.value());
|
@Test
|
||||||
|
public void whenSetUnconstrainedToNonNullValueThenSetValue() {
|
||||||
|
sut.set("a");
|
||||||
|
|
||||||
|
assertEquals("a", sut.value());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenSetUnconstrainedToNullThenThrowAHelpfulError() {
|
||||||
|
IllegalArgumentException ex = assertThrows(IllegalArgumentException.class, () -> {
|
||||||
|
sut.set(null);
|
||||||
|
});
|
||||||
|
assertThat(ex.getMessage(), containsString("Setting \"mytext\" must be a String"));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
Add table
Add a link
Reference in a new issue