View Javadoc
1   /*
2    *
3    * The DbUnit Database Testing Framework
4    * Copyright (C)2002-2004, DbUnit.org
5    *
6    * This library is free software; you can redistribute it and/or
7    * modify it under the terms of the GNU Lesser General Public
8    * License as published by the Free Software Foundation; either
9    * version 2.1 of the License, or (at your option) any later version.
10   *
11   * This library is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14   * Lesser General Public License for more details.
15   *
16   * You should have received a copy of the GNU Lesser General Public
17   * License along with this library; if not, write to the Free Software
18   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19   *
20   */
21  package org.dbunit.testutil;
22  
23  import java.io.BufferedReader;
24  import java.io.File;
25  import java.io.FileInputStream;
26  import java.io.IOException;
27  import java.io.InputStream;
28  import java.io.InputStreamReader;
29  
30  import org.dbunit.assertion.DefaultFailureHandler;
31  import org.dbunit.assertion.JUnitFailureFactory;
32  import org.dbunit.assertion.SimpleAssert;
33  import org.slf4j.Logger;
34  import org.slf4j.LoggerFactory;
35  
36  /**
37   * Simple utility to compare file or stream data with each other.
38   * 
39   * <p>
40   * From  "Dale E Martin" dmartin@c..
41   * Date  Thursday, March 14, 2002 2:42 pm
42   * To  junit@yahoogroups.com
43   * Subject  [junit] file assert, a starting point
44   *
45   * OK, this isn't rocket science or anything but it's working for me.  What it
46   * is is a couple of assert methods that compare files, so your expected
47   * results can be located in a file.
48   *
49   * I did not use the diff library as the docs are in French, which I have not
50   * studied for about 15 years and am not ready to pick back up ;-)
51   *
52   * I declare this code to be open to the public domain and anyone may do
53   * anything they like with it.  Before inclusion in JUnit I'm sure you'll need
54   * to tweak it some, but it's working for my needs and maybe it will help
55   * someone else.
56   *
57   * Later,
58   *       Dale
59   * </p>
60   * 
61   * 
62   * @author Dale E Martin
63   * @author Last changed by: $Author$
64   * @version $Revision$ $Date$
65   * @since ? (pre 2.2)
66   */
67  public class FileAsserts
68  {
69      private static final DefaultFailureHandler FAILURE_HANDLER = new DefaultFailureHandler();
70      static{
71          FAILURE_HANDLER.setFailureFactory(new JUnitFailureFactory());
72      }
73      private static final SimpleAssert ASSERT = new SimpleAssert(FAILURE_HANDLER);
74  
75      /**
76       * Logger for this class
77       */
78      private static final Logger logger = LoggerFactory.getLogger(FileAsserts.class);
79  
80      private static String processOneLine(int lineNumber,
81              BufferedReader expectedData,
82              BufferedReader actualData)
83              throws IOException
84      {
85      	if(logger.isDebugEnabled())
86      		logger.debug("processOneLine(lineNumber={}, expectedData={}, actualData={}) - start", 
87      				new Object[] {new Integer(lineNumber), expectedData, actualData} );
88  
89          String problem = null;
90          String expectedLine = expectedData.readLine();
91          if (!actualData.ready())
92          {
93              problem = "at line " + lineNumber + ", expected:\n" +
94                      expectedLine + "\n" +
95                      "but actual file was not ready for reading at this line.";
96          }
97          else
98          {
99              String actualLine = actualData.readLine();
100             if (!expectedLine.equals(actualLine))
101             {
102                 // Uh oh, they did not match.
103                 problem = "at line " + lineNumber + " there was a mismatch.  Expected:\n";
104                 int maxLen = expectedLine.length();
105                 if (expectedLine.length() > actualLine.length())
106                 {
107                     maxLen = actualLine.length();
108                 }
109                 int startOffset = 0;
110                 for (int i = 0; i < maxLen; i++)
111                 {
112                     if (expectedLine.charAt(i) != actualLine.charAt(i))
113                     {
114                         startOffset = i;
115                         break;
116                     }
117                 }
118                 problem += expectedLine.substring(startOffset) + "\n" +
119                         "actual was:\n" +
120                         actualLine.substring(startOffset) + "\n";
121             }
122         }
123         return problem;
124     }
125 
126     public static void assertEquals(BufferedReader expected,
127             BufferedReader actual) throws Exception
128     {
129         logger.debug("assertEquals(expected={}, actual={}) - start", expected, actual);
130 
131         ASSERT.assertNotNull(expected);
132         ASSERT.assertNotNull(actual);
133 
134         String problem = null;
135         try
136         {
137             int lineCounter = 0;
138             while (expected.ready() && problem == null)
139             {
140                 problem = processOneLine(lineCounter, expected, actual);
141                 lineCounter++;
142             }
143         }
144         finally
145         {
146             expected.close();
147             actual.close();
148         }
149 
150         if (problem != null)
151         {
152             ASSERT.fail(problem);
153         }
154     }
155 
156     public static void assertEquals(InputStream expected, File actual)
157             throws Exception
158     {
159         logger.debug("assertEquals(expected={}, actual={}) - start", expected, actual);
160 
161         ASSERT.assertNotNull(expected);
162         ASSERT.assertNotNull(actual);
163 
164         ASSERT.assertTrue(actual.canRead());
165 
166         
167         BufferedReader expectedData = new BufferedReader(new InputStreamReader(expected));
168 
169         BufferedReader actualData =
170                 new BufferedReader(new InputStreamReader(new FileInputStream(actual)));
171         assertEquals(expectedData, actualData);
172     }
173 
174     public static void assertEquals(File expected, File actual) throws Exception
175     {
176         logger.debug("assertEquals(expected={}, actual={}) - start", expected, actual);
177 
178         ASSERT.assertNotNull(expected);
179         ASSERT.assertNotNull(actual);
180 
181         ASSERT.assertTrue(expected.canRead());
182         ASSERT.assertTrue(actual.canRead());
183 
184         BufferedReader expectedData =
185                 new BufferedReader(new InputStreamReader(new FileInputStream(expected)));
186         BufferedReader actualData =
187                 new BufferedReader(new InputStreamReader(new FileInputStream(actual)));
188         assertEquals(expectedData, actualData);
189     }
190 }
191 
192 
193