Skip to content

Commit 183ad48

Browse files
committed
fix: some methods were not checking for valid keys
This is now fixed and tests are added for those situations.
1 parent bf37676 commit 183ad48

File tree

2 files changed

+55
-4
lines changed

2 files changed

+55
-4
lines changed

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

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -426,10 +426,15 @@ private Cursor addNewKeyValue(String rawKey, String key, String rawValue, String
426426
@Override
427427
public String remove(Object key) {
428428
String skey = key.toString();
429-
removeItem(skey);
430-
return values.remove(skey);
429+
if (containsKey(key)) {
430+
removeItem(skey);
431+
return values.remove(skey);
432+
} else {
433+
return null;
434+
}
431435
}
432436

437+
// Calling code MUST make sure skey exists!
433438
private void removeItem(String skey) {
434439
setComment(skey, Collections.emptyList());
435440
Cursor pos = indexOf(skey);
@@ -580,7 +585,11 @@ private String getPrefix(String comment) {
580585

581586
private List<Integer> findPropertyCommentLines(String key) {
582587
Cursor pos = indexOf(key);
583-
return findPropertyCommentLines(pos);
588+
if (pos.hasToken()) {
589+
return findPropertyCommentLines(pos);
590+
} else {
591+
return Collections.emptyList();
592+
}
584593
}
585594

586595
/**

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

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import java.util.AbstractMap;
1111
import java.util.Collections;
1212
import java.util.Iterator;
13+
import java.util.NoSuchElementException;
1314
import org.junit.jupiter.api.Test;
1415

1516
public class TestProperties {
@@ -197,6 +198,24 @@ void testGetRaw() throws IOException, URISyntaxException {
197198
assertThat(p.getRaw("key.4")).isEqualTo("\\u1234\u1234");
198199
}
199200

201+
@Test
202+
void testGetNonExistent() throws IOException, URISyntaxException {
203+
Properties p = Properties.loadProperties(getResource("/test.properties"));
204+
assertThat(p.get("wrong")).isNull();
205+
}
206+
207+
@Test
208+
void testGetPropertyNonExistent() throws IOException, URISyntaxException {
209+
Properties p = Properties.loadProperties(getResource("/test.properties"));
210+
assertThat(p.getProperty("wrong")).isNull();
211+
}
212+
213+
@Test
214+
void testGetPropertyDefault() throws IOException, URISyntaxException {
215+
Properties p = Properties.loadProperties(getResource("/test.properties"));
216+
assertThat(p.getProperty("wrong", "right")).isEqualTo("right");
217+
}
218+
200219
@Test
201220
void testGetComment() throws IOException, URISyntaxException {
202221
Properties p = Properties.loadProperties(getResource("/test.properties"));
@@ -206,13 +225,20 @@ void testGetComment() throws IOException, URISyntaxException {
206225
.containsExactly("# another comment", "! and a comment", "! block");
207226
}
208227

228+
@Test
229+
void testGetCommentNonExistent() throws IOException, URISyntaxException {
230+
Properties p = Properties.loadProperties(getResource("/test.properties"));
231+
assertThat(p.getComment("wrong")).isEmpty();
232+
}
233+
209234
@Test
210235
void testCommentFirstLine() throws IOException, URISyntaxException {
211236
Properties p = Properties.loadProperties(getResource("/test-commentfirstline.properties"));
212237
assertThat(p.getComment("one")).containsExactly("#comment1", "# comment2");
213238
StringWriter sw = new StringWriter();
214239
p.store(sw);
215-
assertThat(sw.toString()).isEqualTo(readAll(getResource("/test-commentfirstline.properties")));
240+
assertThat(sw.toString())
241+
.isEqualTo(readAll(getResource("/test-commentfirstline.properties")));
216242
}
217243

218244
@Test
@@ -229,6 +255,16 @@ void testSetComment() throws IOException, URISyntaxException {
229255
assertThat(sw.toString()).isEqualTo(readAll(getResource("/test-comment.properties")));
230256
}
231257

258+
@Test
259+
void testSetCommentNonExistent() throws IOException, URISyntaxException {
260+
Properties p = Properties.loadProperties(getResource("/test.properties"));
261+
assertThatThrownBy(
262+
() -> {
263+
p.setComment("wrong", "dummy");
264+
})
265+
.isInstanceOf(NoSuchElementException.class);
266+
}
267+
232268
@Test
233269
void testPut() throws IOException, URISyntaxException {
234270
Properties p = new Properties();
@@ -499,6 +535,12 @@ void testRemoveAll() throws IOException, URISyntaxException {
499535
assertThat(sw.toString()).isEqualTo(readAll(getResource("/test-removeall.properties")));
500536
}
501537

538+
@Test
539+
void testRemoveNonExistent() throws IOException, URISyntaxException {
540+
Properties p = Properties.loadProperties(getResource("/test.properties"));
541+
assertThat(p.remove("wrong")).isNull();
542+
}
543+
502544
@Test
503545
void testRemoveMiddleIterator() throws IOException, URISyntaxException {
504546
Properties p = Properties.loadProperties(getResource("/test.properties"));

0 commit comments

Comments
 (0)