Author: gdusbabek
Date: Wed Mar  9 20:22:55 2011
New Revision: 1079974

URL: http://svn.apache.org/viewvc?rev=1079974&view=rev
Log:
clean up JdbcDriverTest. patch by gdusbabek, reviewed by eevans. CASSANDRA-2124

Modified:
    
cassandra/trunk/drivers/java/test/org/apache/cassandra/cql/driver/jdbc/JdbcDriverTest.java

Modified: 
cassandra/trunk/drivers/java/test/org/apache/cassandra/cql/driver/jdbc/JdbcDriverTest.java
URL: 
http://svn.apache.org/viewvc/cassandra/trunk/drivers/java/test/org/apache/cassandra/cql/driver/jdbc/JdbcDriverTest.java?rev=1079974&r1=1079973&r2=1079974&view=diff
==============================================================================
--- 
cassandra/trunk/drivers/java/test/org/apache/cassandra/cql/driver/jdbc/JdbcDriverTest.java
 (original)
+++ 
cassandra/trunk/drivers/java/test/org/apache/cassandra/cql/driver/jdbc/JdbcDriverTest.java
 Wed Mar  9 20:22:55 2011
@@ -29,6 +29,7 @@ import java.sql.SQLException;
 import java.sql.Statement;
 
 import org.apache.cassandra.config.ConfigurationException;
+import org.junit.Test;
 
 /**
  * Test case for unit test of various methods of JDBC implementation.
@@ -37,9 +38,7 @@ public class JdbcDriverTest extends Embe
 {
     private java.sql.Connection con = null;
 
-    /**
-     * SetUp
-     */
+    /** SetUp */
     @Override
     protected void setUp()
     {
@@ -48,7 +47,8 @@ public class JdbcDriverTest extends Embe
             startCassandraServer();
             
Class.forName("org.apache.cassandra.cql.driver.jdbc.CassandraDriver");
             con = 
DriverManager.getConnection("jdbc:cassandra:root/root@localhost:9170/Keyspace1");
-            prepareData();
+            final String updateQ = "UPDATE Standard1 SET \"first\" = 
\"firstrec\", \"last\" = \"lastrec\" WHERE KEY = \"jsmith\"";
+            executeNoResults(con, updateQ);
         }
         catch (ClassNotFoundException e)
         {
@@ -68,14 +68,15 @@ public class JdbcDriverTest extends Embe
         }
     }
 
-    /**
-     * Method to test statement.
-     */
+    /** Method to test statement. */
+    @Test
     public void testWithStatement()
     {
         try
         {
-            scrollResultset(withStatement(con), "first", "last");
+            String selectQ = "SELECT \"first\", \"last\" FROM Standard1 WHERE 
KEY=\"jsmith\"";
+            Statement stmt = con.createStatement();
+            scrollResultset(stmt.executeQuery(selectQ), "first", "last");
         }
         catch (SQLException e)
         {
@@ -83,15 +84,14 @@ public class JdbcDriverTest extends Embe
         }
     }
 
