Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions src/main/java/org/codejive/properties/Cursor.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,11 @@ public int position() {
}

public boolean hasToken() {
return index >= 0 && index < tokens.size();
return hasToken(index);
}

private boolean hasToken(int idx) {
return idx >= 0 && idx < tokens.size();
}

public PropertiesParser.Token token() {
Expand Down Expand Up @@ -142,6 +146,7 @@ public int prevCount(Predicate<PropertiesParser.Token> accept) {
}

public Cursor add(PropertiesParser.Token token) {
index = Math.max(index, 0);
addToken(index++, token);
return this;
}
Expand All @@ -151,7 +156,7 @@ public Cursor addEol() {
}

private void addToken(int index, PropertiesParser.Token token) {
if (hasToken()) {
if (hasToken(index)) {
tokens.add(index, token);
} else {
tokens.add(token);
Expand Down
10 changes: 8 additions & 2 deletions src/main/java/org/codejive/properties/Properties.java
Original file line number Diff line number Diff line change
Expand Up @@ -393,8 +393,14 @@ private Cursor addNewKeyValue(String rawKey, String key, String rawValue, String
while (pos.isType(PropertiesParser.Type.WHITESPACE, PropertiesParser.Type.COMMENT)) {
pos.prev();
}
// Make sure we're either at the start or we've found a VALUE
validate(pos.atStart() || pos.isType(PropertiesParser.Type.VALUE), pos);
// Make sure we're either at the start or we've found a property
validate(
pos.atStart()
|| pos.isType(
PropertiesParser.Type.VALUE,
PropertiesParser.Type.SEPARATOR,
PropertiesParser.Type.KEY),
pos);
// Add a newline whitespace token if necessary
if (pos.hasToken()) {
pos.next();
Expand Down
51 changes: 51 additions & 0 deletions src/test/java/org/codejive/properties/TestProperties.java
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,15 @@ void testStoreHeaderCrLf() throws IOException, URISyntaxException {
.isEqualTo(readAll(getResource("/testcrlf-storeheader.properties")));
}

@Test
void testStoreTest2() throws IOException, URISyntaxException {
Path f = getResource("/test2.properties");
Properties p = Properties.loadProperties(f);
StringWriter sw = new StringWriter();
p.store(sw);
assertThat(sw.toString()).isEqualTo(readAll(f));
}

@Test
void testLf() throws IOException, URISyntaxException {
Path f = getResource("/test.properties");
Expand Down Expand Up @@ -481,6 +490,48 @@ void testPutNew() throws IOException, URISyntaxException {
assertThat(sw.toString()).isEqualTo(readAll(getResource("/test-putnew.properties")));
}

@Test
void testPutNewTest2() throws IOException, URISyntaxException {
Path f = getResource("/test2.properties");
Properties p = Properties.loadProperties(f);
p.put("five", "5");
StringWriter sw = new StringWriter();
p.store(sw);
assertThat(sw.toString()).isEqualTo(readAll(getResource("/test2-putnew.properties")));
}

@Test
void testPutNewTest3() throws IOException, URISyntaxException {
Path f = getResource("/test3.properties");
Properties p = Properties.loadProperties(f);
p.put("five", "5");
StringWriter sw = new StringWriter();
p.store(sw);
assertThat(sw.toString()).isEqualTo(readAll(getResource("/test3-putnew.properties")));
}

@Test
void testPutNewAfterKey() throws IOException, URISyntaxException {
Path f = getResource("/test-keyatend.properties");
Properties p = Properties.loadProperties(f);
p.put("five", "5");
StringWriter sw = new StringWriter();
p.store(sw);
assertThat(sw.toString())
.isEqualTo(readAll(getResource("/test-keyatend-putnew.properties")));
}

@Test
void testPutNewAfterSeparator() throws IOException, URISyntaxException {
Path f = getResource("/test-separatoratend.properties");
Properties p = Properties.loadProperties(f);
p.put("five", "5");
StringWriter sw = new StringWriter();
p.store(sw);
assertThat(sw.toString())
.isEqualTo(readAll(getResource("/test-separatoratend-putnew.properties")));
}

@Test
void testPutFirstWithHeader() throws IOException, URISyntaxException {
try (StringReader sr = new StringReader("# A header comment")) {
Expand Down
1 change: 0 additions & 1 deletion src/test/resources/test-comment.properties
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,3 @@ multiline = one \
two \
three
key.4 = \u1234ሴ
# final comment
1 change: 0 additions & 1 deletion src/test/resources/test-escaped.properties
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,3 @@ multiline = one \
two \
three
key.4 = \u1234\u1234
# final comment
18 changes: 18 additions & 0 deletions src/test/resources/test-keyatend-putnew.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#comment1
# comment2

! comment3
one=simple
two=value containing spaces
# another comment
! and a comment
! block
three=and escapes\n\t\r\f
\ with\ spaces = everywhere
altsep:value
multiline = one \
two \
three
key.4 = \u1234ሴ
key.at.end
five=5
17 changes: 17 additions & 0 deletions src/test/resources/test-keyatend.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#comment1
# comment2

! comment3
one=simple
two=value containing spaces
# another comment
! and a comment
! block
three=and escapes\n\t\r\f
\ with\ spaces = everywhere
altsep:value
multiline = one \
two \
three
key.4 = \u1234ሴ
key.at.end
1 change: 0 additions & 1 deletion src/test/resources/test-putnew.properties
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,3 @@ multiline = one \
three
key.4 = \u1234ሴ
five=5
# final comment
1 change: 0 additions & 1 deletion src/test/resources/test-removeall.properties
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#comment1
# comment2

# final comment
1 change: 0 additions & 1 deletion src/test/resources/test-removecomment.properties
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,3 @@ multiline = one \
two \
three
key.4 = \u1234ሴ
# final comment
1 change: 0 additions & 1 deletion src/test/resources/test-removefirst.properties
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,3 @@ multiline = one \
two \
three
key.4 = \u1234ሴ
# final comment
1 change: 0 additions & 1 deletion src/test/resources/test-removelast.properties
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,3 @@ altsep:value
multiline = one \
two \
three
# final comment
1 change: 0 additions & 1 deletion src/test/resources/test-removemiddle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,3 @@ multiline = one \
two \
three
key.4 = \u1234ሴ
# final comment
18 changes: 18 additions & 0 deletions src/test/resources/test-separatoratend-putnew.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#comment1
# comment2

! comment3
one=simple
two=value containing spaces
# another comment
! and a comment
! block
three=and escapes\n\t\r\f
\ with\ spaces = everywhere
altsep:value
multiline = one \
two \
three
key.4 = \u1234ሴ
separator.at.end=
five=5
17 changes: 17 additions & 0 deletions src/test/resources/test-separatoratend.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#comment1
# comment2

! comment3
one=simple
two=value containing spaces
# another comment
! and a comment
! block
three=and escapes\n\t\r\f
\ with\ spaces = everywhere
altsep:value
multiline = one \
two \
three
key.4 = \u1234ሴ
separator.at.end=
1 change: 0 additions & 1 deletion src/test/resources/test-storeheader.properties
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,3 @@ multiline = one \
two \
three
key.4 = \u1234ሴ
# final comment
1 change: 0 additions & 1 deletion src/test/resources/test-unescaped.properties
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,3 @@ multiline = one \
two \
three
key.4 = ሴሴ
# final comment
1 change: 0 additions & 1 deletion src/test/resources/test.properties
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,3 @@ multiline = one \
two \
three
key.4 = \u1234ሴ
# final comment
18 changes: 18 additions & 0 deletions src/test/resources/test2-putnew.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#comment1
# comment2

! comment3
one=simple
two=value containing spaces
# another comment
! and a comment
! block
three=and escapes\n\t\r\f
\ with\ spaces = everywhere
altsep:value
multiline = one \
two \
three
key.4 = \u1234ሴ
five=5
# final comment
17 changes: 17 additions & 0 deletions src/test/resources/test2.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#comment1
# comment2

! comment3
one=simple
two=value containing spaces
# another comment
! and a comment
! block
three=and escapes\n\t\r\f
\ with\ spaces = everywhere
altsep:value
multiline = one \
two \
three
key.4 = \u1234ሴ
# final comment
17 changes: 17 additions & 0 deletions src/test/resources/test3-putnew.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#comment1
# comment2

! comment3
one=simple
two=value containing spaces
# another comment
! and a comment
! block
three=and escapes\n\t\r\f
\ with\ spaces = everywhere
altsep:value
multiline = one \
two \
three
key.4 = \u1234ሴ
five=5
16 changes: 16 additions & 0 deletions src/test/resources/test3.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#comment1
# comment2

! comment3
one=simple
two=value containing spaces
# another comment
! and a comment
! block
three=and escapes\n\t\r\f
\ with\ spaces = everywhere
altsep:value
multiline = one \
two \
three
key.4 = \u1234ሴ
1 change: 0 additions & 1 deletion src/test/resources/testcrlf-storeheader.properties
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,3 @@ multiline = one \
two \
three
key.4 = \u1234ሴ
# final comment
1 change: 0 additions & 1 deletion src/test/resources/testcrlf.properties
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,3 @@ multiline = one \
two \
three
key.4 = \u1234ሴ
# final comment
Loading