📄 testutil.java
字号:
/* Derby - Class org.apache.derbyTesting.functionTests.util.TestUtil Copyright 1999, 2004 The Apache Software Foundation or its licensors, as applicable. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */package org.apache.derbyTesting.functionTests.util;import java.sql.*;import java.io.*;import java.lang.reflect.*;import java.util.Enumeration;import java.util.Hashtable;import java.util.Locale;import java.util.Properties;import java.util.StringTokenizer;import java.util.NoSuchElementException;import java.security.AccessController;import java.security.PrivilegedAction;import java.security.PrivilegedExceptionAction;import java.security.PrivilegedActionException;import javax.sql.DataSource;import org.apache.derby.iapi.reference.JDBC30Translation;import org.apache.derby.iapi.services.info.JVMInfo;/** Utility methods for tests, in order to bring some consistency to test output and handle testing framework differences*/public class TestUtil { //Used for JSR169 public static boolean HAVE_DRIVER_CLASS; static{ try{ Class.forName("java.sql.Driver"); HAVE_DRIVER_CLASS = true; } catch(ClassNotFoundException e){ //Used for JSR169 HAVE_DRIVER_CLASS = false; } } public static final int UNKNOWN_FRAMEWORK = -1; /** framework = embedded (or null) jdbc:derby: */ public static final int EMBEDDED_FRAMEWORK = 0; /** framework = DerbyNet for JCC jdbc:derby:net: */ public static final int DERBY_NET_FRAMEWORK = 1; /** framework = DB2JCC for testing JCC against DB2 for debugging jcc problems jdbc:db2:// */ public static final int DB2JCC_FRAMEWORK = 2; // jdbc:db2// /** framework = DerbyNetClient for Derby cient jdbc:derby:// */ public static final int DERBY_NET_CLIENT_FRAMEWORK = 3; // jdbc:derby:// /** framework = DB2jNet OLD_NET_FRAMEWORK is for tests that have not yet been contributed. it can be removed once all tests are at apache */ public static final int OLD_NET_FRAMEWORK = 4; // jdbc:derby:net: private static int framework = UNKNOWN_FRAMEWORK; // DataSource Type strings used to build up datasource names. // e.g. "Embed" + XA_DATASOURCE_STRING + "DataSource private static String XA_DATASOURCE_STRING = "XA"; private static String CONNECTION_POOL_DATASOURCE_STRING = "ConnectionPool"; private static String REGULAR_DATASOURCE_STRING = ""; private static String JSR169_DATASOURCE_STRING = "Simple"; // Methods for making framework dependent decisions in tests. /** * Is this a network testingframework? * return true if the System Property framework is set to Derby Network * client or JCC * * @return true if this is a Network Server test */ public static boolean isNetFramework() { framework = getFramework(); switch (framework) { case DERBY_NET_FRAMEWORK: case DERBY_NET_CLIENT_FRAMEWORK: case DB2JCC_FRAMEWORK: case OLD_NET_FRAMEWORK: return true; default: return false; } } /** Is the JCC driver being used @return true for JCC driver */ public static boolean isJCCFramework() { int framework = getFramework(); switch (framework) { case DERBY_NET_FRAMEWORK: case DB2JCC_FRAMEWORK: case OLD_NET_FRAMEWORK: return true; } return false; } public static boolean isDerbyNetClientFramework() { return (getFramework() == DERBY_NET_CLIENT_FRAMEWORK); } public static boolean isEmbeddedFramework() { return (getFramework() == EMBEDDED_FRAMEWORK); } /** Get the framework from the System Property framework @return constant for framework being used TestUtil.EMBEDDED_FRAMEWORK for embedded TestUtil.DERBY_NET_CLIENT_FRAMEWORK for Derby Network Client TestUtil.DERBY_NET_FRAMEWORK for JCC to Network Server TestUtil.DB2JCC_FRAMEWORK for JCC to DB2 */ private static int getFramework() { if (framework != UNKNOWN_FRAMEWORK) return framework; String frameworkString = (String) AccessController.doPrivileged (new PrivilegedAction() { public Object run() { return System.getProperty("framework"); } } ); if (frameworkString == null || frameworkString.toUpperCase(Locale.ENGLISH).equals("EMBEDDED")) framework = EMBEDDED_FRAMEWORK; else if (frameworkString.toUpperCase(Locale.ENGLISH).equals("DERBYNETCLIENT")) framework = DERBY_NET_CLIENT_FRAMEWORK; else if (frameworkString.toUpperCase(Locale.ENGLISH).equals("DERBYNET")) framework = DERBY_NET_FRAMEWORK; else if (frameworkString.toUpperCase(Locale.ENGLISH).indexOf("DB2JNET") != -1) framework = OLD_NET_FRAMEWORK; return framework; } /** Get URL prefix for current framework. @return url, assume localhost - unless set differently in System property - and assume port 1527 for Network Tests @see getJdbcUrlPrefix(String server, int port) */ public static String getJdbcUrlPrefix() { String hostName=getHostName(); return getJdbcUrlPrefix(hostName, 1527); } /** Get hostName as passed in - if not, set it to "localhost" @return hostName, as passed into system properties, or "localhost" */ public static String getHostName() { String hostName = (String) AccessController.doPrivileged (new PrivilegedAction() { public Object run() { return System.getProperty("hostName"); } } ); if (hostName == null) hostName="localhost"; return hostName; } /** Get URL prefix for current framework @param server host to connect to with client driver ignored for embedded driver @param port port to connect to with client driver ignored with embedded driver @return URL prefix EMBEDDED_FRAMEWORK returns "jdbc:derby" DERBY_NET_FRAMEWORK = "jdbc:derby:net://<server>:port/" DERBY_NET_CLIENT_FRAMEWORK = "jdbc:derby://<server>:port/" DB2_JCC_FRAMEWORK = "jdbc:db2://<server>:port/" */ public static String getJdbcUrlPrefix(String server, int port) { int framework = getFramework(); switch (framework) { case EMBEDDED_FRAMEWORK: return "jdbc:derby:"; case DERBY_NET_FRAMEWORK: case OLD_NET_FRAMEWORK: return "jdbc:derby:net://" + server + ":" + port + "/"; case DERBY_NET_CLIENT_FRAMEWORK: return "jdbc:derby://" + server + ":" + port + "/"; case DB2JCC_FRAMEWORK: return "jdbc:db2://" + server + ":" + port + "/"; } // Unknown framework return null; } /** Load the appropriate driver for the current framework */ public static void loadDriver() throws Exception { final String driverName; framework = getFramework(); switch (framework) { case EMBEDDED_FRAMEWORK: driverName = "org.apache.derby.jdbc.EmbeddedDriver"; break; case DERBY_NET_FRAMEWORK: case OLD_NET_FRAMEWORK: case DB2JCC_FRAMEWORK: driverName = "com.ibm.db2.jcc.DB2Driver"; break; case DERBY_NET_CLIENT_FRAMEWORK: driverName = "org.apache.derby.jdbc.ClientDriver"; break; default: driverName= "org.apache.derby.jdbc.EmbeddedDriver"; break; } try { AccessController.doPrivileged (new PrivilegedExceptionAction() { public Object run() throws Exception { return Class.forName(driverName).newInstance(); } } ); } catch (PrivilegedActionException e) { throw e.getException(); } } /** * Get a data source for the appropriate framework * @param attrs A set of attribute values to set on the datasource. * The appropriate setter method wil b * For example the property databaseName with value wombat, * will mean ds.setDatabaseName("wombat") will be called * @return datasource for current framework */ public static javax.sql.DataSource getDataSource(Properties attrs) { String classname; if(HAVE_DRIVER_CLASS) { classname = getDataSourcePrefix() + REGULAR_DATASOURCE_STRING + "DataSource"; return (javax.sql.DataSource) getDataSourceWithReflection(classname, attrs); } else return getSimpleDataSource(attrs); } public static DataSource getSimpleDataSource(Properties attrs) { String classname = getDataSourcePrefix() + JSR169_DATASOURCE_STRING + "DataSource"; return (javax.sql.DataSource) getDataSourceWithReflection(classname, attrs); } /** * Get an xa data source for the appropriate framework * @param attrs A set of attribute values to set on the datasource. * The appropriate setter method wil b * For example the property databaseName with value wombat, * will mean ds.setDatabaseName("wombat") will be called * @return datasource for current framework */ public static javax.sql.XADataSource getXADataSource(Properties attrs) { String classname = getDataSourcePrefix() + XA_DATASOURCE_STRING + "DataSource"; return (javax.sql.XADataSource) getDataSourceWithReflection(classname, attrs); } /** * Get a ConnectionPoolDataSource for the appropriate framework * @param attrs A set of attribute values to set on the datasource. * The appropriate setter method wil b * For example the property databaseName with value wombat, * will mean ds.setDatabaseName("wombat") will be called * @return datasource for current framework */ public static javax.sql.ConnectionPoolDataSource getConnectionPoolDataSource(Properties attrs) { String classname = getDataSourcePrefix() + CONNECTION_POOL_DATASOURCE_STRING + "DataSource"; return (javax.sql.ConnectionPoolDataSource) getDataSourceWithReflection(classname, attrs); } public static String getDataSourcePrefix() { framework = getFramework(); switch(framework) { case OLD_NET_FRAMEWORK: case DERBY_NET_FRAMEWORK: case DB2JCC_FRAMEWORK: return "com.ibm.db2.jcc.DB2"; case DERBY_NET_CLIENT_FRAMEWORK: return "org.apache.derby.jdbc.Client"; case EMBEDDED_FRAMEWORK: return "org.apache.derby.jdbc.Embedded"; default: Exception e = new Exception("FAIL: No DataSource Prefix for framework: " + framework); e.printStackTrace(); } return null; } static private Class[] STRING_ARG_TYPE = {String.class}; static private Class[] INT_ARG_TYPE = {Integer.TYPE}; static private Class[] BOOLEAN_ARG_TYPE = { Boolean.TYPE }; // A hashtable of special non-string attributes. private static Hashtable specialAttributes = null; private static Object getDataSourceWithReflection(String classname, Properties attrs) { Object[] args = null; Object ds = null; Method sh = null; String methodName = null; if (specialAttributes == null) { specialAttributes = new Hashtable(); specialAttributes.put("portNumber",INT_ARG_TYPE); specialAttributes.put("driverType",INT_ARG_TYPE); specialAttributes.put("retrieveMessagesFromServerOnGetMessage", BOOLEAN_ARG_TYPE); specialAttributes.put("retrieveMessageText", BOOLEAN_ARG_TYPE); } try { ds = Class.forName(classname).newInstance(); // for remote server testing, check whether the hostName is set for the test // if so, and serverName is not yet set explicitly for the datasource, set it now String hostName = getHostName(); if ( (!isEmbeddedFramework()) && (hostName != null ) && (attrs.getProperty("serverName") == null) ) attrs.setProperty("serverName", hostName); for (Enumeration propNames = attrs.propertyNames(); propNames.hasMoreElements();) { String key = (String) propNames.nextElement(); Class[] argType = (Class[]) specialAttributes.get(key); if (argType == null) argType = STRING_ARG_TYPE; String value = attrs.getProperty(key); if (argType == INT_ARG_TYPE) { args = new Integer[] { new Integer(Integer.parseInt(value)) }; } else if (argType == BOOLEAN_ARG_TYPE) { args = new Boolean[] { new Boolean(value) }; } else if (argType == STRING_ARG_TYPE) { args = new String[] { value }; } else // No other property types supported right now { throw new Exception("FAIL: getDataSourceWithReflection: Argument type " + argType[0].getName() + " not supportted for attribute: " + " key:" + key + " value:" +value); } methodName = getSetterName(key); // Need to use reflection to load indirectly
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -