1 package org.dbunit.util; 2 3 25 26 import junit.framework.Assert; 27 28 import java.io.*; 29 30 public class FileAsserts 31 { 32 33 private static String processOneLine(int lineNumber, 34 BufferedReader expectedData, 35 BufferedReader actualData) 36 throws IOException 37 { 38 39 String problem = null; 40 String 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 actualLine = actualData.readLine(); 50 if (!expectedLine.equals(actualLine)) 51 { 52 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 78 { 79 Assert.assertNotNull(expected); 80 Assert.assertNotNull(actual); 81 82 String 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 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 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 |