db-derby-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From d..@apache.org
Subject svn commit: r465935 - in /db/derby/code/trunk/java/testing/org/apache/derbyTesting: functionTests/tests/lang/PrepareExecuteDDL.java junit/JDBC.java
Date Thu, 19 Oct 2006 23:34:11 GMT
Author: djd
Date: Thu Oct 19 16:34:10 2006
New Revision: 465935

URL: http://svn.apache.org/viewvc?view=rev&rev=465935
Log:
DERBY-1976 Improvements in the utility JUnit JDBC methods.

Modified:
    db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/PrepareExecuteDDL.java
    db/derby/code/trunk/java/testing/org/apache/derbyTesting/junit/JDBC.java

Modified: db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/PrepareExecuteDDL.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/PrepareExecuteDDL.java?view=diff&rev=465935&r1=465934&r2=465935
==============================================================================
--- db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/PrepareExecuteDDL.java
(original)
+++ db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/PrepareExecuteDDL.java
Thu Oct 19 16:34:10 2006
@@ -156,7 +156,7 @@
 		if (isSelectStar)
 			;
 		
-		JDBC.assertDrainResults(rs, -1);
+		JDBC.assertDrainResults(rs);
 	}
 	
 	

Modified: db/derby/code/trunk/java/testing/org/apache/derbyTesting/junit/JDBC.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/testing/org/apache/derbyTesting/junit/JDBC.java?view=diff&rev=465935&r1=465934&r2=465935
==============================================================================
--- db/derby/code/trunk/java/testing/org/apache/derbyTesting/junit/JDBC.java (original)
+++ db/derby/code/trunk/java/testing/org/apache/derbyTesting/junit/JDBC.java Thu Oct 19 16:34:10
2006
@@ -316,13 +316,27 @@
 	 * Drain a single ResultSet by reading all of its
 	 * rows and columns. Each column is accessed using
 	 * getString() and asserted that the returned value
