Project Report: fawkez

Packagesummary org.jcoderz.commons.connector.file

org.jcoderz.commons.connector.file.FsConnectionTest

LineHitsNoteSource
1  /*
2   * $Id: FsConnectionTest.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.lang.reflect.InvocationTargetException;
36  import java.lang.reflect.Method;
37  import java.util.logging.Level;
38  import java.util.logging.Logger;
39  import javax.resource.ResourceException;
40  
41  /**
42   * Tests File System connections.
43   *
44   */
45100(1)public class FsConnectionTest
46        extends FsTestCase
47  {
48     /** The full qualified name of this class. */
4975    private static final transient String CLASSNAME = FsConnectionTest.class
50           .getName();
51     /** The logger to use. */
52100    private static final transient Logger logger = Logger.getLogger(CLASSNAME);
53  
54     /** Tests the connector interfaces. */
55     public void testInterfaces ()
56     {
57        try
58        {
59100          getConnection().close();
60        }
610       catch (ResourceException e)
62        {
630          fail("Got an unexpected resource exception while testing File "
64                 + "Connector interfaces. " + e.getMessage());
65100       }
66100    }
67  
68     /**
69      * Tests whether the methods of the FsConnection throw a ResourceException
70      * after the connection has been closed. A client should not use a closed
71      * connection.
72      */
73     public void testClosed ()
74     {
75100       final String msg = "closed.";
76100       final FsConnection c = getConnection();
77100       closeConnection(c);
78  
79100       mustBeNotAvailable(msg, c);
80100    }
81  
82     /**
83      * Tests whether the methods of the FsConnection throw a ResourceException
84      * after the underlying managed connection has been cleaned up.
85      */
86     public void testCleanedUp ()
87     {
88100       final String msg = "cleaned up.";
89100       final FsConnection c = getConnection();
90  
91100       cleanUpManagedConnection();
92  
93100       mustBeNotAvailable(msg, c);
94100    }
95  
96     /**
97      * Tests whether the methods of the FsConnection throw a ResourceException
98      * after the underlying managed connection has been destroyed.
99      */
100     public void testDestroyed ()
101     {
102100       final String msg = "destroyed.";
103100       final FsConnection c = getConnection();
104  
105100       destroyManagedConnection();
106  
107100       mustBeNotAvailable(msg, c);
108100    }
109  
110     /**
111      * Tests close method on multiply connection instances created by the same
112      * managed connection factory.
113      */
114     public void testMultiplyConnectionsClose ()
115     {
116100       final String msg = "closed.";
117100       final FsConnection c1 = getConnection();
118100       final FsConnection c2 = getConnection();
119        try
120        {
121100          c1.close();
122100          c2.close();
123        }
1240       catch (ResourceException re)
125        {
1260          fail("MultiplyConnectionClose failed due to a ResourceException "
127                 + re.getMessage());
128100       }
129  
130100       mustBeNotAvailable(msg, c1);
131100       mustBeNotAvailable(msg, c2);
132100    }
133  
134     /**
135      * Tests close method on multiply connection instances created by the same
136      * managed connection factory.
137      */
138     public void testMultiplyConnectionsClosed ()
139     {
140100       final FsConnection c1 = getConnection();
141100       final FsConnection c2 = getConnection();
142        try
143        {
144100          c1.close();
145100          c2.close();
146        }
1470       catch (ResourceException re)
148        {
1490          fail("MultiplyConnectionClose failed due to a ResourceException "
150                 + re.getMessage());
151100       }
152100    }
153  
154     /**
155      * Tests whether the methods of the FsConnection throw a ResourceException
156      * after the underlying managed connection has been destroyed.
157      */
158     public void testMultiplyConnectionsDestroyed ()
159     {
160100       final String msg = "destroyed.";
161  
162100       final FsConnection c1 = getConnection();
163100       final FsConnection c2 = getConnection();
164  
165100       destroyManagedConnection();
166  
167100       mustBeNotAvailable(msg, c1);
168100       mustBeNotAvailable(msg, c2);
169100    }
170  
171     /**
172      * Tests whether the methods of the FsConnection throw a ResourceException
173      * after the underlying managed connection has been destroyed.
174      */
175     public void testMultiplyConnectionsCleanedUp ()
176     {
177100       final String msg = "cleaned up.";
178  
179100       final FsConnection c1 = getConnection();
180100       final FsConnection c2 = getConnection();
181  
182100       cleanUpManagedConnection();
183  
184100       mustBeNotAvailable(msg, c1);
185100       mustBeNotAvailable(msg, c2);
186100    }
187  
188     /** Calls destroy() on the Connection Manager. */
189     private void destroyManagedConnection ()
190     {
191        try
192        {
193100          getConnectionManager().destroy();
194        }
1950       catch (ResourceException e)
196        {
1970          fail("Got an unexpected resource exception while testing destroyed "
198                 + "connections. " + e.getMessage());
199100       }
200100    }
201  
202     /** Calls cleanUp() on the Connection Manager. */
203     private void cleanUpManagedConnection ()
204     {
205        try
206        {
207100          getConnectionManager().cleanUp();
208        }
2090       catch (ResourceException e)
210        {
2110          fail("Got an unexpected resource exception while testing cleaned "
212                 + "connections. " + e.getMessage());
213100       }
214100    }
215  
216  
217     /**
218      * Checks whether the given connection is not available
219      * @param msg1 message suffix
220      * @param c connection to be test.
221      */
222     private void mustBeNotAvailable (final String msg1, final FsConnection c)
223     {
224100       final Method [] m = FsConnection.class.getMethods();
225100       for (int i = 0; i < m.length; i++)
226        {
227100          final Class [] cl = m[i].getParameterTypes();
228100          final Object [] objs = new Object [cl.length];
229100          for (int j = 0; j < objs.length; j++)
230           {
231              try
232              {
233100                objs[j] = cl[j].newInstance();
234              }
2350             catch (Exception e1)
236              {
2370                fail("Failed to create a new instance of the class "
238                       + cl[j].getName());
239100             }
240           }
241  
242           try
243           {
244              //System.out.println("Invoking method " + m[i].getName());
2450             m[i].invoke(c, objs);
2460             fail("Method '" + m[i].getName()
247                    + "' did not throw a ResourceException after the "
248                    + "underlying connection had been " + msg1);
249           }
2500          catch (IllegalArgumentException e1)
251           {
2520             fail("Method " + m[i].getName()
253                    + " throw a IllegalArgumentException.");
254           }
2550          catch (IllegalAccessException e1)
256           {
2570             fail("Method " + m[i].getName()
258                    + " threw a IllegalAccessException.");
259           }
260100          catch (InvocationTargetException e1)
261           {
262100             assertTrue("Method '" + m[i].getName()
263                    + "' must throw a ResourceException after the "
264                    + "underlying connection has been " + msg1,
265                    (e1.getTargetException()
266                          instanceof javax.resource.spi.IllegalStateException));
2670          }
268        }
269100    }
270  
271     private void closeConnection (final FsConnection c)
272     {
273        try
274        {
275100          c.close();
276        }
2770       catch (ResourceException e)
278        {
2790          logger.log(Level.SEVERE,
280                 "Unexcpected error while connection's closing.", e);
2810          fail("Unexcpected error while connection's closing. "
282                 + e.getMessage());
283100       }
284100    }
285  }

Findings in this File

c (1) 45 : 0 Type Javadoc comment is missing an @author tag.