root/trunk/test/java/org/jcoderz/commons/connector/file/FsConnectionServerTest.java

Revision 1011, 8.0 kB (checked in by amandel, 4 years ago)

Aligned svn keyword settings.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
Line 
1/*
2 * $Id$
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 */
33package org.jcoderz.commons.connector.file;
34
35import java.io.File;
36import java.io.RandomAccessFile;
37import java.util.Properties;
38import java.util.logging.Level;
39import java.util.logging.Logger;
40import javax.resource.ResourceException;
41import junit.framework.Test;
42import junit.framework.TestSuite;
43import org.jcoderz.commons.InternalErrorException;
44import org.jcoderz.commons.ServerTestCase;
45import org.jcoderz.commons.util.IoUtil;
46
47
48/**
49 *
50 * Server testcases for the commons file connector.
51 *
52 */
53public class FsConnectionServerTest
54      extends ServerTestCase
55{
56   /** The full qualified name of this class. */
57   private static final String CLASSNAME
58         = FsConnectionServerTest.class.getName();
59   /** The logger to use. */
60   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;
78      if (hasTestCases())
79      {
80         suite = getSuite(FsConnectionServerTest.class);
81      }
82      else
83      {
84         suite = new TestSuite(FsConnectionServerTest.class);
85      }
86      return suite;
87   }
88
89   /**
90    * Tests FsConnection lookup.
91    */
92   public void testConnectionLookup ()
93   {
94      try
95      {
96         final FsConnection fc = FsConnectionUtil.getFileSystemConnection();
97         assertNotNull(MSG_LOOKUP_FAILED, fc);
98         FsConnectionUtil.close(fc);
99      }
100      catch (InternalErrorException iee)
101      {
102         iee.log();
103         fail(MSG_LOOKUP_FAILED +  ", caught exception " + iee.getMessage());
104      }
105   }
106
107
108   /**
109    * Tests connection reusage.
110    */
111   public void testCloseConnection ()
112   {
113      for (int i = 0; i < MAX_CONNECTIONS; i++)
114      {
115         final FsConnection fc = FsConnectionUtil.getFileSystemConnection();
116         try
117         {
118            fc.close();
119         }
120         catch (ResourceException e)
121         {
122            logger.log(Level.SEVERE, MSG_ERROR_CLOSING, e);
123            fail(MSG_ERROR_CLOSING);
124         }
125      }
126   }
127
128   /**
129    * Tests {@link FsConnection#close()}.
130    */
131   public void testCloseConnection3 ()
132   {
133      for (int i = 0; i < MAX_CONNECTIONS; i++)
134      {
135         final FsConnection fc1 = FsConnectionUtil.getFileSystemConnection();
136         final FsConnection fc2 = FsConnectionUtil.getFileSystemConnection();
137         final FsConnection fc3 = FsConnectionUtil.getFileSystemConnection();
138         try
139         {
140            fc1.close();
141            fc2.close();
142            fc3.close();
143         }
144         catch (ResourceException e)
145         {
146            logger.log(Level.SEVERE, MSG_ERROR_CLOSING, e);
147            fail(MSG_ERROR_CLOSING);
148         }
149      }
150   }
151
152   /**
153    * Tests the methd {@link FsConnection#createTempFile()}.
154    *
155    */
156   public void testCreateTempFile ()
157   {
158      final FsConnection fc = FsConnectionUtil.getFileSystemConnection();
159      try
160      {
161         final String f = fc.createTempFile();
162         logger.finer("Created temp file " + f);
163         fc.deleteFile(f);
164         fc.close();
165      }
166      catch (ResourceException e)
167      {
168         final String msg = "testCreateTempFile failed";
169         logger.log(Level.SEVERE, msg, e);
170         fail(msg);
171      }
172   }
173
174   /**
175    * Tests handling of big files.
176    */
177   public void testBigFile ()
178   {
179      final String msg = "testBigFile failed";
180      final FsConnection fc = FsConnectionUtil.getFileSystemConnection();
181      RandomAccessFile raf = null;
182      try
183      {
184         final String f = fc.createTempFile();
185         logger.finer("Created temp file " + f);
186         final String copyF = f + "Copy";
187         raf = fc.getRandomAccessFile(f, "rw");
188
189         raf.seek(FsConnectionFactory.FILE_TRANSFER_CHUNK_SIZE_DEF_VALUE + 1);
190         raf.writeChars("Just a teststring!");
191         logger.finer("Writen " + raf.length() + " bytes into the file " + f);
192         IoUtil.close(raf);
193         raf = null;
194
195         fc.moveFile(f, copyF);
196         logger.finer("Moved " + f + " to " + copyF);
197         assertTrue("The destination file " + copyF
198               + " does not exist after the file moving.", fc.isExists(copyF));
199         assertFalse("Just moved file " + copyF + " does exits",
200               fc.isExists(f));
201
202         fc.deleteFile(copyF);
203         logger.finer("Deleted " + copyF);
204         assertFalse("Deleted file " + copyF + " exists.", fc.isExists(copyF));
205      }
206      catch (ResourceException r)
207      {
208         logger.log(Level.SEVERE, msg, r);
209         fail(msg);
210      }
211      catch (Exception e)
212      {
213         logger.log(Level.SEVERE, msg, e);
214         fail(msg);
215      }
216
217      IoUtil.close(raf);
218      FsConnectionUtil.close(fc);
219   }
220
221   /**
222    * Tests handling of big files.
223    */
224   public void testUserProperties ()
225   {
226      final String msg = "testUserProperties failed";
227      final Properties props = new Properties();
228      FsConnection fc = FsConnectionUtil.getFileSystemConnection();
229      RandomAccessFile raf = null;
230      try
231      {
232         final String tmpDir = fc.createTempFile();
233         fc.deleteFile(tmpDir);
234         fc.close();
235         final File dir = new File(tmpDir);
236         dir.mkdirs();
237
238         props.setProperty(FsConnectionFactory.PROP_TEMP_DIR, dir.toString());
239         props.setProperty(FsConnectionFactory.PROP_FILE_TRANSFER_CHUNK_SIZE,
240               String.valueOf(CHUNK_SIZE));
241         fc = FsConnectionUtil.getFileSystemConnection(props);
242
243         final String tmp = fc.createTempFile();
244         final File tmpFile = new File(tmp);
245         assertEquals("Newly created temp file " + tmpFile
246               + " has a wrong parent dir. Expected " + dir,
247                  tmpFile.getParentFile(), dir);
248
249         raf = fc.getRandomAccessFile(tmp, "rw");
250
251         raf.seek(CHUNK_SIZE + CHUNK_SIZE);
252         raf.writeChars("Just a teststring!");
253         logger.finer("Writen " + raf.length() + " bytes into the file "
254               + tmpFile);
255         IoUtil.close(raf);
256
257         final String copyF = tmp + "Copy";
258
259         fc.moveFile(tmp, copyF);
260         fc.deleteFile(copyF);
261         dir.delete();
262      }
263      catch (Exception e)
264      {
265         logger.log(Level.SEVERE, msg, e);
266         fail(msg);
267      }
268
269      IoUtil.close(raf);
270      FsConnectionUtil.close(fc);
271   }
272}
Note: See TracBrowser for help on using the browser.