1 54 55 package junitx.framework; 56 57 import java.io.BufferedReader ; 58 import java.io.File ; 59 import java.io.FileInputStream ; 60 import java.io.IOException ; 61 import java.io.InputStreamReader ; 62 import java.io.LineNumberReader ; 63 import java.io.Reader ; 64 65 72 public class FileAssert { 73 74 77 private FileAssert() { 78 } 79 80 90 public static void assertEquals(String message, 91 File expected, 92 File 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 eis = null; 103 FileInputStream ais = null; 104 105 try { 106 try { 107 eis = new FileInputStream (expected); 108 ais = new FileInputStream (actual); 109 110 BufferedReader expData = new BufferedReader (new InputStreamReader (eis)); 111 BufferedReader actData = new BufferedReader (new InputStreamReader (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 e) { 122 throw new AssertionFailedError(e); 123 } 124 } 125 126 130 public static void assertEquals(File expected, 131 File actual) { 132 assertEquals(null, expected, actual); 133 } 134 135 139 protected static void assertEquals(String message, 140 Reader expected, 141 Reader actual) { 142 Assert.assertNotNull(message, expected); 143 Assert.assertNotNull(message, actual); 144 145 LineNumberReader expReader = new LineNumberReader (expected); 146 LineNumberReader actReader = new LineNumberReader (actual); 147 148 Assert.assertNotNull(message, expReader); 149 Assert.assertNotNull(message, actReader); 150 151 String formatted = ""; 152 if (message != null) { 153 formatted = message + " "; 154 } 155 156 String expLine; 157 String 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 e) { 188 throw new AssertionFailedError(e); 189 } 190 } 191 192 196 public static void assertBinaryEquals(File expected, 197 File actual) { 198 assertBinaryEquals(null, expected, actual); 199 } 200 201 205 public static void assertBinaryEquals(String message, 206 File expected, 207 File 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 eis = null; 218 FileInputStream ais = null; 219 220 try { 221 try { 222 eis = new FileInputStream (expected); 223 ais = new FileInputStream (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 formatted = ""; 250 if (message != null) { 251 formatted = message + " "; 252 } 253 Assert.fail(formatted + "files differ at byte " + (pos + i + 1)); } 255 } 256 257 pos += expBuff.length; 258 return; 259 } 260 } finally { 261 eis.close(); 262 ais.close(); 263 } 264 } catch (IOException e) { 265 throw new AssertionFailedError(e); 266 } 267 } 268 269 } 270 | Popular Tags |