KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > io > IOUtilsTestCase


1 /*
2  * Copyright 1999-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
17 package org.apache.commons.io;
18
19 import java.io.File JavaDoc;
20 import java.io.FileInputStream JavaDoc;
21 import java.io.FileOutputStream JavaDoc;
22 import java.io.FileReader JavaDoc;
23 import java.io.FileWriter JavaDoc;
24 import java.io.IOException JavaDoc;
25 import java.util.Arrays JavaDoc;
26
27 import org.apache.commons.io.testtools.*;
28
29 // Note: jdk1.2 dependency
30

31 /**
32  * This is used to test IOUtils for correctness. The following checks are performed:
33  * <ul>
34  * <li>The return must not be null, must be the same type and equals() to the method's second arg</li>
35  * <li>All bytes must have been read from the source (available() == 0)</li>
36  * <li>The source and destination content must be identical (byte-wise comparison check)</li>
37  * <li>The output stream must not have been closed (a byte/char is written to test this, and
38  * subsequent size checked)</li>
39  * </ul>
40  * Due to interdependencies in IOUtils and IOUtilsTestlet, one bug may cause
41  * multiple tests to fail.
42  *
43  * @author <a HREF="mailto:jefft@apache.org">Jeff Turner</a>
44  */

45 public class IOUtilsTestCase extends FileBasedTestCase {
46     /*
47      * Note: this is not particularly beautiful code. A better way to check for
48      * flush and close status would be to implement "trojan horse" wrapper
49      * implementations of the various stream classes, which set a flag when
50      * relevant methods are called. (JT)
51      */

52
53     private static final int FILE_SIZE = 1024 * 4 + 1;
54
55     private File JavaDoc m_testFile;
56
57     public void setUp()
58     {
59         try
60         {
61             getTestDirectory().mkdirs();
62             m_testFile = new File JavaDoc( getTestDirectory(), "file2-test.txt" );
63
64             createFile( m_testFile, FILE_SIZE );
65         }
66         catch( IOException JavaDoc ioe )
67         {
68             throw new RuntimeException JavaDoc( "Can't run this test because "
69                     + "environment could not be built: " + ioe.getMessage());
70         }
71     }
72
73     public void tearDown()
74     {
75         try
76         {
77             FileUtils.deleteDirectory( getTestDirectory() );
78         }
79         catch( IOException JavaDoc ioe )
80         {
81             // Ignore, because by this time, it is too late.
82
}
83     }
84
85     public IOUtilsTestCase( String JavaDoc name )
86     {
87         super( name );
88     }
89
90     /** Assert that the contents of two byte arrays are the same. */
91     private void assertEqualContent( byte[] b0, byte[] b1 )
92         throws IOException JavaDoc
93     {
94         assertTrue( "Content not equal according to java.util.Arrays#equals()", Arrays.equals( b0, b1 ) );
95     }
96
97     public void testInputStreamToString()
98         throws Exception JavaDoc
99     {
100         FileInputStream JavaDoc fin = new FileInputStream JavaDoc( m_testFile );
101         try {
102             String JavaDoc out = IOUtils.toString( fin );
103             assertNotNull( out );
104             assertTrue( "Not all bytes were read", fin.available() == 0 );
105             assertTrue( "Wrong output size: out.length()=" + out.length() +
106                         "!=" + FILE_SIZE, out.length() == FILE_SIZE );
107         } finally {
108             fin.close();
109         }
110     }
111
112     public void testReaderToString()
113         throws Exception JavaDoc
114     {
115         FileReader JavaDoc fin = new FileReader JavaDoc( m_testFile );
116         try {
117             String JavaDoc out = IOUtils.toString( fin );
118             assertNotNull( out );
119             assertTrue( "Wrong output size: out.length()=" +
120                         out.length() + "!=" + FILE_SIZE,
121                         out.length() == FILE_SIZE );
122         } finally {
123             fin.close();
124         }
125     }
126
127     public void testStringToOutputStream()
128         throws Exception JavaDoc
129     {
130         File JavaDoc destination = newFile( "copy5.txt" );
131         FileReader JavaDoc fin = new FileReader JavaDoc( m_testFile );
132         String JavaDoc str;
133         try {
134             // Create our String. Rely on testReaderToString() to make sure this is valid.
135
str = IOUtils.toString( fin );
136         } finally {
137             fin.close();
138         }
139         
140         FileOutputStream JavaDoc fout = new FileOutputStream JavaDoc( destination );
141         try {
142             CopyUtils.copy( str, fout );
143             //Note: this method *does* flush. It is equivalent to:
144
// OutputStreamWriter _out = new OutputStreamWriter(fout);
145
// CopyUtils.copy( str, _out, 4096 ); // copy( Reader, Writer, int );
146
// _out.flush();
147
// out = fout;
148
// note: we don't flush here; this IOUtils method does it for us
149

150             checkFile( destination, m_testFile );
151             checkWrite( fout );
152         } finally {
153             fout.close();
154         }
155         deleteFile( destination );
156     }
157
158     public void testStringToWriter()
159         throws Exception JavaDoc
160     {
161         File JavaDoc destination = newFile( "copy6.txt" );
162         FileReader JavaDoc fin = new FileReader JavaDoc( m_testFile );
163         String JavaDoc str;
164         try {
165             // Create our String. Rely on testReaderToString() to make sure this is valid.
166
str = IOUtils.toString( fin );
167         } finally {
168             fin.close();
169         }
170         
171         FileWriter JavaDoc fout = new FileWriter JavaDoc( destination );
172         try {
173             CopyUtils.copy( str, fout );
174             fout.flush();
175
176             checkFile( destination, m_testFile );
177             checkWrite( fout );
178         } finally {
179             fout.close();
180         }
181         deleteFile( destination );
182     }
183
184     public void testInputStreamToByteArray()
185         throws Exception JavaDoc
186     {
187         FileInputStream JavaDoc fin = new FileInputStream JavaDoc( m_testFile );
188         try {
189             byte[] out = IOUtils.toByteArray( fin );
190             assertNotNull( out );
191             assertTrue( "Not all bytes were read", fin.available() == 0 );
192             assertTrue( "Wrong output size: out.length=" + out.length +
193                         "!=" + FILE_SIZE, out.length == FILE_SIZE );
194             assertEqualContent( out, m_testFile );
195         } finally {
196             fin.close();
197         }
198     }
199
200     public void testStringToByteArray()
201         throws Exception JavaDoc
202     {
203         FileReader JavaDoc fin = new FileReader JavaDoc( m_testFile );
204         try {
205             // Create our String. Rely on testReaderToString() to make sure this is valid.
206
String JavaDoc str = IOUtils.toString( fin );
207
208             byte[] out = IOUtils.toByteArray( str );
209             assertEqualContent( str.getBytes(), out );
210         } finally {
211             fin.close();
212         }
213     }
214
215     public void testByteArrayToWriter()
216         throws Exception JavaDoc
217     {
218         File JavaDoc destination = newFile( "copy7.txt" );
219         FileInputStream JavaDoc fin = new FileInputStream JavaDoc( m_testFile );
220         byte[] in;
221         try {
222             // Create our byte[]. Rely on testInputStreamToByteArray() to make sure this is valid.
223
in = IOUtils.toByteArray( fin );
224         } finally {
225             fin.close();
226         }
227
228         FileWriter JavaDoc fout = new FileWriter JavaDoc( destination );
229         try {
230             CopyUtils.copy( in, fout );
231             fout.flush();
232             checkFile( destination, m_testFile );
233             checkWrite( fout );
234         } finally {
235             fout.close();
236         }
237         deleteFile( destination );
238     }
239
240     public void testByteArrayToString()
241         throws Exception JavaDoc
242     {
243         FileInputStream JavaDoc fin = new FileInputStream JavaDoc( m_testFile );
244         try {
245             byte[] in = IOUtils.toByteArray( fin );
246             // Create our byte[]. Rely on testInputStreamToByteArray() to make sure this is valid.
247
String JavaDoc str = IOUtils.toString( in );
248             assertEqualContent( in, str.getBytes() );
249         } finally {
250             fin.close();
251         }
252     }
253
254     public void testByteArrayToOutputStream()
255         throws Exception JavaDoc
256     {
257         File JavaDoc destination = newFile( "copy8.txt" );
258         FileInputStream JavaDoc fin = new FileInputStream JavaDoc( m_testFile );
259         byte[] in;
260         try {
261             // Create our byte[]. Rely on testInputStreamToByteArray() to make sure this is valid.
262
in = IOUtils.toByteArray( fin );
263         } finally {
264             fin.close();
265         }
266
267         FileOutputStream JavaDoc fout = new FileOutputStream JavaDoc( destination );
268         try {
269             CopyUtils.copy( in, fout );
270
271             fout.flush();
272
273             checkFile( destination, m_testFile );
274             checkWrite( fout );
275         } finally {
276             fout.close();
277         }
278         deleteFile( destination );
279     }
280
281
282 }
283
284
Popular Tags