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

📄 csunittest.java

📁 Java写的TDS协议(JDBC/ODBC)实现
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
    boolean   passed = true;


    output.println("Starting test #t0031-  test text columns");


    int         count    = 0;
    dropTable("#t0031");

    count = stmt.executeUpdate("create table #t0031                " +
    "  (t_nullable      text null,     " +
    "   t_notnull       text not null, " +
    "   i               int not null)  ");
    output.println("Creating table affected " + count + " rows");

    stmt.executeUpdate("insert into #t0031 values(null, '',   1)");
    stmt.executeUpdate("insert into #t0031 values(null, 'b1', 2)");
    stmt.executeUpdate("insert into #t0031 values('',   '',   3)");
    stmt.executeUpdate("insert into #t0031 values('',   'b2', 4)");
    stmt.executeUpdate("insert into #t0031 values('a1', '',   5)");
    stmt.executeUpdate("insert into #t0031 values('a2', 'b3', 6)");


    ResultSet  rs = stmt.executeQuery("select * from #t0031 " +
    " order by i ");



    if (!rs.next()) {
      throw new SQLException("Failed");
    }
    passed = passed && (rs.getString(1) == null);
    passed = passed && (rs.getString(2).equals(""));
    passed = passed && (rs.getInt(3) == 1);

    if (!rs.next()) {
      throw new SQLException("Failed");
    }
    passed = passed && (rs.getString(1) == null);
    passed = passed && (rs.getString(2).equals("b1"));
    passed = passed && (rs.getInt(3) == 2);

    if (!rs.next()) {
      throw new SQLException("Failed");
    }
    passed = passed && (rs.getString(1).equals(""));
    passed = passed && (rs.getString(2).equals(""));
    passed = passed && (rs.getInt(3) == 3);

    if (!rs.next()) {
      throw new SQLException("Failed");
    }
    passed = passed && (rs.getString(1).equals(""));
    passed = passed && (rs.getString(2).equals("b2"));
    passed = passed && (rs.getInt(3) == 4);

    if (!rs.next()) {
      throw new SQLException("Failed");
    }
    passed = passed && (rs.getString(1).equals("a1"));
    passed = passed && (rs.getString(2).equals(""));
    passed = passed && (rs.getInt(3) == 5);

    if (!rs.next()) {
      throw new SQLException("Failed");
    }
    passed = passed && (rs.getString(1).equals("a2"));
    passed = passed && (rs.getString(2).equals("b3"));
    passed = passed && (rs.getInt(3) == 6);

    assertTrue(passed);
  }

  public void testSpHelpSysUsers0032() throws Exception {
    Statement   stmt = con.createStatement();
    boolean   passed = true;
    boolean   isResultSet;
    boolean   done = false;
    int       i;
    int       updateCount = 0;


    output.println("Starting test #t0032-  test sp_help sysusers");


    int         count    = 0;


    isResultSet = stmt.execute("sp_help sysusers");

    output.println("Executed the statement.  rc is " + isResultSet);

    do {
      if (isResultSet) {
        output.println("About to call getResultSet");
        ResultSet          rs   = stmt.getResultSet();
        ResultSetMetaData  meta = rs.getMetaData();
        updateCount = 0;
        while(rs.next()) {
          for(i=1; i<=meta.getColumnCount(); i++) {
            output.print(rs.getString(i) + "\t");
          }
          output.println("");
        }
        output.println("Done processing the result set");
      }
      else {
        output.println("About to call getUpdateCount()");
        updateCount = stmt.getUpdateCount();
        output.println("Updated " + updateCount + " rows");
      }
      output.println("About to call getMoreResults()");
      isResultSet = stmt.getMoreResults();
      done = !isResultSet && updateCount==-1;
    } while (!done);

    assertTrue(passed);
  }
  static String longString(char ch) {
    int                 i;
    String              str255 = "";

    for(i=0; i<255; i++) {
      str255 = str255 + ch;
    }
    return str255;
  }
  public void testExceptionByUpdate0033() throws Exception {
    boolean passed;
    Statement   stmt = con.createStatement();
    output.println("Starting test #t0033-  make sure Statement.executeUpdate() throws exception");

    try {
      passed = false;
      stmt.executeUpdate("I am sure this is an error");
    }
    catch (SQLException e) {
      output.println("The exception is " + e.getMessage());
      passed = true;
    }
    assertTrue(passed);
  }

  public void testInsertConflict0049() throws Exception {
    Statement   stmt = con.createStatement();


    int         i;
    int         count    = 0;
    dropTable("#t0049b");    // important: first drop this because of foreign key
    dropTable("#t0049a");

    String query =
    "create table #t0049a(                    " +
    "  a integer identity(1,1) primary key,  " +
    "  b char    not null)";

    count = stmt.executeUpdate(query);
    output.println("Creating table affected " + count + " rows");


    query =
    "create table #t0049b(                    " +
    "  a integer not null,                   " +
    "  c char    not null,                   " +
    "  foreign key (a) references #t0049a(a)) ";
    count = stmt.executeUpdate(query);
    output.println("Creating table affected " + count + " rows");


    query = "insert into #t0049b (a, c) values (?, ?)";
    java.sql.PreparedStatement pstmt = con.prepareStatement(query);

    try {
      pstmt.setInt(1, 1);
      pstmt.setString(2, "a");
      count = pstmt.executeUpdate();
    }
    catch(SQLException e) {
      if (! (e.getMessage().startsWith("INSERT statement conflicted"))) {
        throw e;
      }
    }
    pstmt.close();

    count = stmt.executeUpdate("insert into #t0049a (b) values ('a')");
    output.println("insert affected " + count + " rows");

    pstmt = con.prepareStatement(query);
    pstmt.setInt(1, 1);
    pstmt.setString(2, "a");
    count = pstmt.executeUpdate();


  }

  public void testxx0050() throws Exception {
    Statement   stmt = con.createStatement();
    boolean passed = true;
    int         i;
    int         count    = 0;

    try {
      stmt.executeUpdate("drop procedure p0050");
    }
    catch (SQLException e) {
      if (! (e.getMessage().startsWith("Cannot drop the procedure 'p0050', because it does"))) {
        throw e;
      }
    }
    dropTable("#t0050b");
    dropTable("#t0050a");

    String query =
    "create table #t0050a(                    " +
    "  a integer identity(1,1) primary key,  " +
    "  b char    not null)";

    count = stmt.executeUpdate(query);
    output.println("Creating table affected " + count + " rows");


    query =
    "create table #t0050b(                    " +
    "  a integer not null,                   " +
    "  c char    not null,                   " +
    "  foreign key (a) references #t0050a(a)) ";
    count = stmt.executeUpdate(query);
    output.println("Creating table affected " + count + " rows");

    query =
    "create procedure #p0050 (@a integer, @c char) as " +
    "   insert into #t0050b (a, c) values (@a, @c)    ";
    count = stmt.executeUpdate(query);
    output.println("Creating procedure affected " + count + " rows");


    query = "exec #p0050 ?, ?";
    java.sql.PreparedStatement pstmt = con.prepareStatement(query);

    try {
      pstmt.setInt(1, 1);
      pstmt.setString(2, "a");
      count = pstmt.executeUpdate();
    }
    catch(SQLException e) {
      if (! (e.getMessage().startsWith("INSERT statement conflicted"))) {
        throw e;
      }
    }
    pstmt.close();

    count = stmt.executeUpdate("insert into #t0050a (b) values ('a')");
    output.println("insert affected " + count + " rows");

    pstmt = con.prepareStatement(query);
    pstmt.setInt(1, 1);
    pstmt.setString(2, "a");
    count = pstmt.executeUpdate();
    assertTrue(passed);
  }

  public void testxx0051() throws Exception {
    boolean passed = true;
    int         i;
    int         count    = 0;
    Statement   stmt     = con.createStatement();

    try {
      String           types[] = {"TABLE"};
      DatabaseMetaData dbMetaData = con.getMetaData( );
      ResultSet        rs         = dbMetaData.getTables( null, "%", "t%", types);

      while(rs.next()) {
        output.println("Table " + rs.getString(3));
        output.println("  catalog " + rs.getString(1));
        output.println("  schema  " + rs.getString(2));
        output.println("  name    " + rs.getString(3));
        output.println("  type    " + rs.getString(4));
        output.println("  remarks " + rs.getString(5));
      }
    }
    catch(java.sql.SQLException e) {
      passed = false;
      output.println("Exception caught.  " + e.getMessage());
      e.printStackTrace();
    }
    assertTrue(passed);
  }
  public void testxx0055() throws Exception {
    boolean passed = true;
    int         i;
    int         count    = 0;
    Statement   stmt     = con.createStatement();

    try {
      String           expectedNames[] = {
        "TABLE_CAT",
        "TABLE_SCHEM",
        "TABLE_NAME",
        "TABLE_TYPE",
        "REMARKS"
      };
      String           types[] = {"TABLE"};
      DatabaseMetaData dbMetaData = con.getMetaData();
      ResultSet        rs         = dbMetaData.getTables( null, "%", "t%", types);
      ResultSetMetaData rsMetaData = rs.getMetaData();

      if (rsMetaData.getColumnCount() != 5) {
        if (passed) {
          passed = false;
          output.println("Bad column count.  Should be 5, was "
          + rsMetaData.getColumnCount());
        }
      }

      for(i=0; passed && i<expectedNames.length; i++) {
        if (! rsMetaData.getColumnName(i+1).equals(expectedNames[i])) {
          passed = false;
          output.println("Bad name for column " + (i+1) + ".  "
          + "Was " + rsMetaData.getColumnName(i+1)
          + ", expected "
          + expectedNames[i]);
        }
      }
    }
    catch(java.sql.SQLException e) {
      passed = false;
      output.println("Exception caught.  " + e.getMessage());
      e.printStackTrace();
    }
    assertTrue(passed);
  }

  public void testxx0052() throws Exception {
    boolean passed = true;

    // ugly, I know
    byte[] image = {
      (byte)0x47, (byte)0x49, (byte)0x46, (byte)0x38,
      (byte)0x39, (byte)0x61, (byte)0x0A, (byte)0x00,
      (byte)0x0A, (byte)0x00, (byte)0x80, (byte)0xFF,
      (byte)0x00, (byte)0xD7, (byte)0x3D, (byte)0x1B,
      (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x2C,
      (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00,
      (byte)0x0A, (byte)0x00, (byte)0x0A, (byte)0x00,
      (byte)0x00, (byte)0x02, (byte)0x08, (byte)0x84,
      (byte)0x8F, (byte)0xA9, (byte)0xCB, (byte)0xED,
      (byte)0x0F, (byte)0x63, (byte)0x2B, (byte)0x00,
      (byte)0x3B,
    };

    int         i;
    int         count    = 0;
    Statement   stmt     = con.createStatement();

    dropTable("#t0052");

    try {
      String sql =
      "create table #t0052 (                                  " +
      " myvarchar                varchar(2000) not null,     " +
      " myvarbinary              varbinary(2000) not null)   ";

      stmt.executeUpdate(sql);

      sql =
      "insert into #t0052               " +
      "  (myvarchar,                   " +
      "   myvarbinary)                 " +
      " values                         " +
      "  (\'This is a test with german umlauts 漩黒', " +
      "   0x4749463839610A000A0080FF00D73D1B0000002C000000000A000A00000208848FA9CBED0F632B003B" +
      "  )";
      stmt.executeUpdate(sql);

      sql = "select * from #t0052";
      ResultSet rs = stmt.executeQuery(sql);
      if (!rs.next()) {
        passed = false;
      }
      else {
        output.println("Testing getAsciiStream()");
        InputStream in = rs.getAsciiStream("myvarchar");
        String expect = "This is a test with german umlauts ???";
        byte[] toRead = new byte[expect.length()];
        count = in.read(toRead);
        if (count == expect.length()) {
          for (i=0; i<expect.length(); i++) {
            if (expect.charAt(i) != toRead[i]) {
              passed = false;
              output.println("Expected "+expect.charAt(i)
              + " but was "
              + toRead[i]);
            }
          }
        } else {
          passed = false;
          output.println("Premature end in "
          + "getAsciiStream(\"myvarchar\") "
          + count + " instead of "
          + expect.length());
        }
        in.close();

        in = rs.getAsciiStream(2);
        toRead = new byte[41];
        count = in.read(toRead);
        if (count == 41) {
          for (i=0; i<41; i++) {
            if (toRead[i] != (toRead[i] & 0x7F)) {
              passed = false;
              output.println("Non ASCII characters in getAsciiStream");
              break;
            }
          }
        } else {
          passed = false;
          output.println("Premature end in getAsciiStream(1) "
          +count+" instead of 41");
        }
        in.close();

        output.println("Testing getUnicodeStream()");
        Reader reader = rs.getCharacterStream("myvarchar");
        expect = "This is a test with german umlauts 漩

⌨️ 快捷键说明

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