| 1 | /* |
|---|
| 2 | * $Id$ |
|---|
| 3 | * |
|---|
| 4 | * Copyright 2006, The jCoderZ.org Project. All rights reserved. |
|---|
| 5 | * |
|---|
| 6 | * Redistribution and use in source and binary forms, with or without |
|---|
| 7 | * modification, are permitted provided that the following conditions are |
|---|
| 8 | * met: |
|---|
| 9 | * |
|---|
| 10 | * * Redistributions of source code must retain the above copyright |
|---|
| 11 | * notice, this list of conditions and the following disclaimer. |
|---|
| 12 | * * Redistributions in binary form must reproduce the above |
|---|
| 13 | * copyright notice, this list of conditions and the following |
|---|
| 14 | * disclaimer in the documentation and/or other materials |
|---|
| 15 | * provided with the distribution. |
|---|
| 16 | * * Neither the name of the jCoderZ.org Project nor the names of |
|---|
| 17 | * its contributors may be used to endorse or promote products |
|---|
| 18 | * derived from this software without specific prior written |
|---|
| 19 | * permission. |
|---|
| 20 | * |
|---|
| 21 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS "AS IS" AND |
|---|
| 22 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
|---|
| 23 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
|---|
| 24 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS |
|---|
| 25 | * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
|---|
| 26 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
|---|
| 27 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR |
|---|
| 28 | * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
|---|
| 29 | * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR |
|---|
| 30 | * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF |
|---|
| 31 | * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
|---|
| 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 | * Test LazyFileOutputStream class. |
|---|
| 46 | * |
|---|
| 47 | * @author Andreas Mandel |
|---|
| 48 | */ |
|---|
| 49 | public class LazyFileOutputStreamTest |
|---|
| 50 | extends TestCase |
|---|
| 51 | { |
|---|
| 52 | public void testNewFile () |
|---|
| 53 | throws IOException |
|---|
| 54 | { |
|---|
| 55 | final File file = File.createTempFile("test", "new"); |
|---|
| 56 | try |
|---|
| 57 | { |
|---|
| 58 | final LazyFileOutputStream out = new LazyFileOutputStream(file); |
|---|
| 59 | out.write(1); |
|---|
| 60 | assertFalse("New stream should never be buffered.", |
|---|
| 61 | out.isBuffered()); |
|---|
| 62 | out.close(); |
|---|
| 63 | assertFalse("New stream should not be buffered after close.", |
|---|
| 64 | out.isBuffered()); |
|---|
| 65 | assertContent(file, new byte[] {1}); |
|---|
| 66 | assertTrue("Could not delete temp '" + file + "'.", |
|---|
| 67 | file.delete()); |
|---|
| 68 | } |
|---|
| 69 | finally |
|---|
| 70 | { |
|---|
| 71 | file.delete(); |
|---|
| 72 | } |
|---|
| 73 | } |
|---|
| 74 | |
|---|
| 75 | public void testShorterFile () |
|---|
| 76 | throws IOException |
|---|
| 77 | { |
|---|
| 78 | final File file = File.createTempFile("test", "new"); |
|---|
| 79 | try |
|---|
| 80 | { |
|---|
| 81 | fillTestData(file); |
|---|
| 82 | final LazyFileOutputStream out = new LazyFileOutputStream(file); |
|---|
| 83 | out.write(0); |
|---|
| 84 | assertTrue("Same data should be buffered.", |
|---|
| 85 | out.isBuffered()); |
|---|
| 86 | out.close(); |
|---|
| 87 | assertFalse("Shorter new stream must not be buffered.", |
|---|
| 88 | out.isBuffered()); |
|---|
| 89 | assertContent(file, new byte[] { 0 }); |
|---|
| 90 | assertTrue("Could not delete temp '" + file + "'.", |
|---|
| 91 | file.delete()); |
|---|
| 92 | } |
|---|
| 93 | finally |
|---|
| 94 | { |
|---|
| 95 | file.delete(); |
|---|
| 96 | } |
|---|
| 97 | } |
|---|
| 98 | |
|---|
| 99 | public void testLongerFile () |
|---|
| 100 | throws IOException |
|---|
| 101 | { |
|---|
| 102 | final File file = File.createTempFile("test", "new"); |
|---|
| 103 | try |
|---|
| 104 | { |
|---|
| 105 | fillTestData(file); |
|---|
| 106 | |
|---|
| 107 | final LazyFileOutputStream out = new LazyFileOutputStream(file); |
|---|
| 108 | out.write(0); |
|---|
| 109 | out.write(1); |
|---|
| 110 | out.write(0); |
|---|
| 111 | assertTrue("Same data should be buffered.", |
|---|
| 112 | out.isBuffered()); |
|---|
| 113 | out.write(0); |
|---|
| 114 | assertFalse("Longer new stream must not be buffered.", |
|---|
| 115 | out.isBuffered()); |
|---|
| 116 | assertContent(file, new byte[] {0, 1, 0, 0}); |
|---|
| 117 | out.close(); |
|---|
| 118 | assertFalse("Longer new stream must not be buffered.", |
|---|
| 119 | out.isBuffered()); |
|---|
| 120 | assertTrue("Could not delete temp '" + file + "'.", |
|---|
| 121 | file.delete()); |
|---|
| 122 | } |
|---|
| 123 | finally |
|---|
| 124 | { |
|---|
| 125 | file.delete(); |
|---|
| 126 | } |
|---|
| 127 | } |
|---|
| 128 | |
|---|
| 129 | public void testSameFile () |
|---|
| 130 | throws IOException |
|---|
| 131 | { |
|---|
| 132 | File file = File.createTempFile("test", "new"); |
|---|
| 133 | try |
|---|
| 134 | { |
|---|
| 135 | fillTestData(file); |
|---|
| 136 | |
|---|
| 137 | final LazyFileOutputStream out = new LazyFileOutputStream(file); |
|---|
| 138 | out.write(0); |
|---|
| 139 | out.write(1); |
|---|
| 140 | out.write(0); |
|---|
| 141 | assertTrue("Same data should be buffered.", |
|---|
| 142 | out.isBuffered()); |
|---|
| 143 | out.close(); |
|---|
| 144 | assertTrue("Same data should be buffered.", |
|---|
| 145 | out.isBuffered()); |
|---|
| 146 | assertContent(file, new byte[] {0, 1, 0}); |
|---|
| 147 | assertTrue("Could not delete temp '" + file + "'.", |
|---|
| 148 | file.delete()); |
|---|
| 149 | } |
|---|
| 150 | finally |
|---|
| 151 | { |
|---|
| 152 | file.delete(); |
|---|
| 153 | } |
|---|
| 154 | } |
|---|
| 155 | |
|---|
| 156 | public void testDifferentFile () |
|---|
| 157 | throws IOException |
|---|
| 158 | { |
|---|
| 159 | File file = File.createTempFile("test", "new"); |
|---|
| 160 | try |
|---|
| 161 | { |
|---|
| 162 | fillTestData(file); |
|---|
| 163 | |
|---|
| 164 | final LazyFileOutputStream out = new LazyFileOutputStream(file); |
|---|
| 165 | out.write(0); |
|---|
| 166 | out.write(1); |
|---|
| 167 | assertTrue("Same data should be buffered.", |
|---|
| 168 | out.isBuffered()); |
|---|
| 169 | out.write(1); |
|---|
| 170 | assertFalse("Different data should not be buffered.", |
|---|
| 171 | out.isBuffered()); |
|---|
| 172 | out.close(); |
|---|
| 173 | assertContent(file, new byte[] {0, 1, 1}); |
|---|
| 174 | assertTrue("Could not delete temp '" + file + "'.", |
|---|
| 175 | file.delete()); |
|---|
| 176 | } |
|---|
| 177 | finally |
|---|
| 178 | { |
|---|
| 179 | file.delete(); |
|---|
| 180 | } |
|---|
| 181 | } |
|---|
| 182 | |
|---|
| 183 | private void fillTestData (File file) |
|---|
| 184 | throws FileNotFoundException, IOException |
|---|
| 185 | { |
|---|
| 186 | final FileOutputStream fos = new FileOutputStream(file); |
|---|
| 187 | fos.write(0); |
|---|
| 188 | fos.write(1); |
|---|
| 189 | fos.write(0); |
|---|
| 190 | fos.close(); |
|---|
| 191 | } |
|---|
| 192 | |
|---|
| 193 | private void assertContent (File file, byte[] bs) |
|---|
| 194 | throws IOException |
|---|
| 195 | { |
|---|
| 196 | final InputStream is = new FileInputStream(file); |
|---|
| 197 | try |
|---|
| 198 | { |
|---|
| 199 | for (int i = 0; i < bs.length; i++) |
|---|
| 200 | { |
|---|
| 201 | final int c = is.read(); |
|---|
| 202 | assertEquals("File content unexpected.", bs[i], c); |
|---|
| 203 | } |
|---|
| 204 | assertEquals("Stream to long?", -1, is.read()); |
|---|
| 205 | } |
|---|
| 206 | finally |
|---|
| 207 | { |
|---|
| 208 | IoUtil.close(is); |
|---|
| 209 | } |
|---|
| 210 | } |
|---|
| 211 | } |
|---|