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
9
* http://www.apache.org/licenses/LICENSE-2.0
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.
17
package org.apache.commons.io.output;
20
import java.io.FileOutputStream;
21
import java.io.FileWriter;
22
import java.io.IOException;
23
import java.io.OutputStreamWriter;
24
import java.io.Writer;
25
import java.nio.charset.Charset;
28
import junit.framework.AssertionFailedError;
30
import org.apache.commons.io.IOUtils;
31
import org.apache.commons.io.testtools.FileBasedTestCase;
34
* Tests that the encoding is actually set and used.
36
* @version $Revision: 609864 $ $Date: 2008-01-08 04:20:00 +0000 (Tue, 08 Jan 2008) $
38
public class FileWriterWithEncodingTest extends FileBasedTestCase {
40
private String defaultEncoding;
43
private String textContent;
45
public FileWriterWithEncodingTest(String name) {
50
File encodingFinder = new File(getTestDirectory(), "finder.txt");
51
OutputStreamWriter out = null;
53
out = new OutputStreamWriter(new FileOutputStream(encodingFinder));
54
defaultEncoding = out.getEncoding();
55
} catch (IOException ex) {
56
throw new RuntimeException(ex.getMessage());
58
IOUtils.closeQuietly(out);
60
file1 = new File(getTestDirectory(), "testfile1.txt");
61
file2 = new File(getTestDirectory(), "testfile2.txt");
62
char[] arr = new char[1024];
63
for (int i = 0; i < arr.length; i++) {
66
textContent = new String(arr);
69
public void tearDown() {
70
defaultEncoding = null;
76
//-----------------------------------------------------------------------
77
public void testSameEncoding() throws Exception {
78
FileWriter fw1 = null;
79
FileWriterWithEncoding fw2 = null;
81
fw1 = new FileWriter(file1); // default encoding
82
fw2 = new FileWriterWithEncoding(file2, defaultEncoding);
83
assertEquals(true, file1.exists());
84
assertEquals(true, file2.exists());
86
fw1.write(textContent);
87
fw2.write(textContent);
91
checkFile(file1, file2);
94
IOUtils.closeQuietly(fw1);
95
IOUtils.closeQuietly(fw2);
97
assertEquals(true, file1.exists());
98
assertEquals(true, file2.exists());
101
public void testDifferentEncoding() throws Exception {
102
Map map = Charset.availableCharsets();
103
if (map.containsKey("UTF-16BE")) {
104
FileWriter fw1 = null;
105
FileWriterWithEncoding fw2 = null;
107
fw1 = new FileWriter(file1); // default encoding
108
fw2 = new FileWriterWithEncoding(file2, defaultEncoding);
109
assertEquals(true, file1.exists());
110
assertEquals(true, file2.exists());
112
fw1.write(textContent);
113
fw2.write(textContent);
118
checkFile(file1, file2);
120
} catch (AssertionFailedError ex) {
125
IOUtils.closeQuietly(fw1);
126
IOUtils.closeQuietly(fw2);
128
assertEquals(true, file1.exists());
129
assertEquals(true, file2.exists());
131
if (map.containsKey("UTF-16LE")) {
132
FileWriter fw1 = null;
133
FileWriterWithEncoding fw2 = null;
135
fw1 = new FileWriter(file1); // default encoding
136
fw2 = new FileWriterWithEncoding(file2, defaultEncoding);
137
assertEquals(true, file1.exists());
138
assertEquals(true, file2.exists());
140
fw1.write(textContent);
141
fw2.write(textContent);
146
checkFile(file1, file2);
148
} catch (AssertionFailedError ex) {
153
IOUtils.closeQuietly(fw1);
154
IOUtils.closeQuietly(fw2);
156
assertEquals(true, file1.exists());
157
assertEquals(true, file2.exists());
161
//-----------------------------------------------------------------------
162
public void testConstructor_File_encoding_badEncoding() throws IOException {
163
Writer writer = null;
165
writer = new FileWriterWithEncoding(file1, "BAD-ENCODE");
167
} catch (IOException ex) {
169
assertEquals(false, file1.exists());
171
IOUtils.closeQuietly(writer);
173
assertEquals(false, file1.exists());
176
//-----------------------------------------------------------------------
177
public void testConstructor_File_directory() throws IOException {
178
Writer writer = null;
180
writer = new FileWriterWithEncoding(getTestDirectory(), defaultEncoding);
182
} catch (IOException ex) {
184
assertEquals(false, file1.exists());
186
IOUtils.closeQuietly(writer);
188
assertEquals(false, file1.exists());
191
//-----------------------------------------------------------------------
192
public void testConstructor_File_nullFile() throws IOException {
193
Writer writer = null;
195
writer = new FileWriterWithEncoding((File) null, defaultEncoding);
197
} catch (NullPointerException ex) {
199
assertEquals(false, file1.exists());
201
IOUtils.closeQuietly(writer);
203
assertEquals(false, file1.exists());
206
//-----------------------------------------------------------------------
207
public void testConstructor_fileName_nullFile() throws IOException {
208
Writer writer = null;
210
writer = new FileWriterWithEncoding((String) null, defaultEncoding);
212
} catch (NullPointerException ex) {
214
assertEquals(false, file1.exists());
216
IOUtils.closeQuietly(writer);
218
assertEquals(false, file1.exists());