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 java.io.File;
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;
26  import java.util.Map;
27  
28  import junit.framework.AssertionFailedError;
29  
30  import org.apache.commons.io.IOUtils;
31  import org.apache.commons.io.testtools.FileBasedTestCase;
32  
33  /**
34   * Tests that the encoding is actually set and used.
35   *
36   * @version $Revision: 609864 $ $Date: 2008-01-08 04:20:00 +0000 (Tue, 08 Jan 2008) $
37   */
38  public class FileWriterWithEncodingTest extends FileBasedTestCase {
39  
40      private String defaultEncoding;
41      private File file1;
42      private File file2;
43      private String textContent;
44  
45      public FileWriterWithEncodingTest(String name) {
46          super(name);
47      }
48  
49      public void setUp() {
50          File encodingFinder = new File(getTestDirectory(), "finder.txt");
51          OutputStreamWriter out = null;
52          try {
53              out = new OutputStreamWriter(new FileOutputStream(encodingFinder));
54              defaultEncoding = out.getEncoding();
55          } catch (IOException ex) {
56              throw new RuntimeException(ex.getMessage());
57          } finally {
58              IOUtils.closeQuietly(out);
59          }
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++) {
64              arr[i] = (char) i;
65          }
66          textContent = new String(arr);
67      }
68  
69      public void tearDown() {
70          defaultEncoding = null;
71          file1.delete();
72          file2.delete();
73          textContent = null;
74      }
75  
76      //-----------------------------------------------------------------------
77      public void testSameEncoding() throws Exception {
78          FileWriter fw1 = null;
79          FileWriterWithEncoding fw2 = null;
80          try {
81              fw1 = new FileWriter(file1);  // default encoding
82              fw2 = new FileWriterWithEncoding(file2, defaultEncoding);
83              assertEquals(true, file1.exists());
84              assertEquals(true, file2.exists());
85              
86              fw1.write(textContent);
87              fw2.write(textContent);
88              
89              fw1.flush();
90              fw2.flush();
91              checkFile(file1, file2);
92              
93          } finally {
94              IOUtils.closeQuietly(fw1);
95              IOUtils.closeQuietly(fw2);
96          }
97          assertEquals(true, file1.exists());
98          assertEquals(true, file2.exists());
99      }
100 
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;
106             try {
107                 fw1 = new FileWriter(file1);  // default encoding
108                 fw2 = new FileWriterWithEncoding(file2, defaultEncoding);
109                 assertEquals(true, file1.exists());
110                 assertEquals(true, file2.exists());
111                 
112                 fw1.write(textContent);
113                 fw2.write(textContent);
114                 
115                 fw1.flush();
116                 fw2.flush();
117                 try {
118                     checkFile(file1, file2);
119                     fail();
120                 } catch (AssertionFailedError ex) {
121                     // success
122                 }
123                 
124             } finally {
125                 IOUtils.closeQuietly(fw1);
126                 IOUtils.closeQuietly(fw2);
127             }
128             assertEquals(true, file1.exists());
129             assertEquals(true, file2.exists());
130         }
131         if (map.containsKey("UTF-16LE")) {
132             FileWriter fw1 = null;
133             FileWriterWithEncoding fw2 = null;
134             try {
135                 fw1 = new FileWriter(file1);  // default encoding
136                 fw2 = new FileWriterWithEncoding(file2, defaultEncoding);
137                 assertEquals(true, file1.exists());
138                 assertEquals(true, file2.exists());
139                 
140                 fw1.write(textContent);
141                 fw2.write(textContent);
142                 
143                 fw1.flush();
144                 fw2.flush();
145                 try {
146                     checkFile(file1, file2);
147                     fail();
148                 } catch (AssertionFailedError ex) {
149                     // success
150                 }
151                 
152             } finally {
153                 IOUtils.closeQuietly(fw1);
154                 IOUtils.closeQuietly(fw2);
155             }
156             assertEquals(true, file1.exists());
157             assertEquals(true, file2.exists());
158         }
159     }
160 
161     //-----------------------------------------------------------------------
162     public void testConstructor_File_encoding_badEncoding() throws IOException {
163         Writer writer = null;
164         try {
165             writer = new FileWriterWithEncoding(file1, "BAD-ENCODE");
166             fail();
167         } catch (IOException ex) {
168             // expected
169             assertEquals(false, file1.exists());
170         } finally {
171             IOUtils.closeQuietly(writer);
172         }
173         assertEquals(false, file1.exists());
174     }
175 
176     //-----------------------------------------------------------------------
177     public void testConstructor_File_directory() throws IOException {
178         Writer writer = null;
179         try {
180             writer = new FileWriterWithEncoding(getTestDirectory(), defaultEncoding);
181             fail();
182         } catch (IOException ex) {
183             // expected
184             assertEquals(false, file1.exists());
185         } finally {
186             IOUtils.closeQuietly(writer);
187         }
188         assertEquals(false, file1.exists());
189     }
190 
191     //-----------------------------------------------------------------------
192     public void testConstructor_File_nullFile() throws IOException {
193         Writer writer = null;
194         try {
195             writer = new FileWriterWithEncoding((File) null, defaultEncoding);
196             fail();
197         } catch (NullPointerException ex) {
198             // expected
199             assertEquals(false, file1.exists());
200         } finally {
201             IOUtils.closeQuietly(writer);
202         }
203         assertEquals(false, file1.exists());
204     }
205 
206     //-----------------------------------------------------------------------
207     public void testConstructor_fileName_nullFile() throws IOException {
208         Writer writer = null;
209         try {
210             writer = new FileWriterWithEncoding((String) null, defaultEncoding);
211             fail();
212         } catch (NullPointerException ex) {
213             // expected
214             assertEquals(false, file1.exists());
215         } finally {
216             IOUtils.closeQuietly(writer);
217         }
218         assertEquals(false, file1.exists());
219     }
220 
221 }