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

📄 csunittest.java

📁 Java写的TDS协议(JDBC/ODBC)实现
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
/*
 * CSUnitTest.java
 *
 * Created on 8. September 2001, 07:54
 */
package net.sourceforge.jtds.test;

import java.sql.*;
import java.math.BigDecimal;
import junit.framework.TestCase;
import junit.framework.TestSuite;
import junit.framework.AssertionFailedError;
import java.io.*;
import java.lang.reflect.*;

import net.sourceforge.jtds.util.Logger;

/**
 *
 * @author  builder
 * @version 1.0
 */
public class CSUnitTest extends DatabaseTestCase {

  public CSUnitTest(String name) {
    super(name);
    if (output == null)
      try {
        output = new PrintStream(new FileOutputStream("nul"));
      }
      catch (FileNotFoundException ex) {
        throw new RuntimeException("could not create device nul");
      }
  }

  static PrintStream output = null;

  public static void main(String args[]) {
    Logger.setActive(true);
    if (args.length > 0) {
      output = System.out;
      junit.framework.TestSuite s = new TestSuite();
      for (int i = 0; i < args.length; i++) {
        s.addTest(new CSUnitTest(args[i]));
      }
      junit.textui.TestRunner.run(s);
    }
    else {
      junit.textui.TestRunner.run(CSUnitTest.class);
    }
  }


  public void testMaxRows0003() throws Exception {
    dropTable("#t0003");
    Statement stmt = con.createStatement();

    int count = stmt.executeUpdate("create table #t0003              "
    + "  (i  integer not null)       ");
    PreparedStatement  pStmt = con.prepareStatement(
    "insert into #t0003 values (?)");

    final int rowsToAdd = 100;
    count = 0;
    for(int i=1; i<=rowsToAdd; i++) {
      pStmt.setInt(1, i);
      count += pStmt.executeUpdate();
    }
    assertTrue("count: " + count + " rowsToAdd: " + rowsToAdd,count == rowsToAdd);
    pStmt.close();
    pStmt = con.prepareStatement(
    "select i from #t0003 order by i");
    int   rowLimit = 32;
    pStmt.setMaxRows(rowLimit);

    assertTrue(pStmt.getMaxRows() == rowLimit);
    ResultSet  rs = pStmt.executeQuery();
    count = 0;
    while(rs.next()) {
      int   n = rs.getInt("i");
      count++;
    }
    assertTrue(count == rowLimit);
  }



