001: // Copyright (c) 2003-2007, Jodd Team (jodd.sf.net). All Rights Reserved.
002:
003: package jodd.io;
004:
005: import junit.framework.TestCase;
006:
007: import java.io.File;
008: import java.io.IOException;
009:
010: public class FileUtilTest extends TestCase {
011:
012: protected String dataRoot;
013:
014: @Override
015: protected void setUp() throws Exception {
016: super .setUp();
017: if (dataRoot != null) {
018: return;
019: }
020: dataRoot = System.getProperty("mod.dir");
021: if (dataRoot == null) {
022: dataRoot = "";
023: }
024: dataRoot += "/test/data";
025: System.out.println(dataRoot);
026: }
027:
028: public void testFileManipulation() {
029: try {
030: FileUtil.copy(new File(dataRoot, "sb.dat"), new File(
031: dataRoot, "sb1.dat"));
032: assertFalse(FileUtil.isNewer(new File(dataRoot, "sb.dat"),
033: new File(dataRoot, "sb1.dat")));
034: assertFalse(FileUtil.isOlder(new File(dataRoot, "sb.dat"),
035: new File(dataRoot, "sb1.dat")));
036: FileUtil.delete(new File(dataRoot, "sb1.dat"));
037: } catch (Exception ex) {
038: fail("FileUtil.copy " + ex.toString());
039: }
040: }
041:
042: public void testString() {
043: String s = "This is a test file\nIt only has\nthree lines!!";
044:
045: try {
046: FileUtil.writeString(new File(dataRoot, "test.txt"), s);
047: } catch (Exception ex) {
048: fail("FileUtil.writeString " + ex.toString());
049: }
050:
051: String s2 = null;
052: try {
053: s2 = FileUtil.readString(dataRoot + "/test.txt");
054: } catch (Exception ex) {
055: fail("FileUtil.readString " + ex.toString());
056: }
057: assertEquals(s, s2);
058:
059: // test unicode chars (i.e. greaer then 255)
060: char buf[] = s.toCharArray();
061: buf[0] = 256;
062: s = new String(buf);
063:
064: try {
065: FileUtil.writeString(dataRoot + "/test.txt", s);
066: } catch (Exception ex) {
067: fail("FileUtil.writeString " + ex.toString());
068: }
069:
070: try {
071: s2 = FileUtil.readString(dataRoot + "/test.txt");
072: } catch (Exception ex) {
073: fail("FileUtil.readString " + ex.toString());
074: }
075:
076: assertEquals(s.substring(1), s2.substring(1));
077: assertTrue(s.charAt(0) != s2.charAt(0));
078:
079: try {
080: FileUtil.delete(dataRoot + "/test.txt");
081: } catch (IOException ioex) {
082: fail("FileUtil.delete" + ioex.toString());
083: }
084: }
085:
086: public void testUnicodeString() {
087: String s = "This is a test file\nIt only has\nthree lines!!";
088:
089: char buf[] = s.toCharArray();
090: buf[0] = 256;
091: s = new String(buf);
092:
093: try {
094: FileUtil.writeString(dataRoot + "/test2.txt", s, "UTF-16");
095: } catch (Exception ex) {
096: fail("FileUtil.writeString " + ex.toString());
097: }
098:
099: String s2 = null;
100: try {
101: s2 = FileUtil.readString(dataRoot + "/test2.txt", "UTF-16");
102: } catch (Exception ex) {
103: fail("FileUtil.readString " + ex.toString());
104: }
105: assertEquals(s, s2);
106:
107: try {
108: FileUtil.delete(dataRoot + "/test2.txt");
109: } catch (IOException ioex) {
110: fail("FileUtil.delete" + ioex.toString());
111: }
112:
113: }
114:
115: public void testFileManipulations() {
116: String root = dataRoot + "/file/";
117: String tmp = root + "tmp/";
118: String tmp2 = root + "xxx/";
119: String tmp3 = root + "zzz/";
120:
121: // copy
122: try {
123: FileUtil.copyFile(root + "a.txt", root + "w.txt");
124: FileUtil.copyFile(root + "a.png", root + "w.png");
125: FileUtil.copyFile(root + "a.txt", root + "w.txt");
126: } catch (IOException ioex) {
127: fail(ioex.toString());
128: }
129:
130: // mkdirs
131: try {
132: FileUtil.mkdir(tmp);
133: FileUtil.mkdirs(tmp + "x/");
134: FileUtil.copyFileToDir(root + "a.txt", tmp);
135: FileUtil.copyFileToDir(root + "a.png", tmp);
136: } catch (IOException ioex) {
137: fail(ioex.toString());
138: }
139:
140: // don't overwrite
141: try {
142: FileUtil.copyFileToDir(root + "a.txt", tmp, FileUtil
143: .newSettings().dontOverwrite());
144: fail("copy file don't overwrite");
145: } catch (IOException e) {
146: // ignore
147: }
148:
149: // move
150: try {
151: FileUtil.moveFile(root + "w.txt", tmp + "w.txt");
152: FileUtil.moveFileToDir(root + "w.png", tmp);
153: } catch (IOException ioex) {
154: fail(ioex.toString());
155: }
156:
157: try {
158: FileUtil.moveFileToDir(root + "w.png", tmp, FileUtil
159: .cloneSettings().dontOverwrite());
160: fail("move file don't overwrite");
161: } catch (IOException e) {
162: // ignore
163: }
164:
165: // delete
166:
167: try {
168: FileUtil.deleteFile(tmp + "a.txt");
169: FileUtil.deleteFile(tmp + "a.png");
170: FileUtil.deleteFile(tmp + "w.txt");
171: FileUtil.deleteFile(tmp + "w.png");
172: } catch (IOException ioex) {
173: fail(ioex.toString());
174: }
175:
176: try {
177: FileUtil.deleteFile(tmp + "a.txt");
178: fail("delete file strict delete");
179: } catch (IOException e) {
180: // ignore
181: }
182:
183: // movedir
184: try {
185: FileUtil.moveDir(tmp, tmp2);
186: } catch (IOException ioex) {
187: fail(ioex.toString());
188: }
189:
190: // copyDir
191: try {
192: FileUtil.copyDir(tmp2, tmp3);
193: } catch (IOException ioex) {
194: fail(ioex.toString());
195: }
196:
197: // deleteDir
198: try {
199: FileUtil.deleteDir(tmp2);
200: FileUtil.deleteDir(tmp3);
201: } catch (IOException ioex) {
202: fail(ioex.toString());
203: }
204: }
205: }
|