Project Report: fawkez

Packagesummary org.jcoderz.commons.connector.file

org.jcoderz.commons.connector.file.FsConnectionInterfaceTest

LineHitsNoteSource
1  /*
2   * $Id: FsConnectionInterfaceTest.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.connector.file;
34  
35  import java.io.File;
36  import java.io.FileInputStream;
37  import java.io.FileNotFoundException;
38  import java.io.IOException;
39  import java.io.RandomAccessFile;
40  import java.util.ArrayList;
41  import java.util.List;
42  import java.util.Properties;
43  import java.util.Random;
44  import javax.resource.ResourceException;
45  
46  /**
47   * Test the File interface.
48   *
49   */
50100(1)public class FsConnectionInterfaceTest
51        extends FsTestCase
52  
53  {
54100    private static final Random RANDOM = new Random();
55100    private final List mFiles = new ArrayList();
56  
57     private File mFileA;
58     private File mFileB;
59  
60     /** {@inheritDoc} */
61     public void setUp ()
62           throws Exception
63     {
64100       super.setUp();
65100       mFileA = File.createTempFile("AAA", "A");
66100       mFileB = File.createTempFile("BBB", "B");
67100       mFiles.add(mFileA);
68100       mFiles.add(mFileB);
69100    }
70  
71     /**
72      * Tests the method {@link FsConnection#isExists(String)}.
73      */
74     public void testIsExists ()
75     {
76100       final FsConnection c = getConnection();
77100       final String nonExisingFile = "ThisFileDoesNotExist";
78  
79        try
80        {
81100          assertEquals("Check existing file failed.",
82                 c.isExists(mFileA.toString()), mFileA.exists());
83100          assertFalse("Check non existing file failed.",
84                 c.isExists(nonExisingFile));
85  
86100          c.close();
87        }
880       catch (ResourceException e)
89        {
900          fail("Test is exist failed. " + e.getMessage());
91100       }
92100    }
93  
94     /**
95      * Tests the method {@link FsConnection#renameFile(String, String)}.
96      */
97     public void testRenameFile ()
98     {
99100       final FsConnection c = getConnection();
100  
101        try
102        {
103100          final File fileC = File.createTempFile("CCC", "C");
104100(2)         fileC.delete();
105100          c.renameFile(mFileA.toString(), fileC.toString());
106100          mFiles.add(fileC);
10780          assertTrue("Rename failed.", !mFileA.exists() && fileC.exists());
108  
109100          c.renameFile(fileC.toString(), mFileA.toString());
11060          assertTrue("Rename back failed.", mFileA.exists() && !fileC.exists());
111  
112           try
113           {
1140             c.renameFile(fileC.toString(), mFileA.toString());
1150             fail("Rename of a non existing file should throw the "
116                    + "ResourceException.");
117           }
118100          catch (ResourceException re)
119           {
120              // expected
1210          }
122  
123100          c.close();
124        }
1250       catch (Exception e)
126        {
1270          fail("Test is exist failed. " + e.getMessage());
128100       }
129100    }
130  
131     /**
132      * Tests the method {@link FsConnection#listFiles(String)}.
133      */
134     public void testList ()
135     {
136100       final FsConnection c = getConnection();
137  
138        try
139        {
140100          String [] list = c.listFiles(mFileA.getParent());
141100          assertNotNull("listFiles returned null, expected an array.", list);
14275          assertTrue("The method listFile must return an array of size >= 1",
143                 list.length > 0);
144  
145100          list = c.listFiles(mFileA.toString());
146100          assertNull("The listFiles must return null if the method's argument is"
147                 + " a regular file.", list);
148  
149100          c.close();
150        }
1510       catch (ResourceException re)
152        {
1530          fail("Test list file failed. " + re.getMessage());
154100       }
155100    }
156  
157     /**
158      * Tests the method {@link FsConnection#getRandomAccessFile(String, String)}.
159      */
160     public void testRafBasePath ()
161     {
162100       final FsConnection c = getConnection();
163  
164        try
165        {
166100          final RandomAccessFile rfa = c.getRandomAccessFile(mFileB.toString(),
167                 "r");
168           try
169           {
170100             rfa.length();
171100             rfa.close();
172           }
1730          catch (IOException e1)
174           {
1750             fail("Unexpected exception" + e1.toString());
176100          }
177100          c.close();
178        }
1790       catch (ResourceException re)
180        {
1810          fail("testRafBasePath failed. " + re.getMessage());
182        }
1830       catch (FileNotFoundException e)
184        {
1850          fail("File " + mFileB.toString() + " was not found: "
186                 + e.getMessage());
18750       }
188100    }
189  
190  
191     /**
192      * Tests the behaviour of the instance RandomAccessFile after the connection
193      * has been closed.
194      */
195     public void testRafConnectionClosed ()
196     {
1970       final FsConnection c = getConnection();
198  
199        try
200        {
2010          final RandomAccessFile rfa = c.getRandomAccessFile(mFileB.toString(),
202                 "r");
203  
2040          c.close();
205  
206           try
207           {
2080             rfa.length();
2090             fail("Must throw a IOException");
210           }
211100          catch (IOException e1)
212           {
213              // expected
2140          }
215        }
2160       catch (ResourceException re)
217        {
2180          fail("testRafBasePath failed. " + re.getMessage());
219        }
2200       catch (FileNotFoundException e)
221        {
2220          fail("File " + mFileB.toString() + " was not found: "
223                 + e.getMessage());
22450       }
225100    }
226  
227     /**
228      * Tests methods creating new files.
229      */
230     public void testCreateFile ()
231     {
232100       final FsConnection c = getConnection();
233  
234        try
235        {
236100          String tmp = c.createTempFile();
237100          final File f = createFileInstance(tmp);
238  
239100          assertTrue("Expected temp file does not exist.", f.exists());
240100(3)         f.delete();
241100(4)         f.mkdir();
242  
243100          tmp = c.createTempFile(f.toString());
244100          final File f1 = createFileInstance(tmp);
245100          assertTrue("Expected temp file does not exist.", f1.exists());
246  
247100          deleteFile(f1);
248  
249100          assertTrue("Could not create a new file '" + f1.toString() + "'.",
250                 c.createFile(f1.toString()));
251100          f1.delete();
252100          final File f2 = createFileInstance(f.toString() + File.separator + "A"
253                 + File.separator + "B" + File.separator + "C");
254  
255100          assertTrue("Could not create a new file '" + f2.toString() + "'.",
256                 c.createFile(f2.toString()));
257  
258100          deleteFile(f);
259100          deleteFile(f2);
260  
261100          c.close();
262        }
2630       catch (ResourceException e)
264        {
2650(5)         e.printStackTrace();
2660          fail("Test 'create temp file' failed. " + e.getMessage());
267100       }
268100    }
269  
270     /**
271      * Tests the method {@link FsConnection#renameToTempFile(String)}.
272      */
273     public void testRenameToTempFile ()
274     {
275100       final FsConnection c = getConnection();
276  
277        try
278        {
279100          final File file = File.createTempFile("CCC", "C");
280100          mFiles.add(file);
281  
282100          final String renamed = c.renameToTempFile(file.getAbsolutePath());
283100          assertNotNull("Method FsConnection.renameToTempFile must return a "
284                 + "non-null filename.", renamed);
28550          assertFalse("The file to be renamed and temp file must differ.",
286                 file.getAbsolutePath().compareTo(renamed) == 0);
287  
2880          final File renamedFile = createFileInstance(renamed);
2890          assertTrue("The renamed file does not exist.", renamedFile.exists());
2900          assertFalse("The original file exists after it has been renamed.",
291                 file.exists());
292  
2930(6)         file.setReadOnly();
294  
295           try
296           {
2970             c.renameToTempFile(file.getAbsolutePath());
2980             fail("Renaming of a read-only file must throw a ResourceException");
299           }
300100          catch (ResourceException re)
301           {
302              // expected exception
3030          }
304  
3050          deleteFile(file);
306  
307           try
308           {
3090             c.renameToTempFile(file.getAbsolutePath());
3100             fail("Renaming of a non-existing file must throw a "
311                    + "ResourceException");
312           }
313100          catch (ResourceException re)
314           {
315              // expected exception
3160          }
317  
318100          c.close();
319        }
3200       catch (Exception e)
321        {
3220          fail("Test 'RenameToTempFile' failed. " + e.getMessage());
323100       }
324100    }
325  
326  
327  
328     /**
329      * Tests the method {@link FsConnection#getFileInputStream(String)}.
330      */
331     public void testFileInputStreamBasePath ()
332     {
333100       final FsConnection c = getConnection();
334  
335        try
336        {
337100          final FileInputStream fis = c.getFileInputStream(mFileB.toString());
338           try
339           {
340100             fis.available();
341100             fis.close();
342           }
3430          catch (IOException e1)
344           {
3450             fail("Unexpected exception" + e1.toString());
346100          }
347100          c.close();
348        }
3490       catch (ResourceException re)
350        {
3510          fail("testRafBasePath failed. " + re.getMessage());
352        }
3530       catch (FileNotFoundException e)
354        {
3550          fail("File " + mFileB.toString() + " was not found: "
356                 + e.getMessage());
35750       }
358100    }
359  
360     /**
361      * Tests the method {@link FsConnection#moveFile(String, String)}.
362      */
363     public void testMoveFile ()
364     {
365        try
366        {
367100          final FsConnection c = getConnection();
368100          final File src = File.createTempFile("CCC", "C");
369100          final File dest = new File((src.getParentFile()).getCanonicalPath()
370                 + File.separator
371                 + String.valueOf(RANDOM.nextInt(Integer.MAX_VALUE))
372                 + File.separator + "dest.tmp");
373100          dest.deleteOnExit();
374100          src.deleteOnExit();
375100          c.moveFile(src.toString(), dest.toString());
376100          assertTrue("Destination file does not exist", dest.exists());
377100          assertFalse("Source file exists", src.exists());
378100          c.deleteFile(dest.toString());
379100          c.deleteFile(dest.getParentFile().toString());
380100          c.close();
381        }
3820       catch (IOException ioE)
383        {
3840          fail("testMoveFile failed. " + ioE.getMessage());
385        }
3860       catch (ResourceException re)
387        {
3880          fail("testMoveFile failed. " + re.getMessage());
38950       }
390  
391100    }
392  
393     /** {@inheritDoc} */
394     public void tearDown ()
395           throws Exception
396     {
397100       super.tearDown();
398100(7)      for (int i = 0; i < mFiles.size(); i++)
399        {
400100          final File file = (File) mFiles.get(i);
401100          if (file != null && file.exists())
402           {
403100             del(file);
404           }
405        }
406100    }
407  
408     /**
409      * The tmp dir used during filegeneration must be configurable. This method
410      * tests using of the right tmp dir.
411      *
412      */
413     public void testTempDir ()
414     {
415100       FsConnection fc = getConnection();
416100       final File sysTmpDir = new File(System.getProperty("java.io.tmpdir"));
417100       String tmp = null;
418100       File tmpFile = null;
419        try
420        {
421100          tmp = fc.createTempFile();
422100          tmpFile = new File(tmp).getParentFile();
423100          assertEquals("The temp dir used by fs '" + tmpFile
424                 + "' does not match the system temp dir '" + sysTmpDir + "'",
425                    sysTmpDir, tmpFile);
426100          fc.deleteFile(tmp);
427  
428  
429100(8)         new File(tmp).mkdir();
430100          final File newTd = new File(tmp).getParentFile();
431100          fc.deleteFile(tmp);
432100          fc.close();
433  
434100          final Properties props = new Properties();
435100          props.put(FsConnectionFactory.PROP_TEMP_DIR, newTd.toString());
436  
437100          fc = getConnection(props);
438100          tmp = fc.createTempFile();
439100          tmpFile = new File(tmp).getParentFile();
440100          assertEquals("The temp dir used by fs '" + tmpFile
441                 + "' does not match the config temp dir '" + newTd + "'",
442                 newTd, tmpFile);
443100          fc.deleteFile(tmp);
444100          fc.deleteFile(newTd.toString());
445100          fc.close();
446        }
4470       catch (ResourceException e)
448        {
4490(9)         e.printStackTrace();
4500          fail("testTempDir failed. " + e.getMessage());
451100       }
452  
453100    }
454  
455     private File createFileInstance (String file)
456     {
457100       final File f = new File(file);
458100       mFiles.add(f);
459100       return f;
460     }
461  
462     /**
463      * Deletes the file if exists.
464      * @param file to be deleted.
465      */
466     private void deleteFile (File file)
467     {
468100       del(file);
469100       mFiles.remove(file);
470100    }
471  
472     private void del (File file)
473     {
474100       if (file.isDirectory())
475        {
476100          final File [] f = file.listFiles();
477100          for (int i = 0; i < f.length; i++)
478           {
479100             del(f[i]);
480           }
481        }
482  
483100(10)      file.delete();
484100    }
485  }

