001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.commons.io.testtools;
018:
019: import java.io.BufferedOutputStream;
020: import java.io.File;
021: import java.io.FileOutputStream;
022: import java.io.IOException;
023: import java.io.InputStream;
024: import java.io.OutputStream;
025: import java.io.OutputStreamWriter;
026: import java.io.PrintWriter;
027: import java.io.Reader;
028: import java.io.Writer;
029: import java.util.Arrays;
030:
031: import junit.framework.AssertionFailedError;
032: import junit.framework.TestCase;
033:
034: import org.apache.commons.io.FileUtils;
035: import org.apache.commons.io.IOUtils;
036: import org.apache.commons.io.output.ByteArrayOutputStream;
037:
038: /**
039: * Base class for testcases doing tests with files.
040: *
041: * @author Jeremias Maerki
042: * @author Gareth Davis
043: */
044: public abstract class FileBasedTestCase extends TestCase {
045:
046: private static File testDir;
047:
048: public FileBasedTestCase(String name) {
049: super (name);
050: }
051:
052: public static File getTestDirectory() {
053: if (testDir == null) {
054: testDir = (new File("test/io/")).getAbsoluteFile();
055: }
056: testDir.mkdirs();
057: return testDir;
058: }
059:
060: protected void createFile(File file, long size) throws IOException {
061: if (!file.getParentFile().exists()) {
062: throw new IOException("Cannot create file " + file
063: + " as the parent directory does not exist");
064: }
065: BufferedOutputStream output = new BufferedOutputStream(
066: new java.io.FileOutputStream(file));
067: try {
068: generateTestData(output, size);
069: } finally {
070: IOUtils.closeQuietly(output);
071: }
072: }
073:
074: protected byte[] generateTestData(long size) {
075: try {
076: ByteArrayOutputStream baout = new ByteArrayOutputStream();
077: generateTestData(baout, size);
078: return baout.toByteArray();
079: } catch (IOException ioe) {
080: throw new RuntimeException("This should never happen: "
081: + ioe.getMessage());
082: }
083: }
084:
085: protected void generateTestData(OutputStream out, long size)
086: throws IOException {
087: for (int i = 0; i < size; i++) {
088: //output.write((byte)'X');
089:
090: // nice varied byte pattern compatible with Readers and Writers
091: out.write((byte) ((i % 127) + 1));
092: }
093: }
094:
095: protected void createLineBasedFile(File file, String[] data)
096: throws IOException {
097: if (file.getParentFile() != null
098: && !file.getParentFile().exists()) {
099: throw new IOException("Cannot create file " + file
100: + " as the parent directory does not exist");
101: }
102: PrintWriter output = new PrintWriter(new OutputStreamWriter(
103: new FileOutputStream(file), "UTF-8"));
104: try {
105: for (int i = 0; i < data.length; i++) {
106: output.println(data[i]);
107: }
108: } finally {
109: IOUtils.closeQuietly(output);
110: }
111: }
112:
113: protected File newFile(String filename) throws IOException {
114: File destination = new File(getTestDirectory(), filename);
115: /*
116: assertTrue( filename + "Test output data file shouldn't previously exist",
117: !destination.exists() );
118: */
119: if (destination.exists()) {
120: FileUtils.forceDelete(destination);
121: }
122: return destination;
123: }
124:
125: protected void checkFile(File file, File referenceFile)
126: throws Exception {
127: assertTrue("Check existence of output file", file.exists());
128: assertEqualContent(referenceFile, file);
129: }
130:
131: /** Assert that the content of two files is the same. */
132: private void assertEqualContent(File f0, File f1)
133: throws IOException {
134: /* This doesn't work because the filesize isn't updated until the file
135: * is closed.
136: assertTrue( "The files " + f0 + " and " + f1 +
137: " have differing file sizes (" + f0.length() +
138: " vs " + f1.length() + ")", ( f0.length() == f1.length() ) );
139: */
140: InputStream is0 = new java.io.FileInputStream(f0);
141: try {
142: InputStream is1 = new java.io.FileInputStream(f1);
143: try {
144: byte[] buf0 = new byte[1024];
145: byte[] buf1 = new byte[1024];
146: int n0 = 0;
147: int n1 = 0;
148:
149: while (-1 != n0) {
150: n0 = is0.read(buf0);
151: n1 = is1.read(buf1);
152: assertTrue(
153: "The files "
154: + f0
155: + " and "
156: + f1
157: + " have differing number of bytes available ("
158: + n0 + " vs " + n1 + ")",
159: (n0 == n1));
160:
161: assertTrue("The files " + f0 + " and " + f1
162: + " have different content", Arrays.equals(
163: buf0, buf1));
164: }
165: } finally {
166: is1.close();
167: }
168: } finally {
169: is0.close();
170: }
171: }
172:
173: /** Assert that the content of a file is equal to that in a byte[]. */
174: protected void assertEqualContent(byte[] b0, File file)
175: throws IOException {
176: InputStream is = new java.io.FileInputStream(file);
177: int count = 0, numRead = 0;
178: byte[] b1 = new byte[b0.length];
179: try {
180: while (count < b0.length && numRead >= 0) {
181: numRead = is.read(b1, count, b0.length);
182: count += numRead;
183: }
184: assertEquals("Different number of bytes: ", b0.length,
185: count);
186: for (int i = 0; i < count; i++) {
187: assertEquals("byte " + i + " differs", b0[i], b1[i]);
188: }
189: } finally {
190: is.close();
191: }
192: }
193:
194: /** Assert that the content of a file is equal to that in a char[]. */
195: protected void assertEqualContent(char[] c0, File file)
196: throws IOException {
197: Reader ir = new java.io.FileReader(file);
198: int count = 0, numRead = 0;
199: char[] c1 = new char[c0.length];
200: try {
201: while (count < c0.length && numRead >= 0) {
202: numRead = ir.read(c1, count, c0.length);
203: count += numRead;
204: }
205: assertEquals("Different number of chars: ", c0.length,
206: count);
207: for (int i = 0; i < count; i++) {
208: assertEquals("char " + i + " differs", c0[i], c1[i]);
209: }
210: } finally {
211: ir.close();
212: }
213: }
214:
215: protected void checkWrite(OutputStream output) throws Exception {
216: try {
217: new java.io.PrintStream(output).write(0);
218: } catch (Throwable t) {
219: throw new AssertionFailedError(
220: "The copy() method closed the stream "
221: + "when it shouldn't have. "
222: + t.getMessage());
223: }
224: }
225:
226: protected void checkWrite(Writer output) throws Exception {
227: try {
228: new java.io.PrintWriter(output).write('a');
229: } catch (Throwable t) {
230: throw new AssertionFailedError(
231: "The copy() method closed the stream "
232: + "when it shouldn't have. "
233: + t.getMessage());
234: }
235: }
236:
237: protected void deleteFile(File file) throws Exception {
238: if (file.exists()) {
239: assertTrue("Couldn't delete file: " + file, file.delete());
240: }
241: }
242:
243: }
|