Project Report: fawkez

Packagesummary org.jcoderz.commons.util

org.jcoderz.commons.util.IoUtilTest

LineHitsNoteSource
1  /*
2   * $Id: IoUtilTest.java 1638 2010-09-13 19:24:53Z mgriffel $
3   *
4   * Copyright 2006, The jCoderZ.org Project. All rights reserved.
5   *
6   * Redistribution and use in source and binary forms, with or without
7   * modification, are permitted provided that the following conditions are
8   * met:
9   *
10   *    * Redistributions of source code must retain the above copyright
11   *      notice, this list of conditions and the following disclaimer.
12   *    * Redistributions in binary form must reproduce the above
13   *      copyright notice, this list of conditions and the following
14   *      disclaimer in the documentation and/or other materials
15   *      provided with the distribution.
16   *    * Neither the name of the jCoderZ.org Project nor the names of
17   *      its contributors may be used to endorse or promote products
18   *      derived from this software without specific prior written
19   *      permission.
20   *
21   * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS "AS IS" AND
22   * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
24   * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS
25   * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26   * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27   * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
28   * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
29   * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
30   * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
31   * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32   */
33  package org.jcoderz.commons.util;
34  
35  import java.io.BufferedInputStream;
36  import java.io.ByteArrayInputStream;
37  import java.io.ByteArrayOutputStream;
38  import java.io.File;
39  import java.io.FileInputStream;
40  import java.io.FileOutputStream;
41  import java.io.IOException;
42  import java.io.InputStream;
43  import java.io.OutputStream;
44  import java.io.RandomAccessFile;
45  import java.io.Reader;
46  import java.io.Writer;
47  import java.nio.ByteBuffer;
48  import java.nio.channels.Channel;
49  import java.nio.channels.ReadableByteChannel;
50  import java.util.Arrays;
51  import java.util.Random;
52  
53  import junit.framework.TestCase;
54  
55  
56  /**
57   * Unit Tests for the class {@link org.jcoderz.commons.util.IoUtil}.
58   *
59   * @author Michael Griffel
60   */
61100 public class IoUtilTest
62        extends TestCase
63  {
64100    private static final Random RANDOM_GENERATOR = new Random();
65     
66     private static final int GAP_SIZE = 10 * 1024;
67  
68     /**
69      * Tests the method {@link IoUtil#readFully(InputStream, int)}.
70      * @throws IOException in case of an unexpected I/O error.
71      */
72     public void testReadFully ()
73           throws IOException
74     {
75100       read1kBytes();
76100       read0Bytes();
77100       readAhead();
78100       underread();
79100    }
80  
81     /**
82      * Tests the method {@link IoUtil#close(InputStream)}.
83      */
84     public void testSafeCloseInputStream ()
85     {
86100       final InputStream in = new ByteArrayInputStream(new byte[0])
87100(1)         {
88              public void close ()
89                    throws IOException
90              {
91100                throw new IOException();
92              }
93           };
94  
95100       IoUtil.close(in);
96100       IoUtil.close(in);
97100       IoUtil.close((InputStream) null);
98100    }
99  
100     /**
101      * Tests the method {@link IoUtil#close(OutputStream)}.
102      */
103     public void testSafeCloseOutputStream ()
104     {
105100       final OutputStream out = new ByteArrayOutputStream()
106100(2)         {
107              public void close ()
108                    throws IOException
109              {
110100                throw new IOException();
111              }
112           };
113  
114100       IoUtil.close(out);
115100       IoUtil.close(out);
116100       IoUtil.close((OutputStream) null);
117100    }
118  
119     /**
120      * Tests the method {@link IoUtil#close(Reader)}.
121      */
122     public void testSafeCloseReader ()
123     {
124100       final Reader reader = new Reader()
125100(3)         {
126              public void close ()
127                    throws IOException
128              {
129100                throw new IOException();
130              }
131  
132              public int read (char[] cbuf, int off, int len)
133              {
1340                return 0;
135              }
136           };
137  
138100       IoUtil.close(reader);
139100       IoUtil.close(reader);
140100       IoUtil.close((Reader) null);
141100    }
142  
143     /**
144      * Tests the method {@link IoUtil#close(Writer)}.
145      */
146     public void testSafeCloseWriter ()
147     {
148100       final Writer writer = new Writer()
149100(4)         {
150              public void close ()
151                    throws IOException
152              {
153100                throw new IOException();
154              }
155  
156              public void flush ()
157              {
158                  // NOP
1590             }
160  
161              public void write (char[] cbuf, int off, int len)
162              {
163                  // NOP
1640             }
165           };
166  
167100       IoUtil.close(writer);
168100       IoUtil.close(writer);
169100       IoUtil.close((Writer) null);
170100    }
171  
172     /**
173      * Tests the method {@link IoUtil#close(RandomAccessFile)}.
174      * @throws Exception in case of an unexpected error.
175      */
176     public void testSafeCloseRandomAccessFile ()
177           throws Exception
178     {
179100       final File tmp = File.createTempFile("tmp", "tmp");
180100       tmp.deleteOnExit();
181100       final RandomAccessFile file = new RandomAccessFile(tmp, "r")
182100(5)         {
183              public void close ()
184                    throws IOException
185              {
186100                throw new IOException();
187              }
188           };
189  
190100       IoUtil.close(file);
191100       IoUtil.close(file);
192100       IoUtil.close((RandomAccessFile) null);
193100    }
194  
195     /**
196      * Tests the method {@link IoUtil#close(Channel)}.
197      * @throws Exception in case of an unexpected error.
198      */
199     public void testSafeCloseChannel ()
200           throws Exception
201     {
202100       final Channel channel = new ChannelImpl();
203100       IoUtil.close(channel);
204100       IoUtil.close(channel);
205100       IoUtil.close((Channel) null);
206100    }
207  
208     
209 (6)(7)(8)   public void testSkip() throws Exception {
210100        final File tmp = File.createTempFile("fawkez", "tmp");
211100(9)(10)(11)       FileOutputStream out = new FileOutputStream(tmp);
212100        out.write(1);
213100        out.write(new byte[GAP_SIZE]);
214100(12)       out.write(2);
215100        out.close();
216100(13)(14)       final InputStream in = new BufferedInputStream(new FileInputStream((tmp)));
217100(15)       int first = in.read();
218100        IoUtil.skip(in, GAP_SIZE); // instead of in.skip(GAP_SIZE);
219100(16)       int second = in.read();
220100(17)(18)       assertEquals(3, first + second);
221         
222100    }
223  
224     private void read1kBytes ()
225           throws IOException
226     {
227100       final byte[] inData = new byte[Constants.BYTES_PER_KILO_BYTE];
228100       final InputStream in = createRandomInputStream(inData);
229100       final byte[] outData = IoUtil.readFully(in, inData.length);
230100       assertTrue("bytes arrays should be equal",
231              Arrays.equals(inData, outData));
232100    }
233  
234     private void read0Bytes ()
235           throws IOException
236     {
237100       final byte[] inData = new byte[0];
238100       final InputStream in = createRandomInputStream(inData);
239100       final byte[] outData = IoUtil.readFully(in, inData.length);
240100       assertTrue("bytes arrays should be equal",
241              Arrays.equals(inData, outData));
242100    }
243  
244     private void readAhead ()
245     {
2460       final byte[] inData = new byte[Constants.BYTES_PER_KILO_BYTE];
2470       final InputStream in = createRandomInputStream(inData);
248        try
249        {
2500          IoUtil.readFully(in, inData.length + 1);
2510          fail("Expected IOException for reading more bytes than available");
252        }
253100       catch (IOException expected)
254        {
255100          assertNotNull("Expected a real exception.", expected);
2560       }
257100    }
258     private void underread ()
259           throws IOException
260     {
261100       final byte[] inData = new byte[Constants.BYTES_PER_KILO_BYTE];
262100       final InputStream in = createRandomInputStream(inData);
263100       final byte[] outData = IoUtil.readFully(in, inData.length - 1);
264100       final byte[] strippedInData = new byte[inData.length - 1];
265100       System.arraycopy(inData, 0, strippedInData, 0, inData.length - 1);
266100       assertTrue("bytes arrays should be equal except the last one",
267              Arrays.equals(strippedInData, outData));
268100    }
269  
270     private InputStream createRandomInputStream (byte[] inData)
271     {
272100       RANDOM_GENERATOR.nextBytes(inData);
273100       final InputStream in = new ByteArrayInputStream(inData);
274100(19)(20)      return in;
275     }
276  
277100    private static final class ChannelImpl
278           implements ReadableByteChannel
279     {
280100       private boolean mIsOpen = true;
281        
282        /** {@inheritDoc} */
283        public int read (ByteBuffer dst)
284        {
2850          return 0;
286        }
287        
288        /** {@inheritDoc} */
289        public void close ()
290              throws IOException
291        {
292100          if (mIsOpen)
293           {
294100             mIsOpen = false;
295           }
296           else
297           {
298100             throw new IOException();
299           }
300100       }
301  
302        /** {@inheritDoc} */
303        public boolean isOpen ()
304        {
3050          return mIsOpen;
306        }
307     }
308  
309  
310  }

