Author: markt
Date: Wed Feb 26 15:11:39 2014
New Revision: 1572084
URL: http://svn.apache.org/r1572084
Log:
Fix FindBugs resource closure warnings
Modified:
commons/proper/dbcp/trunk/src/test/java/org/apache/commons/dbcp2/datasources/TestSharedPoolDataSource.java
Modified:
commons/proper/dbcp/trunk/src/test/java/org/apache/commons/dbcp2/datasources/TestSharedPoolDataSource.java
URL:
http://svn.apache.org/viewvc/commons/proper/dbcp/trunk/src/test/java/org/apache/commons/dbcp2/datasources/TestSharedPoolDataSource.java?rev=1572084&r1=1572083&r2=1572084&view=diff
==============================================================================
---
commons/proper/dbcp/trunk/src/test/java/org/apache/commons/dbcp2/datasources/TestSharedPoolDataSource.java
(original)
+++
commons/proper/dbcp/trunk/src/test/java/org/apache/commons/dbcp2/datasources/TestSharedPoolDataSource.java
Wed Feb 26 15:11:39 2014
@@ -21,6 +21,7 @@ import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
+import java.sql.Statement;
import java.util.ArrayList;
import javax.sql.DataSource;
@@ -87,29 +88,21 @@ public class TestSharedPoolDataSource ex
* then correct password for same user illustrates
* JIRA: DBCP-245
*/
- public void testIncorrectPassword() throws Exception
- {
+ public void testIncorrectPassword() throws Exception {
+
ds.getConnection("u2", "p2").close();
- try {
- // Use bad password
- ds.getConnection("u1", "zlsafjk");
+ try (Connection c = ds.getConnection("u1", "zlsafjk")){ // Use bad
password
fail("Able to retrieve connection with incorrect password");
} catch (SQLException e1) {
// should fail
-
}
// Use good password
ds.getConnection("u1", "p1").close();
- try
- {
- ds.getConnection("u1", "x");
+ try (Connection c = ds.getConnection("u1", "x")) {
fail("Able to retrieve connection with incorrect password");
- }
- catch (SQLException e)
- {
- if (!e.getMessage().startsWith("Given password did not match"))
- {
+ } catch (SQLException e) {
+ if (!e.getMessage().startsWith("Given password did not match")) {
throw e;
}
// else the exception was expected
@@ -120,13 +113,11 @@ public class TestSharedPoolDataSource ex
// Try related users and passwords
ds.getConnection("foo", "bar").close();
- try {
- ds.getConnection("foob", "ar");
+ try (Connection c = ds.getConnection("u1", "ar")) {
fail("Should have caused an SQLException");
} catch (SQLException expected) {
}
- try {
- ds.getConnection("foo", "baz");
+ try (Connection c = ds.getConnection("u1", "baz")) {
fail("Should have generated SQLException");
} catch (SQLException expected) {
}
@@ -217,13 +208,9 @@ public class TestSharedPoolDataSource ex
stmt.close();
conn.close();
- try
- {
- conn.createStatement();
+ try (Statement s = conn.createStatement()){
fail("Can't use closed connections");
- }
- catch(SQLException e)
- {
+ } catch(SQLException e) {
// expected
}
@@ -307,23 +294,16 @@ public class TestSharedPoolDataSource ex
}
@Override
- public void testMaxTotal()
- throws Exception
- {
+ public void testMaxTotal() throws Exception {
Connection[] c = new Connection[getMaxTotal()];
- for (int i=0; i<c.length; i++)
- {
+ for (int i=0; i<c.length; i++) {
c[i] = ds.getConnection();
assertTrue(c[i] != null);
}
- try
- {
- ds.getConnection();
+ try (Connection conn = ds.getConnection()){
fail("Allowed to open more than DefaultMaxTotal connections.");
- }
- catch(java.sql.SQLException e)
- {
+ } catch(java.sql.SQLException e) {
// should only be able to open 10 connections, so this test should
// throw an exception
}
@@ -560,8 +540,7 @@ public class TestSharedPoolDataSource ex
// See DBCP-8
public void testChangePassword() throws Exception {
- try {
- ds.getConnection("foo", "bay");
+ try (Connection c = ds.getConnection("foo", "bay")){
fail("Should have generated SQLException");
} catch (SQLException expected) {
}
@@ -571,8 +550,7 @@ public class TestSharedPoolDataSource ex
con1.close();
con2.close();
TesterDriver.addUser("foo","bay"); // change the user/password setting
- try {
- Connection con4 = ds.getConnection("foo", "bay"); // new password
+ try (Connection con4 = ds.getConnection("foo", "bay")) { // new
password
// Idle instances with old password should have been cleared
assertEquals("Should be no idle connections in the pool",
0, ((SharedPoolDataSource) ds).getNumIdle());
@@ -580,8 +558,7 @@ public class TestSharedPoolDataSource ex
// Should be one idle instance with new pwd
assertEquals("Should be one idle connection in the pool",
1, ((SharedPoolDataSource) ds).getNumIdle());
- try {
- ds.getConnection("foo", "bar"); // old password
+ try (Connection con4b = ds.getConnection("foo", "bar")) { // old
password
fail("Should have generated SQLException");
} catch (SQLException expected) {
}