Clover Coverage Report
Coverage timestamp: Fri May 24 2013 13:47:27 UTC
905   1,663   177   12.4
188   1,353   0.2   73
73     2.42  
1    
 
  IoUtilityTest       Line # 46 905 177 88.8% 0.8876501
 
  (50)
 
1    /* This file is part of the project "Hilbert II" - http://www.qedeq.org
2    *
3    * Copyright 2000-2013, 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.List;
34    import java.util.Properties;
35    import java.util.Random;
36   
37    import org.qedeq.base.test.QedeqTestCase;
38    import org.qedeq.base.utility.EqualsUtility;
39    import org.qedeq.base.utility.StringUtility;
40   
41    /**
42    * Test {@link org.qedeq.kernel.utility.IoUtility}.
43    *
44    * @author Michael Meyling
45    */
 
46    public class IoUtilityTest extends QedeqTestCase {
47   
48   
 
49  1 toggle public void testPrintAllSystemProperties() {
50    // we just call the method - at least we have some infos in the log about system properties
51  1 IoUtility.printAllSystemProperties();
52    }
53   
54    /**
55    * Test {@link IoUtility#getDefaultEncoding()}.
56    */
 
57  1 toggle public void testGetDefaultEncoding() {
58  1 assertEquals(System.getProperty("file.encoding"), IoUtility.getDefaultEncoding());
59  1 final String encoding = new InputStreamReader(new ByteArrayInputStream(
60    new byte[0])).getEncoding();
61    // UTF-8 and UTF8 are the same, so we remove all "-" ...
62  1 if (!StringUtility.replace(System.getProperty("file.encoding"), "-", "").equals(
63    StringUtility.replace(encoding, "-", ""))) {
64  1 System.out.println("This system showed the java property \"file.encoding\" "
65    + System.getProperty("file.encoding") + " but the detected writing default is: "
66    + "\"" + IoUtility.getDefaultEncoding()
67    + "\"\nThis might be ok, but you should check it");
68    }
69    }
70   
71    /**
72    * Test {@link IoUtility#loadFile(String fileName, String encoding)}.
73    *
74    * @throws Exception Test failed.
75    */
 
76  1 toggle public void testLoadFileStringString() throws Exception {
77  1 final File file = new File(getOutdir(), "IoUtilityTestLoadStringString.txt");
78  1 if (file.exists()) {
79  0 assertTrue(file.delete());
80    }
81  1 IoUtility.saveFileBinary(file, StringUtility.hex2byte(
82    "FF FE 49 00 20 00 61 00 6D 00 20 00 64 00 72 00"
83    + "65 00 61 00 6D 00 69 00 6E 00 67 00 20 00 6F 00"
84    + "66 00 20 00 61 00 20 00 77 00 68 00 69 00 74 00"
85    + "65 00 20 00 63 00 68 00 72 00 69 00 73 00 74 00"
86    + "6D 00 61 00 73 00 73 00 2E 00 2E 00 2E 00"));
87  1 assertEquals("I am dreaming of a white christmass...", IoUtility.loadFile(file.toString(),
88    "UTF16"));
89  1 assertTrue(file.delete());
90    }
91   
92    /**
93    * Test {@link IoUtility#getWorkingEncoding(String)}.
94    *
95    * @throws Exception Test failed.
96    */
 
97  1 toggle public void testGetWorkingEncoding() throws Exception {
98  1 assertEquals("ISO-8859-1", IoUtility.getWorkingEncoding("unknown"));
99  1 System.err.println("^ above text is ok, this was intended test behaviour");
100  1 assertEquals("UTF-8", IoUtility.getWorkingEncoding("UTF-8"));
101  1 assertEquals("UTF-16", IoUtility.getWorkingEncoding("UTF-16"));
102  1 assertEquals("UTF8", IoUtility.getWorkingEncoding("UTF8"));
103  1 assertEquals("ISO-8859-1", IoUtility.getWorkingEncoding(null));
104  1 System.err.println("^ above text is ok, this was intended test behaviour");
105    }
106   
107    /**
108    * Test {@link IoUtility#loadStream(InputStream, StringBuffer)}.
109    *
110    * @throws Exception Test failed.
111    */
 
112  1 toggle public void testLoadStreamInputStreamStringBuffer() throws Exception {
113  1 final File file = new File(getOutdir(), "testLoadStreamInputStreamStringBuffer.txt");
114  1 if (file.exists()) {
115  0 assertTrue(file.delete());
116    }
117  1 final OutputStream out = new FileOutputStream(file);
118  1 final String expected = "We all live in a yellow submarine ...";
119  1 out.write(expected.getBytes());
120  1 out.close();
121  1 final StringBuffer buffer = new StringBuffer();
122  1 final InputStream in = new FileInputStream(file);
123  1 IoUtility.loadStream(in, buffer);
124  1 in.close();
125  1 assertEquals(expected, buffer.toString());
126  1 assertTrue(file.delete());
127    }
128   
129    /**
130    * Test {@link IoUtility#loadStreamWithoutException(InputStream, int)}.
131    *
132    * @throws Exception Test failed.
133    */
 
134  1 toggle public void testLoadStreamWithoutException() throws Exception {
135  1 final String expected = "We all live in a yellow submarine ...";
136  1 assertEquals("", IoUtility.loadStreamWithoutException(null, 100));
137  1 assertEquals("", IoUtility.loadStreamWithoutException(null, -100));
138  1 assertEquals("", IoUtility.loadStreamWithoutException(new InputStream() {
 
139  0 toggle public int read() throws IOException {
140  0 throw new IOException("i am an error input stream!");
141    }
142    }, 0));
143  1 assertEquals("", IoUtility.loadStreamWithoutException(new InputStream() {
 
144  0 toggle public int read() throws IOException {
145  0 throw new IOException("i am an error input stream!");
146    }
147    }, -99));
148  1 assertEquals(expected, streamLoad(expected, expected.length()));
149  1 assertEquals(expected, streamLoad(expected, expected.length() * 100));
150  1 assertEquals(expected.substring(0, expected.length() - 1),
151    streamLoad(expected, expected.length() - 1));
152  1 assertEquals(expected.substring(0, 1),
153    streamLoad(expected, 1));
154  1 assertEquals("", streamLoad(expected, -2));
155   
156    }
157   
 
158  5 toggle private String streamLoad(final String expected, final int length) throws IOException {
159  5 final File file = new File(getOutdir(), "testLoadStreamWithoutException.txt");
160  5 if (file.exists()) {
161  0 assertTrue(file.delete());
162    }
163  5 try {
164  5 final OutputStream out = new FileOutputStream(file);
165  5 try {
166  5 out.write(expected.getBytes());
167    } finally {
168  5 out.close();
169    }
170  5 final InputStream in = new FileInputStream(file);
171  5 try {
172  5 return IoUtility.loadStreamWithoutException(in, length);
173    } finally {
174    // assert that stream is not closed yet
175  5 in.close();
176    }
177    } finally {
178  5 assertTrue(file.delete());
179    }
180    }
181   
182    /**
183    * Test {@link IoUtility#loadReader(Reader, StringBuffer)}.
184    *
185    * @throws Exception Test failed.
186    */
 
187  1 toggle public void testLoadReader() throws Exception {
188  1 final File file = new File(getOutdir(), "testLoadReaderStringBuffer.txt");
189  1 if (file.exists()) {
190  0 assertTrue(file.delete());
191    }
192  1 final Writer out = new FileWriter(file);
193  1 final String expected = "We all live in a yellow submarine ...";
194  1 out.write(expected);
195  1 out.close();
196  1 final StringBuffer buffer = new StringBuffer();
197  1 final Reader in = new FileReader(file);
198  1 IoUtility.loadReader(in, buffer);
199    // test if reader can be closed
200  1 in.close();
201  1 assertEquals(expected, buffer.toString());
202  1 assertTrue(file.delete());
203    }
204   
205    /**
206    * Test {@link IoUtility#loadFile(File, StringBuffer)}.
207    *
208    * @throws Exception Test failed.
209    */
 
210  1 toggle public void testLoadFileFileStringBuffer() throws Exception {
211  1 final File file = new File(getOutdir(), "testLoadFileFileStringBuffer.txt");
212  1 if (file.exists()) {
213  0 assertTrue(file.delete());
214    }
215  1 final Writer out = new FileWriter(file);
216  1 final String expected = "We all live in a yellow submarine ...";
217  1 out.write(expected);
218  1 out.close();
219  1 final StringBuffer buffer = new StringBuffer();
220  1 IoUtility.loadFile(file, buffer);
221  1 assertEquals(expected, buffer.toString());
222  1 assertTrue(file.delete());
223    }
224   
225    /**
226    * Test {@link IoUtility#loadFile(File, StringBuffer, String)}.
227    *
228    * @throws Exception Test failed.
229    */
 
230  1 toggle public void testLoadFileFileStringBufferString() throws Exception {
231  1 final File file = new File(getOutdir(), "testLoadFileFileStringBufferString.txt");
232  1 if (file.exists()) {
233  0 assertTrue(file.delete());
234    }
235  1 final String expected1 = "We all live in a yellow submarine ...";
236  1 final String encoding1 = "UTF-16";
237  1 assertEquals(expected1, loadFile(file, expected1, encoding1));
238  1 final String expected2 = "We all live in a yellow submarine\n"
239    + "Yellow submarine, yellow submarine\n"
240    + "We all live in a yellow submarine\n"
241    + "Yellow submarine, yellow submarine\n"
242    + "\n"
243    + "As we live a life of ease\n"
244    + "Everyone of us has all we need\n"
245    + "Sky of blue and sea of green\n"
246    + "In our yellow submarine ";
247  1 final String encoding2 = "UnicodeBigUnmarked";
248  1 assertEquals(expected2, loadFile(file, expected2, encoding2));
249  1 final String encoding3 = "ASCII";
250  1 assertEquals(expected2, loadFile(file, expected2, encoding3));
251  1 assertTrue(file.delete());
252    }
253   
 
254  3 toggle private String loadFile(final File file, final String expected, final String encoding)
255    throws IOException {
256  3 final OutputStream out = new FileOutputStream(file);
257  3 try {
258  3 out.write(expected.getBytes(encoding));
259    } finally {
260  3 out.close();
261    }
262  3 final StringBuffer buffer = new StringBuffer();
263  3 IoUtility.loadFile(file, buffer, encoding);
264  3 return buffer.toString();
265    }
266   
267    /**
268    * Test {@link IoUtility#loadFileBinary(File)}.
269    *
270    * @throws Exception Test failed.
271    */
 
272  1 toggle public void testLoadFileBinary1() throws Exception {
273  1 final File file = new File(getOutdir(), "IoUtilityTestLoadBinary1.bin");
274  1 if (file.exists()) {
275  0 assertTrue(file.delete());
276    }
277  1 final OutputStream out = new FileOutputStream(file);
278  1 try {
279  257 for (int i = 0; i < 256; i++) {
280  256 out.write(i);
281    }
282  257 for (int i = 0; i < 256; i++) {
283  256 out.write(i);
284    }
285    } finally {
286  1 out.close();
287    }
288  1 final byte [] loaded = IoUtility.loadFileBinary(file);
289  1 assertEquals(512, loaded.length);
290  257 for (int i = 0; i < 256; i++) {
291  256 assertEquals((byte) i, loaded[i]);
292    }
293  257 for (int i = 0; i < 256; i++) {
294  256 assertEquals((byte) i, loaded[i]);
295    }
296  1 assertTrue(file.delete());
297    }
298   
299    /**
300    * Test {@link IoUtility#loadFileBinary(File)}.
301    *
302    * @throws Exception Test failed.
303    */
 
304  1 toggle public void testLoadFileBinary2() throws Exception {
305  1 final File file = new File(getOutdir(), "IoUtilityTestLoadBinary2.bin");
306  1 if (file.exists()) {
307  0 assertTrue(file.delete());
308    }
309  1 final OutputStream out = new FileOutputStream(file);
310  1 try {
311  513 for (int j = 0; j < 512; j++) {
312  131584 for (int i = 0; i < 256; i++) {
313  131072 out.write(i);
314    }
315  131584 for (int i = 0; i < 256; i++) {
316  131072 out.write(255 - i);
317    }
318    }
319    } finally {
320  1 out.close();
321    }
322  1 final byte [] loaded = IoUtility.loadFileBinary(file);
323  1 assertEquals(512 * 2 * 256, loaded.length);
324  513 for (int j = 0; j < 512; j++) {
325  131584 for (int i = 0; i < 256; i++) {
326  131072 assertEquals((byte) i, loaded[i + 512 * j]);
327    }
328  131584 for (int i = 0; i < 256; i++) {
329  131072 assertEquals((byte) (255 - i), loaded[i + 256 + 512 * j]);
330    }
331    }
332  1 assertTrue(file.delete());
333    }
334   
335    /**
336    * Test {@link IoUtility#saveFileBinary(File, byte[])}.
337    *
338    * @throws Exception Test failed.
339    */
 
340  1 toggle public void testSaveFileBinary() throws Exception {
341  1 final File file = new File(getOutdir(), "IoUtilityTestSaveFileBinary.bin");
342  1 if (file.exists()) {
343  0 assertTrue(file.delete());
344    }
345  1 final byte[] data = new byte[512];
346  513 for (int i = 0; i < 512; i++) {
347  512 data[i] = (byte) i;
348    }
349  1 IoUtility.saveFileBinary(file, data);
350  1 final InputStream in = new FileInputStream(file);
351  1 try {
352  257 for (int i = 0; i < 256; i++) {
353  256 assertEquals(i, in.read());
354    }
355  257 for (int i = 0; i < 256; i++) {
356  256 assertEquals(i, in.read());
357    }
358  1 assertEquals(-1, in.read());
359    } finally {
360  1 in.close();
361    }
362  1 assertTrue(file.delete());
363    }
364   
365    /**
366    * Test {@link IoUtility#saveFileBinary(File, byte[])}.
367    *
368    * @throws Exception Test failed.
369    */
 
370  1 toggle public void testLoadAndSaveFileBinary() throws Exception {
371  1 final File file = new File(getOutdir(), "testLoadAndSaveFileBinary.bin");
372  1 if (file.exists()) {
373  0 assertTrue(file.delete());
374    }
375  1 Random random = new Random(1001);
376  1 final byte[] data = new byte[100000];
377  513 for (int i = 0; i < 512; i++) {
378  512 data[i] = (byte) i;
379    }
380  99489 for (int i = 512; i < data.length; i++) {
381  99488 data[i] = (byte) random.nextInt();
382    }
383  1 IoUtility.saveFileBinary(file, data);
384  1 assertTrue(EqualsUtility.equals(data, IoUtility.loadFileBinary(file)));
385  1 assertTrue(file.delete());
386    }
387   
388    /**
389    * Test {@link IoUtility#compareFilesBinary(File, File)}.
390    *
391    * @throws Exception Test failed.
392    */
 
393  1 toggle public void testCompareFileBinary() throws Exception {
394  1 final File file1 = new File(getOutdir(), "IoUtilityTestLoadBinary1.bin");
395  1 if (file1.exists()) {
396  0 assertTrue(file1.delete());
397    }
398  1 final File file2 = new File(getOutdir(), "IoUtilityTestLoadBinary2.bin");
399  1 if (file2.exists()) {
400  0 assertTrue(file2.delete());
401    }
402  1 assertFalse(IoUtility.compareFilesBinary(null, file1));
403  1 assertFalse(IoUtility.compareFilesBinary(file2, null));
404  1 assertTrue(IoUtility.compareFilesBinary(null, null));
405  1 assertTrue(IoUtility.compareFilesBinary(file1, file1));
406  1 try {
407  1 assertTrue(IoUtility.compareFilesBinary(file1, file2));
408  0 fail("FileNotFoundException expected");
409    } catch (FileNotFoundException e) {
410    // expected;
411    }
412  1 Random random = new Random(1001);
413  1 final byte[] data = new byte[1025];
414  1026 for (int i = 0; i < data.length; i++) {
415  1025 data[i] = (byte) random.nextInt();
416    }
417  1 IoUtility.saveFileBinary(file1, data);
418  1 IoUtility.saveFileBinary(file2, data);
419  1 assertTrue(IoUtility.compareFilesBinary(file1, file2));
420  1 data[1000] += 1;
421  1 IoUtility.saveFileBinary(file2, data);
422  1 assertFalse(IoUtility.compareFilesBinary(file1, file2));
423  1 data[999] -= 1;
424  1 IoUtility.saveFileBinary(file2, data);
425  1 assertFalse(IoUtility.compareFilesBinary(file1, file2));
426  1 IoUtility.saveFileBinary(file2, new byte[] {});
427  1 assertEquals(0, file2.length());
428  1 assertFalse(IoUtility.compareFilesBinary(file1, file2));
429  1 assertFalse(IoUtility.compareFilesBinary(file2, file1));
430  1 assertTrue(IoUtility.compareFilesBinary(file1, file1));
431  1 assertTrue(IoUtility.compareFilesBinary(file2, file2));
432  1 IoUtility.saveFileBinary(file2, new byte[] {'A', 'B', 'C'});
433  1 assertFalse(IoUtility.compareFilesBinary(file1, file2));
434  1 assertFalse(IoUtility.compareFilesBinary(file2, file1));
435  1 IoUtility.saveFileBinary(file1, data);
436  1 assertFalse(IoUtility.compareFilesBinary(file1, file2));
437  1 assertTrue(file1.delete());
438  1 assertTrue(file2.delete());
439    }
440   
441    /**
442    * Test {@link IoUtility#loadFile(URL, StringBuffer)}.
443    *
444    * @throws Exception Test failed.
445    */
 
446  1 toggle public void testLoadFileURLStringBuffer() throws Exception {
447  1 final File file = new File(getOutdir(), "testLoadFileURLStringBuffer.txt");
448  1 if (file.exists()) {
449  0 assertTrue(file.delete());
450    }
451  1 final Writer out = new FileWriter(file);
452  1 final String expected = "We all live in a yellow submarine ...";
453  1 out.write(expected);
454  1 out.close();
455  1 final StringBuffer buffer = new StringBuffer();
456  1 IoUtility.loadFile(file.toURL(), buffer);
457  1 assertEquals(expected, buffer.toString());
458  1 assertTrue(file.delete());
459    }
460   
461    /**
462    * Test {@link IoUtility#loadFile(URL, StringBuffer, String)}.
463    *
464    * @throws Exception Test failed.
465    */
 
466  1 toggle public void testLoadFileURLStringBufferString() throws Exception {
467  1 final File file = new File(getOutdir(), "testLoadFileURLStringBufferString.txt");
468  1 if (file.exists()) {
469  0 assertTrue(file.delete());
470    }
471  1 final Writer out = new FileWriter(file);
472  1 final String expected = "We all live in a yellow submarine ...\n";
473  1 final String encoding = "UTF-8";
474  1 out.write(expected);
475  1 out.close();
476  1 final StringBuffer buffer = new StringBuffer();
477  1 IoUtility.loadFile(file.toURL(), buffer, encoding);
478  1 assertEquals(expected, buffer.toString());
479  1 IoUtility.saveFileBinary(file, StringUtility.hex2byte(
480    "FF FE 49 00 20 00 61 00 6D 00 20 00 64 00 72 00"
481    + "65 00 61 00 6D 00 69 00 6E 00 67 00 20 00 6F 00"
482    + "66 00 20 00 61 00 20 00 77 00 68 00 69 00 74 00"
483    + "65 00 20 00 63 00 68 00 72 00 69 00 73 00 74 00"
484    + "6D 00 61 00 73 00 73 00 2E 00 2E 00 2E 00"));
485  1 IoUtility.loadFile(file.toURL(), buffer, "UTF16");
486  1 assertEquals(expected + "I am dreaming of a white christmass...", buffer.toString());
487  1 assertTrue(file.delete());
488    }
489   
490    /**
491    * Test {@link IoUtility#saveFile(URL, File))}.
492    *
493    * @throws Exception Test failed.
494    */
 
495  1 toggle public void testSaveFileURLFile() throws Exception {
496  1 final File file1 = new File(getOutdir(), "testSaveFileURLFile1.txt");
497  1 final File file2 = new File(getOutdir(), "testSaveFileURLFile2.txt");
498  1 if (file1.exists()) {
499  0 assertTrue(file1.delete());
500    }
501  1 if (file2.exists()) {
502  0 assertTrue(file2.delete());
503    }
504  1 IoUtility.saveFileBinary(file1, StringUtility.hex2byte(
505    "FF FE 49 00 20 00 61 00 6D 00 20 00 64 00 72 00"
506    + "65 00 61 00 6D 00 69 00 6E 00 67 00 20 00 6F 00"
507    + "66 00 20 00 61 00 20 00 77 00 68 00 69 00 74 00"
508    + "65 00 20 00 63 00 68 00 72 00 69 00 73 00 74 00"
509    + "6D 00 61 00 73 00 73 00 2E 00 2E 00 2E 00"));
510  1 IoUtility.saveFile(file1.toURL(), file2);
511  1 assertTrue(IoUtility.compareFilesBinary(file1, file2));
512  1 assertTrue(file1.delete());
513  1 assertTrue(file2.delete());
514    }
515   
516    /**
517    * Test {@link IoUtility#saveFile(InputStream, File))}.
518    *
519    * @throws Exception Test failed.
520    */
 
521  1 toggle public void testSaveFileInputStreamFile() throws Exception {
522  1 final File file1 = new File(getOutdir(), "testSaveFileInputStreamFile1.txt");
523  1 final File file2 = new File(getOutdir(), "testSaveFileInputStreamFile2.txt");
524  1 InputStream in = null;
525  1 try {
526  1 if (file1.exists()) {
527  0 assertTrue(file1.delete());
528    }
529  1 if (file2.exists()) {
530  0 assertTrue(file2.delete());
531    }
532  1 IoUtility.saveFileBinary(file1, StringUtility.hex2byte(
533    "FF FE 49 00 20 00 61 00 6D 00 20 00 64 00 72 00"
534    + "65 00 61 00 6D 00 69 00 6E 00 67 00 20 00 6F 00"
535    + "66 00 20 00 61 00 20 00 77 00 68 00 69 00 74 00"
536    + "65 00 20 00 63 00 68 00 72 00 69 00 73 00 74 00"
537    + "6D 00 61 00 73 00 73 00 2E 00 2E 00 2E 00"));
538  1 in = new FileInputStream(file1);
539  1 IoUtility.saveFile(in, file2);
540  1 assertTrue(IoUtility.compareFilesBinary(file1, file2));
541    } finally {
542  1 if (in != null) {
543  1 in.close();
544    }
545    }
546  1 assertTrue(file1.delete());
547  1 assertTrue(file2.delete());
548    }
549   
550    /**
551    * Test {@link IoUtility#stringToReader(String))}.
552    *
553    * @throws Exception Test failed.
554    */
 
555  1 toggle public void testStringToReader() throws Exception {
556  1 final String expected = "We all live in a yellow submarine\n"
557    + "Yellow submarine, yellow submarine\n"
558    + "We all live in a yellow submarine\n"
559    + "Yellow submarine, yellow submarine\n"
560    + "\n"
561    + "As we live a life of ease\n"
562    + "Everyone of us has all we need\n"
563    + "Sky of blue and sea of green\n"
564    + "In our yellow submarine ";
565  1 final Reader reader = IoUtility.stringToReader(expected);
566  1 final StringBuffer buffer = new StringBuffer();
567  1 int c;
568  ? while (0 <= (c = (reader.read()))) {
569  249 buffer.append((char) c);
570    }
571  1 assertEquals(expected, buffer.toString());
572    }
573   
574    /**
575    * Test {@link IoUtility#saveFile(File, String))}.
576    *
577    * @throws Exception Test failed.
578    */
 
579  1 toggle public void testSaveFileFileString() throws Exception {
580  1 final File file1 = new File(getOutdir(), "testSaveFileFileString1.txt");
581  1 final File file2 = new File(getOutdir(), "testSaveFileFileString2.txt");
582  1 InputStream in = null;
583  1 try {
584  1 if (file1.exists()) {
585  0 assertTrue(file1.delete());
586    }
587  1 if (file2.exists()) {
588  0 assertTrue(file2.delete());
589    }
590  1 IoUtility.saveFile(file1, StringUtility.hex2String(
591    "FF FE 49 00 20 00 61 00 6D 00 20 00 64 00 72 00"
592    + "65 00 61 00 6D 00 69 00 6E 00 67 00 20 00 6F 00"
593    + "66 00 20 00 61 00 20 00 77 00 68 00 69 00 74 00"
594    + "65 00 20 00 63 00 68 00 72 00 69 00 73 00 74 00"
595    + "6D 00 61 00 73 00 73 00 2E 00 2E 00 2E 00"));
596  1 in = new FileInputStream(file1);
597  1 IoUtility.saveFile(in, file2);
598  1 assertTrue(IoUtility.compareFilesBinary(file1, file2));
599    } finally {
600  1 if (in != null) {
601  1 in.close();
602    }
603    }
604  1 assertTrue(file1.delete());
605  1 assertTrue(file2.delete());
606    }
607   
608    /**
609    * Test {@link IoUtility#saveFile(File, StringBuffer))}.
610    *
611    * @throws Exception Test failed.
612    */
 
613  1 toggle public void testSaveFileFileStringBuffer() throws Exception {
614  1 final File file1 = new File(getOutdir(), "testSaveFileFileStringBuffer1.txt");
615  1 final File file2 = new File(getOutdir(), "testSaveFileFileStringBuffer2.txt");
616  1 InputStream in = null;
617  1 try {
618  1 if (file1.exists()) {
619  0 assertTrue(file1.delete());
620    }
621  1 if (file2.exists()) {
622  0 assertTrue(file2.delete());
623    }
624  1 final StringBuffer buffer = new StringBuffer(StringUtility.hex2String(
625    "FF FE 49 00 20 00 61 00 6D 00 20 00 64 00 72 00"
626    + "65 00 61 00 6D 00 69 00 6E 00 67 00 20 00 6F 00"
627    + "66 00 20 00 61 00 20 00 77 00 68 00 69 00 74 00"
628    + "65 00 20 00 63 00 68 00 72 00 69 00 73 00 74 00"
629    + "6D 00 61 00 73 00 73 00 2E 00 2E 00 2E 00"));
630  1 IoUtility.saveFile(file1, buffer);
631  1 in = new FileInputStream(file1);
632  1 IoUtility.saveFile(in, file2);
633  1 assertTrue(IoUtility.compareFilesBinary(file1, file2));
634    } finally {
635  1 if (in != null) {
636  1 in.close();
637    }
638    }
639  1 assertTrue(file1.delete());
640  1 assertTrue(file2.delete());
641    }
642   
643    /**
644    * Test {@link IoUtility#saveFile(File, String, String)))}.
645    *
646    * @throws Exception Test failed.
647    */
 
648  1 toggle public void testSaveFileFileStringString() throws Exception {
649  1 final File file1 = new File(getOutdir(), "testSaveFileFileStringString1.txt");
650  1 final File file2 = new File(getOutdir(), "testSaveFileFileStringString2.txt");
651  1 final File file3 = new File(getOutdir(), "testSaveFileFileStringString3.txt");
652  1 InputStream in = null;
653  1 try {
654  1 if (file1.exists()) {
655  0 assertTrue(file1.delete());
656    }
657  1 if (file2.exists()) {
658  0 assertTrue(file2.delete());
659    }
660  1 if (file3.exists()) {
661  0 assertTrue(file3.delete());
662    }
663  1 final String data = StringUtility.hex2String(
664    "FF FE 49 00 20 00 61 00 6D 00 20 00 64 00 72 00"
665    + "65 00 61 00 6D 00 69 00 6E 00 67 00 20 00 6F 00"
666    + "66 00 20 00 61 00 20 00 77 00 68 00 69 00 74 00"
667    + "65 00 20 00 63 00 68 00 72 00 69 00 73 00 74 00"
668    + "6D 00 61 00 73 00 73 00 2E 00 2E 00 2E 00");
669  1 IoUtility.saveFile(file1, data, "ISO-8859-1");
670  1 in = new FileInputStream(file1);
671  1 IoUtility.saveFile(in, file2);
672  1 assertTrue(IoUtility.compareFilesBinary(file1, file2));
673  1 IoUtility.saveFile(file3, "I am dreaming of a white christmass...", "UTF-16");
674  1 assertEquals("I am dreaming of a white christmass...", IoUtility.loadFile(
675    file3.toString(), "UTF-16"));
676    } finally {
677  1 if (in != null) {
678  1 in.close();
679    }
680    }
681  1 assertTrue(file1.delete());
682  1 assertTrue(file2.delete());
683  1 assertTrue(file3.delete());
684    }
685   
686    /**
687    * Test {@link IoUtility#saveFile(String, String)}.
688    *
689    * @throws Exception Test failed.
690    */
 
691  1 toggle public void testSaveFileStringString() throws Exception {
692  1 final File file1 = new File(getOutdir(), "testSaveFileStringString1.txt");
693  1 final File file2 = new File(getOutdir(), "testSaveFileStringString2.txt");
694  1 InputStream in = null;
695  1 try {
696  1 if (file1.exists()) {
697  0 assertTrue(file1.delete());
698    }
699  1 if (file2.exists()) {
700  0 assertTrue(file2.delete());
701    }
702  1 final StringBuffer buffer = new StringBuffer(StringUtility.hex2String(
703    "FF FE 49 00 20 00 61 00 6D 00 20 00 64 00 72 00"
704    + "65 00 61 00 6D 00 69 00 6E 00 67 00 20 00 6F 00"
705    + "66 00 20 00 61 00 20 00 77 00 68 00 69 00 74 00"
706    + "65 00 20 00 63 00 68 00 72 00 69 00 73 00 74 00"
707    + "6D 00 61 00 73 00 73 00 2E 00 2E 00 2E 00"));
708  1 IoUtility.saveFile(file1.toString(), buffer.toString());
709  1 in = new FileInputStream(file1);
710  1 IoUtility.saveFile(in, file2);
711  1 assertTrue(IoUtility.compareFilesBinary(file1, file2));
712    } finally {
713  1 if (in != null) {
714  1 in.close();
715    }
716    }
717  1 assertTrue(file1.delete());
718  1 assertTrue(file2.delete());
719    }
720   
721    /**
722    * Test {@link IoUtility#saveFile(String, StringBuffer)}.
723    *
724    * @throws Exception Test failed.
725    */
 
726  1 toggle public void testSaveFileStringStringBuffer() throws Exception {
727  1 final File file1 = new File(getOutdir(), "testSaveFileStringString1.txt");
728  1 final File file2 = new File(getOutdir(), "testSaveFileStringString2.txt");
729  1 InputStream in = null;
730  1 try {
731  1 if (file1.exists()) {
732  0 assertTrue(file1.delete());
733    }
734  1 if (file2.exists()) {
735  0 assertTrue(file2.delete());
736    }
737  1 final StringBuffer buffer = new StringBuffer(StringUtility.hex2String(
738    "FF FE 49 00 20 00 61 00 6D 00 20 00 64 00 72 00"
739    + "65 00 61 00 6D 00 69 00 6E 00 67 00 20 00 6F 00"
740    + "66 00 20 00 61 00 20 00 77 00 68 00 69 00 74 00"
741    + "65 00 20 00 63 00 68 00 72 00 69 00 73 00 74 00"
742    + "6D 00 61 00 73 00 73 00 2E 00 2E 00 2E 00"));
743  1 IoUtility.saveFile(file1.toString(), buffer);
744  1 in = new FileInputStream(file1);
745  1 IoUtility.saveFile(in, file2);
746  1 assertTrue(IoUtility.compareFilesBinary(file1, file2));
747    } finally {
748  1 if (in != null) {
749  1 in.close();
750    }
751    }
752  1 assertTrue(file1.delete());
753  1 assertTrue(file2.delete());
754    }
755   
756    /**
757    * Test {@link IoUtility#saveFile(File, StringBuffer, String)}.
758    *
759    * @throws Exception Test failed.
760    */
 
761  1 toggle public void testSaveFileFileStringBufferString() throws Exception {
762  1 final File file1 = new File(getOutdir(), "testSaveFileFileStringBufferString1.txt");
763  1 final File file2 = new File(getOutdir(), "testSaveFileFileStringBufferString2.txt");
764  1 final File file3 = new File(getOutdir(), "testSaveFileFileStringBufferString3.txt");
765  1 InputStream in = null;
766  1 try {
767  1 if (file1.exists()) {
768  0 assertTrue(file1.delete());
769    }
770  1 if (file2.exists()) {
771  0 assertTrue(file2.delete());
772    }
773  1 if (file3.exists()) {
774  0 assertTrue(file3.delete());
775    }
776  1 final String data = StringUtility.hex2String(
777    "FF FE 49 00 20 00 61 00 6D 00 20 00 64 00 72 00"
778    + "65 00 61 00 6D 00 69 00 6E 00 67 00 20 00 6F 00"
779    + "66 00 20 00 61 00 20 00 77 00 68 00 69 00 74 00"
780    + "65 00 20 00 63 00 68 00 72 00 69 00 73 00 74 00"
781    + "6D 00 61 00 73 00 73 00 2E 00 2E 00 2E 00");
782  1 final StringBuffer buffer = new StringBuffer(data);
783  1 IoUtility.saveFile(file1, buffer, "ISO-8859-1");
784  1 in = new FileInputStream(file1);
785  1 IoUtility.saveFile(in, file2);
786  1 assertTrue(IoUtility.compareFilesBinary(file1, file2));
787  1 buffer.setLength(0);
788  1 buffer.append("I am dreaming of a white christmass...");
789  1 IoUtility.saveFile(file3, buffer, "UTF-16");
790  1 assertEquals("I am dreaming of a white christmass...", IoUtility.loadFile(
791    file3.toString(), "UTF-16"));
792    } finally {
793  1 if (in != null) {
794  1 in.close();
795    }
796    }
797  1 assertTrue(file1.delete());
798  1 assertTrue(file2.delete());
799  1 assertTrue(file3.delete());
800    }
801   
802    /**
803    * Test {@link IoUtility#copyFile(File, File)}.
804    *
805    * @throws Exception Test failed.
806    */
 
807  1 toggle public void testCopyFile() throws Exception {
808  1 final File file1 = new File(getOutdir(), "IoUtilityTestCopyFile1.bin");
809  1 if (file1.exists()) {
810  0 assertTrue(file1.delete());
811    }
812  1 final File file2 = new File(getOutdir(), "IoUtilityTestCopyFile2.bin");
813  1 if (file2.exists()) {
814  0 assertTrue(file2.delete());
815    }
816  1 Random random = new Random(1007);
817  1 final byte[] data = new byte[1025];
818  1026 for (int i = 0; i < data.length; i++) {
819  1025 data[i] = (byte) random.nextInt();
820    }
821  1 IoUtility.saveFileBinary(file1, data);
822  1 IoUtility.copyFile(file1, file1);
823  1 IoUtility.copyFile(file1, file2);
824  1 assertTrue(IoUtility.compareFilesBinary(file1, file2));
825  1 file1.delete();
826  1 file2.delete();
827    }
828   
829    /**
830    * Test {@link IoUtility#copy(File, File)}.
831    *
832    * @throws Exception Test failed.
833    */
 
834  1 toggle public void testCopyDirectory() throws Exception {
835  1 final File dir1 = new File(getOutdir(), "testCopyDirectory_directory1");
836  1 if (dir1.exists()) {
837  0 assertTrue(IoUtility.deleteDir(dir1, true));
838    }
839  1 final File dir2 = new File(dir1, "dir2");
840  1 final File file1 = new File(dir1, "test1.txt");
841  1 final File file2 = new File(dir1, "test2.txt");
842  1 final File file3 = new File(dir2, "test3.txt");
843  1 assertTrue(dir2.mkdirs());
844  1 IoUtility.saveFile(file1, "File 1", "ISO-8859-1");
845  1 IoUtility.saveFile(file2, "File 2", "ISO-8859-1");
846  1 IoUtility.saveFile(file3, "File 3", "ISO-8859-1");
847  1 final File dir3 = new File(dir1, "dir3");
848  1 assertTrue(dir3.mkdirs());
849  1 final File file5 = new File(dir3, "test5.bin");
850  1 Random random = new Random(1007);
851  1 final byte[] data = new byte[1025];
852  1026 for (int i = 0; i < data.length; i++) {
853  1025 data[i] = (byte) random.nextInt();
854    }
855  1 IoUtility.saveFileBinary(file5, data);
856   
857  1 final File copy = new File(getOutdir(), "testCopyDirectory_directory2");
858  1 IoUtility.copy(dir1, copy);
859  1 final File copyDir2 = new File(copy, "dir2");
860  1 final File copyFile1 = new File(copy, "test1.txt");
861  1 final File copyFile2 = new File(copy, "test2.txt");
862  1 final File copyFile3 = new File(copyDir2, "test3.txt");
863  1 final File copyDir3 = new File(copy, "dir3");
864  1 final File copyFile5 = new File(copyDir3, "test5.bin");
865  1 assertTrue(IoUtility.compareFilesBinary(file1, copyFile1));
866  1 assertTrue(IoUtility.compareFilesBinary(file2, copyFile2));
867  1 assertTrue(IoUtility.compareFilesBinary(file3, copyFile3));
868  1 assertTrue(IoUtility.compareFilesBinary(file5, copyFile5));
869  1 assertTrue(IoUtility.deleteDir(dir1, true));
870  1 assertTrue(IoUtility.deleteDir(copy, true));
871    }
872   
873    /**
874    * Test {@link IoUtility#copy(File, File)}.
875    *
876    * @throws Exception Test failed.
877    */
 
878  1 toggle public void testCopyDirectory2() throws Exception {
879  1 final File file1 = new File(getOutdir(), "testCopyDirectory2_1.bin");
880  1 if (file1.exists()) {
881  0 assertTrue(file1.delete());
882    }
883  1 final File file2 = new File(getOutdir(), "testCopyDirectory2_2.bin");
884  1 if (file2.exists()) {
885  0 assertTrue(file2.delete());
886    }
887  1 Random random = new Random(1007);
888  1 final byte[] data = new byte[1025];
889  1026 for (int i = 0; i < data.length; i++) {
890  1025 data[i] = (byte) random.nextInt();
891    }
892  1 IoUtility.saveFileBinary(file1, data);
893  1 IoUtility.copy(file1, file2);
894  1 assertTrue(IoUtility.compareFilesBinary(file1, file2));
895  1 file1.delete();
896  1 file2.delete();
897    }
898   
899    /**
900    * Test {@link IoUtility#copy(String, String)}.
901    *
902    * @throws Exception Test failed.
903    */
 
904  1 toggle public void testCopyDirectory3() throws Exception {
905  1 final File dir1 = new File(getOutdir(), "testCopyDirectory3_directory1");
906  1 if (dir1.exists()) {
907  0 assertTrue(IoUtility.deleteDir(dir1, true));
908    }
909  1 final File dir2 = new File(dir1, "dir2");
910  1 final File file1 = new File(dir1, "test1.txt");
911  1 final File file2 = new File(dir1, "test2.txt");
912  1 final File file3 = new File(dir2, "test3.txt");
913  1 assertTrue(dir2.mkdirs());
914  1 IoUtility.saveFile(file1, "File 1", "ISO-8859-1");
915  1 IoUtility.saveFile(file2, "File 2", "ISO-8859-1");
916  1 IoUtility.saveFile(file3, "File 3", "ISO-8859-1");
917  1 final File dir3 = new File(dir1, "dir3");
918  1 assertTrue(dir3.mkdirs());
919  1 final File file5 = new File(dir3, "test5.bin");
920  1 Random random = new Random(1007);
921  1 final byte[] data = new byte[1025];
922  1026 for (int i = 0; i < data.length; i++) {
923  1025 data[i] = (byte) random.nextInt();
924    }
925  1 IoUtility.saveFileBinary(file5, data);
926   
927  1 final File copy = new File(getOutdir(), "testCopyDirectory3_directory2");
928  1 IoUtility.copy(dir1.getPath(), copy.getPath());
929   
930  1 final File copyDir2 = new File(copy, "dir2");
931  1 final File copyFile1 = new File(copy, "test1.txt");
932  1 final File copyFile2 = new File(copy, "test2.txt");
933  1 final File copyFile3 = new File(copyDir2, "test3.txt");
934  1 final File copyDir3 = new File(copy, "dir3");
935  1 final File copyFile5 = new File(copyDir3, "test5.bin");
936  1 assertTrue(IoUtility.compareFilesBinary(file1, copyFile1));
937  1 assertTrue(IoUtility.compareFilesBinary(file2, copyFile2));
938  1 assertTrue(IoUtility.compareFilesBinary(file3, copyFile3));
939  1 assertTrue(IoUtility.compareFilesBinary(file5, copyFile5));
940  1 assertTrue(IoUtility.deleteDir(dir1, true));
941  1 assertTrue(IoUtility.deleteDir(copy, true));
942    }
943   
944    /**
945    * Test {@link IoUtility#compareTextFiles(File, File, String)}.
946    *
947    * @throws Exception Test failed.
948    */
 
949  1 toggle public void testCompareTextFiles1() throws Exception {
950  1 final File file1 = new File(getOutdir(), "testCompareTextFiles1.txt");
951  1 if (file1.exists()) {
952  0 assertTrue(file1.delete());
953    }
954  1 final File file2 = new File(getOutdir(), "testCompareTextFiles2.txt");
955  1 if (file2.exists()) {
956  0 assertTrue(file2.delete());
957    }
958  1 assertFalse(IoUtility.compareTextFiles(null, file1, "ISO-8859-1"));
959  1 assertFalse(IoUtility.compareTextFiles(file2, null, "ISO-8859-1"));
960  1 assertTrue(IoUtility.compareTextFiles(file1, file1, "ISO-8859-1"));
961  1 try {
962  1 assertTrue(IoUtility.compareTextFiles(file1, file2, "ISO-8859-1"));
963  0 fail("FileNotFoundException expected");
964    } catch (FileNotFoundException e) {
965    // expected;
966    }
967  1 Random random = new Random(1001);
968  1 final byte[] data = new byte[1025];
969  1026 for (int i = 0; i < data.length; i++) {
970  1025 data[i] = (byte) random.nextInt();
971    }
972  1 IoUtility.saveFileBinary(file1, data);
973  1 IoUtility.saveFileBinary(file2, data);
974  1 try {
975  1 IoUtility.compareTextFiles(file1, file2, "iso");
976  0 fail("UnsupportedEncodingException expected");
977    } catch (UnsupportedEncodingException e) {
978    // expected;
979    }
980  1 try {
981  1 IoUtility.compareTextFiles(file1, file2, null);
982  0 fail("NullPointerException expected");
983    } catch (NullPointerException e) {
984    // expected;
985    }
986  1 assertTrue(IoUtility.compareTextFiles(file1, file2, "ISO-8859-1"));
987  1 data[1000] += 1;
988  1 IoUtility.saveFileBinary(file2, data);
989  1 assertFalse(IoUtility.compareTextFiles(file1, file2, "ISO-8859-1"));
990  1 data[999] -= 1;
991  1 IoUtility.saveFileBinary(file2, data);
992  1 assertFalse(IoUtility.compareTextFiles(file1, file2, "ISO-8859-1"));
993  1 IoUtility.saveFileBinary(file2, new byte[] {});
994  1 assertEquals(0, file2.length());
995  1 assertFalse(IoUtility.compareTextFiles(file1, file2, "ISO-8859-1"));
996  1 assertTrue(IoUtility.compareTextFiles(file1, file1, "ISO-8859-1"));
997  1 assertTrue(IoUtility.compareTextFiles(file2, file2, "ISO-8859-1"));
998  1 assertTrue(file1.delete());
999  1 assertTrue(file2.delete());
1000    }
1001   
 
1002  1 toggle public void testListFilesRecursively() throws Exception {
1003  1 final File dir1 = new File(getOutdir(), "testListFilesRecursively");
1004  1 if (dir1.exists()) {
1005  0 assertTrue(IoUtility.deleteDir(dir1, true));
1006    }
1007  1 final File dir2 = new File(dir1, "dir2");
1008  1 final File file1 = new File(dir1, "dingo.txt");
1009  1 final File file2 = new File(dir1, "dongo.txt");
1010  1 final File file3 = new File(dir2, "bingo.txt");
1011  1 assertTrue(dir2.mkdirs());
1012  1 IoUtility.saveFile(file1, "File 1", "ISO-8859-1");
1013  1 IoUtility.saveFile(file2, "File 2", "ISO-8859-1");
1014  1 IoUtility.saveFile(file3, "File 3", "ISO-8859-1");
1015  1 final File dir3 = new File(dir1, "dir3");
1016  1 assertTrue(dir3.mkdirs());
1017  1 final File file5 = new File(dir3, "bongo.txt");
1018  1 IoUtility.saveFile(file5, "File 5", "ISO-8859-1");
1019  1 final List list = IoUtility.listFilesRecursively(dir1, new FileFilter() {
 
1020  4 toggle public boolean accept(File pathname) {
1021  4 return pathname.getName().equals("bongo.txt");
1022    }
1023    });
1024  1 assertEquals(0, list.size());
1025  1 final List list1 = IoUtility.listFilesRecursively(dir1, new FileFilter() {
 
1026  6 toggle public boolean accept(File pathname) {
1027  6 return pathname.isDirectory() || pathname.getName().equals("bongo.txt");
1028    }
1029    });
1030  1 assertEquals(1, list1.size());
1031  1 final List list2 = IoUtility.listFilesRecursively(dir1, new FileFilter() {
 
1032  4 toggle public boolean accept(File pathname) {
1033  4 return pathname.getName().endsWith(".txt");
1034    }
1035    });
1036  1 assertEquals(2, list2.size());
1037  1 final List list3 = IoUtility.listFilesRecursively(dir1, new FileFilter() {
 
1038  6 toggle public boolean accept(File pathname) {
1039  6 return pathname.isDirectory() || pathname.getName().endsWith(".txt");
1040    }
1041    });
1042  1 assertEquals(4, list3.size());
1043  1 final List list4 = IoUtility.listFilesRecursively(file5, new FileFilter() {
 
1044  0 toggle public boolean accept(File pathname) {
1045  0 return false;
1046    }
1047    });
1048  1 assertEquals(1, list4.size());
1049  1 assertEquals(file5, list4.get(0));
1050  1 assertEquals(4, list3.size());
1051  1 final List list5 = IoUtility.listFilesRecursively(dir1, new FileFilter() {
 
1052  4 toggle public boolean accept(File pathname) {
1053  4 return false;
1054    }
1055    });
1056  1 assertEquals(0, list5.size());
1057    }
1058   
1059    /**
1060    * Test {@link IoUtility#compareTextFiles(File, File, String)}.
1061    *
1062    * @throws Exception Test failed.
1063    */
 
1064  1 toggle public void testCompareTextFiles2() throws Exception {
1065  1 final String text = "When she goes, shes gone.@" + "If she stays, she stays here.@"
1066    + "The girl does what she wants to do.@" + "She knows what she wants to do.@"
1067    + "And I know Im fakin it,@" + "Im not really makin it.@" + "@"
1068    + "Im such a dubious soul,@" + "And a walk in the garden@" + "Wears me down.@"
1069    + "Tangled in the fallen vines,@" + "Pickin up the punch lines,@"
1070    + "Ive just been fakin it,@" + "Not really makin it.@" + "@" + "Is there any danger?@"
1071    + "No, no, not really.@" + "Just lean on me.@" + "Takin time to treat@"
1072    + "Your friendly neighbors honestly.@" + "Ive just been fakin it,@"
1073    + "Im not really makin it.@" + "This feeling of fakin it--@"
1074    + "I still havent shaken it.@" + "@" + "Prior to this lifetime@"
1075    + "I surely was a tailor.@" + "(good morning, mr. leitch.@"
1076    + "Have you had a busy day? )@" + "I own the tailors face and hands.@"
1077    + "I am the tailors face and hands and@" + "I know Im fakin it,@"
1078    + "Im not really makin it.@" + "This feeling of fakin it--@"
1079    + "I still havent shaken it.";
1080  1 final File file1 = new File(getOutdir(), "testCompareTextFiles1.txt");
1081  1 if (file1.exists()) {
1082  0 assertTrue(file1.delete());
1083    }
1084  1 final File file2 = new File(getOutdir(), "testCompareTextFiles2.txt");
1085  1 if (file2.exists()) {
1086  0 assertTrue(file2.delete());
1087    }
1088  1 IoUtility.saveFile(file1, "Line1", "ISO-8859-1");
1089  1 IoUtility.saveFile(file2, "Line1", "ISO-8859-1");
1090   
1091  1 assertFalse(IoUtility.compareTextFiles(null, file1, "ISO-8859-1"));
1092  1 assertFalse(IoUtility.compareTextFiles(file2, null, "ISO-8859-1"));
1093  1 assertTrue(IoUtility.compareTextFiles(null, null, "ISO-8859-1"));
1094  1 try {
1095  1 IoUtility.compareTextFiles(file1, file2, "iso");
1096  0 fail("UnsupportedEncodingException expected");
1097    } catch (UnsupportedEncodingException e) {
1098    // expected;
1099    }
1100  1 IoUtility.saveFile(file2, "line1", "ISO-8859-1");
1101  1 assertFalse(IoUtility.compareTextFiles(file1, file2, "ISO-8859-1"));
1102  1 assertFalse(IoUtility.compareTextFiles(file2, file1, "ISO-8859-1"));
1103   
1104  1 IoUtility.saveFile(file1, StringUtility.replace(text, "@", "\n"), "ISO-8859-1");
1105  1 IoUtility.saveFile(file2, StringUtility.replace(text, "@", "\n"), "ISO-8859-1");
1106  1 assertTrue(IoUtility.compareTextFiles(file1, file2, "ISO-8859-1"));
1107  1 assertTrue(IoUtility.compareTextFiles(file2, file1, "ISO-8859-1"));
1108   
1109  1 IoUtility.saveFile(file1, StringUtility.replace(text, "@", "\015\012"), "ISO-8859-1");
1110  1 IoUtility.saveFile(file2, StringUtility.replace(text, "@", "\015\012"), "ISO-8859-1");
1111  1 assertTrue(IoUtility.compareTextFiles(file1, file2, "ISO-8859-1"));
1112  1 assertTrue(IoUtility.compareTextFiles(file2, file1, "ISO-8859-1"));
1113   
1114  1 IoUtility.saveFile(file1, StringUtility.replace(text, "@", "\012"), "ISO-8859-1");
1115  1 IoUtility.saveFile(file2, StringUtility.replace(text, "@", "\015\012"), "ISO-8859-1");
1116  1 assertTrue(IoUtility.compareTextFiles(file1, file2, "ISO-8859-1"));
1117  1 assertTrue(IoUtility.compareTextFiles(file2, file1, "ISO-8859-1"));
1118   
1119  1 IoUtility.saveFile(file1, StringUtility.replace(text + "@", "@", "\012"), "ISO-8859-1");
1120  1 IoUtility.saveFile(file2, StringUtility.replace(text, "@", "\015\012"), "ISO-8859-1");
1121  1 assertTrue(IoUtility.compareTextFiles(file1, file2, "ISO-8859-1"));
1122  1 assertTrue(IoUtility.compareTextFiles(file2, file1, "ISO-8859-1"));
1123   
1124  1 IoUtility.saveFile(file1, StringUtility.replace(text, "@", "\012"), "ISO-8859-1");
1125  1 IoUtility.saveFile(file2, StringUtility.replace(text + "@", "@", "\015\012"), "ISO-8859-1");
1126  1 assertTrue(IoUtility.compareTextFiles(file1, file2, "ISO-8859-1"));
1127  1 assertTrue(IoUtility.compareTextFiles(file2, file1, "ISO-8859-1"));
1128   
1129  1 IoUtility.saveFile(file1, StringUtility.replace(text + "@", "@", "\012"), "ISO-8859-1");
1130  1 IoUtility.saveFile(file2, StringUtility.replace(text + "@", "@", "\015\012"), "ISO-8859-1");
1131  1 assertTrue(IoUtility.compareTextFiles(file1, file2, "ISO-8859-1"));
1132  1 assertTrue(IoUtility.compareTextFiles(file2, file1, "ISO-8859-1"));
1133  1 assertTrue(IoUtility.compareTextFiles(file1, file2, "UTF8"));
1134  1 assertTrue(IoUtility.compareTextFiles(file2, file1, "UTF8"));
1135  1 assertFalse(IoUtility.compareTextFiles(file1, file2, "UTF-16"));
1136  1 assertFalse(IoUtility.compareTextFiles(file2, file1, "UTF-16"));
1137   
1138  1 IoUtility.saveFile(file1, StringUtility.replace(text, "@", "\012"), "ISO-8859-1");
1139  1 IoUtility.saveFile(file2, StringUtility.replace(text, "@", "\012"), "UTF8");
1140  1 assertTrue(IoUtility.compareTextFiles(file1, file2, "ISO-8859-1"));
1141  1 assertTrue(IoUtility.compareTextFiles(file2, file1, "ISO-8859-1"));
1142  1 assertTrue(IoUtility.compareTextFiles(file1, file2, "UTF8"));
1143  1 assertTrue(IoUtility.compareTextFiles(file2, file1, "UTF8"));
1144  1 assertTrue(IoUtility.compareTextFiles(file1, file2, "UTF-16"));
1145  1 assertTrue(IoUtility.compareTextFiles(file2, file1, "UTF-16"));
1146   
1147  1 IoUtility.saveFile(file1, StringUtility.replace(text, "@", "\012"), "UTF16");
1148  1 IoUtility.saveFile(file2, StringUtility.replace(text, "@", "\012"), "UTF8");
1149  1 assertFalse(IoUtility.compareTextFiles(file1, file2, "UTF-16"));
1150  1 assertFalse(IoUtility.compareTextFiles(file2, file1, "UTF8"));
1151   
1152  1 IoUtility.saveFile(file1, StringUtility.replace(text, "@", "\012"), "UTF-16");
1153  1 IoUtility.saveFile(file2, StringUtility.replace(text, "@", "\012"), "UTF-16");
1154  1 assertTrue(IoUtility.compareTextFiles(file1, file2, "ISO-8859-1"));
1155  1 assertTrue(IoUtility.compareTextFiles(file2, file1, "ISO-8859-1"));
1156  1 assertTrue(IoUtility.compareTextFiles(file1, file2, "UTF8"));
1157  1 assertTrue(IoUtility.compareTextFiles(file2, file1, "UTF8"));
1158  1 assertTrue(IoUtility.compareTextFiles(file1, file2, "UTF-16"));
1159  1 assertTrue(IoUtility.compareTextFiles(file2, file1, "UTF-16"));
1160   
1161  1 IoUtility.saveFile(file1, StringUtility.replace(text, "@", "\012"), "UTF-16");
1162  1 IoUtility.saveFile(file2, StringUtility.replace(text, "@", "\012\015"), "UTF-16");
1163  1 assertFalse(IoUtility.compareTextFiles(file1, file2, "ISO-8859-1"));
1164  1 assertFalse(IoUtility.compareTextFiles(file2, file1, "ISO-8859-1"));
1165  1 assertFalse(IoUtility.compareTextFiles(file1, file2, "UTF8"));
1166  1 assertFalse(IoUtility.compareTextFiles(file2, file1, "UTF8"));
1167  1 assertTrue(IoUtility.compareTextFiles(file1, file2, "UTF16"));
1168  1 assertTrue(IoUtility.compareTextFiles(file2, file1, "UTF16"));
1169   
1170  1 IoUtility.saveFile(file1, StringUtility.replace(text, "@", "\012"), "UTF-16");
1171  1 IoUtility.saveFile(file2, StringUtility.replace(text, "@", "\015\012"), "UTF-16");
1172  1 assertFalse(IoUtility.compareTextFiles(file1, file2, "ISO-8859-1"));
1173  1 assertFalse(IoUtility.compareTextFiles(file2, file1, "ISO-8859-1"));
1174  1 assertFalse(IoUtility.compareTextFiles(file1, file2, "UTF8"));
1175  1 assertFalse(IoUtility.compareTextFiles(file2, file1, "UTF8"));
1176  1 assertTrue(IoUtility.compareTextFiles(file1, file2, "UTF-16"));
1177  1 assertTrue(IoUtility.compareTextFiles(file2, file1, "UTF-16"));
1178   
1179  1 IoUtility.saveFile(file1, StringUtility.replace(text, "@", "\012"), "ISO-8859-1");
1180  1 IoUtility.saveFile(file2, StringUtility.replace(text, "@", "\015"), "ISO-8859-1");
1181  1 assertTrue(IoUtility.compareTextFiles(file1, file2, "ISO-8859-1"));
1182  1 assertTrue(IoUtility.compareTextFiles(file2, file1, "ISO-8859-1"));
1183  1 assertTrue(IoUtility.compareTextFiles(file1, file2, "UTF8"));
1184  1 assertTrue(IoUtility.compareTextFiles(file2, file1, "UTF8"));
1185  1 assertFalse(IoUtility.compareTextFiles(file1, file2, "UTF-16"));
1186  1 assertFalse(IoUtility.compareTextFiles(file2, file1, "UTF-16"));
1187   
1188  1 IoUtility.saveFile(file1, StringUtility.replace(text, "@", "\015\012"), "ISO-8859-1");
1189  1 IoUtility.saveFile(file2, StringUtility.replace(text, "@", "" + (char) 0x2029),
1190    "ISO-8859-1");
1191  1 assertTrue(IoUtility.compareTextFiles(file1, file2, "ISO-8859-1"));
1192  1 assertTrue(IoUtility.compareTextFiles(file2, file1, "ISO-8859-1"));
1193  1 assertTrue(IoUtility.compareTextFiles(file1, file2, "UTF8"));
1194  1 assertTrue(IoUtility.compareTextFiles(file2, file1, "UTF8"));
1195  1 assertFalse(IoUtility.compareTextFiles(file1, file2, "UTF-16"));
1196  1 assertFalse(IoUtility.compareTextFiles(file2, file1, "UTF-16"));
1197   
1198  1 assertTrue(file1.delete());
1199  1 assertTrue(file2.delete());
1200    }
1201   
1202    /**
1203    * Test {@link IoUtility#compareTextFiles(File, File, int, String)}.
1204    *
1205    * @throws Exception Test failed.
1206    */
 
1207  1 toggle public void testCompareTextFiles3() throws Exception {
1208  1 final String text = "When she goes, shes gone.@" + "If she stays, she stays here.@"
1209    + "The girl does what she wants to do.@" + "She knows what she wants to do.@"
1210    + "And I know Im fakin it,@" + "Im not really makin it.@" + "@"
1211    + "Im such a dubious soul,@" + "And a walk in the garden@" + "Wears me down.@"
1212    + "Tangled in the fallen vines,@" + "Pickin up the punch lines,@"
1213    + "Ive just been fakin it,@" + "Not really makin it.@" + "@" + "Is there any danger?@"
1214    + "No, no, not really.@" + "Just lean on me.@" + "Takin time to treat@"
1215    + "Your friendly neighbors honestly.@" + "Ive just been fakin it,@"
1216    + "Im not really makin it.@" + "This feeling of fakin it--@"
1217    + "I still havent shaken it.@" + "@" + "Prior to this lifetime@"
1218    + "I surely was a tailor.@" + "(good morning, mr. leitch.@"
1219    + "Have you had a busy day? )@" + "I own the tailors face and hands.@"
1220    + "I am the tailors face and hands and@" + "I know Im fakin it,@"
1221    + "Im not really makin it.@" + "This feeling of fakin it--@"
1222    + "I still havent shaken it.";
1223  1 compareFilesAt(text, -1);
1224  1 compareFilesAt(text, 0);
1225  1 compareFilesAt(text, 20);
1226    // compareFilesAt(text, 200);
1227    }
1228   
 
1229  3 toggle private void compareFilesAt(final String text, int s) throws IOException {
1230  3 final File file1 = new File(getOutdir(), "testCompareTextFiles1.txt");
1231  3 if (file1.exists()) {
1232  0 assertTrue(file1.delete());
1233    }
1234  3 final File file2 = new File(getOutdir(), "testCompareTextFiles2.txt");
1235  3 if (file2.exists()) {
1236  0 assertTrue(file2.delete());
1237    }
1238  3 IoUtility.saveFile(file1, "Line1", "ISO-8859-1");
1239  3 IoUtility.saveFile(file2, "Line1", "ISO-8859-1");
1240   
1241  3 assertTrue(IoUtility.compareTextFiles(file1, file1, s, "ISO-8859-1"));
1242  3 assertFalse(IoUtility.compareTextFiles(null, file1, s, "ISO-8859-1"));
1243  3 assertFalse(IoUtility.compareTextFiles(file2, null, s, "ISO-8859-1"));
1244  3 assertTrue(IoUtility.compareTextFiles(null, null, s, "ISO-8859-1"));
1245  3 try {
1246  3 IoUtility.compareTextFiles(file1, file2, s, "iso");
1247  1 if (s >= 0) {
1248  0 fail("UnsupportedEncodingException expected");
1249    }
1250    } catch (UnsupportedEncodingException e) {
1251    // expected;
1252    }
1253  3 IoUtility.saveFile(file2, "line1", "ISO-8859-1");
1254  3 if (s == 0) {
1255  1 assertFalse(IoUtility.compareTextFiles(file1, file2, s, "ISO-8859-1"));
1256  1 assertFalse(IoUtility.compareTextFiles(file2, file1, s, "ISO-8859-1"));
1257    } else {
1258  2 assertTrue(IoUtility.compareTextFiles(file1, file2, s, "ISO-8859-1"));
1259  2 assertTrue(IoUtility.compareTextFiles(file2, file1, s, "ISO-8859-1"));
1260    }
1261   
1262  3 IoUtility.saveFile(file1, StringUtility.replace(text, "@", "\n"), "ISO-8859-1");
1263  3 IoUtility.saveFile(file2, StringUtility.replace(text, "@", "\n"), "ISO-8859-1");
1264  3 assertTrue(IoUtility.compareTextFiles(file1, file2, s, "ISO-8859-1"));
1265  3 assertTrue(IoUtility.compareTextFiles(file2, file1, s, "ISO-8859-1"));
1266   
1267  3 IoUtility.saveFile(file1, StringUtility.replace(text, "@", "\015\012"), "ISO-8859-1");
1268  3 IoUtility.saveFile(file2, StringUtility.replace(text, "@", "\015\012"), "ISO-8859-1");
1269  3 assertTrue(IoUtility.compareTextFiles(file1, file2, s, "ISO-8859-1"));
1270  3 assertTrue(IoUtility.compareTextFiles(file2, file1, s, "ISO-8859-1"));
1271   
1272  3 IoUtility.saveFile(file1, StringUtility.replace(text, "@", "\012"), "ISO-8859-1");
1273  3 IoUtility.saveFile(file2, StringUtility.replace(text, "@", "\015\012"), "ISO-8859-1");
1274  3 assertTrue(IoUtility.compareTextFiles(file1, file2, s, "ISO-8859-1"));
1275  3 assertTrue(IoUtility.compareTextFiles(file2, file1, s, "ISO-8859-1"));
1276   
1277  3 IoUtility.saveFile(file1, StringUtility.replace(text + "@", "@", "\012"), "ISO-8859-1");
1278  3 IoUtility.saveFile(file2, StringUtility.replace(text, "@", "\015\012"), "ISO-8859-1");
1279  3 assertTrue(IoUtility.compareTextFiles(file1, file2, s, "ISO-8859-1"));
1280  3 assertTrue(IoUtility.compareTextFiles(file2, file1, s, "ISO-8859-1"));
1281   
1282  3 IoUtility.saveFile(file1, StringUtility.replace(text, "@", "\012"), "ISO-8859-1");
1283  3 IoUtility.saveFile(file2, StringUtility.replace(text + "@", "@", "\015\012"), "ISO-8859-1");
1284  3 assertTrue(IoUtility.compareTextFiles(file1, file2, s, "ISO-8859-1"));
1285  3 assertTrue(IoUtility.compareTextFiles(file2, file1, s, "ISO-8859-1"));
1286   
1287  3 IoUtility.saveFile(file1, StringUtility.replace(text + "@", "@", "\012"), "ISO-8859-1");
1288  3 IoUtility.saveFile(file2, StringUtility.replace(text + "@", "@", "\015\012"), "ISO-8859-1");
1289  3 assertTrue(IoUtility.compareTextFiles(file1, file2, s, "ISO-8859-1"));
1290  3 assertTrue(IoUtility.compareTextFiles(file2, file1, s, "ISO-8859-1"));
1291  3 assertTrue(IoUtility.compareTextFiles(file1, file2, s, "UTF8"));
1292  3 assertTrue(IoUtility.compareTextFiles(file2, file1, s, "UTF8"));
1293  3 if (s == 0) {
1294  1 assertFalse(IoUtility.compareTextFiles(file1, file2, s, "UTF-16"));
1295  1 assertFalse(IoUtility.compareTextFiles(file2, file1, s, "UTF-16"));
1296    } else {
1297  2 assertTrue(IoUtility.compareTextFiles(file1, file2, s, "UTF-16"));
1298  2 assertTrue(IoUtility.compareTextFiles(file2, file1, s, "UTF-16"));
1299    }
1300  3 IoUtility.saveFile(file1, StringUtility.replace(text, "@", "\012"), "ISO-8859-1");
1301  3 IoUtility.saveFile(file2, StringUtility.replace(text, "@", "\012"), "UTF8");
1302  3 assertTrue(IoUtility.compareTextFiles(file1, file2, s, "ISO-8859-1"));
1303  3 assertTrue(IoUtility.compareTextFiles(file2, file1, s, "ISO-8859-1"));
1304  3 assertTrue(IoUtility.compareTextFiles(file1, file2, s, "UTF8"));
1305  3 assertTrue(IoUtility.compareTextFiles(file2, file1, s, "UTF8"));
1306  3 assertTrue(IoUtility.compareTextFiles(file1, file2, s, "UTF-16"));
1307  3 assertTrue(IoUtility.compareTextFiles(file2, file1, s, "UTF-16"));
1308   
1309  3 IoUtility.saveFile(file1, StringUtility.replace(text, "@", "\012"), "UTF16");
1310  3 IoUtility.saveFile(file2, StringUtility.replace(text, "@", "\012"), "UTF8");
1311  3 if (s == 0) {
1312  1 assertFalse(IoUtility.compareTextFiles(file1, file2, s, "UTF-16"));
1313  1 assertFalse(IoUtility.compareTextFiles(file2, file1, s, "UTF8"));
1314  2 } else if (s < 0){
1315  1 assertTrue(IoUtility.compareTextFiles(file1, file2, s, "UTF-16"));
1316  1 assertTrue(IoUtility.compareTextFiles(file2, file1, s, "UTF8"));
1317    }
1318   
1319  3 IoUtility.saveFile(file1, StringUtility.replace(text, "@", "\012"), "UTF-16");
1320  3 IoUtility.saveFile(file2, StringUtility.replace(text, "@", "\012"), "UTF-16");
1321  3 assertTrue(IoUtility.compareTextFiles(file1, file2, s, "ISO-8859-1"));
1322  3 assertTrue(IoUtility.compareTextFiles(file2, file1, s, "ISO-8859-1"));
1323  3 assertTrue(IoUtility.compareTextFiles(file1, file2, s, "UTF8"));
1324  3 assertTrue(IoUtility.compareTextFiles(file2, file1, s, "UTF8"));
1325  3 assertTrue(IoUtility.compareTextFiles(file1, file2, s, "UTF-16"));
1326  3 assertTrue(IoUtility.compareTextFiles(file2, file1, "UTF-16"));
1327   
1328  3 IoUtility.saveFile(file1, StringUtility.replace(text, "@", "\012"), "UTF-16");
1329  3 IoUtility.saveFile(file2, StringUtility.replace(text, "@", "\012\015"), "UTF-16");
1330  3 if (s >= 0) {
1331  2 assertFalse(IoUtility.compareTextFiles(file1, file2, s, "ISO-8859-1"));
1332  2 assertFalse(IoUtility.compareTextFiles(file2, file1, s, "ISO-8859-1"));
1333  2 assertFalse(IoUtility.compareTextFiles(file1, file2, s, "UTF8"));
1334  2 assertFalse(IoUtility.compareTextFiles(file2, file1, s, "UTF8"));
1335  2 assertFalse(IoUtility.compareTextFiles(file1, file2, s, "UTF16"));
1336  2 assertFalse(IoUtility.compareTextFiles(file2, file1, s, "UTF16"));
1337    }
1338   
1339  3 IoUtility.saveFile(file1, StringUtility.replace(text, "@", "\012"), "UTF-16");
1340  3 IoUtility.saveFile(file2, StringUtility.replace(text, "@", "\015\012"), "UTF-16");
1341  3 if (s >= 0) {
1342  2 assertFalse(IoUtility.compareTextFiles(file1, file2, s, "ISO-8859-1"));
1343  2 assertFalse(IoUtility.compareTextFiles(file2, file1, s, "ISO-8859-1"));
1344  2 assertFalse(IoUtility.compareTextFiles(file1, file2, s, "UTF8"));
1345  2 assertFalse(IoUtility.compareTextFiles(file2, file1, s, "UTF8"));
1346  2 assertTrue(IoUtility.compareTextFiles(file1, file2, s, "UTF-16"));
1347  2 assertTrue(IoUtility.compareTextFiles(file2, file1, s, "UTF-16"));
1348    }
1349   
1350  3 IoUtility.saveFile(file1, StringUtility.replace(text, "@", "\012"), "ISO-8859-1");
1351  3 IoUtility.saveFile(file2, StringUtility.replace(text, "@", "\015"), "ISO-8859-1");
1352  3 assertTrue(IoUtility.compareTextFiles(file1, file2, s, "ISO-8859-1"));
1353  3 assertTrue(IoUtility.compareTextFiles(file2, file1, s, "ISO-8859-1"));
1354  3 assertTrue(IoUtility.compareTextFiles(file1, file2, s, "UTF8"));
1355  3 assertTrue(IoUtility.compareTextFiles(file2, file1, s, "UTF8"));
1356  3 if (s == 0) {
1357  1 assertFalse(IoUtility.compareTextFiles(file1, file2, s, "UTF-16"));
1358  1 assertFalse(IoUtility.compareTextFiles(file2, file1, s, "UTF-16"));
1359    } else {
1360  2 assertTrue(IoUtility.compareTextFiles(file1, file2, s, "UTF-16"));
1361  2 assertTrue(IoUtility.compareTextFiles(file2, file1, s, "UTF-16"));
1362    }
1363   
1364    // IoUtility.saveFile(file1, StringUtility.replace(text, "@", "\015\012"), "ISO-8859-1");
1365    // IoUtility.saveFile(file2, StringUtility.replace(text, "@", "" + (char) 0x2029),
1366    // "ISO-8859-1");
1367    // assertTrue(IoUtility.compareTextFiles(file1, file2, s, "ISO-8859-1"));
1368    // assertTrue(IoUtility.compareTextFiles(file2, file1, s, "ISO-8859-1"));
1369    // assertTrue(IoUtility.compareTextFiles(file1, file2, s, "UTF8"));
1370    // assertTrue(IoUtility.compareTextFiles(file2, file1, s, "UTF8"));
1371    // assertFalse(IoUtility.compareTextFiles(file1, file2, s, "UTF-16"));
1372    // assertFalse(IoUtility.compareTextFiles(file2, file1, s, "UTF-16"));
1373   
1374  3 assertTrue(file1.delete());
1375  3 assertTrue(file2.delete());
1376    }
1377   
1378    /**
1379    * Test {@link IoUtility#deleteDir(File, boolean)}.
1380    *
1381    * @throws Exception Test failed.
1382    */
 
1383  1 toggle public void testDeleteDirFileBoolean() throws Exception {
1384  1 final File dir1 = new File(getOutdir(), "testDeleteDirFileBoolean_directory");
1385  1 if (dir1.exists()) {
1386  0 assertTrue(IoUtility.deleteDir(dir1, true));
1387    }
1388  1 final File dir2 = new File(dir1, "testDeleteDirFileBoolean_directory2");
1389  1 final File file1 = new File(dir1, "testDeleteDirFileBoolean1.txt");
1390  1 final File file2 = new File(dir1, "testDeleteDirFileBoolean2.txt");
1391  1 final File file3 = new File(dir2, "testDeleteDirFileBoolean3.txt");
1392  1 assertTrue(dir2.mkdirs());
1393   
1394  1 IoUtility.saveFile(file1, "File 1", "ISO-8859-1");
1395  1 IoUtility.saveFile(file2, "File 2", "ISO-8859-1");
1396  1 IoUtility.saveFile(file3, "File 3", "ISO-8859-1");
1397  1 assertTrue(IoUtility.deleteDir(dir1, true));
1398  1 assertTrue(!dir1.exists());
1399   
1400  1 assertTrue(IoUtility.deleteDir(new File(getOutdir(), "testDeleteDirFileBoolean"), true));
1401  1 final File file = new File(getOutdir() + "/testDeleteDirFileBoolean/my/test/path");
1402  1 IoUtility.createNecessaryDirectories(file);
1403  1 IoUtility.saveFile(file, new StringBuffer("hei"), "UTF-8");
1404  1 assertTrue(IoUtility.deleteDir(new File(getOutdir(), "testDeleteDirFileBoolean"), true));
1405   
1406    }
1407   
1408    /**
1409    * Test {@link IoUtility#deleteDir(File, FileFilter)}.
1410    *
1411    * @throws Exception Test failed.
1412    */
 
1413  1 toggle public void testDeleteDirFileFileFilter() throws Exception {
1414  1 final File dir1 = new File(getOutdir(), "testDeleteDirFileFileFilter_directory");
1415  1 assertTrue(IoUtility.deleteDir(dir1, true));
1416  1 final File dir2 = new File(dir1, "testDeleteDirFileFileFilter_directory2");
1417  1 final File file1 = new File(dir1, "testDeleteDirFileFileFilter1.txt");
1418  1 final File file2 = new File(dir1, "testDeleteDirFileFileFilter2.txt");
1419  1 final File file3 = new File(dir2, "testDeleteDirFileFileFilter_3.txt");
1420  1 assertTrue(dir2.mkdirs());
1421   
1422  1 IoUtility.saveFile(file1, "File 1", "ISO-8859-1");
1423  1 IoUtility.saveFile(file2, "File 2", "ISO-8859-1");
1424  1 IoUtility.saveFile(file3, "File 3", "ISO-8859-1");
1425  1 assertTrue(IoUtility.deleteDir(dir1, new FileFilter() {
1426   
 
1427  3 toggle public boolean accept(final File pathname) {
1428  3 return pathname.toString().indexOf("testDeleteDirFileFileFilter_") >= 0;
1429    }
1430    }));
1431  1 assertTrue(dir1.exists());
1432  1 assertFalse(dir2.exists());
1433  1 assertFalse(file1.exists());
1434  1 assertFalse(file2.exists());
1435  1 assertFalse(file3.exists());
1436  1 assertTrue(IoUtility.deleteDir(dir1, true));
1437    }
1438   
 
1439  1 toggle public void testGetUserHomeDirectory() throws Exception {
1440  1 assertTrue(IoUtility.getUserHomeDirectory().exists());
1441  1 assertEquals(new File(System.getProperty("user.home")).getCanonicalPath(),
1442    IoUtility.getUserHomeDirectory().getCanonicalPath());
1443    }
1444   
 
1445  1 toggle public void testTransformURLPathToFilePath() throws Exception {
1446  1 final File start = new File("empty path");
1447  1 assertEquals(UrlUtility.transformURLPathToFilePath(UrlUtility.toUrl(start)).getCanonicalPath(),
1448    start.getCanonicalPath());
1449    }
1450   
 
1451  1 toggle public void testCreateNecessaryDirectories() throws Exception {
1452  1 assertTrue(IoUtility.deleteDir(new File(getOutdir(), "createNecessaryDirectories"), true));
1453  1 final File file = new File(getOutdir() + "/createNecessaryDirectories/my/test/path");
1454  1 IoUtility.createNecessaryDirectories(file);
1455  1 IoUtility.saveFile(file, new StringBuffer("hei"), "UTF-8");
1456  1 try {
1457  1 final File file1 = new File(getOutdir(), "createNecessaryDirectories/my");
1458  1 IoUtility.saveFile(file1, "hi", "UTF-8");
1459  0 final File file2 = new File(getOutdir(), "createNecessaryDirectories/my/test.txt");
1460  0 IoUtility.createNecessaryDirectories(file2);
1461  0 fail("Exception expected");
1462    } catch (IOException e) {
1463    // OK
1464    }
1465  1 assertTrue(IoUtility.deleteDir(new File(getOutdir(), "createNecessaryDirectories"), true));
1466    }
1467   
 
1468  1 toggle public void testCreateRelativePath() throws Exception {
1469  1 IoUtility.createNecessaryDirectories(null);
1470  1 final File dir1 = new File(getOutdir(), "createRelativePath1").getCanonicalFile();
1471  1 assertEquals("", IoUtility.createRelativePath(dir1, dir1));
1472  1 final File dir2 = new File(getOutdir(), "createRelativePath2/subdir").getCanonicalFile();
1473  1 assertEquals("../createRelativePath2/subdir/", IoUtility.createRelativePath(dir1, dir2));
1474  1 assertEquals("", IoUtility.createRelativePath(dir2, dir2));
1475    }
1476   
1477    /**
1478    * Test {@link IoUtility#createRelativePath(final File origin, final File next)}.
1479    *
1480    * @throws Exception Test failed.
1481    */
 
1482  1 toggle public void testCreateRelativePath2() throws Exception {
1483  1 assertEquals("local/", IoUtility.createRelativePath(new File("."), new File("local")));
1484  1 assertEquals("text/", IoUtility.createRelativePath(new File("/local/data"),
1485    new File("/local/data/text")));
1486  1 assertEquals("../../green/", IoUtility.createRelativePath(new File("/local/data"),
1487    new File("/green/")));
1488  1 assertEquals("../green/", IoUtility.createRelativePath(new File("/local/data"),
1489    new File("/local/green")));
1490  1 assertEquals("", IoUtility.createRelativePath(new File("/blue/../green/yellow"),
1491    new File("/green/yellow")));
1492    }
1493   
 
1494  1 toggle public void testWaitln() {
1495  1 final InputStream save = System.in;
1496  1 final String input = "\n";
1497  1 System.setIn(new ByteArrayInputStream(input.getBytes()));
1498  1 IoUtility.waitln();
1499  1 System.setIn(save);
1500    }
1501   
 
1502  1 toggle public void testCloseInputStream() throws Exception {
1503  1 IoUtility.close((InputStream) null);
1504  1 final InputStream in = new InputStream() {
1505    boolean isClosed = false;
 
1506  2 toggle public void close() throws IOException {
1507  2 if (isClosed) {
1508  1 throw new IOException("is already closed");
1509    }
1510  1 isClosed = true;
1511    }
 
1512  2 toggle public int read() throws IOException {
1513  2 if (isClosed) {
1514  1 return 0;
1515    } else {
1516  1 return 1;
1517    }
1518    }
1519    };
1520  1 assertEquals(1, in.read());
1521  1 IoUtility.close(in);
1522  1 assertEquals(0, in.read());
1523  1 IoUtility.close(in);
1524    }
1525   
 
1526  1 toggle public void testCloseReader() throws Exception {
1527  1 IoUtility.close((Reader) null);
1528  1 final Reader in = new Reader() {
1529    boolean isClosed = false;
 
1530  2 toggle public void close() throws IOException {
1531  2 if (isClosed) {
1532  1 throw new IOException("is already closed");
1533    }
1534  1 isClosed = true;
1535    }
 
1536  0 toggle public int read() throws IOException {
1537  0 if (isClosed) {
1538  0 return 0;
1539    } else {
1540  0 return 1;
1541    }
1542    }
 
1543  2 toggle public int read(char[] cbuf, int off, int len) throws IOException {
1544  2 if (isClosed) {
1545  1 return 0;
1546    } else {
1547  1 return 1;
1548    }
1549    }
1550    };
1551  1 assertEquals(1, in.read(null, 0, 0));
1552  1 IoUtility.close(in);
1553  1 assertEquals(0, in.read(null, 0, 0));
1554  1 IoUtility.close(in);
1555    }
1556   
 
1557  1 toggle public void testCloseOutputStream() throws Exception {
1558  1 IoUtility.close((OutputStream) null);
1559  1 final OutputStream out = new OutputStream() {
1560    boolean isClosed = false;
 
1561  2 toggle public void close() throws IOException {
1562  2 if (isClosed) {
1563  1 throw new IOException("is already closed");
1564    }
1565  1 isClosed = true;
1566    }
 
1567  2 toggle public void write(int b) throws IOException {
1568  2 if (isClosed) {
1569  1 throw new IOException("is already closed");
1570    }
1571    }
1572    };
1573  1 out.write(0);
1574  1 IoUtility.close(out);
1575  1 try {
1576  1 out.write(0);
1577  0 fail("Exception expected");
1578    } catch (IOException e) {
1579    // OK
1580    }
1581  1 IoUtility.close(out);
1582    }
1583   
 
1584  1 toggle public void testCloseWriter() throws Exception {
1585  1 IoUtility.close((Writer) null);
1586  1 final Writer out = new Writer() {
1587    boolean isClosed = false;
 
1588  2 toggle public void close() throws IOException {
1589  2 if (isClosed) {
1590  1 throw new IOException("is already closed");
1591    }
1592  1 isClosed = true;
1593    }
 
1594  2 toggle public void write(int b) throws IOException {
1595  2 if (isClosed) {
1596  1 throw new IOException("is already closed");
1597    }
1598    }
 
1599  0 toggle public void flush() throws IOException {
1600    }
 
1601  0 toggle public void write(char[] cbuf, int off, int len) throws IOException {
1602    }
1603    };
1604  1 out.write(0);
1605  1 IoUtility.close(out);
1606  1 try {
1607  1 out.write(0);
1608  0 fail("Exception expected");
1609    } catch (IOException e) {
1610    // OK
1611    }
1612  1 IoUtility.close(out);
1613    }
1614   
 
1615  1 toggle public void testGetStartDirectory() {
1616  1 final String webStart = (String) System.getProperties().get("javawebstart.version");
1617  1 final Properties sysProps = System.getProperties();
1618  1 sysProps.remove("javawebstart.version");
1619  1 assertEquals(new File("."), IoUtility.getStartDirectory("qedeq"));
1620  1 System.setProperty("javawebstart.version", "1.7");
1621  1 assertEquals(new File(IoUtility.getUserHomeDirectory(), ".qedeq"),
1622    IoUtility.getStartDirectory("qedeq"));
1623  1 if (webStart != null) {
1624  0 System.setProperty("javawebstart.version", webStart);
1625    } else {
1626  1 sysProps.remove("javawebstart.version");
1627    }
1628    }
1629   
 
1630  1 toggle public void testLoadPropertiesUrl() throws Exception {
1631  1 try {
1632  1 IoUtility.loadProperties(null);
1633  0 fail("Exception expected");
1634    } catch (RuntimeException e) {
1635    // OK
1636    }
1637  1 final File file = new File(getOutdir(), "loadProperties.properties");
1638  1 assertTrue(IoUtility.deleteDir(file, true));
1639  1 IoUtility.saveFile(file, "sharif=is a cat", "ISO-8859-1");
1640  1 final Properties prop = IoUtility.loadProperties(UrlUtility.toUrl(file));
1641  1 assertEquals("is a cat", prop.getProperty("sharif"));
1642  1 assertTrue(file.delete());
1643    }
1644   
 
1645  1 toggle public void testSleep() {
1646  1 long start = System.currentTimeMillis();
1647  1 IoUtility.sleep(1150);
1648  1 long time = System.currentTimeMillis() - start;
1649  1 assertTrue(time > 1100);
1650  1 assertTrue(time < 1200);
1651    }
1652   
 
1653  1 toggle public void testGetJavaVersion() {
1654    // at least there should be no Exception ...
1655  1 IoUtility.getJavaVersion();
1656    }
1657   
 
1658  1 toggle public void testGetSortedSystemProperties() {
1659    // at least there should be no Exception ...
1660  1 IoUtility.getSortedSystemProperties();
1661    }
1662   
1663    }