setting: enforce non-nullable (restore 8.15.x behavior) (#17522)

This commit is contained in:
Rye Biesemeyer 2025-04-09 09:28:38 -07:00 committed by GitHub
parent 815fa8be1c
commit 712b37e1df
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 135 additions and 14 deletions

View file

@ -34,5 +34,10 @@ describe LogStash::Setting::SettingString do
expect(subject.value).to eq("a")
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

View file

@ -45,6 +45,9 @@ public class SettingString extends BaseSetting<String> {
@Override
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());
}

View file

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

View file

@ -20,31 +20,73 @@ 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.assertEquals;
import static org.junit.Assert.assertThrows;
// Mirrored from logstash-core/spec/logstash/settings/string_spec.rb
@RunWith(Enclosed.class)
public class SettingStringTest {
private static final List<String> POSSIBLE_VALUES = List.of("a", "b", "c");
private SettingString sut;
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 SettingString("mytext", POSSIBLE_VALUES.iterator().next(), true, POSSIBLE_VALUES);
@Before
public void setUp() throws Exception {
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 void whenSetValueNotPresentInPossibleValuesThenThrowAnError() {
sut.set("d");
}
public static class WithoutValueConstraintCase {
private SettingString sut;
@Test
public void whenSetValuePresentInPossibleValuesThenSetValue() {
sut.set("a");
@Before
public void setUp() throws Exception {
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"));
}
}
}