bug fixed for PreparedStatementImpl

This commit is contained in:
wenshao
2013-05-26 13:59:59 +08:00
parent aba3bac949
commit abd919c6fd
6 changed files with 241 additions and 9 deletions
@@ -48,7 +48,7 @@ public class DruidDataSourceTest_notEmptyWait2 extends TestCase {
startLatch.countDown();
try {
Connection conn = dataSource.getConnection();
Thread.sleep(1);
Thread.sleep(2);
conn.close();
} catch (Exception e) {
// e.printStackTrace();
@@ -0,0 +1,76 @@
package com.alibaba.druid.bvt.pool;
import java.math.BigDecimal;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.Types;
import junit.framework.TestCase;
import org.junit.Assert;
import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.proxy.jdbc.PreparedStatementProxyImpl;
public class PreparedStatementProxyImplTest extends TestCase {
private DruidDataSource dataSource;
protected void setUp() throws Exception {
dataSource = new DruidDataSource();
dataSource.setUrl("jdbc:mock:xxx");
dataSource.setInitialSize(1);
dataSource.setFilters("log4j");
}
protected void tearDown() throws Exception {
dataSource.close();
}
public void test_setObject() throws Exception {
String sql = "insert t values(?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
Connection conn = dataSource.getConnection();
PreparedStatement stmt = conn.prepareStatement(sql);
stmt.setObject(1, (byte) 1);
stmt.setObject(2, (short) 1);
stmt.setObject(3, (int) 1);
stmt.setObject(4, (long) 1);
stmt.setObject(5, (float) 1);
stmt.setObject(6, (double) 1);
stmt.setObject(7, new BigDecimal(1));
stmt.setObject(8, true);
stmt.setObject(9, "xxx");
stmt.setObject(10, new java.sql.Date(System.currentTimeMillis()));
stmt.setObject(11, new java.util.Date(System.currentTimeMillis()));
stmt.setObject(12, new java.sql.Timestamp(System.currentTimeMillis()));
stmt.setObject(13, new java.sql.Time(System.currentTimeMillis()));
stmt.execute();
PreparedStatementProxyImpl stmtProxy = stmt.unwrap(PreparedStatementProxyImpl.class);
Assert.assertNotNull(stmtProxy);
Assert.assertEquals(Types.TINYINT, stmtProxy.getParameters().get(1).getSqlType());
Assert.assertEquals(Types.SMALLINT, stmtProxy.getParameters().get(2).getSqlType());
Assert.assertEquals(Types.INTEGER, stmtProxy.getParameters().get(3).getSqlType());
Assert.assertEquals(Types.BIGINT, stmtProxy.getParameters().get(4).getSqlType());
Assert.assertEquals(Types.FLOAT, stmtProxy.getParameters().get(5).getSqlType());
Assert.assertEquals(Types.DOUBLE, stmtProxy.getParameters().get(6).getSqlType());
Assert.assertEquals(Types.DECIMAL, stmtProxy.getParameters().get(7).getSqlType());
Assert.assertEquals(Types.BOOLEAN, stmtProxy.getParameters().get(8).getSqlType());
Assert.assertEquals(Types.VARCHAR, stmtProxy.getParameters().get(9).getSqlType());
Assert.assertEquals(Types.DATE, stmtProxy.getParameters().get(10).getSqlType());
Assert.assertEquals(Types.DATE, stmtProxy.getParameters().get(11).getSqlType());
Assert.assertEquals(Types.TIMESTAMP, stmtProxy.getParameters().get(12).getSqlType());
Assert.assertEquals(Types.TIME, stmtProxy.getParameters().get(13).getSqlType());
stmt.close();
conn.close();
}
}
@@ -0,0 +1,55 @@
/*
* Copyright 1999-2011 Alibaba Group Holding Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alibaba.druid.bvt.sql.mysql;
import java.util.List;
import org.junit.Assert;
import com.alibaba.druid.sql.MysqlTest;
import com.alibaba.druid.sql.SQLUtils;
import com.alibaba.druid.sql.ast.SQLStatement;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlInsertStatement;
import com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser;
import com.alibaba.druid.sql.dialect.mysql.visitor.MySqlSchemaStatVisitor;
public class MySqlInsertTest_7 extends MysqlTest {
public void test_0() throws Exception {
String sql = "insert into Apply (applicant_id, applytime, applytype, approver_id, end, process, start) values (?, ?, ?, ?, ?, ?, ?)";
MySqlStatementParser parser = new MySqlStatementParser(sql);
List<SQLStatement> statementList = parser.parseStatementList();
SQLStatement stmt = statementList.get(0);
MySqlInsertStatement insertStmt = (MySqlInsertStatement) stmt;
Assert.assertEquals(7, insertStmt.getColumns().size());
Assert.assertEquals(1, insertStmt.getValuesList().size());
Assert.assertEquals(7, insertStmt.getValuesList().get(0).getValues().size());
Assert.assertEquals(1, statementList.size());
MySqlSchemaStatVisitor visitor = new MySqlSchemaStatVisitor();
stmt.accept(visitor);
Assert.assertEquals("INSERT INTO Apply (applicant_id, applytime, applytype, approver_id, end" + //
"\n\t, process, start)" + //
"\nVALUES (?, ?, ?, ?, ?" + //
"\n\t, ?, ?)", //
SQLUtils.toMySqlString(insertStmt));
}
}