From 65ba64fee7a175a0a057fb61eaac3431d4630e83 Mon Sep 17 00:00:00 2001 From: wenshao Date: Sun, 26 Aug 2012 17:29:56 +0800 Subject: [PATCH 1/3] add testcase --- .../druid/bvt/pool/DruidDataSourceTest4.java | 102 ++++++++++++++++++ 1 file changed, 102 insertions(+) create mode 100644 src/test/java/com/alibaba/druid/bvt/pool/DruidDataSourceTest4.java diff --git a/src/test/java/com/alibaba/druid/bvt/pool/DruidDataSourceTest4.java b/src/test/java/com/alibaba/druid/bvt/pool/DruidDataSourceTest4.java new file mode 100644 index 0000000000..a6c6f5da8d --- /dev/null +++ b/src/test/java/com/alibaba/druid/bvt/pool/DruidDataSourceTest4.java @@ -0,0 +1,102 @@ +package com.alibaba.druid.bvt.pool; + +import java.sql.SQLException; + +import junit.framework.Assert; +import junit.framework.TestCase; + +import com.alibaba.druid.pool.DruidDataSource; +import com.alibaba.druid.pool.ExceptionSorter; +import com.alibaba.druid.pool.vendor.MySqlValidConnectionChecker; + +public class DruidDataSourceTest4 extends TestCase { + + private DruidDataSource dataSource; + + protected void setUp() throws Exception { + dataSource = new DruidDataSource(); + dataSource.setUrl("jdbc:mock:xxx"); + dataSource.setTestOnBorrow(false); + dataSource.setInitialSize(1); + + } + + protected void tearDown() throws Exception { + dataSource.close(); + } + + public void test_getTransactionThresholdMillis() { + Assert.assertEquals(0, dataSource.getTransactionThresholdMillis()); + + dataSource.setTransactionThresholdMillis(100); + + Assert.assertEquals(100, dataSource.getTransactionThresholdMillis()); + } + + public void test_getTransactionHistogramRanges() { + Assert.assertNotNull(dataSource.getTransactionHistogramRanges()); + } + + public void test_getTransactionHistogramRanges_1() { + Assert.assertEquals(5, dataSource.getTransactionHistogramRanges().length); + } + + public void test_setValidConnectionCheckerClassName() throws Exception { + Assert.assertNull(dataSource.getValidConnectionChecker()); + dataSource.setValidConnectionCheckerClassName(MySqlValidConnectionChecker.class.getName()); + Assert.assertNotNull(dataSource.getValidConnectionChecker()); + } + + public void test_setMinIdle() throws Exception { + Assert.assertEquals(0, dataSource.getMinIdle()); + dataSource.init(); + dataSource.setMinIdle(1); + Assert.assertEquals(1, dataSource.getMinIdle()); + } + + public void test_setMinIdle_error() throws Exception { + Assert.assertEquals(0, dataSource.getMinIdle()); + dataSource.init(); + Exception error = null; + try { + dataSource.setMinIdle(100); + } catch (Exception e) { + error = e; + } + Assert.assertNotNull(error); + Assert.assertEquals(0, dataSource.getMinIdle()); + } + + public void test_setExceptionSorter_error() throws Exception { + dataSource.setExceptionSorter("xx"); + } + + public void test_setExceptionSorter_error2() throws Exception { + Exception error = null; + try { + dataSource.setExceptionSorter(MyExceptionSorter.class.getName()); + } catch (Exception e) { + error = e; + } + Assert.assertNotNull(error); + } + + public void test_getParentLogger() throws Exception { + Exception error = null; + try { + dataSource.getParentLogger(); + } catch (Exception e) { + error = e; + } + Assert.assertNotNull(error); + } + + private class MyExceptionSorter implements ExceptionSorter { + + @Override + public boolean isExceptionFatal(SQLException e) { + return false; + } + + } +} From 5efff43abccaf06974158d355f0aff7a36056ef7 Mon Sep 17 00:00:00 2001 From: wenshao Date: Sun, 26 Aug 2012 17:40:49 +0800 Subject: [PATCH 2/3] add testcase --- .../DruidPooledCallableStatementTest.java | 70 ++++++++++++------- 1 file changed, 46 insertions(+), 24 deletions(-) diff --git a/src/test/java/com/alibaba/druid/bvt/pool/DruidPooledCallableStatementTest.java b/src/test/java/com/alibaba/druid/bvt/pool/DruidPooledCallableStatementTest.java index 639b3afbd1..bc829e9e3d 100644 --- a/src/test/java/com/alibaba/druid/bvt/pool/DruidPooledCallableStatementTest.java +++ b/src/test/java/com/alibaba/druid/bvt/pool/DruidPooledCallableStatementTest.java @@ -21,6 +21,8 @@ public class DruidPooledCallableStatementTest extends TestCase { private DruidDataSource dataSource; + private boolean throwError = true; + protected void setUp() throws Exception { dataSource = new DruidDataSource(); @@ -32,7 +34,11 @@ public class DruidPooledCallableStatementTest extends TestCase { public boolean callableStatement_wasNull(FilterChain chain, CallableStatementProxy statement) throws SQLException { + if (throwError) { throw new SQLException(); + } else { + return chain.callableStatement_wasNull(statement); + } } }); } @@ -41,11 +47,27 @@ public class DruidPooledCallableStatementTest extends TestCase { dataSource.close(); } - public void test_wasNull() throws Exception { + public void test_wasNull_noerror() throws Exception { Connection conn = dataSource.getConnection(); CallableStatement stmt = conn.prepareCall("select 1"); stmt.execute(); + throwError = false; + stmt.wasNull(); + + Assert.assertEquals(0, dataSource.getErrorCount()); + + stmt.close(); + conn.close(); + + Assert.assertEquals(1, dataSource.getPoolingCount()); + } + + public void test_wasNull_error() throws Exception { + Connection conn = dataSource.getConnection(); + CallableStatement stmt = conn.prepareCall("select 1"); + stmt.execute(); + Assert.assertEquals(0, dataSource.getErrorCount()); Exception error = null; @@ -63,14 +85,14 @@ public class DruidPooledCallableStatementTest extends TestCase { Assert.assertEquals(1, dataSource.getPoolingCount()); } - + public void test_getObject() throws Exception { Connection conn = dataSource.getConnection(); DruidPooledCallableStatement stmt = (DruidPooledCallableStatement) conn.prepareCall("select 1"); stmt.execute(); Assert.assertEquals(0, dataSource.getErrorCount()); - + Exception error = null; try { stmt.getObject(1, String.class); @@ -78,22 +100,22 @@ public class DruidPooledCallableStatementTest extends TestCase { error = e; } Assert.assertNotNull(error); - + Assert.assertEquals(0, dataSource.getErrorCount()); - + stmt.close(); conn.close(); - + Assert.assertEquals(1, dataSource.getPoolingCount()); } - + public void test_getObject_1() throws Exception { Connection conn = dataSource.getConnection(); DruidPooledCallableStatement stmt = (DruidPooledCallableStatement) conn.prepareCall("select 1"); stmt.execute(); - + Assert.assertEquals(0, dataSource.getErrorCount()); - + Exception error = null; try { stmt.getObject("1", String.class); @@ -101,48 +123,48 @@ public class DruidPooledCallableStatementTest extends TestCase { error = e; } Assert.assertNotNull(error); - + Assert.assertEquals(0, dataSource.getErrorCount()); - + stmt.close(); conn.close(); - + Assert.assertEquals(1, dataSource.getPoolingCount()); } - + public void test_wrap() throws Exception { - + Connection conn = dataSource.getConnection(); CallableStatement stmt = conn.prepareCall("select 1"); - + Assert.assertNotNull(stmt.unwrap(CallableStatement.class)); Assert.assertEquals(MockCallableStatement.class, stmt.unwrap(CallableStatement.class).getClass()); - + stmt.close(); conn.close(); } - + public void test_wrap_1() throws Exception { - + Connection conn = dataSource.getConnection(); CallableStatement stmt = conn.prepareCall("select 1"); - + Assert.assertNotNull(stmt.unwrap(PreparedStatement.class)); Assert.assertEquals(MockCallableStatement.class, stmt.unwrap(CallableStatement.class).getClass()); - + stmt.close(); conn.close(); } - + public void test_wrap_2() throws Exception { dataSource.getProxyFilters().clear(); - + Connection conn = dataSource.getConnection(); CallableStatement stmt = conn.prepareCall("select 1"); - + Assert.assertNotNull(stmt.unwrap(PreparedStatement.class)); Assert.assertEquals(MockCallableStatement.class, stmt.unwrap(CallableStatement.class).getClass()); - + stmt.close(); conn.close(); } From c3f4b79de498b063a5b106babf4077b5a4e8b901 Mon Sep 17 00:00:00 2001 From: wenshao Date: Sun, 26 Aug 2012 17:59:23 +0800 Subject: [PATCH 3/3] add license --- .../support/jconsole/model/DruidTableModel.java | 15 +++++++++++++++ .../support/jconsole/model/RowHeaderTable.java | 15 +++++++++++++++ .../support/jconsole/util/TableDataProcessor.java | 15 +++++++++++++++ 3 files changed, 45 insertions(+) diff --git a/src/main/java/com/alibaba/druid/support/jconsole/model/DruidTableModel.java b/src/main/java/com/alibaba/druid/support/jconsole/model/DruidTableModel.java index 77f5cfbe67..36f69fdfe3 100644 --- a/src/main/java/com/alibaba/druid/support/jconsole/model/DruidTableModel.java +++ b/src/main/java/com/alibaba/druid/support/jconsole/model/DruidTableModel.java @@ -1,3 +1,18 @@ +/* + * 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.support.jconsole.model; import java.util.ArrayList; diff --git a/src/main/java/com/alibaba/druid/support/jconsole/model/RowHeaderTable.java b/src/main/java/com/alibaba/druid/support/jconsole/model/RowHeaderTable.java index 561a1a5551..0689652ebd 100644 --- a/src/main/java/com/alibaba/druid/support/jconsole/model/RowHeaderTable.java +++ b/src/main/java/com/alibaba/druid/support/jconsole/model/RowHeaderTable.java @@ -1,3 +1,18 @@ +/* + * 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.support.jconsole.model; import java.awt.Color; diff --git a/src/main/java/com/alibaba/druid/support/jconsole/util/TableDataProcessor.java b/src/main/java/com/alibaba/druid/support/jconsole/util/TableDataProcessor.java index 8b58e6f438..8e937b8b0d 100644 --- a/src/main/java/com/alibaba/druid/support/jconsole/util/TableDataProcessor.java +++ b/src/main/java/com/alibaba/druid/support/jconsole/util/TableDataProcessor.java @@ -1,3 +1,18 @@ +/* + * 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.support.jconsole.util; import java.util.ArrayList;