From 5ff4608fb87ba8b54442c972fe2f1035ed7e49a5 Mon Sep 17 00:00:00 2001
From: kruthar <kruthar@gmail.com>
Date: Tue, 3 Nov 2015 22:28:51 -0600
Subject: [PATCH] [jdbc] testing code cleanup

reverted a few things that were not necessary
* semicolon change - coming in another PR
* .gitignore change - no longer using derby

Code cleanup
* test scoped hsqldb
* removed derby artifacts and references
* setting driver class is not necessary
* cleaned up unused objects in delete
* now running cleanup on client
* removed old assert(true) stub
---
 .gitignore                                    |  1 -
 jdbc/pom.xml                                  |  1 +
 .../java/com/yahoo/ycsb/db/JdbcDBClient.java  |  8 +--
 .../com/yahoo/ycsb/db/JdbcDBClientTest.java   | 49 ++++++++++++-------
 4 files changed, 35 insertions(+), 24 deletions(-)

diff --git a/.gitignore b/.gitignore
index 98c46d18..882b67e2 100644
--- a/.gitignore
+++ b/.gitignore
@@ -3,7 +3,6 @@ target
 
 # ignore output files from testing
 output*
-derby.log
 
 # ignore standard eclipse
 .project
diff --git a/jdbc/pom.xml b/jdbc/pom.xml
index bebf2a3b..c03a9b68 100644
--- a/jdbc/pom.xml
+++ b/jdbc/pom.xml
@@ -51,6 +51,7 @@ LICENSE file.
       <groupId>org.hsqldb</groupId>
       <artifactId>hsqldb</artifactId>
       <version>2.3.3</version>
+      <scope>test</scope>
     </dependency>
   </dependencies>
 </project>
diff --git a/jdbc/src/main/java/com/yahoo/ycsb/db/JdbcDBClient.java b/jdbc/src/main/java/com/yahoo/ycsb/db/JdbcDBClient.java
index d59b89f6..968d7322 100644
--- a/jdbc/src/main/java/com/yahoo/ycsb/db/JdbcDBClient.java
+++ b/jdbc/src/main/java/com/yahoo/ycsb/db/JdbcDBClient.java
@@ -245,7 +245,7 @@ public class JdbcDBClient extends DB implements JdbcDBClientConstants {
     for (int i = 0; i < insertType.numFields; i++) {
       insert.append(",?");
     }
-    insert.append(")");
+    insert.append(");");
     PreparedStatement insertStatement = getShardConnectionByKey(key).prepareStatement(insert.toString());
     PreparedStatement stmt = cachedStatements.putIfAbsent(insertType, insertStatement);
     if (stmt == null) return insertStatement;
@@ -259,7 +259,7 @@ public class JdbcDBClient extends DB implements JdbcDBClientConstants {
     read.append(" WHERE ");
     read.append(PRIMARY_KEY);
     read.append(" = ");
-    read.append("?");
+    read.append("?;");
     PreparedStatement readStatement = getShardConnectionByKey(key).prepareStatement(read.toString());
     PreparedStatement stmt = cachedStatements.putIfAbsent(readType, readStatement);
     if (stmt == null) return readStatement;
@@ -272,7 +272,7 @@ public class JdbcDBClient extends DB implements JdbcDBClientConstants {
     delete.append(deleteType.tableName);
     delete.append(" WHERE ");
     delete.append(PRIMARY_KEY);
-    delete.append(" = ?");
+    delete.append(" = ?;");
     PreparedStatement deleteStatement = getShardConnectionByKey(key).prepareStatement(delete.toString());
     PreparedStatement stmt = cachedStatements.putIfAbsent(deleteType, deleteStatement);
     if (stmt == null) return deleteStatement;
@@ -292,7 +292,7 @@ public class JdbcDBClient extends DB implements JdbcDBClientConstants {
     }
     update.append(" WHERE ");
     update.append(PRIMARY_KEY);
-    update.append(" = ?");
+    update.append(" = ?;");
     PreparedStatement insertStatement = getShardConnectionByKey(key).prepareStatement(update.toString());
     PreparedStatement stmt = cachedStatements.putIfAbsent(updateType, insertStatement);
     if (stmt == null) return insertStatement;
diff --git a/jdbc/src/test/java/com/yahoo/ycsb/db/JdbcDBClientTest.java b/jdbc/src/test/java/com/yahoo/ycsb/db/JdbcDBClientTest.java
index e2983d08..901c8a66 100644
--- a/jdbc/src/test/java/com/yahoo/ycsb/db/JdbcDBClientTest.java
+++ b/jdbc/src/test/java/com/yahoo/ycsb/db/JdbcDBClientTest.java
@@ -1,3 +1,20 @@
+/**
+ * Copyright (c) 2015 Yahoo! Inc. All rights reserved.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License"); you
+ * may not use this file except in compliance with the License. You
+ * may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
+ * implied. See the License for the specific language governing
+ * permissions and limitations under the License. See accompanying
+ * LICENSE file.
+ */
+
 package com.yahoo.ycsb.db;
 
 import static org.junit.Assert.*;
@@ -33,9 +50,7 @@ public class JdbcDBClientTest {
     @BeforeClass
     public static void setup() {
         try {
-            Class driverClass = Class.forName(TEST_DB_DRIVER);
             jdbcConnection = DriverManager.getConnection(TEST_DB_URL);
-
             jdbcDBClient = new JdbcDBClient();
 
             Properties p = new Properties();
@@ -45,10 +60,6 @@ public class JdbcDBClientTest {
 
             jdbcDBClient.setProperties(p);
             jdbcDBClient.init();
-
-        } catch (ClassNotFoundException e) {
-            e.printStackTrace();
-            fail("Could not find Driver Class: " + TEST_DB_DRIVER);
         } catch (SQLException e) {
             e.printStackTrace();
             fail("Could not create local Database");
@@ -61,13 +72,19 @@ public class JdbcDBClientTest {
     @AfterClass
     public static void teardown() {
         try {
-            jdbcConnection.close();
-            jdbcConnection = DriverManager.getConnection(TEST_DB_URL + ";shutdown=true");
-        } catch (SQLNonTransientConnectionException e) {
-            // Expected exception when database is destroyed
+            if (jdbcConnection != null) {
+                jdbcConnection.close();
+            }
         } catch (SQLException e) {
             e.printStackTrace();
-            fail("Could not drop local Database");
+        }
+        
+        try {
+            if (jdbcDBClient != null) {
+                jdbcDBClient.cleanup();
+            }
+        } catch (DBException e) {
+            e.printStackTrace();
         }
     }
 
@@ -98,7 +115,6 @@ public class JdbcDBClientTest {
             e.printStackTrace();
             fail("Failed to prepare test");
         }
-
     }
 
     /*
@@ -224,9 +240,6 @@ public class JdbcDBClientTest {
             e.printStackTrace();
             fail("Failed updateTest");
         }
-
-
-        assertTrue(true);
     }
 
     @Test
@@ -263,12 +276,10 @@ public class JdbcDBClientTest {
     @Test
     public void deleteTest() {
         try {
-            String insertBeforeKey = "user0";
-            HashMap<String, ByteIterator> insertBeforeMap = insertRow(insertBeforeKey);
+            insertRow("user0");
             String deleteKey = "user1";
             insertRow(deleteKey);
-            String insertAfterKey = "user2";
-            HashMap<String, ByteIterator> insertAfterMap = insertRow(insertAfterKey);
+            insertRow("user2");
 
             jdbcDBClient.delete(TABLE_NAME, deleteKey);
 
-- 
GitLab