This is an automated email from the ASF dual-hosted git repository. ggregory pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/commons-dbcp.git
The following commit(s) were added to refs/heads/master by this push: new a3e63014 Format nits a3e63014 is described below commit a3e630144a7bca279e3cd4900d65140503c109d5 Author: Gary Gregory <gardgreg...@gmail.com> AuthorDate: Sun Jul 10 08:26:21 2022 -0400 Format nits --- .../commons/dbcp2/BasicDataSourceFactory.java | 2 +- .../org/apache/commons/dbcp2/TestPStmtKey.java | 526 ++--- .../org/apache/commons/dbcp2/TesterResultSet.java | 2384 ++++++++++---------- .../dbcp2/cpdsadapter/TestDriverAdapterCPDS.java | 804 +++---- .../datasources/TestCPDSConnectionFactory.java | 2 +- 5 files changed, 1859 insertions(+), 1859 deletions(-) diff --git a/src/main/java/org/apache/commons/dbcp2/BasicDataSourceFactory.java b/src/main/java/org/apache/commons/dbcp2/BasicDataSourceFactory.java index b99a8e1b..457bc9e0 100644 --- a/src/main/java/org/apache/commons/dbcp2/BasicDataSourceFactory.java +++ b/src/main/java/org/apache/commons/dbcp2/BasicDataSourceFactory.java @@ -152,7 +152,7 @@ public class BasicDataSourceFactory implements ObjectFactory { PROP_MAX_OPEN_PREPARED_STATEMENTS, PROP_CONNECTION_PROPERTIES, PROP_MAX_CONN_LIFETIME_MILLIS, PROP_LOG_EXPIRED_CONNECTIONS, PROP_ROLLBACK_ON_RETURN, PROP_ENABLE_AUTO_COMMIT_ON_RETURN, PROP_DEFAULT_QUERY_TIMEOUT, PROP_FAST_FAIL_VALIDATION, PROP_DISCONNECTION_SQL_CODES, PROP_JMX_NAME, - PROP_REGISTER_CONNECTION_MBEAN, PROP_CONNECTION_FACTORY_CLASS_NAME }; + PROP_REGISTER_CONNECTION_MBEAN, PROP_CONNECTION_FACTORY_CLASS_NAME}; /** * Obsolete properties from DBCP 1.x. with warning strings suggesting new properties. LinkedHashMap will guarantee diff --git a/src/test/java/org/apache/commons/dbcp2/TestPStmtKey.java b/src/test/java/org/apache/commons/dbcp2/TestPStmtKey.java index e2d745ae..2e8e4979 100644 --- a/src/test/java/org/apache/commons/dbcp2/TestPStmtKey.java +++ b/src/test/java/org/apache/commons/dbcp2/TestPStmtKey.java @@ -1,263 +1,263 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You 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 org.apache.commons.dbcp2; - -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertNotEquals; -import static org.junit.jupiter.api.Assertions.assertNull; -import static org.junit.jupiter.api.Assertions.assertTrue; - -import java.sql.ResultSet; -import java.sql.Statement; -import java.util.Arrays; - -import org.apache.commons.dbcp2.PoolingConnection.StatementType; -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.Test; - -/** - * Tests {@link PStmtKey}. - * - * @since 2.4.0 - */ -public class TestPStmtKey { - - /** - * Tests constructors with different catalog. - */ - @Test - public void testCtorDifferentCatalog() { - Assertions.assertNotEquals(new PStmtKey("sql", "catalog1", "schema1"), new PStmtKey("sql", "catalog2", "schema1")); - Assertions.assertNotEquals(new PStmtKey("sql", "catalog1", "schema1", 0), - new PStmtKey("sql", "catalog2", "schema1", 0)); - Assertions.assertNotEquals(new PStmtKey("sql", "catalog1", "schema1", 0, 0), - new PStmtKey("sql", "catalog2", "schema1", 0, 0)); - Assertions.assertNotEquals(new PStmtKey("sql", "catalog1", "schema1", 0, 0, 0), - new PStmtKey("sql", "catalog2", "schema1", 0, 0, 0)); - // - Assertions.assertNotEquals(new PStmtKey("sql", "catalog1", "schema1", 0, 0, 0, null), - new PStmtKey("sql", "catalog2", "schema1", 0, 0, 0, null)); - Assertions.assertNotEquals(new PStmtKey("sql", "catalog1", "schema1", 0, 0, 0, StatementType.PREPARED_STATEMENT), - new PStmtKey("sql", "catalog2", "schema1", 0, 0, 0, StatementType.PREPARED_STATEMENT)); - // - Assertions.assertNotEquals(new PStmtKey("sql", "catalog1", "schema1", 0, 0, null), - new PStmtKey("sql", "catalog2", "schema1", 0, 0, null)); - Assertions.assertNotEquals(new PStmtKey("sql", "catalog1", "schema1", 0, 0, StatementType.PREPARED_STATEMENT), - new PStmtKey("sql", "catalog2", "schema1", 0, 0, StatementType.PREPARED_STATEMENT)); - // - Assertions.assertNotEquals(new PStmtKey("sql", "catalog1", "schema1", (int[]) null), - new PStmtKey("sql", "catalog2", "schema1", (int[]) null)); - Assertions.assertNotEquals(new PStmtKey("sql", "catalog1", "schema1", new int[1]), - new PStmtKey("sql", "catalog2", "schema1", new int[1])); - Assertions.assertNotEquals(new PStmtKey("sql", "catalog1", "schema1", (String[]) null), - new PStmtKey("sql", "catalog2", "schema1", (String[]) null)); - Assertions.assertNotEquals(new PStmtKey("sql", "catalog1", "schema1", new String[] {"A" }), - new PStmtKey("sql", "catalog2", "schema1", new String[] {"A" })); - Assertions.assertNotEquals(new PStmtKey("sql", "catalog1", "schema1", StatementType.PREPARED_STATEMENT), - new PStmtKey("sql", "catalog2", "schema1", StatementType.PREPARED_STATEMENT)); - Assertions.assertNotEquals( - new PStmtKey("sql", "catalog1", "schema1", StatementType.PREPARED_STATEMENT, Integer.MAX_VALUE), - new PStmtKey("sql", "catalog2", "schema1", StatementType.PREPARED_STATEMENT, Integer.MAX_VALUE)); - } - - /** - * Tests constructors with different schemas. - */ - @Test - public void testCtorDifferentSchema() { - Assertions.assertNotEquals(new PStmtKey("sql", "catalog1", "schema1"), new PStmtKey("sql", "catalog1", "schema2")); - Assertions.assertNotEquals(new PStmtKey("sql", "catalog1", "schema1", 0), - new PStmtKey("sql", "catalog1", "schema2", 0)); - Assertions.assertNotEquals(new PStmtKey("sql", "catalog1", "schema1", 0, 0), - new PStmtKey("sql", "catalog1", "schema2", 0, 0)); - Assertions.assertNotEquals(new PStmtKey("sql", "catalog1", "schema1", 0, 0, 0), - new PStmtKey("sql", "catalog1", "schema2", 0, 0, 0)); - // - Assertions.assertNotEquals(new PStmtKey("sql", "catalog1", "schema1", 0, 0, 0, null), - new PStmtKey("sql", "catalog1", "schema2", 0, 0, 0, null)); - Assertions.assertNotEquals(new PStmtKey("sql", "catalog1", "schema1", 0, 0, 0, StatementType.PREPARED_STATEMENT), - new PStmtKey("sql", "catalog1", "schema2", 0, 0, 0, StatementType.PREPARED_STATEMENT)); - // - Assertions.assertNotEquals(new PStmtKey("sql", "catalog1", "schema1", 0, 0, null), - new PStmtKey("sql", "catalog1", "schema2", 0, 0, null)); - Assertions.assertNotEquals(new PStmtKey("sql", "catalog1", "schema1", 0, 0, StatementType.PREPARED_STATEMENT), - new PStmtKey("sql", "catalog1", "schema2", 0, 0, StatementType.PREPARED_STATEMENT)); - // - Assertions.assertNotEquals(new PStmtKey("sql", "catalog1", "schema1", (int[]) null), - new PStmtKey("sql", "catalog1", "schema2", (int[]) null)); - Assertions.assertNotEquals(new PStmtKey("sql", "catalog1", "schema1", new int[1]), - new PStmtKey("sql", "catalog1", "schema2", new int[1])); - Assertions.assertNotEquals(new PStmtKey("sql", "catalog1", "schema1", (String[]) null), - new PStmtKey("sql", "catalog1", "schema2", (String[]) null)); - Assertions.assertNotEquals(new PStmtKey("sql", "catalog1", "schema1", new String[] {"A" }), - new PStmtKey("sql", "catalog1", "schema2", new String[] {"A" })); - Assertions.assertNotEquals(new PStmtKey("sql", "catalog1", "schema1", StatementType.PREPARED_STATEMENT), - new PStmtKey("sql", "catalog1", "schema2", StatementType.PREPARED_STATEMENT)); - Assertions.assertNotEquals( - new PStmtKey("sql", "catalog1", "schema1", StatementType.PREPARED_STATEMENT, Integer.MAX_VALUE), - new PStmtKey("sql", "catalog1", "schema2", StatementType.PREPARED_STATEMENT, Integer.MAX_VALUE)); - } - - /** - * Tests constructors with different catalog. - */ - @Test - public void testCtorEquals() { - Assertions.assertEquals(new PStmtKey("sql", "catalog1", "schema1"), new PStmtKey("sql", "catalog1", "schema1")); - Assertions.assertEquals(new PStmtKey("sql", "catalog1", "schema1", 0), - new PStmtKey("sql", "catalog1", "schema1", 0)); - Assertions.assertEquals(new PStmtKey("sql", "catalog1", "schema1", 0, 0), - new PStmtKey("sql", "catalog1", "schema1", 0, 0)); - Assertions.assertEquals(new PStmtKey("sql", "catalog1", "schema1", 0, 0, 0), - new PStmtKey("sql", "catalog1", "schema1", 0, 0, 0)); - // - Assertions.assertEquals(new PStmtKey("sql", "catalog1", "schema1", 0, 0, 0, null), - new PStmtKey("sql", "catalog1", "schema1", 0, 0, 0, null)); - Assertions.assertEquals(new PStmtKey("sql", "catalog1", "schema1", 0, 0, 0, StatementType.PREPARED_STATEMENT), - new PStmtKey("sql", "catalog1", "schema1", 0, 0, 0, StatementType.PREPARED_STATEMENT)); - // - Assertions.assertEquals(new PStmtKey("sql", "catalog1", "schema1", 0, 0, null), - new PStmtKey("sql", "catalog1", "schema1", 0, 0, null)); - Assertions.assertEquals(new PStmtKey("sql", "catalog1", "schema1", 0, 0, StatementType.PREPARED_STATEMENT), - new PStmtKey("sql", "catalog1", "schema1", 0, 0, StatementType.PREPARED_STATEMENT)); - // - Assertions.assertEquals(new PStmtKey("sql", "catalog1", "schema1", (int[]) null), - new PStmtKey("sql", "catalog1", "schema1", (int[]) null)); - Assertions.assertEquals(new PStmtKey("sql", "catalog1", "schema1", new int[1]), - new PStmtKey("sql", "catalog1", "schema1", new int[1])); - Assertions.assertEquals(new PStmtKey("sql", "catalog1", "schema1", (String[]) null), - new PStmtKey("sql", "catalog1", "schema1", (String[]) null)); - Assertions.assertEquals(new PStmtKey("sql", "catalog1", "schema1", new String[] {"A" }), - new PStmtKey("sql", "catalog1", "schema1", new String[] {"A" })); - Assertions.assertEquals(new PStmtKey("sql", "catalog1", "schema1", StatementType.PREPARED_STATEMENT), - new PStmtKey("sql", "catalog1", "schema1", StatementType.PREPARED_STATEMENT)); - Assertions.assertEquals( - new PStmtKey("sql", "catalog1", "schema1", StatementType.PREPARED_STATEMENT, Integer.MAX_VALUE), - new PStmtKey("sql", "catalog1", "schema1", StatementType.PREPARED_STATEMENT, Integer.MAX_VALUE)); - } - - /** - * Tests {@link org.apache.commons.dbcp2.PStmtKey#PStmtKey(String, String, String, int[])}. - * - * See https://issues.apache.org/jira/browse/DBCP-494 - */ - @Test - public void testCtorStringStringArrayOfInts() { - final int[] input = {0, 0 }; - final PStmtKey pStmtKey = new PStmtKey("", "", "", input); - Assertions.assertArrayEquals(input, pStmtKey.getColumnIndexes()); - input[0] = 1; - input[1] = 1; - Assertions.assertFalse(Arrays.equals(input, pStmtKey.getColumnIndexes())); - } - - /** - * Tests {@link org.apache.commons.dbcp2.PStmtKey#PStmtKey(String, String, String, int[])}. - * - * See https://issues.apache.org/jira/browse/DBCP-494 - */ - @Test - public void testCtorStringStringArrayOfNullInts() { - final int[] input = null; - final PStmtKey pStmtKey = new PStmtKey("", "", "", input); - Assertions.assertArrayEquals(input, pStmtKey.getColumnIndexes()); - } - - /** - * Tests {@link org.apache.commons.dbcp2.PStmtKey#PStmtKey(String, String, String, String[])}. - * - * See https://issues.apache.org/jira/browse/DBCP-494 - */ - @Test - public void testCtorStringStringArrayOfNullStrings() { - final String[] input = null; - final PStmtKey pStmtKey = new PStmtKey("", "", "", input); - Assertions.assertArrayEquals(input, pStmtKey.getColumnNames()); - } - - /** - * Tests {@link org.apache.commons.dbcp2.PStmtKey#PStmtKey(String, String, String, String[])}. - * - * See https://issues.apache.org/jira/browse/DBCP-494 - */ - @Test - public void testCtorStringStringArrayOfStrings() { - final String[] input = {"A", "B" }; - final PStmtKey pStmtKey = new PStmtKey("", "", "", input); - Assertions.assertArrayEquals(input, pStmtKey.getColumnNames()); - input[0] = "C"; - input[1] = "D"; - Assertions.assertFalse(Arrays.equals(input, pStmtKey.getColumnNames())); - } - - @Test - public void testEquals() { - final PStmtKey pStmtKey = new PStmtKey("SELECT 1", "catalog", "public", - java.sql.ResultSet.TYPE_SCROLL_INSENSITIVE, java.sql.ResultSet.CONCUR_READ_ONLY, - StatementType.CALLABLE_STATEMENT); - assertEquals(pStmtKey, pStmtKey); - assertNotEquals(null, pStmtKey); - assertNotEquals(pStmtKey, new Object()); - assertNotEquals(pStmtKey, new PStmtKey("SELECT 2", "catalog", "public", - ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY, - StatementType.CALLABLE_STATEMENT)); - assertNotEquals(pStmtKey, new PStmtKey("SELECT 1", "anothercatalog", "public", - ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY, - StatementType.CALLABLE_STATEMENT)); - assertNotEquals(pStmtKey, new PStmtKey("SELECT 1", "catalog", "private", - ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY, - StatementType.CALLABLE_STATEMENT)); - assertNotEquals(pStmtKey, new PStmtKey("SELECT 1", "catalog", "public", - ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY, - StatementType.CALLABLE_STATEMENT)); - assertNotEquals(pStmtKey, new PStmtKey("SELECT 1", "catalog", "public", - ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE, - StatementType.CALLABLE_STATEMENT)); - assertNotEquals(pStmtKey, new PStmtKey("SELECT 1", "catalog", "public", - ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY, - StatementType.PREPARED_STATEMENT)); - assertEquals(pStmtKey, new PStmtKey("SELECT 1", "catalog", "public", - ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY, - StatementType.CALLABLE_STATEMENT)); - assertEquals(pStmtKey.hashCode(), new PStmtKey("SELECT 1", "catalog", "public", - java.sql.ResultSet.TYPE_SCROLL_INSENSITIVE, java.sql.ResultSet.CONCUR_READ_ONLY, - StatementType.CALLABLE_STATEMENT).hashCode()); - } - - @Test - public void testGettersSetters() { - final PStmtKey pStmtKey = new PStmtKey("SELECT 1", "catalog", "public"); - assertEquals("SELECT 1", pStmtKey.getSql()); - assertEquals("public", pStmtKey.getSchema()); - assertEquals("catalog", pStmtKey.getCatalog()); - assertNull(pStmtKey.getAutoGeneratedKeys()); - assertNull(pStmtKey.getResultSetConcurrency()); - assertNull(pStmtKey.getResultSetHoldability()); - assertNull(pStmtKey.getResultSetType()); - assertEquals(StatementType.PREPARED_STATEMENT, pStmtKey.getStmtType()); - } - - @Test - public void testToString() { - final PStmtKey pStmtKey = new PStmtKey("SELECT 1", "catalog", "public", - StatementType.CALLABLE_STATEMENT, Statement.RETURN_GENERATED_KEYS); - assertTrue(pStmtKey.toString().contains("sql=SELECT 1")); - assertTrue(pStmtKey.toString().contains("schema=public")); - assertTrue(pStmtKey.toString().contains("autoGeneratedKeys=1")); - assertTrue(pStmtKey.toString().contains("statementType=CALLABLE_STATEMENT")); - } -} +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 org.apache.commons.dbcp2; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotEquals; +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertTrue; + +import java.sql.ResultSet; +import java.sql.Statement; +import java.util.Arrays; + +import org.apache.commons.dbcp2.PoolingConnection.StatementType; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +/** + * Tests {@link PStmtKey}. + * + * @since 2.4.0 + */ +public class TestPStmtKey { + + /** + * Tests constructors with different catalog. + */ + @Test + public void testCtorDifferentCatalog() { + Assertions.assertNotEquals(new PStmtKey("sql", "catalog1", "schema1"), new PStmtKey("sql", "catalog2", "schema1")); + Assertions.assertNotEquals(new PStmtKey("sql", "catalog1", "schema1", 0), + new PStmtKey("sql", "catalog2", "schema1", 0)); + Assertions.assertNotEquals(new PStmtKey("sql", "catalog1", "schema1", 0, 0), + new PStmtKey("sql", "catalog2", "schema1", 0, 0)); + Assertions.assertNotEquals(new PStmtKey("sql", "catalog1", "schema1", 0, 0, 0), + new PStmtKey("sql", "catalog2", "schema1", 0, 0, 0)); + // + Assertions.assertNotEquals(new PStmtKey("sql", "catalog1", "schema1", 0, 0, 0, null), + new PStmtKey("sql", "catalog2", "schema1", 0, 0, 0, null)); + Assertions.assertNotEquals(new PStmtKey("sql", "catalog1", "schema1", 0, 0, 0, StatementType.PREPARED_STATEMENT), + new PStmtKey("sql", "catalog2", "schema1", 0, 0, 0, StatementType.PREPARED_STATEMENT)); + // + Assertions.assertNotEquals(new PStmtKey("sql", "catalog1", "schema1", 0, 0, null), + new PStmtKey("sql", "catalog2", "schema1", 0, 0, null)); + Assertions.assertNotEquals(new PStmtKey("sql", "catalog1", "schema1", 0, 0, StatementType.PREPARED_STATEMENT), + new PStmtKey("sql", "catalog2", "schema1", 0, 0, StatementType.PREPARED_STATEMENT)); + // + Assertions.assertNotEquals(new PStmtKey("sql", "catalog1", "schema1", (int[]) null), + new PStmtKey("sql", "catalog2", "schema1", (int[]) null)); + Assertions.assertNotEquals(new PStmtKey("sql", "catalog1", "schema1", new int[1]), + new PStmtKey("sql", "catalog2", "schema1", new int[1])); + Assertions.assertNotEquals(new PStmtKey("sql", "catalog1", "schema1", (String[]) null), + new PStmtKey("sql", "catalog2", "schema1", (String[]) null)); + Assertions.assertNotEquals(new PStmtKey("sql", "catalog1", "schema1", new String[] {"A" }), + new PStmtKey("sql", "catalog2", "schema1", new String[] {"A" })); + Assertions.assertNotEquals(new PStmtKey("sql", "catalog1", "schema1", StatementType.PREPARED_STATEMENT), + new PStmtKey("sql", "catalog2", "schema1", StatementType.PREPARED_STATEMENT)); + Assertions.assertNotEquals( + new PStmtKey("sql", "catalog1", "schema1", StatementType.PREPARED_STATEMENT, Integer.MAX_VALUE), + new PStmtKey("sql", "catalog2", "schema1", StatementType.PREPARED_STATEMENT, Integer.MAX_VALUE)); + } + + /** + * Tests constructors with different schemas. + */ + @Test + public void testCtorDifferentSchema() { + Assertions.assertNotEquals(new PStmtKey("sql", "catalog1", "schema1"), new PStmtKey("sql", "catalog1", "schema2")); + Assertions.assertNotEquals(new PStmtKey("sql", "catalog1", "schema1", 0), + new PStmtKey("sql", "catalog1", "schema2", 0)); + Assertions.assertNotEquals(new PStmtKey("sql", "catalog1", "schema1", 0, 0), + new PStmtKey("sql", "catalog1", "schema2", 0, 0)); + Assertions.assertNotEquals(new PStmtKey("sql", "catalog1", "schema1", 0, 0, 0), + new PStmtKey("sql", "catalog1", "schema2", 0, 0, 0)); + // + Assertions.assertNotEquals(new PStmtKey("sql", "catalog1", "schema1", 0, 0, 0, null), + new PStmtKey("sql", "catalog1", "schema2", 0, 0, 0, null)); + Assertions.assertNotEquals(new PStmtKey("sql", "catalog1", "schema1", 0, 0, 0, StatementType.PREPARED_STATEMENT), + new PStmtKey("sql", "catalog1", "schema2", 0, 0, 0, StatementType.PREPARED_STATEMENT)); + // + Assertions.assertNotEquals(new PStmtKey("sql", "catalog1", "schema1", 0, 0, null), + new PStmtKey("sql", "catalog1", "schema2", 0, 0, null)); + Assertions.assertNotEquals(new PStmtKey("sql", "catalog1", "schema1", 0, 0, StatementType.PREPARED_STATEMENT), + new PStmtKey("sql", "catalog1", "schema2", 0, 0, StatementType.PREPARED_STATEMENT)); + // + Assertions.assertNotEquals(new PStmtKey("sql", "catalog1", "schema1", (int[]) null), + new PStmtKey("sql", "catalog1", "schema2", (int[]) null)); + Assertions.assertNotEquals(new PStmtKey("sql", "catalog1", "schema1", new int[1]), + new PStmtKey("sql", "catalog1", "schema2", new int[1])); + Assertions.assertNotEquals(new PStmtKey("sql", "catalog1", "schema1", (String[]) null), + new PStmtKey("sql", "catalog1", "schema2", (String[]) null)); + Assertions.assertNotEquals(new PStmtKey("sql", "catalog1", "schema1", new String[] {"A" }), + new PStmtKey("sql", "catalog1", "schema2", new String[] {"A" })); + Assertions.assertNotEquals(new PStmtKey("sql", "catalog1", "schema1", StatementType.PREPARED_STATEMENT), + new PStmtKey("sql", "catalog1", "schema2", StatementType.PREPARED_STATEMENT)); + Assertions.assertNotEquals( + new PStmtKey("sql", "catalog1", "schema1", StatementType.PREPARED_STATEMENT, Integer.MAX_VALUE), + new PStmtKey("sql", "catalog1", "schema2", StatementType.PREPARED_STATEMENT, Integer.MAX_VALUE)); + } + + /** + * Tests constructors with different catalog. + */ + @Test + public void testCtorEquals() { + Assertions.assertEquals(new PStmtKey("sql", "catalog1", "schema1"), new PStmtKey("sql", "catalog1", "schema1")); + Assertions.assertEquals(new PStmtKey("sql", "catalog1", "schema1", 0), + new PStmtKey("sql", "catalog1", "schema1", 0)); + Assertions.assertEquals(new PStmtKey("sql", "catalog1", "schema1", 0, 0), + new PStmtKey("sql", "catalog1", "schema1", 0, 0)); + Assertions.assertEquals(new PStmtKey("sql", "catalog1", "schema1", 0, 0, 0), + new PStmtKey("sql", "catalog1", "schema1", 0, 0, 0)); + // + Assertions.assertEquals(new PStmtKey("sql", "catalog1", "schema1", 0, 0, 0, null), + new PStmtKey("sql", "catalog1", "schema1", 0, 0, 0, null)); + Assertions.assertEquals(new PStmtKey("sql", "catalog1", "schema1", 0, 0, 0, StatementType.PREPARED_STATEMENT), + new PStmtKey("sql", "catalog1", "schema1", 0, 0, 0, StatementType.PREPARED_STATEMENT)); + // + Assertions.assertEquals(new PStmtKey("sql", "catalog1", "schema1", 0, 0, null), + new PStmtKey("sql", "catalog1", "schema1", 0, 0, null)); + Assertions.assertEquals(new PStmtKey("sql", "catalog1", "schema1", 0, 0, StatementType.PREPARED_STATEMENT), + new PStmtKey("sql", "catalog1", "schema1", 0, 0, StatementType.PREPARED_STATEMENT)); + // + Assertions.assertEquals(new PStmtKey("sql", "catalog1", "schema1", (int[]) null), + new PStmtKey("sql", "catalog1", "schema1", (int[]) null)); + Assertions.assertEquals(new PStmtKey("sql", "catalog1", "schema1", new int[1]), + new PStmtKey("sql", "catalog1", "schema1", new int[1])); + Assertions.assertEquals(new PStmtKey("sql", "catalog1", "schema1", (String[]) null), + new PStmtKey("sql", "catalog1", "schema1", (String[]) null)); + Assertions.assertEquals(new PStmtKey("sql", "catalog1", "schema1", new String[] {"A" }), + new PStmtKey("sql", "catalog1", "schema1", new String[] {"A" })); + Assertions.assertEquals(new PStmtKey("sql", "catalog1", "schema1", StatementType.PREPARED_STATEMENT), + new PStmtKey("sql", "catalog1", "schema1", StatementType.PREPARED_STATEMENT)); + Assertions.assertEquals( + new PStmtKey("sql", "catalog1", "schema1", StatementType.PREPARED_STATEMENT, Integer.MAX_VALUE), + new PStmtKey("sql", "catalog1", "schema1", StatementType.PREPARED_STATEMENT, Integer.MAX_VALUE)); + } + + /** + * Tests {@link org.apache.commons.dbcp2.PStmtKey#PStmtKey(String, String, String, int[])}. + * + * See https://issues.apache.org/jira/browse/DBCP-494 + */ + @Test + public void testCtorStringStringArrayOfInts() { + final int[] input = {0, 0}; + final PStmtKey pStmtKey = new PStmtKey("", "", "", input); + Assertions.assertArrayEquals(input, pStmtKey.getColumnIndexes()); + input[0] = 1; + input[1] = 1; + Assertions.assertFalse(Arrays.equals(input, pStmtKey.getColumnIndexes())); + } + + /** + * Tests {@link org.apache.commons.dbcp2.PStmtKey#PStmtKey(String, String, String, int[])}. + * + * See https://issues.apache.org/jira/browse/DBCP-494 + */ + @Test + public void testCtorStringStringArrayOfNullInts() { + final int[] input = null; + final PStmtKey pStmtKey = new PStmtKey("", "", "", input); + Assertions.assertArrayEquals(input, pStmtKey.getColumnIndexes()); + } + + /** + * Tests {@link org.apache.commons.dbcp2.PStmtKey#PStmtKey(String, String, String, String[])}. + * + * See https://issues.apache.org/jira/browse/DBCP-494 + */ + @Test + public void testCtorStringStringArrayOfNullStrings() { + final String[] input = null; + final PStmtKey pStmtKey = new PStmtKey("", "", "", input); + Assertions.assertArrayEquals(input, pStmtKey.getColumnNames()); + } + + /** + * Tests {@link org.apache.commons.dbcp2.PStmtKey#PStmtKey(String, String, String, String[])}. + * + * See https://issues.apache.org/jira/browse/DBCP-494 + */ + @Test + public void testCtorStringStringArrayOfStrings() { + final String[] input = {"A", "B"}; + final PStmtKey pStmtKey = new PStmtKey("", "", "", input); + Assertions.assertArrayEquals(input, pStmtKey.getColumnNames()); + input[0] = "C"; + input[1] = "D"; + Assertions.assertFalse(Arrays.equals(input, pStmtKey.getColumnNames())); + } + + @Test + public void testEquals() { + final PStmtKey pStmtKey = new PStmtKey("SELECT 1", "catalog", "public", + java.sql.ResultSet.TYPE_SCROLL_INSENSITIVE, java.sql.ResultSet.CONCUR_READ_ONLY, + StatementType.CALLABLE_STATEMENT); + assertEquals(pStmtKey, pStmtKey); + assertNotEquals(null, pStmtKey); + assertNotEquals(pStmtKey, new Object()); + assertNotEquals(pStmtKey, new PStmtKey("SELECT 2", "catalog", "public", + ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY, + StatementType.CALLABLE_STATEMENT)); + assertNotEquals(pStmtKey, new PStmtKey("SELECT 1", "anothercatalog", "public", + ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY, + StatementType.CALLABLE_STATEMENT)); + assertNotEquals(pStmtKey, new PStmtKey("SELECT 1", "catalog", "private", + ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY, + StatementType.CALLABLE_STATEMENT)); + assertNotEquals(pStmtKey, new PStmtKey("SELECT 1", "catalog", "public", + ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY, + StatementType.CALLABLE_STATEMENT)); + assertNotEquals(pStmtKey, new PStmtKey("SELECT 1", "catalog", "public", + ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE, + StatementType.CALLABLE_STATEMENT)); + assertNotEquals(pStmtKey, new PStmtKey("SELECT 1", "catalog", "public", + ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY, + StatementType.PREPARED_STATEMENT)); + assertEquals(pStmtKey, new PStmtKey("SELECT 1", "catalog", "public", + ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY, + StatementType.CALLABLE_STATEMENT)); + assertEquals(pStmtKey.hashCode(), new PStmtKey("SELECT 1", "catalog", "public", + java.sql.ResultSet.TYPE_SCROLL_INSENSITIVE, java.sql.ResultSet.CONCUR_READ_ONLY, + StatementType.CALLABLE_STATEMENT).hashCode()); + } + + @Test + public void testGettersSetters() { + final PStmtKey pStmtKey = new PStmtKey("SELECT 1", "catalog", "public"); + assertEquals("SELECT 1", pStmtKey.getSql()); + assertEquals("public", pStmtKey.getSchema()); + assertEquals("catalog", pStmtKey.getCatalog()); + assertNull(pStmtKey.getAutoGeneratedKeys()); + assertNull(pStmtKey.getResultSetConcurrency()); + assertNull(pStmtKey.getResultSetHoldability()); + assertNull(pStmtKey.getResultSetType()); + assertEquals(StatementType.PREPARED_STATEMENT, pStmtKey.getStmtType()); + } + + @Test + public void testToString() { + final PStmtKey pStmtKey = new PStmtKey("SELECT 1", "catalog", "public", + StatementType.CALLABLE_STATEMENT, Statement.RETURN_GENERATED_KEYS); + assertTrue(pStmtKey.toString().contains("sql=SELECT 1")); + assertTrue(pStmtKey.toString().contains("schema=public")); + assertTrue(pStmtKey.toString().contains("autoGeneratedKeys=1")); + assertTrue(pStmtKey.toString().contains("statementType=CALLABLE_STATEMENT")); + } +} diff --git a/src/test/java/org/apache/commons/dbcp2/TesterResultSet.java b/src/test/java/org/apache/commons/dbcp2/TesterResultSet.java index effcc460..c67b4d1f 100644 --- a/src/test/java/org/apache/commons/dbcp2/TesterResultSet.java +++ b/src/test/java/org/apache/commons/dbcp2/TesterResultSet.java @@ -1,1192 +1,1192 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You 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 org.apache.commons.dbcp2; - -import java.io.InputStream; -import java.io.Reader; -import java.math.BigDecimal; -import java.nio.charset.StandardCharsets; -import java.sql.Array; -import java.sql.Blob; -import java.sql.Clob; -import java.sql.NClob; -import java.sql.Ref; -import java.sql.ResultSet; -import java.sql.ResultSetMetaData; -import java.sql.RowId; -import java.sql.SQLException; -import java.sql.SQLType; -import java.sql.SQLWarning; -import java.sql.SQLXML; -import java.sql.Statement; -import java.util.Calendar; -import java.util.Map; - -/** - * A dummy {@link ResultSet}, for testing purposes. - */ -public class TesterResultSet extends AbandonedTrace implements ResultSet { - - protected int _type = ResultSet.TYPE_FORWARD_ONLY; - protected int _concurrency = ResultSet.CONCUR_READ_ONLY; - protected Object[][] _data; - protected int _currentRow = -1; - protected Statement _statement; - protected int _rowsLeft = 2; - protected boolean _open = true; - protected boolean _sqlExceptionOnClose; - - public TesterResultSet(final Statement stmt) { - _statement = stmt; - } - public TesterResultSet(final Statement stmt, final int type, final int concurrency) { - _statement = stmt; - _data = null; - _type = type; - _concurrency = concurrency; - } - public TesterResultSet(final Statement stmt, final Object[][] data) { - _statement = stmt; - _data = data; - } - - @Override - public boolean absolute( final int row ) throws SQLException { - checkOpen(); - return false; - } - - @Override - public void afterLast() throws SQLException { - checkOpen(); - } - - @Override - public void beforeFirst() throws SQLException { - checkOpen(); - } - - @Override - public void cancelRowUpdates() throws SQLException { - checkOpen(); - } - - protected void checkOpen() throws SQLException { - if(!_open) { - throw new SQLException("ResultSet is closed."); - } - } - - @Override - public void clearWarnings() throws SQLException { - checkOpen(); - } - - @Override - public void close() throws SQLException { - if (_sqlExceptionOnClose) { - throw new SQLException("TestSQLExceptionOnClose"); - } - - if (!_open) { - return; - } - - // Not all result sets are generated from statements eg DatabaseMetaData - if (_statement != null) { - ((TesterStatement)_statement)._resultSet = null; - } - - _open = false; - } - - @Override - public void deleteRow() throws SQLException { - checkOpen(); - } - - @Override - public int findColumn(final String columnName) throws SQLException { - checkOpen(); - return 1; - } - - @Override - public boolean first() throws SQLException { - checkOpen(); - return false; - } - - @Override - public Array getArray(final int i) throws SQLException { - checkOpen(); - return null; - } - - @Override - public Array getArray(final String colName) throws SQLException { - checkOpen(); - return null; - } - - @Override - public java.io.InputStream getAsciiStream(final int columnIndex) throws SQLException { - checkOpen(); - return null; - } - - @Override - public java.io.InputStream getAsciiStream(final String columnName) throws SQLException { - checkOpen(); - return null; - } - - @Override - public BigDecimal getBigDecimal(final int columnIndex) throws SQLException { - checkOpen(); - return new BigDecimal(columnIndex); - } - - /** @deprecated */ - @Deprecated - @Override - public BigDecimal getBigDecimal(final int columnIndex, final int scale) throws SQLException { - checkOpen(); - return new BigDecimal(columnIndex); - } - - @Override - public BigDecimal getBigDecimal(final String columnName) throws SQLException { - checkOpen(); - return new BigDecimal(columnName.hashCode()); - } - - /** @deprecated */ - @Deprecated - @Override - public BigDecimal getBigDecimal(final String columnName, final int scale) throws SQLException { - checkOpen(); - return new BigDecimal(columnName.hashCode()); - } - - @Override - public java.io.InputStream getBinaryStream(final int columnIndex) throws SQLException { - checkOpen(); - return null; - } - - @Override - public java.io.InputStream getBinaryStream(final String columnName) throws SQLException { - checkOpen(); - return null; - } - - @Override - public Blob getBlob(final int i) throws SQLException { - checkOpen(); - return null; - } - - @Override - public Blob getBlob(final String colName) throws SQLException { - checkOpen(); - return null; - } - - @Override - public boolean getBoolean(final int columnIndex) throws SQLException { - checkOpen(); - return true; - } - - @Override - public boolean getBoolean(final String columnName) throws SQLException { - checkOpen(); - return true; - } - - @Override - public byte getByte(final int columnIndex) throws SQLException { - checkOpen(); - return (byte)columnIndex; - } - - @Override - public byte getByte(final String columnName) throws SQLException { - checkOpen(); - return (byte)columnName.hashCode(); - } - - @Override - public byte[] getBytes(final int columnIndex) throws SQLException { - checkOpen(); - return new byte[] { (byte)columnIndex }; - } - - @Override - public byte[] getBytes(final String columnName) throws SQLException { - checkOpen(); - return columnName.getBytes(StandardCharsets.UTF_8); - } - - @Override - public java.io.Reader getCharacterStream(final int columnIndex) throws SQLException { - checkOpen(); - return null; - } - - @Override - public java.io.Reader getCharacterStream(final String columnName) throws SQLException { - checkOpen(); - return null; - } - - @Override - public Clob getClob(final int i) throws SQLException { - checkOpen(); - return null; - } - - @Override - public Clob getClob(final String colName) throws SQLException { - checkOpen(); - return null; - } - - @Override - public int getConcurrency() throws SQLException { - return this._concurrency; - } - - @Override - public String getCursorName() throws SQLException { - checkOpen(); - return null; - } - - @Override - public java.sql.Date getDate(final int columnIndex) throws SQLException { - checkOpen(); - return null; - } - - @Override -public java.sql.Date getDate(final int columnIndex, final Calendar cal) throws SQLException { - checkOpen(); - return null; -} - - @Override - public java.sql.Date getDate(final String columnName) throws SQLException { - checkOpen(); - return null; - } - - @Override - public java.sql.Date getDate(final String columnName, final Calendar cal) throws SQLException { - checkOpen(); - return null; - } - - @Override - public double getDouble(final int columnIndex) throws SQLException { - checkOpen(); - return columnIndex; - } - - @Override - public double getDouble(final String columnName) throws SQLException { - checkOpen(); - return columnName.hashCode(); - } - - @Override - public int getFetchDirection() throws SQLException { - checkOpen(); - return 1; - } - - @Override - public int getFetchSize() throws SQLException { - checkOpen(); - return 2; - } - - - @Override - public float getFloat(final int columnIndex) throws SQLException { - checkOpen(); - return columnIndex; - } - - @Override - public float getFloat(final String columnName) throws SQLException { - checkOpen(); - return columnName.hashCode(); - } - - @Override - public int getHoldability() throws SQLException { - throw new SQLException("Not implemented."); - } - - @Override - public int getInt(final int columnIndex) throws SQLException { - checkOpen(); - return (short)columnIndex; - } - - @Override - public int getInt(final String columnName) throws SQLException { - checkOpen(); - return columnName.hashCode(); - } - - @Override - public long getLong(final int columnIndex) throws SQLException { - checkOpen(); - return columnIndex; - } - - @Override - public long getLong(final String columnName) throws SQLException { - checkOpen(); - return columnName.hashCode(); - } - - @Override - public ResultSetMetaData getMetaData() throws SQLException { - checkOpen(); - return null; - } - - @Override - public Reader getNCharacterStream(final int columnIndex) throws SQLException { - throw new SQLException("Not implemented."); - } - - @Override - public Reader getNCharacterStream(final String columnLabel) throws SQLException { - throw new SQLException("Not implemented."); - } - - @Override - public NClob getNClob(final int columnIndex) throws SQLException { - throw new SQLException("Not implemented."); - } - - @Override - public NClob getNClob(final String columnLabel) throws SQLException { - throw new SQLException("Not implemented."); - } - - @Override - public String getNString(final int columnIndex) throws SQLException { - throw new SQLException("Not implemented."); - } - - @Override - public String getNString(final String columnLabel) throws SQLException { - throw new SQLException("Not implemented."); - } - - @Override - public Object getObject(final int columnIndex) throws SQLException { - checkOpen(); - if (_data != null) { - return _data[_currentRow][columnIndex-1]; - } - return new Object(); - } - - @Override - public <T> T getObject(final int columnIndex, final Class<T> type) throws SQLException { - throw new SQLException("Not implemented."); - } - - @Override - public Object getObject(final int i, final Map<String,Class<?>> map) throws SQLException { - checkOpen(); - return new Object(); - } - - @Override - public Object getObject(final String columnName) throws SQLException { - checkOpen(); - return columnName; - } - - @Override - public <T> T getObject(final String columnLabel, final Class<T> type) - throws SQLException { - throw new SQLException("Not implemented."); - } - - @Override - public Object getObject(final String colName, final Map<String,Class<?>> map) throws SQLException { - checkOpen(); - return colName; - } - - @Override - public Ref getRef(final int i) throws SQLException { - checkOpen(); - return null; - } - - @Override - public Ref getRef(final String colName) throws SQLException { - checkOpen(); - return null; - } - - @Override - public int getRow() throws SQLException { - checkOpen(); - return 3 - _rowsLeft; - } - - @Override - public RowId getRowId(final int columnIndex) throws SQLException { - throw new SQLException("Not implemented."); - } - - @Override - public RowId getRowId(final String columnLabel) throws SQLException { - throw new SQLException("Not implemented."); - } - - @Override - public short getShort(final int columnIndex) throws SQLException { - checkOpen(); - return (short)columnIndex; - } - - @Override - public short getShort(final String columnName) throws SQLException { - checkOpen(); - return (short)columnName.hashCode(); - } - - @Override - public SQLXML getSQLXML(final int columnIndex) throws SQLException { - throw new SQLException("Not implemented."); - } - - @Override - public SQLXML getSQLXML(final String columnLabel) throws SQLException { - throw new SQLException("Not implemented."); - } - - @Override - public Statement getStatement() throws SQLException { - checkOpen(); - return _statement; - } - - @Override - public String getString(final int columnIndex) throws SQLException { - checkOpen(); - if (columnIndex == -1) { - throw new SQLException("broken connection"); - } - if (_data != null) { - return (String) getObject(columnIndex); - } - return "String" + columnIndex; - } - - @Override - public String getString(final String columnName) throws SQLException { - checkOpen(); - return columnName; - } - - @Override - public java.sql.Time getTime(final int columnIndex) throws SQLException { - checkOpen(); - return null; - } - - @Override - public java.sql.Time getTime(final int columnIndex, final Calendar cal) throws SQLException { - checkOpen(); - return null; - } - - @Override - public java.sql.Time getTime(final String columnName) throws SQLException { - checkOpen(); - return null; - } - - @Override - public java.sql.Time getTime(final String columnName, final Calendar cal) throws SQLException { - checkOpen(); - return null; - } - - @Override - public java.sql.Timestamp getTimestamp(final int columnIndex) throws SQLException { - checkOpen(); - return null; - } - - @Override - public java.sql.Timestamp getTimestamp(final int columnIndex, final Calendar cal) throws SQLException { - checkOpen(); - return null; - } - - @Override - public java.sql.Timestamp getTimestamp(final String columnName) throws SQLException { - checkOpen(); - return null; - } - - - @Override - public java.sql.Timestamp getTimestamp(final String columnName, final Calendar cal) - throws SQLException { - checkOpen(); - return null; - } - - @Override - public int getType() throws SQLException { - return this._type; - } - - /** @deprecated */ - @Deprecated - @Override - public java.io.InputStream getUnicodeStream(final int columnIndex) throws SQLException { - checkOpen(); - return null; - } - - /** @deprecated */ - @Deprecated - @Override - public java.io.InputStream getUnicodeStream(final String columnName) throws SQLException { - checkOpen(); - return null; - } - - @Override - public java.net.URL getURL(final int columnIndex) throws SQLException { - throw new SQLException("Not implemented."); - } - - @Override - public java.net.URL getURL(final String columnName) throws SQLException { - throw new SQLException("Not implemented."); - } - - @Override - public SQLWarning getWarnings() throws SQLException { - checkOpen(); - return null; - } - - @Override - public void insertRow() throws SQLException { - checkOpen(); - } - - @Override - public boolean isAfterLast() throws SQLException { - checkOpen(); - return _rowsLeft < 0; - } - - @Override - public boolean isBeforeFirst() throws SQLException { - checkOpen(); - return _rowsLeft == 2; - } - - @Override - public boolean isClosed() throws SQLException { - return !_open; - } - - @Override - public boolean isFirst() throws SQLException { - checkOpen(); - return _rowsLeft == 1; - } - - @Override - public boolean isLast() throws SQLException { - checkOpen(); - return _rowsLeft == 0; - } - - public boolean isSqlExceptionOnClose() { - return _sqlExceptionOnClose; - } - - @Override - public boolean isWrapperFor(final Class<?> iface) throws SQLException { - throw new SQLException("Not implemented."); - } - - @Override - public boolean last() throws SQLException { - checkOpen(); - return false; - } - - @Override - public void moveToCurrentRow() throws SQLException { - checkOpen(); - } - - @Override - public void moveToInsertRow() throws SQLException { - checkOpen(); - } - - @Override - public boolean next() throws SQLException { - checkOpen(); - if (_data != null) { - _currentRow++; - return _currentRow < _data.length; - } - return --_rowsLeft > 0; - } - - @Override - public boolean previous() throws SQLException { - checkOpen(); - return false; - } - - @Override - public void refreshRow() throws SQLException { - checkOpen(); - } - - @Override - public boolean relative( final int rows ) throws SQLException { - checkOpen(); - return false; - } - - @Override - public boolean rowDeleted() throws SQLException { - checkOpen(); - return false; - } - - @Override - public boolean rowInserted() throws SQLException { - checkOpen(); - return false; - } - - @Override - public boolean rowUpdated() throws SQLException { - checkOpen(); - return false; - } - - @Override - public void setFetchDirection(final int direction) throws SQLException { - checkOpen(); - } - - @Override - public void setFetchSize(final int rows) throws SQLException { - checkOpen(); - } - - public void setSqlExceptionOnClose(final boolean sqlExceptionOnClose) { - this._sqlExceptionOnClose = sqlExceptionOnClose; - } - - @Override - public <T> T unwrap(final Class<T> iface) throws SQLException { - throw new SQLException("Not implemented."); - } - - @Override - public void updateArray(final int columnIndex, final java.sql.Array x) - throws SQLException { - throw new SQLException("Not implemented."); - } - - @Override - public void updateArray(final String columnName, final java.sql.Array x) - throws SQLException { - throw new SQLException("Not implemented."); - } - - @Override - public void updateAsciiStream(final int columnIndex, final InputStream inputStream) throws SQLException { - throw new SQLException("Not implemented."); - } - - - @Override - public void updateAsciiStream(final int columnIndex, final InputStream inputStream, final long length) throws SQLException { - throw new SQLException("Not implemented."); - } - - @Override - public void updateAsciiStream(final int columnIndex, - final java.io.InputStream x, - final int length) throws SQLException { - checkOpen(); - } - - @Override - public void updateAsciiStream(final String columnLabel, final InputStream inputStream) throws SQLException { - throw new SQLException("Not implemented."); - } - - @Override - public void updateAsciiStream(final String columnLabel, final InputStream inputStream, final long length) throws SQLException { - throw new SQLException("Not implemented."); - } - - @Override - public void updateAsciiStream(final String columnName, - final java.io.InputStream x, - final int length) throws SQLException { - checkOpen(); - } - - @Override - public void updateBigDecimal(final int columnIndex, final BigDecimal x) throws SQLException { - checkOpen(); - } - - @Override - public void updateBigDecimal(final String columnName, final BigDecimal x) throws SQLException { - checkOpen(); - } - - @Override - public void updateBinaryStream(final int columnIndex, final InputStream inputStream) throws SQLException { - throw new SQLException("Not implemented."); - } - - @Override - public void updateBinaryStream(final int columnIndex, final InputStream inputStream, final long length) throws SQLException { - throw new SQLException("Not implemented."); - } - - @Override - public void updateBinaryStream(final int columnIndex, - final java.io.InputStream x, - final int length) throws SQLException { - checkOpen(); - } - - @Override - public void updateBinaryStream(final String columnLabel, final InputStream inputStream) throws SQLException { - throw new SQLException("Not implemented."); - } - - @Override - public void updateBinaryStream(final String columnLabel, final InputStream inputStream, final long length) throws SQLException { - throw new SQLException("Not implemented."); - } - - @Override - public void updateBinaryStream(final String columnName, - final java.io.InputStream x, - final int length) throws SQLException { - checkOpen(); - } - - @Override - public void updateBlob(final int columnIndex, final InputStream inputStream) throws SQLException { - throw new SQLException("Not implemented."); - } - - @Override - public void updateBlob(final int columnIndex, final InputStream inputStream, final long length) throws SQLException { - throw new SQLException("Not implemented."); - } - - @Override - public void updateBlob(final int columnIndex, final java.sql.Blob x) - throws SQLException { - throw new SQLException("Not implemented."); - } - - @Override - public void updateBlob(final String columnLabel, final InputStream inputStream) throws SQLException { - throw new SQLException("Not implemented."); - } - - @Override - public void updateBlob(final String columnLabel, final InputStream inputStream, final long length) throws SQLException { - throw new SQLException("Not implemented."); - } - - @Override - public void updateBlob(final String columnName, final java.sql.Blob x) - throws SQLException { - throw new SQLException("Not implemented."); - } - - @Override - public void updateBoolean(final int columnIndex, final boolean x) throws SQLException { - checkOpen(); - } - - @Override - public void updateBoolean(final String columnName, final boolean x) throws SQLException { - checkOpen(); - } - - @Override - public void updateByte(final int columnIndex, final byte x) throws SQLException { - checkOpen(); - } - - @Override - public void updateByte(final String columnName, final byte x) throws SQLException { - checkOpen(); - } - - @Override - public void updateBytes(final int columnIndex, final byte[] x) throws SQLException { - checkOpen(); - } - - @Override - public void updateBytes(final String columnName, final byte[] x) throws SQLException { - checkOpen(); - } - - @Override - public void updateCharacterStream(final int columnIndex, - final java.io.Reader x, - final int length) throws SQLException { - checkOpen(); - } - - @Override - public void updateCharacterStream(final int columnIndex, final Reader reader) throws SQLException { - throw new SQLException("Not implemented."); - } - - - @Override - public void updateCharacterStream(final int columnIndex, final Reader reader, final long length) throws SQLException { - throw new SQLException("Not implemented."); - } - - @Override - public void updateCharacterStream(final String columnName, - final java.io.Reader reader, - final int length) throws SQLException { - checkOpen(); - } - - @Override - public void updateCharacterStream(final String columnLabel, final Reader reader) throws SQLException { - throw new SQLException("Not implemented."); - } - - @Override - public void updateCharacterStream(final String columnLabel, final Reader reader, final long length) throws SQLException { - throw new SQLException("Not implemented."); - } - - @Override - public void updateClob(final int columnIndex, final java.sql.Clob x) - throws SQLException { - throw new SQLException("Not implemented."); - } - - @Override - public void updateClob(final int columnIndex, final Reader reader) throws SQLException { - throw new SQLException("Not implemented."); - } - - @Override - public void updateClob(final int columnIndex, final Reader reader, final long length) throws SQLException { - throw new SQLException("Not implemented."); - } - - @Override - public void updateClob(final String columnName, final java.sql.Clob x) - throws SQLException { - throw new SQLException("Not implemented."); - } - - @Override - public void updateClob(final String columnLabel, final Reader reader) throws SQLException { - throw new SQLException("Not implemented."); - } - - @Override - public void updateClob(final String columnLabel, final Reader reader, final long length) throws SQLException { - throw new SQLException("Not implemented."); - } - - @Override - public void updateDate(final int columnIndex, final java.sql.Date x) throws SQLException { - checkOpen(); - } - - @Override - public void updateDate(final String columnName, final java.sql.Date x) throws SQLException { - checkOpen(); - } - - @Override - public void updateDouble(final int columnIndex, final double x) throws SQLException { - checkOpen(); - } - - @Override - public void updateDouble(final String columnName, final double x) throws SQLException { - checkOpen(); - } - - @Override - public void updateFloat(final int columnIndex, final float x) throws SQLException { - checkOpen(); - } - - @Override - public void updateFloat(final String columnName, final float x) throws SQLException { - checkOpen(); - } - - @Override - public void updateInt(final int columnIndex, final int x) throws SQLException { - checkOpen(); - } - - @Override - public void updateInt(final String columnName, final int x) throws SQLException { - checkOpen(); - } - - @Override - public void updateLong(final int columnIndex, final long x) throws SQLException { - checkOpen(); - } - - @Override - public void updateLong(final String columnName, final long x) throws SQLException { - checkOpen(); - } - - @Override - public void updateNCharacterStream(final int columnIndex, final Reader reader) throws SQLException { - throw new SQLException("Not implemented."); - } - - @Override - public void updateNCharacterStream(final int columnIndex, final Reader reader, final long length) throws SQLException { - throw new SQLException("Not implemented."); - } - - @Override - public void updateNCharacterStream(final String columnLabel, final Reader reader) throws SQLException { - throw new SQLException("Not implemented."); - } - - @Override - public void updateNCharacterStream(final String columnLabel, final Reader reader, final long length) throws SQLException { - throw new SQLException("Not implemented."); - } - - @Override - public void updateNClob(final int columnIndex, final NClob value) throws SQLException { - throw new SQLException("Not implemented."); - } - - @Override - public void updateNClob(final int columnIndex, final Reader reader) throws SQLException { - throw new SQLException("Not implemented."); - } - - @Override - public void updateNClob(final int columnIndex, final Reader reader, final long length) throws SQLException { - throw new SQLException("Not implemented."); - } - - @Override - public void updateNClob(final String columnLabel, final NClob value) throws SQLException { - throw new SQLException("Not implemented."); - } - - @Override - public void updateNClob(final String columnLabel, final Reader reader) throws SQLException { - throw new SQLException("Not implemented."); - } - - @Override - public void updateNClob(final String columnLabel, final Reader reader, final long length) throws SQLException { - throw new SQLException("Not implemented."); - } - - @Override - public void updateNString(final int columnIndex, final String value) throws SQLException { - throw new SQLException("Not implemented."); - } - - @Override - public void updateNString(final String columnLabel, final String value) throws SQLException { - throw new SQLException("Not implemented."); - } - - @Override - public void updateNull(final int columnIndex) throws SQLException { - checkOpen(); - } - - @Override - public void updateNull(final String columnName) throws SQLException { - checkOpen(); - } - - @Override - public void updateObject(final int columnIndex, final Object x) throws SQLException { - checkOpen(); - } - - @Override - public void updateObject(final int columnIndex, final Object x, final int scale) throws SQLException { - checkOpen(); - } - - @Override - public void updateObject(final int columnIndex, final Object x, final SQLType targetSqlType) throws SQLException { - checkOpen(); - } - - @Override - public void updateObject(final int columnIndex, final Object x, final SQLType targetSqlType, final int scaleOrLength) throws SQLException { - checkOpen(); - } - - @Override - public void updateObject(final String columnName, final Object x) throws SQLException { - checkOpen(); - } - - @Override - public void updateObject(final String columnName, final Object x, final int scale) throws SQLException { - checkOpen(); - } - - @Override - public void updateObject(final String columnLabel, final Object x, final SQLType targetSqlType) throws SQLException { - checkOpen(); - } - - @Override - public void updateObject(final String columnLabel, final Object x, final SQLType targetSqlType, final int scaleOrLength) - throws SQLException { - checkOpen(); - } - - @Override - public void updateRef(final int columnIndex, final java.sql.Ref x) throws SQLException { - throw new SQLException("Not implemented."); - } - - @Override - public void updateRef(final String columnName, final java.sql.Ref x) throws SQLException { - throw new SQLException("Not implemented."); - } - - @Override - public void updateRow() throws SQLException { - checkOpen(); - } - - @Override - public void updateRowId(final int columnIndex, final RowId value) throws SQLException { - throw new SQLException("Not implemented."); - } - - @Override - public void updateRowId(final String columnLabel, final RowId value) throws SQLException { - throw new SQLException("Not implemented."); - } - - @Override - public void updateShort(final int columnIndex, final short x) throws SQLException { - checkOpen(); - } - - @Override - public void updateShort(final String columnName, final short x) throws SQLException { - checkOpen(); - } - - @Override - public void updateSQLXML(final int columnIndex, final SQLXML value) throws SQLException { - throw new SQLException("Not implemented."); - } - - @Override - public void updateSQLXML(final String columnLabel, final SQLXML value) throws SQLException { - throw new SQLException("Not implemented."); - } - - @Override - public void updateString(final int columnIndex, final String x) throws SQLException { - checkOpen(); - } - - @Override - public void updateString(final String columnName, final String x) throws SQLException { - checkOpen(); - } - - @Override - public void updateTime(final int columnIndex, final java.sql.Time x) throws SQLException { - checkOpen(); - } - - @Override - public void updateTime(final String columnName, final java.sql.Time x) throws SQLException { - checkOpen(); - } - - @Override - public void updateTimestamp(final int columnIndex, final java.sql.Timestamp x) throws SQLException { - checkOpen(); - } - - @Override - public void updateTimestamp(final String columnName, final java.sql.Timestamp x) - throws SQLException { - checkOpen(); - } - - @Override - public boolean wasNull() throws SQLException { - checkOpen(); - return false; - } -} +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 org.apache.commons.dbcp2; + +import java.io.InputStream; +import java.io.Reader; +import java.math.BigDecimal; +import java.nio.charset.StandardCharsets; +import java.sql.Array; +import java.sql.Blob; +import java.sql.Clob; +import java.sql.NClob; +import java.sql.Ref; +import java.sql.ResultSet; +import java.sql.ResultSetMetaData; +import java.sql.RowId; +import java.sql.SQLException; +import java.sql.SQLType; +import java.sql.SQLWarning; +import java.sql.SQLXML; +import java.sql.Statement; +import java.util.Calendar; +import java.util.Map; + +/** + * A dummy {@link ResultSet}, for testing purposes. + */ +public class TesterResultSet extends AbandonedTrace implements ResultSet { + + protected int _type = ResultSet.TYPE_FORWARD_ONLY; + protected int _concurrency = ResultSet.CONCUR_READ_ONLY; + protected Object[][] _data; + protected int _currentRow = -1; + protected Statement _statement; + protected int _rowsLeft = 2; + protected boolean _open = true; + protected boolean _sqlExceptionOnClose; + + public TesterResultSet(final Statement stmt) { + _statement = stmt; + } + public TesterResultSet(final Statement stmt, final int type, final int concurrency) { + _statement = stmt; + _data = null; + _type = type; + _concurrency = concurrency; + } + public TesterResultSet(final Statement stmt, final Object[][] data) { + _statement = stmt; + _data = data; + } + + @Override + public boolean absolute( final int row ) throws SQLException { + checkOpen(); + return false; + } + + @Override + public void afterLast() throws SQLException { + checkOpen(); + } + + @Override + public void beforeFirst() throws SQLException { + checkOpen(); + } + + @Override + public void cancelRowUpdates() throws SQLException { + checkOpen(); + } + + protected void checkOpen() throws SQLException { + if(!_open) { + throw new SQLException("ResultSet is closed."); + } + } + + @Override + public void clearWarnings() throws SQLException { + checkOpen(); + } + + @Override + public void close() throws SQLException { + if (_sqlExceptionOnClose) { + throw new SQLException("TestSQLExceptionOnClose"); + } + + if (!_open) { + return; + } + + // Not all result sets are generated from statements eg DatabaseMetaData + if (_statement != null) { + ((TesterStatement)_statement)._resultSet = null; + } + + _open = false; + } + + @Override + public void deleteRow() throws SQLException { + checkOpen(); + } + + @Override + public int findColumn(final String columnName) throws SQLException { + checkOpen(); + return 1; + } + + @Override + public boolean first() throws SQLException { + checkOpen(); + return false; + } + + @Override + public Array getArray(final int i) throws SQLException { + checkOpen(); + return null; + } + + @Override + public Array getArray(final String colName) throws SQLException { + checkOpen(); + return null; + } + + @Override + public java.io.InputStream getAsciiStream(final int columnIndex) throws SQLException { + checkOpen(); + return null; + } + + @Override + public java.io.InputStream getAsciiStream(final String columnName) throws SQLException { + checkOpen(); + return null; + } + + @Override + public BigDecimal getBigDecimal(final int columnIndex) throws SQLException { + checkOpen(); + return new BigDecimal(columnIndex); + } + + /** @deprecated */ + @Deprecated + @Override + public BigDecimal getBigDecimal(final int columnIndex, final int scale) throws SQLException { + checkOpen(); + return new BigDecimal(columnIndex); + } + + @Override + public BigDecimal getBigDecimal(final String columnName) throws SQLException { + checkOpen(); + return new BigDecimal(columnName.hashCode()); + } + + /** @deprecated */ + @Deprecated + @Override + public BigDecimal getBigDecimal(final String columnName, final int scale) throws SQLException { + checkOpen(); + return new BigDecimal(columnName.hashCode()); + } + + @Override + public java.io.InputStream getBinaryStream(final int columnIndex) throws SQLException { + checkOpen(); + return null; + } + + @Override + public java.io.InputStream getBinaryStream(final String columnName) throws SQLException { + checkOpen(); + return null; + } + + @Override + public Blob getBlob(final int i) throws SQLException { + checkOpen(); + return null; + } + + @Override + public Blob getBlob(final String colName) throws SQLException { + checkOpen(); + return null; + } + + @Override + public boolean getBoolean(final int columnIndex) throws SQLException { + checkOpen(); + return true; + } + + @Override + public boolean getBoolean(final String columnName) throws SQLException { + checkOpen(); + return true; + } + + @Override + public byte getByte(final int columnIndex) throws SQLException { + checkOpen(); + return (byte)columnIndex; + } + + @Override + public byte getByte(final String columnName) throws SQLException { + checkOpen(); + return (byte)columnName.hashCode(); + } + + @Override + public byte[] getBytes(final int columnIndex) throws SQLException { + checkOpen(); + return new byte[] {(byte) columnIndex}; + } + + @Override + public byte[] getBytes(final String columnName) throws SQLException { + checkOpen(); + return columnName.getBytes(StandardCharsets.UTF_8); + } + + @Override + public java.io.Reader getCharacterStream(final int columnIndex) throws SQLException { + checkOpen(); + return null; + } + + @Override + public java.io.Reader getCharacterStream(final String columnName) throws SQLException { + checkOpen(); + return null; + } + + @Override + public Clob getClob(final int i) throws SQLException { + checkOpen(); + return null; + } + + @Override + public Clob getClob(final String colName) throws SQLException { + checkOpen(); + return null; + } + + @Override + public int getConcurrency() throws SQLException { + return this._concurrency; + } + + @Override + public String getCursorName() throws SQLException { + checkOpen(); + return null; + } + + @Override + public java.sql.Date getDate(final int columnIndex) throws SQLException { + checkOpen(); + return null; + } + + @Override +public java.sql.Date getDate(final int columnIndex, final Calendar cal) throws SQLException { + checkOpen(); + return null; +} + + @Override + public java.sql.Date getDate(final String columnName) throws SQLException { + checkOpen(); + return null; + } + + @Override + public java.sql.Date getDate(final String columnName, final Calendar cal) throws SQLException { + checkOpen(); + return null; + } + + @Override + public double getDouble(final int columnIndex) throws SQLException { + checkOpen(); + return columnIndex; + } + + @Override + public double getDouble(final String columnName) throws SQLException { + checkOpen(); + return columnName.hashCode(); + } + + @Override + public int getFetchDirection() throws SQLException { + checkOpen(); + return 1; + } + + @Override + public int getFetchSize() throws SQLException { + checkOpen(); + return 2; + } + + + @Override + public float getFloat(final int columnIndex) throws SQLException { + checkOpen(); + return columnIndex; + } + + @Override + public float getFloat(final String columnName) throws SQLException { + checkOpen(); + return columnName.hashCode(); + } + + @Override + public int getHoldability() throws SQLException { + throw new SQLException("Not implemented."); + } + + @Override + public int getInt(final int columnIndex) throws SQLException { + checkOpen(); + return (short)columnIndex; + } + + @Override + public int getInt(final String columnName) throws SQLException { + checkOpen(); + return columnName.hashCode(); + } + + @Override + public long getLong(final int columnIndex) throws SQLException { + checkOpen(); + return columnIndex; + } + + @Override + public long getLong(final String columnName) throws SQLException { + checkOpen(); + return columnName.hashCode(); + } + + @Override + public ResultSetMetaData getMetaData() throws SQLException { + checkOpen(); + return null; + } + + @Override + public Reader getNCharacterStream(final int columnIndex) throws SQLException { + throw new SQLException("Not implemented."); + } + + @Override + public Reader getNCharacterStream(final String columnLabel) throws SQLException { + throw new SQLException("Not implemented."); + } + + @Override + public NClob getNClob(final int columnIndex) throws SQLException { + throw new SQLException("Not implemented."); + } + + @Override + public NClob getNClob(final String columnLabel) throws SQLException { + throw new SQLException("Not implemented."); + } + + @Override + public String getNString(final int columnIndex) throws SQLException { + throw new SQLException("Not implemented."); + } + + @Override + public String getNString(final String columnLabel) throws SQLException { + throw new SQLException("Not implemented."); + } + + @Override + public Object getObject(final int columnIndex) throws SQLException { + checkOpen(); + if (_data != null) { + return _data[_currentRow][columnIndex-1]; + } + return new Object(); + } + + @Override + public <T> T getObject(final int columnIndex, final Class<T> type) throws SQLException { + throw new SQLException("Not implemented."); + } + + @Override + public Object getObject(final int i, final Map<String,Class<?>> map) throws SQLException { + checkOpen(); + return new Object(); + } + + @Override + public Object getObject(final String columnName) throws SQLException { + checkOpen(); + return columnName; + } + + @Override + public <T> T getObject(final String columnLabel, final Class<T> type) + throws SQLException { + throw new SQLException("Not implemented."); + } + + @Override + public Object getObject(final String colName, final Map<String,Class<?>> map) throws SQLException { + checkOpen(); + return colName; + } + + @Override + public Ref getRef(final int i) throws SQLException { + checkOpen(); + return null; + } + + @Override + public Ref getRef(final String colName) throws SQLException { + checkOpen(); + return null; + } + + @Override + public int getRow() throws SQLException { + checkOpen(); + return 3 - _rowsLeft; + } + + @Override + public RowId getRowId(final int columnIndex) throws SQLException { + throw new SQLException("Not implemented."); + } + + @Override + public RowId getRowId(final String columnLabel) throws SQLException { + throw new SQLException("Not implemented."); + } + + @Override + public short getShort(final int columnIndex) throws SQLException { + checkOpen(); + return (short)columnIndex; + } + + @Override + public short getShort(final String columnName) throws SQLException { + checkOpen(); + return (short)columnName.hashCode(); + } + + @Override + public SQLXML getSQLXML(final int columnIndex) throws SQLException { + throw new SQLException("Not implemented."); + } + + @Override + public SQLXML getSQLXML(final String columnLabel) throws SQLException { + throw new SQLException("Not implemented."); + } + + @Override + public Statement getStatement() throws SQLException { + checkOpen(); + return _statement; + } + + @Override + public String getString(final int columnIndex) throws SQLException { + checkOpen(); + if (columnIndex == -1) { + throw new SQLException("broken connection"); + } + if (_data != null) { + return (String) getObject(columnIndex); + } + return "String" + columnIndex; + } + + @Override + public String getString(final String columnName) throws SQLException { + checkOpen(); + return columnName; + } + + @Override + public java.sql.Time getTime(final int columnIndex) throws SQLException { + checkOpen(); + return null; + } + + @Override + public java.sql.Time getTime(final int columnIndex, final Calendar cal) throws SQLException { + checkOpen(); + return null; + } + + @Override + public java.sql.Time getTime(final String columnName) throws SQLException { + checkOpen(); + return null; + } + + @Override + public java.sql.Time getTime(final String columnName, final Calendar cal) throws SQLException { + checkOpen(); + return null; + } + + @Override + public java.sql.Timestamp getTimestamp(final int columnIndex) throws SQLException { + checkOpen(); + return null; + } + + @Override + public java.sql.Timestamp getTimestamp(final int columnIndex, final Calendar cal) throws SQLException { + checkOpen(); + return null; + } + + @Override + public java.sql.Timestamp getTimestamp(final String columnName) throws SQLException { + checkOpen(); + return null; + } + + + @Override + public java.sql.Timestamp getTimestamp(final String columnName, final Calendar cal) + throws SQLException { + checkOpen(); + return null; + } + + @Override + public int getType() throws SQLException { + return this._type; + } + + /** @deprecated */ + @Deprecated + @Override + public java.io.InputStream getUnicodeStream(final int columnIndex) throws SQLException { + checkOpen(); + return null; + } + + /** @deprecated */ + @Deprecated + @Override + public java.io.InputStream getUnicodeStream(final String columnName) throws SQLException { + checkOpen(); + return null; + } + + @Override + public java.net.URL getURL(final int columnIndex) throws SQLException { + throw new SQLException("Not implemented."); + } + + @Override + public java.net.URL getURL(final String columnName) throws SQLException { + throw new SQLException("Not implemented."); + } + + @Override + public SQLWarning getWarnings() throws SQLException { + checkOpen(); + return null; + } + + @Override + public void insertRow() throws SQLException { + checkOpen(); + } + + @Override + public boolean isAfterLast() throws SQLException { + checkOpen(); + return _rowsLeft < 0; + } + + @Override + public boolean isBeforeFirst() throws SQLException { + checkOpen(); + return _rowsLeft == 2; + } + + @Override + public boolean isClosed() throws SQLException { + return !_open; + } + + @Override + public boolean isFirst() throws SQLException { + checkOpen(); + return _rowsLeft == 1; + } + + @Override + public boolean isLast() throws SQLException { + checkOpen(); + return _rowsLeft == 0; + } + + public boolean isSqlExceptionOnClose() { + return _sqlExceptionOnClose; + } + + @Override + public boolean isWrapperFor(final Class<?> iface) throws SQLException { + throw new SQLException("Not implemented."); + } + + @Override + public boolean last() throws SQLException { + checkOpen(); + return false; + } + + @Override + public void moveToCurrentRow() throws SQLException { + checkOpen(); + } + + @Override + public void moveToInsertRow() throws SQLException { + checkOpen(); + } + + @Override + public boolean next() throws SQLException { + checkOpen(); + if (_data != null) { + _currentRow++; + return _currentRow < _data.length; + } + return --_rowsLeft > 0; + } + + @Override + public boolean previous() throws SQLException { + checkOpen(); + return false; + } + + @Override + public void refreshRow() throws SQLException { + checkOpen(); + } + + @Override + public boolean relative( final int rows ) throws SQLException { + checkOpen(); + return false; + } + + @Override + public boolean rowDeleted() throws SQLException { + checkOpen(); + return false; + } + + @Override + public boolean rowInserted() throws SQLException { + checkOpen(); + return false; + } + + @Override + public boolean rowUpdated() throws SQLException { + checkOpen(); + return false; + } + + @Override + public void setFetchDirection(final int direction) throws SQLException { + checkOpen(); + } + + @Override + public void setFetchSize(final int rows) throws SQLException { + checkOpen(); + } + + public void setSqlExceptionOnClose(final boolean sqlExceptionOnClose) { + this._sqlExceptionOnClose = sqlExceptionOnClose; + } + + @Override + public <T> T unwrap(final Class<T> iface) throws SQLException { + throw new SQLException("Not implemented."); + } + + @Override + public void updateArray(final int columnIndex, final java.sql.Array x) + throws SQLException { + throw new SQLException("Not implemented."); + } + + @Override + public void updateArray(final String columnName, final java.sql.Array x) + throws SQLException { + throw new SQLException("Not implemented."); + } + + @Override + public void updateAsciiStream(final int columnIndex, final InputStream inputStream) throws SQLException { + throw new SQLException("Not implemented."); + } + + + @Override + public void updateAsciiStream(final int columnIndex, final InputStream inputStream, final long length) throws SQLException { + throw new SQLException("Not implemented."); + } + + @Override + public void updateAsciiStream(final int columnIndex, + final java.io.InputStream x, + final int length) throws SQLException { + checkOpen(); + } + + @Override + public void updateAsciiStream(final String columnLabel, final InputStream inputStream) throws SQLException { + throw new SQLException("Not implemented."); + } + + @Override + public void updateAsciiStream(final String columnLabel, final InputStream inputStream, final long length) throws SQLException { + throw new SQLException("Not implemented."); + } + + @Override + public void updateAsciiStream(final String columnName, + final java.io.InputStream x, + final int length) throws SQLException { + checkOpen(); + } + + @Override + public void updateBigDecimal(final int columnIndex, final BigDecimal x) throws SQLException { + checkOpen(); + } + + @Override + public void updateBigDecimal(final String columnName, final BigDecimal x) throws SQLException { + checkOpen(); + } + + @Override + public void updateBinaryStream(final int columnIndex, final InputStream inputStream) throws SQLException { + throw new SQLException("Not implemented."); + } + + @Override + public void updateBinaryStream(final int columnIndex, final InputStream inputStream, final long length) throws SQLException { + throw new SQLException("Not implemented."); + } + + @Override + public void updateBinaryStream(final int columnIndex, + final java.io.InputStream x, + final int length) throws SQLException { + checkOpen(); + } + + @Override + public void updateBinaryStream(final String columnLabel, final InputStream inputStream) throws SQLException { + throw new SQLException("Not implemented."); + } + + @Override + public void updateBinaryStream(final String columnLabel, final InputStream inputStream, final long length) throws SQLException { + throw new SQLException("Not implemented."); + } + + @Override + public void updateBinaryStream(final String columnName, + final java.io.InputStream x, + final int length) throws SQLException { + checkOpen(); + } + + @Override + public void updateBlob(final int columnIndex, final InputStream inputStream) throws SQLException { + throw new SQLException("Not implemented."); + } + + @Override + public void updateBlob(final int columnIndex, final InputStream inputStream, final long length) throws SQLException { + throw new SQLException("Not implemented."); + } + + @Override + public void updateBlob(final int columnIndex, final java.sql.Blob x) + throws SQLException { + throw new SQLException("Not implemented."); + } + + @Override + public void updateBlob(final String columnLabel, final InputStream inputStream) throws SQLException { + throw new SQLException("Not implemented."); + } + + @Override + public void updateBlob(final String columnLabel, final InputStream inputStream, final long length) throws SQLException { + throw new SQLException("Not implemented."); + } + + @Override + public void updateBlob(final String columnName, final java.sql.Blob x) + throws SQLException { + throw new SQLException("Not implemented."); + } + + @Override + public void updateBoolean(final int columnIndex, final boolean x) throws SQLException { + checkOpen(); + } + + @Override + public void updateBoolean(final String columnName, final boolean x) throws SQLException { + checkOpen(); + } + + @Override + public void updateByte(final int columnIndex, final byte x) throws SQLException { + checkOpen(); + } + + @Override + public void updateByte(final String columnName, final byte x) throws SQLException { + checkOpen(); + } + + @Override + public void updateBytes(final int columnIndex, final byte[] x) throws SQLException { + checkOpen(); + } + + @Override + public void updateBytes(final String columnName, final byte[] x) throws SQLException { + checkOpen(); + } + + @Override + public void updateCharacterStream(final int columnIndex, + final java.io.Reader x, + final int length) throws SQLException { + checkOpen(); + } + + @Override + public void updateCharacterStream(final int columnIndex, final Reader reader) throws SQLException { + throw new SQLException("Not implemented."); + } + + + @Override + public void updateCharacterStream(final int columnIndex, final Reader reader, final long length) throws SQLException { + throw new SQLException("Not implemented."); + } + + @Override + public void updateCharacterStream(final String columnName, + final java.io.Reader reader, + final int length) throws SQLException { + checkOpen(); + } + + @Override + public void updateCharacterStream(final String columnLabel, final Reader reader) throws SQLException { + throw new SQLException("Not implemented."); + } + + @Override + public void updateCharacterStream(final String columnLabel, final Reader reader, final long length) throws SQLException { + throw new SQLException("Not implemented."); + } + + @Override + public void updateClob(final int columnIndex, final java.sql.Clob x) + throws SQLException { + throw new SQLException("Not implemented."); + } + + @Override + public void updateClob(final int columnIndex, final Reader reader) throws SQLException { + throw new SQLException("Not implemented."); + } + + @Override + public void updateClob(final int columnIndex, final Reader reader, final long length) throws SQLException { + throw new SQLException("Not implemented."); + } + + @Override + public void updateClob(final String columnName, final java.sql.Clob x) + throws SQLException { + throw new SQLException("Not implemented."); + } + + @Override + public void updateClob(final String columnLabel, final Reader reader) throws SQLException { + throw new SQLException("Not implemented."); + } + + @Override + public void updateClob(final String columnLabel, final Reader reader, final long length) throws SQLException { + throw new SQLException("Not implemented."); + } + + @Override + public void updateDate(final int columnIndex, final java.sql.Date x) throws SQLException { + checkOpen(); + } + + @Override + public void updateDate(final String columnName, final java.sql.Date x) throws SQLException { + checkOpen(); + } + + @Override + public void updateDouble(final int columnIndex, final double x) throws SQLException { + checkOpen(); + } + + @Override + public void updateDouble(final String columnName, final double x) throws SQLException { + checkOpen(); + } + + @Override + public void updateFloat(final int columnIndex, final float x) throws SQLException { + checkOpen(); + } + + @Override + public void updateFloat(final String columnName, final float x) throws SQLException { + checkOpen(); + } + + @Override + public void updateInt(final int columnIndex, final int x) throws SQLException { + checkOpen(); + } + + @Override + public void updateInt(final String columnName, final int x) throws SQLException { + checkOpen(); + } + + @Override + public void updateLong(final int columnIndex, final long x) throws SQLException { + checkOpen(); + } + + @Override + public void updateLong(final String columnName, final long x) throws SQLException { + checkOpen(); + } + + @Override + public void updateNCharacterStream(final int columnIndex, final Reader reader) throws SQLException { + throw new SQLException("Not implemented."); + } + + @Override + public void updateNCharacterStream(final int columnIndex, final Reader reader, final long length) throws SQLException { + throw new SQLException("Not implemented."); + } + + @Override + public void updateNCharacterStream(final String columnLabel, final Reader reader) throws SQLException { + throw new SQLException("Not implemented."); + } + + @Override + public void updateNCharacterStream(final String columnLabel, final Reader reader, final long length) throws SQLException { + throw new SQLException("Not implemented."); + } + + @Override + public void updateNClob(final int columnIndex, final NClob value) throws SQLException { + throw new SQLException("Not implemented."); + } + + @Override + public void updateNClob(final int columnIndex, final Reader reader) throws SQLException { + throw new SQLException("Not implemented."); + } + + @Override + public void updateNClob(final int columnIndex, final Reader reader, final long length) throws SQLException { + throw new SQLException("Not implemented."); + } + + @Override + public void updateNClob(final String columnLabel, final NClob value) throws SQLException { + throw new SQLException("Not implemented."); + } + + @Override + public void updateNClob(final String columnLabel, final Reader reader) throws SQLException { + throw new SQLException("Not implemented."); + } + + @Override + public void updateNClob(final String columnLabel, final Reader reader, final long length) throws SQLException { + throw new SQLException("Not implemented."); + } + + @Override + public void updateNString(final int columnIndex, final String value) throws SQLException { + throw new SQLException("Not implemented."); + } + + @Override + public void updateNString(final String columnLabel, final String value) throws SQLException { + throw new SQLException("Not implemented."); + } + + @Override + public void updateNull(final int columnIndex) throws SQLException { + checkOpen(); + } + + @Override + public void updateNull(final String columnName) throws SQLException { + checkOpen(); + } + + @Override + public void updateObject(final int columnIndex, final Object x) throws SQLException { + checkOpen(); + } + + @Override + public void updateObject(final int columnIndex, final Object x, final int scale) throws SQLException { + checkOpen(); + } + + @Override + public void updateObject(final int columnIndex, final Object x, final SQLType targetSqlType) throws SQLException { + checkOpen(); + } + + @Override + public void updateObject(final int columnIndex, final Object x, final SQLType targetSqlType, final int scaleOrLength) throws SQLException { + checkOpen(); + } + + @Override + public void updateObject(final String columnName, final Object x) throws SQLException { + checkOpen(); + } + + @Override + public void updateObject(final String columnName, final Object x, final int scale) throws SQLException { + checkOpen(); + } + + @Override + public void updateObject(final String columnLabel, final Object x, final SQLType targetSqlType) throws SQLException { + checkOpen(); + } + + @Override + public void updateObject(final String columnLabel, final Object x, final SQLType targetSqlType, final int scaleOrLength) + throws SQLException { + checkOpen(); + } + + @Override + public void updateRef(final int columnIndex, final java.sql.Ref x) throws SQLException { + throw new SQLException("Not implemented."); + } + + @Override + public void updateRef(final String columnName, final java.sql.Ref x) throws SQLException { + throw new SQLException("Not implemented."); + } + + @Override + public void updateRow() throws SQLException { + checkOpen(); + } + + @Override + public void updateRowId(final int columnIndex, final RowId value) throws SQLException { + throw new SQLException("Not implemented."); + } + + @Override + public void updateRowId(final String columnLabel, final RowId value) throws SQLException { + throw new SQLException("Not implemented."); + } + + @Override + public void updateShort(final int columnIndex, final short x) throws SQLException { + checkOpen(); + } + + @Override + public void updateShort(final String columnName, final short x) throws SQLException { + checkOpen(); + } + + @Override + public void updateSQLXML(final int columnIndex, final SQLXML value) throws SQLException { + throw new SQLException("Not implemented."); + } + + @Override + public void updateSQLXML(final String columnLabel, final SQLXML value) throws SQLException { + throw new SQLException("Not implemented."); + } + + @Override + public void updateString(final int columnIndex, final String x) throws SQLException { + checkOpen(); + } + + @Override + public void updateString(final String columnName, final String x) throws SQLException { + checkOpen(); + } + + @Override + public void updateTime(final int columnIndex, final java.sql.Time x) throws SQLException { + checkOpen(); + } + + @Override + public void updateTime(final String columnName, final java.sql.Time x) throws SQLException { + checkOpen(); + } + + @Override + public void updateTimestamp(final int columnIndex, final java.sql.Timestamp x) throws SQLException { + checkOpen(); + } + + @Override + public void updateTimestamp(final String columnName, final java.sql.Timestamp x) + throws SQLException { + checkOpen(); + } + + @Override + public boolean wasNull() throws SQLException { + checkOpen(); + return false; + } +} diff --git a/src/test/java/org/apache/commons/dbcp2/cpdsadapter/TestDriverAdapterCPDS.java b/src/test/java/org/apache/commons/dbcp2/cpdsadapter/TestDriverAdapterCPDS.java index 4c5c93d5..49c3e224 100644 --- a/src/test/java/org/apache/commons/dbcp2/cpdsadapter/TestDriverAdapterCPDS.java +++ b/src/test/java/org/apache/commons/dbcp2/cpdsadapter/TestDriverAdapterCPDS.java @@ -1,402 +1,402 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You 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 org.apache.commons.dbcp2.cpdsadapter; - -import static org.junit.jupiter.api.Assertions.assertArrayEquals; -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertNotNull; -import static org.junit.jupiter.api.Assertions.assertNull; -import static org.junit.jupiter.api.Assertions.assertThrows; -import static org.junit.jupiter.api.Assertions.assertTrue; -import static org.junit.jupiter.api.Assertions.fail; - -import java.io.PrintWriter; -import java.sql.Connection; -import java.sql.PreparedStatement; -import java.sql.ResultSet; -import java.sql.SQLException; -import java.sql.SQLFeatureNotSupportedException; -import java.time.Duration; -import java.util.Properties; - -import javax.naming.NamingException; -import javax.naming.Reference; -import javax.naming.StringRefAddr; -import javax.sql.DataSource; - -import org.apache.commons.dbcp2.Constants; -import org.apache.commons.dbcp2.datasources.SharedPoolDataSource; -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; - -/** - * Tests for DriverAdapterCPDS - */ -public class TestDriverAdapterCPDS { - - private static class ThreadDbcp367 extends Thread { - - private final DataSource ds; - - private volatile boolean failed; - - public ThreadDbcp367(final DataSource ds) { - this.ds = ds; - } - - public boolean isFailed() { - return failed; - } - - @Override - public void run() { - Connection c = null; - try { - for (int j=0; j < 5000; j++) { - c = ds.getConnection(); - c.close(); - } - } catch (final SQLException sqle) { - failed = true; - sqle.printStackTrace(); - } - } - } - - private DriverAdapterCPDS pcds; - - @BeforeEach - public void setUp() throws Exception { - pcds = new DriverAdapterCPDS(); - pcds.setDriver("org.apache.commons.dbcp2.TesterDriver"); - pcds.setUrl("jdbc:apache:commons:testdriver"); - pcds.setUser("foo"); - pcds.setPassword("bar"); - pcds.setPoolPreparedStatements(false); - } - - @Test - public void testClosingWithUserName() - throws Exception { - final Connection[] c = new Connection[10]; - for (int i=0; i<c.length; i++) { - c[i] = pcds.getPooledConnection("u1", "p1").getConnection(); - } - - // close one of the connections - c[0].close(); - assertTrue(c[0].isClosed()); - // get a new connection - c[0] = pcds.getPooledConnection("u1", "p1").getConnection(); - - for (final Connection element : c) { - element.close(); - } - - // open all the connections - for (int i=0; i<c.length; i++) { - c[i] = pcds.getPooledConnection("u1", "p1").getConnection(); - } - for (final Connection element : c) { - element.close(); - } - } - - // https://issues.apache.org/jira/browse/DBCP-376 - @Test - public void testDbcp367() throws Exception { - final ThreadDbcp367[] threads = new ThreadDbcp367[200]; - - pcds.setPoolPreparedStatements(true); - pcds.setMaxPreparedStatements(-1); - pcds.setAccessToUnderlyingConnectionAllowed(true); - - try (final SharedPoolDataSource spds = new SharedPoolDataSource()) { - spds.setConnectionPoolDataSource(pcds); - spds.setMaxTotal(threads.length + 10); - spds.setDefaultMaxWait(Duration.ofMillis(-1)); - spds.setDefaultMaxIdle(10); - spds.setDefaultAutoCommit(Boolean.FALSE); - - spds.setValidationQuery("SELECT 1"); - spds.setDefaultDurationBetweenEvictionRuns(Duration.ofSeconds(10)); - spds.setDefaultNumTestsPerEvictionRun(-1); - spds.setDefaultTestWhileIdle(true); - spds.setDefaultTestOnBorrow(true); - spds.setDefaultTestOnReturn(false); - - for (int i = 0; i < threads.length; i++) { - threads[i] = new ThreadDbcp367(spds); - threads[i].start(); - } - - for (int i = 0; i < threads.length; i++) { - threads[i].join(); - Assertions.assertFalse(threads[i].isFailed(), "Thread " + i + " has failed"); - } - } - } - - @SuppressWarnings("deprecation") - @Test - public void testDeprecatedAccessors() { - int i = 0; - // - i++; - pcds.setMinEvictableIdleTimeMillis(i); - assertEquals(i, pcds.getMinEvictableIdleTimeMillis()); - assertEquals(Duration.ofMillis(i), pcds.getMinEvictableIdleDuration()); - // - i++; - pcds.setTimeBetweenEvictionRunsMillis(i); - assertEquals(i, pcds.getTimeBetweenEvictionRunsMillis()); - assertEquals(Duration.ofMillis(i), pcds.getDurationBetweenEvictionRuns()); - } - - @Test - public void testGetObjectInstance() throws Exception { - final Reference ref = pcds.getReference(); - final Object o = pcds.getObjectInstance(ref, null, null, null); - assertEquals(pcds.getDriver(), ((DriverAdapterCPDS) o).getDriver()); - } - - @Test - public void testGetObjectInstanceChangeDescription() throws Exception { - final Reference ref = pcds.getReference(); - for (int i = 0; i < ref.size(); i++) { - if (ref.get(i).getType().equals("description")) { - ref.remove(i); - break; - } - } - ref.add(new StringRefAddr("description", "anything")); - final Object o = pcds.getObjectInstance(ref, null, null, null); - assertEquals(pcds.getDescription(), ((DriverAdapterCPDS) o).getDescription()); - } - - @Test - public void testGetObjectInstanceNull() throws Exception { - final Object o = pcds.getObjectInstance(null, null, null, null); - assertNull(o); - } - - @Test - public void testGetParentLogger() { - assertThrows(SQLFeatureNotSupportedException.class, pcds::getParentLogger); - } - - @Test - public void testGetReference() throws NamingException { - final Reference ref = pcds.getReference(); - assertEquals(pcds.getDriver(), ref.get("driver").getContent()); - assertEquals(pcds.getDescription(), ref.get("description").getContent()); - } - - @Test - public void testGettersAndSetters() { - pcds.setUser("foo"); - assertEquals("foo", pcds.getUser()); - pcds.setPassword("bar"); - assertEquals("bar", pcds.getPassword()); - pcds.setPassword(new char[] {'a', 'b'}); - assertArrayEquals(new char[] {'a', 'b'}, pcds.getPasswordCharArray()); - final PrintWriter pw = new PrintWriter(System.err); - pcds.setLogWriter(pw); - @SuppressWarnings("resource") - final PrintWriter logWriter = pcds.getLogWriter(); - assertEquals(pw, logWriter); - pcds.setLoginTimeout(10); - assertEquals(10, pcds.getLoginTimeout()); - pcds.setMaxIdle(100); - assertEquals(100, pcds.getMaxIdle()); - pcds.setDurationBetweenEvictionRuns(Duration.ofMillis(100)); - assertEquals(100, pcds.getDurationBetweenEvictionRuns().toMillis()); - pcds.setNumTestsPerEvictionRun(1); - assertEquals(1, pcds.getNumTestsPerEvictionRun()); - pcds.setMinEvictableIdleDuration(Duration.ofMillis(11)); - assertEquals(Duration.ofMillis(11), pcds.getMinEvictableIdleDuration()); - pcds.setDescription("jo"); - assertEquals("jo", pcds.getDescription()); - } - - /** - * JIRA: DBCP-245 - */ - @Test - public void testIncorrectPassword() throws Exception - { - pcds.getPooledConnection("u2", "p2").close(); - try { - // Use bad password - pcds.getPooledConnection("u1", "zlsafjk"); - fail("Able to retrieve connection with incorrect password"); - } catch (final SQLException e1) { - // should fail - - } - - // Use good password - pcds.getPooledConnection("u1", "p1").close(); - try { - pcds.getPooledConnection("u1", "x"); - fail("Able to retrieve connection with incorrect password"); - } - catch (final SQLException e) { - if (!e.getMessage().startsWith("x is not the correct password")) { - throw e; - } - // else the exception was expected - } - - // Make sure we can still use our good password. - pcds.getPooledConnection("u1", "p1").close(); - } - - /** - * JIRA: DBCP-442 - */ - @Test - public void testNullValidationQuery() throws Exception { - try (final SharedPoolDataSource spds = new SharedPoolDataSource()) { - spds.setConnectionPoolDataSource(pcds); - spds.setDefaultTestOnBorrow(true); - try (final Connection c = spds.getConnection()) { - // close right away - } - } - } - - @Test - public void testSetConnectionProperties() throws Exception { - // Set user property to bad value - pcds.setUser("bad"); - // Supply correct value in connection properties - // This will overwrite field value - final Properties properties = new Properties(); - properties.put(Constants.KEY_USER, "foo"); - properties.put(Constants.KEY_PASSWORD, pcds.getPassword()); - pcds.setConnectionProperties(properties); - pcds.getPooledConnection().close(); - assertEquals("foo", pcds.getUser()); - // Put bad password into properties - properties.put("password", "bad"); - // This does not change local field - assertEquals("bar", pcds.getPassword()); - // Supply correct password in getPooledConnection - // Call will succeed and overwrite property - pcds.getPooledConnection("foo", "bar").close(); - assertEquals("bar", pcds.getConnectionProperties().getProperty("password")); - } - - @Test - public void testSetConnectionPropertiesConnectionCalled() throws Exception { - final Properties properties = new Properties(); - // call to the connection - pcds.getPooledConnection().close(); - assertThrows(IllegalStateException.class, () -> pcds.setConnectionProperties(properties)); - } - - @Test - public void testSetConnectionPropertiesNull() throws Exception { - pcds.setConnectionProperties(null); - } - - @Test - public void testSetPasswordNull() throws Exception { - pcds.setPassword("Secret"); - assertEquals("Secret", pcds.getPassword()); - pcds.setPassword((char[]) null); - assertNull(pcds.getPassword()); - } - - @Test - public void testSetPasswordNullWithConnectionProperties() throws Exception { - pcds.setConnectionProperties(new Properties()); - pcds.setPassword("Secret"); - assertEquals("Secret", pcds.getPassword()); - pcds.setPassword((char[]) null); - assertNull(pcds.getPassword()); - } - - @Test - public void testSetPasswordThenModCharArray() { - final char[] pwd = {'a' }; - pcds.setPassword(pwd); - assertEquals("a", pcds.getPassword()); - pwd[0] = 'b'; - assertEquals("a", pcds.getPassword()); - } - - @Test - public void testSetUserNull() throws Exception { - pcds.setUser("Alice"); - assertEquals("Alice", pcds.getUser()); - pcds.setUser(null); - assertNull(pcds.getUser()); - } - - @Test - public void testSetUserNullWithConnectionProperties() throws Exception { - pcds.setConnectionProperties(new Properties()); - pcds.setUser("Alice"); - assertEquals("Alice", pcds.getUser()); - pcds.setUser(null); - assertNull(pcds.getUser()); - } - - @Test - public void testSimple() throws Exception { - try (final Connection conn = pcds.getPooledConnection().getConnection()) { - assertNotNull(conn); - try (final PreparedStatement stmt = conn.prepareStatement("select * from dual")) { - assertNotNull(stmt); - try (final ResultSet rset = stmt.executeQuery()) { - assertNotNull(rset); - assertTrue(rset.next()); - } - } - } - } - - @Test - public void testSimpleWithUsername() throws Exception { - try (final Connection conn = pcds.getPooledConnection("u1", "p1").getConnection()) { - assertNotNull(conn); - try (final PreparedStatement stmt = conn.prepareStatement("select * from dual")) { - assertNotNull(stmt); - try (final ResultSet rset = stmt.executeQuery()) { - assertNotNull(rset); - assertTrue(rset.next()); - } - } - } - } - - @Test - public void testToStringWithoutConnectionProperties() throws ClassNotFoundException - { - final DriverAdapterCPDS cleanCpds = new DriverAdapterCPDS(); - cleanCpds.setDriver( "org.apache.commons.dbcp2.TesterDriver" ); - cleanCpds.setUrl( "jdbc:apache:commons:testdriver" ); - cleanCpds.setUser( "foo" ); - cleanCpds.setPassword( "bar" ); - - cleanCpds.toString(); - } -} +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 org.apache.commons.dbcp2.cpdsadapter; + +import static org.junit.jupiter.api.Assertions.assertArrayEquals; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.junit.jupiter.api.Assertions.fail; + +import java.io.PrintWriter; +import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.sql.SQLFeatureNotSupportedException; +import java.time.Duration; +import java.util.Properties; + +import javax.naming.NamingException; +import javax.naming.Reference; +import javax.naming.StringRefAddr; +import javax.sql.DataSource; + +import org.apache.commons.dbcp2.Constants; +import org.apache.commons.dbcp2.datasources.SharedPoolDataSource; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +/** + * Tests for DriverAdapterCPDS + */ +public class TestDriverAdapterCPDS { + + private static class ThreadDbcp367 extends Thread { + + private final DataSource ds; + + private volatile boolean failed; + + public ThreadDbcp367(final DataSource ds) { + this.ds = ds; + } + + public boolean isFailed() { + return failed; + } + + @Override + public void run() { + Connection c = null; + try { + for (int j=0; j < 5000; j++) { + c = ds.getConnection(); + c.close(); + } + } catch (final SQLException sqle) { + failed = true; + sqle.printStackTrace(); + } + } + } + + private DriverAdapterCPDS pcds; + + @BeforeEach + public void setUp() throws Exception { + pcds = new DriverAdapterCPDS(); + pcds.setDriver("org.apache.commons.dbcp2.TesterDriver"); + pcds.setUrl("jdbc:apache:commons:testdriver"); + pcds.setUser("foo"); + pcds.setPassword("bar"); + pcds.setPoolPreparedStatements(false); + } + + @Test + public void testClosingWithUserName() + throws Exception { + final Connection[] c = new Connection[10]; + for (int i=0; i<c.length; i++) { + c[i] = pcds.getPooledConnection("u1", "p1").getConnection(); + } + + // close one of the connections + c[0].close(); + assertTrue(c[0].isClosed()); + // get a new connection + c[0] = pcds.getPooledConnection("u1", "p1").getConnection(); + + for (final Connection element : c) { + element.close(); + } + + // open all the connections + for (int i=0; i<c.length; i++) { + c[i] = pcds.getPooledConnection("u1", "p1").getConnection(); + } + for (final Connection element : c) { + element.close(); + } + } + + // https://issues.apache.org/jira/browse/DBCP-376 + @Test + public void testDbcp367() throws Exception { + final ThreadDbcp367[] threads = new ThreadDbcp367[200]; + + pcds.setPoolPreparedStatements(true); + pcds.setMaxPreparedStatements(-1); + pcds.setAccessToUnderlyingConnectionAllowed(true); + + try (final SharedPoolDataSource spds = new SharedPoolDataSource()) { + spds.setConnectionPoolDataSource(pcds); + spds.setMaxTotal(threads.length + 10); + spds.setDefaultMaxWait(Duration.ofMillis(-1)); + spds.setDefaultMaxIdle(10); + spds.setDefaultAutoCommit(Boolean.FALSE); + + spds.setValidationQuery("SELECT 1"); + spds.setDefaultDurationBetweenEvictionRuns(Duration.ofSeconds(10)); + spds.setDefaultNumTestsPerEvictionRun(-1); + spds.setDefaultTestWhileIdle(true); + spds.setDefaultTestOnBorrow(true); + spds.setDefaultTestOnReturn(false); + + for (int i = 0; i < threads.length; i++) { + threads[i] = new ThreadDbcp367(spds); + threads[i].start(); + } + + for (int i = 0; i < threads.length; i++) { + threads[i].join(); + Assertions.assertFalse(threads[i].isFailed(), "Thread " + i + " has failed"); + } + } + } + + @SuppressWarnings("deprecation") + @Test + public void testDeprecatedAccessors() { + int i = 0; + // + i++; + pcds.setMinEvictableIdleTimeMillis(i); + assertEquals(i, pcds.getMinEvictableIdleTimeMillis()); + assertEquals(Duration.ofMillis(i), pcds.getMinEvictableIdleDuration()); + // + i++; + pcds.setTimeBetweenEvictionRunsMillis(i); + assertEquals(i, pcds.getTimeBetweenEvictionRunsMillis()); + assertEquals(Duration.ofMillis(i), pcds.getDurationBetweenEvictionRuns()); + } + + @Test + public void testGetObjectInstance() throws Exception { + final Reference ref = pcds.getReference(); + final Object o = pcds.getObjectInstance(ref, null, null, null); + assertEquals(pcds.getDriver(), ((DriverAdapterCPDS) o).getDriver()); + } + + @Test + public void testGetObjectInstanceChangeDescription() throws Exception { + final Reference ref = pcds.getReference(); + for (int i = 0; i < ref.size(); i++) { + if (ref.get(i).getType().equals("description")) { + ref.remove(i); + break; + } + } + ref.add(new StringRefAddr("description", "anything")); + final Object o = pcds.getObjectInstance(ref, null, null, null); + assertEquals(pcds.getDescription(), ((DriverAdapterCPDS) o).getDescription()); + } + + @Test + public void testGetObjectInstanceNull() throws Exception { + final Object o = pcds.getObjectInstance(null, null, null, null); + assertNull(o); + } + + @Test + public void testGetParentLogger() { + assertThrows(SQLFeatureNotSupportedException.class, pcds::getParentLogger); + } + + @Test + public void testGetReference() throws NamingException { + final Reference ref = pcds.getReference(); + assertEquals(pcds.getDriver(), ref.get("driver").getContent()); + assertEquals(pcds.getDescription(), ref.get("description").getContent()); + } + + @Test + public void testGettersAndSetters() { + pcds.setUser("foo"); + assertEquals("foo", pcds.getUser()); + pcds.setPassword("bar"); + assertEquals("bar", pcds.getPassword()); + pcds.setPassword(new char[] {'a', 'b'}); + assertArrayEquals(new char[] {'a', 'b'}, pcds.getPasswordCharArray()); + final PrintWriter pw = new PrintWriter(System.err); + pcds.setLogWriter(pw); + @SuppressWarnings("resource") + final PrintWriter logWriter = pcds.getLogWriter(); + assertEquals(pw, logWriter); + pcds.setLoginTimeout(10); + assertEquals(10, pcds.getLoginTimeout()); + pcds.setMaxIdle(100); + assertEquals(100, pcds.getMaxIdle()); + pcds.setDurationBetweenEvictionRuns(Duration.ofMillis(100)); + assertEquals(100, pcds.getDurationBetweenEvictionRuns().toMillis()); + pcds.setNumTestsPerEvictionRun(1); + assertEquals(1, pcds.getNumTestsPerEvictionRun()); + pcds.setMinEvictableIdleDuration(Duration.ofMillis(11)); + assertEquals(Duration.ofMillis(11), pcds.getMinEvictableIdleDuration()); + pcds.setDescription("jo"); + assertEquals("jo", pcds.getDescription()); + } + + /** + * JIRA: DBCP-245 + */ + @Test + public void testIncorrectPassword() throws Exception + { + pcds.getPooledConnection("u2", "p2").close(); + try { + // Use bad password + pcds.getPooledConnection("u1", "zlsafjk"); + fail("Able to retrieve connection with incorrect password"); + } catch (final SQLException e1) { + // should fail + + } + + // Use good password + pcds.getPooledConnection("u1", "p1").close(); + try { + pcds.getPooledConnection("u1", "x"); + fail("Able to retrieve connection with incorrect password"); + } + catch (final SQLException e) { + if (!e.getMessage().startsWith("x is not the correct password")) { + throw e; + } + // else the exception was expected + } + + // Make sure we can still use our good password. + pcds.getPooledConnection("u1", "p1").close(); + } + + /** + * JIRA: DBCP-442 + */ + @Test + public void testNullValidationQuery() throws Exception { + try (final SharedPoolDataSource spds = new SharedPoolDataSource()) { + spds.setConnectionPoolDataSource(pcds); + spds.setDefaultTestOnBorrow(true); + try (final Connection c = spds.getConnection()) { + // close right away + } + } + } + + @Test + public void testSetConnectionProperties() throws Exception { + // Set user property to bad value + pcds.setUser("bad"); + // Supply correct value in connection properties + // This will overwrite field value + final Properties properties = new Properties(); + properties.put(Constants.KEY_USER, "foo"); + properties.put(Constants.KEY_PASSWORD, pcds.getPassword()); + pcds.setConnectionProperties(properties); + pcds.getPooledConnection().close(); + assertEquals("foo", pcds.getUser()); + // Put bad password into properties + properties.put("password", "bad"); + // This does not change local field + assertEquals("bar", pcds.getPassword()); + // Supply correct password in getPooledConnection + // Call will succeed and overwrite property + pcds.getPooledConnection("foo", "bar").close(); + assertEquals("bar", pcds.getConnectionProperties().getProperty("password")); + } + + @Test + public void testSetConnectionPropertiesConnectionCalled() throws Exception { + final Properties properties = new Properties(); + // call to the connection + pcds.getPooledConnection().close(); + assertThrows(IllegalStateException.class, () -> pcds.setConnectionProperties(properties)); + } + + @Test + public void testSetConnectionPropertiesNull() throws Exception { + pcds.setConnectionProperties(null); + } + + @Test + public void testSetPasswordNull() throws Exception { + pcds.setPassword("Secret"); + assertEquals("Secret", pcds.getPassword()); + pcds.setPassword((char[]) null); + assertNull(pcds.getPassword()); + } + + @Test + public void testSetPasswordNullWithConnectionProperties() throws Exception { + pcds.setConnectionProperties(new Properties()); + pcds.setPassword("Secret"); + assertEquals("Secret", pcds.getPassword()); + pcds.setPassword((char[]) null); + assertNull(pcds.getPassword()); + } + + @Test + public void testSetPasswordThenModCharArray() { + final char[] pwd = {'a'}; + pcds.setPassword(pwd); + assertEquals("a", pcds.getPassword()); + pwd[0] = 'b'; + assertEquals("a", pcds.getPassword()); + } + + @Test + public void testSetUserNull() throws Exception { + pcds.setUser("Alice"); + assertEquals("Alice", pcds.getUser()); + pcds.setUser(null); + assertNull(pcds.getUser()); + } + + @Test + public void testSetUserNullWithConnectionProperties() throws Exception { + pcds.setConnectionProperties(new Properties()); + pcds.setUser("Alice"); + assertEquals("Alice", pcds.getUser()); + pcds.setUser(null); + assertNull(pcds.getUser()); + } + + @Test + public void testSimple() throws Exception { + try (final Connection conn = pcds.getPooledConnection().getConnection()) { + assertNotNull(conn); + try (final PreparedStatement stmt = conn.prepareStatement("select * from dual")) { + assertNotNull(stmt); + try (final ResultSet rset = stmt.executeQuery()) { + assertNotNull(rset); + assertTrue(rset.next()); + } + } + } + } + + @Test + public void testSimpleWithUsername() throws Exception { + try (final Connection conn = pcds.getPooledConnection("u1", "p1").getConnection()) { + assertNotNull(conn); + try (final PreparedStatement stmt = conn.prepareStatement("select * from dual")) { + assertNotNull(stmt); + try (final ResultSet rset = stmt.executeQuery()) { + assertNotNull(rset); + assertTrue(rset.next()); + } + } + } + } + + @Test + public void testToStringWithoutConnectionProperties() throws ClassNotFoundException + { + final DriverAdapterCPDS cleanCpds = new DriverAdapterCPDS(); + cleanCpds.setDriver( "org.apache.commons.dbcp2.TesterDriver" ); + cleanCpds.setUrl( "jdbc:apache:commons:testdriver" ); + cleanCpds.setUser( "foo" ); + cleanCpds.setPassword( "bar" ); + + cleanCpds.toString(); + } +} diff --git a/src/test/java/org/apache/commons/dbcp2/datasources/TestCPDSConnectionFactory.java b/src/test/java/org/apache/commons/dbcp2/datasources/TestCPDSConnectionFactory.java index d2cb48ca..41a7476c 100644 --- a/src/test/java/org/apache/commons/dbcp2/datasources/TestCPDSConnectionFactory.java +++ b/src/test/java/org/apache/commons/dbcp2/datasources/TestCPDSConnectionFactory.java @@ -147,7 +147,7 @@ public class TestCPDSConnectionFactory { @Test public void testSetPasswordThenModCharArray_Deprecated() { final CPDSConnectionFactory factory = new CPDSConnectionFactory(cpds, null, -1, false, "userName", "password"); - final char[] pwd = {'a' }; + final char[] pwd = {'a'}; factory.setPassword(pwd); assertEquals("a", String.valueOf(factory.getPasswordCharArray())); pwd[0] = 'b';