|
| 1 | +/* |
| 2 | + * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//io/src/test/org/apache/commons/io/testtools/FileBasedTestCase.java,v 1.1 2003/08/21 18:42:53 jeremias Exp $ |
| 3 | + * $Revision: 1.1 $ |
| 4 | + * $Date: 2003/08/21 18:42:53 $ |
| 5 | + * |
| 6 | + * ==================================================================== |
| 7 | + * The Apache Software License, Version 1.1 |
| 8 | + * |
| 9 | + * Copyright (c) 2001-2003 The Apache Software Foundation. All rights |
| 10 | + * reserved. |
| 11 | + * |
| 12 | + * Redistribution and use in source and binary forms, with or without |
| 13 | + * modification, are permitted provided that the following conditions |
| 14 | + * are met: |
| 15 | + * |
| 16 | + * 1. Redistributions of source code must retain the above copyright |
| 17 | + * notice, this list of conditions and the following disclaimer. |
| 18 | + * |
| 19 | + * 2. Redistributions in binary form must reproduce the above copyright |
| 20 | + * notice, this list of conditions and the following disclaimer in |
| 21 | + * the documentation and/or other materials provided with the |
| 22 | + * distribution. |
| 23 | + * |
| 24 | + * 3. The end-user documentation included with the redistribution, if |
| 25 | + * any, must include the following acknowlegement: |
| 26 | + * "This product includes software developed by the |
| 27 | + * Apache Software Foundation (http://www.apache.org/)." |
| 28 | + * Alternately, this acknowlegement may appear in the software itself, |
| 29 | + * if and wherever such third-party acknowlegements normally appear. |
| 30 | + * |
| 31 | + * 4. The names "The Jakarta Project", "Commons", and "Apache Software |
| 32 | + * Foundation" must not be used to endorse or promote products derived |
| 33 | + * from this software without prior written permission. For written |
| 34 | + * permission, please contact apache@apache.org. |
| 35 | + * |
| 36 | + * 5. Products derived from this software may not be called "Apache" |
| 37 | + * nor may "Apache" appear in their names without prior written |
| 38 | + * permission of the Apache Software Foundation. |
| 39 | + * |
| 40 | + * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED |
| 41 | + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
| 42 | + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 43 | + * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR |
| 44 | + * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 45 | + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 46 | + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF |
| 47 | + * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
| 48 | + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
| 49 | + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT |
| 50 | + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 51 | + * SUCH DAMAGE. |
| 52 | + * ==================================================================== |
| 53 | + * |
| 54 | + * This software consists of voluntary contributions made by many |
| 55 | + * individuals on behalf of the Apache Software Foundation. For more |
| 56 | + * information on the Apache Software Foundation, please see |
| 57 | + * <http://www.apache.org/>. |
| 58 | + */ |
| 59 | +package org.apache.commons.io.testtools; |
| 60 | + |
| 61 | +import java.io.BufferedOutputStream; |
| 62 | +import java.io.ByteArrayOutputStream; |
| 63 | +import java.io.File; |
| 64 | +import java.io.IOException; |
| 65 | +import java.io.InputStream; |
| 66 | +import java.io.OutputStream; |
| 67 | +import java.io.Writer; |
| 68 | +import java.util.Arrays; |
| 69 | + |
| 70 | +import org.apache.commons.io.FileUtils; |
| 71 | +import org.apache.commons.io.IOUtils; |
| 72 | + |
| 73 | +import junit.framework.AssertionFailedError; |
| 74 | +import junit.framework.TestCase; |
| 75 | + |
| 76 | +/** |
| 77 | + * Base class for testcases doing tests with files. |
| 78 | + * |
| 79 | + * @author Jeremias Maerki |
| 80 | + */ |
| 81 | +public abstract class FileBasedTestCase extends TestCase { |
| 82 | + |
| 83 | + private static File testDir; |
| 84 | + |
| 85 | + public FileBasedTestCase(String name) { |
| 86 | + super(name); |
| 87 | + } |
| 88 | + |
| 89 | + public static File getTestDirectory() { |
| 90 | + if (testDir == null) { |
| 91 | + testDir = (new File("test/io/")).getAbsoluteFile(); |
| 92 | + } |
| 93 | + return testDir; |
| 94 | + } |
| 95 | + |
| 96 | + protected void createFile(final File file, final long size) |
| 97 | + throws IOException { |
| 98 | + if (!file.getParentFile().exists()) { |
| 99 | + throw new IOException("Cannot create file " + file |
| 100 | + + " as the parent directory does not exist"); |
| 101 | + } |
| 102 | + final BufferedOutputStream output = |
| 103 | + new BufferedOutputStream(new java.io.FileOutputStream(file)); |
| 104 | + try { |
| 105 | + generateTestData(output, size); |
| 106 | + } finally { |
| 107 | + IOUtils.shutdownStream(output); |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + protected byte[] generateTestData(final long size) { |
| 112 | + try { |
| 113 | + ByteArrayOutputStream baout = new ByteArrayOutputStream(); |
| 114 | + generateTestData(baout, size); |
| 115 | + return baout.toByteArray(); |
| 116 | + } catch (IOException ioe) { |
| 117 | + throw new RuntimeException("This should never happen: " + ioe.getMessage()); |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + protected void generateTestData(final OutputStream out, final long size) |
| 122 | + throws IOException { |
| 123 | + for (int i = 0; i < size; i++) { |
| 124 | + //output.write((byte)'X'); |
| 125 | + |
| 126 | + // nice varied byte pattern compatible with Readers and Writers |
| 127 | + out.write( (byte)( (i % 127) + 1) ); |
| 128 | + } |
| 129 | + } |
| 130 | + |
| 131 | + protected File newFile(String filename) throws IOException { |
| 132 | + final File destination = new File( getTestDirectory(), filename ); |
| 133 | + /* |
| 134 | + assertTrue( filename + "Test output data file shouldn't previously exist", |
| 135 | + !destination.exists() ); |
| 136 | + */ |
| 137 | + if (destination.exists()) { |
| 138 | + FileUtils.forceDelete(destination); |
| 139 | + } |
| 140 | + return destination; |
| 141 | + } |
| 142 | + |
| 143 | + protected void checkFile( final File file, final File referenceFile ) |
| 144 | + throws Exception { |
| 145 | + assertTrue( "Check existence of output file", file.exists() ); |
| 146 | + assertEqualContent( referenceFile, file ); |
| 147 | + } |
| 148 | + |
| 149 | + /** Assert that the content of two files is the same. */ |
| 150 | + private void assertEqualContent( final File f0, final File f1 ) |
| 151 | + throws IOException |
| 152 | + { |
| 153 | + /* This doesn't work because the filesize isn't updated until the file |
| 154 | + * is closed. |
| 155 | + assertTrue( "The files " + f0 + " and " + f1 + |
| 156 | + " have differing file sizes (" + f0.length() + |
| 157 | + " vs " + f1.length() + ")", ( f0.length() == f1.length() ) ); |
| 158 | + */ |
| 159 | + final InputStream is0 = new java.io.FileInputStream( f0 ); |
| 160 | + try { |
| 161 | + final InputStream is1 = new java.io.FileInputStream( f1 ); |
| 162 | + try { |
| 163 | + final byte[] buf0 = new byte[ 1024 ]; |
| 164 | + final byte[] buf1 = new byte[ 1024 ]; |
| 165 | + int n0 = 0; |
| 166 | + int n1 = 0; |
| 167 | + |
| 168 | + while( -1 != n0 ) |
| 169 | + { |
| 170 | + n0 = is0.read( buf0 ); |
| 171 | + n1 = is1.read( buf1 ); |
| 172 | + assertTrue( "The files " + f0 + " and " + f1 + |
| 173 | + " have differing number of bytes available (" + n0 + |
| 174 | + " vs " + n1 + ")", ( n0 == n1 ) ); |
| 175 | + |
| 176 | + assertTrue( "The files " + f0 + " and " + f1 + |
| 177 | + " have different content", Arrays.equals( buf0, buf1 ) ); |
| 178 | + } |
| 179 | + } finally { |
| 180 | + is1.close(); |
| 181 | + } |
| 182 | + } finally { |
| 183 | + is0.close(); |
| 184 | + } |
| 185 | + } |
| 186 | + |
| 187 | + /** Assert that the content of a file is equal to that in a byte[]. */ |
| 188 | + protected void assertEqualContent( final byte[] b0, final File file ) |
| 189 | + throws IOException |
| 190 | + { |
| 191 | + final InputStream is = new java.io.FileInputStream( file ); |
| 192 | + try { |
| 193 | + byte[] b1 = new byte[ b0.length ]; |
| 194 | + int numRead = is.read( b1 ); |
| 195 | + assertTrue( "Different number of bytes", numRead == b0.length && is.available() == 0 ); |
| 196 | + for( int i = 0; |
| 197 | + i < numRead; |
| 198 | + assertTrue( "Byte " + i + " differs (" + b0[ i ] + " != " + b1[ i ] + ")", |
| 199 | + b0[ i ] == b1[ i ] ), i++ |
| 200 | + ); |
| 201 | + } finally { |
| 202 | + is.close(); |
| 203 | + } |
| 204 | + } |
| 205 | + |
| 206 | + protected void checkWrite(final OutputStream output) throws Exception { |
| 207 | + try { |
| 208 | + new java.io.PrintStream(output).write(0); |
| 209 | + } catch (final Throwable t) { |
| 210 | + throw new AssertionFailedError( |
| 211 | + "The copy() method closed the stream " |
| 212 | + + "when it shouldn't have. " |
| 213 | + + t.getMessage()); |
| 214 | + } |
| 215 | + } |
| 216 | + |
| 217 | + protected void checkWrite(final Writer output) throws Exception { |
| 218 | + try { |
| 219 | + new java.io.PrintWriter(output).write('a'); |
| 220 | + } catch (final Throwable t) { |
| 221 | + throw new AssertionFailedError( |
| 222 | + "The copy() method closed the stream " |
| 223 | + + "when it shouldn't have. " |
| 224 | + + t.getMessage()); |
| 225 | + } |
| 226 | + } |
| 227 | + |
| 228 | + protected void deleteFile( final File file ) |
| 229 | + throws Exception { |
| 230 | + if (file.exists()) { |
| 231 | + assertTrue("Couldn't delete file: " + file, file.delete()); |
| 232 | + } |
| 233 | + } |
| 234 | + |
| 235 | + |
| 236 | +} |
0 commit comments