| 1 | | | |
| 2 | | | |
| 3 | | | |
| 4 | | | |
| 5 | | | |
| 6 | | | |
| 7 | | | |
| 8 | | | |
| 9 | | | |
| 10 | | | |
| 11 | | | |
| 12 | | | |
| 13 | | | |
| 14 | | | |
| 15 | | | |
| 16 | | | |
| 17 | | | |
| 18 | | | |
| 19 | | | |
| 20 | | | |
| 21 | | | |
| 22 | | | |
| 23 | | | |
| 24 | | | |
| 25 | | | |
| 26 | | | |
| 27 | | | |
| 28 | | | |
| 29 | | | |
| 30 | | | |
| 31 | | | |
| 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 | | | |
| 43 | | | |
| 44 | | | |
| 45 | 100 | (1) | public class FsConnectionTest |
| 46 | | | extends FsTestCase |
| 47 | | | { |
| 48 | | | |
| 49 | 75 | | private static final transient String CLASSNAME = FsConnectionTest.class |
| 50 | | | .getName(); |
| 51 | | | |
| 52 | 100 | | private static final transient Logger logger = Logger.getLogger(CLASSNAME); |
| 53 | | | |
| 54 | | | |
| 55 | | | public void testInterfaces () |
| 56 | | | { |
| 57 | | | try |
| 58 | | | { |
| 59 | 100 | | getConnection().close(); |
| 60 | | | } |
| 61 | 0 | | catch (ResourceException e) |
| 62 | | | { |
| 63 | 0 | | fail("Got an unexpected resource exception while testing File " |
| 64 | | | + "Connector interfaces. " + e.getMessage()); |
| 65 | 100 | | } |
| 66 | 100 | | } |
| 67 | | | |
| 68 | | | |
| 69 | | | |
| 70 | | | |
| 71 | | | |
| 72 | | | |
| 73 | | | public void testClosed () |
| 74 | | | { |
| 75 | 100 | | final String msg = "closed."; |
| 76 | 100 | | final FsConnection c = getConnection(); |
| 77 | 100 | | closeConnection(c); |
| 78 | | | |
| 79 | 100 | | mustBeNotAvailable(msg, c); |
| 80 | 100 | | } |
| 81 | | | |
| 82 | | | |
| 83 | | | |
| 84 | | | |
| 85 | | | |
| 86 | | | public void testCleanedUp () |
| 87 | | | { |
| 88 | 100 | | final String msg = "cleaned up."; |
| 89 | 100 | | final FsConnection c = getConnection(); |
| 90 | | | |
| 91 | 100 | | cleanUpManagedConnection(); |
| 92 | | | |
| 93 | 100 | | mustBeNotAvailable(msg, c); |
| 94 | 100 | | } |
| 95 | | | |
| 96 | | | |
| 97 | | | |
| 98 | | | |
| 99 | | | |
| 100 | | | public void testDestroyed () |
| 101 | | | { |
| 102 | 100 | | final String msg = "destroyed."; |
| 103 | 100 | | final FsConnection c = getConnection(); |
| 104 | | | |
| 105 | 100 | | destroyManagedConnection(); |
| 106 | | | |
| 107 | 100 | | mustBeNotAvailable(msg, c); |
| 108 | 100 | | } |
| 109 | | | |
| 110 | | | |
| 111 | | | |
| 112 | | | |
| 113 | | | |
| 114 | | | public void testMultiplyConnectionsClose () |
| 115 | | | { |
| 116 | 100 | | final String msg = "closed."; |
| 117 | 100 | | final FsConnection c1 = getConnection(); |
| 118 | 100 | | final FsConnection c2 = getConnection(); |
| 119 | | | try |
| 120 | | | { |
| 121 | 100 | | c1.close(); |
| 122 | 100 | | c2.close(); |
| 123 | | | } |
| 124 | 0 | | catch (ResourceException re) |
| 125 | | | { |
| 126 | 0 | | fail("MultiplyConnectionClose failed due to a ResourceException " |
| 127 | | | + re.getMessage()); |
| 128 | 100 | | } |
| 129 | | | |
| 130 | 100 | | mustBeNotAvailable(msg, c1); |
| 131 | 100 | | mustBeNotAvailable(msg, c2); |
| 132 | 100 | | } |
| 133 | | | |
| 134 | | | |
| 135 | | | |
| 136 | | | |
| 137 | | | |
| 138 | | | public void testMultiplyConnectionsClosed () |
| 139 | | | { |
| 140 | 100 | | final FsConnection c1 = getConnection(); |
| 141 | 100 | | final FsConnection c2 = getConnection(); |
| 142 | | | try |
| 143 | | | { |
| 144 | 100 | | c1.close(); |
| 145 | 100 | | c2.close(); |
| 146 | | | } |
| 147 | 0 | | catch (ResourceException re) |
| 148 | | | { |
| 149 | 0 | | fail("MultiplyConnectionClose failed due to a ResourceException " |
| 150 | | | + re.getMessage()); |
| 151 | 100 | | } |
| 152 | 100 | | } |
| 153 | | | |
| 154 | | | |
| 155 | | | |
| 156 | | | |
| 157 | | | |
| 158 | | | public void testMultiplyConnectionsDestroyed () |
| 159 | | | { |
| 160 | 100 | | final String msg = "destroyed."; |
| 161 | | | |
| 162 | 100 | | final FsConnection c1 = getConnection(); |
| 163 | 100 | | final FsConnection c2 = getConnection(); |
| 164 | | | |
| 165 | 100 | | destroyManagedConnection(); |
| 166 | | | |
| 167 | 100 | | mustBeNotAvailable(msg, c1); |
| 168 | 100 | | mustBeNotAvailable(msg, c2); |
| 169 | 100 | | } |
| 170 | | | |
| 171 | | | |
| 172 | | | |
| 173 | | | |
| 174 | | | |
| 175 | | | public void testMultiplyConnectionsCleanedUp () |
| 176 | | | { |
| 177 | 100 | | final String msg = "cleaned up."; |
| 178 | | | |
| 179 | 100 | | final FsConnection c1 = getConnection(); |
| 180 | 100 | | final FsConnection c2 = getConnection(); |
| 181 | | | |
| 182 | 100 | | cleanUpManagedConnection(); |
| 183 | | | |
| 184 | 100 | | mustBeNotAvailable(msg, c1); |
| 185 | 100 | | mustBeNotAvailable(msg, c2); |
| 186 | 100 | | } |
| 187 | | | |
| 188 | | | |
| 189 | | | private void destroyManagedConnection () |
| 190 | | | { |
| 191 | | | try |
| 192 | | | { |
| 193 | 100 | | getConnectionManager().destroy(); |
| 194 | | | } |
| 195 | 0 | | catch (ResourceException e) |
| 196 | | | { |
| 197 | 0 | | fail("Got an unexpected resource exception while testing destroyed " |
| 198 | | | + "connections. " + e.getMessage()); |
| 199 | 100 | | } |
| 200 | 100 | | } |
| 201 | | | |
| 202 | | | |
| 203 | | | private void cleanUpManagedConnection () |
| 204 | | | { |
| 205 | | | try |
| 206 | | | { |
| 207 | 100 | | getConnectionManager().cleanUp(); |
| 208 | | | } |
| 209 | 0 | | catch (ResourceException e) |
| 210 | | | { |
| 211 | 0 | | fail("Got an unexpected resource exception while testing cleaned " |
| 212 | | | + "connections. " + e.getMessage()); |
| 213 | 100 | | } |
| 214 | 100 | | } |
| 215 | | | |
| 216 | | | |
| 217 | | | |
| 218 | | | |
| 219 | | | @param |
| 220 | | | @param |
| 221 | | | |
| 222 | | | private void mustBeNotAvailable (final String msg1, final FsConnection c) |
| 223 | | | { |
| 224 | 100 | | final Method [] m = FsConnection.class.getMethods(); |
| 225 | 100 | | for (int i = 0; i < m.length; i++) |
| 226 | | | { |
| 227 | 100 | | final Class [] cl = m[i].getParameterTypes(); |
| 228 | 100 | | final Object [] objs = new Object [cl.length]; |
| 229 | 100 | | for (int j = 0; j < objs.length; j++) |
| 230 | | | { |
| 231 | | | try |
| 232 | | | { |
| 233 | 100 | | objs[j] = cl[j].newInstance(); |
| 234 | | | } |
| 235 | 0 | | catch (Exception e1) |
| 236 | | | { |
| 237 | 0 | | fail("Failed to create a new instance of the class " |
| 238 | | | + cl[j].getName()); |
| 239 | 100 | | } |
| 240 | | | } |
| 241 | | | |
| 242 | | | try |
| 243 | | | { |
| 244 | | | |
| 245 | 0 | | m[i].invoke(c, objs); |
| 246 | 0 | | fail("Method '" + m[i].getName() |
| 247 | | | + "' did not throw a ResourceException after the " |
| 248 | | | + "underlying connection had been " + msg1); |
| 249 | | | } |
| 250 | 0 | | catch (IllegalArgumentException e1) |
| 251 | | | { |
| 252 | 0 | | fail("Method " + m[i].getName() |
| 253 | | | + " throw a IllegalArgumentException."); |
| 254 | | | } |
| 255 | 0 | | catch (IllegalAccessException e1) |
| 256 | | | { |
| 257 | 0 | | fail("Method " + m[i].getName() |
| 258 | | | + " threw a IllegalAccessException."); |
| 259 | | | } |
| 260 | 100 | | catch (InvocationTargetException e1) |
| 261 | | | { |
| 262 | 100 | | 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)); |
| 267 | 0 | | } |
| 268 | | | } |
| 269 | 100 | | } |
| 270 | | | |
| 271 | | | private void closeConnection (final FsConnection c) |
| 272 | | | { |
| 273 | | | try |
| 274 | | | { |
| 275 | 100 | | c.close(); |
| 276 | | | } |
| 277 | 0 | | catch (ResourceException e) |
| 278 | | | { |
| 279 | 0 | | logger.log(Level.SEVERE, |
| 280 | | | "Unexcpected error while connection's closing.", e); |
| 281 | 0 | | fail("Unexcpected error while connection's closing. " |
| 282 | | | + e.getMessage()); |
| 283 | 100 | | } |
| 284 | 100 | | } |
| 285 | | | } |