Skip to content

Commit d583514

Browse files
Fixes when dealing with unusual properties files and other edge-cases.
- Properties#put no longer crashes when the last property is at the end of the file and has no value - Properties#put inserts the correct amount of blank lines when the file contains no properties but some number of comments. - Properties#load no longer swallows the last key if it has no value. - Cursor#add skips the inserted item when there are no tokens (matching the behaviour seen when there are tokens) - Cursor#addToken now uses the correct index when checking Cursor#hasToken. Thus the line is inserted in the expected position, instead of at the end of the file.
1 parent e3f23d6 commit d583514

4 files changed

Lines changed: 231 additions & 9 deletions

File tree

‎src/main/java/org/codejive/properties/Cursor.java‎

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,29 @@ private Cursor(List<PropertiesParser.Token> tokens, int index) {
2424
this.index = index;
2525
}
2626

27+
/**
28+
* @return {@code true} if the Cursor is at the start of the document, before the first token.
29+
*/
2730
public boolean atStart() {
2831
return index < 0;
2932
}
3033

34+
/**
35+
* @return {@code true} if the Cursor is at the end of the document, after the last token.
36+
*/
37+
public boolean atEnd() {
38+
return index >= tokens.size();
39+
}
40+
3141
public int position() {
3242
return index;
3343
}
3444

3545
public boolean hasToken() {
46+
return hasToken(index);
47+
}
48+
49+
private boolean hasToken(int index) {
3650
return index >= 0 && index < tokens.size();
3751
}
3852

@@ -141,17 +155,32 @@ public int prevCount(Predicate<PropertiesParser.Token> accept) {
141155
return cnt;
142156
}
143157

158+
/**
159+
* Inserts a token at the current position, pushing the current token (if any) forwards.
160+
* <br/>
161+
* This method advances the cursor by one token (causing it to point at the initial token again).
162+
* @param token the token to insert.
163+
* @return {@code this}
164+
*/
144165
public Cursor add(PropertiesParser.Token token) {
166+
if (index < 0)
167+
index = 0;
168+
145169
addToken(index++, token);
146170
return this;
147171
}
148172

173+
/**
174+
* Inserts an EOL Token at the current position.
175+
* @see #add(PropertiesParser.Token)
176+
* @return {@code this}
177+
*/
149178
public Cursor addEol() {
150179
return add(PropertiesParser.Token.EOL);
151180
}
152181

