@@ -51,54 +51,119 @@ public class FileWriterWithEncodingTest {
5151 private String textContent ;
5252 private final char [] anotherTestContent = {'f' , 'z' , 'x' };
5353
54- @ BeforeEach
55- public void setUp () throws Exception {
56- final File encodingFinder = new File (temporaryFolder , "finder.txt" );
57- try (OutputStreamWriter out = new OutputStreamWriter (Files .newOutputStream (encodingFinder .toPath ()))) {
58- defaultEncoding = out .getEncoding ();
54+ @ Test
55+ public void constructor_File_directory () {
56+ assertThrows (IOException .class , () -> {
57+ try (Writer writer = new FileWriterWithEncoding (temporaryFolder , defaultEncoding )) {
58+ // empty
59+ }
60+ });
61+ assertFalse (file1 .exists ());
62+ }
63+
64+ @ Test
65+ public void constructor_File_encoding_badEncoding () {
66+ assertThrows (IOException .class , () -> {
67+ try (Writer writer = new FileWriterWithEncoding (file1 , "BAD-ENCODE" )) {
68+ // empty
69+ }
70+ });
71+ assertFalse (file1 .exists ());
72+ }
73+
74+ @ Test
75+ public void constructor_File_existingFile_withContent () throws Exception {
76+ try (FileWriter fw1 = new FileWriter (file1 );) {
77+ fw1 .write (textContent );
78+ fw1 .write (65 );
5979 }
60- file1 = new File (temporaryFolder , "testfile1.txt" );
61- file2 = new File (temporaryFolder , "testfile2.txt" );
62- final char [] arr = new char [1024 ];
63- final char [] chars = "ABCDEFGHIJKLMNOPQabcdefgihklmnopq" .toCharArray ();
64- for (int i = 0 ; i < arr .length ; i ++) {
65- arr [i ] = chars [i % chars .length ];
80+ assertEquals (1025 , file1 .length ());
81+
82+ try (FileWriterWithEncoding fw1 = new FileWriterWithEncoding (file1 , defaultEncoding )) {
83+ fw1 .write ("ABcd" );
6684 }
67- textContent = new String (arr );
85+
86+ assertEquals (4 , file1 .length ());
6887 }
6988
7089 @ Test
71- public void sameEncoding_string_constructor () throws Exception {
72- successfulRun (new FileWriterWithEncoding (file2 , defaultEncoding ));
90+ public void constructor_File_nullFile () {
91+ assertThrows (NullPointerException .class , () -> {
92+ try (Writer writer = new FileWriterWithEncoding ((File ) null , defaultEncoding )) {
93+ // empty
94+ }
95+ });
96+ assertFalse (file1 .exists ());
7397 }
7498
7599 @ Test
76- public void sameEncoding_string_string_constructor () throws Exception {
77- successfulRun (new FileWriterWithEncoding (file2 .getPath (), defaultEncoding ));
100+ public void constructor_fileName_nullFile () {
101+ assertThrows (NullPointerException .class , () -> {
102+ try (Writer writer = new FileWriterWithEncoding ((String ) null , defaultEncoding )) {
103+ // empty
104+ }
105+ });
106+ assertFalse (file1 .exists ());
78107 }
79108
80109 @ Test
81110 public void sameEncoding_Charset_constructor () throws Exception {
82111 successfulRun (new FileWriterWithEncoding (file2 , Charset .defaultCharset ()));
83112 }
84113
85- @ Test
86- public void sameEncoding_string_Charset_constructor () throws Exception {
87- successfulRun (new FileWriterWithEncoding (file2 .getPath (), Charset .defaultCharset ()));
88- }
89-
90114 @ Test
91115 public void sameEncoding_CharsetEncoder_constructor () throws Exception {
92116 final CharsetEncoder enc = Charset .defaultCharset ().newEncoder ();
93117 successfulRun (new FileWriterWithEncoding (file2 , enc ));
94118 }
95119
120+ @ Test
121+ public void sameEncoding_null_Charset_constructor () throws Exception {
122+ try {
123+ successfulRun (new FileWriterWithEncoding (file2 , (Charset ) null ));
124+ fail ();
125+ } catch (final NullPointerException ignore ) {
126+ // empty
127+ }
128+ }
129+
130+ @ Test
131+ public void sameEncoding_string_Charset_constructor () throws Exception {
132+ successfulRun (new FileWriterWithEncoding (file2 .getPath (), Charset .defaultCharset ()));
133+ }
134+
96135 @ Test
97136 public void sameEncoding_string_CharsetEncoder_constructor () throws Exception {
98137 final CharsetEncoder enc = Charset .defaultCharset ().newEncoder ();
99138 successfulRun (new FileWriterWithEncoding (file2 .getPath (), enc ));
100139 }
101140
141+ @ Test
142+ public void sameEncoding_string_constructor () throws Exception {
143+ successfulRun (new FileWriterWithEncoding (file2 , defaultEncoding ));
144+ }
145+
146+ @ Test
147+ public void sameEncoding_string_string_constructor () throws Exception {
148+ successfulRun (new FileWriterWithEncoding (file2 .getPath (), defaultEncoding ));
149+ }
150+
151+ @ BeforeEach
152+ public void setUp () throws Exception {
153+ final File encodingFinder = new File (temporaryFolder , "finder.txt" );
154+ try (OutputStreamWriter out = new OutputStreamWriter (Files .newOutputStream (encodingFinder .toPath ()))) {
155+ defaultEncoding = out .getEncoding ();
156+ }
157+ file1 = new File (temporaryFolder , "testfile1.txt" );
158+ file2 = new File (temporaryFolder , "testfile2.txt" );
159+ final char [] arr = new char [1024 ];
160+ final char [] chars = "ABCDEFGHIJKLMNOPQabcdefgihklmnopq" .toCharArray ();
161+ for (int i = 0 ; i < arr .length ; i ++) {
162+ arr [i ] = chars [i % chars .length ];
163+ }
164+ textContent = new String (arr );
165+ }
166+
102167 private void successfulRun (final FileWriterWithEncoding fw21 ) throws Exception {
103168 try (FileWriter fw1 = new FileWriter (file1 ); // default encoding
104169 FileWriterWithEncoding fw2 = fw21 ) {
@@ -161,69 +226,4 @@ private void writeTestPayload(final FileWriter fw1, final FileWriterWithEncoding
161226 fw1 .flush ();
162227 fw2 .flush ();
163228 }
164-
165- @ Test
166- public void constructor_File_encoding_badEncoding () {
167- assertThrows (IOException .class , () -> {
168- try (Writer writer = new FileWriterWithEncoding (file1 , "BAD-ENCODE" )) {
169- // empty
170- }
171- });
172- assertFalse (file1 .exists ());
173- }
174-
175- @ Test
176- public void constructor_File_directory () {
177- assertThrows (IOException .class , () -> {
178- try (Writer writer = new FileWriterWithEncoding (temporaryFolder , defaultEncoding )) {
179- // empty
180- }
181- });
182- assertFalse (file1 .exists ());
183- }
184-
185- @ Test
186- public void constructor_File_nullFile () {
187- assertThrows (NullPointerException .class , () -> {
188- try (Writer writer = new FileWriterWithEncoding ((File ) null , defaultEncoding )) {
189- // empty
190- }
191- });
192- assertFalse (file1 .exists ());
193- }
194-
195- @ Test
196- public void constructor_fileName_nullFile () {
197- assertThrows (NullPointerException .class , () -> {
198- try (Writer writer = new FileWriterWithEncoding ((String ) null , defaultEncoding )) {
199- // empty
200- }
201- });
202- assertFalse (file1 .exists ());
203- }
204-
205- @ Test
206- public void sameEncoding_null_Charset_constructor () throws Exception {
207- try {
208- successfulRun (new FileWriterWithEncoding (file2 , (Charset ) null ));
209- fail ();
210- } catch (final NullPointerException ignore ) {
211- // empty
212- }
213- }
214-
215- @ Test
216- public void constructor_File_existingFile_withContent () throws Exception {
217- try (FileWriter fw1 = new FileWriter (file1 );) {
218- fw1 .write (textContent );
219- fw1 .write (65 );
220- }
221- assertEquals (1025 , file1 .length ());
222-
223- try (FileWriterWithEncoding fw1 = new FileWriterWithEncoding (file1 , defaultEncoding )) {
224- fw1 .write ("ABcd" );
225- }
226-
227- assertEquals (4 , file1 .length ());
228- }
229229}
0 commit comments