mirror of
https://github.com/alibaba/druid.git
synced 2023-03-04 19:17:16 +00:00
Merge pull request #368 from wenshao/master
remove unused code & add testcase
This commit is contained in:
+69
-66
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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 {
|
||||
|
||||
}
|
||||
@@ -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);
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
}
|
||||
+40
@@ -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));
|
||||
}
|
||||
}
|
||||
@@ -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<JtdsConnection> 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();
|
||||
}
|
||||
}
|
||||
@@ -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();
|
||||
}
|
||||
}
|
||||
@@ -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());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user