From a54308000f0b50052fd928a1cde0e814e09d5d10 Mon Sep 17 00:00:00 2001 From: Tako Schotanus Date: Wed, 30 Sep 2026 13:19:55 +0200 Subject: [PATCH] fix: replaceValue no longer crashes Fixes #82 --- .../java/org/codejive/properties/Properties.java | 13 ++++++++++--- .../org/codejive/properties/TestProperties.java | 15 +++++++++++++++ 2 files changed, 25 insertions(+), 3 deletions(-) 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)