Author: markt
Date: Wed Feb 26 15:18:14 2014
New Revision: 1572088
URL: http://svn.apache.org/r1572088
Log:
FindBugs low hanging fruit
Modified:
commons/proper/dbcp/trunk/src/test/java/org/apache/commons/dbcp2/TestBasicDataSourceFactory.java
commons/proper/dbcp/trunk/src/test/java/org/apache/commons/dbcp2/TestDelegatingConnection.java
commons/proper/dbcp/trunk/src/test/java/org/apache/commons/dbcp2/TestPStmtPoolingBasicDataSource.java
commons/proper/dbcp/trunk/src/test/java/org/apache/commons/dbcp2/TesterConnection.java
Modified:
commons/proper/dbcp/trunk/src/test/java/org/apache/commons/dbcp2/TestBasicDataSourceFactory.java
URL:
http://svn.apache.org/viewvc/commons/proper/dbcp/trunk/src/test/java/org/apache/commons/dbcp2/TestBasicDataSourceFactory.java?rev=1572088&r1=1572087&r2=1572088&view=diff
==============================================================================
---
commons/proper/dbcp/trunk/src/test/java/org/apache/commons/dbcp2/TestBasicDataSourceFactory.java
(original)
+++
commons/proper/dbcp/trunk/src/test/java/org/apache/commons/dbcp2/TestBasicDataSourceFactory.java
Wed Feb 26 15:18:14 2014
@@ -20,8 +20,6 @@ package org.apache.commons.dbcp2;
import java.sql.Connection;
import java.util.Properties;
-import javax.sql.DataSource;
-
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
@@ -43,10 +41,9 @@ public class TestBasicDataSourceFactory
public void testNoProperties() throws Exception {
Properties properties = new Properties();
- DataSource ds = BasicDataSourceFactory.createDataSource(properties);
+ BasicDataSource ds =
BasicDataSourceFactory.createDataSource(properties);
assertNotNull(ds);
- assertTrue(ds instanceof BasicDataSource);
}
public void testProperties() throws Exception {
Modified:
commons/proper/dbcp/trunk/src/test/java/org/apache/commons/dbcp2/TestDelegatingConnection.java
URL:
http://svn.apache.org/viewvc/commons/proper/dbcp/trunk/src/test/java/org/apache/commons/dbcp2/TestDelegatingConnection.java?rev=1572088&r1=1572087&r2=1572088&view=diff
==============================================================================
---
commons/proper/dbcp/trunk/src/test/java/org/apache/commons/dbcp2/TestDelegatingConnection.java
(original)
+++
commons/proper/dbcp/trunk/src/test/java/org/apache/commons/dbcp2/TestDelegatingConnection.java
Wed Feb 26 15:18:14 2014
@@ -18,6 +18,7 @@
package org.apache.commons.dbcp2;
import java.sql.Connection;
+import java.sql.PreparedStatement;
import java.sql.SQLException;
import junit.framework.Test;
@@ -99,7 +100,7 @@ public class TestDelegatingConnection ex
conn = new DelegatingConnection<>(pc);
pc.close();
conn.close();
- conn.prepareStatement("");
+ try (PreparedStatement ps = conn.prepareStatement("")){}
fail("Expecting SQLException");
} catch (SQLException ex) {
assertTrue(ex.getMessage().endsWith("is closed."));
Modified:
commons/proper/dbcp/trunk/src/test/java/org/apache/commons/dbcp2/TestPStmtPoolingBasicDataSource.java
URL:
http://svn.apache.org/viewvc/commons/proper/dbcp/trunk/src/test/java/org/apache/commons/dbcp2/TestPStmtPoolingBasicDataSource.java?rev=1572088&r1=1572087&r2=1572088&view=diff
==============================================================================
---
commons/proper/dbcp/trunk/src/test/java/org/apache/commons/dbcp2/TestPStmtPoolingBasicDataSource.java
(original)
+++
commons/proper/dbcp/trunk/src/test/java/org/apache/commons/dbcp2/TestPStmtPoolingBasicDataSource.java
Wed Feb 26 15:18:14 2014
@@ -5,9 +5,9 @@
* 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.
@@ -27,7 +27,7 @@ import junit.framework.TestSuite;
/**
* TestSuite for BasicDataSource with prepared statement pooling enabled
- *
+ *
* @author Dirk Verbeeck
* @version $Revision$ $Date$
*/
@@ -58,23 +58,22 @@ public class TestPStmtPoolingBasicDataSo
public void testPreparedStatementPooling() throws Exception {
Connection conn = getConnection();
assertNotNull(conn);
-
+
PreparedStatement stmt1 = conn.prepareStatement("select 'a' from
dual");
assertNotNull(stmt1);
-
+
PreparedStatement stmt2 = conn.prepareStatement("select 'b' from
dual");
assertNotNull(stmt2);
-
+
assertTrue(stmt1 != stmt2);
-
+
// go over the maxOpen limit
PreparedStatement stmt3 = null;
- try {
- conn.prepareStatement("select 'c' from dual");
+ try (PreparedStatement ps = conn.prepareStatement("select 'c' from
dual")) {
fail("expected SQLException");
- }
+ }
catch (SQLException e) {}
-
+
// make idle
stmt2.close();
@@ -83,13 +82,13 @@ public class TestPStmtPoolingBasicDataSo
assertNotNull(stmt3);
assertTrue(stmt3 != stmt1);
assertTrue(stmt3 != stmt2);
-
+
// normal reuse of statement
stmt1.close();
PreparedStatement stmt4 = conn.prepareStatement("select 'a' from
dual");
assertNotNull(stmt4);
}
-
+
/**
* Verifies that the prepared statement pool behaves as an LRU cache,
* closing least-recently-used statements idle in the pool to make room
@@ -97,10 +96,10 @@ public class TestPStmtPoolingBasicDataSo
*/
public void testLRUBehavior() throws Exception {
ds.setMaxOpenPreparedStatements(3);
-
+
Connection conn = getConnection();
assertNotNull(conn);
-
+
// Open 3 statements and then close them into the pool
PreparedStatement stmt1 = conn.prepareStatement("select 'a' from
dual");
PreparedStatement inner1 = (PreparedStatement)
((DelegatingPreparedStatement) stmt1).getInnermostDelegate();
@@ -111,16 +110,16 @@ public class TestPStmtPoolingBasicDataSo
stmt1.close();
Thread.sleep(100); // Make sure return timestamps are different
stmt2.close();
- Thread.sleep(100);
+ Thread.sleep(100);
stmt3.close();
-
+
// Pool now has three idle statements, getting another one will force
oldest (stmt1) out
PreparedStatement stmt4 = conn.prepareStatement("select 'd' from
dual");
assertNotNull(stmt4);
-
+
// Verify that inner1 has been closed
try {
- inner1.clearParameters();
+ inner1.clearParameters();
fail("expecting SQLExcption - statement should be closed");
} catch (SQLException ex) {
//Expected
@@ -128,25 +127,25 @@ public class TestPStmtPoolingBasicDataSo
// But others are still open
inner2.clearParameters();
inner3.clearParameters();
-
+
// Now make sure stmt1 does not come back from the dead
PreparedStatement stmt5 = conn.prepareStatement("select 'a' from
dual");
PreparedStatement inner5 = (PreparedStatement)
((DelegatingPreparedStatement) stmt5).getInnermostDelegate();
assertNotSame(inner5, inner1);
-
+
// inner2 should be closed now
try {
- inner2.clearParameters();
+ inner2.clearParameters();
fail("expecting SQLExcption - statement should be closed");
} catch (SQLException ex) {
//Expected
}
// But inner3 should still be open
- inner3.clearParameters();
+ inner3.clearParameters();
}
// Bugzilla Bug 27246
- // PreparedStatement cache should be different depending on the Catalog
+ // PreparedStatement cache should be different depending on the Catalog
public void testPStmtCatalog() throws Exception {
Connection conn = getConnection();
conn.setCatalog("catalog1");
@@ -154,7 +153,7 @@ public class TestPStmtPoolingBasicDataSo
TesterPreparedStatement inner1 = (TesterPreparedStatement)
stmt1.getInnermostDelegate();
assertEquals("catalog1", inner1.getCatalog());
stmt1.close();
-
+
conn.setCatalog("catalog2");
DelegatingPreparedStatement stmt2 = (DelegatingPreparedStatement)
conn.prepareStatement("select 'a' from dual");
TesterPreparedStatement inner2 = (TesterPreparedStatement)
stmt2.getInnermostDelegate();
@@ -166,7 +165,7 @@ public class TestPStmtPoolingBasicDataSo
TesterPreparedStatement inner3 = (TesterPreparedStatement)
stmt1.getInnermostDelegate();
assertEquals("catalog1", inner3.getCatalog());
stmt3.close();
-
+
assertNotSame(inner1, inner2);
assertSame(inner1, inner3);
}
@@ -179,29 +178,29 @@ public class TestPStmtPoolingBasicDataSo
assertNotNull(conn1);
assertEquals(1, ds.getNumActive());
assertEquals(0, ds.getNumIdle());
-
+
PreparedStatement stmt1 = conn1.prepareStatement("select 'a' from
dual");
assertNotNull(stmt1);
-
+
Statement inner1 = ((DelegatingPreparedStatement)
stmt1).getInnermostDelegate();
assertNotNull(inner1);
-
+
stmt1.close();
Connection conn2 = conn1;
assertNotNull(conn2);
assertEquals(1, ds.getNumActive());
assertEquals(0, ds.getNumIdle());
-
+
PreparedStatement stmt2 = conn2.prepareStatement("select 'a' from
dual");
assertNotNull(stmt2);
Statement inner2 = ((DelegatingPreparedStatement)
stmt2).getInnermostDelegate();
assertNotNull(inner2);
-
+
assertSame(inner1, inner2);
}
-
+
public void testPStmtPoolingAccrossClose() throws Exception {
ds.setMaxTotal(1); // only one connection in pool needed
ds.setMaxIdle(1);
@@ -210,13 +209,13 @@ public class TestPStmtPoolingBasicDataSo
assertNotNull(conn1);
assertEquals(1, ds.getNumActive());
assertEquals(0, ds.getNumIdle());
-
+
PreparedStatement stmt1 = conn1.prepareStatement("select 'a' from
dual");
assertNotNull(stmt1);
-
+
Statement inner1 = ((DelegatingPreparedStatement)
stmt1).getInnermostDelegate();
assertNotNull(inner1);
-
+
stmt1.close();
conn1.close();
@@ -227,13 +226,13 @@ public class TestPStmtPoolingBasicDataSo
assertNotNull(conn2);
assertEquals(1, ds.getNumActive());
assertEquals(0, ds.getNumIdle());
-
+
PreparedStatement stmt2 = conn2.prepareStatement("select 'a' from
dual");
assertNotNull(stmt2);
Statement inner2 = ((DelegatingPreparedStatement)
stmt2).getInnermostDelegate();
assertNotNull(inner2);
-
+
assertSame(inner1, inner2);
}
}
Modified:
commons/proper/dbcp/trunk/src/test/java/org/apache/commons/dbcp2/TesterConnection.java
URL:
http://svn.apache.org/viewvc/commons/proper/dbcp/trunk/src/test/java/org/apache/commons/dbcp2/TesterConnection.java?rev=1572088&r1=1572087&r2=1572088&view=diff
==============================================================================
---
commons/proper/dbcp/trunk/src/test/java/org/apache/commons/dbcp2/TesterConnection.java
(original)
+++
commons/proper/dbcp/trunk/src/test/java/org/apache/commons/dbcp2/TesterConnection.java
Wed Feb 26 15:18:14 2014
@@ -52,12 +52,11 @@ public class TesterConnection implements
protected boolean _readOnly = false;
protected SQLWarning warnings = null;
protected String username = null;
- protected String password = null;
protected Exception failure;
- public TesterConnection(String username, String password) {
+ public TesterConnection(String username,
+ @SuppressWarnings("unused") String password) {
this.username = username;
- this.password = password;
}
public String getUsername() {