1 21 22 package org.apache.derbyTesting.functionTests.tests.derbynet; 23 24 import org.apache.derby.jdbc.EmbeddedDataSource; 25 import org.apache.derby.jdbc.EmbeddedConnectionPoolDataSource; 26 import org.apache.derby.jdbc.EmbeddedXADataSource; 27 28 import java.sql.Connection ; 29 import java.sql.ResultSet ; 30 import java.sql.PreparedStatement ; 31 import java.sql.CallableStatement ; 32 import java.sql.Statement ; 33 import java.sql.SQLException ; 34 import java.sql.DriverManager ; 35 36 import javax.sql.DataSource ; 37 import javax.sql.XADataSource ; 38 import javax.sql.PooledConnection ; 39 import javax.sql.XAConnection ; 40 import javax.sql.ConnectionPoolDataSource ; 41 import javax.transaction.xa.XAResource ; 42 import javax.transaction.xa.XAException ; 43 import javax.transaction.xa.Xid ; 44 import javax.sql.ConnectionEventListener ; 45 import javax.sql.ConnectionEvent ; 46 import org.apache.derby.tools.JDBCDisplayUtil; 47 import org.apache.derby.tools.ij; 48 import org.apache.derby.drda.NetworkServerControl; 49 import org.apache.derbyTesting.functionTests.util.TestUtil; 50 import java.io.*; 51 import java.net.InetAddress ; 52 import java.util.Hashtable ; 53 import java.util.Properties ; 54 55 import javax.naming.*; 56 import javax.naming.directory.*; 57 58 import java.lang.reflect.*; 59 60 public class dataSourcePermissions_net extends org.apache.derbyTesting.functionTests.tests.jdbcapi.dataSourcePermissions 61 { 62 63 private static int NETWORKSERVER_PORT; 64 private static String hostName; 65 private static NetworkServerControl networkServer = null; 66 67 private static FileOutputStream serverOutput; 68 69 public static void main(String [] args) throws Exception { 70 71 hostName = TestUtil.getHostName(); 72 if (hostName.equals("localhost")) 73 NETWORKSERVER_PORT = 20000; 74 else 75 NETWORKSERVER_PORT = 1527; 76 77 ij.getPropertyArg(args); 79 80 86 try { 87 TestUtil.loadDriver(); 88 } catch (Exception e) { 89 e.printStackTrace(); 90 } 91 92 String fileName = System.getProperty( "derby.system.home", "") 93 + "serverConsoleOutput.log"; 94 serverOutput = new FileOutputStream(fileName); 95 96 if (hostName.equals("localhost")) 97 { 98 networkServer = new NetworkServerControl(InetAddress.getByName("localhost"),NETWORKSERVER_PORT); 100 networkServer.start(new PrintWriter(serverOutput)); 101 102 if (!isServerStarted(networkServer, 60)) 104 System.exit(-1); 105 } 106 107 108 try { 110 dataSourcePermissions_net tester = 111 new dataSourcePermissions_net(); 112 tester.setProperties(); 113 tester.runTest(); 114 if (TestUtil.isDerbyNetClientFramework()) 115 tester.testClientDataSourceProperties(); 116 new dataSourcePermissions_net().cleanUp(); 117 118 } catch (Exception e) { 119 System.out.println("FAIL - Exiting due to unexpected error: " + 124 e.getMessage()); 125 e.printStackTrace(); 126 } 127 128 if (hostName.equals("localhost")) 130 { 131 132 networkServer.shutdown(); 133 134 Thread.sleep(5000); 137 } 138 System.out.println("Completed dataSourcePermissions_net"); 139 140 serverOutput.close(); 141 142 } 143 144 145 public dataSourcePermissions_net() { 146 147 148 } 149 150 151 public void setProperties() { 152 System.setProperty("database", 154 TestUtil.getJdbcUrlPrefix(hostName, 155 NETWORKSERVER_PORT) + 156 "wombat;create=true"); 157 System.setProperty("ij.user", "EDWARD"); 158 System.setProperty("ij.password", "noodle"); 159 160 } 161 162 public String getJDBCUrl(String db, String attrs) { 163 String hostName = TestUtil.getHostName(); 165 if (hostName.equals("localhost")) 166 NETWORKSERVER_PORT = 20000; 167 else 168 NETWORKSERVER_PORT = 1527; 169 String s = TestUtil.getJdbcUrlPrefix(hostName, NETWORKSERVER_PORT) 170 + db; 171 if (attrs != null) 172 if (TestUtil.isJCCFramework()) 173 s = s + ":" + attrs + ";"; 174 else 175 s = s + ";" + attrs; 176 return s; 177 178 } 179 180 public javax.sql.DataSource getDS(String database, String user, String 181 password) 182 { 183 return getDS(database,user,password,null); 184 } 185 186 public javax.sql.DataSource getDS(String database, String user, String 187 password, Properties attrs) 188 { 189 190 if (attrs == null) 191 attrs = new Properties (); 192 attrs.setProperty("databaseName", database); 193 if (user != null) 194 attrs.setProperty("user", user); 195 if (password != null) 196 attrs.setProperty("password", password); 197 attrs = addRequiredAttributes(attrs); 198 return TestUtil.getDataSource(attrs); 199 } 200 201 202 203 public javax.sql.ConnectionPoolDataSource getCPDS(String database, String user, String password) { 204 Properties attrs = new Properties (); 205 attrs.setProperty("databaseName", database); 206 if (user != null) 207 attrs.setProperty("user", user); 208 if (password != null) 209 attrs.setProperty("password", password); 210 attrs = addRequiredAttributes(attrs); 211 return TestUtil.getConnectionPoolDataSource(attrs); 212 } 213 214 private Properties addRequiredAttributes(Properties attrs) 215 { 216 hostName = TestUtil.getHostName(); 218 if (TestUtil.isJCCFramework()) 219 { 220 attrs.setProperty("driverType","4"); 221 225 attrs.setProperty("serverName",hostName); 226 } 227 230 if (!hostName.equals("localhost")) 231 { 232 attrs.setProperty("serverName",hostName); 233 attrs.setProperty("portNumber", "1527"); 234 } 235 else 236 attrs.setProperty("portNumber", "20000"); 237 return attrs; 239 } 240 241 public boolean supportsUnicodeNames() { 242 return false; 243 } 244 245 public boolean supportsPooling() { 246 return true; 247 } 248 public boolean supportsXA() { 249 return false; 250 } 251 252 public void start() { 253 } 254 255 public void shutdown() { 256 257 try { 258 259 DriverManager.getConnection(TestUtil.getJdbcUrlPrefix(hostName, 260 NETWORKSERVER_PORT) + 261 "wombat;shutdown=true", 262 "EDWARD", "noodle"); 263 264 System.out.println("FAIL - Shutdown returned connection"); 265 266 } catch (SQLException sqle) { 267 268 System.out.println("EXPECTED SHUTDOWN " + sqle.getMessage()); 269 } 270 271 } 272 273 protected static boolean isServerStarted(NetworkServerControl server, int ntries) 274 { 275 for (int i = 1; i <= ntries; i ++) 276 { 277 try { 278 Thread.sleep(500); 279 server.ping(); 280 return true; 281 } 282 catch (Exception e) { 283 if (i == ntries) 284 return false; 285 } 286 } 287 return false; 288 } 289 290 294 public void testClientDataSourceProperties() throws SQLException 295 { 296 testRetrieveMessageText(); 297 testDescription(); 298 299 testConnectionAttributes(); 301 302 allUsernameAndPasswordTests(); 304 } 305 306 311 public void testRetrieveMessageText() throws SQLException 312 { 313 Connection conn; 314 String retrieveMessageTextProperty = "retrieveMessageText"; 315 Class [] argType = { Boolean.TYPE }; 316 String methodName = TestUtil.getSetterName(retrieveMessageTextProperty); 317 Object [] args; 318 319 try { 320 DataSource ds = getDS("wombat", "EDWARD", "noodle"); 321 Method sh = ds.getClass().getMethod(methodName, argType); 322 args = new Boolean [] { new Boolean (false) }; 323 sh.invoke(ds, args); 324 conn = ds.getConnection(); 325 checkMessageText(conn,"false"); 326 conn.close(); 327 328 ds = getDS("wombat", "EDWARD", "noodle"); 330 args = new Boolean [] { new Boolean (true) }; 331 sh.invoke(ds, args); 332 conn = ds.getConnection(); 333 checkMessageText(conn,"true"); 334 conn.close(); 335 } 336 catch (Exception e) 337 { 338 System.out.println("FAIL: testRetrieveMessageText() Unexpected Exception " + e.getMessage()); 339 e.printStackTrace(); 340 } 341 } 342 343 346 public void testDescription() throws SQLException 347 { 348 String descriptionProperty = "description"; 349 Class [] argType = { String .class}; 350 String setterMethodName = TestUtil.getSetterName(descriptionProperty); 351 String getterMethodName = TestUtil.getGetterName(descriptionProperty); 352 353 Object [] args; 354 355 try { 356 String setDescription = "Everything you ever wanted to know about this datasource"; 357 DataSource ds = getDS("wombat", "EDWARD", "noodle"); 358 Method sh = ds.getClass().getMethod(setterMethodName, argType); 360 args = new Object [] { new String (setDescription) }; 361 sh.invoke(ds, args); 362 sh = ds.getClass().getMethod(getterMethodName, null); 364 String getDescription = (String ) sh.invoke(ds, null); 365 if (!setDescription.equals(getDescription)) 366 throw new Exception ("getDescription() " + getDescription + 367 " does not match setDescription() "); 368 } 369 catch (Exception e) 370 { 371 System.out.println("FAIL: testDescription() Unexpected Exception " + e.getMessage()); 372 e.printStackTrace(); 373 } 374 } 375 376 public void checkMessageText(Connection conn, String 377 retrieveMessageTextValue) throws SQLException 378 { 379 System.out.println("** checkMessageText() with retrieveMessageText= " + 380 retrieveMessageTextValue); 381 382 try { 383 conn.createStatement().executeQuery("SELECT * FROM APP.NOTTHERE"); 384 } 385 catch (SQLException e) 386 { 387 String expectedSQLState = "42X05"; 388 String sqlState = e.getSQLState(); 389 if (sqlState == null || ! sqlState.equals(expectedSQLState)) 390 { 391 System.out.println("Incorrect SQLState. Got: " + sqlState + 392 " should be: " + expectedSQLState); 393 throw e; 394 } 395 if (retrieveMessageTextValue.equals("true") ) 396 { 397 if (e.getMessage().indexOf("does not exist") != -1) 398 System.out.println("PASS: Message Text retrieved properly"); 399 else 400 { 401 System.out.println("FAIL: Message text was not retrieved"); 402 throw e; 403 } 404 } 405 else 406 if (e.getMessage().indexOf("does not exist") == -1) 408 { 409 System.out.println("PASS: Message text not retrieved"); 410 } 411 else 412 { 413 System.out.println("FAIL: Message Text should not have been retrieved"); 414 throw e; 415 } 416 417 } 418 } 419 420 426 public void testConnectionAttributes() { 427 try { 428 System.out.println("Begin connection attribute tests"); 429 testDataSourceConnection("One attribute test: ", 430 "EDWARD", "noodle", "create=true"); 431 testDataSourceConnection("Another different attribute: ", 432 "EDWARD", "noodle", "tracefile=trace.out"); 433 testDataSourceConnection("Two Attributes: ", 434 "EDWARD", "noodle", "create=true;tracefile=trace.out"); 435 System.out.println("End connection attribute tests"); 436 } 437 catch (Exception e) 438 { 439 System.out.println("FAIL: testSetConnectionAttributes() Unexpected Exception " + e.getMessage()); 440 e.printStackTrace(System.out); 441 } 442 } 443 444 450 public void allUsernameAndPasswordTests() { 451 452 try { 453 System.out.println("Begin username and password tests"); 454 455 testDataSourceConnection("Normal test: ", "EDWARD", "noodle", null); 456 457 testDataSourceConnection("No username or password, only attributes test: ", 458 null, null, "user=EDWARD;password=noodle"); 459 460 testDataSourceConnection("Bogus username and password, good attributes test: ", 461 "Whatis", "theMatrix?", "user=EDWARD;password=noodle"); 462 463 testDataSourceConnection("Username, password attribute test: ", 464 "EDWARD", null, "password=noodle"); 465 466 testDataSourceConnection("Password, username attribute test: ", 467 null, "noodle", "user=EDWARD"); 468 469 System.out.println("Turning off authentication"); 470 DataSource ds = getDS("wombat", "EDWARD", "noodle"); 471 Connection conn = ds.getConnection(); 472 CallableStatement cs = conn.prepareCall("CALL SYSCS_UTIL.SYSCS_SET_DATABASE_PROPERTY(?, ?)"); 473 cs.setString(1, "derby.connection.requireAuthentication"); 474 cs.setString(2, "false"); 475 cs.execute(); 476 cs.close(); 477 cs = null; 478 conn.close(); 479 shutdown(); 481 start(); 482 483 testDataSourceConnection("Username, no password test: ", 484 "EDWARD", null, null); 485 486 testDataSourceConnection("No username, password test: ", 487 null, "noodle", null); 488 489 testDataSourceConnection("No username, no password test: ", 490 null, null, null); 491 492 System.out.println("Turning on authentication"); 493 ds = getDS("wombat", "EDWARD", "noodle"); 494 conn = ds.getConnection(); 495 cs = conn.prepareCall("CALL SYSCS_UTIL.SYSCS_SET_DATABASE_PROPERTY(?, ?)"); 496 cs.setString(1, "derby.connection.requireAuthentication"); 497 cs.setString(2, "true"); 498 cs.execute(); 499 cs.close(); 500 cs = null; 501 conn.close(); 502 shutdown(); 503 start(); 504 505 System.out.println("End username and password tests"); 506 } 507 catch (Exception e) 508 { 509 System.out.println("FAIL: allUsernameAndPasswordTests. Unexpected Exception " + e.getMessage()); 510 e.printStackTrace(System.out); 511 } 512 } 513 514 525 public void testDataSourceConnection(String testType, String username, String password, String attributes) throws SQLException { 526 try { 527 System.out.print(testType); 528 Properties props = null; 529 if (attributes != null) { 530 props = new Properties (); 531 props.put("ConnectionAttributes", attributes); 532 } 533 DataSource ds = getDS("wombat", username, password, props); 534 Connection conn = ds.getConnection(); 535 conn.close(); 536 System.out.println("PASS."); 537 } catch (SQLException e) { 538 System.out.println("FAIL. Unexpected Exception: "); 539 e.printStackTrace(System.out); 540 } 541 } 542 } 543 544 545 546 547 548 549 | Popular Tags |