From 40e1551ac2941c3404326e4ab1fc1f8a0c6e6399 Mon Sep 17 00:00:00 2001 From: wenshao Date: Sat, 15 Jun 2013 11:17:02 +0800 Subject: [PATCH 1/6] add Slf4jFilter testcase --- .../druid/bvt/filter/Slf4jFilterTest.java | 38 +++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 src/test/java/com/alibaba/druid/bvt/filter/Slf4jFilterTest.java diff --git a/src/test/java/com/alibaba/druid/bvt/filter/Slf4jFilterTest.java b/src/test/java/com/alibaba/druid/bvt/filter/Slf4jFilterTest.java new file mode 100644 index 0000000000..67c86685f1 --- /dev/null +++ b/src/test/java/com/alibaba/druid/bvt/filter/Slf4jFilterTest.java @@ -0,0 +1,38 @@ +package com.alibaba.druid.bvt.filter; + +import java.sql.Connection; + +import org.junit.Assert; + +import junit.framework.TestCase; + +import com.alibaba.druid.filter.logging.Slf4jLogFilter; +import com.alibaba.druid.pool.DruidDataSource; +import com.alibaba.druid.util.JdbcUtils; + +public class Slf4jFilterTest extends TestCase { + + private DruidDataSource dataSource; + + protected void setUp() throws Exception { + dataSource = new DruidDataSource(); + + dataSource.setUrl("jdbc:mock:xxx"); + dataSource.setFilters("slf4j"); + dataSource.setDbType("mysql"); + } + + protected void tearDown() throws Exception { + JdbcUtils.close(dataSource); + } + + public void test_slf4j() throws Exception { + dataSource.init(); + + Slf4jLogFilter filter = dataSource.unwrap(Slf4jLogFilter.class); + Assert.assertNotNull(filter); + + Connection conn = dataSource.getConnection(); + conn.close(); + } +} From a3b70e92e162f1832f2776bfc1e457ab81af4f79 Mon Sep 17 00:00:00 2001 From: wenshao Date: Sat, 15 Jun 2013 11:55:34 +0800 Subject: [PATCH 2/6] remove unused code & add testcase --- .../druid/sql/visitor/SQLShardingContext.java | 21 ----- .../bvt/pool/xa/JtdsXAConnectionTest.java | 83 +++++++++++++++++++ 2 files changed, 83 insertions(+), 21 deletions(-) delete mode 100644 src/main/java/com/alibaba/druid/sql/visitor/SQLShardingContext.java create mode 100644 src/test/java/com/alibaba/druid/bvt/pool/xa/JtdsXAConnectionTest.java diff --git a/src/main/java/com/alibaba/druid/sql/visitor/SQLShardingContext.java b/src/main/java/com/alibaba/druid/sql/visitor/SQLShardingContext.java deleted file mode 100644 index ecae764c5f..0000000000 --- a/src/main/java/com/alibaba/druid/sql/visitor/SQLShardingContext.java +++ /dev/null @@ -1,21 +0,0 @@ -/* - * 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.sql.visitor; - - -public class SQLShardingContext { - -} diff --git a/src/test/java/com/alibaba/druid/bvt/pool/xa/JtdsXAConnectionTest.java b/src/test/java/com/alibaba/druid/bvt/pool/xa/JtdsXAConnectionTest.java new file mode 100644 index 0000000000..491ea58334 --- /dev/null +++ b/src/test/java/com/alibaba/druid/bvt/pool/xa/JtdsXAConnectionTest.java @@ -0,0 +1,83 @@ +package com.alibaba.druid.bvt.pool.xa; + +import java.lang.reflect.Constructor; + +import junit.framework.TestCase; +import net.sourceforge.jtds.jdbc.JtdsConnection; + +import org.junit.Assert; + +import com.alibaba.druid.pool.xa.JtdsXAConnection; +import com.alibaba.druid.pool.xa.JtdsXAResource; + +public class JtdsXAConnectionTest extends TestCase { + + public void test_jtds() throws Exception { + Constructor constrcutor = JtdsConnection.class.getDeclaredConstructor(); + constrcutor.setAccessible(true); + JtdsConnection jtdsConn = constrcutor.newInstance(); + JtdsXAConnection xaConn = new JtdsXAConnection(jtdsConn); + Assert.assertSame(jtdsConn, xaConn.getConnection()); + + JtdsXAResource xaResource = (JtdsXAResource) xaConn.getXAResource(); + Assert.assertTrue(xaResource.isSameRM(xaResource)); + Assert.assertFalse(xaResource.isSameRM(null)); + + { + Exception error = null; + try { + xaResource.commit(null, true); + } catch (Exception ex) { + error = ex; + } + Assert.assertNotNull(error); + } + { + Exception error = null; + try { + xaResource.start(null, 0); + } catch (Exception ex) { + error = ex; + } + Assert.assertNotNull(error); + } + { + Exception error = null; + try { + xaResource.end(null, 0); + } catch (Exception ex) { + error = ex; + } + Assert.assertNotNull(error); + } + { + Exception error = null; + try { + xaResource.forget(null); + } catch (Exception ex) { + error = ex; + } + Assert.assertNotNull(error); + } + { + Exception error = null; + try { + xaResource.rollback(null); + } catch (Exception ex) { + error = ex; + } + Assert.assertNotNull(error); + } + xaResource.recover(0); + { + Exception error = null; + try { + xaResource.prepare(null); + } catch (Exception ex) { + error = ex; + } + Assert.assertNotNull(error); + } + xaConn.close(); + } +} From 3fb946946bf43d6c5060f5d673881c2bac7f39f9 Mon Sep 17 00:00:00 2001 From: wenshao Date: Sat, 15 Jun 2013 11:57:45 +0800 Subject: [PATCH 3/6] remove unused code --- .../sql/dialect/postgresql/ast/PGOrderBy.java | 15 ---- .../dialect/postgresql/ast/PGOrderByItem.java | 87 ------------------- 2 files changed, 102 deletions(-) delete mode 100644 src/main/java/com/alibaba/druid/sql/dialect/postgresql/ast/PGOrderByItem.java diff --git a/src/main/java/com/alibaba/druid/sql/dialect/postgresql/ast/PGOrderBy.java b/src/main/java/com/alibaba/druid/sql/dialect/postgresql/ast/PGOrderBy.java index e5fd6ca6ea..61251ad9b8 100644 --- a/src/main/java/com/alibaba/druid/sql/dialect/postgresql/ast/PGOrderBy.java +++ b/src/main/java/com/alibaba/druid/sql/dialect/postgresql/ast/PGOrderBy.java @@ -50,19 +50,4 @@ public class PGOrderBy extends SQLOrderBy { visitor.endVisit(this); } - public void output(StringBuffer buf) { - buf.append("ORDER "); - if (this.sibings) { - buf.append("SIBLINGS "); - } - buf.append("BY "); - - int i = 0; - for (int size = this.items.size(); i < size; ++i) { - if (i != 0) { - buf.append(", "); - } - ((PGOrderByItem) this.items.get(i)).output(buf); - } - } } diff --git a/src/main/java/com/alibaba/druid/sql/dialect/postgresql/ast/PGOrderByItem.java b/src/main/java/com/alibaba/druid/sql/dialect/postgresql/ast/PGOrderByItem.java deleted file mode 100644 index 5994489ff8..0000000000 --- a/src/main/java/com/alibaba/druid/sql/dialect/postgresql/ast/PGOrderByItem.java +++ /dev/null @@ -1,87 +0,0 @@ -/* - * 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.sql.dialect.postgresql.ast; - -import com.alibaba.druid.sql.ast.SQLOrderingSpecification; -import com.alibaba.druid.sql.ast.statement.SQLSelectOrderByItem; -import com.alibaba.druid.sql.dialect.postgresql.visitor.PGASTVisitor; -import com.alibaba.druid.sql.visitor.SQLASTVisitor; - -public class PGOrderByItem extends SQLSelectOrderByItem { - - private static final long serialVersionUID = 1L; - - private NullsOrderType nullsOrderType; - - public PGOrderByItem(){ - - } - - public NullsOrderType getNullsOrderType() { - return this.nullsOrderType; - } - - public void setNullsOrderType(NullsOrderType nullsOrderType) { - this.nullsOrderType = nullsOrderType; - } - - protected void accept0(SQLASTVisitor visitor) { - if (visitor instanceof PGASTVisitor) { - accept0((PGASTVisitor) visitor); - } else { - super.accept0(visitor); - } - } - - protected void accept0(PGASTVisitor visitor) { - if (visitor.visit(this)) { - acceptChild(visitor, this.expr); - } - - visitor.endVisit(this); - } - - public void output(StringBuffer buf) { - this.expr.output(buf); - if (SQLOrderingSpecification.ASC.equals(this.type)) { - buf.append(" ASC"); - } else if (SQLOrderingSpecification.DESC.equals(this.type)) { - buf.append(" DESC"); - } - if (NullsOrderType.NullsFirst.equals(this.nullsOrderType)) { - buf.append(" NULLS FIRST"); - } else if (NullsOrderType.NullsLast.equals(this.nullsOrderType)) { - buf.append(" NULLS LAST"); - } - } - - public static enum NullsOrderType { - NullsFirst, NullsLast; - - public String toFormalString() { - if (NullsFirst.equals(this)) { - return "NULLS FIRST"; - } - - if (NullsLast.equals(this)) { - return "NULLS LAST"; - } - - throw new IllegalArgumentException(); - } - } - -} From 47e60c8fc8e6f16b61fc49054a73fc8e5f7f5027 Mon Sep 17 00:00:00 2001 From: wenshao Date: Sat, 15 Jun 2013 12:05:39 +0800 Subject: [PATCH 4/6] improve MSSQLValidConnectionChecker & add testcase --- .../vendor/MSSQLValidConnectionChecker.java | 135 +++++++++--------- .../MSSQLValidConnectionCheckerTest.java | 40 ++++++ 2 files changed, 109 insertions(+), 66 deletions(-) create mode 100644 src/test/java/com/alibaba/druid/bvt/pool/vendor/MSSQLValidConnectionCheckerTest.java diff --git a/src/main/java/com/alibaba/druid/pool/vendor/MSSQLValidConnectionChecker.java b/src/main/java/com/alibaba/druid/pool/vendor/MSSQLValidConnectionChecker.java index 32b5ccbd0a..838d0696f3 100644 --- a/src/main/java/com/alibaba/druid/pool/vendor/MSSQLValidConnectionChecker.java +++ b/src/main/java/com/alibaba/druid/pool/vendor/MSSQLValidConnectionChecker.java @@ -1,66 +1,69 @@ -/* - * 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.pool.vendor; - -import java.io.Serializable; -import java.sql.Connection; -import java.sql.SQLException; -import java.sql.Statement; - -import com.alibaba.druid.pool.ValidConnectionChecker; -import com.alibaba.druid.pool.ValidConnectionCheckerAdapter; -import com.alibaba.druid.support.logging.Log; -import com.alibaba.druid.support.logging.LogFactory; -import com.alibaba.druid.util.JdbcUtils; - -/** - * A MSSQLValidConnectionChecker. - */ -public class MSSQLValidConnectionChecker extends ValidConnectionCheckerAdapter implements ValidConnectionChecker, Serializable { - - private static final long serialVersionUID = 1L; - - private static final String QUERY = "SELECT 1"; - private static final Log LOG = LogFactory.getLog(MSSQLValidConnectionChecker.class); - - public boolean isValidConnection(final Connection c, String valiateQuery, int validationQueryTimeout) { - try { - if (c.isClosed()) { - return false; - } - } catch (SQLException ex) { - // skip - return false; - } - - Statement stmt = null; - - try { - stmt = c.createStatement(); - stmt.setQueryTimeout(validationQueryTimeout); - stmt.execute(QUERY); - return true; - } catch (SQLException e) { - if (LOG.isWarnEnabled()) { - LOG.warn("warning: connection validation failed for current managed connection."); - } - return false; - } finally { - JdbcUtils.close(stmt); - } - } - -} +/* + * 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.pool.vendor; + +import java.io.Serializable; +import java.sql.Connection; +import java.sql.SQLException; +import java.sql.Statement; + +import com.alibaba.druid.pool.ValidConnectionChecker; +import com.alibaba.druid.pool.ValidConnectionCheckerAdapter; +import com.alibaba.druid.support.logging.Log; +import com.alibaba.druid.support.logging.LogFactory; +import com.alibaba.druid.util.JdbcUtils; + +/** + * A MSSQLValidConnectionChecker. + */ +public class MSSQLValidConnectionChecker extends ValidConnectionCheckerAdapter implements ValidConnectionChecker, Serializable { + + private static final long serialVersionUID = 1L; + + private static final Log LOG = LogFactory.getLog(MSSQLValidConnectionChecker.class); + + public MSSQLValidConnectionChecker(){ + + } + + public boolean isValidConnection(final Connection c, String valiateQuery, int validationQueryTimeout) { + try { + if (c.isClosed()) { + return false; + } + } catch (SQLException ex) { + // skip + return false; + } + + Statement stmt = null; + + try { + stmt = c.createStatement(); + stmt.setQueryTimeout(validationQueryTimeout); + stmt.execute(valiateQuery); + return true; + } catch (SQLException e) { + if (LOG.isWarnEnabled()) { + LOG.warn("warning: connection validation failed for current managed connection."); + } + return false; + } finally { + JdbcUtils.close(stmt); + } + } + +} diff --git a/src/test/java/com/alibaba/druid/bvt/pool/vendor/MSSQLValidConnectionCheckerTest.java b/src/test/java/com/alibaba/druid/bvt/pool/vendor/MSSQLValidConnectionCheckerTest.java new file mode 100644 index 0000000000..4f6f525078 --- /dev/null +++ b/src/test/java/com/alibaba/druid/bvt/pool/vendor/MSSQLValidConnectionCheckerTest.java @@ -0,0 +1,40 @@ +package com.alibaba.druid.bvt.pool.vendor; + +import java.sql.SQLException; + +import junit.framework.TestCase; + +import org.junit.Assert; + +import com.alibaba.druid.mock.MockConnection; +import com.alibaba.druid.pool.vendor.MSSQLValidConnectionChecker; + +public class MSSQLValidConnectionCheckerTest extends TestCase { + + public void test_0() throws Exception { + MSSQLValidConnectionChecker checker = new MSSQLValidConnectionChecker(); + + MockConnection conn = new MockConnection(); + + Assert.assertTrue(checker.isValidConnection(conn, "select 1", 10)); + + } + + public void test_closed() throws Exception { + MSSQLValidConnectionChecker checker = new MSSQLValidConnectionChecker(); + + MockConnection conn = new MockConnection(); + + conn.close(); + Assert.assertFalse(checker.isValidConnection(conn, "select 1", 10)); + } + + public void test_error() throws Exception { + MSSQLValidConnectionChecker checker = new MSSQLValidConnectionChecker(); + + MockConnection conn = new MockConnection(); + conn.setError(new SQLException()); + + Assert.assertFalse(checker.isValidConnection(conn, "select 1", 10)); + } +} From a4a31362bfff7446142fae65581631f2a2931d79 Mon Sep 17 00:00:00 2001 From: wenshao Date: Sat, 15 Jun 2013 12:18:00 +0800 Subject: [PATCH 5/6] add testcase --- .../bvt/spring/SqlMapSessionWrapperTest.java | 39 +++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 src/test/java/com/alibaba/druid/bvt/spring/SqlMapSessionWrapperTest.java diff --git a/src/test/java/com/alibaba/druid/bvt/spring/SqlMapSessionWrapperTest.java b/src/test/java/com/alibaba/druid/bvt/spring/SqlMapSessionWrapperTest.java new file mode 100644 index 0000000000..29e440b234 --- /dev/null +++ b/src/test/java/com/alibaba/druid/bvt/spring/SqlMapSessionWrapperTest.java @@ -0,0 +1,39 @@ +package com.alibaba.druid.bvt.spring; + +import junit.framework.TestCase; + +import org.junit.Assert; +import org.springframework.context.support.ClassPathXmlApplicationContext; + +import com.alibaba.druid.support.ibatis.SqlMapSessionWrapper; +import com.ibatis.sqlmap.engine.impl.SqlMapClientImpl; +import com.ibatis.sqlmap.engine.impl.SqlMapSessionImpl; + +public class SqlMapSessionWrapperTest extends TestCase { + + private ClassPathXmlApplicationContext context = null; + + protected void setUp() throws Exception { + context = new ClassPathXmlApplicationContext("com/alibaba/druid/pool/ibatis/spring-config-ibatis.xml"); + } + + protected void tearDown() throws Exception { + context.close(); + } + + @SuppressWarnings("deprecation") + public void test_wrap() throws Exception { + SqlMapClientImpl client = (SqlMapClientImpl) context.getBean("master-sqlMapClient"); + Assert.assertNotNull(client); + + SqlMapSessionImpl session = new SqlMapSessionImpl(client); + SqlMapSessionWrapper wrapper = new SqlMapSessionWrapper(client, session); + + wrapper.startTransaction(); + wrapper.commitTransaction(); + wrapper.getDataSource(); + wrapper.getCurrentConnection(); + wrapper.getUserConnection(); + wrapper.close(); + } +} From 2aabdaea4b938e4b1185eda9bd03e9e4484769c1 Mon Sep 17 00:00:00 2001 From: wenshao Date: Sat, 15 Jun 2013 12:37:11 +0800 Subject: [PATCH 6/6] add slf4j logging testcase --- .../com/alibaba/druid/wall/WallFilter.java | 7 ------ .../bvt/support/logging/SLF4JImplTest.java | 24 +++++++++++++++++++ 2 files changed, 24 insertions(+), 7 deletions(-) create mode 100644 src/test/java/com/alibaba/druid/bvt/support/logging/SLF4JImplTest.java diff --git a/src/main/java/com/alibaba/druid/wall/WallFilter.java b/src/main/java/com/alibaba/druid/wall/WallFilter.java index 08e843db7d..5b0285221a 100644 --- a/src/main/java/com/alibaba/druid/wall/WallFilter.java +++ b/src/main/java/com/alibaba/druid/wall/WallFilter.java @@ -23,7 +23,6 @@ import java.util.List; import java.util.Map; import java.util.Set; -import com.alibaba.druid.DruidRuntimeException; import com.alibaba.druid.filter.FilterAdapter; import com.alibaba.druid.filter.FilterChain; import com.alibaba.druid.proxy.jdbc.CallableStatementProxy; @@ -159,12 +158,6 @@ public class WallFilter extends FilterAdapter implements WallFilterMBean { return inited; } - public void checkInit() { - if (inited) { - throw new DruidRuntimeException("wall filter is inited"); - } - } - @Override public void statement_addBatch(FilterChain chain, StatementProxy statement, String sql) throws SQLException { createWallContext(statement); diff --git a/src/test/java/com/alibaba/druid/bvt/support/logging/SLF4JImplTest.java b/src/test/java/com/alibaba/druid/bvt/support/logging/SLF4JImplTest.java new file mode 100644 index 0000000000..5e9d684bb2 --- /dev/null +++ b/src/test/java/com/alibaba/druid/bvt/support/logging/SLF4JImplTest.java @@ -0,0 +1,24 @@ +package com.alibaba.druid.bvt.support.logging; + +import junit.framework.TestCase; + +import com.alibaba.druid.pool.DruidDataSource; +import com.alibaba.druid.support.logging.SLF4JImpl; + +public class SLF4JImplTest extends TestCase { + + public void test_0() throws Exception { + SLF4JImpl impl = new SLF4JImpl(DruidDataSource.class.getName()); + + impl.isDebugEnabled(); + impl.isInfoEnabled(); + impl.isWarnEnabled(); + impl.debug(""); + impl.debug("", new Exception()); + impl.info(""); + impl.warn(""); + impl.warn("", new Exception()); + impl.error(""); + impl.error("", new Exception()); + } +}