Findings in this File

f (21) Use assertEquals(x, y) instead of assertTrue(x.equals(y)) Ok.
f (22) Use assertEquals(x, y) instead of assertTrue(x.equals(y)) Ok.
f (23) Use assertEquals(x, y) instead of assertTrue(x.equals(y)) Ok.
i (1) 87 : 0 The class org.jcoderz.commons.util.IoUtilTest$1 could be refactored into a named _static_ inner class (test code)
i (2) 106 : 0 The class org.jcoderz.commons.util.IoUtilTest$2 could be refactored into a named _static_ inner class (test code)
i (3) 125 : 0 The class org.jcoderz.commons.util.IoUtilTest$3 could be refactored into a named _static_ inner class (test code)
i (4) 149 : 0 The class org.jcoderz.commons.util.IoUtilTest$4 could be refactored into a named _static_ inner class (test code)
i (5) 182 : 0 The class org.jcoderz.commons.util.IoUtilTest$5 could be refactored into a named _static_ inner class (test code)
c (6) 209 : 4 Missing a Javadoc comment.
c (7) 209 : 24 '(' is not preceded with whitespace.
c (8) 209 : 44 '{' should be on a new line.
c (9) 211 : 25 Variable 'out' should be declared final.
i (10) 211 : 0 Method org.jcoderz.commons.util.IoUtilTest.testSkip() may fail to clean up stream or resource of type java.io.OutputStream (test code) Decreased severity from 'warning' for testcode.
i (11) 211 : 0 org.jcoderz.commons.util.IoUtilTest.testSkip() may fail to close stream on exception (test code)
c (12) 214 : 18 '2' is a magic number.
c (13) 216 : 0 Line is longer than 80 characters.
c (14) 216 : 76 This statement may have some unnecessary parentheses
c (15) 217 : 12 Variable 'first' should be declared final.
c (16) 219 : 12 Variable 'second' should be declared final.
c (17) 220 : 21 '3' is a magic number.
c (18) 220 : 8 JUnit assertions should include a message
i (19) 274 : 0 method org.jcoderz.commons.util.IoUtilTest.createRandomInputStream(byte[]) stores return result in local before immediately returning it (test code) Decreased severity from 'warning' for testcode.
c (20) 274 : 7 Consider simply returning the value vs storing it in local variable 'in'