| 1 | | | |
| 2 | | | |
| 3 | | | |
| 4 | | | |
| 5 | | | |
| 6 | | | |
| 7 | | | |
| 8 | | | |
| 9 | | | |
| 10 | | | |
| 11 | | | |
| 12 | | | |
| 13 | | | |
| 14 | | | |
| 15 | | | |
| 16 | | | |
| 17 | | | |
| 18 | | | |
| 19 | | | |
| 20 | | | |
| 21 | | | |
| 22 | | | |
| 23 | | | |
| 24 | | | |
| 25 | | | |
| 26 | | | |
| 27 | | | |
| 28 | | | |
| 29 | | | |
| 30 | | | |
| 31 | | | |
| 32 | | | |
| 33 | | | package org.jcoderz.commons.util; |
| 34 | | | |
| 35 | | | import java.io.File; |
| 36 | | | import java.io.FileInputStream; |
| 37 | | | import java.io.FileNotFoundException; |
| 38 | | | import java.io.FileOutputStream; |
| 39 | | | import java.io.IOException; |
| 40 | | | import java.io.InputStream; |
| 41 | | | |
| 42 | | | import junit.framework.TestCase; |
| 43 | | | |
| 44 | | | |
| 45 | | | |
| 46 | | | |
| 47 | | | @author |
| 48 | | | |
| 49 | 100 | | public class LazyFileOutputStreamTest |
| 50 | | | extends TestCase |
| 51 | | | { |
| 52 | | (1) | public void testNewFile () |
| 53 | | | throws IOException |
| 54 | | | { |
| 55 | 100 | (2)(3) | final File file = File.createTempFile("test", "new"); |
| 56 | | | try |
| 57 | | | { |
| 58 | 100 | (4) | final LazyFileOutputStream out = new LazyFileOutputStream(file); |
| 59 | 100 | | out.write(1); |
| 60 | 100 | | assertFalse("New stream should never be buffered.", |
| 61 | | | out.isBuffered()); |
| 62 | 100 | | out.close(); |
| 63 | 100 | | assertFalse("New stream should not be buffered after close.", |
| 64 | | | out.isBuffered()); |
| 65 | 100 | | assertContent(file, new byte[] {1}); |
| 66 | 100 | (5) | assertTrue("Could not delete temp '" + file + "'.", |
| 67 | | | file.delete()); |
| 68 | | | } |
| 69 | | | finally |
| 70 | | | { |
| 71 | 50 | (6) | file.delete(); |
| 72 | 100 | | } |
| 73 | 100 | | } |
| 74 | | | |
| 75 | | (7) | public void testShorterFile () |
| 76 | | | throws IOException |
| 77 | | | { |
| 78 | 100 | | final File file = File.createTempFile("test", "new"); |
| 79 | | | try |
| 80 | | | { |
| 81 | 100 | | fillTestData(file); |
| 82 | 100 | (8) | final LazyFileOutputStream out = new LazyFileOutputStream(file); |
| 83 | 100 | | out.write(0); |
| 84 | 100 | (9) | assertTrue("Same data should be buffered.", |
| 85 | | | out.isBuffered()); |
| 86 | 100 | | out.close(); |
| 87 | 100 | | assertFalse("Shorter new stream must not be buffered.", |
| 88 | | | out.isBuffered()); |
| 89 | 100 | (10) | assertContent(file, new byte[] { 0 }); |
| 90 | 100 | | assertTrue("Could not delete temp '" + file + "'.", |
| 91 | | | file.delete()); |
| 92 | | | } |
| 93 | | | finally |
| 94 | | | { |
| 95 | 50 | (11) | file.delete(); |
| 96 | 100 | | } |
| 97 | 100 | | } |
| 98 | | | |
| 99 | | (12) | public void testLongerFile () |
| 100 | | | throws IOException |
| 101 | | | { |
| 102 | 100 | | final File file = File.createTempFile("test", "new"); |
| 103 | | | try |
| 104 | | | { |
| 105 | 100 | | fillTestData(file); |
| 106 | | | |
| 107 | 100 | (13) | final LazyFileOutputStream out = new LazyFileOutputStream(file); |
| 108 | 100 | | out.write(0); |
| 109 | 100 | | out.write(1); |
| 110 | 100 | | out.write(0); |
| 111 | 100 | | assertTrue("Same data should be buffered.", |
| 112 | | | out.isBuffered()); |
| 113 | 100 | | out.write(0); |
| 114 | 100 | | assertFalse("Longer new stream must not be buffered.", |
| 115 | | | out.isBuffered()); |
| 116 | 100 | | assertContent(file, new byte[] {0, 1, 0, 0}); |
| 117 | 100 | | out.close(); |
| 118 | 100 | | assertFalse("Longer new stream must not be buffered.", |
| 119 | | | out.isBuffered()); |
| 120 | 100 | | assertTrue("Could not delete temp '" + file + "'.", |
| 121 | | | file.delete()); |
| 122 | | | } |
| 123 | | | finally |
| 124 | | | { |
| 125 | 50 | (14) | file.delete(); |
| 126 | 100 | | } |
| 127 | 100 | | } |
| 128 | | | |
| 129 | | (15) | public void testSameFile () |
| 130 | | | throws IOException |
| 131 | | | { |
| 132 | 100 | (16) | File file = File.createTempFile("test", "new"); |
| 133 | | | try |
| 134 | | | { |
| 135 | 100 | | fillTestData(file); |
| 136 | | | |
| 137 | 100 | (17) | final LazyFileOutputStream out = new LazyFileOutputStream(file); |
| 138 | 100 | | out.write(0); |
| 139 | 100 | | out.write(1); |
| 140 | 100 | | out.write(0); |
| 141 | 100 | | assertTrue("Same data should be buffered.", |
| 142 | | | out.isBuffered()); |
| 143 | 100 | | out.close(); |
| 144 | 100 | | assertTrue("Same data should be buffered.", |
| 145 | | | out.isBuffered()); |
| 146 | 100 | | assertContent(file, new byte[] {0, 1, 0}); |
| 147 | 100 | | assertTrue("Could not delete temp '" + file + "'.", |
| 148 | | | file.delete()); |
| 149 | | | } |
| 150 | | | finally |
| 151 | | | { |
| 152 | 50 | (18) | file.delete(); |
| 153 | 100 | | } |
| 154 | 100 | | } |
| 155 | | | |
| 156 | | (19) | public void testDifferentFile () |
| 157 | | | throws IOException |
| 158 | | | { |
| 159 | 100 | (20) | File file = File.createTempFile("test", "new"); |
| 160 | | | try |
| 161 | | | { |
| 162 | 100 | | fillTestData(file); |
| 163 | | | |
| 164 | 100 | (21) | final LazyFileOutputStream out = new LazyFileOutputStream(file); |
| 165 | 100 | | out.write(0); |
| 166 | 100 | | out.write(1); |
| 167 | 100 | | assertTrue("Same data should be buffered.", |
| 168 | | | out.isBuffered()); |
| 169 | 100 | | out.write(1); |
| 170 | 100 | | assertFalse("Different data should not be buffered.", |
| 171 | | | out.isBuffered()); |
| 172 | 100 | | out.close(); |
| 173 | 100 | | assertContent(file, new byte[] {0, 1, 1}); |
| 174 | 100 | | assertTrue("Could not delete temp '" + file + "'.", |
| 175 | | | file.delete()); |
| 176 | | | } |
| 177 | | | finally |
| 178 | | | { |
| 179 | 50 | (22) | file.delete(); |
| 180 | 100 | | } |
| 181 | 100 | | } |
| 182 | | | |
| 183 | | | private void fillTestData (File file) |
| 184 | | | throws FileNotFoundException, IOException |
| 185 | | | { |
| 186 | 100 | (23)(24) | final FileOutputStream fos = new FileOutputStream(file); |
| 187 | 100 | | fos.write(0); |
| 188 | 100 | | fos.write(1); |
| 189 | 100 | | fos.write(0); |
| 190 | 100 | | fos.close(); |
| 191 | 100 | | } |
| 192 | | | |
| 193 | | | private void assertContent (File file, byte[] bs) |
| 194 | | | throws IOException |
| 195 | | | { |
| 196 | 100 | | final InputStream is = new FileInputStream(file); |
| 197 | | | try |
| 198 | | | { |
| 199 | 100 | | for (int i = 0; i < bs.length; i++) |
| 200 | | | { |
| 201 | 100 | | final int c = is.read(); |
| 202 | 100 | | assertEquals("File content unexpected.", bs[i], c); |
| 203 | | | } |
| 204 | 100 | | assertEquals("Stream to long?", -1, is.read()); |
| 205 | | | } |
| 206 | | | finally |
| 207 | | | { |
| 208 | 50 | | IoUtil.close(is); |
| 209 | 100 | | } |
| 210 | 100 | | } |
| 211 | | | } |