KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > avalon > excalibur > io > test > IOUtilTestCase


1 /*
2  * Copyright The Apache Software Foundation. All rights reserved.
3  *
4  * This software is published under the terms of the Apache Software License
5  * version 1.1, a copy of which has been included with this distribution in
6  * the LICENSE.txt file.
7  */

8 package org.apache.avalon.excalibur.io.test;
9
10 import java.io.*;
11 import java.util.Arrays JavaDoc;
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; // Note: jdk1.2 dependency
16

17 /**
18  * This is used to test IOUtil for correctness. The following checks are performed:
19  * <ul>
20  * <li>The return must not be null, must be the same type and equals() to the method's second arg</li>
21  * <li>All bytes must have been read from the source (available() == 0)</li>
22  * <li>The source and destination content must be identical (byte-wise comparison check)</li>
23  * <li>The output stream must not have been closed (a byte/char is written to test this, and
24  * subsequent size checked)</li>
25  * </ul>
26  * Due to interdependencies in IOUtils and IOUtilsTestlet, one bug may cause
27  * multiple tests to fail.
28  *
29  * @author <a HREF="mailto:jefft@apache.org">Jeff Turner</a>
30  */

31
32 /*
33  * Note: this is not particularly beautiful code. A better way to check for
34  * flush and close status would be to implement "trojan horse" wrapper
35  * implementations of the various stream classes, which set a flag when
36  * relevant methods are called. (JT)
37  */

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 JavaDoc("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             // Ignore, because by this time, it is too late.
75
}
76     }
77
78     public IOUtilTestCase(String JavaDoc 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) ); // nice varied byte pattern compatible with Readers and Writers
92
}
93
94         output.close();
95     }
96
97     /** Assert that the contents of two byte arrays are the same. */
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 /** Assert that the content of two files is the same. */
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     /** Assert that the content of a file is equal to that in a byte[]. */
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 JavaDoc
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 JavaDoc
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 JavaDoc
181     {
182         final FileInputStream fin = new FileInputStream( m_testFile );
183         final String JavaDoc 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 JavaDoc
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         //Note: this method *does* flush. It is equivalent to:
199
// final OutputStreamWriter _out = new OutputStreamWriter(fout);
200
// IOUtil.copy( fin, _out, 4096 ); // copy( Reader, Writer, int );
201
// _out.flush();
202
// out = fout;
203

204         // Note: rely on the method to flush
205
checkFile( destination );
206         checkWrite( fout );
207         fout.close();
208         fin.close();
209         deleteFile( destination );
210     }
211
212     public void testReaderToWriter()
213         throws Exception JavaDoc
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 JavaDoc
230     {
231         final FileReader fin = new FileReader( m_testFile );
232         final String JavaDoc 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 JavaDoc
242     {
243         final File destination = newFile("copy5.txt");
244         final FileReader fin = new FileReader( m_testFile );
245         // Create our String. Rely on testReaderToString() to make sure this is valid.
246
final String JavaDoc str = IOUtil.toString( fin );
247         final FileOutputStream fout = new FileOutputStream( destination );
248         IOUtil.copy( str, fout );
249         //Note: this method *does* flush. It is equivalent to:
250
// final OutputStreamWriter _out = new OutputStreamWriter(fout);
251
// IOUtil.copy( str, _out, 4096 ); // copy( Reader, Writer, int );
252
// _out.flush();
253
// out = fout;
254
// note: we don't flush here; this IOUtils method does it for us
255

256         checkFile( destination );
257         checkWrite( fout );
258         fout.close();
259         fin.close();
260         deleteFile( destination );
261     }
262
263     public void testStringToWriter()
264         throws Exception JavaDoc
265     {
266         final File destination = newFile("copy6.txt");
267         FileReader fin = new FileReader( m_testFile );
268         // Create our String. Rely on testReaderToString() to make sure this is valid.
269
final String JavaDoc 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 JavaDoc
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 JavaDoc
298     {
299         final FileReader fin = new FileReader( m_testFile );
300
301         // Create our String. Rely on testReaderToString() to make sure this is valid.
302
final String JavaDoc 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 JavaDoc
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         // Create our byte[]. Rely on testInputStreamToByteArray() to make sure this is valid.
317
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 JavaDoc
330     {
331         final FileInputStream fin = new FileInputStream( m_testFile );
332         final byte[] in = IOUtil.toByteArray( fin );
333         // Create our byte[]. Rely on testInputStreamToByteArray() to make sure this is valid.
334
String JavaDoc str = IOUtil.toString( in );
335         assertEqualContent( in, str.getBytes() );
336         fin.close();
337     }
338
339
340     public void testByteArrayToOutputStream()
341         throws Exception JavaDoc
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         // Create our byte[]. Rely on testInputStreamToByteArray() to make sure this is valid.
348
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     //////////////////////////////////////////////////////
363
// xxxxxxxxx
364

365
366     private File newFile(String JavaDoc filename)
367         throws Exception JavaDoc
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 JavaDoc
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 JavaDoc
385     {
386         try
387         {
388             new PrintStream( output ).write( 0 );
389         }
390         catch( final Throwable JavaDoc 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 JavaDoc
399     {
400         try
401         {
402             new PrintWriter( output ).write( 'a' );
403         }
404         catch( final Throwable JavaDoc 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 JavaDoc
413     {
414         assertTrue( "Wrong output size: file.length()=" +
415                 file.length() + "!=" + FILE_SIZE + 1,
416                 file.length() == FILE_SIZE + 1 );
417
418         //assertTrue( "File would not delete", (file.delete() || ( !file.exists() )));
419
}
420 }
421
Popular Tags