KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > junitx > framework > FileAssertTest


1 package junitx.framework;
2
3 import junit.framework.AssertionFailedError;
4 import junit.framework.TestCase;
5 import junitx.util.XMLPropertyManager;
6
7 import java.io.File JavaDoc;
8 import java.io.FileInputStream JavaDoc;
9 import java.io.FileOutputStream JavaDoc;
10 import java.io.StringReader JavaDoc;
11
12 /**
13  * @version $Revision: 1.6 $ $Date: 2003/04/28 02:47:32 $
14  * @author <a HREF="mailto:vbossica@users.sourceforge.net">Vladimir R. Bossicard</a>
15  */

16 public class FileAssertTest
17         extends TestCase {
18
19     public FileAssertTest(String JavaDoc name) {
20         super(name);
21     }
22
23     public void testSucceed()
24             throws Exception JavaDoc {
25         String JavaDoc input = "line1\n" +
26                 "line2\n" +
27                 "line3\n" +
28                 "line4\n" +
29                 "line5\n";
30         FileAssert.assertEquals(null, new StringReader JavaDoc(input), new StringReader JavaDoc(input));
31     }
32
33     public void testFailDiffer()
34             throws Exception JavaDoc {
35         String JavaDoc expected = "line1\n" +
36                 "line2\n" +
37                 "line3\n" +
38                 "line4\n" +
39                 "line5\n";
40         String JavaDoc actual = "line1\n" +
41                 "line2\n" +
42                 "line10\n" +
43                 "line4\n" +
44                 "line5\n";
45         try {
46             FileAssert.assertEquals(null, new StringReader JavaDoc(expected), new StringReader JavaDoc(actual));
47         } catch (AssertionFailedError e) {
48             return;
49         }
50         fail();
51     }
52
53     public void testFailLonger()
54             throws Exception JavaDoc {
55         String JavaDoc expected = "line1\n" +
56                 "line2\n" +
57                 "line3\n" +
58                 "line4\n" +
59                 "line5\n";
60         String JavaDoc actual = "line1\n" +
61                 "line2\n" +
62                 "line3\n" +
63                 "line4\n" +
64                 "line5\n" +
65                 "line6\n";
66         try {
67             FileAssert.assertEquals(null, new StringReader JavaDoc(expected), new StringReader JavaDoc(actual));
68         } catch (AssertionFailedError e) {
69             return;
70         }
71         fail();
72     }
73
74     public void testFailShorter()
75             throws Exception JavaDoc {
76         String JavaDoc expected = "line1\n" +
77                 "line2\n" +
78                 "line3\n" +
79                 "line4\n" +
80                 "line5\n";
81         String JavaDoc actual = "line1\n" +
82                 "line2\n" +
83                 "line3\n" +
84                 "line4\n";
85         try {
86             FileAssert.assertEquals(null, new StringReader JavaDoc(expected), new StringReader JavaDoc(actual));
87         } catch (AssertionFailedError e) {
88             return;
89         }
90         fail();
91     }
92
93     public void testSucceedFile()
94             throws Exception JavaDoc {
95         File JavaDoc expected = new File JavaDoc(XMLPropertyManager.getProperty("expected.file"));
96         File JavaDoc actual = new File JavaDoc(XMLPropertyManager.getProperty("normal.file"));
97
98         FileAssert.assertEquals(expected, actual);
99     }
100
101     public void testFailFileDiffer()
102             throws Exception JavaDoc {
103         File JavaDoc expected = new File JavaDoc(XMLPropertyManager.getProperty("expected.file"));
104         File JavaDoc actual = new File JavaDoc(XMLPropertyManager.getProperty("differ.file"));
105
106         try {
107             FileAssert.assertEquals(expected, actual);
108         } catch (AssertionFailedError e) {
109             return;
110         }
111         fail();
112     }
113
114     public void testFailFileLonger()
115             throws Exception JavaDoc {
116         File JavaDoc expected = new File JavaDoc(XMLPropertyManager.getProperty("expected.file"));
117         File JavaDoc actual = new File JavaDoc(XMLPropertyManager.getProperty("long.file"));
118
119         try {
120             FileAssert.assertEquals(expected, actual);
121         } catch (AssertionFailedError e) {
122             return;
123         }
124         fail();
125     }
126
127     public void testFailFileShorter()
128             throws Exception JavaDoc {
129         File JavaDoc expected = new File JavaDoc(XMLPropertyManager.getProperty("expected.file"));
130         File JavaDoc actual = new File JavaDoc(XMLPropertyManager.getProperty("short.file"));
131
132         try {
133             FileAssert.assertEquals(expected, actual);
134         } catch (AssertionFailedError e) {
135             return;
136         }
137         fail();
138     }
139
140     public void testSucceedBinaryFile()
141             throws Exception JavaDoc {
142         File JavaDoc expected = new File JavaDoc(XMLPropertyManager.getProperty("expected.binary.file"));
143         File JavaDoc actual = new File JavaDoc(XMLPropertyManager.getProperty("normal.binary.file"));
144
145         FileAssert.assertBinaryEquals(expected, actual);
146     }
147
148     public void testFailBinaryFileDiffer()
149             throws Exception JavaDoc {
150         File JavaDoc expected = new File JavaDoc(XMLPropertyManager.getProperty("expected.binary.file"));
151         File JavaDoc actual = new File JavaDoc(XMLPropertyManager.getProperty("differ.binary.file"));
152
153         try {
154             FileAssert.assertBinaryEquals(expected, actual);
155         } catch (AssertionFailedError e) {
156             ThrowableAssert.assertSimilar(new AssertionFailedError("files differ at byte 151"), e);
157             return;
158         }
159         fail();
160     }
161
162     public void testFailBinaryFileLonger()
163             throws Exception JavaDoc {
164         File JavaDoc expected = new File JavaDoc(XMLPropertyManager.getProperty("expected.binary.file"));
165         File JavaDoc actual = new File JavaDoc(XMLPropertyManager.getProperty("long.binary.file"));
166
167         try {
168             FileAssert.assertBinaryEquals(expected, actual);
169         } catch (AssertionFailedError e) {
170             ThrowableAssert.assertSimilar(new AssertionFailedError("actual file is longer"), e);
171             return;
172         }
173         fail();
174     }
175
176     public void testFailBinaryFileShorter()
177             throws Exception JavaDoc {
178         File JavaDoc expected = new File JavaDoc(XMLPropertyManager.getProperty("expected.binary.file"));
179         File JavaDoc actual = new File JavaDoc(XMLPropertyManager.getProperty("short.binary.file"));
180
181         try {
182             FileAssert.assertBinaryEquals(expected, actual);
183         } catch (AssertionFailedError e) {
184             ThrowableAssert.assertSimilar(new AssertionFailedError("actual file is shorter"), e);
185             return;
186         }
187         fail();
188     }
189
190     public void testSucceedFileLastLine()
191             throws Exception JavaDoc {
192         File JavaDoc expected = new File JavaDoc(XMLPropertyManager.getProperty("expected-lastline.file"));
193         File JavaDoc actual = new File JavaDoc(XMLPropertyManager.getProperty("expected-lastline.file"));
194
195         FileAssert.assertEquals(expected, actual);
196     }
197
198     public void testFailFileLastLine() {
199         File JavaDoc expected = new File JavaDoc(XMLPropertyManager.getProperty("expected-lastline.file"));
200         File JavaDoc actual = new File JavaDoc(XMLPropertyManager.getProperty("differ-lastline.file"));
201
202         try {
203             FileAssert.assertEquals(expected, actual);
204         } catch (AssertionFailedError e) {
205             return;
206         }
207         fail();
208     }
209
210     public void create()
211             throws Exception JavaDoc {
212         File JavaDoc input = new File JavaDoc(XMLPropertyManager.getProperty("expected.binary.file"));
213
214         FileInputStream JavaDoc inputis = new FileInputStream JavaDoc(input);
215         byte[] buff = new byte[400];
216         inputis.read(buff, 0, 400);
217
218         FileOutputStream JavaDoc out;
219
220         out = new FileOutputStream JavaDoc(new File JavaDoc("expected.bin"));
221         out.write(buff, 0, 300);
222         out.close();
223
224         out = new FileOutputStream JavaDoc(new File JavaDoc("normal.bin"));
225         out.write(buff, 0, 300);
226         out.close();
227
228         out = new FileOutputStream JavaDoc(new File JavaDoc("long.bin"));
229         out.write(buff, 0, 400);
230         out.close();
231
232         out = new FileOutputStream JavaDoc(new File JavaDoc("short.bin"));
233         out.write(buff, 0, 200);
234         out.close();
235
236         out = new FileOutputStream JavaDoc(new File JavaDoc("differ.bin"));
237         buff[150] ^= (byte) 0xFF;
238         out.write(buff, 0, 300);
239         out.close();
240
241         inputis.close();
242     }
243
244 }
245
Popular Tags