KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jodd > io > FileUtilTest


1 // Copyright (c) 2003-2007, Jodd Team (jodd.sf.net). All Rights Reserved.
2

3 package jodd.io;
4
5 import junit.framework.TestCase;
6
7 import java.io.File JavaDoc;
8 import java.io.IOException JavaDoc;
9
10 public class FileUtilTest extends TestCase {
11
12     protected String JavaDoc dataRoot;
13
14     protected void setUp() throws Exception JavaDoc {
15         super.setUp();
16         if (dataRoot != null) {
17             return;
18         }
19         dataRoot = System.getProperty("mod.dir");
20         if (dataRoot == null) {
21             dataRoot = "";
22         }
23         dataRoot += "/test/data";
24         System.out.println(dataRoot);
25     }
26
27     public void testFileManipulation() {
28         try {
29             FileUtil.copy(new File JavaDoc(dataRoot, "sb.dat"), new File JavaDoc(dataRoot, "sb1.dat"));
30             assertFalse(FileUtil.isNewer(new File JavaDoc(dataRoot, "sb.dat"), new File JavaDoc(dataRoot, "sb1.dat")));
31             assertFalse(FileUtil.isOlder(new File JavaDoc(dataRoot, "sb.dat"), new File JavaDoc(dataRoot, "sb1.dat")));
32             FileUtil.delete(new File JavaDoc(dataRoot, "sb1.dat"));
33         } catch (Exception JavaDoc ex) {
34             fail("FileUtil.copy " + ex.toString());
35         }
36     }
37
38     public void testString() {
39         String JavaDoc s = "This is a test file\nIt only has\nthree lines!!";
40
41         try {
42             FileUtil.writeString(new File JavaDoc(dataRoot, "test.txt"), s);
43         } catch(Exception JavaDoc ex) {
44             fail("FileUtil.writeString " + ex.toString());
45         }
46
47         String JavaDoc s2 = null;
48         try {
49             s2 = FileUtil.readString(dataRoot + "/test.txt");
50         } catch(Exception JavaDoc ex) {
51             fail("FileUtil.readString " + ex.toString());
52         }
53         assertEquals(s, s2);
54
55         // test unicode chars (i.e. greaer then 255)
56
char buf[] = s.toCharArray();
57         buf[0] = 256;
58         s = new String JavaDoc(buf);
59
60         try {
61             FileUtil.writeString(dataRoot + "/test.txt", s);
62         } catch(Exception JavaDoc ex) {
63             fail("FileUtil.writeString " + ex.toString());
64         }
65
66         try {
67             s2 = FileUtil.readString(dataRoot + "/test.txt");
68         } catch(Exception JavaDoc ex) {
69             fail("FileUtil.readString " + ex.toString());
70         }
71
72         assertEquals(s.substring(1), s2.substring(1));
73         assertTrue(s.charAt(0) != s2.charAt(0));
74
75         try {
76             FileUtil.delete(dataRoot + "/test.txt");
77         } catch (IOException JavaDoc ioex) {
78             fail("FileUtil.delete" + ioex.toString());
79         }
80     }
81
82
83     public void testUnicodeString() {
84         String JavaDoc s = "This is a test file\nIt only has\nthree lines!!";
85
86         char buf[] = s.toCharArray();
87         buf[0] = 256;
88         s = new String JavaDoc(buf);
89
90         try {
91             FileUtil.writeString(dataRoot + "/test2.txt", s, "UTF-16");
92         } catch(Exception JavaDoc ex) {
93             fail("FileUtil.writeString " + ex.toString());
94         }
95
96         String JavaDoc s2 = null;
97         try {
98             s2 = FileUtil.readString(dataRoot + "/test2.txt", "UTF-16");
99         } catch(Exception JavaDoc ex) {
100             fail("FileUtil.readString " + ex.toString());
101         }
102         assertEquals(s, s2);
103
104         try {
105             FileUtil.delete(dataRoot + "/test2.txt");
106         } catch (IOException JavaDoc ioex) {
107             fail("FileUtil.delete" + ioex.toString());
108         }
109
110     }
111
112     public void testFileManipulations() {
113         String JavaDoc root = dataRoot + "/file/";
114         String JavaDoc tmp = root + "tmp/";
115         String JavaDoc tmp2 = root + "xxx/";
116         String JavaDoc tmp3 = root + "zzz/";
117
118         // copy
119
try {
120             FileUtil.copyFile(root + "a.txt", root + "w.txt");
121             FileUtil.copyFile(root + "a.png", root + "w.png");
122             FileUtil.copyFile(root + "a.txt", root + "w.txt");
123         } catch(IOException JavaDoc ioex) {
124             fail(ioex.toString());
125         }
126
127         // mkdirs
128
try {
129             FileUtil.mkdir(tmp);
130             FileUtil.mkdirs(tmp + "x/");
131             FileUtil.copyFileToDir(root + "a.txt", tmp);
132             FileUtil.copyFileToDir(root + "a.png", tmp);
133         } catch (IOException JavaDoc ioex) {
134             fail(ioex.toString());
135         }
136
137         // don't overwrite
138
try {
139             FileUtil.copyFileToDir(root + "a.txt", tmp, FileUtil.newSettings().dontOverwrite());
140             fail("copy file don't overwrite");
141         } catch (IOException JavaDoc e) {
142             // ignore
143
}
144
145         // move
146
try {
147             FileUtil.moveFile(root + "w.txt", tmp + "w.txt");
148             FileUtil.moveFileToDir(root + "w.png", tmp);
149         } catch (IOException JavaDoc ioex) {
150             fail(ioex.toString());
151         }
152
153         try {
154             FileUtil.moveFileToDir(root + "w.png", tmp, FileUtil.cloneSettings().dontOverwrite());
155             fail("move file don't overwrite");
156         } catch (IOException JavaDoc e) {
157             // ignore
158
}
159
160
161         // delete
162

163         try {
164             FileUtil.deleteFile(tmp + "a.txt");
165             FileUtil.deleteFile(tmp + "a.png");
166             FileUtil.deleteFile(tmp + "w.txt");
167             FileUtil.deleteFile(tmp + "w.png");
168         } catch (IOException JavaDoc ioex) {
169             fail(ioex.toString());
170         }
171
172         try {
173             FileUtil.deleteFile(tmp + "a.txt");
174             fail("delete file strict delete");
175         } catch (IOException JavaDoc e) {
176             // ignore
177
}
178
179         // movedir
180
try {
181             FileUtil.moveDir(tmp, tmp2);
182         } catch (IOException JavaDoc ioex) {
183             fail(ioex.toString());
184         }
185
186         // copyDir
187
try {
188             FileUtil.copyDir(tmp2, tmp3);
189         } catch (IOException JavaDoc ioex) {
190             fail(ioex.toString());
191         }
192
193         // deleteDir
194
try {
195             FileUtil.deleteDir(tmp2);
196             FileUtil.deleteDir(tmp3);
197         } catch (IOException JavaDoc ioex) {
198             fail(ioex.toString());
199         }
200     }
201 }
202
Popular Tags