153182
private void addToken(int index, PropertiesParser.Token token) {
154-
if (hasToken()) {
183+
if (hasToken(index)) {
155184
tokens.add(index, token);
156185
} else {
157186
tokens.add(token);

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

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -393,27 +393,30 @@ private Cursor addNewKeyValue(String rawKey, String key, String rawValue, String
393393
while (pos.isType(PropertiesParser.Type.WHITESPACE, PropertiesParser.Type.COMMENT)) {
394394
pos.prev();
395395
}
396-
// Make sure we're either at the start or we've found a VALUE
397-
validate(pos.atStart() || pos.isType(PropertiesParser.Type.VALUE), pos);
396+
// Make sure we're either at the start or we've found a property
397+
validate(pos.atStart() || pos.isType(PropertiesParser.Type.VALUE, PropertiesParser.Type.SEPARATOR, PropertiesParser.Type.KEY), pos);
398398
// Add a newline whitespace token if necessary
399399
if (pos.hasToken()) {
400400
pos.next();
401-
if (pos.isEol()) {
402-
pos.next().addEol().prev();
403-
} else {
404-
pos.addEol();
405-
}
401+
pos.addEol();
406402
} else {
407403
// We're at the start, meaning there are no properties yet,
408404
// but there might be comments, so we move forward again,
409405
// skipping any header comments
410406
pos = skipHeaderCommentLines();
411407
if (pos.position() > 0) {
412408
// We have to make sure there are at least 2 EOLs after the last comment
409+
pos.prev(); // move cursor back onto the last eol (otherwise prevCount fails)
413410
int eols = pos.prevCount(t -> t.isEol());
414-
for (int i = 0; i < 2 - eols; i++) {
411+
pos.skip(eols); // return to the position from before 'prevCount' was called
412+
pos.next(); // move cursor past the last eol
413+
int numEolsToAdd = Math.max(0, 2 - eols);
414+
for (int i = 0; i < numEolsToAdd; i++) {
415415
pos.addEol();
416416
}
417+
// if there is another comment following this token, push it to a new line below this property.
418+
if (!pos.atEnd())
419+
pos.addEol().prev();
417420
}
418421
}
419422
// Add tokens for key, separator and value
@@ -858,11 +861,16 @@ private Properties load(List<PropertiesParser.Token> ts) {
858861
String key = null;
859862
for (PropertiesParser.Token token : tokens) {
860863
if (token.type == PropertiesParser.Type.KEY) {
864+
if (key != null)
865+
values.put(key, "");
861866
key = token.getText();
862867
} else if (token.type == PropertiesParser.Type.VALUE) {
863868
values.put(key, token.getText());
869+
key = null;
864870
}
865871
}
872+
if (key != null)
873+
values.put(key, "");
866874
return this;
867875
}
868876

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package org.codejive.properties;
2+
3+
import static org.assertj.core.api.Assertions.*;
4+
5+
import org.junit.jupiter.api.Test;
6+
7+
import java.io.ByteArrayOutputStream;
8+
import java.io.IOException;
9+
import java.io.StringReader;
10+
11+
public class TestCursor {
12+
@Test
13+
void addTokenToEmptyDocument() throws IOException {
14+
Properties p = Properties.loadProperties(new StringReader(""));
15+
Cursor c = p.first();
16+
17+
// expect no tokens to be in the document
18+
assertThat(c.hasToken()).isEqualTo(false);
19+
20+
// expect that add should not throw
21+
PropertiesParser.Token addedToken = new PropertiesParser.Token(PropertiesParser.Type.KEY, "test", "test");
22+
c.add(addedToken);
23+
24+
// expect that add skips the added token
25+
assertThat(c.hasToken()).isEqualTo(false);
26+
c.prev();
27+
assertThat(c.hasToken()).isEqualTo(true);
28+
assertThat(c.token()).isEqualTo(addedToken);
29+
}
30+
31+
@Test
32+
void addTokensToDocument() throws IOException {
33+
Properties p = Properties.loadProperties(new StringReader("key=value"
34+
+ "\nkey2=value2"));
35+
36+
Cursor c = p.first();
37+
c.add(new PropertiesParser.Token(PropertiesParser.Type.COMMENT, "# beginning"));
38+
c.addEol();
39+
40+
c = p.indexOf("key2");
41+
c.add(new PropertiesParser.Token(PropertiesParser.Type.COMMENT, "# middle"));
42+
c.addEol();
43+
44+
c = p.last();
45+
c.next();
46+
c.addEol();
47+
c.add(new PropertiesParser.Token(PropertiesParser.Type.COMMENT, "# end"));
48+
49+
ByteArrayOutputStream os = new ByteArrayOutputStream();
50+
p.store(os);
51+
assertThat(os.toString()).isEqualTo("# beginning" +
52+
"\nkey=value" +
53+
"\n# middle" +
54+
"\nkey2=value2" +
55+
"\n# end");
56+
}
57+
}

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

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -469,6 +469,55 @@ void testPutFirstWithHeader() throws IOException, URISyntaxException {
469469
}
470470
}
471471

472+
@Test
473+
void testPutFirstWithHeader1Eol() throws IOException, URISyntaxException {
474+
try (StringReader sr = new StringReader("# A header comment\n")) {
475+
Properties p = Properties.loadProperties(sr);
476+
p.put("first", "dummy");
477+
StringWriter sw = new StringWriter();
478+
p.store(sw);
479+
assertThat(sw.toString())
480+
.isEqualTo(readAll(getResource("/test-putfirstwithheader.properties")));
481+
}
482+
}
483+
484+
@Test
485+
void testPutFirstWithHeader2Eol() throws IOException, URISyntaxException {
486+
try (StringReader sr = new StringReader("# A header comment\n\n")) {
487+
Properties p = Properties.loadProperties(sr);
488+
p.put("first", "dummy");
489+
StringWriter sw = new StringWriter();
490+
p.store(sw);
491+
assertThat(sw.toString())
492+
.isEqualTo(readAll(getResource("/test-putfirstwithheader.properties")));
493+
}
494+
}
495+
496+
@Test
497+
void testPutFirstWithHeader3Eol() throws IOException, URISyntaxException {
498+
String expected = "# A header comment\n\n\nfirst=dummy";
499+
try (StringReader sr = new StringReader("# A header comment\n\n\n")) {
500+
Properties p = Properties.loadProperties(sr);
501+
p.put("first", "dummy");
502+
StringWriter sw = new StringWriter();
503+
p.store(sw);
504+
assertThat(sw.toString()).isEqualTo(expected);
505+
}
506+
}
507+
508+
@Test
509+
void testPutFirstWithHeaderAndTrailer() throws IOException, URISyntaxException {
510+
String given = "# A header comment\n\n\n# A trailer\n";
511+
String expected = "# A header comment\n\n\nfirst=dummy\n# A trailer\n";
512+
try (StringReader sr = new StringReader(given)) {
513+
Properties p = Properties.loadProperties(sr);
514+
p.put("first", "dummy");
515+
StringWriter sw = new StringWriter();
516+
p.store(sw);
517+
assertThat(sw.toString()).isEqualTo(expected);
518+
}
519+
}
520+
472521
@Test
473522
void testPutNull() throws IOException, URISyntaxException {
474523
Properties p = new Properties();
@@ -715,6 +764,85 @@ void testPutAll() {
715764
assertThat(p.getProperty("foo")).isEqualTo("bar");
716765
}
717766

767+
@Test
768+
void testLoadEmptyValue() throws IOException {
769+
String document = "firstline=\n" +
770+
"secondline=";
771+
Properties p = Properties.loadProperties(new StringReader(document));
772+
java.util.Properties ju = new java.util.Properties();
773+
ju.load(new StringReader(document));
774+
assertThat(p.asJUProperties()).isEqualTo(ju);
775+
776+
// also verify that put works
777+
p.put("thirdline", "");
778+
ju.put("thirdline", "");
779+
assertThat(p.asJUProperties()).isEqualTo(ju);
780+
781+
// verify store
782+
StringWriter sw = new StringWriter();
783+
p.store(sw);
784+
assertThat(sw.toString()).isEqualTo("firstline=\n" +
785+
"secondline=\n" +
786+
"thirdline=");
787+
}
788+
789+
@Test
790+
void testLoadMissingSeparator() throws IOException {
791+
String document = "firstline\n" +
792+
"secondline";
793+
Properties p = Properties.loadProperties(new StringReader(document));
794+
java.util.Properties ju = new java.util.Properties();
795+
ju.load(new StringReader(document));
796+
assertThat(p.asJUProperties()).isEqualTo(ju);
797+
798+
// also verify that put works
799+
p.put("thirdline", "");
800+
ju.put("thirdline", "");
801+
assertThat(p.asJUProperties()).isEqualTo(ju);
802+
803+
// verify store
804+
StringWriter sw = new StringWriter();
805+
p.store(sw);
806+
assertThat(sw.toString()).isEqualTo("firstline\n" +
807+
"secondline\n" +
808+
"thirdline=");
809+
}
810+
811+
@Test
812+
void testPutTrailingSpace() throws IOException {
813+
String document = "foo=x \n";
814+
Properties p = Properties.loadProperties(new StringReader(document));
815+
p.put("bar", "");
816+
StringWriter sw = new StringWriter();
817+
p.store(sw);
818+
assertThat(sw.toString()).isEqualTo("foo=x \n" +
819+
"bar=\n");
820+
}
821+
822+
@Test
823+
void testPutMissingSeparatorTrailingSpace() throws IOException {
824+
String document = "foo \n";
825+
Properties p = Properties.loadProperties(new StringReader(document));
826+
p.put("bar", "");
827+
StringWriter sw = new StringWriter();
828+
p.store(sw);
829+
assertThat(sw.toString()).isEqualTo("foo \n" +
830+
"bar=\n");
831+
}
832+
833+
@Test
834+
void testPutMissingSeparatorTrailingComment() throws IOException {
835+
String document = "foo\n" +
836+
"# trailer";
837+
Properties p = Properties.loadProperties(new StringReader(document));
838+
p.put("bar", "");
839+
StringWriter sw = new StringWriter();
840+
p.store(sw);
841+
assertThat(sw.toString()).isEqualTo("foo\n" +
842+
"bar=\n" +
843+
"# trailer");
844+
}
845+
718846
private Path getResource(String name) throws URISyntaxException {
719847
return Paths.get(getClass().getResource(name).toURI());
720848
}

0 commit comments

Comments
 (0)