KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > junitx > framework > FileAssert


1 /*
2  * The JUnit-addons Software License, Version 1.0
3  * (based on the Apache Software License, Version 1.1)
4  *
5  * Copyright (c) 2002-2003 Vladimir R. Bossicard. All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * 1. Redistributions of source code must retain the above copyright
12  * notice, this list of conditions and the following disclaimer.
13  *
14  * 2. Redistributions in binary form must reproduce the above copyright
15  * notice, this list of conditions and the following disclaimer in
16  * the documentation and/or other materials provided with the
17  * distribution.
18  *
19  * 3. The end-user documentation included with the redistribution, if
20  * any, must include the following acknowlegement:
21  * "This product includes software developed by Vladimir R.
22  * Bossicard as well as other contributors
23  * (http://junit-addons.sourceforge.net/)."
24  * Alternately, this acknowlegement may appear in the software itself,
25  * if and wherever such third-party acknowlegements normally appear.
26  *
27  * 4. The name "JUnit-addons" must not be used to endorse or promote
28  * products derived from this software without prior written
29  * permission. For written permission, please contact
30  * vbossica@users.sourceforge.net.
31  *
32  * 5. Products derived from this software may not be called "JUnit-addons"
33  * nor may "JUnit-addons" appear in their names without prior written
34  * permission of the project managers.
35  *
36  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
37  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
38  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
39  * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
40  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
41  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
42  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
43  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
44  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
45  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
46  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
47  * SUCH DAMAGE.
48  * ======================================================================
49  *
50  * This software consists of voluntary contributions made by many
51  * individuals. For more information on the JUnit-addons Project, please
52  * see <http://junit-addons.sourceforge.net/>.
53  */

54
55 package junitx.framework;
56
57 import java.io.BufferedReader JavaDoc;
58 import java.io.File JavaDoc;
59 import java.io.FileInputStream JavaDoc;
60 import java.io.IOException JavaDoc;
61 import java.io.InputStreamReader JavaDoc;
62 import java.io.LineNumberReader JavaDoc;
63 import java.io.Reader JavaDoc;
64
65 /**
66  * A set of assert methods specially targetted to asserting files.
67  *
68  * @version $Revision: 1.8 $ $Date: 2003/04/28 02:47:32 $
69  * @author <a HREF="mailto:vbossica@users.sourceforge.net">Vladimir R. Bossicard</a>
70  * @author <a HREF="mailto:dchalker@users.sourceforge.net">Dean Chalker</a>
71  */

