KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > dbunit > util > FileAsserts


1 package org.dbunit.util;
2
3 /*
4  * From "Dale E Martin" <dmartin@c..>
5  * Date Thursday, March 14, 2002 2:42 pm
6  * To junit@yahoogroups.com
7  * Subject [junit] file assert, a starting point
8  *
9  * OK, this isn't rocket science or anything but it's working for me. What it
10  * is is a couple of assert methods that compare files, so your expected
11  * results can be located in a file.
12  *
13  * I did not use the diff library as the docs are in French, which I have not
14  * studied for about 15 years and am not ready to pick back up ;-)
15  *
16  * I declare this code to be open to the public domain and anyone may do
17  * anything they like with it. Before inclusion in JUnit I'm sure you'll need
18  * to tweak it some, but it's working for my needs and maybe it will help
19  * someone else.
20  *
21  * Later,
22  * Dale
23  *
24  */

25
26 import junit.framework.Assert;
27
28 import java.io.*;
29
30 public class FileAsserts
31 {
32
33     private static String JavaDoc processOneLine(int lineNumber,
34             BufferedReader expectedData,
35             BufferedReader actualData)
36             throws IOException
37     {
38
39         String JavaDoc problem = null;
40         String JavaDoc expectedLine = expectedData.readLine();
41         if (!actualData.ready())
42         {
43             problem = "at line " + lineNumber + ", expected:\n" +
44                     expectedLine + "\n" +
45                     "but actual file was not ready for reading at this line.";
46         }
47         else
48         {
49             String JavaDoc actualLine = actualData.readLine();
50             if (!expectedLine.equals(actualLine))
51             {
52                 // Uh oh, they did not match.
53
problem = "at line " + lineNumber + " there was a mismatch. Expected:\n";
54                 int maxLen = expectedLine.length();
55                 if (expectedLine.length() > actualLine.length())
56                 {
57                     maxLen = actualLine.length();
58                 }
59                 int startOffset = 0;
60                 for (int i = 0; i < maxLen; i++)
61                 {
62                     if (expectedLine.charAt(i) != actualLine.charAt(i))
63                     {
64                         startOffset = i;
65                         break;
66                     }
67                 }
68                 problem += expectedLine.substring(startOffset) + "\n" +
69                         "actual was:\n" +
70                         actualLine.substring(startOffset) + "\n";
71             }
72         }
73         return problem;
74     }
75
76     public static void assertEquals(BufferedReader expected,
77             BufferedReader actual) throws Exception JavaDoc
78     {
79         Assert.assertNotNull(expected);
80         Assert.assertNotNull(actual);
81
82         String JavaDoc problem = null;
83         try
84         {
85             int lineCounter = 0;
86             while (expected.ready() && problem == null)
87             {
88                 problem = processOneLine(lineCounter, expected, actual);
89                 lineCounter++;
90             }
91         }
92         finally
93         {
94             expected.close();
95             actual.close();
96         }
97
98         if (problem != null)
99         {
100             Assert.fail(problem);
101         }
102     }
103
104     public static void assertEquals(InputStream expected, File actual)
105             throws Exception JavaDoc
106     {
107         Assert.assertNotNull(expected);
108         Assert.assertNotNull(actual);
109
110         Assert.assertTrue(actual.canRead());
111
112         
113         BufferedReader expectedData = new BufferedReader(new InputStreamReader(expected));
114
115         BufferedReader actualData =
116                 new BufferedReader(new InputStreamReader(new FileInputStream(actual)));
117         assertEquals(expectedData, actualData);
118     }
119
120     public static void assertEquals(File expected, File actual) throws Exception JavaDoc
121     {
122         Assert.assertNotNull(expected);
123         Assert.assertNotNull(actual);
124
125         Assert.assertTrue(expected.canRead());
126         Assert.assertTrue(actual.canRead());
127
128         BufferedReader expectedData =
129                 new BufferedReader(new InputStreamReader(new FileInputStream(expected)));
130         BufferedReader actualData =
131                 new BufferedReader(new InputStreamReader(new FileInputStream(actual)));
132         assertEquals(expectedData, actualData);
133     }
134 }
135
136
137
138
Popular Tags