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

📄 testself.java

📁 用于数据库数据潜移
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/* Copyright (c) 1995-2000, 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.
 *
 * Neither the name of the Hypersonic SQL 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 THE HYPERSONIC SQL GROUP,
 * 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.
 *
 * 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-2005, 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.
 *
 * 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.
 *
 * 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.io.File;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.Date;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.sql.Types;

import org.hsqldb.lib.Sort;

/**
 *  Main test class, containing several JDBC and script based tests to
 *  verify correct operation of the engine.<p>
 *
 *  The tests consist of the following:
 * <ul>
 * <li>
 *  Built-in tests for operations, especially those relating to JDBC.
 *</li>
 * <li>
 *  Speed tests using insert / delete / update on a simple table.<p>
 *</li>
 * <li>
 *  Script based SQL tests consisting of:<p>
 *  <code>TestSelf.txt</code> : the main test script.<p>
 *  <code>TestSelfXXXX.txt</code> : specialised test scripts that
 *  will be run in alphabetical filename order.<p>
 *</li>
 * </ul>
 *
 *  Tests can be added by writing new scripts in the standard format described
 *  in <code>TestSelf.txt</code> and naming the script in the correct format,
 *  <code>TestSelfXXXX.txt</code>, where XXXX is the description of the new
 *  test.<p>
 *  The database can be shutdown at the end of each script (using the
 *  SHUTDOWN command). This allows a test to be divided into two or more
 *  scripts in order to test the persistence mechanism for both objects
 *  created via DDL or data stored in the database. An example of this
 *  is the set of supplied scripts, <code>TestSelfCreate.txt</code>,
 *  <code>TestSelfModify.txt</code> and <code>TestSelfVerify.txt</code>.
 *  (fredt@users)
 *
 * @author Thomas Mueller (Hypersonic SQL Group)
 * @version 1.7.2
 * @since Hypersonic SQL
 */
class TestSelf
    extends TestUtil {

  /**
   *  This test in invoked from the command line using:
   * <pre>
   * TestSelf [records [-m]]
   *
   * </pre>
   *
   * -m means run the tests in-memory only
   *
   * @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");
    delete(path + ".log");
  }

  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.createStatement().execute("SHUTDOWN");
    cConnection.close();
  }

  static void testPersistence() {

    deleteDatabase("test1");

    try {
      String url = "jdbc:hsqldb:test1;sql.enforce_strict_size=true";
      String user = "sa";
      String password = "";
      Connection cConnection = null;
      String[] filelist;
      String absolute = new File("TestSelf.txt").getAbsolutePath();

      filelist = new File(new File(absolute).getParent()).list();

      Sort.sort( (Object[]) filelist, new Sort.StringComparator(), 0,
                filelist.length - 1);

      for (int i = 0; i < filelist.length; i++) {
        String fname = filelist[i];

        if (fname.startsWith("TestSelf") && fname.endsWith(".txt")
            && !fname.equals("TestSelf.txt")) {
          print("Openning DB");

          cConnection = DriverManager.getConnection(url, user,
              password);

          testScript(cConnection, fname);
          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 byte[] b1 = {
      0, 1, -128, 44, 12
  };
  static byte[] b2 = {
      10, 127
  };

  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,temp char)";

      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);
      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();
      readTabProfileTest(sStatement);

      byte[] b2n;
      byte[] b1n;
      boolean mismatch;

      s = "select \"org.hsqldb.lib.ArrayUtil.containsAt\"(licence,0, ?) "
          + "from TabProfile";
      p = cConnection.prepareStatement(s);

      p.setBytes(1, b2);

      r = p.executeQuery();

      r.next();

      boolean boo1 = r.getBoolean(1);

      r.next();

      boolean boo2 = r.getBoolean(1);

      // test boo1 != boo2

      /** @todo fredt - nested procedure call doesn't parse  */
      /*
                  s = "select \"org.hsqldb.lib.StringConverter.hexToByte\""
                      + "(\"org.hsqldb.lib.StringConverter.byteToHex\"(car)) "
                      + "from TabProfile";
                  r = sStatement.executeQuery(s);
                  r.next();
                  b1n = r.getBytes(1);
                  r.next();
                  b1n = r.getBytes(1);
       */

      /** @todo fredt - alias does not resolve */
      /*
           s = "select \"org.hsqldb.lib.StringConverter.byteToHex\"(car) temp, " +
                      "\"org.hsqldb.lib.StringConverter.hexToByte\"(temp) "
                      + "from TabProfile";
                  r = sStatement.executeQuery(s);
                  r.next();
                  b1n = r.getBytes(2);
                  r.next();
                  b1n = r.getBytes(2);
       */
      s = "update tabprofile set temp = \"org.hsqldb.lib.StringConverter.byteToHex\"(licence)";

      sStatement.executeUpdate(s);

      s = "select \"org.hsqldb.lib.StringConverter.hexToByte\"(temp) "
          + "from TabProfile order by id desc";
      r = sStatement.executeQuery(s);

      r.next();

      b1n = r.getBytes(1);

      for (int i = 0; i < b1n.length; i++) {
        if (b1[i] != b1n[i]) {
          mismatch = true;
        }
      }

      r.next();

      b2n = r.getBytes(1);

⌨️ 快捷键说明

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