KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > io > testtools > FileBasedTestCase


1 /*
2  * Copyright 2001-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.commons.io.testtools;
17
18 import java.io.BufferedOutputStream JavaDoc;
19 import java.io.File JavaDoc;
20 import java.io.IOException JavaDoc;
21 import java.io.InputStream JavaDoc;
22 import java.io.OutputStream JavaDoc;
23 import java.io.Writer JavaDoc;
24 import java.util.Arrays JavaDoc;
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 /**
34  * Base class for testcases doing tests with files.
35  *
36  * @author Jeremias Maerki
37  */

38 public abstract class FileBasedTestCase extends TestCase {
39
40     private static File JavaDoc testDir;
41
42     public FileBasedTestCase(String JavaDoc name) {
43         super(name);
44     }
45     
46     public static File JavaDoc getTestDirectory() {
47         if (testDir == null) {
48             testDir = (new File JavaDoc("test/io/")).getAbsoluteFile();
49         }
50         return testDir;
51     }
52     
53     protected void createFile(File JavaDoc file, long size)
54             throws IOException JavaDoc {
55         if (!file.getParentFile().exists()) {
56             throw new IOException JavaDoc("Cannot create file " + file
57                 + " as the parent directory does not exist");
58         }
59         BufferedOutputStream JavaDoc output =
60             new BufferedOutputStream JavaDoc(new java.io.FileOutputStream JavaDoc(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 JavaDoc ioe) {
74             throw new RuntimeException JavaDoc("This should never happen: " + ioe.getMessage());
75         }
76     }
77     
78     protected void generateTestData(OutputStream JavaDoc out, long size)
79                 throws IOException JavaDoc {
80         for (int i = 0; i < size; i++) {
81             //output.write((byte)'X');
82

83             // nice varied byte pattern compatible with Readers and Writers
84
out.write( (byte)( (i % 127) + 1) );
85         }
86     }
87
88     protected File JavaDoc newFile(String JavaDoc filename) throws IOException JavaDoc {
89         File JavaDoc destination = new File JavaDoc( getTestDirectory(), filename );
90         /*
91         assertTrue( filename + "Test output data file shouldn't previously exist",
92                     !destination.exists() );
93         */

94         if (destination.exists()) {
95             FileUtils.forceDelete(destination);
96         }
97         return destination;
98     }
99
100     protected void checkFile( File JavaDoc file, File JavaDoc referenceFile )
101                 throws Exception JavaDoc {
102         assertTrue( "Check existence of output file", file.exists() );
103         assertEqualContent( referenceFile, file );
104     }
105
106     /** Assert that the content of two files is the same. */
107     private void assertEqualContent( File JavaDoc f0, File JavaDoc f1 )
108         throws IOException JavaDoc
109     {
110         /* This doesn't work because the filesize isn't updated until the file
111          * is closed.
112         assertTrue( "The files " + f0 + " and " + f1 +
113                     " have differing file sizes (" + f0.length() +
114                     " vs " + f1.length() + ")", ( f0.length() == f1.length() ) );
115         */

116         InputStream JavaDoc is0 = new java.io.FileInputStream JavaDoc( f0 );
117         try {
118             InputStream JavaDoc is1 = new java.io.FileInputStream JavaDoc( 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     /** Assert that the content of a file is equal to that in a byte[]. */
145     protected void assertEqualContent( byte[] b0, File JavaDoc file )
146         throws IOException JavaDoc
147     {
148         InputStream JavaDoc is = new java.io.FileInputStream JavaDoc( 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 JavaDoc output) throws Exception JavaDoc {
164         try {
165             new java.io.PrintStream JavaDoc(output).write(0);
166         } catch (Throwable JavaDoc 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 JavaDoc output) throws Exception JavaDoc {
175         try {
176             new java.io.PrintWriter JavaDoc(output).write('a');
177         } catch (Throwable JavaDoc 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 JavaDoc file )
186         throws Exception JavaDoc {
187         if (file.exists()) {
188             assertTrue("Couldn't delete file: " + file, file.delete());
189         }
190     }
191     
192
193 }
194
Popular Tags