diff --git a/src/main/java/org/codejive/properties/Properties.java b/src/main/java/org/codejive/properties/Properties.java index f887417..f1604aa 100644 --- a/src/main/java/org/codejive/properties/Properties.java +++ b/src/main/java/org/codejive/properties/Properties.java @@ -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 diff --git a/src/test/java/org/codejive/properties/TestProperties.java b/src/test/java/org/codejive/properties/TestProperties.java index f5fa348..d69915c 100644 --- a/src/test/java/org/codejive/properties/TestProperties.java +++ b/src/test/java/org/codejive/properties/TestProperties.java @@ -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)