1 7 package com.inversoft.util.test; 8 9 10 import java.io.File ; 11 import java.io.FileReader ; 12 13 import junit.framework.TestCase; 14 15 import com.inversoft.util.FileTools; 16 17 18 24 public class FileToolsTest extends TestCase { 25 26 public FileToolsTest(String name) { 27 super(name); 28 } 29 30 public void testConvertPath() { 31 String unixPath = "foo/bar/file.txt"; 32 String dosPath = "foo\\bar\\file.txt"; 33 String result = FileTools.convertPath(unixPath); 34 35 if (File.pathSeparatorChar == '/') { 37 assertEquals(unixPath, result); 38 } 39 40 if (File.pathSeparatorChar == '\\') { 42 assertEquals(dosPath, result); 43 } 44 45 String result2 = FileTools.convertPath(dosPath); 46 47 if (File.pathSeparatorChar == '/') { 49 assertEquals(unixPath, result2); 50 } 51 52 if (File.pathSeparatorChar == '\\') { 54 assertEquals(dosPath, result2); 55 } 56 } 57 58 62 public void testCopy() { 63 String path = FileTools.convertPath("src/com/inversoft/util/test/Bean.java"); 64 File from = new File (path); 65 File to = new File (path + ".copy"); 66 67 try { 68 FileTools.copy(from, to); 69 } catch (Exception e) { 70 fail(e.toString()); 71 } 72 73 assertTrue("Should exist", to.exists()); 75 assertTrue("Should be same size", from.length() == to.length()); 76 77 try { 78 FileReader fromReader = new FileReader (from); 80 FileReader toReader = new FileReader (to); 81 int fromChar = 0; 82 int toChar = 1; 83 84 do { 85 fromChar = fromReader.read(); 86 toChar = toReader.read(); 87 assertTrue("Should be equal", fromChar == toChar); 88 } while (fromChar != -1); 89 } catch (Exception e) { 90 fail(e.toString()); 91 } 92 } 93 94 98 public void testCopyWithDelete() { 99 String path = FileTools.convertPath("src/com/inversoft/util/test/Bean.java"); 100 File from = new File (path); 101 File to = new File (path + ".copy"); 102 103 assertTrue(to.exists()); long before = to.lastModified(); 105 try { 106 FileTools.copy(from, to); 107 } catch (Exception e) { 108 fail(e.toString()); 109 } 110 111 long after = to.lastModified(); 112 assertTrue(before <= after); 113 114 assertTrue("Should exist", to.exists()); 116 assertTrue("Should be same size", from.length() == to.length()); 117 118 try { 119 FileReader fromReader = new FileReader (from); 121 FileReader toReader = new FileReader (to); 122 int fromChar = 0; 123 int toChar = 1; 124 125 do { 126 fromChar = fromReader.read(); 127 toChar = toReader.read(); 128 assertTrue("Should be equal", fromChar == toChar); 129 } while (fromChar != -1); 130 } catch (Exception e) { 131 fail(e.toString()); 132 } 133 } 134 135 138 public void testCopyLargeFile() { 139 String path = FileTools.convertPath("src/com/inversoft/util/test/ObjectToolsTest.java"); 140 File from = new File (path); 141 File to = new File (path + ".copy"); 142 143 try { 144 FileTools.copy(from, to); 145 } catch (Exception e) { 146 fail(e.toString()); 147 } 148 149 assertTrue("Should exist", to.exists()); 151 assertTrue("Should be same size", from.length() == to.length()); 152 153 try { 154 FileReader fromReader = new FileReader (from); 156 FileReader toReader = new FileReader (to); 157 int fromChar = 0; 158 int toChar = 1; 159 160 do { 161 fromChar = fromReader.read(); 162 toChar = toReader.read(); 163 assertTrue("Should be equal", fromChar == toChar); 164 } while (fromChar != -1); 165 } catch (Exception e) { 166 fail(e.toString()); 167 } 168 } 169 } 170 | Popular Tags |