⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 testutil.java

📁 derby database source code.good for you.
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
			// setDatabaseName			sh = ds.getClass().getMethod(methodName, argType);			sh.invoke(ds, args);		}		} catch (Exception e)		{			System.out.println("Error accessing method " + methodName);			System.out.println(e.getMessage());			e.printStackTrace();		}		return ds;	}		public static String  getSetterName(String attribute)	{		return "set" + Character.toUpperCase(attribute.charAt(0)) + attribute.substring(1);	}		public static String  getGetterName(String attribute)	{		return "get" + Character.toUpperCase(attribute.charAt(0)) + attribute.substring(1);	}	// Some methods for test output.	public static void dumpSQLExceptions(SQLException sqle) {		TestUtil.dumpSQLExceptions(sqle, false);	}	public static void dumpSQLExceptions(SQLException sqle, boolean expected) {		String prefix = "";		if (!expected) {			System.out.println("FAIL -- unexpected exception ****************");		}		else		{			prefix = "EXPECTED ";		}		do		{			System.out.println(prefix + "SQLSTATE("+sqle.getSQLState()+"): " + sqle.getMessage());			sqle = sqle.getNextException();		} while (sqle != null);	}	  public static String sqlNameFromJdbc(int jdbcType) {		switch (jdbcType) {			case Types.BIT 		:  return "Types.BIT";			case JDBC30Translation.SQL_TYPES_BOOLEAN  : return "Types.BOOLEAN";			case Types.TINYINT 	:  return "Types.TINYINT";			case Types.SMALLINT	:  return "SMALLINT";			case Types.INTEGER 	:  return "INTEGER";			case Types.BIGINT 	:  return "BIGINT";			case Types.FLOAT 	:  return "Types.FLOAT";			case Types.REAL 	:  return "REAL";			case Types.DOUBLE 	:  return "DOUBLE";			case Types.NUMERIC 	:  return "Types.NUMERIC";			case Types.DECIMAL	:  return "DECIMAL";			case Types.CHAR		:  return "CHAR";			case Types.VARCHAR 	:  return "VARCHAR";			case Types.LONGVARCHAR 	:  return "LONG VARCHAR";            case Types.CLOB     :  return "CLOB";			case Types.DATE 		:  return "DATE";			case Types.TIME 		:  return "TIME";			case Types.TIMESTAMP 	:  return "TIMESTAMP";			case Types.BINARY			:  return "CHAR () FOR BIT DATA";			case Types.VARBINARY	 	:  return "VARCHAR () FOR BIT DATA";			case Types.LONGVARBINARY 	:  return "LONG VARCHAR FOR BIT DATA";            case Types.BLOB             :  return "BLOB";			case Types.OTHER		:  return "Types.OTHER";			case Types.NULL		:  return "Types.NULL";			default : return String.valueOf(jdbcType);		}	}	  public static String getNameFromJdbcType(int jdbcType) {		switch (jdbcType) {			case Types.BIT 		:  return "Types.BIT";			case JDBC30Translation.SQL_TYPES_BOOLEAN  : return "Types.BOOLEAN";			case Types.TINYINT 	:  return "Types.TINYINT";			case Types.SMALLINT	:  return "Types.SMALLINT";			case Types.INTEGER 	:  return "Types.INTEGER";			case Types.BIGINT 	:  return "Types.BIGINT";			case Types.FLOAT 	:  return "Types.FLOAT";			case Types.REAL 	:  return "Types.REAL";			case Types.DOUBLE 	:  return "Types.DOUBLE";			case Types.NUMERIC 	:  return "Types.NUMERIC";			case Types.DECIMAL	:  return "Types.DECIMAL";			case Types.CHAR		:  return "Types.CHAR";			case Types.VARCHAR 	:  return "Types.VARCHAR";			case Types.LONGVARCHAR 	:  return "Types.LONGVARCHAR";            case Types.CLOB     :  return "Types.CLOB";			case Types.DATE 		:  return "Types.DATE";			case Types.TIME 		:  return "Types.TIME";			case Types.TIMESTAMP 	:  return "Types.TIMESTAMP";			case Types.BINARY			:  return "Types.BINARY";			case Types.VARBINARY	 	:  return "Types.VARBINARY";			case Types.LONGVARBINARY 	:  return "Types.LONGVARBINARY";            case Types.BLOB             :  return "Types.BLOB";			case Types.OTHER		:  return "Types.OTHER";			case Types.NULL		:  return "Types.NULL";			default : return String.valueOf(jdbcType);		}	}	/*** Some routines for printing test information to html  **/	public static String TABLE_START_TAG =  "<TABLE border=1 cellspacing=1 cellpadding=1  bgcolor=white  style='width:100%'>";	public static String TABLE_END_TAG = "</TABLE>";	public static String TD_INVERSE =		"<td  valign=bottom align=center style=background:#DADADA;  padding:.75pt .75pt .75pt .75pt'> <p class=MsoNormal style='margin-top:6.0pt;margin-right:0in;margin-bottom:  6.0pt;margin-left:0in'><b><span style='font-size:8.5pt;font-family:Arial;  color:black'>";	public static String TD_CENTER = "<TD valign=center align=center> <p class=MsoNormal style='margin-top:6.0pt;margin-right:0in;margin-bottom:6.0pt;margin-left:0in'><b><span style='font-size:8.5pt;font-family:Arial;  color:black'>";	public static String TD_LEFT = "<TD valign=center align=left> <p class=MsoNormal style='margin-top:6.0pt;margin-right:0in;margin-bottom:6.0pt;margin-left:0in'><b><span style='font-size:8.5pt;font-family:Arial;  color:black'>";		public static String TD_END = "</SPAN></TD>";	public static String END_HTML_PAGE="</BODY> </HTML>";		public static void startHTMLPage(String title, String author)	{		System.out.println("<HTML> \n <HEAD>");		System.out.println(" <meta http-equiv=\"Content-Type\"content=\"text/html; charset=iso-8859-1\">");		System.out.println("<meta name=\"Author\" content=\"" + author +  "\">");		System.out.println("<title>" + title + "</title>");		System.out.println("</HEAD> <BODY>");		System.out.println("<H1>" + title + "</H1>");	}	public static void endHTMLPage()	{		System.out.println(END_HTML_PAGE);	}/*	public static void main(String[] argv)	{		testBoolArrayToHTMLTable();	}*/	/** 	 * Converts 2 dimensional boolean array into an HTML table.	 * used by casting.java to print out casting doc	 *	 * @param rowLabels   - Row labels	 * @param colLabels   - Column labels	 **/	public static void printBoolArrayHTMLTable(String rowDescription,											   String columnDescription,											   String [] rowLabels, 											   String [] colLabels,											   boolean[][] array,											   String tableInfo)	{		System.out.println("<H2>" + tableInfo + "</H2>");		System.out.println(TABLE_START_TAG);		System.out.println("<TR>");		// Print corner with labels		System.out.println(TD_INVERSE +columnDescription + "---><BR><BR><BR><BR><BR>");		System.out.println("<---" +rowDescription);		System.out.println(TD_END);				// Print column headers		for (int i = 0; i < colLabels.length; i++)		{			System.out.println(TD_INVERSE);			for (int c = 0; c < colLabels[i].length() && c < 20; c++)			{				System.out.println(colLabels[i].charAt(c) + "<BR>");			}			System.out.println(TD_END);		}		System.out.println("</TR>");		// Print the Row Labels and Data		for (int i = 0; i < rowLabels.length; i ++)		{			System.out.println("<TR>");			System.out.println(TD_LEFT);			System.out.println("<C> " +  rowLabels[i] + "</C>");			System.out.println(TD_END);			for (int j = 0; j < colLabels.length; j ++)			{				System.out.println(TD_CENTER);				System.out.println((array[i][j]) ? "Y" : "-");				System.out.println(TD_END);			}			System.out.println("</TR>");		}		System.out.println(TABLE_END_TAG);		System.out.println("<P><P>");	}	/**	 * Just converts a string to a hex literal to assist in converting test	 * cases that used to insert strings into bit data tables	 * Converts using UTF-16BE just like the old casts used to.	 *	 * @ param s  String to convert  (e.g	 * @ resturns hex literal that can be inserted into a bit column.	 */	public static String stringToHexLiteral(String s)	{		byte[] bytes;		String hexLiteral = null;		try {			bytes = s.getBytes("UTF-16BE");			hexLiteral = convertToHexString(bytes);		}		catch (UnsupportedEncodingException ue)		{			System.out.println("This shouldn't happen as UTF-16BE should be supported");			ue.printStackTrace();		}		return hexLiteral;	}	private static String convertToHexString(byte [] buf)	{		StringBuffer str = new StringBuffer();		str.append("X'");		String val;		int byteVal;		for (int i = 0; i < buf.length; i++)		{			byteVal = buf[i] & 0xff;			val = Integer.toHexString(byteVal);			if (val.length() < 2)				str.append("0");			str.append(val);		}		return str.toString() +"'";	}	/**		Get the JDBC version, inferring it from the driver.		We cannot use the JDBC DatabaseMetaData method		as it is not present in JDBC 2.0.	*/	public static int getJDBCMajorVersion(Connection conn)	{		try {			conn.getClass().getMethod("setSavepoint", null);			return 3;		} catch (NoSuchMethodException e) {			return 2;		} catch (NoClassDefFoundError e2) {			return 2;		}	}    /**        Drop the test objects passed in as a string identifying the        type of object (e.g. TABLE, PROCEDURE) and its name.        Thus, for example, a testObject array could be:        {"TABLE MYSCHEMA.MYTABLE", "PROCEDURE THISDUMMY"}        The statement passed in must be a 'live' statement in the test.    */    public static void cleanUpTest (Statement s, String[] testObjects)                                    throws SQLException {        /* drop each object named */        for (int i=0; i < testObjects.length; i++) {            try {                s.execute("drop " + testObjects[i]);                //System.out.println("now dropping " + testObjects[i]);            } catch (SQLException se) { // ignore...            }        }	    }        /**     * Get connection to given database using the connection attributes. This     * method is used by tests to get a secondary connection with      * different set of attributes. It does not use what is specified in      * app_properties file or system properties. This method uses DataSource      * class for CDC/Foundation Profile environments, which are based on      * JSR169. Using DataSource will not work with other j9 profiles. So     * DriverManager is used for non-JSR169. The method is used as a wrapper to     * hide this difference in getting connections in different environments.     *       * @param databaseName     * @param connAttrs     * @return Connection to database      * @throws SQLException on failure to connect.     * @throws ClassNotFoundException on failure to load driver.     * @throws InstantiationException on failure to load driver.     * @throws IllegalAccessException on failure to load driver.     */    public static Connection getConnection(String databaseName, String connAttrs)    	throws SQLException {        try {            Connection conn;            if(TestUtil.HAVE_DRIVER_CLASS) {                // following is like loadDriver(), but                // that method throws Exception, we want finer granularity                String driverName;                int 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;                }                 // q: do we need a privileged action here, like in loadDriver?                Class.forName(driverName).newInstance();				                String url = getJdbcUrlPrefix() + databaseName;                if (connAttrs != null) url += ";" + connAttrs;                if (framework == DERBY_NET_FRAMEWORK)                {                    if (( connAttrs == null) || ((connAttrs != null) && (connAttrs.indexOf("user") < 0)))                        url += ":" + "user=APP;password=APP;retrieveMessagesFromServerOnGetMessage=true;";                }                conn = DriverManager.getConnection(url);    	    }    	    else {    		    //Use DataSource for JSR169	    	    Properties prop = new Properties();	            prop.setProperty("databaseName", databaseName);    		    if (connAttrs != null)	                prop.setProperty("connectionAttributes", connAttrs);	            conn = getDataSourceConnection(prop);    	    }            return conn;    	} catch (ClassNotFoundException cnfe) { 		    System.out.println("FAILure: Class not found!");		    cnfe.printStackTrace();		    return null;    	} catch (InstantiationException inste) {    		System.out.println("FAILure: Cannot instantiate class");    		inste.printStackTrace();    		return null;    	} catch (IllegalAccessException ille) {    		System.out.println("FAILure: Not allowed to use class");    		ille.printStackTrace();    		return null;    	}    }        public static Connection getDataSourceConnection (Properties prop) throws SQLException {		DataSource ds = TestUtil.getDataSource(prop);		try {			Connection conn = ds.getConnection();			return conn;		}		catch (SQLException e) {			throw e;		}	}		public static void shutdownUsingDataSource (String dbName) throws SQLException {		Properties prop = new Properties();		prop.setProperty("databaseName", dbName );		prop.setProperty("shutdownDatabase", "shutdown" );		DataSource ds = TestUtil.getDataSource(prop);		try {			Connection conn = ds.getConnection();		}		catch (SQLException e) {			throw e;		}	}		//Used by metadata tests for DatabaseMetadata.getURL	public static boolean compareURL(String url) {					if(isEmbeddedFramework()) {			if(url.compareTo("jdbc:derby:wombat") == 0)				return true;		} else if(isNetFramework()) {			try {				StringTokenizer urlTokenizer = new StringTokenizer(url, "/");				String urlStart = urlTokenizer.nextToken();				urlTokenizer.nextToken();				String urlEnd = urlTokenizer.nextToken();								if(urlEnd.compareTo("wombat;create=true") != 0)					return false;								if(isJCCFramework() && (urlStart.compareTo("jdbc:derby:net:") == 0))					return true;								if(isDerbyNetClientFramework() && (urlStart.compareTo("jdbc:derby:") == 0))					return true;							} catch (NoSuchElementException nsee) {				//Should not reach here.				return false;			}		}				return false;	}}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -