Clover Coverage Report
Coverage timestamp: Sat Sep 18 2010 04:09:52 UTC
567   1,111   112   15.32
140   868   0.2   37
37     3.03  
1    
 
  IoUtilityTest       Line # 44 567 112 86.3% 0.86290324
 
  (30)
 
1    /* This file is part of the project "Hilbert II" - http://www.qedeq.org
2    *
3    * Copyright 2000-2010, Michael Meyling <mime@qedeq.org>.
4    *
5    * "Hilbert II" is free software; you can redistribute
6    * it and/or modify it under the terms of the GNU General Public
7    * License as published by the Free Software Foundation; either
8    * version 2 of the License, or (at your option) any later version.
9    *
10    * This program is distributed in the hope that it will be useful,
11    * but WITHOUT ANY WARRANTY; without even the implied warranty of
12    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13    * GNU General Public License for more details.
14    */
15   
16    package org.qedeq.base.io;
17   
18    import java.io.ByteArrayInputStream;
19    import java.io.File;
20    import java.io.FileFilter;
21    import java.io.FileInputStream;
22    import java.io.FileNotFoundException;
23    import java.io.FileOutputStream;
24    import java.io.FileReader;
25    import java.io.FileWriter;
26    import java.io.IOException;
27    import java.io.InputStream;
28    import java.io.InputStreamReader;
29    import java.io.OutputStream;
30    import java.io.Reader;
31    import java.io.UnsupportedEncodingException;
32    import java.io.Writer;
33    import java.util.Random;
34   
35    import org.qedeq.base.test.QedeqTestCase;
36    import org.qedeq.base.utility.EqualsUtility;
37    import org.qedeq.base.utility.StringUtility;
38   
39    /**
40    * Test {@link org.qedeq.kernel.utility.TextInput}.
41    *
42    * @author Michael Meyling
43    */
 
44    public class IoUtilityTest extends QedeqTestCase {
45   
46   
47    /*
48    * @see TestCase#setUp()
49    */
 
50  30 toggle protected void setUp() throws Exception {
51  30 super.setUp();
52    }
53   
54    /*
55    * @see TestCase#tearDown()
56    */
 
57  30 toggle protected void tearDown() throws Exception {
58  30 super.tearDown();
59    }
60   
61    /**
62    * Test {@link IoUtility#getDefaultEncoding()}. TODO mime 20090630 throws no exception no more
63    *
64    * @throws Exception Test failed.
65    */
 
66  1 toggle public void testGetDefaultEncoding() throws Exception {
67  1 assertEquals(System.getProperty("file.encoding"), IoUtility.getDefaultEncoding());
68  1 final String encoding = new InputStreamReader(new ByteArrayInputStream(
69    new byte[0])).getEncoding();
70    // UTF-8 and UTF8 are the same, so we remove all "-" ...
71  1 if (!StringUtility.replace(System.getProperty("file.encoding"), "-", "").equals(encoding)) {
72  1 System.out.println("This system showed the java property \"file.encoding\" "
73    + System.getProperty("file.encoding") + " but the detected writing default is: "
74    + "\"" + IoUtility.getDefaultEncoding()
75    + "\"\nThis might be ok, but you should check it");
76    }
77    }
78   
79    /**
80    * Test {@link IoUtility#loadFile(String fileName, String encoding)}.
81    *
82    * @throws Exception Test failed.
83    */
 
84  1 toggle public void testLoadFileStringString() throws Exception {
85  1 final File file = new File("IoUtilityTestLoadStringString.txt");
86  1 if (file.exists()) {
87  0 assertTrue(file.delete());
88    }
89  1 IoUtility.saveFileBinary(file, StringUtility.hex2byte(
90    "FF FE 49 00 20 00 61 00 6D 00 20 00 64 00 72 00"
91    + "65 00 61 00 6D 00 69 00 6E 00 67 00 20 00 6F 00"
92    + "66 00 20 00 61 00 20 00 77 00 68 00 69 00 74 00"
93    + "65 00 20 00 63 00 68 00 72 00 69 00 73 00 74 00"
94    + "6D 00 61 00 73 00 73 00 2E 00 2E 00 2E 00"));
95  1 assertEquals("I am dreaming of a white christmass...", IoUtility.loadFile(file.toString(),
96    "UTF16"));
97  1 assertTrue(file.delete());
98    }
99   
100    /**
101    * Test {@link IoUtility#getWorkingEncoding(String)}.
102    *
103    * @throws Exception Test failed.
104    */
 
105  1 toggle public void testGetWorkingEncoding() throws Exception {
106  1 assertEquals("ISO-8859-1", IoUtility.getWorkingEncoding("unknown"));
107  1 System.err.println("^ above text is ok, this was intended test behaviour");
108  1 assertEquals("UTF-8", IoUtility.getWorkingEncoding("UTF-8"));
109  1 assertEquals("UTF-16", IoUtility.getWorkingEncoding("UTF-16"));
110  1 assertEquals("UTF8", IoUtility.getWorkingEncoding("UTF8"));
111  1 assertEquals("ISO-8859-1", IoUtility.getWorkingEncoding(null));
112  1 System.err.println("^ above text is ok, this was intended test behaviour");
113    }
114   
115    /**
116    * Test {@link IoUtility#createRelativePath(final File origin, final File next)}.
117    *
118    * @throws Exception Test failed.
119    */
 
120  1 toggle public void testCreateRelativePath() throws Exception {
121  1 assertEquals("local", IoUtility.createRelativePath(new File("."), new File("local")));
122  1 assertEquals("text", IoUtility.createRelativePath(new File("/local/data"),
123    new File("/local/data/text")));
124  1 assertEquals("../../green", IoUtility.createRelativePath(new File("/local/data"),
125    new File("/green")));
126  1 assertEquals("../green", IoUtility.createRelativePath(new File("/local/data"),
127    new File("/local/green")));
128    }
129   
130    /**
131    * Test {@link IoUtility#loadStream(InputStream, StringBuffer)}.
132    *
133    * @throws Exception Test failed.
134    */
 
135  1 toggle public void testLoadStreamInputStreamStringBuffer() throws Exception {
136  1 final File file = new File("testLoadStreamInputStreamStringBuffer.txt");
137  1 if (file.exists()) {
138  0 assertTrue(file.delete());
139    }
140  1 final OutputStream out = new FileOutputStream(file);
141  1 final String expected = "We all live in a yellow submarine ...";
142  1 out.write(expected.getBytes());
143  1 out.close();
144  1 final StringBuffer buffer = new StringBuffer();
145  1 final InputStream in = new FileInputStream(file);
146  1 IoUtility.loadStream(in, buffer);
147  1 in.close();
148  1 assertEquals(expected, buffer.toString());
149  1 assertTrue(file.delete());
150    }
151   
152    /**
153    * Test {@link IoUtility#loadStreamWithoutException(InputStream, int)}.
154    *
155    * @throws Exception Test failed.
156    */
 
157  1 toggle public void testLoadStreamWithoutException() throws Exception {
158  1 final String expected = "We all live in a yellow submarine ...";
159  1 assertEquals("", IoUtility.loadStreamWithoutException(null, 100));
160  1 assertEquals("", IoUtility.loadStreamWithoutException(null, -100));
161  1 assertEquals("", IoUtility.loadStreamWithoutException(new InputStream() {
 
162  0 toggle public int read() throws IOException {
163  0 throw new IOException("i am an error input stream!");
164    }
165    }, 0));
166  1 assertEquals("", IoUtility.loadStreamWithoutException(new InputStream() {
 
167  0 toggle public int read() throws IOException {
168  0 throw new IOException("i am an error input stream!");
169    }
170    }, -99));
171  1 assertEquals(expected, streamLoad(expected, expected.length()));
172  1 assertEquals(expected, streamLoad(expected, expected.length() * 100));
173  1 assertEquals(expected.substring(0, expected.length() - 1),
174    streamLoad(expected, expected.length() - 1));
175  1 assertEquals(expected.substring(0, 1),
176    streamLoad(expected, 1));
177  1 assertEquals("", streamLoad(expected, -2));
178   
179    }
180   
 
181  5 toggle private String streamLoad(final String expected, final int length) throws IOException {
182  5 final File file = new File("testLoadStreamWithoutException.txt");
183  5 if (file.exists()) {
184  0 assertTrue(file.delete());
185    }
186  5 try {
187  5 final OutputStream out = new FileOutputStream(file);
188  5 try {
189  5 out.write(expected.getBytes());
190    } finally {
191  5 if (out != null) {
192  5 out.close();
193    }
194    }
195  5 final InputStream in = new FileInputStream(file);
196  5 try {
197  5 return IoUtility.loadStreamWithoutException(in, length);
198    } finally {
199  5 if (in != null) {
200    // assert that stream is not closed yet
201  5 in.close();
202    }
203    }
204    } finally {
205  5 assertTrue(file.delete());
206    }
207    }
208   
209    /**
210    * Test {@link IoUtility#loadReader(Reader, StringBuffer)}.
211    *
212    * @throws Exception Test failed.
213    */
 
214  1 toggle public void testLoadReader() throws Exception {
215  1 final File file = new File("testLoadReaderStringBuffer.txt");
216  1 if (file.exists()) {
217  0 assertTrue(file.delete());
218    }
219  1 final Writer out = new FileWriter(file);
220  1 final String expected = "We all live in a yellow submarine ...";
221  1 out.write(expected);
222  1 out.close();
223  1 final StringBuffer buffer = new StringBuffer();
224  1 final Reader in = new FileReader(file);
225  1 IoUtility.loadReader(in, buffer);
226    // test if reader can be closed
227  1 in.close();
228  1 assertEquals(expected, buffer.toString());
229  1 assertTrue(file.delete());
230    }
231   
232    /**
233    * Test {@link IoUtility#loadFile(File, StringBuffer)}.
234    *
235    * @throws Exception Test failed.
236    */
 
237  1 toggle public void testLoadFileFileStringBuffer() throws Exception {
238  1 final File file = new File("testLoadFileFileStringBuffer.txt");
239  1 if (file.exists()) {
240  0 assertTrue(file.delete());
241    }
242  1 final Writer out = new FileWriter(file);
243  1 final String expected = "We all live in a yellow submarine ...";
244  1 out.write(expected);
245  1 out.close();
246  1 final StringBuffer buffer = new StringBuffer();
247  1 IoUtility.loadFile(file, buffer);
248  1 assertEquals(expected, buffer.toString());
249  1 assertTrue(file.delete());
250    }
251   
252    /**
253    * Test {@link IoUtility#loadFile(File, StringBuffer, String)}.
254    *
255    * @throws Exception Test failed.
256    */
 
257  1 toggle public void testLoadFileFileStringBufferString() throws Exception {
258  1 final File file = new File("testLoadFileFileStringBufferString.txt");
259  1 if (file.exists()) {
260  0 assertTrue(file.delete());
261    }
262  1 final String expected1 = "We all live in a yellow submarine ...";
263  1 final String encoding1 = "UTF-16";
264  1 assertEquals(expected1, loadFile(file, expected1, encoding1));
265  1 final String expected2 = "We all live in a yellow submarine\n"
266    + "Yellow submarine, yellow submarine\n"
267    + "We all live in a yellow submarine\n"
268    + "Yellow submarine, yellow submarine\n"
269    + "\n"
270    + "As we live a life of ease\n"
271    + "Everyone of us has all we need\n"
272    + "Sky of blue and sea of green\n"
273    + "In our yellow submarine ";
274  1 final String encoding2 = "UnicodeBigUnmarked";
275  1 assertEquals(expected2, loadFile(file, expected2, encoding2));
276  1 final String encoding3 = "ASCII";
277  1 assertEquals(expected2, loadFile(file, expected2, encoding3));
278  1 assertTrue(file.delete());
279    }
280   
 
281  3 toggle private String loadFile(final File file, final String expected, final String encoding)
282    throws FileNotFoundException, IOException, UnsupportedEncodingException {
283  3 final OutputStream out = new FileOutputStream(file);
284  3 try {
285  3 out.write(expected.getBytes(encoding));
286    } finally {
287  3 if (out != null) {
288  3 out.close();
289    }
290    }
291  3 final StringBuffer buffer = new StringBuffer();
292  3 IoUtility.loadFile(file, buffer, encoding);
293  3 return buffer.toString();
294    }
295   
296    /**
297    * Test {@link IoUtility#loadFileBinary(File)}.
298    *
299    * @throws Exception Test failed.
300    */
 
301  1 toggle public void testLoadFileBinary1() throws Exception {
302  1 final File file = new File("IoUtilityTestLoadBinary1.bin");
303  1 if (file.exists()) {
304  0 assertTrue(file.delete());
305    }
306  1 final OutputStream out = new FileOutputStream(file);
307  1 try {
308  257 for (int i = 0; i < 256; i++) {
309  256 out.write(i);
310    }
311  257 for (int i = 0; i < 256; i++) {
312  256 out.write(i);
313    }
314    } finally {
315  1 out.close();
316    }
317  1 final byte [] loaded = IoUtility.loadFileBinary(file);
318  1 assertEquals(512, loaded.length);
319  257 for (int i = 0; i < 256; i++) {
320  256 assertEquals((byte) i, loaded[i]);
321    }
322  257 for (int i = 0; i < 256; i++) {
323  256 assertEquals((byte) i, loaded[i]);
324    }
325  1 assertTrue(file.delete());
326    }
327   
328    /**
329    * Test {@link IoUtility#loadFileBinary(File)}.
330    *
331    * @throws Exception Test failed.
332    */
 
333  1 toggle public void testLoadFileBinary2() throws Exception {
334  1 final File file = new File("IoUtilityTestLoadBinary2.bin");
335  1 if (file.exists()) {
336  0 assertTrue(file.delete());
337    }
338  1 final OutputStream out = new FileOutputStream(file);
339  1 try {
340  513 for (int j = 0; j < 512; j++) {
341  131584 for (int i = 0; i < 256; i++) {
342  131072 out.write(i);
343    }
344  131584 for (int i = 0; i < 256; i++) {
345  131072 out.write(255 - i);
346    }
347    }
348    } finally {
349  1 out.close();
350    }
351  1 final byte [] loaded = IoUtility.loadFileBinary(file);
352  1 assertEquals(512 * 2 * 256, loaded.length);
353  513 for (int j = 0; j < 512; j++) {
354  131584 for (int i = 0; i < 256; i++) {
355  131072 assertEquals((byte) i, loaded[i + 512 * j]);
356    }
357  131584 for (int i = 0; i < 256; i++) {
358  131072 assertEquals((byte) (255 - i), loaded[i + 256 + 512 * j]);
359    }
360    }
361  1 assertTrue(file.delete());
362    }
363   
364    /**
365    * Test {@link IoUtility#saveFileBinary(File, byte[])}.
366    *
367    * @throws Exception Test failed.
368    */
 
369  1 toggle public void testSaveFileBinary() throws Exception {
370  1 final File file = new File("IoUtilityTestSaveFileBinary.bin");
371  1 if (file.exists()) {
372  0 assertTrue(file.delete());
373    }
374  1 final byte[] data = new byte[512];
375  513 for (int i = 0; i < 512; i++) {
376  512 data[i] = (byte) i;
377    }
378  1 IoUtility.saveFileBinary(file, data);
379  1 final InputStream in = new FileInputStream(file);
380  1 try {
381  257 for (int i = 0; i < 256; i++) {
382  256 assertEquals(i, in.read());
383    }
384  257 for (int i = 0; i < 256; i++) {
385  256 assertEquals(i, in.read());
386    }
387  1 assertEquals(-1, in.read());
388    } finally {
389  1 in.close();
390    }
391  1 assertTrue(file.delete());
392    }
393   
394    /**
395    * Test {@link IoUtility#saveFileBinary(File, byte[])}.
396    *
397    * @throws Exception Test failed.
398    */
 
399  1 toggle public void testLoadAndSaveFileBinary() throws Exception {
400  1 final File file = new File("testLoadAndSaveFileBinary.bin");
401  1 if (file.exists()) {
402  0 assertTrue(file.delete());
403    }
404  1 Random random = new Random(1001);
405  1 final byte[] data = new byte[100000];
406  513 for (int i = 0; i < 512; i++) {
407  512 data[i] = (byte) i;
408    }
409  99489 for (int i = 512; i < data.length; i++) {
410  99488 data[i] = (byte) random.nextInt();
411    }
412  1 IoUtility.saveFileBinary(file, data);
413  1 assertTrue(EqualsUtility.equals(data, IoUtility.loadFileBinary(file)));
414  1 assertTrue(file.delete());
415    }
416   
417    /**
418    * Test {@link IoUtility#compareFilesBinary(File, File)}.
419    *
420    * @throws Exception Test failed.
421    */
 
422  1 toggle public void testCompareFileBinary() throws Exception {
423  1 final File file1 = new File("IoUtilityTestLoadBinary1.bin");
424  1 if (file1.exists()) {
425  0 assertTrue(file1.delete());
426    }
427  1 final File file2 = new File("IoUtilityTestLoadBinary2.bin");
428  1 if (file2.exists()) {
429  0 assertTrue(file2.delete());
430    }
431  1 assertFalse(IoUtility.compareFilesBinary(null, file1));
432  1 assertFalse(IoUtility.compareFilesBinary(file2, null));
433  1 assertTrue(IoUtility.compareFilesBinary(null, null));
434  1 try {
435  1 assertTrue(IoUtility.compareFilesBinary(file1, file2));
436  0 fail("FileNotFoundException expected");
437    } catch (FileNotFoundException e) {
438    // expected;
439    }
440  1 Random random = new Random(1001);
441  1 final byte[] data = new byte[1025];
442  1026 for (int i = 0; i < data.length; i++) {
443  1025 data[i] = (byte) random.nextInt();
444    }
445  1 IoUtility.saveFileBinary(file1, data);
446  1 IoUtility.saveFileBinary(file2, data);
447  1 assertTrue(IoUtility.compareFilesBinary(file1, file2));
448  1 data[1000] += 1;
449  1 IoUtility.saveFileBinary(file2, data);
450  1 assertFalse(IoUtility.compareFilesBinary(file1, file2));
451  1 data[999] -= 1;
452  1 IoUtility.saveFileBinary(file2, data);
453  1 assertFalse(IoUtility.compareFilesBinary(file1, file2));
454  1 IoUtility.saveFileBinary(file2, new byte[] {});
455  1 assertEquals(0, file2.length());
456  1 assertFalse(IoUtility.compareFilesBinary(file1, file2));
457  1 assertFalse(IoUtility.compareFilesBinary(file2, file1));
458  1 assertTrue(IoUtility.compareFilesBinary(file1, file1));
459  1 assertTrue(IoUtility.compareFilesBinary(file2, file2));
460  1 IoUtility.saveFileBinary(file2, new byte[] {'A', 'B', 'C'});
461  1 assertFalse(IoUtility.compareFilesBinary(file1, file2));
462  1 assertFalse(IoUtility.compareFilesBinary(file2, file1));
463  1 assertTrue(file1.delete());
464  1 assertTrue(file2.delete());
465    }
466   
467    /**
468    * Test {@link IoUtility#loadFile(URL, StringBuffer)}.
469    *
470    * @throws Exception Test failed.
471    */
 
472  1 toggle public void testLoadFileURLStringBuffer() throws Exception {
473  1 final File file = new File("testLoadFileURLStringBuffer.txt");
474  1 if (file.exists()) {
475  0 assertTrue(file.delete());
476    }
477  1 final Writer out = new FileWriter(file);
478  1 final String expected = "We all live in a yellow submarine ...";
479  1 out.write(expected);
480  1 out.close();
481  1 final StringBuffer buffer = new StringBuffer();
482  1 IoUtility.loadFile(file.toURL(), buffer);
483  1 assertEquals(expected, buffer.toString());
484  1 assertTrue(file.delete());
485    }
486   
487    /**
488    * Test {@link IoUtility#loadFile(URL, StringBuffer, String)}.
489    *
490    * @throws Exception Test failed.
491    */
 
492  1 toggle public void testLoadFileURLStringBufferString() throws Exception {
493  1 final File file = new File("testLoadFileURLStringBufferString.txt");
494  1 if (file.exists()) {
495  0 assertTrue(file.delete());
496    }
497  1 final Writer out = new FileWriter(file);
498  1 final String expected = "We all live in a yellow submarine ...\n";
499  1 final String encoding = "UTF-8";
500  1 out.write(expected);
501  1 out.close();
502  1 final StringBuffer buffer = new StringBuffer();
503  1 IoUtility.loadFile(file.toURL(), buffer, encoding);
504  1 assertEquals(expected, buffer.toString());
505  1 IoUtility.saveFileBinary(file, StringUtility.hex2byte(
506    "FF FE 49 00 20 00 61 00 6D 00 20 00 64 00 72 00"
507    + "65 00 61 00 6D 00 69 00 6E 00 67 00 20 00 6F 00"
508    + "66 00 20 00 61 00 20 00 77 00 68 00 69 00 74 00"
509    + "65 00 20 00 63 00 68 00 72 00 69 00 73 00 74 00"
510    + "6D 00 61 00 73 00 73 00 2E 00 2E 00 2E 00"));
511  1 IoUtility.loadFile(file.toURL(), buffer, "UTF16");
512  1 assertEquals(expected + "I am dreaming of a white christmass...", buffer.toString());
513  1 assertTrue(file.delete());
514    }
515   
516    /**
517    * Test {@link IoUtility#saveFile(URL, File))}.
518    *
519    * @throws Exception Test failed.
520    */
 
521  1 toggle public void testSaveFileURLFile() throws Exception {
522  1 final File file1 = new File("testSaveFileURLFile1.txt");
523  1 final File file2 = new File("testSaveFileURLFile2.txt");
524  1 if (file1.exists()) {
525  0 assertTrue(file1.delete());
526    }
527  1 if (file2.exists()) {
528  0 assertTrue(file2.delete());
529    }
530  1 IoUtility.saveFileBinary(file1, StringUtility.hex2byte(
531    "FF FE 49 00 20 00 61 00 6D 00 20 00 64 00 72 00"
532    + "65 00 61 00 6D 00 69 00 6E 00 67 00 20 00 6F 00"
533    + "66 00 20 00 61 00 20 00 77 00 68 00 69 00 74 00"
534    + "65 00 20 00 63 00 68 00 72 00 69 00 73 00 74 00"
535    + "6D 00 61 00 73 00 73 00 2E 00 2E 00 2E 00"));
536  1 IoUtility.saveFile(file1.toURL(), file2);
537  1 assertTrue(IoUtility.compareFilesBinary(file1, file2));
538  1 assertTrue(file1.delete());
539  1 assertTrue(file2.delete());
540    }
541   
542    /**
543    * Test {@link IoUtility#saveFile(InputStream, File))}.
544    *
545    * @throws Exception Test failed.
546    */
 
547  1 toggle public void testSaveFileInputStreamFile() throws Exception {
548  1 final File file1 = new File("testSaveFileInputStreamFile1.txt");
549  1 final File file2 = new File("testSaveFileInputStreamFile2.txt");
550  1 InputStream in = null;
551  1 try {
552  1 if (file1.exists()) {
553  0 assertTrue(file1.delete());
554    }
555  1 if (file2.exists()) {
556  0 assertTrue(file2.delete());
557    }
558  1 IoUtility.saveFileBinary(file1, StringUtility.hex2byte(
559    "FF FE 49 00 20 00 61 00 6D 00 20 00 64 00 72 00"
560    + "65 00 61 00 6D 00 69 00 6E 00 67 00 20 00 6F 00"
561    + "66 00 20 00 61 00 20 00 77 00 68 00 69 00 74 00"
562    + "65 00 20 00 63 00 68 00 72 00 69 00 73 00 74 00"
563    + "6D 00 61 00 73 00 73 00 2E 00 2E 00 2E 00"));
564  1 in = new FileInputStream(file1);
565  1 IoUtility.saveFile(in, file2);
566  1 assertTrue(IoUtility.compareFilesBinary(file1, file2));
567    } finally {
568  1 if (in != null) {
569  1 in.close();
570    }
571    }
572  1 assertTrue(file1.delete());
573  1 assertTrue(file2.delete());
574    }
575   
576    /**
577    * Test {@link IoUtility#stringToReader(String))}.
578    *
579    * @throws Exception Test failed.
580    */
 
581  1 toggle public void testStringToReader() throws Exception {
582  1 final String expected = "We all live in a yellow submarine\n"
583    + "Yellow submarine, yellow submarine\n"
584    + "We all live in a yellow submarine\n"
585    + "Yellow submarine, yellow submarine\n"
586    + "\n"
587    + "As we live a life of ease\n"
588    + "Everyone of us has all we need\n"
589    + "Sky of blue and sea of green\n"
590    + "In our yellow submarine ";
591  1 final Reader reader = IoUtility.stringToReader(expected);
592  1 final StringBuffer buffer = new StringBuffer();
593  1 int c;
594  ? while (0 <= (c = (reader.read()))) {
595  249 buffer.append((char) c);
596    }
597  1 assertEquals(expected, buffer.toString());
598    }
599   
600    /**
601    * Test {@link IoUtility#saveFile(File, String))}.
602    *
603    * @throws Exception Test failed.
604    */
 
605  1 toggle public void testSaveFileFileString() throws Exception {
606  1 final File file1 = new File("testSaveFileFileString1.txt");
607  1 final File file2 = new File("testSaveFileFileString2.txt");
608  1 InputStream in = null;
609  1 try {
610  1 if (file1.exists()) {
611  0 assertTrue(file1.delete());
612    }
613  1 if (file2.exists()) {
614  0 assertTrue(file2.delete());
615    }
616  1 IoUtility.saveFile(file1, StringUtility.hex2String(
617    "FF FE 49 00 20 00 61 00 6D 00 20 00 64 00 72 00"
618    + "65 00 61 00 6D 00 69 00 6E 00 67 00 20 00 6F 00"
619    + "66 00 20 00 61 00 20 00 77 00 68 00 69 00 74 00"
620    + "65 00 20 00 63 00 68 00 72 00 69 00 73 00 74 00"
621    + "6D 00 61 00 73 00 73 00 2E 00 2E 00 2E 00"));
622  1 in = new FileInputStream(file1);
623  1 IoUtility.saveFile(in, file2);
624  1 assertTrue(IoUtility.compareFilesBinary(file1, file2));
625    } finally {
626  1 if (in != null) {
627  1 in.close();
628    }
629    }
630  1 assertTrue(file1.delete());
631  1 assertTrue(file2.delete());
632    }
633   
634    /**
635    * Test {@link IoUtility#saveFile(File, StringBuffer))}.
636    *
637    * @throws Exception Test failed.
638    */
 
639  1 toggle public void testSaveFileFileStringBuffer() throws Exception {
640  1 final File file1 = new File("testSaveFileFileStringBuffer1.txt");
641  1 final File file2 = new File("testSaveFileFileStringBuffer2.txt");
642  1 InputStream in = null;
643  1 try {
644  1 if (file1.exists()) {
645  0 assertTrue(file1.delete());
646    }
647  1 if (file2.exists()) {
648  0 assertTrue(file2.delete());
649    }
650  1 final StringBuffer buffer = new StringBuffer(StringUtility.hex2String(
651    "FF FE 49 00 20 00 61 00 6D 00 20 00 64 00 72 00"
652    + "65 00 61 00 6D 00 69 00 6E 00 67 00 20 00 6F 00"
653    + "66 00 20 00 61 00 20 00 77 00 68 00 69 00 74 00"
654    + "65 00 20 00 63 00 68 00 72 00 69 00 73 00 74 00"
655    + "6D 00 61 00 73 00 73 00 2E 00 2E 00 2E 00"));
656  1 IoUtility.saveFile(file1, buffer);
657  1 in = new FileInputStream(file1);
658  1 IoUtility.saveFile(in, file2);
659  1 assertTrue(IoUtility.compareFilesBinary(file1, file2));
660    } finally {
661  1 if (in != null) {
662  1 in.close();
663    }
664    }
665  1 assertTrue(file1.delete());
666  1 assertTrue(file2.delete());
667    }
668   
669    /**
670    * Test {@link IoUtility#saveFile(File, String, String)))}.
671    *
672    * @throws Exception Test failed.
673    */
 
674  1 toggle public void testSaveFileFileStringString() throws Exception {
675  1 final File file1 = new File("testSaveFileFileStringString1.txt");
676  1 final File file2 = new File("testSaveFileFileStringString2.txt");
677  1 final File file3 = new File("testSaveFileFileStringString3.txt");
678  1 InputStream in = null;
679  1 try {
680  1 if (file1.exists()) {
681  0 assertTrue(file1.delete());
682    }
683  1 if (file2.exists()) {
684  0 assertTrue(file2.delete());
685    }
686  1 if (file3.exists()) {
687  0 assertTrue(file3.delete());
688    }
689  1 final String data = StringUtility.hex2String(
690    "FF FE 49 00 20 00 61 00 6D 00 20 00 64 00 72 00"
691    + "65 00 61 00 6D 00 69 00 6E 00 67 00 20 00 6F 00"
692    + "66 00 20 00 61 00 20 00 77 00 68 00 69 00 74 00"
693    + "65 00 20 00 63 00 68 00 72 00 69 00 73 00 74 00"
694    + "6D 00 61 00 73 00 73 00 2E 00 2E 00 2E 00");
695  1 IoUtility.saveFile(file1, data, "ISO-8859-1");
696  1 in = new FileInputStream(file1);
697  1 IoUtility.saveFile(in, file2);
698  1 assertTrue(IoUtility.compareFilesBinary(file1, file2));
699  1 IoUtility.saveFile(file3, "I am dreaming of a white christmass...", "UTF-16");
700  1 assertEquals("I am dreaming of a white christmass...", IoUtility.loadFile(
701    file3.toString(), "UTF-16"));
702    } finally {
703  1 if (in != null) {
704  1 in.close();
705    }
706    }
707  1 assertTrue(file1.delete());
708  1 assertTrue(file2.delete());
709  1 assertTrue(file3.delete());
710    }
711   
712    /**
713    * Test {@link IoUtility#saveFile(String, String)}.
714    *
715    * @throws Exception Test failed.
716    */
 
717  1 toggle public void testSaveFileStringString() throws Exception {
718  1 final File file1 = new File("testSaveFileStringString1.txt");
719  1 final File file2 = new File("testSaveFileStringString2.txt");
720  1 InputStream in = null;
721  1 try {
722  1 if (file1.exists()) {
723  0 assertTrue(file1.delete());
724    }
725  1 if (file2.exists()) {
726  0 assertTrue(file2.delete());
727    }
728  1 final StringBuffer buffer = new StringBuffer(StringUtility.hex2String(
729    "FF FE 49 00 20 00 61 00 6D 00 20 00 64 00 72 00"
730    + "65 00 61 00 6D 00 69 00 6E 00 67 00 20 00 6F 00"
731    + "66 00 20 00 61 00 20 00 77 00 68 00 69 00 74 00"
732    + "65 00 20 00 63 00 68 00 72 00 69 00 73 00 74 00"
733    + "6D 00 61 00 73 00 73 00 2E 00 2E 00 2E 00"));
734  1 IoUtility.saveFile(file1.toString(), buffer.toString());
735  1 in = new FileInputStream(file1);
736  1 IoUtility.saveFile(in, file2);
737  1 assertTrue(IoUtility.compareFilesBinary(file1, file2));
738    } finally {
739  1 if (in != null) {
740  1 in.close();
741    }
742    }
743  1 assertTrue(file1.delete());
744  1 assertTrue(file2.delete());
745    }
746   
747    /**
748    * Test {@link IoUtility#saveFile(String, StringBuffer)}.
749    *
750    * @throws Exception Test failed.
751    */
 
752  1 toggle public void testSaveFileStringStringBuffer() throws Exception {
753  1 final File file1 = new File("testSaveFileStringString1.txt");
754  1 final File file2 = new File("testSaveFileStringString2.txt");
755  1 InputStream in = null;
756  1 try {
757  1 if (file1.exists()) {
758  0 assertTrue(file1.delete());
759    }
760  1 if (file2.exists()) {
761  0 assertTrue(file2.delete());
762    }
763  1 final StringBuffer buffer = new StringBuffer(StringUtility.hex2String(
764    "FF FE 49 00 20 00 61 00 6D 00 20 00 64 00 72 00"
765    + "65 00 61 00 6D 00 69 00 6E 00 67 00 20 00 6F 00"
766    + "66 00 20 00 61 00 20 00 77 00 68 00 69 00 74 00"
767    + "65 00 20 00 63 00 68 00 72 00 69 00 73 00 74 00"
768    + "6D 00 61 00 73 00 73 00 2E 00 2E 00 2E 00"));
769  1 IoUtility.saveFile(file1.toString(), buffer);
770  1 in = new FileInputStream(file1);
771  1 IoUtility.saveFile(in, file2);
772  1 assertTrue(IoUtility.compareFilesBinary(file1, file2));
773    } finally {
774  1 if (in != null) {
775  1 in.close();
776    }
777    }
778  1 assertTrue(file1.delete());
779  1 assertTrue(file2.delete());
780    }
781   
782    /**
783    * Test {@link IoUtility#saveFile(File, StringBuffer, String)}.
784    *
785    * @throws Exception Test failed.
786    */
 
787  1 toggle public void testSaveFileFileStringBufferString() throws Exception {
788  1 final File file1 = new File("testSaveFileFileStringBufferString1.txt");
789  1 final File file2 = new File("testSaveFileFileStringBufferString2.txt");
790  1 final File file3 = new File("testSaveFileFileStringBufferString3.txt");
791  1 InputStream in = null;
792  1 try {
793  1 if (file1.exists()) {
794  0 assertTrue(file1.delete());
795    }
796  1 if (file2.exists()) {
797  0 assertTrue(file2.delete());
798    }
799  1 if (file3.exists()) {
800  0 assertTrue(file3.delete());
801    }
802  1 final String data = StringUtility.hex2String(
803    "FF FE 49 00 20 00 61 00 6D 00 20 00 64 00 72 00"
804    + "65 00 61 00 6D 00 69 00 6E 00 67 00 20 00 6F 00"
805    + "66 00 20 00 61 00 20 00 77 00 68 00 69 00 74 00"
806    + "65 00 20 00 63 00 68 00 72 00 69 00 73 00 74 00"
807    + "6D 00 61 00 73 00 73 00 2E 00 2E 00 2E 00");
808  1 final StringBuffer buffer = new StringBuffer(data);
809  1 IoUtility.saveFile(file1, buffer, "ISO-8859-1");
810  1 in = new FileInputStream(file1);
811  1 IoUtility.saveFile(in, file2);
812  1 assertTrue(IoUtility.compareFilesBinary(file1, file2));
813  1 buffer.setLength(0);
814  1 buffer.append("I am dreaming of a white christmass...");
815  1 IoUtility.saveFile(file3, buffer, "UTF-16");
816  1 assertEquals("I am dreaming of a white christmass...", IoUtility.loadFile(
817    file3.toString(), "UTF-16"));
818    } finally {
819  1 if (in != null) {
820  1 in.close();
821    }
822    }
823  1 assertTrue(file1.delete());
824  1 assertTrue(file2.delete());
825  1 assertTrue(file3.delete());
826    }
827   
828    /**
829    * Test {@link IoUtility#copyFile(File, File)}.
830    *
831    * @throws Exception Test failed.
832    */
 
833  1 toggle public void testCopyFile() throws Exception {
834  1 final File file1 = new File("IoUtilityTestCopyFile1.bin");
835  1 if (file1.exists()) {
836  0 assertTrue(file1.delete());
837    }
838  1 final File file2 = new File("IoUtilityTestCopyFile2.bin");
839  1 if (file2.exists()) {
840  0 assertTrue(file2.delete());
841    }
842  1 Random random = new Random(1007);
843  1 final byte[] data = new byte[1025];
844  1026 for (int i = 0; i < data.length; i++) {
845  1025 data[i] = (byte) random.nextInt();
846    }
847  1 IoUtility.saveFileBinary(file1, data);
848  1 IoUtility.copyFile(file1, file1);
849  1 IoUtility.copyFile(file1, file2);
850  1 assertTrue(IoUtility.compareFilesBinary(file1, file2));
851  1 file1.delete();
852  1 file2.delete();
853    }
854   
855   
856    /**
857    * Test {@link IoUtility#compareTextFiles(File, File, String)}.
858    *
859    * @throws Exception Test failed.
860    */
 
861  1 toggle public void testCompareTextFiles1() throws Exception {
862  1 final File file1 = new File("testCompareTextFiles1.txt");
863  1 if (file1.exists()) {
864  0 assertTrue(file1.delete());
865    }
866  1 final File file2 = new File("testCompareTextFiles2.txt");
867  1 if (file2.exists()) {
868  0 assertTrue(file2.delete());
869    }
870  1 assertFalse(IoUtility.compareTextFiles(null, file1, "ISO-8859-1"));
871  1 assertFalse(IoUtility.compareTextFiles(file2, null, "ISO-8859-1"));
872  1 try {
873  1 assertTrue(IoUtility.compareTextFiles(file1, file2, "ISO-8859-1"));
874  0 fail("FileNotFoundException expected");
875    } catch (FileNotFoundException e) {
876    // expected;
877    }
878  1 Random random = new Random(1001);
879  1 final byte[] data = new byte[1025];
880  1026 for (int i = 0; i < data.length; i++) {
881  1025 data[i] = (byte) random.nextInt();
882    }
883  1 IoUtility.saveFileBinary(file1, data);
884  1 IoUtility.saveFileBinary(file2, data);
885  1 try {
886  1 IoUtility.compareTextFiles(file1, file2, "iso");
887  0 fail("UnsupportedEncodingException expected");
888    } catch (UnsupportedEncodingException e) {
889    // expected;
890    }
891  1 try {
892  1 IoUtility.compareTextFiles(file1, file2, null);
893  0 fail("NullPointerException expected");
894    } catch (NullPointerException e) {
895    // expected;
896    }
897  1 assertTrue(IoUtility.compareTextFiles(file1, file2, "ISO-8859-1"));
898  1 data[1000] += 1;
899  1 IoUtility.saveFileBinary(file2, data);
900  1 assertFalse(IoUtility.compareTextFiles(file1, file2, "ISO-8859-1"));
901  1 data[999] -= 1;
902  1 IoUtility.saveFileBinary(file2, data);
903  1 assertFalse(IoUtility.compareTextFiles(file1, file2, "ISO-8859-1"));
904  1 IoUtility.saveFileBinary(file2, new byte[] {});
905  1 assertEquals(0, file2.length());
906  1 assertFalse(IoUtility.compareTextFiles(file1, file2, "ISO-8859-1"));
907  1 assertTrue(IoUtility.compareTextFiles(file1, file1, "ISO-8859-1"));
908  1 assertTrue(IoUtility.compareTextFiles(file2, file2, "ISO-8859-1"));
909  1 assertTrue(file1.delete());
910  1 assertTrue(file2.delete());
911    }
912   
913    /**
914    * Test {@link IoUtility#compareTextFiles(File, File, String)}.
915    *
916    * @throws Exception Test failed.
917    */
 
918  1 toggle public void testCompareTextFiles2() throws Exception {
919  1 final String text = "When she goes, shes gone.@" + "If she stays, she stays here.@"
920    + "The girl does what she wants to do.@" + "She knows what she wants to do.@"
921    + "And I know Im fakin it,@" + "Im not really makin it.@" + "@"
922    + "Im such a dubious soul,@" + "And a walk in the garden@" + "Wears me down.@"
923    + "Tangled in the fallen vines,@" + "Pickin up the punch lines,@"
924    + "Ive just been fakin it,@" + "Not really makin it.@" + "@" + "Is there any danger?@"
925    + "No, no, not really.@" + "Just lean on me.@" + "Takin time to treat@"
926    + "Your friendly neighbors honestly.@" + "Ive just been fakin it,@"
927    + "Im not really makin it.@" + "This feeling of fakin it--@"
928    + "I still havent shaken it.@" + "@" + "Prior to this lifetime@"
929    + "I surely was a tailor.@" + "(good morning, mr. leitch.@"
930    + "Have you had a busy day? )@" + "I own the tailors face and hands.@"
931    + "I am the tailors face and hands and@" + "I know Im fakin it,@"
932    + "Im not really makin it.@" + "This feeling of fakin it--@"
933    + "I still havent shaken it.";
934  1 final File file1 = new File("testCompareTextFiles1.txt");
935  1 if (file1.exists()) {
936  0 assertTrue(file1.delete());
937    }
938  1 final File file2 = new File("testCompareTextFiles2.txt");
939  1 if (file2.exists()) {
940  0 assertTrue(file2.delete());
941    }
942  1 IoUtility.saveFile(file1, "Line1", "ISO-8859-1");
943  1 IoUtility.saveFile(file2, "Line1", "ISO-8859-1");
944   
945  1 assertFalse(IoUtility.compareTextFiles(null, file1, "ISO-8859-1"));
946  1 assertFalse(IoUtility.compareTextFiles(file2, null, "ISO-8859-1"));
947  1 assertTrue(IoUtility.compareTextFiles(null, null, "ISO-8859-1"));
948  1 try {
949  1 IoUtility.compareTextFiles(file1, file2, "iso");
950  0 fail("UnsupportedEncodingException expected");
951    } catch (UnsupportedEncodingException e) {
952    // expected;
953    }
954  1 IoUtility.saveFile(file2, "line1", "ISO-8859-1");
955  1 assertFalse(IoUtility.compareTextFiles(file1, file2, "ISO-8859-1"));
956  1 assertFalse(IoUtility.compareTextFiles(file2, file1, "ISO-8859-1"));
957   
958  1 IoUtility.saveFile(file1, StringUtility.replace(text, "@", "\n"), "ISO-8859-1");
959  1 IoUtility.saveFile(file2, StringUtility.replace(text, "@", "\n"), "ISO-8859-1");
960  1 assertTrue(IoUtility.compareTextFiles(file1, file2, "ISO-8859-1"));
961  1 assertTrue(IoUtility.compareTextFiles(file2, file1, "ISO-8859-1"));
962   
963  1 IoUtility.saveFile(file1, StringUtility.replace(text, "@", "\015\012"), "ISO-8859-1");
964  1 IoUtility.saveFile(file2, StringUtility.replace(text, "@", "\015\012"), "ISO-8859-1");
965  1 assertTrue(IoUtility.compareTextFiles(file1, file2, "ISO-8859-1"));
966  1 assertTrue(IoUtility.compareTextFiles(file2, file1, "ISO-8859-1"));
967   
968  1 IoUtility.saveFile(file1, StringUtility.replace(text, "@", "\012"), "ISO-8859-1");
969  1 IoUtility.saveFile(file2, StringUtility.replace(text, "@", "\015\012"), "ISO-8859-1");
970  1 assertTrue(IoUtility.compareTextFiles(file1, file2, "ISO-8859-1"));
971  1 assertTrue(IoUtility.compareTextFiles(file2, file1, "ISO-8859-1"));
972   
973  1 IoUtility.saveFile(file1, StringUtility.replace(text + "@", "@", "\012"), "ISO-8859-1");
974  1 IoUtility.saveFile(file2, StringUtility.replace(text, "@", "\015\012"), "ISO-8859-1");
975  1 assertTrue(IoUtility.compareTextFiles(file1, file2, "ISO-8859-1"));
976  1 assertTrue(IoUtility.compareTextFiles(file2, file1, "ISO-8859-1"));
977   
978  1 IoUtility.saveFile(file1, StringUtility.replace(text, "@", "\012"), "ISO-8859-1");
979  1 IoUtility.saveFile(file2, StringUtility.replace(text + "@", "@", "\015\012"), "ISO-8859-1");
980  1 assertTrue(IoUtility.compareTextFiles(file1, file2, "ISO-8859-1"));
981  1 assertTrue(IoUtility.compareTextFiles(file2, file1, "ISO-8859-1"));
982   
983  1 IoUtility.saveFile(file1, StringUtility.replace(text + "@", "@", "\012"), "ISO-8859-1");
984  1 IoUtility.saveFile(file2, StringUtility.replace(text + "@", "@", "\015\012"), "ISO-8859-1");
985  1 assertTrue(IoUtility.compareTextFiles(file1, file2, "ISO-8859-1"));
986  1 assertTrue(IoUtility.compareTextFiles(file2, file1, "ISO-8859-1"));
987  1 assertTrue(IoUtility.compareTextFiles(file1, file2, "UTF8"));
988  1 assertTrue(IoUtility.compareTextFiles(file2, file1, "UTF8"));
989  1 assertFalse(IoUtility.compareTextFiles(file1, file2, "UTF-16"));
990  1 assertFalse(IoUtility.compareTextFiles(file2, file1, "UTF-16"));
991   
992  1 IoUtility.saveFile(file1, StringUtility.replace(text, "@", "\012"), "ISO-8859-1");
993  1 IoUtility.saveFile(file2, StringUtility.replace(text, "@", "\012"), "UTF8");
994  1 assertTrue(IoUtility.compareTextFiles(file1, file2, "ISO-8859-1"));
995  1 assertTrue(IoUtility.compareTextFiles(file2, file1, "ISO-8859-1"));
996  1 assertTrue(IoUtility.compareTextFiles(file1, file2, "UTF8"));
997  1 assertTrue(IoUtility.compareTextFiles(file2, file1, "UTF8"));
998  1 assertTrue(IoUtility.compareTextFiles(file1, file2, "UTF-16"));
999  1 assertTrue(IoUtility.compareTextFiles(file2, file1, "UTF-16"));
1000   
1001  1 IoUtility.saveFile(file1, StringUtility.replace(text, "@", "\012"), "UTF16");
1002  1 IoUtility.saveFile(file2, StringUtility.replace(text, "@", "\012"), "UTF8");
1003  1 assertFalse(IoUtility.compareTextFiles(file1, file2, "UTF-16"));
1004  1 assertFalse(IoUtility.compareTextFiles(file2, file1, "UTF8"));
1005   
1006  1 IoUtility.saveFile(file1, StringUtility.replace(text, "@", "\012"), "UTF-16");
1007  1 IoUtility.saveFile(file2, StringUtility.replace(text, "@", "\012"), "UTF-16");
1008  1 assertTrue(IoUtility.compareTextFiles(file1, file2, "ISO-8859-1"));
1009  1 assertTrue(IoUtility.compareTextFiles(file2, file1, "ISO-8859-1"));
1010  1 assertTrue(IoUtility.compareTextFiles(file1, file2, "UTF8"));
1011  1 assertTrue(IoUtility.compareTextFiles(file2, file1, "UTF8"));
1012  1 assertTrue(IoUtility.compareTextFiles(file1, file2, "UTF-16"));
1013  1 assertTrue(IoUtility.compareTextFiles(file2, file1, "UTF-16"));
1014   
1015  1 IoUtility.saveFile(file1, StringUtility.replace(text, "@", "\012"), "UTF-16");
1016  1 IoUtility.saveFile(file2, StringUtility.replace(text, "@", "\012\015"), "UTF-16");
1017  1 assertFalse(IoUtility.compareTextFiles(file1, file2, "ISO-8859-1"));
1018  1 assertFalse(IoUtility.compareTextFiles(file2, file1, "ISO-8859-1"));
1019  1 assertFalse(IoUtility.compareTextFiles(file1, file2, "UTF8"));
1020  1 assertFalse(IoUtility.compareTextFiles(file2, file1, "UTF8"));
1021  1 assertTrue(IoUtility.compareTextFiles(file1, file2, "UTF16"));
1022  1 assertTrue(IoUtility.compareTextFiles(file2, file1, "UTF16"));
1023   
1024  1 IoUtility.saveFile(file1, StringUtility.replace(text, "@", "\012"), "UTF-16");
1025  1 IoUtility.saveFile(file2, StringUtility.replace(text, "@", "\015\012"), "UTF-16");
1026  1 assertFalse(IoUtility.compareTextFiles(file1, file2, "ISO-8859-1"));
1027  1 assertFalse(IoUtility.compareTextFiles(file2, file1, "ISO-8859-1"));
1028  1 assertFalse(IoUtility.compareTextFiles(file1, file2, "UTF8"));
1029  1 assertFalse(IoUtility.compareTextFiles(file2, file1, "UTF8"));
1030  1 assertTrue(IoUtility.compareTextFiles(file1, file2, "UTF-16"));
1031  1 assertTrue(IoUtility.compareTextFiles(file2, file1, "UTF-16"));
1032   
1033  1 IoUtility.saveFile(file1, StringUtility.replace(text, "@", "\012"), "ISO-8859-1");
1034  1 IoUtility.saveFile(file2, StringUtility.replace(text, "@", "\015"), "ISO-8859-1");
1035  1 assertTrue(IoUtility.compareTextFiles(file1, file2, "ISO-8859-1"));
1036  1 assertTrue(IoUtility.compareTextFiles(file2, file1, "ISO-8859-1"));
1037  1 assertTrue(IoUtility.compareTextFiles(file1, file2, "UTF8"));
1038  1 assertTrue(IoUtility.compareTextFiles(file2, file1, "UTF8"));
1039  1 assertFalse(IoUtility.compareTextFiles(file1, file2, "UTF-16"));
1040  1 assertFalse(IoUtility.compareTextFiles(file2, file1, "UTF-16"));
1041   
1042  1 IoUtility.saveFile(file1, StringUtility.replace(text, "@", "\015\012"), "ISO-8859-1");
1043  1 IoUtility.saveFile(file2, StringUtility.replace(text, "@", "" + (char) 0x2029),
1044    "ISO-8859-1");
1045  1 assertTrue(IoUtility.compareTextFiles(file1, file2, "ISO-8859-1"));
1046  1 assertTrue(IoUtility.compareTextFiles(file2, file1, "ISO-8859-1"));
1047  1 assertTrue(IoUtility.compareTextFiles(file1, file2, "UTF8"));
1048  1 assertTrue(IoUtility.compareTextFiles(file2, file1, "UTF8"));
1049  1 assertFalse(IoUtility.compareTextFiles(file1, file2, "UTF-16"));
1050  1 assertFalse(IoUtility.compareTextFiles(file2, file1, "UTF-16"));
1051   
1052  1 assertTrue(file1.delete());
1053  1 assertTrue(file2.delete());
1054    }
1055   
1056    /**
1057    * Test {@link IoUtility#testCompareTextFiles(File, File, String)}.
1058    *
1059    * @throws Exception Test failed.
1060    */
 
1061  1 toggle public void testDeleteDirFileBoolean() throws Exception {
1062  1 final File dir1 = new File("testDeleteDirFileBoolean_directory");
1063  1 if (dir1.exists()) {
1064  0 assertTrue(IoUtility.deleteDir(dir1, true));
1065    }
1066  1 final File dir2 = new File(dir1, "testDeleteDirFileBoolean_directory2");
1067  1 final File file1 = new File(dir1, "testDeleteDirFileBoolean1.txt");
1068  1 final File file2 = new File(dir1, "testDeleteDirFileBoolean2.txt");
1069  1 final File file3 = new File(dir2, "testDeleteDirFileBoolean3.txt");
1070  1 assertTrue(dir2.mkdirs());
1071   
1072  1 IoUtility.saveFile(file1, "File 1", "ISO-8859-1");
1073  1 IoUtility.saveFile(file2, "File 2", "ISO-8859-1");
1074  1 IoUtility.saveFile(file3, "File 3", "ISO-8859-1");
1075  1 assertTrue(IoUtility.deleteDir(dir1, true));
1076  1 assertTrue(!dir1.exists());
1077    }
1078   
1079    /**
1080    * Test {@link IoUtility#testCompareTextFiles(File, File, String)}.
1081    *
1082    * @throws Exception Test failed.
1083    */
 
1084  1 toggle public void testDeleteDirFileFileFilter() throws Exception {
1085  1 final File dir1 = new File("testDeleteDirFileFileFilter_directory");
1086  1 if (dir1.exists()) {
1087  0 assertTrue(IoUtility.deleteDir(dir1, true));
1088    }
1089  1 final File dir2 = new File(dir1, "testDeleteDirFileFileFilter_directory2");
1090  1 final File file1 = new File(dir1, "testDeleteDirFileFileFilter1.txt");
1091  1 final File file2 = new File(dir1, "testDeleteDirFileFileFilter2.txt");
1092  1 final File file3 = new File(dir2, "testDeleteDirFileFileFilter_3.txt");
1093  1 assertTrue(dir2.mkdirs());
1094   
1095  1 IoUtility.saveFile(file1, "File 1", "ISO-8859-1");
1096  1 IoUtility.saveFile(file2, "File 2", "ISO-8859-1");
1097  1 IoUtility.saveFile(file3, "File 3", "ISO-8859-1");
1098  1 assertTrue(IoUtility.deleteDir(dir1, new FileFilter() {
1099   
 
1100  3 toggle public boolean accept(final File pathname) {
1101  3 return pathname.toString().indexOf("testDeleteDirFileFileFilter_") >= 0;
1102    }
1103    }));
1104  1 assertTrue(dir1.exists());
1105  1 assertFalse(dir2.exists());
1106  1 assertFalse(file1.exists());
1107  1 assertFalse(file2.exists());
1108  1 assertFalse(file3.exists());
1109    }
1110   
1111    }