Project Report: fawkez

Packagesummary org.jcoderz.commons.connector.file

org.jcoderz.commons.connector.file.FsConnectionServerTest

LineHitsNoteSource
1  /*
2   * $Id: FsConnectionServerTest.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.RandomAccessFile;
37  import java.util.Properties;
38  import java.util.logging.Level;
39  import java.util.logging.Logger;
40  import javax.resource.ResourceException;
41  import junit.framework.Test;
42  import junit.framework.TestSuite;
43  import org.jcoderz.commons.InternalErrorException;
44  import org.jcoderz.commons.ServerTestCase;
45  import org.jcoderz.commons.util.IoUtil;
46  
47  
48  /**
49   *
50   * Server testcases for the commons file connector.
51   *
52   */
530(1)public class FsConnectionServerTest
54        extends ServerTestCase
55  {
56     /** The full qualified name of this class. */
570    private static final String CLASSNAME
58           = FsConnectionServerTest.class.getName();
59     /** The logger to use. */
600    private static final transient Logger logger = Logger.getLogger(CLASSNAME);
61  
62     private static final int MAX_CONNECTIONS = 100;
63  
64     private static final String MSG_LOOKUP_FAILED
65           = "FsConnection lookup failed.";
66     private static final String MSG_ERROR_CLOSING
67           = "Error while connection closing.";
68  
69     private static final long CHUNK_SIZE = 1000;
70  
71     /**
72      * Creates the testsuite.
73      * @return Test The testsuite
74      */
75     public static Test suite ()
76     {
77        final TestSuite suite;
780       if (hasTestCases())
79        {
800          suite = getSuite(FsConnectionServerTest.class);
81        }
82        else
83        {
840          suite = new TestSuite(FsConnectionServerTest.class);
85        }
860       return suite;
87     }
88  
89     /**
90      * Tests FsConnection lookup.
91      */
92     public void testConnectionLookup ()
93     {
94        try
95        {
960          final FsConnection fc = FsConnectionUtil.getFileSystemConnection();
970          assertNotNull(MSG_LOOKUP_FAILED, fc);
980          FsConnectionUtil.close(fc);
99        }
1000       catch (InternalErrorException iee)
101        {
1020          iee.log();
1030          fail(MSG_LOOKUP_FAILED + ", caught exception " + iee.getMessage());
1040       }
1050    }
106  
107  
108     /**
109      * Tests connection reusage.
110      */
111     public void testCloseConnection ()
112     {
1130       for (int i = 0; i < MAX_CONNECTIONS; i++)
114        {
1150          final FsConnection fc = FsConnectionUtil.getFileSystemConnection();
116           try
117           {
1180             fc.close();
119           }
1200          catch (ResourceException e)
121           {
1220             logger.log(Level.SEVERE, MSG_ERROR_CLOSING, e);
1230             fail(MSG_ERROR_CLOSING);
1240          }
125        }
1260    }
127  
128     /**
129      * Tests {@link FsConnection#close()}.
130      */
131     public void testCloseConnection3 ()
132     {
1330       for (int i = 0; i < MAX_CONNECTIONS; i++)
134        {
1350          final FsConnection fc1 = FsConnectionUtil.getFileSystemConnection();
1360          final FsConnection fc2 = FsConnectionUtil.getFileSystemConnection();
1370          final FsConnection fc3 = FsConnectionUtil.getFileSystemConnection();
138           try
139           {
1400             fc1.close();
1410             fc2.close();
1420             fc3.close();
143           }
1440          catch (ResourceException e)
145           {
1460             logger.log(Level.SEVERE, MSG_ERROR_CLOSING, e);
1470             fail(MSG_ERROR_CLOSING);
1480          }
149        }
1500    }
151  
152     /**
153      * Tests the methd {@link FsConnection#createTempFile()}.
154      *
155      */
156     public void testCreateTempFile ()
157     {
1580       final FsConnection fc = FsConnectionUtil.getFileSystemConnection();
159        try
160        {
1610          final String f = fc.createTempFile();
1620          logger.finer("Created temp file " + f);
1630          fc.deleteFile(f);
1640          fc.close();
165        }
1660       catch (ResourceException e)
167        {
1680          final String msg = "testCreateTempFile failed";
1690          logger.log(Level.SEVERE, msg, e);
1700          fail(msg);
1710       }
1720    }
173  
174     /**
175      * Tests handling of big files.
176      */
177     public void testBigFile ()
178     {
1790       final String msg = "testBigFile failed";
1800       final FsConnection fc = FsConnectionUtil.getFileSystemConnection();
1810       RandomAccessFile raf = null;
182        try
183        {
1840          final String f = fc.createTempFile();
1850          logger.finer("Created temp file " + f);
1860          final String copyF = f + "Copy";
1870          raf = fc.getRandomAccessFile(f, "rw");
188  
1890          raf.seek(FsConnectionFactory.FILE_TRANSFER_CHUNK_SIZE_DEF_VALUE + 1);
1900          raf.writeChars("Just a teststring!");
1910          logger.finer("Writen " + raf.length() + " bytes into the file " + f);
1920          IoUtil.close(raf);
1930          raf = null;
194  
1950          fc.moveFile(f, copyF);
1960          logger.finer("Moved " + f + " to " + copyF);
1970          assertTrue("The destination file " + copyF
198                 + " does not exist after the file moving.", fc.isExists(copyF));
1990          assertFalse("Just moved file " + copyF + " does exits",
200                 fc.isExists(f));
201  
2020          fc.deleteFile(copyF);
2030          logger.finer("Deleted " + copyF);
2040          assertFalse("Deleted file " + copyF + " exists.", fc.isExists(copyF));
205        }
2060       catch (ResourceException r)
207        {
2080          logger.log(Level.SEVERE, msg, r);
2090          fail(msg);
210        }
2110       catch (Exception e)
212        {
2130          logger.log(Level.SEVERE, msg, e);
2140          fail(msg);
2150       }
216  
2170       IoUtil.close(raf);
2180       FsConnectionUtil.close(fc);
2190    }
220  
221     /**
222      * Tests handling of big files.
223      */
224     public void testUserProperties ()
225     {
2260       final String msg = "testUserProperties failed";
2270       final Properties props = new Properties();
2280       FsConnection fc = FsConnectionUtil.getFileSystemConnection();
2290       RandomAccessFile raf = null;
230        try
231        {
2320          final String tmpDir = fc.createTempFile();
2330          fc.deleteFile(tmpDir);
2340          fc.close();
2350          final File dir = new File(tmpDir);
2360(2)         dir.mkdirs();
237  
2380          props.setProperty(FsConnectionFactory.PROP_TEMP_DIR, dir.toString());
2390          props.setProperty(FsConnectionFactory.PROP_FILE_TRANSFER_CHUNK_SIZE,
240                 String.valueOf(CHUNK_SIZE));
2410          fc = FsConnectionUtil.getFileSystemConnection(props);
242  
2430          final String tmp = fc.createTempFile();
2440          final File tmpFile = new File(tmp);
2450          assertEquals("Newly created temp file " + tmpFile
246                 + " has a wrong parent dir. Expected " + dir,
247                    tmpFile.getParentFile(), dir);
248  
2490          raf = fc.getRandomAccessFile(tmp, "rw");
250  
2510          raf.seek(CHUNK_SIZE + CHUNK_SIZE);
2520          raf.writeChars("Just a teststring!");
2530          logger.finer("Writen " + raf.length() + " bytes into the file "
254                 + tmpFile);
2550          IoUtil.close(raf);
256  
2570          final String copyF = tmp + "Copy";
258  
2590          fc.moveFile(tmp, copyF);
2600          fc.deleteFile(copyF);
2610(3)         dir.delete();
262        }
2630       catch (Exception e)
264        {
2650          logger.log(Level.SEVERE, msg, e);
2660          fail(msg);
2670       }
268  
2690       IoUtil.close(raf);
2700       FsConnectionUtil.close(fc);
2710    }
272  }

Findings in this File

c (1) 53 : 0 Type Javadoc comment is missing an @author tag.
i (2) 236 : 0 org.jcoderz.commons.connector.file.FsConnectionServerTest.testUserProperties() ignores exceptional return value of java.io.File.mkdirs() (test code) Decreased severity from 'warning' for testcode.
i (3) 261 : 0 org.jcoderz.commons.connector.file.FsConnectionServerTest.testUserProperties() ignores exceptional return value of java.io.File.delete() (test code) Decreased severity from 'warning' for testcode.