📄 checkdriver.java
字号:
// user/password in url testConnect(driver, frameworkPrefix + "testpropdb;user=testuser;password=testpass", null); // user in url, password in property info.clear(); info.setProperty("password","testpass"); testConnect(driver,frameworkPrefix + "testpropdb;user=testusr",info); // different users in url and in properties. URL is the winner info.clear(); info.setProperty("user","APP"); info.setProperty("password","xxxx"); testConnect(driver, frameworkPrefix + "testpropdb;user=testuser;password=testpass", null); // shutdown with properties info.clear(); info.setProperty("shutdown","true"); try { testConnect(driver,frameworkPrefix + "testcreatedb1", info); } catch (SQLException se) { System.out.println("Expected Exception:" + se.getSQLState() + ":" + se.getMessage()); } } /** * Check that drivers accept the correct urls and reject those for other supported drivers. * * @param driver driver we are testing. * * @throws SQLException */ private static void checkAcceptsURL(Driver driver) throws SQLException{ for (int u = 0; u < urls.length;u++) { String url = urls[u]; //System.out.println("acceptsURLTable[" + u +"][" + frameworkOffset+ "]"); boolean expectedAcceptance = acceptsURLTable[u][frameworkOffset]; boolean actualAcceptance = driver.acceptsURL(url); System.out.println("checking acceptsURL(" + url + ")" ); assertExpectedURLAcceptance(url, expectedAcceptance, actualAcceptance); } } /** * Load the driver and check java.sql.Driver methods, * @return * @throws Exception */ private static Driver loadAndCheckDriverForFramework() throws Exception { TestUtil.loadDriver(); String frameworkURL = TestUtil.getJdbcUrlPrefix() + "testpropdb;create=true"; // Test that we loaded the right driver by making a connection Driver driver = DriverManager.getDriver(frameworkURL); Properties props = new Properties(); props.put("user","testuser"); props.put("password","testpass"); Connection conn = DriverManager.getConnection(frameworkURL, props); DatabaseMetaData dbmd = conn.getMetaData(); System.out.println("jdbcCompliant() = " + driver.jdbcCompliant()); // Just check versions against database metadata to avoid more master updates. // Metadata test prints the actual version. int majorVersion = driver.getMajorVersion(); if (majorVersion == dbmd.getDriverMajorVersion()) System.out.println("driver.getMajorVersion() = EXPECTED VERSION"); else new Exception("FAILED: unexpected value for getMajorVersion(): " + majorVersion).printStackTrace(); int minorVersion = driver.getMinorVersion(); if (minorVersion == dbmd.getDriverMinorVersion()) System.out.println("driver.getMinorVersion() = EXPECTED VERSION"); else new Exception("FAILED: unexpected value for getMinorVersion()" + minorVersion).printStackTrace(System.out); conn.close(); return driver; } /** * Check the actual return value of acceptsURL against the expected value and error and stack * trace if they don't match * * @param url URL that was checked for acceptsURL * @param expectedAcceptance expected return value * @param actualAcceptance actual return value * */ private static void assertExpectedURLAcceptance(String url, boolean expectedAcceptance, boolean actualAcceptance) { if (actualAcceptance != expectedAcceptance) { new Exception("FAILED acceptsURL check. url = " + url + " expectedAcceptance = " + expectedAcceptance + " actualAcceptance = " + actualAcceptance).printStackTrace(System.out); } } /** * Tests client URLs to see connection is successful or the correct exception is thrown. * * @param driver * @throws SQLException */ private static void doClientURLTest(Driver driver){ if (!TestUtil.isDerbyNetClientFramework()) return; System.out.println("doClientURLTest()"); Properties info = null; //test with null Properties object for (int i = 0; i < clientUrls.length;i++) { String url = clientUrls[i]; System.out.println("doClientURLTest with url: " + replaceSystemHome(url)); try{ Connection conn = testConnect(driver,url,info); if(conn != null) System.out.println("PASSED:Connection Successful with url: " + replaceSystemHome(url) ); } catch(SQLException se){ System.out.println("EXPECTED EXCEPTION:"+replaceSystemHome(se.getMessage())); } } } /** * Tests URL with spaces in database name to check create and connect works. * (DERBY-618). Make sure that the specified database gets created. We need * to check this because even without the patch for DERBY-618, no exception * gets thrown when we try to connect to a database name with spaces. * Instead, client driver extracts the database name as the string before * the first occurence of space separator. Hence the database which gets * created is wrong. e.g, if we specified database name as * "db name with spaces", the database that got created by client driver * was "db", which was wrong. The URL returned by call to * conn.getMetaData().getURL() was also wrong. * * @param driver * @throws SQLException */ private static void testDbNameWithSpaces(Driver driver) throws SQLException { System.out.println("START testDbNameWithSpaces ..."); Connection conn = null; Properties info = null; String url = null; if(TestUtil.isEmbeddedFramework()) url = EMBEDDED_URL_WITH_SPACES; else if(TestUtil.isDerbyNetClientFramework()) url = CLIENT_URL_WITH_SPACES; else if(TestUtil.isJCCFramework()) { url = JCC_URL_WITH_SPACES; // JCC requires user and password info = new Properties(); info.put("user", "tester"); info.put("password", "testpass"); } conn = testConnect(driver, url, info); if(conn != null) System.out.println("PASSED:Connection Successful with url: " + url ); // Check that the specified database (with spaces) is created File file = new File(DERBY_SYSTEM_HOME + File.separator + DB_NAME_WITH_SPACES); if(file.exists()) System.out.println("testDbNameWithSpaces PASSED - Database created successfully"); else System.out.println("testDbNameWithSpaces FAILED - Database not created correctly"); } /** * Make java.sql.Driver.connect(String url, Properties info call) and print the status of * the connection. * * @param driver driver for framework * @param url url to pass to Driver.connect() * @param info properties to pass to Driver.Connect() * * @throws SQLException on error. */ private static Connection testConnect(Driver driver, String url, Properties info) throws SQLException { String infoString = null; if (info != null) infoString = replaceSystemHome(info.toString()); String urlString = replaceSystemHome(url); Connection conn = driver.connect(url,info); if(conn == null){ System.out.println("Null connection returned for url "+urlString); return conn; } System.out.println("\nConnection info for connect(" + urlString + ", " + infoString +")"); String getUrlValue = conn.getMetaData().getURL(); // URL may include path of DERBY_SYSTEM_HOME for traceFile // filter it out. getUrlValue = replaceSystemHome(getUrlValue); System.out.println("getURL() = " + getUrlValue); System.out.println("getUserName() = " + conn.getMetaData().getUserName()); // CURRENT SCHEMA should match getUserName() ResultSet rs = conn.createStatement().executeQuery("VALUES(CURRENT SCHEMA)"); rs.next(); System.out.println("CURRENT SCHEMA = " + rs.getString(1)); conn.close(); return conn; } /** * @param origString * * @return origString with derby.system.home path replaed with [DERBY_SYSTEM_HOME] */ private static String replaceSystemHome(String origString) { String replaceString = DERBY_SYSTEM_HOME + File.separator; int offset = origString.indexOf(replaceString); if (offset == -1) return origString; else return origString.substring(0,offset) + "[DERBY_SYSTEM_HOME]/"+ origString.substring(offset + replaceString.length()); } }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -