1 3 package jodd.io; 4 5 import junit.framework.TestCase; 6 7 import java.io.File ; 8 import java.io.IOException ; 9 10 public class FileUtilTest extends TestCase { 11 12 protected String dataRoot; 13 14 protected void setUp() throws Exception { 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 (dataRoot, "sb.dat"), new File (dataRoot, "sb1.dat")); 30 assertFalse(FileUtil.isNewer(new File (dataRoot, "sb.dat"), new File (dataRoot, "sb1.dat"))); 31 assertFalse(FileUtil.isOlder(new File (dataRoot, "sb.dat"), new File (dataRoot, "sb1.dat"))); 32 FileUtil.delete(new File (dataRoot, "sb1.dat")); 33 } catch (Exception ex) { 34 fail("FileUtil.copy " + ex.toString()); 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(new File (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 assertEquals(s, s2); 54 55 char buf[] = s.toCharArray(); 57 buf[0] = 256; 58 s = new String (buf); 59 60 try { 61 FileUtil.writeString(dataRoot + "/test.txt", s); 62 } catch(Exception ex) { 63 fail("FileUtil.writeString " + ex.toString()); 64 } 65 66 try { 67 s2 = FileUtil.readString(dataRoot + "/test.txt"); 68 } catch(Exception 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 ioex) { 78 fail("FileUtil.delete" + ioex.toString()); 79 } 80 } 81 82 83 public void testUnicodeString() { 84 String 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 (buf); 89 90 try { 91 FileUtil.writeString(dataRoot + "/test2.txt", s, "UTF-16"); 92 } catch(Exception ex) { 93 fail("FileUtil.writeString " + ex.toString()); 94 } 95 96 String s2 = null; 97 try { 98 s2 = FileUtil.readString(dataRoot + "/test2.txt", "UTF-16"); 99 } catch(Exception ex) { 100 fail("FileUtil.readString " + ex.toString()); 101 } 102 assertEquals(s, s2); 103 104 try { 105 FileUtil.delete(dataRoot + "/test2.txt"); 106 } catch (IOException ioex) { 107 fail("FileUtil.delete" + ioex.toString()); 108 } 109 110 } 111 112 public void testFileManipulations() { 113 String root = dataRoot + "/file/"; 114 String tmp = root + "tmp/"; 115 String tmp2 = root + "xxx/"; 116 String tmp3 = root + "zzz/"; 117 118 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 ioex) { 124 fail(ioex.toString()); 125 } 126 127 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 ioex) { 134 fail(ioex.toString()); 135 } 136 137 try { 139 FileUtil.copyFileToDir(root + "a.txt", tmp, FileUtil.newSettings().dontOverwrite()); 140 fail("copy file don't overwrite"); 141 } catch (IOException e) { 142 } 144 145 try { 147 FileUtil.moveFile(root + "w.txt", tmp + "w.txt"); 148 FileUtil.moveFileToDir(root + "w.png", tmp); 149 } catch (IOException 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 e) { 157 } 159 160 161 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 ioex) { 169 fail(ioex.toString()); 170 } 171 172 try { 173 FileUtil.deleteFile(tmp + "a.txt"); 174 fail("delete file strict delete"); 175 } catch (IOException e) { 176 } 178 179 try { 181 FileUtil.moveDir(tmp, tmp2); 182 } catch (IOException ioex) { 183 fail(ioex.toString()); 184 } 185 186 try { 188 FileUtil.copyDir(tmp2, tmp3); 189 } catch (IOException ioex) { 190 fail(ioex.toString()); 191 } 192 193 try { 195 FileUtil.deleteDir(tmp2); 196 FileUtil.deleteDir(tmp3); 197 } catch (IOException ioex) { 198 fail(ioex.toString()); 199 } 200 } 201 } 202 | Popular Tags |