Skip to content

Commit 7de7d02

Browse files
committed
Fix Properties.store(OutputStream os, String comment)
This fixes the store method taking an `OutputStream` by flushing and closing the writer created to wrap it. It also introduces ApprovalTests (https://approvaltests.com/) to open a diff window when comparing Strings and File contents
1 parent 2fd8468 commit 7de7d02

File tree

5 files changed

+54
-2
lines changed

5 files changed

+54
-2
lines changed

pom.xml

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
<assertj.version>3.21.0</assertj.version>
3535
<spotless.version>2.12.2</spotless.version>
3636
<google-java-format.version>1.7</google-java-format.version>
37+
<approvaltests.version>24.2.0</approvaltests.version>
3738
</properties>
3839

3940
<dependencies>
@@ -49,6 +50,12 @@
4950
<version>${assertj.version}</version>
5051
<scope>test</scope>
5152
</dependency>
53+
<dependency>
54+
<groupId>com.approvaltests</groupId>
55+
<artifactId>approvaltests</artifactId>
56+
<version>${approvaltests.version}</version>
57+
<scope>test</scope>
58+
</dependency>
5259
</dependencies>
5360

5461
<build>
@@ -66,6 +73,9 @@
6673
<include>.gitignore</include>
6774
<include>.gitattributes</include>
6875
</includes>
76+
<excludes>
77+
<exclude>src/test/approvals/**</exclude>
78+
</excludes>
6979
<trimTrailingWhitespace />
7080
<endWithNewline />
7181
<indent>
@@ -187,4 +197,4 @@
187197
<url>https://s01.oss.sonatype.org/service/local/staging/deploy/maven2/</url>
188198
</repository>
189199
</distributionManagement>
190-
</project>
200+
</project>

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -910,7 +910,9 @@ public void store(Path file, String... comment) throws IOException {
910910
* @throws IOException Thrown when any IO error occurs during operation
911911
*/
912912
public void store(OutputStream out, String... comment) throws IOException {
913-
store(new OutputStreamWriter(out, StandardCharsets.ISO_8859_1), comment);
913+
store(
914+
new BufferedWriter(new OutputStreamWriter(out, StandardCharsets.ISO_8859_1)),
915+
comment);
914916
}
915917

916918
/**
@@ -938,6 +940,7 @@ public void store(Writer writer, String... comment) throws IOException {
938940
writer.write(pos.raw());
939941
pos.next();
940942
}
943+
writer.flush();
941944
}
942945

943946
/**
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#comment1
2+
# comment2
3+
4+
! comment3
5+
one=simple
6+
two=value containing spaces
7+
# another comment
8+
! and a comment
9+
! block
10+
three=and escapes\n\t\r\f
11+
\ with\ spaces = everywhere
12+
altsep:value
13+
multiline = one \
14+
two \
15+
three
16+
key.4 = \u1234\u1234
17+
# final comment
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package org.codejive.properties;
2+
3+
/**
4+
* This class is used by the ApprovalTests framework
5+
*
6+
* @see <a
7+
* href="https://github.com/approvals/ApprovalTests.Java/blob/master/approvaltests/docs/Configuration.md#package-level-settings">ApprovalTests
8+
* Configuration</a>
9+
*/
10+
public class PackageSettings {
11+
public static String ApprovalBaseDirectory = "../approvals";
12+
}

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import java.util.Collections;
1212
import java.util.Iterator;
1313
import java.util.NoSuchElementException;
14+
import org.approvaltests.Approvals;
1415
import org.junit.jupiter.api.Test;
1516

1617
public class TestProperties {
@@ -118,6 +119,15 @@ void testStore() throws IOException, URISyntaxException {
118119
assertThat(sw.toString()).isEqualTo(readAll(f));
119120
}
120121

122+
@Test
123+
void testStoreOutputStream() throws IOException, URISyntaxException {
124+
Path f = getResource("/test-escaped.properties");
125+
Properties p = Properties.loadProperties(f);
126+
ByteArrayOutputStream os = new ByteArrayOutputStream();
127+
p.store(os);
128+
Approvals.verify(os.toString());
129+
}
130+
121131
@Test
122132
void testStoreCrLf() throws IOException, URISyntaxException {
123133
Path f = getResource("/testcrlf.properties");

0 commit comments

Comments
 (0)