1 16 17 package org.apache.commons.io; 18 19 import java.io.File ; 20 import java.io.FileInputStream ; 21 import java.io.FileOutputStream ; 22 import java.io.FileReader ; 23 import java.io.FileWriter ; 24 import java.io.IOException ; 25 import java.util.Arrays ; 26 27 import org.apache.commons.io.testtools.*; 28 29 31 45 public class IOUtilsTestCase extends FileBasedTestCase { 46 52 53 private static final int FILE_SIZE = 1024 * 4 + 1; 54 55 private File m_testFile; 56 57 public void setUp() 58 { 59 try 60 { 61 getTestDirectory().mkdirs(); 62 m_testFile = new File ( getTestDirectory(), "file2-test.txt" ); 63 64 createFile( m_testFile, FILE_SIZE ); 65 } 66 catch( IOException ioe ) 67 { 68 throw new RuntimeException ( "Can't run this test because " 69 + "environment could not be built: " + ioe.getMessage()); 70 } 71 } 72 73 public void tearDown() 74 { 75 try 76 { 77 FileUtils.deleteDirectory( getTestDirectory() ); 78 } 79 catch( IOException ioe ) 80 { 81 } 83 } 84 85 public IOUtilsTestCase( String name ) 86 { 87 super( name ); 88 } 89 90 91 private void assertEqualContent( byte[] b0, byte[] b1 ) 92 throws IOException 93 { 94 assertTrue( "Content not equal according to java.util.Arrays#equals()", Arrays.equals( b0, b1 ) ); 95 } 96 97 public void testInputStreamToString() 98 throws Exception 99 { 100 FileInputStream fin = new FileInputStream ( m_testFile ); 101 try { 102 String out = IOUtils.toString( fin ); 103 assertNotNull( out ); 104 assertTrue( "Not all bytes were read", fin.available() == 0 ); 105 assertTrue( "Wrong output size: out.length()=" + out.length() + 106 "!=" + FILE_SIZE, out.length() == FILE_SIZE ); 107 } finally { 108 fin.close(); 109 } 110 } 111 112 public void testReaderToString() 113 throws Exception 114 { 115 FileReader fin = new FileReader ( m_testFile ); 116 try { 117 String out = IOUtils.toString( fin ); 118 assertNotNull( out ); 119 assertTrue( "Wrong output size: out.length()=" + 120 out.length() + "!=" + FILE_SIZE, 121 out.length() == FILE_SIZE ); 122 } finally { 123 fin.close(); 124 } 125 } 126 127 public void testStringToOutputStream() 128 throws Exception 129 { 130 File destination = newFile( "copy5.txt" ); 131 FileReader fin = new FileReader ( m_testFile ); 132 String str; 133 try { 134 str = IOUtils.toString( fin ); 136 } finally { 137 fin.close(); 138 } 139 140 FileOutputStream fout = new FileOutputStream ( destination ); 141 try { 142 CopyUtils.copy( str, fout ); 143 150 checkFile( destination, m_testFile ); 151 checkWrite( fout ); 152 } finally { 153 fout.close(); 154 } 155 deleteFile( destination ); 156 } 157 158 public void testStringToWriter() 159 throws Exception 160 { 161 File destination = newFile( "copy6.txt" ); 162 FileReader fin = new FileReader ( m_testFile ); 163 String str; 164 try { 165 str = IOUtils.toString( fin ); 167 } finally { 168 fin.close(); 169 } 170 171 FileWriter fout = new FileWriter ( destination ); 172 try { 173 CopyUtils.copy( str, fout ); 174 fout.flush(); 175 176 checkFile( destination, m_testFile ); 177 checkWrite( fout ); 178 } finally { 179 fout.close(); 180 } 181 deleteFile( destination ); 182 } 183 184 public void testInputStreamToByteArray() 185 throws Exception 186 { 187 FileInputStream fin = new FileInputStream ( m_testFile ); 188 try { 189 byte[] out = IOUtils.toByteArray( fin ); 190 assertNotNull( out ); 191 assertTrue( "Not all bytes were read", fin.available() == 0 ); 192 assertTrue( "Wrong output size: out.length=" + out.length + 193 "!=" + FILE_SIZE, out.length == FILE_SIZE ); 194 assertEqualContent( out, m_testFile ); 195 } finally { 196 fin.close(); 197 } 198 } 199 200 public void testStringToByteArray() 201 throws Exception 202 { 203 FileReader fin = new FileReader ( m_testFile ); 204 try { 205 String str = IOUtils.toString( fin ); 207 208 byte[] out = IOUtils.toByteArray( str ); 209 assertEqualContent( str.getBytes(), out ); 210 } finally { 211 fin.close(); 212 } 213 } 214 215 public void testByteArrayToWriter() 216 throws Exception 217 { 218 File destination = newFile( "copy7.txt" ); 219 FileInputStream fin = new FileInputStream ( m_testFile ); 220 byte[] in; 221 try { 222 in = IOUtils.toByteArray( fin ); 224 } finally { 225 fin.close(); 226 } 227 228 FileWriter fout = new FileWriter ( destination ); 229 try { 230 CopyUtils.copy( in, fout ); 231 fout.flush(); 232 checkFile( destination, m_testFile ); 233 checkWrite( fout ); 234 } finally { 235 fout.close(); 236 } 237 deleteFile( destination ); 238 } 239 240 public void testByteArrayToString() 241 throws Exception 242 { 243 FileInputStream fin = new FileInputStream ( m_testFile ); 244 try { 245 byte[] in = IOUtils.toByteArray( fin ); 246 String str = IOUtils.toString( in ); 248 assertEqualContent( in, str.getBytes() ); 249 } finally { 250 fin.close(); 251 } 252 } 253 254 public void testByteArrayToOutputStream() 255 throws Exception 256 { 257 File destination = newFile( "copy8.txt" ); 258 FileInputStream fin = new FileInputStream ( m_testFile ); 259 byte[] in; 260 try { 261 in = IOUtils.toByteArray( fin ); 263 } finally { 264 fin.close(); 265 } 266 267 FileOutputStream fout = new FileOutputStream ( destination ); 268 try { 269 CopyUtils.copy( in, fout ); 270 271 fout.flush(); 272 273 checkFile( destination, m_testFile ); 274 checkWrite( fout ); 275 } finally { 276 fout.close(); 277 } 278 deleteFile( destination ); 279 } 280 281 282 } 283 284 | Popular Tags |