Skip to content

Commit cdc9fc7

Browse files
committed
fix: adding properties after partial properties now works
This was thirowing an illegal state exception because the code assumed that any previous property had to end in a VALUE. But this didn't take into account partial properties that either don't have a value or don't even have a separator (=).
1 parent c48b6cc commit cdc9fc7

6 files changed

Lines changed: 100 additions & 2 deletions

File tree

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

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -393,8 +393,14 @@ private Cursor addNewKeyValue(String rawKey, String key, String rawValue, String
393393
while (pos.isType(PropertiesParser.Type.WHITESPACE, PropertiesParser.Type.COMMENT)) {
394394
pos.prev();
395395
}
396-
// Make sure we're either at the start or we've found a VALUE
397-
validate(pos.atStart() || pos.isType(PropertiesParser.Type.VALUE), pos);
396+
// Make sure we're either at the start or we've found a property
397+
validate(
398+
pos.atStart()
399+
|| pos.isType(
400+
PropertiesParser.Type.VALUE,
401+
PropertiesParser.Type.SEPARATOR,
402+
PropertiesParser.Type.KEY),
403+
pos);
398404
// Add a newline whitespace token if necessary
399405
if (pos.hasToken()) {
400406
pos.next();

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

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -510,6 +510,28 @@ void testPutNewTest3() throws IOException, URISyntaxException {
510510
assertThat(sw.toString()).isEqualTo(readAll(getResource("/test3-putnew.properties")));
511511
}
512512

513+
@Test
514+
void testPutNewAfterKey() throws IOException, URISyntaxException {
515+
Path f = getResource("/test-keyatend.properties");
516+
Properties p = Properties.loadProperties(f);
517+
p.put("five", "5");
518+
StringWriter sw = new StringWriter();
519+
p.store(sw);
520+
assertThat(sw.toString())
521+
.isEqualTo(readAll(getResource("/test-keyatend-putnew.properties")));
522+
}
523+
524+
@Test
525+
void testPutNewAfterSeparator() throws IOException, URISyntaxException {
526+
Path f = getResource("/test-separatoratend.properties");
527+
Properties p = Properties.loadProperties(f);
528+
p.put("five", "5");
529+
StringWriter sw = new StringWriter();
530+
p.store(sw);
531+
assertThat(sw.toString())
532+
.isEqualTo(readAll(getResource("/test-separatoratend-putnew.properties")));
533+
}
534+
513535
@Test
514536
void testPutFirstWithHeader() throws IOException, URISyntaxException {
515537
try (StringReader sr = new StringReader("# A header comment")) {
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#comment1
2+
# comment2
3+
4+
! comment3
5+
one=simple
6+
two=value containing spaces
7+
# another comment
8+
! and a comment
9+
! block
10+
three=and escapes\n\t\r\f
11+
\ with\ spaces = everywhere
12+
altsep:value
13+
multiline = one \
14+
two \
15+
three
16+
key.4 = \u1234ሴ
17+
key.at.end
18+
five=5
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#comment1
2+
# comment2
3+
4+
! comment3
5+
one=simple
6+
two=value containing spaces
7+
# another comment
8+
! and a comment
9+
! block
10+
three=and escapes\n\t\r\f
11+
\ with\ spaces = everywhere
12+
altsep:value
13+
multiline = one \
14+
two \
15+
three
16+
key.4 = \u1234ሴ
17+
key.at.end
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#comment1
2+
# comment2
3+
4+
! comment3
5+
one=simple
6+
two=value containing spaces
7+
# another comment
8+
! and a comment
9+
! block
10+
three=and escapes\n\t\r\f
11+
\ with\ spaces = everywhere
12+
altsep:value
13+
multiline = one \
14+
two \
15+
three
16+
key.4 = \u1234ሴ
17+
separator.at.end=
18+
five=5
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#comment1
2+
# comment2
3+
4+
! comment3
5+
one=simple
6+
two=value containing spaces
7+
# another comment
8+
! and a comment
9+
! block
10+
three=and escapes\n\t\r\f
11+
\ with\ spaces = everywhere
12+
altsep:value
13+
multiline = one \
14+
two \
15+
three
16+
key.4 = \u1234ሴ
17+
separator.at.end=

0 commit comments

Comments
 (0)