1 8 package org.apache.avalon.excalibur.io.test; 9 10 import java.io.*; 11 import java.util.Arrays ; 12 import org.apache.avalon.excalibur.io.IOUtil; 13 import org.apache.avalon.excalibur.io.FileUtil; 14 import junit.framework.TestCase; 15 import junit.framework.AssertionFailedError; 17 31 32 38 public final class IOUtilTestCase 39 extends TestCase 40 { 41 private final int FILE_SIZE = 1024 * 4 + 1; 42 43 private File m_testDirectory; 44 private File m_testFile; 45 46 public void setUp() 47 { 48 try 49 { 50 m_testDirectory = (new File( "test/io/" )).getAbsoluteFile(); 51 if( !m_testDirectory.exists() ) 52 { 53 m_testDirectory.mkdirs(); 54 } 55 56 m_testFile = new File( m_testDirectory, "file2-test.txt" ); 57 58 createFile( m_testFile, FILE_SIZE ); 59 } 60 catch (IOException ioe) 61 { 62 throw new RuntimeException ("Can't run this test because environment could not be built"); 63 } 64 } 65 66 public void tearDown() 67 { 68 try 69 { 70 FileUtil.deleteDirectory("test"); 71 } 72 catch (IOException ioe) 73 { 74 } 76 } 77 78 public IOUtilTestCase(String name) 79 { 80 super( name ); 81 } 82 83 private void createFile( final File file, final long size ) 84 throws IOException 85 { 86 final BufferedOutputStream output = 87 new BufferedOutputStream( new FileOutputStream( file ) ); 88 89 for( int i = 0; i < size; i++ ) 90 { 91 output.write( (byte)(i % 128) ); } 93 94 output.close(); 95 } 96 97 98 private void assertEqualContent( final byte[] b0, final byte[] b1 ) 99 throws IOException 100 { 101 assertTrue( "Content not equal according to java.util.Arrays#equals()", Arrays.equals( b0, b1 ) ); 102 } 103 104 105 private void assertEqualContent( final File f0, final File f1 ) 106 throws IOException 107 { 108 final FileInputStream is0 = new FileInputStream( f0 ); 109 final FileInputStream is1 = new FileInputStream( f1 ); 110 final byte[] buf0 = new byte[ FILE_SIZE ]; 111 final byte[] buf1 = new byte[ FILE_SIZE ]; 112 int n0 = 0; 113 int n1 = 0; 114 115 while( -1 != n0 ) 116 { 117 n0 = is0.read( buf0 ); 118 n1 = is1.read( buf1 ); 119 assertTrue( "The files " + f0 + " and " + f1 + 120 " have differing number of bytes available (" + n0 + 121 " vs " + n1 + ")", (n0 == n1) ); 122 123 assertTrue( "The files " + f0 + " and " + f1 + 124 " have different content", Arrays.equals( buf0, buf1 ) ); 125 } 126 } 127 128 129 private void assertEqualContent( final byte[] b0, final File file ) 130 throws IOException 131 { 132 final FileInputStream is = new FileInputStream( file ); 133 byte[] b1 = new byte[b0.length]; 134 int numRead = is.read(b1); 135 assertTrue( "Different number of bytes", numRead == b0.length && is.available() == 0 ); 136 for (int i=0; 137 i<numRead; 138 assertTrue( "Byte "+i+" differs ("+b0[i]+" != "+b1[i]+")", b0[i] == b1[i] ), i++ 139 ); 140 } 141 142 public void testInputStreamToOutputStream() 143 throws Exception 144 { 145 final File destination = newFile("copy1.txt"); 146 final FileInputStream fin = new FileInputStream( m_testFile ); 147 final FileOutputStream fout = new FileOutputStream( destination ); 148 149 IOUtil.copy( fin, fout ); 150 assertTrue( "Not all bytes were read", fin.available() == 0 ); 151 fout.flush(); 152 153 checkFile( destination ); 154 checkWrite( fout ); 155 fout.close(); 156 fin.close(); 157 deleteFile( destination ); 158 } 159 160 public void testInputStreamToWriter() 161 throws Exception 162 { 163 final File destination = newFile("copy2.txt"); 164 final FileInputStream fin = new FileInputStream( m_testFile ); 165 final FileWriter fout = new FileWriter( destination ); 166 167 IOUtil.copy( fin, fout ); 168 169 assertTrue( "Not all bytes were read", fin.available() == 0 ); 170 fout.flush(); 171 172 checkFile( destination ); 173 checkWrite( fout ); 174 fout.close(); 175 fin.close(); 176 deleteFile( destination ); 177 } 178 179 public void testInputStreamToString() 180 throws Exception 181 { 182 final FileInputStream fin = new FileInputStream( m_testFile ); 183 final String out = IOUtil.toString( fin ); 184 assertNotNull( out ); 185 assertTrue( "Not all bytes were read", fin.available() == 0 ); 186 assertTrue( "Wrong output size: out.length()=" + out.length() + 187 "!=" + FILE_SIZE, out.length() == FILE_SIZE ); 188 fin.close(); 189 } 190 191 public void testReaderToOutputStream() 192 throws Exception 193 { 194 final File destination = newFile("copy3.txt"); 195 final FileReader fin = new FileReader( m_testFile ); 196 final FileOutputStream fout = new FileOutputStream( destination ); 197 IOUtil.copy( fin, fout ); 198 204 checkFile( destination ); 206 checkWrite( fout ); 207 fout.close(); 208 fin.close(); 209 deleteFile( destination ); 210 } 211 212 public void testReaderToWriter() 213 throws Exception 214 { 215 final File destination = newFile("copy4.txt"); 216 final FileReader fin = new FileReader( m_testFile ); 217 final FileWriter fout = new FileWriter( destination ); 218 IOUtil.copy( fin, fout ); 219 220 fout.flush(); 221 checkFile( destination ); 222 checkWrite( fout ); 223 fout.close(); 224 fin.close(); 225 deleteFile( destination ); 226 } 227 228 public void testReaderToString() 229 throws Exception 230 { 231 final FileReader fin = new FileReader( m_testFile ); 232 final String out = IOUtil.toString( fin ); 233 assertNotNull( out ); 234 assertTrue( "Wrong output size: out.length()=" + 235 out.length() + "!=" + FILE_SIZE, 236 out.length() == FILE_SIZE ); 237 fin.close(); 238 } 239 240 public void testStringToOutputStream() 241 throws Exception 242 { 243 final File destination = newFile("copy5.txt"); 244 final FileReader fin = new FileReader( m_testFile ); 245 final String str = IOUtil.toString( fin ); 247 final FileOutputStream fout = new FileOutputStream( destination ); 248 IOUtil.copy( str, fout ); 249 256 checkFile( destination ); 257 checkWrite( fout ); 258 fout.close(); 259 fin.close(); 260 deleteFile( destination ); 261 } 262 263 public void testStringToWriter() 264 throws Exception 265 { 266 final File destination = newFile("copy6.txt"); 267 FileReader fin = new FileReader( m_testFile ); 268 final String str = IOUtil.toString( fin ); 270 final FileWriter fout = new FileWriter( destination ); 271 IOUtil.copy( str, fout ); 272 fout.flush(); 273 274 checkFile( destination ); 275 checkWrite( fout ); 276 fout.close(); 277 fin.close(); 278 279 deleteFile( destination ); 280 } 281 282 283 public void testInputStreamToByteArray() 284 throws Exception 285 { 286 final FileInputStream fin = new FileInputStream( m_testFile ); 287 final byte[] out = IOUtil.toByteArray( fin ); 288 assertNotNull( out ); 289 assertTrue( "Not all bytes were read", fin.available() == 0 ); 290 assertTrue( "Wrong output size: out.length=" + out.length + 291 "!=" + FILE_SIZE, out.length == FILE_SIZE ); 292 assertEqualContent( out, m_testFile ); 293 fin.close(); 294 } 295 296 public void testStringToByteArray() 297 throws Exception 298 { 299 final FileReader fin = new FileReader( m_testFile ); 300 301 final String str = IOUtil.toString( fin ); 303 304 final byte[] out = IOUtil.toByteArray( str ); 305 assertEqualContent( str.getBytes(), out ); 306 fin.close(); 307 } 308 309 public void testByteArrayToWriter() 310 throws Exception 311 { 312 final File destination = newFile("copy7.txt"); 313 final FileWriter fout = new FileWriter( destination ); 314 final FileInputStream fin = new FileInputStream( m_testFile ); 315 316 final byte[] in = IOUtil.toByteArray( fin ); 318 IOUtil.copy( in, fout ); 319 fout.flush(); 320 checkFile( destination ); 321 checkWrite( fout ); 322 fout.close(); 323 fin.close(); 324 deleteFile( destination ); 325 } 326 327 328 public void testByteArrayToString() 329 throws Exception 330 { 331 final FileInputStream fin = new FileInputStream( m_testFile ); 332 final byte[] in = IOUtil.toByteArray( fin ); 333 String str = IOUtil.toString( in ); 335 assertEqualContent( in, str.getBytes() ); 336 fin.close(); 337 } 338 339 340 public void testByteArrayToOutputStream() 341 throws Exception 342 { 343 final File destination = newFile("copy8.txt"); 344 final FileOutputStream fout = new FileOutputStream( destination ); 345 final FileInputStream fin = new FileInputStream( m_testFile ); 346 347 final byte[] in = IOUtil.toByteArray( fin ); 349 350 IOUtil.copy( in, fout ); 351 352 fout.flush(); 353 354 checkFile( destination ); 355 checkWrite( fout ); 356 fout.close(); 357 fin.close(); 358 deleteFile( destination ); 359 } 360 361 362 365 366 private File newFile(String filename) 367 throws Exception 368 { 369 final File destination = new File( m_testDirectory, filename ); 370 assertTrue( filename + "Test output data file shouldn't previously exist", 371 !destination.exists() ); 372 373 return destination; 374 } 375 376 private void checkFile( final File file ) 377 throws Exception 378 { 379 assertTrue( "Check existence of output file", file.exists() ); 380 assertEqualContent( m_testFile, file ); 381 } 382 383 private void checkWrite( final OutputStream output ) 384 throws Exception 385 { 386 try 387 { 388 new PrintStream( output ).write( 0 ); 389 } 390 catch( final Throwable t ) 391 { 392 throw new AssertionFailedError( "The copy() method closed the stream " + 393 "when it shouldn't have. " + t.getMessage() ); 394 } 395 } 396 397 private void checkWrite( final Writer output ) 398 throws Exception 399 { 400 try 401 { 402 new PrintWriter( output ).write( 'a' ); 403 } 404 catch( final Throwable t ) 405 { 406 throw new AssertionFailedError( "The copy() method closed the stream " + 407 "when it shouldn't have. " + t.getMessage() ); 408 } 409 } 410 411 private void deleteFile( final File file ) 412 throws Exception 413 { 414 assertTrue( "Wrong output size: file.length()=" + 415 file.length() + "!=" + FILE_SIZE + 1, 416 file.length() == FILE_SIZE + 1 ); 417 418 } 420 } 421 | Popular Tags |