1 21 package net.sourceforge.cobertura.util; 22 23 import java.io.ByteArrayInputStream ; 24 import java.io.ByteArrayOutputStream ; 25 import java.io.File ; 26 import java.io.FileInputStream ; 27 import java.io.FileOutputStream ; 28 import java.io.IOException ; 29 import java.io.InputStream ; 30 import java.io.OutputStream ; 31 32 import junit.framework.TestCase; 33 34 37 public class IOUtilTest extends TestCase 38 { 39 40 private static final byte[] emptyByteArray = new byte[] {}; 41 42 private static final byte[] singletonByteArray = new byte[] { 7 }; 43 44 private static final byte[] smallByteArray = new byte[] { 1, 0, 2, -128, 45 127 }; 46 47 private File createFileWithData(byte[] data) throws IOException 48 { 49 File file = File.createTempFile("IOUtilTest", ".txt"); 50 file.deleteOnExit(); 51 OutputStream src = new FileOutputStream (file); 52 src.write(data); 53 src.close(); 54 return file; 55 } 56 57 public void testMoveFile() throws IOException 58 { 59 File srcFile = createFileWithData(smallByteArray); 61 File destFile = createFileWithData(emptyByteArray); 62 destFile.delete(); 63 assertTrue(!destFile.isFile()); 64 IOUtil.moveFile(srcFile, destFile); 65 assertTrue(destFile.isFile()); 66 InputStream in = new FileInputStream (destFile); 67 for (int i = 0; i < smallByteArray.length; i++) 68 assertEquals(smallByteArray[i], (byte)in.read()); 69 assertEquals(-1, in.read()); 70 in.close(); 71 72 srcFile = createFileWithData(singletonByteArray); 74 destFile = createFileWithData(smallByteArray); 75 IOUtil.moveFile(srcFile, destFile); 76 assertTrue(destFile.isFile()); 77 in = new FileInputStream (destFile); 78 for (int i = 0; i < singletonByteArray.length; i++) 79 assertEquals(singletonByteArray[i], (byte)in.read()); 80 assertEquals(-1, in.read()); 81 in.close(); 82 83 srcFile = createFileWithData(smallByteArray); 85 try 86 { 87 IOUtil.moveFile(srcFile, null); 88 fail("Expected NullPointerException"); 89 } 90 catch (NullPointerException ex) 91 { 92 } 93 94 destFile = createFileWithData(smallByteArray); 95 try 96 { 97 IOUtil.moveFile(null, destFile); 98 fail("Expected NullPointerException"); 99 } 100 catch (NullPointerException ex) 101 { 102 } 103 } 104 105 public void testCopyStream() throws IOException 106 { 107 ByteArrayInputStream in = new ByteArrayInputStream (smallByteArray); 108 ByteArrayOutputStream out = new ByteArrayOutputStream (); 109 IOUtil.copyStream(in, out); 110 assertEquals(smallByteArray, out.toByteArray()); 111 112 in = new ByteArrayInputStream (singletonByteArray); 113 out = new ByteArrayOutputStream (); 114 IOUtil.copyStream(in, out); 115 assertEquals(singletonByteArray, out.toByteArray()); 116 117 in = new ByteArrayInputStream (emptyByteArray); 118 out = new ByteArrayOutputStream (); 119 IOUtil.copyStream(in, out); 120 assertEquals(emptyByteArray, out.toByteArray()); 121 122 byte[] bigArray = generateBigByteArray(); 123 in = new ByteArrayInputStream (bigArray); 124 out = new ByteArrayOutputStream (); 125 IOUtil.copyStream(in, out); 126 assertEquals(bigArray, out.toByteArray()); 127 128 try 129 { 130 IOUtil.copyStream(null, new ByteArrayOutputStream ()); 131 fail("NullPointerException expected"); 132 } 133 catch (NullPointerException ex) 134 { 135 } 136 137 try 138 { 139 IOUtil.copyStream(new ByteArrayInputStream (bigArray), null); 140 fail("NullPointerException expected"); 141 } 142 catch (NullPointerException ex) 143 { 144 } 145 } 146 147 public void testFillByteArrayFromInputStream() throws IOException 148 { 149 byte[] out = IOUtil 150 .createByteArrayFromInputStream(new ByteArrayInputStream ( 151 smallByteArray)); 152 assertEquals(smallByteArray, out); 153 154 out = IOUtil.createByteArrayFromInputStream(new ByteArrayInputStream ( 155 emptyByteArray)); 156 assertEquals(emptyByteArray, out); 157 158 out = IOUtil.createByteArrayFromInputStream(new ByteArrayInputStream ( 159 singletonByteArray)); 160 assertEquals(singletonByteArray, out); 161 162 byte[] bigArray = generateBigByteArray(); 163 out = IOUtil.createByteArrayFromInputStream(new ByteArrayInputStream ( 164 bigArray)); 165 assertEquals(bigArray, out); 166 167 try 168 { 169 IOUtil.createByteArrayFromInputStream(null); 170 fail("NullPointerException expected"); 171 } 172 catch (NullPointerException ex) 173 { 174 } 175 } 176 177 private void assertEquals(byte[] first, byte[] second) 178 { 179 assertEquals(first.length, second.length); 180 for (int i = 0; i < first.length; i++) 181 { 182 assertEquals(first[i], second[i]); 183 } 184 } 185 186 private byte[] generateBigByteArray() 187 { 188 byte[] bigArray = new byte[1000000]; 189 for (int i = 0; i < bigArray.length; i++) 190 { 191 bigArray[i] = (byte)i; 192 } 193 return bigArray; 194 } 195 196 } 197 | Popular Tags |