Project Report: fawkez

Packagesummary org.jcoderz.commons.util

org.jcoderz.commons.util.LazyFileOutputStreamTest

LineHitsNoteSource
1  /*
2   * $Id: LazyFileOutputStreamTest.java 1011 2008-06-16 17:57:36Z amandel $
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.File;
36  import java.io.FileInputStream;
37  import java.io.FileNotFoundException;
38  import java.io.FileOutputStream;
39  import java.io.IOException;
40  import java.io.InputStream;
41  
42  import junit.framework.TestCase;
43  
44  /**
45   * Test LazyFileOutputStream class.
46   *
47   * @author Andreas Mandel
48   */
49100 public class LazyFileOutputStreamTest
50      extends TestCase
51  {
52 (1)    public void testNewFile ()
53          throws IOException
54      {
55100(2)(3)        final File file = File.createTempFile("test", "new");
56          try
57          {
58100(4)            final LazyFileOutputStream out = new LazyFileOutputStream(file);
59100             out.write(1);
60100             assertFalse("New stream should never be buffered.",
61                  out.isBuffered());
62100             out.close();
63100             assertFalse("New stream should not be buffered after close.",
64                  out.isBuffered());
65100             assertContent(file, new byte[] {1});
66100(5)            assertTrue("Could not delete temp '" + file + "'.",
67                  file.delete());
68          }
69          finally
70          {
7150(6)            file.delete();
72100         }
73100     }
74  
75 (7)    public void testShorterFile ()
76          throws IOException
77      {
78100         final File file = File.createTempFile("test", "new");
79          try
80          {
81100             fillTestData(file);
82100(8)            final LazyFileOutputStream out = new LazyFileOutputStream(file);
83100             out.write(0);
84100(9)            assertTrue("Same data should be buffered.",
85                  out.isBuffered());
86100             out.close();
87100             assertFalse("Shorter new stream must not be buffered.",
88                  out.isBuffered());
89100(10)            assertContent(file, new byte[] { 0 });
90100             assertTrue("Could not delete temp '" + file + "'.",
91                  file.delete());
92          }
93          finally
94          {
9550(11)            file.delete();
96100         }
97100     }
98  
99 (12)    public void testLongerFile ()
100          throws IOException
101      {
102100         final File file = File.createTempFile("test", "new");
103          try
104          {
105100             fillTestData(file);
106  
107100(13)            final LazyFileOutputStream out = new LazyFileOutputStream(file);
108100             out.write(0);
109100             out.write(1);
110100             out.write(0);
111100             assertTrue("Same data should be buffered.",
112                  out.isBuffered());
113100             out.write(0);
114100             assertFalse("Longer new stream must not be buffered.",
115                  out.isBuffered());
116100             assertContent(file, new byte[] {0, 1, 0, 0});
117100             out.close();
118100             assertFalse("Longer new stream must not be buffered.",
119                  out.isBuffered());
120100             assertTrue("Could not delete temp '" + file + "'.",
121                  file.delete());
122          }
123          finally
124          {
12550(14)            file.delete();
126100         }
127100     }
128  
129 (15)    public void testSameFile ()
130          throws IOException
131      {
132100(16)        File file = File.createTempFile("test", "new");
133          try
134          {
135100             fillTestData(file);
136  
137100(17)            final LazyFileOutputStream out = new LazyFileOutputStream(file);
138100             out.write(0);
139100             out.write(1);
140100             out.write(0);
141100             assertTrue("Same data should be buffered.",
142                  out.isBuffered());
143100             out.close();
144100             assertTrue("Same data should be buffered.",
145                  out.isBuffered());
146100             assertContent(file, new byte[] {0, 1, 0});
147100             assertTrue("Could not delete temp '" + file + "'.",
148                  file.delete());
149          }
150          finally
151          {
15250(18)            file.delete();
153100         }
154100     }
155  
156 (19)    public void testDifferentFile ()
157          throws IOException
158      {
159100(20)        File file = File.createTempFile("test", "new");
160          try
161          {
162100             fillTestData(file);
163  
164100(21)            final LazyFileOutputStream out = new LazyFileOutputStream(file);
165100             out.write(0);
166100             out.write(1);
167100             assertTrue("Same data should be buffered.",
168                  out.isBuffered());
169100             out.write(1);
170100             assertFalse("Different data should not be buffered.",
171                  out.isBuffered());
172100             out.close();
173100             assertContent(file, new byte[] {0, 1, 1});
174100             assertTrue("Could not delete temp '" + file + "'.",
175                  file.delete());
176          }
177          finally
178          {
17950(22)            file.delete();
180100         }
181100     }
182  
183      private void fillTestData (File file)
184          throws FileNotFoundException, IOException
185      {
186100(23)(24)        final FileOutputStream fos = new FileOutputStream(file);
187100         fos.write(0);
188100         fos.write(1);
189100         fos.write(0);
190100         fos.close();
191100     }
192  
193      private void assertContent (File file, byte[] bs)
194          throws IOException
195      {
196100         final InputStream is = new FileInputStream(file);
197          try
198          {
199100             for (int i = 0; i < bs.length; i++)
200              {
201100                 final int c = is.read();
202100                 assertEquals("File content unexpected.", bs[i], c);
203              }
204100             assertEquals("Stream to long?", -1, is.read());
205          }
206          finally
207          {
20850             IoUtil.close(is);
209100         }
210100     }
211  }

