KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > inversoft > util > test > FileToolsTest


1 /*
2  * Copyright (c) 2003, Inversoft
3  *
4  * This software is distribuable under the GNU Lesser General Public License.
5  * For more information visit gnu.org.
6  */

7 package com.inversoft.util.test;
8
9
10 import java.io.File JavaDoc;
11 import java.io.FileReader JavaDoc;
12
13 import junit.framework.TestCase;
14
15 import com.inversoft.util.FileTools;
16
17
18 /**
19  * This tests the com.inversoft.util.FileTools class. It
20  * only works on UNIX systems
21  *
22  * @author Brian Pontarelli
23  */

24 public class FileToolsTest extends TestCase {
25
26     public FileToolsTest(String JavaDoc name) {
27         super(name);
28     }
29
30     public void testConvertPath() {
31         String JavaDoc unixPath = "foo/bar/file.txt";
32         String JavaDoc dosPath = "foo\\bar\\file.txt";
33         String JavaDoc result = FileTools.convertPath(unixPath);
34
35         // This should only work on unix
36
if (File.pathSeparatorChar == '/') {
37             assertEquals(unixPath, result);
38         }
39
40         // This should only work on windows
41
if (File.pathSeparatorChar == '\\') {
42             assertEquals(dosPath, result);
43         }
44
45         String JavaDoc result2 = FileTools.convertPath(dosPath);
46
47         // This should only work on unix
48
if (File.pathSeparatorChar == '/') {
49             assertEquals(unixPath, result2);
50         }
51
52         // This should only work on windows
53
if (File.pathSeparatorChar == '\\') {
54             assertEquals(dosPath, result2);
55         }
56     }
57
58     /**
59      * Tests that the copy file works correctly by copying a file and comparing
60      * the copy, byte for byte, to the original
61      */

62     public void testCopy() {
63         String JavaDoc path = FileTools.convertPath("src/com/inversoft/util/test/Bean.java");
64         File JavaDoc from = new File JavaDoc(path);
65         File JavaDoc to = new File JavaDoc(path + ".copy");
66
67         try {
68             FileTools.copy(from, to);
69         } catch (Exception JavaDoc e) {
70             fail(e.toString());
71         }
72
73         // Make sure it exists and they are the same size
74
assertTrue("Should exist", to.exists());
75         assertTrue("Should be same size", from.length() == to.length());
76
77         try {
78             // Compare them
79
FileReader JavaDoc fromReader = new FileReader JavaDoc(from);
80             FileReader JavaDoc toReader = new FileReader JavaDoc(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 JavaDoc e) {
90             fail(e.toString());
91         }
92     }
93
94     /**
95      * Tests that the copy file works correctly by copying a file and comparing
96      * the copy, byte for byte, to the original
97      */

98     public void testCopyWithDelete() {
99         String JavaDoc path = FileTools.convertPath("src/com/inversoft/util/test/Bean.java");
100         File JavaDoc from = new File JavaDoc(path);
101         File JavaDoc to = new File JavaDoc(path + ".copy");
102
103         assertTrue(to.exists()); // Just in case
104
long before = to.lastModified();
105         try {
106             FileTools.copy(from, to);
107         } catch (Exception JavaDoc e) {
108             fail(e.toString());
109         }
110
111         long after = to.lastModified();
112         assertTrue(before <= after);
113
114         // Make sure it exists and they are the same size
115
assertTrue("Should exist", to.exists());
116         assertTrue("Should be same size", from.length() == to.length());
117
118         try {
119             // Compare them
120
FileReader JavaDoc fromReader = new FileReader JavaDoc(from);
121             FileReader JavaDoc toReader = new FileReader JavaDoc(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 JavaDoc e) {
131             fail(e.toString());
132         }
133     }
134
135     /**
136      * Tests that the copy file works correctly on a larger file
137      */

138     public void testCopyLargeFile() {
139         String JavaDoc path = FileTools.convertPath("src/com/inversoft/util/test/ObjectToolsTest.java");
140         File JavaDoc from = new File JavaDoc(path);
141         File JavaDoc to = new File JavaDoc(path + ".copy");
142
143         try {
144             FileTools.copy(from, to);
145         } catch (Exception JavaDoc e) {
146             fail(e.toString());
147         }
148
149         // Make sure it exists and they are the same size
150
assertTrue("Should exist", to.exists());
151         assertTrue("Should be same size", from.length() == to.length());
152
153         try {
154             // Compare them
155
FileReader JavaDoc fromReader = new FileReader JavaDoc(from);
156             FileReader JavaDoc toReader = new FileReader JavaDoc(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 JavaDoc e) {
166             fail(e.toString());
167         }
168     }
169 }
170
Popular Tags