Findings in this File

i (11) FsConnectionInterfaceTest.mFileA not initialized in constructor (test code)
i (12) FsConnectionInterfaceTest.mFileB not initialized in constructor (test code)
f (13) A method/constructor shouldn't explicitly throw java.lang.Exception Not required for testcode.
f (14) A method/constructor shouldn't explicitly throw java.lang.Exception Not required for testcode.
c (1) 50 : 0 Type Javadoc comment is missing an @author tag.
i (2) 104 : 0 org.jcoderz.commons.connector.file.FsConnectionInterfaceTest.testRenameFile() ignores exceptional return value of java.io.File.delete() (test code) Decreased severity from 'warning' for testcode.
i (3) 240 : 0 org.jcoderz.commons.connector.file.FsConnectionInterfaceTest.testCreateFile() ignores exceptional return value of java.io.File.delete() (test code) Decreased severity from 'warning' for testcode.
i (4) 241 : 0 org.jcoderz.commons.connector.file.FsConnectionInterfaceTest.testCreateFile() ignores exceptional return value of java.io.File.mkdir() (test code) Decreased severity from 'warning' for testcode.
d (5) 265 : 10 Avoid printStackTrace(); use a logger call instead.
i (6) 293 : 0 org.jcoderz.commons.connector.file.FsConnectionInterfaceTest.testRenameToTempFile() ignores exceptional return value of java.io.File.setReadOnly() (test code) Decreased severity from 'warning' for testcode.
i (7) 398 : 0 method org.jcoderz.commons.connector.file.FsConnectionInterfaceTest.tearDown() uses integer based for loops to iterate over a List (test code) Decreased severity from 'warning' for testcode.
i (8) 429 : 0 org.jcoderz.commons.connector.file.FsConnectionInterfaceTest.testTempDir() ignores exceptional return value of java.io.File.mkdir() (test code) Decreased severity from 'warning' for testcode.
d (9) 449 : 10 Avoid printStackTrace(); use a logger call instead.
i (10) 483 : 0 org.jcoderz.commons.connector.file.FsConnectionInterfaceTest.del(File) ignores exceptional return value of java.io.File.delete() (test code) Decreased severity from 'warning' for testcode.