Findings in this File

c (1) 52 : 5 Missing a Javadoc comment.
i (2) 55 : 55 The String literal "new" appears 5 times in this file; the first occurrence is on line 55 (test code)
i (3) 55 : 47 The String literal "test" appears 5 times in this file; the first occurrence is on line 55 (test code)
i (4) 58 : 0 org.jcoderz.commons.util.LazyFileOutputStreamTest.testNewFile() may fail to close stream on exception (test code)
i (5) 66 : 24 The String literal "Could not delete temp '" appears 5 times in this file; the first occurrence is on line 66 (test code)
i (6) 71 : 0 org.jcoderz.commons.util.LazyFileOutputStreamTest.testNewFile() ignores exceptional return value of java.io.File.delete() (test code) Decreased severity from 'warning' for testcode.
c (7) 75 : 5 Missing a Javadoc comment.
i (8) 82 : 0 org.jcoderz.commons.util.LazyFileOutputStreamTest.testShorterFile() may fail to close stream on exception (test code)
i (9) 84 : 24 The String literal "Same data should be buffered." appears 5 times in this file; the first occurrence is on line 84 (test code)
c (10) 89 : 45 '{' is followed by whitespace.
i (11) 95 : 0 org.jcoderz.commons.util.LazyFileOutputStreamTest.testShorterFile() ignores exceptional return value of java.io.File.delete() (test code) Decreased severity from 'warning' for testcode.
c (12) 99 : 5 Missing a Javadoc comment.
i (13) 107 : 0 org.jcoderz.commons.util.LazyFileOutputStreamTest.testLongerFile() may fail to close stream on exception (test code)
i (14) 125 : 0 org.jcoderz.commons.util.LazyFileOutputStreamTest.testLongerFile() ignores exceptional return value of java.io.File.delete() (test code) Decreased severity from 'warning' for testcode.
c (15) 129 : 5 Missing a Javadoc comment.
c (16) 132 : 14 Variable 'file' should be declared final.
i (17) 137 : 0 org.jcoderz.commons.util.LazyFileOutputStreamTest.testSameFile() may fail to close stream on exception (test code)
i (18) 152 : 0 org.jcoderz.commons.util.LazyFileOutputStreamTest.testSameFile() ignores exceptional return value of java.io.File.delete() (test code) Decreased severity from 'warning' for testcode.
c (19) 156 : 5 Missing a Javadoc comment.
c (20) 159 : 14 Variable 'file' should be declared final.
i (21) 164 : 0 org.jcoderz.commons.util.LazyFileOutputStreamTest.testDifferentFile() may fail to close stream on exception (test code)
i (22) 179 : 0 org.jcoderz.commons.util.LazyFileOutputStreamTest.testDifferentFile() ignores exceptional return value of java.io.File.delete() (test code) Decreased severity from 'warning' for testcode.
i (23) 186 : 0 Method org.jcoderz.commons.util.LazyFileOutputStreamTest.fillTestData(File) may fail to clean up stream or resource of type java.io.OutputStream (test code) Decreased severity from 'warning' for testcode.
i (24) 186 : 0 org.jcoderz.commons.util.LazyFileOutputStreamTest.fillTestData(File) may fail to close stream on exception (test code)