001: package org.dbunit.util;
002:
003: import org.slf4j.Logger;
004: import org.slf4j.LoggerFactory;
005:
006: /*
007: * From "Dale E Martin" <dmartin@c..>
008: * Date Thursday, March 14, 2002 2:42 pm
009: * To junit@yahoogroups.com
010: * Subject [junit] file assert, a starting point
011: *
012: * OK, this isn't rocket science or anything but it's working for me. What it
013: * is is a couple of assert methods that compare files, so your expected
014: * results can be located in a file.
015: *
016: * I did not use the diff library as the docs are in French, which I have not
017: * studied for about 15 years and am not ready to pick back up ;-)
018: *
019: * I declare this code to be open to the public domain and anyone may do
020: * anything they like with it. Before inclusion in JUnit I'm sure you'll need
021: * to tweak it some, but it's working for my needs and maybe it will help
022: * someone else.
023: *
024: * Later,
025: * Dale
026: *
027: */
028:
029: import junit.framework.Assert;
030:
031: import java.io.*;
032:
033: public class FileAsserts {
034:
035: /**
036: * Logger for this class
037: */
038: private static final Logger logger = LoggerFactory
039: .getLogger(FileAsserts.class);
040:
041: private static String processOneLine(int lineNumber,
042: BufferedReader expectedData, BufferedReader actualData)
043: throws IOException {
044: logger.debug("processOneLine(lineNumber=" + lineNumber
045: + ", expectedData=" + expectedData + ", actualData="
046: + actualData + ") - start");
047:
048: String problem = null;
049: String expectedLine = expectedData.readLine();
050: if (!actualData.ready()) {
051: problem = "at line "
052: + lineNumber
053: + ", expected:\n"
054: + expectedLine
055: + "\n"
056: + "but actual file was not ready for reading at this line.";
057: } else {
058: String actualLine = actualData.readLine();
059: if (!expectedLine.equals(actualLine)) {
060: // Uh oh, they did not match.
061: problem = "at line " + lineNumber
062: + " there was a mismatch. Expected:\n";
063: int maxLen = expectedLine.length();
064: if (expectedLine.length() > actualLine.length()) {
065: maxLen = actualLine.length();
066: }
067: int startOffset = 0;
068: for (int i = 0; i < maxLen; i++) {
069: if (expectedLine.charAt(i) != actualLine.charAt(i)) {
070: startOffset = i;
071: break;
072: }
073: }
074: problem += expectedLine.substring(startOffset) + "\n"
075: + "actual was:\n"
076: + actualLine.substring(startOffset) + "\n";
077: }
078: }
079: return problem;
080: }
081:
082: public static void assertEquals(BufferedReader expected,
083: BufferedReader actual) throws Exception {
084: logger.debug("assertEquals(expected=" + expected + ", actual="
085: + actual + ") - start");
086:
087: Assert.assertNotNull(expected);
088: Assert.assertNotNull(actual);
089:
090: String problem = null;
091: try {
092: int lineCounter = 0;
093: while (expected.ready() && problem == null) {
094: problem = processOneLine(lineCounter, expected, actual);
095: lineCounter++;
096: }
097: } finally {
098: expected.close();
099: actual.close();
100: }
101:
102: if (problem != null) {
103: Assert.fail(problem);
104: }
105: }
106:
107: public static void assertEquals(InputStream expected, File actual)
108: throws Exception {
109: logger.debug("assertEquals(expected=" + expected + ", actual="
110: + actual + ") - start");
111:
112: Assert.assertNotNull(expected);
113: Assert.assertNotNull(actual);
114:
115: Assert.assertTrue(actual.canRead());
116:
117: BufferedReader expectedData = new BufferedReader(
118: new InputStreamReader(expected));
119:
120: BufferedReader actualData = new BufferedReader(
121: new InputStreamReader(new FileInputStream(actual)));
122: assertEquals(expectedData, actualData);
123: }
124:
125: public static void assertEquals(File expected, File actual)
126: throws Exception {
127: logger.debug("assertEquals(expected=" + expected + ", actual="
128: + actual + ") - start");
129:
130: Assert.assertNotNull(expected);
131: Assert.assertNotNull(actual);
132:
133: Assert.assertTrue(expected.canRead());
134: Assert.assertTrue(actual.canRead());
135:
136: BufferedReader expectedData = new BufferedReader(
137: new InputStreamReader(new FileInputStream(expected)));
138: BufferedReader actualData = new BufferedReader(
139: new InputStreamReader(new FileInputStream(actual)));
140: assertEquals(expectedData, actualData);
141: }
142: }
|