1 21 22 package org.apache.derbyTesting.functionTests.tests.jdbc4; 23 24 import java.io.File ; 25 import java.io.FileInputStream ; 26 import java.io.FileNotFoundException ; 27 import java.io.FileOutputStream ; 28 import java.io.IOException ; 29 import java.io.InputStream ; 30 import java.io.OutputStream ; 31 import java.io.PrintWriter ; 32 import java.net.InetAddress ; 33 import java.sql.Blob ; 34 import java.sql.Clob ; 35 import java.sql.Connection ; 36 import java.sql.DriverManager ; 37 import java.sql.PreparedStatement ; 38 import java.sql.SQLException ; 39 import java.sql.Statement ; 40 import org.apache.derby.iapi.error.StandardException; 41 import org.apache.derby.drda.NetworkServerControl; 42 import org.apache.derby.tools.ij; 43 import org.apache.derby.shared.common.reference.SQLState; 44 import org.apache.derbyTesting.functionTests.util.SQLStateConstants; 45 import org.apache.derbyTesting.functionTests.util.TestUtil; 46 47 51 52 public class TestConnectionMethods { 53 Connection conn = null; 54 55 private FileOutputStream serverOutput; 56 57 61 public TestConnectionMethods(Connection connIn) { 62 conn = connIn; 63 } 64 65 69 void t_createClob_Client() { 70 int c; 71 Clob clob; 72 try { 73 Statement s = conn.createStatement(); 74 s.execute("create table clobtable2(n int,clobcol CLOB)"); 75 PreparedStatement ps = conn.prepareStatement("insert into clobtable2" + 76 " values(?,?)"); 77 ps.setInt(1,1000); 78 clob = conn.createClob(); 79 File file = new File ("extin/short.txt"); 80 FileInputStream is = new FileInputStream (file); 81 OutputStream os = clob.setAsciiStream(1); 82 c = is.read(); 83 while(c>0) { 84 os.write(c); 85 c = is.read(); 86 } 87 ps.setClob(2, clob); 88 ps.executeUpdate(); 89 } catch(SQLException e) { 90 e.printStackTrace(); 91 } catch(FileNotFoundException fnfe){ 92 fnfe.printStackTrace(); 93 } catch(IOException ioe) { 94 ioe.printStackTrace(); 95 } catch(Exception e) { 96 e.printStackTrace(); 97 } 98 } 99 103 void t_createBlob_Client() { 104 int c; 105 Blob blob; 106 try { 107 Statement s = conn.createStatement(); 108 s.execute("create table blobtable2(n int,blobcol BLOB)"); 109 PreparedStatement ps = conn.prepareStatement("insert into blobtable2" + 110 " values(?,?)"); 111 ps.setInt(1,1000); 112 blob = conn.createBlob(); 113 File file = new File ("extin/short.txt"); 114 FileInputStream is = new FileInputStream (file); 115 OutputStream os = blob.setBinaryStream(1); 116 c = is.read(); 117 while(c>0) { 118 os.write(c); 119 c = is.read(); 120 } 121 ps.setBlob(2, blob); 122 ps.executeUpdate(); 123 } catch(SQLException e) { 124 e.printStackTrace(); 125 } catch(FileNotFoundException fnfe){ 126 fnfe.printStackTrace(); 127 } catch(IOException ioe) { 128 ioe.printStackTrace(); 129 } catch(Exception e) { 130 e.printStackTrace(); 131 } 132 } 133 134 137 void t_isValid() { 138 141 try { 142 conn.isValid(-1); System.out.println("FAIL: isValid(-1): " + 144 "Invalid argument execption not thrown"); 145 } catch (SQLException e) { 146 if(!StandardException.getSQLStateFromIdentifier( 147 SQLState.INVALID_API_PARAMETER).equals(e.getSQLState())) { 148 System.out.println("FAIL: isValid(-1): Unexpected SQLException" + 149 e); 150 } 151 } 152 153 156 try { 157 if (!conn.isValid(0)) { 158 System.out.println("FAIL: isValid(0): returned false"); 159 } 160 } catch(Exception e) { 161 System.out.println("FAIL: isValid(0): Unexpected exception: " + e); 162 } 163 164 167 try { 168 if (!conn.isValid(1)) { 169 System.out.println("FAIL: isValid(1): returned false"); 170 } 171 } catch(Exception e) { 172 System.out.println("FAIL: isValid(1): Unexpected exception: " + e); 173 } 174 175 178 try { 179 conn.close(); 180 } catch (SQLException e) { 181 System.out.println("FAIL: close failed: Unexpected exception: " + e); 182 } 183 184 try { 185 if (conn.isValid(0)) { 186 System.out.println("FAIL: isValid(0) on closed connection: " + 187 "returned true"); 188 } 189 } catch(Exception e) { 190 System.out.println("FAIL: isValid(0) on closed connection: " + 191 "Unexpected exception: " + e); 192 } 193 194 195 try { 196 conn = ij.startJBMS(); 197 } catch (Exception e) { 198 System.out.println("FAIL: failed to open new connection: " + 199 "Unexpected exception: " + e); 200 } 201 202 try { 203 if (!conn.isValid(0)) { 204 System.out.println("FAIL: isValid(0) on open connection: " + 205 "returned false"); 206 } 207 } catch(Exception e) { 208 System.out.println("FAIL: isValid(0) on open connection: " + 209 "Unexpected exception: " + e); 210 } 211 212 215 shutdownDatabase(); 216 217 218 try { 219 if (conn.isValid(0)) { 220 System.out.println("FAIL: isValid(0) on stopped database: " + 221 "returned true"); 222 } 223 } catch(Exception e) { 224 System.out.println("FAIL: isValid(0) on a stopped database: " + 225 "Unexpected exception: " + e); 226 } 227 228 229 try { 230 conn = ij.startJBMS(); 231 } catch (Exception e) { 232 System.out.println("FAIL: failed to re-start database: " + 233 "Unexpected exception: " + e); 234 } 235 236 237 try { 238 if (!conn.isValid(0)) { 239 System.out.println("FAIL: isValid(0) on new connection: " + 240 "returned false"); 241 } 242 } catch(Exception e) { 243 System.out.println("FAIL: isValid(0) on new connection: " + 244 "Unexpected exception: " + e); 245 } 246 247 250 if ( !usingEmbeddedClient() ) { 251 stopNetworkServer(); 252 253 254 try { 255 if (conn.isValid(0)) { 256 System.out.println("FAIL: isValid(0) on stopped database: " + 257 "returned true"); 258 } 259 } catch(Exception e) { 260 System.out.println("FAIL: isValid(0) on a stopped database: " + 261 "Unexpected exception: " + e); 262 } 263 264 268 startNetworkServer(); 269 270 try { 271 conn = ij.startJBMS(); 273 } catch (Exception e) { 274 System.out.println("FAIL: failed to re-start database: " + 275 "Unexpected exception: " + e); 276 e.printStackTrace(); 277 } 278 279 280 try { 281 if (!conn.isValid(0)) { 282 System.out.println("FAIL: isValid(0) on new connection: " + 283 "returned false"); 284 } 285 } catch(Exception e) { 286 System.out.println("FAIL: isValid(0) on new connection: " + 287 "Unexpected exception: " + e); 288 } 289 } 290 } 291 292 public void startTestConnectionMethods_Client() { 293 t_createClob_Client(); 294 t_createBlob_Client(); 295 t_isValid(); 296 } 297 298 public void startTestConnectionMethods_Embedded() { 299 t_isValid(); 300 } 301 302 305 private void shutdownDatabase() { 306 try { 307 String databaseName = System.getProperty("ij.dataSource.databaseName"); 309 if (databaseName != null) { 310 TestUtil.getConnection(databaseName, "shutdown=true"); 311 } 312 else { 313 System.out.println("FAIL: shutdownDatabase: " + 314 "property ij.dataSource.databaseName not defined"); 315 } 316 } catch (Exception e) { 317 } 319 } 320 321 322 325 private void stopNetworkServer() { 326 try { 327 NetworkServerControl networkServer = new NetworkServerControl(); 328 networkServer.shutdown(); 329 if (serverOutput != null) { 330 serverOutput.close(); 331 } 332 } catch(Exception e) { 333 System.out.println("INFO: Network server shutdown returned: " + e); 334 } 335 } 336 337 338 341 private void startNetworkServer() { 342 String hostName = null; 343 int serverPort; 344 345 String serverName = TestUtil.getHostName(); 348 if (serverName.equals("localhost")) { 349 serverPort = 1527; 350 } 351 else { 352 serverPort = 20000; 353 } 354 355 try { 356 String fileName = System.getProperty( "derby.system.home", "") 357 + "serverConsoleOutput.log"; 358 serverOutput = new FileOutputStream (fileName); 359 360 NetworkServerControl networkServer = 361 new NetworkServerControl(InetAddress.getByName(serverName), 362 serverPort); 363 networkServer.start(new PrintWriter (serverOutput)); 364 365 boolean started = false; 367 int retries = 10; while (!started && retries > 0) { 369 try { 370 Thread.sleep(1000); 372 networkServer.ping(); 373 374 started = true; 376 } catch(Exception e) { 377 System.out.println("INFO: ping returned: " + e); 378 retries--; 379 } 380 } 381 382 if (!started) { 384 System.out.println("FAIL: Failed to start network server"); 385 } 386 } catch (Exception e) { 387 System.out.println("FAIL: startNetworkServer got exception: " + e); 388 } 389 } 390 391 392 397 private static boolean usingEmbeddedClient() 398 { 399 return "embedded".equals( System.getProperty( "framework" ) ); 400 } 401 402 403 public static void main(String args[]) { 404 try { 405 ij.getPropertyArg(args); 408 409 Connection conn_main = ij.startJBMS(); 410 411 if ( usingEmbeddedClient() ) 412 { 413 TestConnectionMethods tcm = new TestConnectionMethods( conn_main ); 414 tcm.startTestConnectionMethods_Embedded(); 415 } 416 else { 418 TestConnectionMethods tcm1 = new TestConnectionMethods( conn_main ); 419 tcm1.startTestConnectionMethods_Client(); 420 } 421 422 conn_main.close(); 423 424 } catch(Exception e) { 425 System.out.println(""+e); 426 e.printStackTrace(); 427 } 428 } 429 } 430 | Popular Tags |