KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jodd > file > TestFileUtil


1 package jodd.file;
2
3 import junit.framework.TestCase;
4
5 public class TestFileUtil extends TestCase {
6     
7     protected final String dataRoot = "modules/testcase/data";
8
9     public void testObjSerialization() {
10         SimpleBean sb = new SimpleBean();
11         assertEquals(3, sb.getFoo());
12         try {
13             FileUtil.writeObject(dataRoot + "/sb.dat", sb);
14         } catch (Exception ex) {
15             fail("FileUtil.writeObject " + ex.toString());
16         }
17
18         try {
19             Object o = FileUtil.readObject(dataRoot + "/sb.dat");
20             SimpleBean sb2 = (SimpleBean) o;
21             assertEquals(3, sb2.getFoo());
22         } catch (Exception ex) {
23             fail("FileUtil.readObject " + ex.toString());
24         }
25     }
26
27
28     public void testFileManipulation() {
29
30         try {
31             FileUtil.copy(dataRoot + "/sb.dat", dataRoot + "/sb1.dat");
32         } catch (Exception ex) {
33             fail("FileUtil.copy " + ex.toString());
34         }
35     }
36
37
38     public void testString() {
39         String s = "This is a test file\nIt only has\nthree lines!!";
40
41         try {
42             FileUtil.writeString(dataRoot + "/test.txt", s);
43         } catch(Exception ex) {
44             fail("FileUtil.writeString " + ex.toString());
45         }
46
47         String s2 = null;
48         try {
49             s2 = FileUtil.readString(dataRoot + "/test.txt");
50         } catch(Exception ex) {
51             fail("FileUtil.readString " + ex.toString());
52         }
53
54         assertEquals(s, s2);
55
56         // test unicode chars (i.e. greaer then 255)
57
char buf[] = s.toCharArray();
58         buf[0] = 256;
59         s = new String(buf);
60
61         try {
62             FileUtil.writeString(dataRoot + "/test.txt", s);
63         } catch(Exception ex) {
64             fail("FileUtil.writeString " + ex.toString());
65         }
66
67         try {
68             s2 = FileUtil.readString(dataRoot + "/test.txt");
69         } catch(Exception ex) {
70             fail("FileUtil.readString " + ex.toString());
71         }
72
73         assertEquals(s.substring(1), s2.substring(1));
74         assertTrue(s.charAt(0) != s2.charAt(0));
75     }
76
77     public void testUnicodeString() {
78         String s = "This is a test file\nIt only has\nthree lines!!";
79
80         char buf[] = s.toCharArray();
81         buf[0] = 256;
82         s = new String(buf);
83
84         try {
85             FileUtil.writeString(dataRoot + "/test2.txt", s, "UTF-16");
86         } catch(Exception ex) {
87             fail("FileUtil.writeString " + ex.toString());
88         }
89
90         String s2 = null;
91
92         try {
93             s2 = FileUtil.readString(dataRoot + "/test2.txt", "UTF-16");
94         } catch(Exception ex) {
95             fail("FileUtil.readString " + ex.toString());
96         }
97         assertEquals(s, s2);
98
99     }
100 }
101
Popular Tags