@@ -53,21 +53,44 @@ public final class ObjectFileWriterTest
53
53
@ Before
54
54
public void setup () throws IOException {
55
55
file = tempFolder .newFile ();
56
- writer = new ObjectFileWriter < String >( file . getAbsolutePath () );
56
+ setWriter ( );
57
57
}
58
58
59
59
@ Test
60
60
public void shouldWriteUTF8EncodedOutput () throws IOException {
61
61
assumeFalse ("Default encoding is UTF-8: It is not possible to test whether " +
62
- "ObjectFileWriter sets the encoding to UTF-8 correctly." ,
62
+ "ObjectFileWriter sets the encoding to UTF-8 correctly." ,
63
63
StandardCharsets .UTF_8 .equals (Charset .defaultCharset ()));
64
64
65
65
writer .process (DATA );
66
66
writer .closeStream ();
67
67
68
- final byte [] bytesWritten = Files .readAllBytes (file .toPath ());
69
- assertArrayEquals ((DATA + "\n " ).getBytes (StandardCharsets .UTF_8 ),
70
- bytesWritten ); // FileObjectWriter appends new lines
68
+ assertOutput (DATA + "\n " );
69
+ }
70
+
71
+ @ Test
72
+ public void shouldOverwriteExistingFileByDefault () throws IOException {
73
+ writer .process (DATA );
74
+ writer .closeStream ();
75
+
76
+ setWriter ();
77
+ writer .process (DATA );
78
+ writer .closeStream ();
79
+
80
+ assertOutput (DATA + "\n " );
81
+ }
82
+
83
+ @ Test
84
+ public void shouldAppendToExistingFile () throws IOException {
85
+ writer .process (DATA );
86
+ writer .closeStream ();
87
+
88
+ setWriter ();
89
+ writer .setAppendIfFileExists (true );
90
+ writer .process (DATA );
91
+ writer .closeStream ();
92
+
93
+ assertOutput (DATA + "\n " + DATA + "\n " );
71
94
}
72
95
73
96
@ Override
@@ -83,4 +106,14 @@ protected String getOutput() throws IOException {
83
106
}
84
107
}
85
108
109
+ private void setWriter () {
110
+ writer = new ObjectFileWriter <String >(file .getAbsolutePath ());
111
+ }
112
+
113
+ private void assertOutput (final String expected ) throws IOException {
114
+ final byte [] bytesWritten = Files .readAllBytes (file .toPath ());
115
+ assertArrayEquals (expected .getBytes (StandardCharsets .UTF_8 ),
116
+ bytesWritten ); // FileObjectWriter appends new lines
117
+ }
118
+
86
119
}
0 commit comments