• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements.  See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License.  You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 package org.apache.commons.io.output;
18 
19 import static org.apache.commons.io.test.TestUtils.checkFile;
20 import static org.junit.jupiter.api.Assertions.assertEquals;
21 import static org.junit.jupiter.api.Assertions.assertFalse;
22 import static org.junit.jupiter.api.Assertions.assertThrows;
23 import static org.junit.jupiter.api.Assertions.assertTrue;
24 import static org.junit.jupiter.api.Assertions.fail;
25 
26 import java.io.File;
27 import java.io.FileWriter;
28 import java.io.IOException;
29 import java.io.OutputStreamWriter;
30 import java.io.Writer;
31 import java.nio.charset.Charset;
32 import java.nio.charset.CharsetEncoder;
33 import java.nio.charset.StandardCharsets;
34 import java.nio.file.Files;
35 
36 import org.junit.jupiter.api.BeforeEach;
37 import org.junit.jupiter.api.Test;
38 import org.junit.jupiter.api.io.TempDir;
39 
40 /**
41  * Tests that the encoding is actually set and used.
42  *
43  */
44 public class FileWriterWithEncodingTest {
45 
46     @TempDir
47     public File temporaryFolder;
48 
49     private String defaultEncoding;
50     private File file1;
51     private File file2;
52     private String textContent;
53     private final char[] anotherTestContent = {'f', 'z', 'x'};
54 
55     @Test
constructor_File_directory()56     public void constructor_File_directory() {
57         assertThrows(IOException.class, () -> {
58             try (Writer writer = new FileWriterWithEncoding(temporaryFolder, defaultEncoding)) {
59                 // empty
60             }
61         });
62         assertFalse(file1.exists());
63     }
64 
65     @Test
constructor_File_encoding_badEncoding()66     public void constructor_File_encoding_badEncoding() {
67         assertThrows(IOException.class, () -> {
68             try (Writer writer = new FileWriterWithEncoding(file1, "BAD-ENCODE")) {
69                 // empty
70             }
71         });
72         assertFalse(file1.exists());
73     }
74 
75     @Test
constructor_File_existingFile_withContent()76     public void constructor_File_existingFile_withContent() throws Exception {
77         try (FileWriter fw1 = new FileWriter(file1);) {
78             fw1.write(textContent);
79             fw1.write(65);
80         }
81         assertEquals(1025, file1.length());
82 
83         try (FileWriterWithEncoding fw1 = new FileWriterWithEncoding(file1, defaultEncoding)) {
84             fw1.write("ABcd");
85         }
86 
87         assertEquals(4, file1.length());
88     }
89 
90     @Test
constructor_File_nullFile()91     public void constructor_File_nullFile() {
92         assertThrows(NullPointerException.class, () -> {
93             try (Writer writer = new FileWriterWithEncoding((File) null, defaultEncoding)) {
94                 // empty
95             }
96         });
97         assertFalse(file1.exists());
98     }
99 
100     @Test
constructor_fileName_nullFile()101     public void constructor_fileName_nullFile() {
102         assertThrows(NullPointerException.class, () -> {
103             try (Writer writer = new FileWriterWithEncoding((String) null, defaultEncoding)) {
104                 // empty
105             }
106         });
107         assertFalse(file1.exists());
108     }
109 
110     @Test
constructorAppend_File_existingFile_withContent()111     public void constructorAppend_File_existingFile_withContent() throws Exception {
112         try (FileWriter fw1 = new FileWriter(file1)) {
113             fw1.write("ABcd");
114         }
115         assertEquals(4, file1.length());
116 
117         try (FileWriterWithEncoding fw1 = new FileWriterWithEncoding(file1, defaultEncoding, true)) {
118             fw1.write("XyZ");
119         }
120 
121         assertEquals(7, file1.length());
122     }
123 
124     @Test
sameEncoding_Charset_constructor()125     public void sameEncoding_Charset_constructor() throws Exception {
126         try (FileWriterWithEncoding writer = new FileWriterWithEncoding(file2, Charset.defaultCharset())) {
127             successfulRun(writer);
128         }
129     }
130 
131     @Test
sameEncoding_CharsetEncoder_constructor()132     public void sameEncoding_CharsetEncoder_constructor() throws Exception {
133         try (FileWriterWithEncoding writer = new FileWriterWithEncoding(file2, Charset.defaultCharset().newEncoder())) {
134             successfulRun(writer);
135         }
136     }
137 
138     @Test
sameEncoding_null_Charset_constructor()139     public void sameEncoding_null_Charset_constructor() throws Exception {
140         try (FileWriterWithEncoding writer = new FileWriterWithEncoding(file2, (Charset) null)) {
141             successfulRun(writer);
142         }
143     }
144 
145     @Test
sameEncoding_null_CharsetEncoder_constructor()146     public void sameEncoding_null_CharsetEncoder_constructor() throws Exception {
147         try (FileWriterWithEncoding writer = new FileWriterWithEncoding(file2.getPath(), (CharsetEncoder) null)) {
148             successfulRun(writer);
149         }
150     }
151 
152     @Test
sameEncoding_null_CharsetName_constructor()153     public void sameEncoding_null_CharsetName_constructor() throws Exception {
154         try (FileWriterWithEncoding writer = new FileWriterWithEncoding(file2.getPath(), (String) null)) {
155             successfulRun(writer);
156         }
157     }
158 
159     @Test
sameEncoding_string_Charset_constructor()160     public void sameEncoding_string_Charset_constructor() throws Exception {
161         try (FileWriterWithEncoding writer = new FileWriterWithEncoding(file2.getPath(), Charset.defaultCharset())) {
162             successfulRun(writer);
163         }
164     }
165 
166     @Test
sameEncoding_string_CharsetEncoder_constructor()167     public void sameEncoding_string_CharsetEncoder_constructor() throws Exception {
168         try (FileWriterWithEncoding writer = new FileWriterWithEncoding(file2.getPath(), Charset.defaultCharset().newEncoder())) {
169             successfulRun(writer);
170         }
171     }
172 
173     @Test
sameEncoding_string_constructor()174     public void sameEncoding_string_constructor() throws Exception {
175         try (FileWriterWithEncoding writer = new FileWriterWithEncoding(file2, defaultEncoding)) {
176             successfulRun(writer);
177         }
178     }
179 
180     @Test
sameEncoding_string_string_constructor()181     public void sameEncoding_string_string_constructor() throws Exception {
182         try (FileWriterWithEncoding writer = new FileWriterWithEncoding(file2.getPath(), defaultEncoding)) {
183             successfulRun(writer);
184         }
185     }
186 
187     @BeforeEach
setUp()188     public void setUp() throws Exception {
189         final File encodingFinder = new File(temporaryFolder, "finder.txt");
190         try (OutputStreamWriter out = new OutputStreamWriter(Files.newOutputStream(encodingFinder.toPath()))) {
191             defaultEncoding = out.getEncoding();
192         }
193         file1 = new File(temporaryFolder, "testfile1.txt");
194         file2 = new File(temporaryFolder, "testfile2.txt");
195         final char[] arr = new char[1024];
196         final char[] chars = "ABCDEFGHIJKLMNOPQabcdefgihklmnopq".toCharArray();
197         for (int i = 0; i < arr.length; i++) {
198             arr[i] = chars[i % chars.length];
199         }
200         textContent = new String(arr);
201     }
202 
successfulRun(final FileWriterWithEncoding fw21)203     private void successfulRun(final FileWriterWithEncoding fw21) throws Exception {
204         try (FileWriter fw1 = new FileWriter(file1); // default encoding
205             FileWriterWithEncoding fw2 = fw21) {
206             writeTestPayload(fw1, fw2);
207             checkFile(file1, file2);
208         }
209         assertTrue(file1.exists());
210         assertTrue(file2.exists());
211     }
212 
213     @Test
testDifferentEncoding()214     public void testDifferentEncoding() throws Exception {
215         if (Charset.isSupported(StandardCharsets.UTF_16BE.name())) {
216             try (FileWriter fw1 = new FileWriter(file1); // default encoding
217                 FileWriterWithEncoding fw2 = new FileWriterWithEncoding(file2, defaultEncoding)) {
218                 writeTestPayload(fw1, fw2);
219                 try {
220                     checkFile(file1, file2);
221                     fail();
222                 } catch (final AssertionError ex) {
223                     // success
224                 }
225 
226             }
227             assertTrue(file1.exists());
228             assertTrue(file2.exists());
229         }
230         if (Charset.isSupported(StandardCharsets.UTF_16LE.name())) {
231             try (FileWriter fw1 = new FileWriter(file1); // default encoding
232                 FileWriterWithEncoding fw2 = new FileWriterWithEncoding(file2, defaultEncoding)) {
233                 writeTestPayload(fw1, fw2);
234                 try {
235                     checkFile(file1, file2);
236                     fail();
237                 } catch (final AssertionError ex) {
238                     // success
239                 }
240 
241             }
242             assertTrue(file1.exists());
243             assertTrue(file2.exists());
244         }
245     }
246 
writeTestPayload(final FileWriter fw1, final FileWriterWithEncoding fw2)247     private void writeTestPayload(final FileWriter fw1, final FileWriterWithEncoding fw2) throws IOException {
248         assertTrue(file1.exists());
249         assertTrue(file2.exists());
250 
251         fw1.write(textContent);
252         fw2.write(textContent);
253         fw1.write(65);
254         fw2.write(65);
255         fw1.write(anotherTestContent);
256         fw2.write(anotherTestContent);
257         fw1.write(anotherTestContent, 1, 2);
258         fw2.write(anotherTestContent, 1, 2);
259         fw1.write("CAFE", 1, 2);
260         fw2.write("CAFE", 1, 2);
261 
262         fw1.flush();
263         fw2.flush();
264     }
265 }
266