72 public class FileAssert {
73
74     /**
75      * Don't let anyone have access to this constructor.
76      */

77     private FileAssert() {
78     }
79
80     /**
81      * Asserts that two files are equal. Throws an
82      * <tt>AssertionFailedError</tt> if they are not.<p>
83      *
84      * <b>Note</b>: This assertion method rely on the standard
85      * <tt>junit.framework.Assert(String expected, String actual)</tt> method
86      * to compare the lines of the files. JUnit > 3.8 provides a nicer way to
87      * display differences between two strings but since only lines are
88      * compared (and not entire paragraphs) you can still use JUnit 3.7.
89      */

90     public static void assertEquals(String JavaDoc message,
91                                     File JavaDoc expected,
92                                     File JavaDoc actual) {
93         Assert.assertNotNull(expected);
94         Assert.assertNotNull(actual);
95
96         Assert.assertTrue("File does not exist [" + expected.getAbsolutePath() + "]", expected.exists());
97         Assert.assertTrue("File does not exist [" + actual.getAbsolutePath() + "]", actual.exists());
98
99         Assert.assertTrue("Expected file not readable", expected.canRead());
100         Assert.assertTrue("Actual file not readable", actual.canRead());
101
102         FileInputStream JavaDoc eis = null;
103         FileInputStream JavaDoc ais = null;
104
105         try {
106             try {
107                 eis = new FileInputStream JavaDoc(expected);
108                 ais = new FileInputStream JavaDoc(actual);
109     
110                 BufferedReader JavaDoc expData = new BufferedReader JavaDoc(new InputStreamReader JavaDoc(eis));
111                 BufferedReader JavaDoc actData = new BufferedReader JavaDoc(new InputStreamReader JavaDoc(ais));
112     
113                 Assert.assertNotNull(message, expData);
114                 Assert.assertNotNull(message, actData);
115     
116                 assertEquals(message, expData, actData);
117             } finally {
118                 eis.close();
119                 ais.close();
120             }
121         } catch (IOException JavaDoc e) {
122             throw new AssertionFailedError(e);
123         }
124     }
125
126     /**
127      * Asserts that two files are equal. Throws an
128      * <tt>AssertionFailedError</tt> if they are not.
129      */

130     public static void assertEquals(File JavaDoc expected,
131                                     File JavaDoc actual) {
132         assertEquals(null, expected, actual);
133     }
134
135     /**
136      * <b>Testing only</b> Asserts that two readers are equal. Throws an
137      * <tt>AssertionFailedError</tt> if they are not.
138      */

139     protected static void assertEquals(String JavaDoc message,
140                                        Reader JavaDoc expected,
141                                        Reader JavaDoc actual) {
142         Assert.assertNotNull(message, expected);
143         Assert.assertNotNull(message, actual);
144
145         LineNumberReader JavaDoc expReader = new LineNumberReader JavaDoc(expected);
146         LineNumberReader JavaDoc actReader = new LineNumberReader JavaDoc(actual);
147
148         Assert.assertNotNull(message, expReader);
149         Assert.assertNotNull(message, actReader);
150
151         String JavaDoc formatted = "";
152         if (message != null) {
153             formatted = message + " ";
154         }
155
156         String JavaDoc expLine;
157         String JavaDoc actLine;
158         try {
159             while (true) {
160                 if (!expReader.ready() && !actReader.ready()) {
161                     return;
162                 }
163
164                 expLine = expReader.readLine();
165                 actLine = actReader.readLine();
166
167                 if (expLine == null && actLine == null) {
168                     return;
169                 }
170
171                 int line = expReader.getLineNumber() + 1;
172
173                 if (expReader.ready()) {
174                     if (actReader.ready()) {
175                         Assert.assertEquals(formatted + "Line [" + line + "]", expLine, actLine);
176                     } else {
177                         Assert.fail(formatted + "Line [" + line + "] expected <" + expLine + "> but was <EOF>");
178                     }
179                 } else {
180                     if (actReader.ready()) {
181                         Assert.fail(formatted + "Line [" + line + "] expected <EOF> but was <" + actLine + ">");
182                     } else {
183                         Assert.assertEquals(formatted + "Line [" + line + "]", expLine, actLine);
184                     }
185                 }
186             }
187         } catch (IOException JavaDoc e) {
188             throw new AssertionFailedError(e);
189         }
190     }
191
192     /**
193      * Asserts that two binary files are equal. Throws an
194      * <tt>AssertionFailedError</tt> if they are not.<p>
195      */

196     public static void assertBinaryEquals(File JavaDoc expected,
197                                           File JavaDoc actual) {
198         assertBinaryEquals(null, expected, actual);
199     }
200
201     /**
202      * Asserts that two binary files are equal. Throws an
203      * <tt>AssertionFailedError</tt> if they are not.<p>
204      */

205     public static void assertBinaryEquals(String JavaDoc message,
206                                           File JavaDoc expected,
207                                           File JavaDoc actual) {
208         Assert.assertNotNull(message, expected);
209         Assert.assertNotNull(message, actual);
210
211         Assert.assertTrue("File does not exist [" + expected.getAbsolutePath() + "]", expected.exists());
212         Assert.assertTrue("File does not exist [" + actual.getAbsolutePath() + "]", actual.exists());
213
214         Assert.assertTrue("Expected file not readable", expected.canRead());
215         Assert.assertTrue("Actual file not readable", actual.canRead());
216
217         FileInputStream JavaDoc eis = null;
218         FileInputStream JavaDoc ais = null;
219
220         try {
221             try {
222                 eis = new FileInputStream JavaDoc(expected);
223                 ais = new FileInputStream JavaDoc(actual);
224     
225                 Assert.assertNotNull(message, expected);
226                 Assert.assertNotNull(message, actual);
227     
228                 byte[] expBuff = new byte[8192];
229                 byte[] actBuff = new byte[8192];
230     
231                 long pos = 0;
232                 while (true) {
233                     int expLength = eis.read(expBuff, 0, 8192);
234                     int actLength = ais.read(actBuff, 0, 8192);
235     
236                     if (expLength < actLength) {
237                         Assert.fail("actual file is longer");
238                     }
239                     if (expLength > actLength) {
240                         Assert.fail("actual file is shorter");
241                     }
242     
243                     if (expLength == 0) {
244                         return;
245                     }
246     
247                     for (int i = 0; i < expBuff.length; ++i) {
248                         if (expBuff[i] != actBuff[i]) {
249                             String JavaDoc formatted = "";
250                             if (message != null) {
251                                 formatted = message + " ";
252                             }
253                             Assert.fail(formatted + "files differ at byte " + (pos + i + 1)); // i starts at 0 so +1
254
}
255                     }
256     
257                     pos += expBuff.length;
258                     return;
259                 }
260             } finally {
261                 eis.close();
262                 ais.close();
263             }
264         } catch (IOException JavaDoc e) {
265             throw new AssertionFailedError(e);
266         }
267     }
268
269 }
270
Popular Tags