-   /**
-     * Method to test with prepared statement.
-     */
+   /** Method to test with prepared statement.*/
+   @Test
     public void testWithPreparedStatement()
     {
         try
         {
             final String selectQ = "SELECT \"first\", \"last\" FROM Standard1 
WHERE KEY=\"jsmith\"";
-            scrollResultset(withPreparedStatement(con, selectQ), "first", 
"last");
+            scrollResultset(executePreparedStatementWithResults(con, selectQ), 
"first", "last");
         }
         catch (SQLException e)
         {
@@ -100,17 +100,16 @@ public class JdbcDriverTest extends Embe
         tearDown();
     }
 
-    /**
-     * Method to test with update statement.
-     */
+    /** Method to test with update statement.*/
+    @Test
     public void testWithUpdateStatement()
     {
         try
         {
             final String updateQ = "UPDATE Standard1 SET \"firstN\" = 
\"jdbc\", \"lastN\" = \"m\" WHERE KEY = \"jsmith\"";
-            withUpdateStatement(con, updateQ);
+            executeNoResults(con, updateQ);
             final String updateSelect = "SELECT \"firstN\", \"lastN\" FROM 
Standard1 WHERE KEY=\"jsmith\"";
-            scrollResultset(withPreparedStatement(con, updateSelect), 
"firstN", "lastN");
+            scrollResultset(executePreparedStatementWithResults(con, 
updateSelect), "firstN", "lastN");
         }
         catch (SQLException e)
         {
@@ -118,18 +117,17 @@ public class JdbcDriverTest extends Embe
         }
     }
 
-    /**
-     * Method to test with Delete statement.
-     */
+    /* Method to test with Delete statement. */
+    @Test
     public void testWithDeleteStatement()
     {
         try
         {
             // Delete
             final String deleteQ = "DELETE \"firstN\", \"lastN\" FROM 
Standard1 WHERE KEY=\"jsmith\"";
-            withDeleteStatement(con, deleteQ);
+            executeNoResults(con, deleteQ);
             String updateSelect = "SELECT \"firstN\", \"lastN\" FROM Standard1 
WHERE KEY=\"jsmith\"";
-            scrollResultset(withPreparedStatement(con, updateSelect), 
"firstN", "lastN");
+            scrollResultset(executePreparedStatementWithResults(con, 
updateSelect), "firstN", "lastN");
         } 
         catch (SQLException e)
         {
@@ -144,7 +142,8 @@ public class JdbcDriverTest extends Embe
         {
             if (con != null)
             {
-                deleteData();
+                final String updateQ = "TRUNCATE Standard1";
+                executeNoResults(con, updateQ);
                 con.close();
                 con = null;
             }
@@ -155,117 +154,29 @@ public class JdbcDriverTest extends Embe
         }
     }
 
-    /**
-     * Method to prepare data.
-     * 
-     * @throws SQLException
-     *             sql exception.
-     */
-    private void prepareData() throws SQLException
-    {
-        final String updateQ = "UPDATE Standard1 SET \"first\" = \"firstrec\", 
\"last\" = \"lastrec\" WHERE KEY = \"jsmith\"";
-        withUpdateStatement(con, updateQ);
-    }
-
-    /**
-     * Method to delete data.
-     * 
-     * @throws SQLException
-     *             sql exception.
-     */
-    private void deleteData() throws SQLException
-    {
-        final String updateQ = "TRUNCATE Standard1";
-        withUpdateStatement(con, updateQ);
-    }
-
-    /**
-     * With statement method.
-     * 
-     * @param con
-     *            connection object.
-     * @return rSet result set.
-     * @throws SQLException
-     *             sql exception.
-     */
-    private static ResultSet withStatement(final Connection con) throws 
SQLException
-    {
-        String selectQ = "SELECT \"first\", \"last\" FROM Standard1 WHERE 
KEY=\"jsmith\"";
-        Statement stmt = con.createStatement();
-        return stmt.executeQuery(selectQ);
-    }
-
-    /**
-     * With prepared statement method.
-     * 
-     * @param con
-     *            connection object.
-     * @return rSet result set.
-     * @throws SQLException
-     *             sql exception.
-     */
-    private static ResultSet withPreparedStatement(final Connection con, final 
String selectQ) throws SQLException
-    {
-        PreparedStatement statement = con.prepareStatement(selectQ);
-        return statement.executeQuery();
-    }
-
-    /**
-     * With scroll result set.
-     * 
-     * @param rSet
-     *            result set.
-     * @param columnName
-     *            column names.
-     * @throws SQLException
-     *             sql exception.
-     */
+    /** iterates over a result set checking columns */
     private static void scrollResultset(final ResultSet rSet, final String... 
columnNames) throws SQLException
     {
         assertNotNull(rSet);
         while (rSet.next())
         {
             assertNotNull(rSet.getString(0));
-            assertNotNull(rSet.getString(columnNames[0]));
-            assertNotNull(rSet.getString(columnNames[1]));
+            for (String colName : columnNames)
+                assertNotNull(rSet.getString(colName));
         }
     }
-
-    /**
-     * With update statement
-     * 
-     * @param con
-     *            connection object.
-     * @param updateQ
-     *            update query.
-     * @throws SQLException
-     *             sql exception.
-     */
-    private static void withUpdateStatement(final Connection con, final String 
updateQ) throws SQLException
+    
+    /** executes a prepared statement */
+    private static ResultSet executePreparedStatementWithResults(final 
Connection con, final String selectQ) throws SQLException
     {
-        PreparedStatement statement = con.prepareStatement(updateQ);
-        statement.execute();
+        PreparedStatement statement = con.prepareStatement(selectQ);
+        return statement.executeQuery();
     }
 
-    /**
-     * With Delete statement
-     * 
-     * @param con
-     *            connection object.
-     * @param deleteQ
-     *            delete query.
-     * @throws SQLException
-     *             sql exception.
-     */
-    private static void withDeleteStatement(final Connection con, final String 
deleteQ) throws SQLException
+    /** executes an prepared statement */
+    private static void executeNoResults(final Connection con, final String 
cql) throws SQLException
     {
-        PreparedStatement statement = con.prepareStatement(deleteQ);
+        PreparedStatement statement = con.prepareStatement(cql);
         statement.execute();
     }
-    /*
-     * private static void withCreateStatement(Connection con) throws
-     * SQLException { String createQ =
-     * "CREATE TABLE JdbcU(\"firstN\", \"lastN\") "; PreparedStatement 
statement
-     * = con.prepareStatement(createQ); statement.execute(); }
-     */
 }


Reply via email to