-	 * matches the state of ResultSet.wasNull().  If
-	 * the received row count is non-negative, this method
-	 * also asserts that the number of rows in the result
-	 * set matches the received row count.
-	 * Provides simple testing of the ResultSet when the contents
-	 * are not important.
-	 * @param rs
+	 * matches the state of ResultSet.wasNull().
+	 *
+	 * Provides simple testing of the ResultSet when the
+	 * contents are not important.
+	 *
+	 * @param rs Result set to drain.
+	 * @throws SQLException
+	 */
+	public static void assertDrainResults(ResultSet rs)
+	    throws SQLException
+	{
+		assertDrainResults(rs, -1);
+	}
+
+	/**
+	 * Does the work of assertDrainResults() as described
+	 * above.  If the received row count is non-negative,
+	 * this method also asserts that the number of rows
+	 * in the result set matches the received row count.
+	 *
+	 * @param rs Result set to drain.
 	 * @param expectedRows If non-negative, indicates how
 	 *  many rows we expected to see in the result set.
 	 * @throws SQLException
@@ -348,12 +362,36 @@
 	}
 	
     /**
+     * Takes a result set and an array of expected colum names (as
+     * Strings)  and asserts that the column names in the result
+     * set metadata match the number, order, and names of those
+     * in the array.
+     *
+     * @param rs ResultSet for which we're checking column names.
+     * @param expectedColNames Array of expected column names.
+     */
+    public static void assertColumnNames(ResultSet rs,
+        String [] expectedColNames) throws Exception
+    {
+        ResultSetMetaData rsmd = rs.getMetaData();
+        int actualCols = rsmd.getColumnCount();
+
+        Assert.assertEquals("Unexpected column count:",
+            rsmd.getColumnCount(), expectedColNames.length);
+
+        for (int i = 0; i < actualCols; i++)
+        {
+            Assert.assertEquals("Column names do not match:",
+                expectedColNames[i], rsmd.getColumnName(i+1));
+        }
+    }
+
+    /**
      * Takes a result set and a two-dimensional array and asserts
-     * that the two have equivalent rows and columns.  The first
-     * row in the 2-d array is expected to be the names of the
-     * columns and thus is compared to the metadata column names.
-     * Subsequent rows in the array are then compared with the
-     * corresponding rows in the result set.
+     * that the rows and columns in the result set match the number,
+     * order, and values of those in the array.  Each row in
+     * the array is compared with the corresponding row in the
+     * result set.
      *
      * Will throw an assertion failure if any of the following
      * is true:
@@ -362,38 +400,39 @@
      *  2. Expected vs actual number of rows doesn't match
      *  3. Any column in any row of the result set does not "equal"
      *     the corresponding column in the expected 2-d array.  If
-     *     "allAsStrings" is true then the result set value will be
-     *     retrieved as a String and compared, via the ".equals()"
-     *     method, to the corresponding object in the array (with
-     *     the assumption being that the objects in the array are all
+     *     "allAsTrimmedStrings" is true then the result set value
+     *     will be retrieved as a String and compared, via the ".equals()"
+     *     method, to the corresponding object in the array (with the
+     *     assumption being that the objects in the array are all 
      *     Strings).  Otherwise the result set value will be retrieved
      *     and compared as an Object, which is useful when asserting
      *     the JDBC types of the columns in addition to their values.
      *
+     * NOTE: It follows from #3 that the order of the rows in the
+     * in received result set must match the order of the rows in
+     * the received 2-d array.  Otherwise the result will be an
+     * assertion failure.
+     *
      * @param rs The actual result set.
      * @param expectedRows 2-Dimensional array of objects representing
      *  the expected result set.
-     * @param allAsStrings Whether or not to fetch (and compare) all
-     *  values from the actual result set as Strings; if false the
-     *  values will be fetched and compared as Objects.
+     * @param allAsTrimmedStrings Whether or not to fetch (and compare)
+     *  all values from the actual result set as trimmed Strings; if
+     *  false the values will be fetched and compared as Objects.  For
+     *  more on how this parameter is used, see assertRowInResultSet().
      */
     public static void assertFullResultSet(ResultSet rs,
-        Object [][] expectedRows, boolean allAsStrings)
+        Object [][] expectedRows, boolean allAsTrimmedStrings)
         throws SQLException
     {
         int rows;
-        boolean firstRow = true;
         ResultSetMetaData rsmd = rs.getMetaData();
 
         // Assert that we have the right number of columns.
         Assert.assertEquals("Unexpected column count:",
             expectedRows[0].length, rsmd.getColumnCount());
 
-        /* Assert each row of the result set.  First row of the result
-         * result set is the column names, so we have to make sure we
-         * don't call rs.next() in that case.
-         */
-        for (rows = 0; firstRow || rs.next(); rows++)
+        for (rows = 0; rs.next(); rows++)
         {
             /* If we have more actual rows than expected rows, don't
              * try to assert the row.  Instead just keep iterating
@@ -401,11 +440,9 @@
              */
             if (rows < expectedRows.length)
             {
-                assertRowInResultSet(rs, rsmd, rows,
-                    expectedRows[rows], allAsStrings);
+                assertRowInResultSet(rs, rows + 1,
+                    expectedRows[rows], allAsTrimmedStrings);
             }
-
-            firstRow = false;
         }
 
         // And finally, assert the row count.
@@ -415,78 +452,78 @@
     /**
      * Assert that every column in the current row of the received
      * result set matches the corresponding column in the received
-     * array.  If rowNum is zero then instead of using the current
-     * row of "rs", use the column names as retrieved from the result
-     * set metadata.
-     *
-     * @param rs Result set whose current row we'll check (if rowNum
-     *  is greater than zero).
-     * @param rsmd Metadata object for "rs".  Used for fetching column
-     *  names (if rowNum == 0).
+     * array.  This means that the order of the columns in the result
+     * set must match the order of the values in expectedRow.
+     *
+     * If the expected value for a given row/column is a SQL NULL,
+     * then the corresponding value in the array should be a Java
+     * null.
+     *
+     * @param rs Result set whose current row we'll check.
      * @param rowNum Row number (w.r.t expected rows) that we're
      *  checking.
      * @param expectedRow Array of objects representing the expected
-     *  values for the current row (if rowNum > 0) or else the expected
-     *  column names (if rowNum == 0).
-     * @param asStrings Whether or not to fetch and compare all values
-     *  from "rs" as Strings.
+     *  values for the current row.
+     * @param asTrimmedStrings Whether or not to fetch and compare
+     *  all values from "rs" as trimmed Strings.  If true then the
+     *  value from rs.getString() AND the expected value will both
+     *  be trimmed before comparison.  If such trimming is not
+     *  desired (ex. if we were testing the padding of CHAR columns)
+     *  then this param should be FALSE and the expected values in
+     *  the received array should include the expected whitespace.  
+     *  If for example the caller wants to check the padding of a
+     *  CHAR(8) column, asTrimmedStrings should be FALSE and the
+     *  expected row should contain the expected padding, such as
+     *  "FRED    ".
      */
-    private static void assertRowInResultSet(ResultSet rs,
-        ResultSetMetaData rsmd, int rowNum, Object [] expectedRow,
-        boolean asStrings) throws SQLException
+    private static void assertRowInResultSet(ResultSet rs, int rowNum,
+        Object [] expectedRow, boolean asTrimmedStrings) throws SQLException
     {
         String s;
         boolean ok;
         Object obj = null;
+        ResultSetMetaData rsmd = rs.getMetaData();
         for (int i = 0; i < expectedRow.length; i++)
         {
-            /* First row is column names.  We didn't call rs.next()
-             * in this case, so we have to be sure we don't call
-             * "wasNull" either.  Otherwise we'll see errors in client-
-             * server mode (though embedded doesn't seem to mind).
-             */
-            if (rowNum == 0)
+            if (asTrimmedStrings)
             {
-                obj = rsmd.getColumnName(i+1);
-                ok = ((expectedRow[i] != null) && obj.equals(expectedRow[i]));
-            }
-            else
-            {
-                if (asStrings)
+                // Trim the expected value, if non-null.
+                if (expectedRow[i] != null)
+                    expectedRow[i] = ((String)expectedRow[i]).trim();
+
+                /* Different clients can return different values for
+                 * boolean columns--namely, 0/1 vs false/true.  So in
+                 * order to keep things uniform, take boolean columns
+                 * and get the JDBC string version.  Note: since
+                 * Derby doesn't have a BOOLEAN type, we assume that
+                 * if the column's type is SMALLINT and the expected
+                 * value's string form is "true" or "false", then the
+                 * column is intended to be a mock boolean column.
+                 */
+                if ((expectedRow[i] != null)
+                    && (rsmd.getColumnType(i+1) == Types.SMALLINT))
                 {
-                    /* Different clients can return different values for
-                     * boolean columns--namely, 0/1 vs false/true.  So in
-                     * order to keep things uniform, take boolean columns
-                     * and get the JDBC string version.  Note: since
-                     * Derby doesn't have a BOOLEAN type, we assume that
-                     * if the column's type is SMALLINT and the expected
-                     * value's string form is "true" or "false", then the
-                     * column is intended to be a mock boolean column.
-                     */
-                    if ((expectedRow[i] != null)
-                        && (rsmd.getColumnType(i+1) == Types.SMALLINT))
-                    {
-                        s = expectedRow[i].toString();
-                        if (s.equals("true") || s.equals("false"))
-                            obj = (rs.getShort(i+1) == 0) ? "false" : "true";
-                    }
-                    else
-                    {
-                        obj = rs.getString(i+1);
-
-                        // Trim the string before comparing.
-                        if (obj != null)
-                            obj = ((String)obj).trim();
-                    }
+                    s = expectedRow[i].toString();
+                    if (s.equals("true") || s.equals("false"))
+                        obj = (rs.getShort(i+1) == 0) ? "false" : "true";
                 }
                 else
-                    obj = rs.getObject(i+1);
+                {
+                    obj = rs.getString(i+1);
+
+                    // Trim the rs string.
+                    if (obj != null)
+                        obj = ((String)obj).trim();
+                }
 
-                ok = (rs.wasNull() && (expectedRow[i] == null))
-                    || (!rs.wasNull()
-                        && (expectedRow[i] != null)
-                        && obj.equals(expectedRow[i]));
             }
+            else
+                obj = rs.getObject(i+1);
+
+            ok = (rs.wasNull() && (expectedRow[i] == null))
+                || (!rs.wasNull()
+                    && (expectedRow[i] != null)
+                    && obj.equals(expectedRow[i]));
 
             if (!ok)
             {



Mime
View raw message