Skip to content

Commit b099ad0

Browse files
committed
test: Testing for null being passed to put()
1 parent 088becc commit b099ad0

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

src/main/java/org/codejive/properties/Properties.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,9 @@ public String getRaw(String rawKey) {
293293

294294
@Override
295295
public String put(String key, String value) {
296+
if (key == null || value == null) {
297+
throw new NullPointerException();
298+
}
296299
String rawValue = escape(value, false);
297300
if (values.containsKey(key)) {
298301
replaceValue(key, rawValue, value);

src/test/java/org/codejive/properties/TestProperties.java

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
import org.junit.jupiter.api.Test;
1313

1414
public class TestProperties {
15-
1615
@Test
1716
void testLoad() throws IOException, URISyntaxException {
1817
Properties p = Properties.loadProperties(getResource("/test.properties"));
@@ -303,6 +302,23 @@ void testPutFirstWithHeader() throws IOException, URISyntaxException {
303302
}
304303
}
305304

305+
@Test
306+
void testPutNull() throws IOException, URISyntaxException {
307+
Properties p = new Properties();
308+
assertThatThrownBy(() -> {
309+
p.put("one", null);
310+
}).isInstanceOf(NullPointerException.class);
311+
assertThatThrownBy(() -> {
312+
p.setProperty("one", null);
313+
}).isInstanceOf(NullPointerException.class);
314+
assertThatThrownBy(() -> {
315+
p.put(null, "value");
316+
}).isInstanceOf(NullPointerException.class);
317+
assertThatThrownBy(() -> {
318+
p.setProperty(null, "value");
319+
}).isInstanceOf(NullPointerException.class);
320+
}
321+
306322
@Test
307323
void testRemoveFirst() throws IOException, URISyntaxException {
308324
Properties p = Properties.loadProperties(getResource("/test.properties"));

0 commit comments

Comments
 (0)