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   */
50 (1)public class FsConnectionInterfaceTest
51        extends FsTestCase
52   
53  {
54     private static final Random RANDOM = new Random();
55     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     {
64        super.setUp();
65        mFileA = File.createTempFile("AAA""A");
66        mFileB = File.createTempFile("BBB""B");
67        mFiles.add(mFileA);
68        mFiles.add(mFileB);
69     }
70   
71     /**
72      * Tests the method {@link FsConnection#isExists(String)}.
73      */
74     public void testIsExists ()
75     {
76        final FsConnection c = getConnection();
77        final String nonExisingFile = "ThisFileDoesNotExist";
78   
79        try
80        {
81           assertEquals("Check existing file failed.",
82                 c.isExists(mFileA.toString()), mFileA.exists());
83           assertFalse("Check non existing file failed.",
84                 c.isExists(nonExisingFile));
85   
86           c.close();
87        }
88        catch (ResourceException e)
89        {
90           fail("Test is exist failed. " + e.getMessage());
91        }
92     }
93   
94     /**
95      * Tests the method {@link FsConnection#renameFile(String, String)}.
96      */
97     public void testRenameFile ()
98     {
99        final FsConnection c = getConnection();
100   
101        try
102        {
103           final File fileC = File.createTempFile("CCC""C");
104           fileC.delete();
105           c.renameFile(mFileA.toString(), fileC.toString());
106           mFiles.add(fileC);
107           assertTrue("Rename failed.", !mFileA.exists() && fileC.exists());
108   
109           c.renameFile(fileC.toString(), mFileA.toString());
110           assertTrue("Rename back failed.", mFileA.exists() && !fileC.exists());
111   
112           try
113           {
114              c.renameFile(fileC.toString(), mFileA.toString());
115              fail("Rename of a non existing file should throw the "
116                    + "ResourceException.");
117           }
118           catch (ResourceException re)
119           {
120              // expected
121           }
122   
123           c.close();
124        }
125        catch (Exception e)
126        {
127           fail("Test is exist failed. " + e.getMessage());
128        }
129     }
130   
131     /**
132      * Tests the method {@link FsConnection#listFiles(String)}.
133      */
134     public void testList ()
135     {
136        final FsConnection c = getConnection();
137   
138        try
139        {
140           String [] list = c.listFiles(mFileA.getParent());
141           assertNotNull("listFiles returned null, expected an array.", list);
142           assertTrue("The method listFile must return an array of size >= 1",
143                 list.length > 0);
144   
145           list = c.listFiles(mFileA.toString());
146           assertNull("The listFiles must return null if the method's argument is"
147                 + " a regular file.", list);
148   
149           c.close();
150        }
151        catch (ResourceException re)
152        {
153           fail("Test list file failed. " + re.getMessage());
154        }
155     }
156   
157     /**
158      * Tests the method {@link FsConnection#getRandomAccessFile(String, String)}.
159      */
160     public void testRafBasePath ()
161     {
162        final FsConnection c = getConnection();
163   
164        try
165        {
166           final RandomAccessFile rfa = c.getRandomAccessFile(mFileB.toString(),
167                 "r");
168           try
169           {
170              rfa.length();
171              rfa.close();
172           }
173           catch (IOException e1)
174           {
175              fail("Unexpected exception" + e1.toString());
176           }
177           c.close();
178        }
179        catch (ResourceException re)
180        {
181           fail("testRafBasePath failed. " + re.getMessage());
182        }
183        catch (FileNotFoundException e)
184        {
185           fail("File " + mFileB.toString() + " was not found: "
186                 + e.getMessage());
187        }
188     }
189   
190   
191     /**
192      * Tests the behaviour of the instance RandomAccessFile after the connection
193      * has been closed.
194      */
195     public void testRafConnectionClosed ()
196     {
197        final FsConnection c = getConnection();
198   
199        try
200        {
201           final RandomAccessFile rfa = c.getRandomAccessFile(mFileB.toString(),
202                 "r");
203   
204           c.close();
205   
206           try
207           {
208              rfa.length();
209              fail("Must throw a IOException");
210           }
211           catch (IOException e1)
212           {
213              // expected
214           }
215        }
216        catch (ResourceException re)
217        {
218           fail("testRafBasePath failed. " + re.getMessage());
219        }
220        catch (FileNotFoundException e)
221        {
222           fail("File " + mFileB.toString() + " was not found: "
223                 + e.getMessage());
224        }
225     }
226   
227     /**
228      * Tests methods creating new files.
229      */
230     public void testCreateFile ()
231     {
232        final FsConnection c = getConnection();
233   
234        try
235        {
236           String tmp = c.createTempFile();
237           final File f = createFileInstance(tmp);
238   
239           assertTrue("Expected temp file does not exist.", f.exists());
240           f.delete();
241           f.mkdir();
242   
243           tmp = c.createTempFile(f.toString());
244           final File f1 =  createFileInstance(tmp);
245           assertTrue("Expected temp file does not exist.", f1.exists());
246   
247           deleteFile(f1);
248   
249           assertTrue("Could not create a new file '" + f1.toString() + "'.",
250                 c.createFile(f1.toString()));
251           f1.delete();
252           final File f2 = createFileInstance(f.toString() + File.separator + "A"
253                 + File.separator + "B" + File.separator + "C");
254   
255           assertTrue("Could not create a new file '" + f2.toString() + "'.",
256                 c.createFile(f2.toString()));
257   
258           deleteFile(f);
259           deleteFile(f2);
260   
261           c.close();
262        }
263        catch (ResourceException e)
264        {
265 (2)         (2)e.printStackTrace();
266           fail("Test 'create temp file' failed. " + e.getMessage());
267        }
268     }
269   
270     /**
271      * Tests the method {@link FsConnection#renameToTempFile(String)}.
272      */
273     public void testRenameToTempFile ()
274     {
275        final FsConnection c = getConnection();
276   
277        try
278        {
279           final File file = File.createTempFile("CCC""C");
280           mFiles.add(file);
281   
282           final String renamed = c.renameToTempFile(file.getAbsolutePath());
283           assertNotNull("Method FsConnection.renameToTempFile must return a "
284                 + "non-null filename.", renamed);
285           assertFalse("The file to be renamed and temp file must differ.",
286                 file.getAbsolutePath().compareTo(renamed) == 0);
287   
288           final File renamedFile = createFileInstance(renamed);
289           assertTrue("The renamed file does not exist.", renamedFile.exists());
290           assertFalse("The original file exists after it has been renamed.",
291                 file.exists());
292   
293           file.setReadOnly();
294   
295           try
296           {
297              c.renameToTempFile(file.getAbsolutePath());
298              fail("Renaming of a read-only file must throw a ResourceException");
299           }
300           catch (ResourceException re)
301           {
302              // expected exception
303           }
304   
305           deleteFile(file);
306   
307           try
308           {
309              c.renameToTempFile(file.getAbsolutePath());
310              fail("Renaming of a non-existing file must throw a "
311                    + "ResourceException");
312           }
313           catch (ResourceException re)
314           {
315              // expected exception
316           }
317   
318           c.close();
319        }
320        catch (Exception e)
321        {
322           fail("Test 'RenameToTempFile' failed. " + e.getMessage());
323        }
324     }
325   
326   
327   
328     /**
329      * Tests the method {@link FsConnection#getFileInputStream(String)}.
330      */
331     public void testFileInputStreamBasePath ()
332     {
333        final FsConnection c = getConnection();
334   
335        try
336        {
337           final FileInputStream fis = c.getFileInputStream(mFileB.toString());
338           try
339           {
340              fis.available();
341              fis.close();
342           }
343           catch (IOException e1)
344           {
345              fail("Unexpected exception" + e1.toString());
346           }
347           c.close();
348        }
349        catch (ResourceException re)
350        {
351           fail("testRafBasePath failed. " + re.getMessage());
352        }
353        catch (FileNotFoundException e)
354        {
355           fail("File " + mFileB.toString() + " was not found: "
356                 + e.getMessage());
357        }
358     }
359   
360     /**
361      * Tests the method {@link FsConnection#moveFile(String, String)}.
362      */
363     public void testMoveFile ()
364     {
365        try
366        {
367           final FsConnection c = getConnection();
368           final File src = File.createTempFile("CCC""C");
369           final File dest = new File((src.getParentFile()).getCanonicalPath()
370                 + File.separator
371                 + String.valueOf(RANDOM.nextInt(Integer.MAX_VALUE))
372                 + File.separator + "dest.tmp");
373           dest.deleteOnExit();
374           src.deleteOnExit();
375           c.moveFile(src.toString(), dest.toString());
376           assertTrue("Destination file does not exist", dest.exists());
377           assertFalse("Source file exists", src.exists());
378           c.deleteFile(dest.toString());
379           c.deleteFile(dest.getParentFile().toString());
380           c.close();
381        }
382        catch (IOException ioE)
383        {
384           fail("testMoveFile failed. " + ioE.getMessage());
385        }
386        catch (ResourceException re)
387        {
388           fail("testMoveFile failed. " + re.getMessage());
389        }
390   
391     }
392   
393     /** {@inheritDoc} */
394     public void tearDown ()
395           throws Exception
396     {
397        super.tearDown();
398        for (int i = 0; i < mFiles.size(); i++)
399        {
400           final File file = (File) mFiles.get(i);
401           if (file != null && file.exists())
402           {
403              del(file);
404           }
405        }
406     }
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     {
415        FsConnection fc = getConnection();
416        final File sysTmpDir = new File(System.getProperty("java.io.tmpdir"));
417        String tmp = null;
418        File tmpFile = null;
419        try
420        {
421           tmp = fc.createTempFile();
422           tmpFile = new File(tmp).getParentFile();
423           assertEquals("The temp dir used by fs '" + tmpFile
424                 + "' does not match the system temp dir '" + sysTmpDir +  "'",
425                    sysTmpDir, tmpFile);
426           fc.deleteFile(tmp);
427   
428   
429           new File(tmp).mkdir();
430           final File newTd = new File(tmp).getParentFile();
431           fc.deleteFile(tmp);
432           fc.close();
433   
434           final Properties props = new Properties();
435           props.put(FsConnectionFactory.PROP_TEMP_DIR, newTd.toString());
436   
437           fc = getConnection(props);
438           tmp = fc.createTempFile();
439           tmpFile = new File(tmp).getParentFile();
440           assertEquals("The temp dir used by fs '" + tmpFile
441                 + "' does not match the config temp dir '" + newTd +  "'",
442                 newTd, tmpFile);
443           fc.deleteFile(tmp);
444           fc.deleteFile(newTd.toString());
445           fc.close();
446        }
447        catch (ResourceException e)
448        {
449 (3)         (3)e.printStackTrace();
450           fail("testTempDir failed. " + e.getMessage());
451        }
452   
453     }
454   
455     private File createFileInstance (String file)
456     {
457        final File f = new File(file);
458        mFiles.add(f);
459        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     {
468        del(file);
469        mFiles.remove(file);
470     }
471   
472     private void del (File file)
473     {
474        if (file.isDirectory())
475        {
476           final File [] f = file.listFiles();
477           for (int i = 0; i < f.length; i++)
478           {
479              del(f[i]);
480           }
481        }
482   
483        file.delete();
484     }
485  }
486   

Findings in this File

f (4) A method/constructor shouldn't explicitly throw java.lang.ExceptionNot required for testcode.