safetest.java
来自「jtds的源码 是你学习java的好东西」· Java 代码 · 共 1,764 行 · 第 1/5 页
JAVA
1,764 行
int counter = 0;
while (rs.next()) {
assertEquals(format.parse(dates[counter]), rs.getTimestamp(1));
++counter;
}
// Close everything
rs.close();
stmt.close();
pstmt.close();
}
/**
* Test bug #926620 - Too long value for VARCHAR field.
*/
/* does not work with SQL 6.5
public void testCursorLargeCharInsert0017() throws Exception {
Statement stmt = con.createStatement(
ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
stmt.execute("CREATE TABLE #SAfe0017(value VARCHAR(10) PRIMARY KEY)");
// Create the updateable ResultSet
ResultSet rs = stmt.executeQuery(
"SELECT value FROM #SAfe0017");
// Try inserting a character string less than 10 characters long
rs.moveToInsertRow();
rs.updateString(1, "Test");
rs.insertRow();
rs.moveToCurrentRow();
rs.last();
// Check that we do indeed have one row in the ResultSet now
assertEquals(1, rs.getRow());
// Try inserting a character string more than 10 characters long
rs.moveToInsertRow();
rs.updateString(1, "Testing: 1, 2, 3...");
try {
rs.insertRow();
fail("Should cause an SQLException with native error number 8152"
+ "and SQL state 22001");
} catch (SQLException ex) {
// assertEquals("22001", ex.getSQLState());
assertTrue(ex instanceof DataTruncation);
}
// Close everything
rs.close();
stmt.close();
}*/
/**
* Test for bug [939206] TdsException: can't sent this BigDecimal
*/
public void testBigDecimal1() throws Exception {
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("SELECT @@MAX_PRECISION");
assertTrue(rs.next());
int maxPrecision = rs.getInt(1);
rs.close();
BigDecimal maxval = new BigDecimal("1E+" + maxPrecision);
maxval = maxval.subtract(new BigDecimal(1));
// maxval now = 99999999999999999999999999999999999999
if (maxPrecision > 28) {
stmt.execute("create table #testBigDecimal1 (id int primary key, data01 decimal(38,0), data02 decimal(38,12) null, data03 money)");
} else {
stmt.execute("create table #testBigDecimal1 (id int primary key, data01 decimal(28,0), data02 decimal(28,12) null, data03 money)");
}
PreparedStatement pstmt = con.prepareStatement("insert into #testBigDecimal1 (id, data01, data02, data03) values (?,?,?,?)");
pstmt.setInt(1, 1);
try {
pstmt.setBigDecimal(2, maxval.add(new BigDecimal(1)));
assertTrue(false); // Should fail
} catch (SQLException e) {
// System.out.println(e.getMessage());
// OK Genuinely can't send this one!
}
pstmt.setBigDecimal(2, maxval);
pstmt.setBigDecimal(3, new BigDecimal(1.0 / 3.0)); // Scale > 38
pstmt.setBigDecimal(4, new BigDecimal("12345.56789"));
assertTrue(pstmt.executeUpdate() == 1);
pstmt.close();
rs = stmt.executeQuery("SELECT * FROM #testBigDecimal1");
assertTrue(rs.next());
assertEquals(maxval, rs.getBigDecimal(2));
assertEquals(new BigDecimal("0.333333333333"), rs.getBigDecimal(3)); // Rounded to scale 10
assertEquals(new BigDecimal("12345.5679"), rs.getBigDecimal(4)); // Money has scale of 4
rs.close();
maxval = maxval.negate();
Statement stmt2 = con.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_UPDATABLE);
rs = stmt2.executeQuery("SELECT * FROM #testBigDecimal1");
SQLWarning warn = stmt.getWarnings();
while (warn != null) {
System.out.println(warn.getMessage());
warn = warn.getNextWarning();
}
assertTrue(rs.next());
rs.updateBigDecimal("data01", maxval);
rs.updateNull("data02");
rs.updateObject("data03", new BigDecimal("-12345.56789"), 2); // Round to scale 2
rs.updateRow();
rs.close();
stmt2.close();
rs = stmt.executeQuery("SELECT * FROM #testBigDecimal1");
assertTrue(rs.next());
assertEquals(maxval, rs.getBigDecimal(2));
assertEquals(null, rs.getBigDecimal(3));
assertEquals(new BigDecimal("-12345.5700"), rs.getBigDecimal(4));
rs.close();
stmt.close();
}
/**
* Test for bug [963799] float values change when written to the database
*/
public void testFloat1() throws Exception {
float value = 2.2f;
Statement stmt = con.createStatement();
stmt.execute("create table #testFloat1 (data decimal(28,10))");
stmt.close();
PreparedStatement pstmt = con.prepareStatement("insert into #testFloat1 (data) values (?)");
pstmt.setFloat(1, value);
assertTrue(pstmt.executeUpdate() == 1);
pstmt.close();
pstmt = con.prepareStatement("select data from #testFloat1");
ResultSet rs = pstmt.executeQuery();
assertTrue(rs.next());
assertTrue(value == rs.getFloat(1));
assertTrue(!rs.next());
pstmt.close();
rs.close();
}
/**
* Test for bug [983561] getDatetimeValue truncates fractional milliseconds
*/
public void testDatetimeRounding1() throws Exception {
long dateTime = 1089297738677L;
Timestamp value = new Timestamp(dateTime);
Statement stmt = con.createStatement();
stmt.execute("create table #dtr1 (data datetime)");
stmt.close();
PreparedStatement pstmt = con.prepareStatement("insert into #dtr1 (data) values (?)");
pstmt.setTimestamp(1, value);
assertTrue(pstmt.executeUpdate() == 1);
pstmt.close();
pstmt = con.prepareStatement("select data from #dtr1");
ResultSet rs = pstmt.executeQuery();
assertTrue(rs.next());
assertTrue(value.equals(rs.getTimestamp(1)));
assertTrue(!rs.next());
pstmt.close();
rs.close();
}
public void testSocketConcurrency1() {
final Connection con = this.con;
final int threadCount = 10, loopCount = 10;
final Vector errors = new Vector();
// DriverManager.setLogStream(System.out);
// Create a huge query
StringBuffer queryBuffer = new StringBuffer(4100);
queryBuffer.append("SELECT '");
while (queryBuffer.length() < 2000) {
queryBuffer.append("0123456789");
}
queryBuffer.append("' AS value1, '");
while (queryBuffer.length() < 4000) {
queryBuffer.append("9876543210");
}
queryBuffer.append("' AS value2");
final String query = queryBuffer.toString();
Thread heavyThreads[] = new Thread[threadCount],
lightThreads[] = new Thread[threadCount];
// Create threadCount heavy threads
for (int i = 0; i < threadCount; i++) {
heavyThreads[i] = new Thread() {
public void run() {
try {
Statement stmt = con.createStatement();
for (int i = 0; i < loopCount; i++) {
stmt.execute(query);
}
stmt.close();
} catch (SQLException ex) {
ex.printStackTrace();
errors.add(ex);
}
}
};
}
// Create threadCount light threads
for (int i = 0; i < threadCount; i++) {
lightThreads[i] = new Thread() {
public void run() {
try { sleep(100); } catch (InterruptedException ex) {}
try {
Statement stmt = con.createStatement();
for (int i = 0; i < loopCount; i++) {
stmt.execute("SELECT 1");
}
stmt.close();
} catch (Exception ex) {
ex.printStackTrace();
errors.add(ex);
}
}
};
}
for (int i = 0; i < threadCount; i++) {
heavyThreads[i].start();
lightThreads[i].start();
}
for (int i = 0; i < threadCount; i++) {
try { heavyThreads[i].join(); } catch (InterruptedException ex) {}
try { lightThreads[i].join(); } catch (InterruptedException ex) {}
}
assertEquals(0, errors.size());
}
public void testSocketConcurrency2() {
final Connection con = this.con;
final int threadCount = 10, loopCount = 10;
final Vector errors = new Vector();
// DriverManager.setLogStream(System.out);
// Create a huge query
StringBuffer valueBuffer = new StringBuffer(4000);
while (valueBuffer.length() < 4000) {
valueBuffer.append("0123456789");
}
final String value = valueBuffer.toString();
Thread heavyThreads[] = new Thread[threadCount],
lightThreads[] = new Thread[threadCount];
// Create threadCount heavy threads
for (int i = 0; i < threadCount; i++) {
heavyThreads[i] = new Thread() {
public void run() {
try {
PreparedStatement pstmt = con.prepareStatement(
"SELECT ? AS value1, ? AS value2");
pstmt.setString(1, value);
pstmt.setString(2, value);
for (int i = 0; i < loopCount; i++) {
pstmt.execute();
}
pstmt.close();
} catch (SQLException ex) {
ex.printStackTrace();
errors.add(ex);
}
}
};
}
// Create threadCount light threads
for (int i = 0; i < threadCount; i++) {
lightThreads[i] = new Thread() {
public void run() {
try { sleep(100); } catch (InterruptedException ex) {}
try {
Statement stmt = con.createStatement();
for (int i = 0; i < loopCount; i++) {
stmt.execute("SELECT 1");
}
stmt.close();
} catch (Exception ex) {
ex.printStackTrace();
errors.add(ex);
}
}
};
}
for (int i = 0; i < threadCount; i++) {
heavyThreads[i].start();
lightThreads[i].start();
}
for (int i = 0; i < threadCount; i++) {
try { heavyThreads[i].join(); } catch (InterruptedException ex) {}
try { lightThreads[i].join(); } catch (InterruptedException ex) {}
}
assertEquals(0, errors.size());
}
public void testSocketConcurrency3() {
final Connection con = this.con;
final int threadCount = 10, loopCount = 10;
final Vector errors = new Vector();
// DriverManager.setLogStream(System.out);
// Create a huge query
StringBuffer valueBuffer = new StringBuffer(4000);
while (valueBuffer.length() < 4000) {
valueBuffer.append("0123456789");
}
final String value = valueBuffer.toString();
Thread heavyThreads[] = new Thread[threadCount],
lightThreads[] = new Thread[threadCount];
// Create threadCount heavy threads
for (int i = 0; i < threadCount; i++) {
heavyThreads[i] = new Thread() {
public void run() {
try {
PreparedStatement pstmt = con.prepareStatement(
"SELECT ? AS value1, ? AS value2");
pstmt.setString(1, value);
pstmt.setString(2, value);
for (int i = 0; i < loopCount; i++) {
pstmt.execute();
}
pstmt.close();
} catch (SQLException ex) {
ex.printStackTrace();
errors.add(ex);
}
}
};
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?