1 package junitx.framework; 2 3 import junit.framework.AssertionFailedError; 4 import junit.framework.TestCase; 5 import junitx.util.XMLPropertyManager; 6 7 import java.io.File ; 8 import java.io.FileInputStream ; 9 import java.io.FileOutputStream ; 10 import java.io.StringReader ; 11 12 16 public class FileAssertTest 17 extends TestCase { 18 19 public FileAssertTest(String name) { 20 super(name); 21 } 22 23 public void testSucceed() 24 throws Exception { 25 String input = "line1\n" + 26 "line2\n" + 27 "line3\n" + 28 "line4\n" + 29 "line5\n"; 30 FileAssert.assertEquals(null, new StringReader (input), new StringReader (input)); 31 } 32 33 public void testFailDiffer() 34 throws Exception { 35 String expected = "line1\n" + 36 "line2\n" + 37 "line3\n" + 38 "line4\n" + 39 "line5\n"; 40 String actual = "line1\n" + 41 "line2\n" + 42 "line10\n" + 43 "line4\n" + 44 "line5\n"; 45 try { 46 FileAssert.assertEquals(null, new StringReader (expected), new StringReader (actual)); 47 } catch (AssertionFailedError e) { 48 return; 49 } 50 fail(); 51 } 52 53 public void testFailLonger() 54 throws Exception { 55 String expected = "line1\n" + 56 "line2\n" + 57 "line3\n" + 58 "line4\n" + 59 "line5\n"; 60 String actual = "line1\n" + 61 "line2\n" + 62 "line3\n" + 63 "line4\n" + 64 "line5\n" + 65 "line6\n"; 66 try { 67 FileAssert.assertEquals(null, new StringReader (expected), new StringReader (actual)); 68 } catch (AssertionFailedError e) { 69 return; 70 } 71 fail(); 72 } 73 74 public void testFailShorter() 75 throws Exception { 76 String expected = "line1\n" + 77 "line2\n" + 78 "line3\n" + 79 "line4\n" + 80 "line5\n"; 81 String actual = "line1\n" + 82 "line2\n" + 83 "line3\n" + 84 "line4\n"; 85 try { 86 FileAssert.assertEquals(null, new StringReader (expected), new StringReader (actual)); 87 } catch (AssertionFailedError e) { 88 return; 89 } 90 fail(); 91 } 92 93 public void testSucceedFile() 94 throws Exception { 95 File expected = new File (XMLPropertyManager.getProperty("expected.file")); 96 File actual = new File (XMLPropertyManager.getProperty("normal.file")); 97 98 FileAssert.assertEquals(expected, actual); 99 } 100 101 public void testFailFileDiffer() 102 throws Exception { 103 File expected = new File (XMLPropertyManager.getProperty("expected.file")); 104 File actual = new File (XMLPropertyManager.getProperty("differ.file")); 105 106 try { 107 FileAssert.assertEquals(expected, actual); 108 } catch (AssertionFailedError e) { 109 return; 110 } 111 fail(); 112 } 113 114 public void testFailFileLonger() 115 throws Exception { 116 File expected = new File (XMLPropertyManager.getProperty("expected.file")); 117 File actual = new File (XMLPropertyManager.getProperty("long.file")); 118 119 try { 120 FileAssert.assertEquals(expected, actual); 121 } catch (AssertionFailedError e) { 122 return; 123 } 124 fail(); 125 } 126 127 public void testFailFileShorter() 128 throws Exception { 129 File expected = new File (XMLPropertyManager.getProperty("expected.file")); 130 File actual = new File (XMLPropertyManager.getProperty("short.file")); 131 132 try { 133 FileAssert.assertEquals(expected, actual); 134 } catch (AssertionFailedError e) { 135 return; 136 } 137 fail(); 138 } 139 140 public void testSucceedBinaryFile() 141 throws Exception { 142 File expected = new File (XMLPropertyManager.getProperty("expected.binary.file")); 143 File actual = new File (XMLPropertyManager.getProperty("normal.binary.file")); 144 145 FileAssert.assertBinaryEquals(expected, actual); 146 } 147 148 public void testFailBinaryFileDiffer() 149 throws Exception { 150 File expected = new File (XMLPropertyManager.getProperty("expected.binary.file")); 151 File actual = new File (XMLPropertyManager.getProperty("differ.binary.file")); 152 153 try { 154 FileAssert.assertBinaryEquals(expected, actual); 155 } catch (AssertionFailedError e) { 156 ThrowableAssert.assertSimilar(new AssertionFailedError("files differ at byte 151"), e); 157 return; 158 } 159 fail(); 160 } 161 162 public void testFailBinaryFileLonger() 163 throws Exception { 164 File expected = new File (XMLPropertyManager.getProperty("expected.binary.file")); 165 File actual = new File (XMLPropertyManager.getProperty("long.binary.file")); 166 167 try { 168 FileAssert.assertBinaryEquals(expected, actual); 169 } catch (AssertionFailedError e) { 170 ThrowableAssert.assertSimilar(new AssertionFailedError("actual file is longer"), e); 171 return; 172 } 173 fail(); 174 } 175 176 public void testFailBinaryFileShorter() 177 throws Exception { 178 File expected = new File (XMLPropertyManager.getProperty("expected.binary.file")); 179 File actual = new File (XMLPropertyManager.getProperty("short.binary.file")); 180 181 try { 182 FileAssert.assertBinaryEquals(expected, actual); 183 } catch (AssertionFailedError e) { 184 ThrowableAssert.assertSimilar(new AssertionFailedError("actual file is shorter"), e); 185 return; 186 } 187 fail(); 188 } 189 190 public void testSucceedFileLastLine() 191 throws Exception { 192 File expected = new File (XMLPropertyManager.getProperty("expected-lastline.file")); 193 File actual = new File (XMLPropertyManager.getProperty("expected-lastline.file")); 194 195 FileAssert.assertEquals(expected, actual); 196 } 197 198 public void testFailFileLastLine() { 199 File expected = new File (XMLPropertyManager.getProperty("expected-lastline.file")); 200 File actual = new File (XMLPropertyManager.getProperty("differ-lastline.file")); 201 202 try { 203 FileAssert.assertEquals(expected, actual); 204 } catch (AssertionFailedError e) { 205 return; 206 } 207 fail(); 208 } 209 210 public void create() 211 throws Exception { 212 File input = new File (XMLPropertyManager.getProperty("expected.binary.file")); 213 214 FileInputStream inputis = new FileInputStream (input); 215 byte[] buff = new byte[400]; 216 inputis.read(buff, 0, 400); 217 218 FileOutputStream out; 219 220 out = new FileOutputStream (new File ("expected.bin")); 221 out.write(buff, 0, 300); 222 out.close(); 223 224 out = new FileOutputStream (new File ("normal.bin")); 225 out.write(buff, 0, 300); 226 out.close(); 227 228 out = new FileOutputStream (new File ("long.bin")); 229 out.write(buff, 0, 400); 230 out.close(); 231 232 out = new FileOutputStream (new File ("short.bin")); 233 out.write(buff, 0, 200); 234 out.close(); 235 236 out = new FileOutputStream (new File ("differ.bin")); 237 buff[150] ^= (byte) 0xFF; 238 out.write(buff, 0, 300); 239 out.close(); 240 241 inputis.close(); 242 } 243 244 } 245 | Popular Tags |