  public void testGetAsciiStream0018() throws Exception {
    int         count    = 0;
    Statement   stmt     = con.createStatement();
    ResultSet   rs;

    String bigtext1 =
    "abcdefghijklmnop" +
    "abcdefghijklmnop" +
    "abcdefghijklmnop" +
    "abcdefghijklmnop" +
    "abcdefghijklmnop" +
    "abcdefghijklmnop" +
    "abcdefghijklmnop" +
    "abcdefghijklmnop" +
    "abcdefghijklmnop" +
    "abcdefghijklmnop" +
    "abcdefghijklmnop" +
    "abcdefghijklmnop" +
    "abcdefghijklmnop" +
    "abcdefghijklmnop" +
    "abcdefghijklmnop" +
    "abcdefghijklmnop" +
    "abcdefghijklmnop" +
    "abcdefghijklmnop" +
    "abcdefghijklmnop" +
    "abcdefghijklmnop" +
    "abcdefghijklmnop" +
    "abcdefghijklmnop" +
    "abcdefghijklmnop" +
    "abcdefghijklmnop" +
    "abcdefghijklmnop" +
    "abcdefghijklmnop" +
    "abcdefghijklmnop" +
    "abcdefghijklmnop" +
    "abcdefghijklmnop" +
    "abcdefghijklmnop" +
    "";
    String bigimage1 = "0x" +
    "0123456789abcdef" +
    "fedcba9876543210" +
    "fedcba9876543210" +
    "fedcba9876543210" +
    "fedcba9876543210" +
    "fedcba9876543210" +
    "fedcba9876543210" +
    "fedcba9876543210" +
    "fedcba9876543210" +
    "fedcba9876543210" +
    "fedcba9876543210" +
    "fedcba9876543210" +
    "fedcba9876543210" +
    "fedcba9876543210" +
    "fedcba9876543210" +
    "fedcba9876543210" +
    "fedcba9876543210" +
    "fedcba9876543210" +
    "fedcba9876543210" +
    "fedcba9876543210" +
    "fedcba9876543210" +
    "fedcba9876543210" +
    "fedcba9876543210" +
    "fedcba9876543210" +
    "fedcba9876543210" +
    "fedcba9876543210" +
    "fedcba9876543210" +
    "fedcba9876543210" +
    "fedcba9876543210" +
    "fedcba9876543210" +
    "fedcba9876543210" +
    "fedcba9876543210" +
    "fedcba9876543210" +
    "fedcba9876543210" +
    "fedcba9876543210" +
    "fedcba9876543210" +
    "fedcba9876543210" +
    "fedcba9876543210" +
    "fedcba9876543210" +
    "fedcba9876543210" +
    "fedcba9876543210" +
    "fedcba9876543210" +
    "fedcba9876543210" +
    "fedcba9876543210" +
    "fedcba9876543210" +
    "fedcba9876543210" +
    "fedcba9876543210" +
    "fedcba9876543210" +
    "fedcba9876543210" +
    "fedcba9876543210" +
    "fedcba9876543210" +
    "fedcba9876543210" +
    "fedcba9876543210" +
    "fedcba9876543210" +
    "fedcba9876543210" +
    "fedcba9876543210" +
    "fedcba9876543210" +
    "fedcba9876543210" +
    "fedcba9876543210" +
    "fedcba9876543210" +
    "fedcba9876543210" +
    "fedcba9876543210" +
    "fedcba9876543210" +
    "fedcba9876543210" +
    "";
    dropTable("#t0018");
    String sql =
    "create table #t0018 (                                  " +
    " mybinary                   binary(5) not null,       " +
    " myvarbinary                varbinary(4) not null,    " +
    " mychar                     char(10) not null,        " +
    " myvarchar                  varchar(8) not null,      " +
    " mytext                     text not null,            " +
    " myimage                    image not null,           " +
    " mynullbinary               binary(3) null,           " +
    " mynullvarbinary            varbinary(6) null,        " +
    " mynullchar                 char(10) null,            " +
    " mynullvarchar              varchar(40) null,         " +
    " mynulltext                 text null,                " +
    " mynullimage                image null)               ";

    count = stmt.executeUpdate(sql);
    // Insert a row without nulls via a Statement
    sql =
    "insert into #t0018(       " +
    " mybinary,               " +
    " myvarbinary,            " +
    " mychar,                 " +
    " myvarchar,              " +
    " mytext,                 " +
    " myimage,                " +
    " mynullbinary,           " +
    " mynullvarbinary,        " +
    " mynullchar,             " +
    " mynullvarchar,          " +
    " mynulltext,             " +
    " mynullimage             " +
    ")                        " +
    "values(                  " +
    " 0xffeeddccbb,           " +  // mybinary
    " 0x78,                   " +  // myvarbinary
    " 'Z',                    " +  // mychar
    " '',                     " +  // myvarchar
    " '" + bigtext1 + "',     " +  // mytext
    " " + bigimage1 + ",      " +  // myimage
    " null,                   " +  // mynullbinary
    " null,                   " +  // mynullvarbinary
    " null,                   " +  // mynullchar
    " null,                   " +  // mynullvarchar
    " null,                   " +  // mynulltext
    " null                    " +  // mynullimage
    ")";


    count = stmt.executeUpdate(sql);

    sql = "select * from #t0018";
    rs = stmt.executeQuery(sql);
    if (!rs.next()) {
      assertTrue("should get Result",false);
    }
    else {
      output.println("Getting the results");
      output.println("mybinary is " + rs.getObject("mybinary"));
      output.println("myvarbinary is " + rs.getObject("myvarbinary"));
      output.println("mychar is " + rs.getObject("mychar"));
      output.println("myvarchar is " + rs.getObject("myvarchar"));
      output.println("mytext is " + rs.getObject("mytext"));
      output.println("myimage is " + rs.getObject("myimage"));
      output.println("mynullbinary is " + rs.getObject("mynullbinary"));
      output.println("mynullvarbinary is " + rs.getObject("mynullvarbinary"));
      output.println("mynullchar is " + rs.getObject("mynullchar"));
      output.println("mynullvarchar is " + rs.getObject("mynullvarchar"));
      output.println("mynulltext is " + rs.getObject("mynulltext"));
      output.println("mynullimage is " + rs.getObject("mynullimage"));
    }
  }


