Merge pull request #72 from wenshao/master

添加License信息和单元测试
This commit is contained in:
温高铁
2012-08-26 03:00:36 -07:00
5 changed files with 193 additions and 24 deletions
@@ -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;
@@ -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;
@@ -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;
@@ -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;
}
}
}
@@ -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();
}