CLOUDSTACK-8656: restructure of tests

Project: http://git-wip-us.apache.org/repos/asf/cloudstack/repo
Commit: http://git-wip-us.apache.org/repos/asf/cloudstack/commit/bb8facc3
Tree: http://git-wip-us.apache.org/repos/asf/cloudstack/tree/bb8facc3
Diff: http://git-wip-us.apache.org/repos/asf/cloudstack/diff/bb8facc3

Branch: refs/heads/master
Commit: bb8facc37c5ff5268a962feab7fd868a8437d4be
Parents: 882de20
Author: Daan Hoogland <[email protected]>
Authored: Fri Aug 14 16:07:12 2015 +0200
Committer: Daan Hoogland <[email protected]>
Committed: Fri Aug 14 16:07:12 2015 +0200

----------------------------------------------------------------------
 .../com/cloud/utils/db/TransactionTest.java     | 75 ++++----------------
 1 file changed, 14 insertions(+), 61 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cloudstack/blob/bb8facc3/framework/db/test/com/cloud/utils/db/TransactionTest.java
----------------------------------------------------------------------
diff --git a/framework/db/test/com/cloud/utils/db/TransactionTest.java 
b/framework/db/test/com/cloud/utils/db/TransactionTest.java
index 76bd5a5..fed6632 100644
--- a/framework/db/test/com/cloud/utils/db/TransactionTest.java
+++ b/framework/db/test/com/cloud/utils/db/TransactionTest.java
@@ -38,32 +38,17 @@ public class TransactionTest {
 
     @BeforeClass
     public static void oneTimeSetup() {
-        Connection conn = null;
-        PreparedStatement pstmt = null;
-        try {
-            conn = TransactionLegacy.getStandaloneConnection();
-
-            pstmt =
-                conn.prepareStatement("CREATE TABLE `cloud`.`test` (" + "`id` 
bigint unsigned NOT NULL UNIQUE AUTO_INCREMENT," + "`fld_int` int unsigned,"
-                    + "`fld_long` bigint unsigned," + "`fld_string` 
varchar(255)," + "PRIMARY KEY (`id`)" + ") ENGINE=InnoDB DEFAULT 
CHARSET=utf8;");
+        try (
+                Connection conn = TransactionLegacy.getStandaloneConnection();
+                PreparedStatement pstmt =
+                    conn.prepareStatement("CREATE TABLE `cloud`.`test` (" + 
"`id` bigint unsigned NOT NULL UNIQUE AUTO_INCREMENT," + "`fld_int` int 
unsigned,"
+                        + "`fld_long` bigint unsigned," + "`fld_string` 
varchar(255)," + "PRIMARY KEY (`id`)" + ") ENGINE=InnoDB DEFAULT 
CHARSET=utf8;");
+            ) {
 
             pstmt.execute();
 
         } catch (SQLException e) {
             throw new CloudRuntimeException("Problem with sql", e);
-        } finally {
-            if (pstmt != null) {
-                try {
-                    pstmt.close();
-                } catch (SQLException e) {
-                }
-            }
-            if (conn != null) {
-                try {
-                    conn.close();
-                } catch (SQLException e) {
-                }
-            }
         }
     }
 
@@ -157,57 +142,25 @@ public class TransactionTest {
      * Delete all records after each test, but table is still kept
      */
     public void tearDown() {
-        Connection conn = null;
-        PreparedStatement pstmt = null;
-        try {
-            conn = TransactionLegacy.getStandaloneConnection();
-
-            pstmt = conn.prepareStatement("truncate table `cloud`.`test`");
+        try (
+                Connection conn = TransactionLegacy.getStandaloneConnection();
+                PreparedStatement pstmt = conn.prepareStatement("truncate 
table `cloud`.`test`");
+            ) {
             pstmt.execute();
-
         } catch (SQLException e) {
             throw new CloudRuntimeException("Problem with sql", e);
-        } finally {
-            if (pstmt != null) {
-                try {
-                    pstmt.close();
-                } catch (SQLException e) {
-                }
-            }
-            if (conn != null) {
-                try {
-                    conn.close();
-                } catch (SQLException e) {
-                }
-            }
         }
     }
 
     @AfterClass
     public static void oneTimeTearDown() {
-        Connection conn = null;
-        PreparedStatement pstmt = null;
-        try {
-            conn = TransactionLegacy.getStandaloneConnection();
-
-            pstmt = conn.prepareStatement("DROP TABLE IF EXISTS 
`cloud`.`test`");
+        try (
+                Connection conn = TransactionLegacy.getStandaloneConnection();
+                PreparedStatement pstmt = conn.prepareStatement("DROP TABLE IF 
EXISTS `cloud`.`test`");
+            ) {
             pstmt.execute();
-
         } catch (SQLException e) {
             throw new CloudRuntimeException("Problem with sql", e);
-        } finally {
-            if (pstmt != null) {
-                try {
-                    pstmt.close();
-                } catch (SQLException e) {
-                }
-            }
-            if (conn != null) {
-                try {
-                    conn.close();
-                } catch (SQLException e) {
-                }
-            }
         }
     }
 }

Reply via email to