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
13 changes: 10 additions & 3 deletions src/main/java/org/codejive/properties/Properties.java
Original file line number Diff line number Diff line change
Expand Up @@ -441,9 +441,16 @@ public String putRaw(String rawKey, String rawValue) {
private void replaceValue(String key, String rawValue, String value) {
Cursor pos = indexOf(key);
validate(pos.nextIf(PropertiesParser.Type.KEY), pos);
validate(pos.nextIf(PropertiesParser.Type.SEPARATOR), pos);
validate(pos.isType(PropertiesParser.Type.VALUE), pos);
pos.replace(new PropertiesParser.Token(PropertiesParser.Type.VALUE, rawValue, value));
if (!pos.isType(PropertiesParser.Type.SEPARATOR)) {
pos.add(new PropertiesParser.Token(PropertiesParser.Type.SEPARATOR, "="));
} else {
pos.next();
}
if (pos.isType(PropertiesParser.Type.VALUE)) {
pos.replace(new PropertiesParser.Token(PropertiesParser.Type.VALUE, rawValue, value));
} else {
pos.add(new PropertiesParser.Token(PropertiesParser.Type.VALUE, rawValue, value));
}
}

// Add new tokens to the end of the list of tokens
Expand Down
15 changes: 15 additions & 0 deletions src/test/java/org/codejive/properties/TestProperties.java
Original file line number Diff line number Diff line change
Expand Up @@ -1159,6 +1159,21 @@ void testPutCrLf() throws IOException {
assertThat(sw.toString()).isEqualTo(expected);
}

@Test
void testPutReplaceMissingSepOrValue() throws IOException, URISyntaxException {
final String given = "key1=val1\n" + "key2=\n" + "key3";
final String expected = "key1=value1\n" + "key2=value2\n" + "key3=value3";

Properties p = Properties.loadProperties(new StringReader(given));
p.put("key1", "value1");
p.put("key2", "value2");
p.put("key3", "value3");

StringWriter sw = new StringWriter();
p.store(sw);
assertThat(sw.toString()).isEqualTo(expected);
}

private Path getResource(String name) throws URISyntaxException {
URL resource = getClass().getResource(name);
if (resource == null)
Expand Down
Loading