1 16 package org.apache.commons.io.testtools; 17 18 import java.io.BufferedOutputStream ; 19 import java.io.File ; 20 import java.io.IOException ; 21 import java.io.InputStream ; 22 import java.io.OutputStream ; 23 import java.io.Writer ; 24 import java.util.Arrays ; 25 26 import org.apache.commons.io.FileUtils; 27 import org.apache.commons.io.IOUtils; 28 import org.apache.commons.io.output.ByteArrayOutputStream; 29 30 import junit.framework.AssertionFailedError; 31 import junit.framework.TestCase; 32 33 38 public abstract class FileBasedTestCase extends TestCase { 39 40 private static File testDir; 41 42 public FileBasedTestCase(String name) { 43 super(name); 44 } 45 46 public static File getTestDirectory() { 47 if (testDir == null) { 48 testDir = (new File ("test/io/")).getAbsoluteFile(); 49 } 50 return testDir; 51 } 52 53 protected void createFile(File file, long size) 54 throws IOException { 55 if (!file.getParentFile().exists()) { 56 throw new IOException ("Cannot create file " + file 57 + " as the parent directory does not exist"); 58 } 59 BufferedOutputStream output = 60 new BufferedOutputStream (new java.io.FileOutputStream (file)); 61 try { 62 generateTestData(output, size); 63 } finally { 64 IOUtils.closeQuietly(output); 65 } 66 } 67 68 protected byte[] generateTestData(long size) { 69 try { 70 ByteArrayOutputStream baout = new ByteArrayOutputStream(); 71 generateTestData(baout, size); 72 return baout.toByteArray(); 73 } catch (IOException ioe) { 74 throw new RuntimeException ("This should never happen: " + ioe.getMessage()); 75 } 76 } 77 78 protected void generateTestData(OutputStream out, long size) 79 throws IOException { 80 for (int i = 0; i < size; i++) { 81 83 out.write( (byte)( (i % 127) + 1) ); 85 } 86 } 87 88 protected File newFile(String filename) throws IOException { 89 File destination = new File ( getTestDirectory(), filename ); 90 94 if (destination.exists()) { 95 FileUtils.forceDelete(destination); 96 } 97 return destination; 98 } 99 100 protected void checkFile( File file, File referenceFile ) 101 throws Exception { 102 assertTrue( "Check existence of output file", file.exists() ); 103 assertEqualContent( referenceFile, file ); 104 } 105 106 107 private void assertEqualContent( File f0, File f1 ) 108 throws IOException 109 { 110 116 InputStream is0 = new java.io.FileInputStream ( f0 ); 117 try { 118 InputStream is1 = new java.io.FileInputStream ( f1 ); 119 try { 120 byte[] buf0 = new byte[ 1024 ]; 121 byte[] buf1 = new byte[ 1024 ]; 122 int n0 = 0; 123 int n1 = 0; 124 125 while( -1 != n0 ) 126 { 127 n0 = is0.read( buf0 ); 128 n1 = is1.read( buf1 ); 129 assertTrue( "The files " + f0 + " and " + f1 + 130 " have differing number of bytes available (" + n0 + 131 " vs " + n1 + ")", ( n0 == n1 ) ); 132 133 assertTrue( "The files " + f0 + " and " + f1 + 134 " have different content", Arrays.equals( buf0, buf1 ) ); 135 } 136 } finally { 137 is1.close(); 138 } 139 } finally { 140 is0.close(); 141 } 142 } 143 144 145 protected void assertEqualContent( byte[] b0, File file ) 146 throws IOException 147 { 148 InputStream is = new java.io.FileInputStream ( file ); 149 try { 150 byte[] b1 = new byte[ b0.length ]; 151 int numRead = is.read( b1 ); 152 assertTrue( "Different number of bytes", numRead == b0.length && is.available() == 0 ); 153 for( int i = 0; 154 i < numRead; 155 assertTrue( "Byte " + i + " differs (" + b0[ i ] + " != " + b1[ i ] + ")", 156 b0[ i ] == b1[ i ] ), i++ 157 ); 158 } finally { 159 is.close(); 160 } 161 } 162 163 protected void checkWrite(OutputStream output) throws Exception { 164 try { 165 new java.io.PrintStream (output).write(0); 166 } catch (Throwable t) { 167 throw new AssertionFailedError( 168 "The copy() method closed the stream " 169 + "when it shouldn't have. " 170 + t.getMessage()); 171 } 172 } 173 174 protected void checkWrite(Writer output) throws Exception { 175 try { 176 new java.io.PrintWriter (output).write('a'); 177 } catch (Throwable t) { 178 throw new AssertionFailedError( 179 "The copy() method closed the stream " 180 + "when it shouldn't have. " 181 + t.getMessage()); 182 } 183 } 184 185 protected void deleteFile( File file ) 186 throws Exception { 187 if (file.exists()) { 188 assertTrue("Couldn't delete file: " + file, file.delete()); 189 } 190 } 191 192 193 } 194 | Popular Tags |