KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sourceforge > cobertura > util > IOUtilTest


1 /*
2  * Cobertura - http://cobertura.sourceforge.net/
3  *
4  * Copyright (C) 2005 Grzegorz Lukasik
5  *
6  * Cobertura is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published
8  * by the Free Software Foundation; either version 2 of the License,
9  * or (at your option) any later version.
10  *
11  * Cobertura is distributed in the hope that it will be useful, but
12  * WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with Cobertura; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19  * USA
20  */

21 package net.sourceforge.cobertura.util;
22
23 import java.io.ByteArrayInputStream JavaDoc;
24 import java.io.ByteArrayOutputStream JavaDoc;
25 import java.io.File JavaDoc;
26 import java.io.FileInputStream JavaDoc;
27 import java.io.FileOutputStream JavaDoc;
28 import java.io.IOException JavaDoc;
29 import java.io.InputStream JavaDoc;
30 import java.io.OutputStream JavaDoc;
31
32 import junit.framework.TestCase;
33
34 /**
35  * @author Grzegorz Lukasik
36  */

37 public class IOUtilTest extends TestCase
38 {
39
40     private static final byte[] emptyByteArray = new byte[] {};
41
42     private static final byte[] singletonByteArray = new byte[] { 7 };
43
44     private static final byte[] smallByteArray = new byte[] { 1, 0, 2, -128,
45             127 };
46
47     private File JavaDoc createFileWithData(byte[] data) throws IOException JavaDoc
48     {
49         File JavaDoc file = File.createTempFile("IOUtilTest", ".txt");
50         file.deleteOnExit();
51         OutputStream JavaDoc src = new FileOutputStream JavaDoc(file);
52         src.write(data);
53         src.close();
54         return file;
55     }
56
57     public void testMoveFile() throws IOException JavaDoc
58     {
59         // Move file if destination does not exist
60
File JavaDoc srcFile = createFileWithData(smallByteArray);
61         File JavaDoc destFile = createFileWithData(emptyByteArray);
62         destFile.delete();
63         assertTrue(!destFile.isFile());
64         IOUtil.moveFile(srcFile, destFile);
65         assertTrue(destFile.isFile());
66         InputStream JavaDoc in = new FileInputStream JavaDoc(destFile);
67         for (int i = 0; i < smallByteArray.length; i++)
68             assertEquals(smallByteArray[i], (byte)in.read());
69         assertEquals(-1, in.read());
70         in.close();
71
72         // Move file if destination exists
73
srcFile = createFileWithData(singletonByteArray);
74         destFile = createFileWithData(smallByteArray);
75         IOUtil.moveFile(srcFile, destFile);
76         assertTrue(destFile.isFile());
77         in = new FileInputStream JavaDoc(destFile);
78         for (int i = 0; i < singletonByteArray.length; i++)
79             assertEquals(singletonByteArray[i], (byte)in.read());
80         assertEquals(-1, in.read());
81         in.close();
82
83         // Pass null values
84
srcFile = createFileWithData(smallByteArray);
85         try
86         {
87             IOUtil.moveFile(srcFile, null);
88             fail("Expected NullPointerException");
89         }
90         catch (NullPointerException JavaDoc ex)
91         {
92         }
93
94         destFile = createFileWithData(smallByteArray);
95         try
96         {
97             IOUtil.moveFile(null, destFile);
98             fail("Expected NullPointerException");
99         }
100         catch (NullPointerException JavaDoc ex)
101         {
102         }
103     }
104
105     public void testCopyStream() throws IOException JavaDoc
106     {
107         ByteArrayInputStream JavaDoc in = new ByteArrayInputStream JavaDoc(smallByteArray);
108         ByteArrayOutputStream JavaDoc out = new ByteArrayOutputStream JavaDoc();
109         IOUtil.copyStream(in, out);
110         assertEquals(smallByteArray, out.toByteArray());
111
112         in = new ByteArrayInputStream JavaDoc(singletonByteArray);
113         out = new ByteArrayOutputStream JavaDoc();
114         IOUtil.copyStream(in, out);
115         assertEquals(singletonByteArray, out.toByteArray());
116
117         in = new ByteArrayInputStream JavaDoc(emptyByteArray);
118         out = new ByteArrayOutputStream JavaDoc();
119         IOUtil.copyStream(in, out);
120         assertEquals(emptyByteArray, out.toByteArray());
121
122         byte[] bigArray = generateBigByteArray();
123         in = new ByteArrayInputStream JavaDoc(bigArray);
124         out = new ByteArrayOutputStream JavaDoc();
125         IOUtil.copyStream(in, out);
126         assertEquals(bigArray, out.toByteArray());
127
128         try
129         {
130             IOUtil.copyStream(null, new ByteArrayOutputStream JavaDoc());
131             fail("NullPointerException expected");
132         }
133         catch (NullPointerException JavaDoc ex)
134         {
135         }
136
137         try
138         {
139             IOUtil.copyStream(new ByteArrayInputStream JavaDoc(bigArray), null);
140             fail("NullPointerException expected");
141         }
142         catch (NullPointerException JavaDoc ex)
143         {
144         }
145     }
146
147     public void testFillByteArrayFromInputStream() throws IOException JavaDoc
148     {
149         byte[] out = IOUtil
150                 .createByteArrayFromInputStream(new ByteArrayInputStream JavaDoc(
151                         smallByteArray));
152         assertEquals(smallByteArray, out);
153
154         out = IOUtil.createByteArrayFromInputStream(new ByteArrayInputStream JavaDoc(
155                 emptyByteArray));
156         assertEquals(emptyByteArray, out);
157
158         out = IOUtil.createByteArrayFromInputStream(new ByteArrayInputStream JavaDoc(
159                 singletonByteArray));
160         assertEquals(singletonByteArray, out);
161
162         byte[] bigArray = generateBigByteArray();
163         out = IOUtil.createByteArrayFromInputStream(new ByteArrayInputStream JavaDoc(
164                 bigArray));
165         assertEquals(bigArray, out);
166
167         try
168         {
169             IOUtil.createByteArrayFromInputStream(null);
170             fail("NullPointerException expected");
171         }
172         catch (NullPointerException JavaDoc ex)
173         {
174         }
175     }
176
177     private void assertEquals(byte[] first, byte[] second)
178     {
179         assertEquals(first.length, second.length);
180         for (int i = 0; i < first.length; i++)
181         {
182             assertEquals(first[i], second[i]);
183         }
184     }
185
186     private byte[] generateBigByteArray()
187     {
188         byte[] bigArray = new byte[1000000];
189         for (int i = 0; i < bigArray.length; i++)
190         {
191             bigArray[i] = (byte)i;
192         }
193         return bigArray;
194     }
195
196 }
197
Popular Tags