  public void testMoneyHandling0019() throws Exception {
    java.sql.Statement  stmt   = null;
    BigDecimal          tmp1   = null;
    BigDecimal          tmp2   = null;
    int                 i;
    BigDecimal          money[] = {
      new BigDecimal("922337203685477.5807"),
      new BigDecimal("-922337203685477.5807"),
      new BigDecimal("1.00"),
      new BigDecimal("0.00"),
      new BigDecimal("-1.00")
    };
    BigDecimal          smallmoney[] = {
      new BigDecimal("214748.3647"),
      new BigDecimal("-214748.3648"),
      new BigDecimal("1.00"),
      new BigDecimal("0.00"),
      new BigDecimal("-1.00")
    };

    if (smallmoney.length != money.length) {
      throw new SQLException("Must have same number of elements in " +
      "money and smallmoney");
    }

    stmt = con.createStatement();

    dropTable("#t0019");


    stmt.executeUpdate("create table #t0019 (                     " +
    "  i               integer primary key,   " +
    "  mymoney         money not null,        " +
    "  mysmallmoney    smallmoney not null)   " +
    "");

    for(i=0; i<money.length; i++) {
      stmt.executeUpdate("insert into #t0019 values (" +
      i + ", " + money[i] + ",   " +
      smallmoney[i] + ")         ");
    }


    // long l = System.currentTimeMillis();
    // while (l + 500 > System.currentTimeMillis()) ;
    ResultSet rs = stmt.executeQuery("select * from #t0019 order by i");

    for(i=0; rs.next(); i++) {
      BigDecimal  m;
      BigDecimal  sm;

      m = (BigDecimal)rs.getObject("mymoney");
      sm = (BigDecimal)rs.getObject("mysmallmoney");

      money[i].setScale(2, BigDecimal.ROUND_DOWN);
      smallmoney[i].setScale(2, BigDecimal.ROUND_DOWN);

      assertTrue(m.equals(money[i].setScale(2, BigDecimal.ROUND_DOWN)));
      assertTrue(sm.equals(smallmoney[i].setScale(2, BigDecimal.ROUND_DOWN)));

      output.println(m + ", " + sm);
    }
  }

  /*
  public void testBooleanAndCompute0026() throws Exception {
    Statement   stmt = con.createStatement();
    dropTable("#t0026");
    int count = stmt.executeUpdate("create table #t0026             " +
    "  (i      integer,             " +
    "   b      bit,                 " +
    "   s      char(5),             " +
    "   f      float)               ");
    output.println("Creating table affected " + count + " rows");

    stmt.executeUpdate("insert into #t0026 values(0, 0, 'false', 0.0)");
    stmt.executeUpdate("insert into #t0026 values(0, 0, 'N', 10)");
    stmt.executeUpdate("insert into #t0026 values(1, 1, 'true', 7.0)");
    stmt.executeUpdate("insert into #t0026 values(2, 1, 'Y', -5.0)");

    ResultSet  rs = stmt.executeQuery(
    "select * from #t0026 order by i compute sum(f) by i");

    assertTrue(rs.next());

    assertTrue(!(rs.getBoolean("i")
    || rs.getBoolean("b")
    || rs.getBoolean("s")
    || rs.getBoolean("f")));

    assertTrue(rs.next());

    assertTrue(!(rs.getBoolean("i")
    || rs.getBoolean("b")
    || rs.getBoolean("s")
    || rs.getBoolean("f")));
    assertTrue(rs.next());

    assertTrue(rs.getBoolean("i")
    && rs.getBoolean("b")
    && rs.getBoolean("s")
    && rs.getBoolean("f"));
    assertTrue(rs.next());

    assertTrue(rs.getBoolean("i")
    && rs.getBoolean("b")
    && rs.getBoolean("s")
    && rs.getBoolean("f"));

    ResultSet  rs = stmt.executeQuery(
       "select * from #t0026 order by i compute sum(f) by i");



    if (!rs.next())
    {
       throw new SQLException("Failed");
    }
    passed = passed && (! (rs.getBoolean("i")
                           || rs.getBoolean("b")
                           || rs.getBoolean("s")
                           || rs.getBoolean("f")));


    if (!rs.next())
    {
       throw new SQLException("Failed");
    }
    passed = passed && (! (rs.getBoolean("i")
                           || rs.getBoolean("b")
                           || rs.getBoolean("s")
                           || rs.getBoolean("f")));


    if (!rs.next())
    {
       throw new SQLException("Failed");
    }
    passed = passed && (rs.getBoolean("i")
                        && rs.getBoolean("b")
                        && rs.getBoolean("s")
                        && rs.getBoolean("f"));

    if (!rs.next())
    {
       throw new SQLException("Failed");
    }
    passed = passed && (rs.getBoolean("i")
                        && rs.getBoolean("b")
                        && rs.getBoolean("s")
                        && rs.getBoolean("f"));

   assertTrue(passed);
  }
  */

  public void testDataTypes0027() throws Exception {
    output.println("Test all the SQLServer datatypes in Statement\n"
    + "and PreparedStatement using the preferred getXXX()\n"
    + "instead of getObject like #t0017.java does.");
    output.println("!!!Note- This test is not fully implemented yet!!!");
    Statement   stmt = con.createStatement();
    ResultSet   rs;

⌨️ 快捷键说明

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