Skip to content

Commit 4ee9499

Browse files
committed
fix: now properly handles setting new values for existing keys
1 parent 6b574f4 commit 4ee9499

File tree

5 files changed

+65
-21
lines changed

5 files changed

+65
-21
lines changed

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

Lines changed: 18 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -94,13 +94,12 @@ public String getRaw(String rawKey) {
9494

9595
@Override
9696
public String put(String key, String value) {
97-
String rawKey = escape(key, true);
9897
String rawValue = escape(value, false);
9998
if (values.containsKey(key)) {
100-
int idx = indexOf(key);
101-
addNew(idx, rawKey, key, rawValue, value);
99+
replaceValue(key, rawValue, value);
102100
} else {
103-
addNew(-1, rawKey, key, rawValue, value);
101+
String rawKey = escape(key, true);
102+
addNewKeyValue(rawKey, key, rawValue, value);
104103
}
105104
return values.put(key, value);
106105
}
@@ -117,36 +116,34 @@ public String putRaw(String rawKey, String rawValue) {
117116
String key = unescape(rawKey);
118117
String value = unescape(rawValue);
119118
if (values.containsKey(key)) {
120-
int idx = indexOf(key);
121-
addNew(idx, rawKey, key, rawValue, value);
119+
replaceValue(key, rawValue, value);
122120
} else {
123-
addNew(-1, rawKey, key, rawValue, value);
121+
addNewKeyValue(rawKey, key, rawValue, value);
124122
}
125123
return values.put(key, value);
126124
}
127125

126+
private void replaceValue(String key, String rawValue, String value) {
127+
int idx = indexOf(key);
128+
tokens.remove(idx + 2);
129+
tokens.add(
130+
idx + 2, new PropertiesParser.Token(PropertiesParser.Type.VALUE, rawValue, value));
131+
}
132+
128133
// Add new tokens to the end of the list of tokens
129-
private void addNew(int index, String rawKey, String key, String rawValue, String value) {
134+
private void addNewKeyValue(String rawKey, String key, String rawValue, String value) {
130135
// Add a newline whitespace token if necessary
131-
int idx = index >= 0 ? index : tokens.size();
136+
int idx = tokens.size();
132137
if (idx > 0) {
133138
PropertiesParser.Token token = tokens.get(idx - 1);
134139
if (token.getType() != PropertiesParser.Type.WHITESPACE) {
135-
addToken(index, new PropertiesParser.Token(PropertiesParser.Type.WHITESPACE, "\n"));
140+
tokens.add(new PropertiesParser.Token(PropertiesParser.Type.WHITESPACE, "\n"));
136141
}
137142
}
138143
// Add tokens for key, separator and value
139-
addToken(index, new PropertiesParser.Token(PropertiesParser.Type.KEY, rawKey, key));
140-
addToken(index, new PropertiesParser.Token(PropertiesParser.Type.SEPARATOR, "="));
141-
addToken(index, new PropertiesParser.Token(PropertiesParser.Type.VALUE, rawValue, value));
142-
}
143-
144-
private void addToken(int index, PropertiesParser.Token token) {
145-
if (index >= 0) {
146-
tokens.add(index, token);
147-
} else {
148-
tokens.add(token);
149-
}
144+
tokens.add(new PropertiesParser.Token(PropertiesParser.Type.KEY, rawKey, key));
145+
tokens.add(new PropertiesParser.Token(PropertiesParser.Type.SEPARATOR, "="));
146+
tokens.add(new PropertiesParser.Token(PropertiesParser.Type.VALUE, rawValue, value));
150147
}
151148

152149
@Override

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

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,44 @@ void testPutRaw() throws IOException, URISyntaxException {
196196
assertThat(sw.toString(), equalTo(readAll(getResource("/test-putraw.properties"))));
197197
}
198198

199+
@Test
200+
void testPutReplaceFirst() throws IOException, URISyntaxException {
201+
Properties p = new Properties();
202+
p.put("one", "simple");
203+
p.put("two", "value containing spaces");
204+
p.put("three", "and escapes\n\t\r\f");
205+
p.put("one", "replaced");
206+
StringWriter sw = new StringWriter();
207+
p.store(sw);
208+
assertThat(
209+
sw.toString(), equalTo(readAll(getResource("/test-putreplacefirst.properties"))));
210+
}
211+
212+
@Test
213+
void testPutReplaceMiddle() throws IOException, URISyntaxException {
214+
Properties p = new Properties();
215+
p.put("one", "simple");
216+
p.put("two", "value containing spaces");
217+
p.put("three", "and escapes\n\t\r\f");
218+
p.put("two", "replaced");
219+
StringWriter sw = new StringWriter();
220+
p.store(sw);
221+
assertThat(
222+
sw.toString(), equalTo(readAll(getResource("/test-putreplacemiddle.properties"))));
223+
}
224+
225+
@Test
226+
void testPutReplaceLast() throws IOException, URISyntaxException {
227+
Properties p = new Properties();
228+
p.put("one", "simple");
229+
p.put("two", "value containing spaces");
230+
p.put("three", "and escapes\n\t\r\f");
231+
p.put("three", "replaced");
232+
StringWriter sw = new StringWriter();
233+
p.store(sw);
234+
assertThat(sw.toString(), equalTo(readAll(getResource("/test-putreplacelast.properties"))));
235+
}
236+
199237
@Test
200238
public void testInteropLoad() throws IOException, URISyntaxException {
201239
java.util.Properties p = new java.util.Properties();
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
one=replaced
2+
two=value containing spaces
3+
three=and escapes\n\t\r\f
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
one=simple
2+
two=value containing spaces
3+
three=replaced
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
one=simple
2+
two=replaced
3+
three=and escapes\n\t\r\f

0 commit comments

Comments
 (0)