📄 testself.java
字号:
/* Copyrights and Licenses * * This product includes Hypersonic SQL. * Originally developed by Thomas Mueller and the Hypersonic SQL Group. * * Copyright (c) 1995-2000 by the Hypersonic SQL Group. All rights reserved. * Redistribution and use in source and binary forms, with or without modification, are permitted * provided that the following conditions are met: * - Redistributions of source code must retain the above copyright notice, this list of conditions * and the following disclaimer. * - Redistributions in binary form must reproduce the above copyright notice, this list of * conditions and the following disclaimer in the documentation and/or other materials * provided with the distribution. * - All advertising materials mentioning features or use of this software must display the * following acknowledgment: "This product includes Hypersonic SQL." * - Products derived from this software may not be called "Hypersonic SQL" nor may * "Hypersonic SQL" appear in their names without prior written permission of the * Hypersonic SQL Group. * - Redistributions of any form whatsoever must retain the following acknowledgment: "This * product includes Hypersonic SQL." * This software is provided "as is" and any expressed or implied warranties, including, but * not limited to, the implied warranties of merchantability and fitness for a particular purpose are * disclaimed. In no event shall the Hypersonic SQL Group or its contributors be liable for any * direct, indirect, incidental, special, exemplary, or consequential damages (including, but * not limited to, procurement of substitute goods or services; loss of use, data, or profits; * or business interruption). However caused any on any theory of liability, whether in contract, * strict liability, or tort (including negligence or otherwise) arising in any way out of the use of this * software, even if advised of the possibility of such damage. * This software consists of voluntary contributions made by many individuals on behalf of the * Hypersonic SQL Group. * * * For work added by the HSQL Development Group: * * Copyright (c) 2001-2002, The HSQL Development Group * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * Redistributions of source code must retain the above copyright notice, this * list of conditions and the following disclaimer, including earlier * license statements (above) and comply with all above license conditions. * * Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution, including earlier * license statements (above) and comply with all above license conditions. * * Neither the name of the HSQL Development Group nor the names of its * contributors may be used to endorse or promote products derived from this * software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL HSQL DEVELOPMENT GROUP, HSQLDB.ORG, * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */package org.hsqldb.test;import java.sql.*;import java.io.*;import org.hsqldb.jdbcDriver;/** * Main test class, containing several JDBC and script based tests to * verify correct operation of the engine. * * @version 1.7.0 */class TestSelf { /** * Method declaration * * @param argv */ public static void main(String argv[]) { print("Usage: TestSelf [records [-m]] (-m means in-memory only)"); int max = 500; if (argv.length >= 1) { max = Integer.parseInt(argv[0]); } boolean persistent = true; boolean update = false; if (argv.length >= 2) { String a1 = argv[1]; if (a1.equals("-m")) { persistent = false; } } test(max, persistent); } /** * Method declaration * * @param max * @param persistent */ static void test(int max, boolean persistent) { // DriverManager.setLogStream(System.out); try { DriverManager.registerDriver(new org.hsqldb.jdbcDriver()); if (persistent) { testPersistence(); deleteDatabase("test2"); test("jdbc:hsqldb:test2", "sa", "", true); testPerformance("jdbc:hsqldb:test2", "sa", "", max, true); } test("jdbc:hsqldb:.", "sa", "", false); testPerformance("jdbc:hsqldb:.", "sa", "", max, false); } catch (Exception e) { print("TestSelf error: " + e.getMessage()); e.printStackTrace(); } } static void delete(String file) { try { new File(file).delete(); } catch (Exception e) {} } static void deleteDatabase(String path) { delete(path + ".backup"); delete(path + ".properties"); delete(path + ".script"); delete(path + ".data"); } static void test(String url, String user, String password, boolean persistent) throws Exception { String name = persistent ? "Persistent" : "Memory"; print(name); Connection cConnection = null; try { cConnection = DriverManager.getConnection(url, user, password); } catch (Exception e) { e.printStackTrace(); print("TestSelf init error: " + e.getMessage()); } testMainScript(cConnection, persistent); testTabProfile(cConnection, persistent); testMarotest(cConnection, persistent); cConnection.close(); } static void testPersistence() { deleteDatabase("test1"); try { String url = "jdbc:hsqldb:test1"; String user = "sa"; String password = ""; Connection cConnection = null; cConnection = DriverManager.getConnection(url, user, password); testScript(cConnection, "TestSelfCreate.txt"); cConnection.close(); cConnection = DriverManager.getConnection(url, user, password); testScript(cConnection, "TestSelfModify.txt"); cConnection.close(); cConnection = DriverManager.getConnection(url, user, password); testScript(cConnection, "TestSelfVerify.txt"); cConnection.close(); } catch (Exception e) { e.printStackTrace(); print("TestSelf init error: " + e.getMessage()); } } static void testMainScript(Connection cConnection, boolean persistent) { String name = persistent ? "Persistent" : "Memory"; print(name + " TestScript"); // location of TestSelf.txt relative to the development environment String path = "TestSelf.txt"; testScript(cConnection, path); } static void testScript(Connection cConnection, String path) { try { Statement sStatement = cConnection.createStatement(); File testfile = new File(path); LineNumberReader read = new LineNumberReader(new FileReader(testfile)); String s = ""; print("Opened test script file: " + testfile.getAbsolutePath()); while (true) { String line = read.readLine(); if (line == null) { break; } if (line.startsWith(" ")) { s += line; } else { test(sStatement, s); s = line; } } sStatement.close(); } catch (Exception e) { e.printStackTrace(); print("test script file error: " + e.getMessage()); } } static void testTabProfile(Connection cConnection, boolean persistent) { Statement sStatement = null; ResultSet r; String s = ""; long start; boolean bDropError = false; String name = persistent ? "Persistent" : "Memory"; print(name + " TabProfile"); try { sStatement = cConnection.createStatement(); } catch (Exception e) { e.printStackTrace(); print("TabProfile init error: " + e.getMessage()); return; } try { // prepared statements s = "create table TabProfile(id int primary key," + "car char,won bit,licence varbinary," + "name char,sex char,chance double,birthday date)"; sStatement.execute(s); s = "insert into TabProfile values ( ?, ?, ?, ?," + "'\"John\" the bird''s best friend', 'M',?,?);"; PreparedStatement p = cConnection.prepareStatement(s); p.clearParameters(); p.setInt(1, 10); p.setString(2, "Matchcartoon"); p.setBoolean(3, true); byte[] b1 = { 0, 1, -128, 44, 12 }; p.setBytes(4, b1); p.setDouble(5, 50.5); p.setNull(6, Types.DATE); p.executeUpdate(); p.clearParameters(); p.setInt(1, -2); p.setString(2, "\"Birdie\"'s car ?"); p.setBoolean(3, false); byte b2[] = { 10, 127 }; p.setBytes(4, b2); p.setDouble(5, -3.1415e-20); java.util.Calendar cal = java.util.Calendar.getInstance(); cal.set(2000, 2, 29); // fredt@users - who designed the java.util.Calendar API? p.setDate(6, new Date(cal.getTime().getTime())); p.executeUpdate(); s = "select * from TabProfile where id=-2"; r = sStatement.executeQuery(s); r.next(); if (!r.getString(2).equals("\"Birdie\"'s car ?")) { throw new Exception("Unicode error."); } r.close(); s = "drop table TabProfile"; sStatement.execute(s); s = "create table obj(id int,o object)"; sStatement.execute(s); s = "insert into obj values(?,?)"; p = cConnection.prepareStatement(s); p.setInt(1, 1); int ia1[] = { 1, 2, 3 }; p.setObject(2, ia1); p.executeUpdate(); p.clearParameters(); p.setInt(1, 2); java.awt.Rectangle r1 = new java.awt.Rectangle(10, 11, 12, 13); p.setObject(2, r1); p.executeUpdate(); r = sStatement.executeQuery("SELECT o FROM obj ORDER BY id DESC"); r.next(); java.awt.Rectangle r2 = (java.awt.Rectangle) r.getObject(1); if (r2.x != 10 || r2.y != 11 || r2.width != 12 || r2.height != 13) { throw new Exception("Object data error: Rectangle"); } r.next(); int ia2[] = (int[]) (r.getObject(1)); if (ia2[0] != 1 || ia2[1] != 2 || ia2[2] != 3 || ia2.length != 3) { throw new Exception("Object data error: int[]"); } s = "drop table obj"; sStatement.execute(s); sStatement.close(); } catch (Exception e) { print(""); print("TabProfile error: " + e); print("with SQL command: " + s); e.printStackTrace(); } } static void testMarotest(Connection cConnection, boolean persistent) { Statement sStatement = null; ResultSet r; String s = ""; long start; boolean bDropError = false; String name = persistent ? "Persistent" : "Memory"; print(name + " Marotest");
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -