# ignite-437: create well working assertRsEquals(ResultSet, List<List>).


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

Branch: refs/heads/sprint-3
Commit: e4433296d078d1f02cbf35c3b2dcd83ca0c4e1b7
Parents: dbfdf76
Author: Artem Shutak <ashu...@gridgain.com>
Authored: Mon Mar 16 17:39:30 2015 +0300
Committer: Artem Shutak <ashu...@gridgain.com>
Committed: Mon Mar 16 17:39:30 2015 +0300

----------------------------------------------------------------------
 modules/indexing/pom.xml                        |   6 -
 .../query/h2/sql/IgniteVsH2QueryTest.java       | 124 +++++++++++++------
 2 files changed, 85 insertions(+), 45 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/e4433296/modules/indexing/pom.xml
----------------------------------------------------------------------
diff --git a/modules/indexing/pom.xml b/modules/indexing/pom.xml
index e23c6c2..bd807f9 100644
--- a/modules/indexing/pom.xml
+++ b/modules/indexing/pom.xml
@@ -67,11 +67,5 @@
             <type>test-jar</type>
             <scope>test</scope>
         </dependency>
-        <dependency>
-            <groupId>commons-dbutils</groupId>
-            <artifactId>commons-dbutils</artifactId>
-            <version>1.6</version>
-            <scope>test</scope>
-        </dependency>
     </dependencies>
 </project>

http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/e4433296/modules/indexing/src/test/java/org/apache/ignite/internal/processors/query/h2/sql/IgniteVsH2QueryTest.java
----------------------------------------------------------------------
diff --git 
a/modules/indexing/src/test/java/org/apache/ignite/internal/processors/query/h2/sql/IgniteVsH2QueryTest.java
 
b/modules/indexing/src/test/java/org/apache/ignite/internal/processors/query/h2/sql/IgniteVsH2QueryTest.java
index 8fdeda6..373cd46 100644
--- 
a/modules/indexing/src/test/java/org/apache/ignite/internal/processors/query/h2/sql/IgniteVsH2QueryTest.java
+++ 
b/modules/indexing/src/test/java/org/apache/ignite/internal/processors/query/h2/sql/IgniteVsH2QueryTest.java
@@ -17,8 +17,6 @@
 
 package org.apache.ignite.internal.processors.query.h2.sql;
 
-import org.apache.commons.dbutils.*;
-import org.apache.commons.dbutils.handlers.*;
 import org.apache.ignite.*;
 import org.apache.ignite.cache.*;
 import org.apache.ignite.cache.affinity.*;
@@ -292,41 +290,104 @@ public class IgniteVsH2QueryTest extends 
GridCommonAbstractTest {
     @SuppressWarnings("unchecked")
     private void test0(IgniteCache cache, String sql, Object... args) throws 
SQLException {
         log.info("Sql=" + sql + ", args=" + Arrays.toString(args));
+        
+        ResultSet h2Rs = null;
+        PreparedStatement st = null;
 
-        List<List<?>> h2Res = executeQueryOnH2(sql, args);
-
-        List<List<?>> cacheRes = cache.queryFields(new 
SqlFieldsQuery(sql).setArgs(args)).getAll();
-
-        print("H2 query result.", h2Res);
-        print("Ignite Cache query result.", cacheRes);
-
-        assertRsEquals(h2Res, cacheRes);
-    }
+        try {
+            st = conn.prepareStatement(sql);
+            
+            //TODO apply args ot stat.
 
-    private List<List<?>> executeQueryOnH2(String sql, Object[] args) throws 
SQLException {
-        ResultSet rs = null;
+            h2Rs = st.executeQuery();
 
-        try {
-            try(PreparedStatement st = conn.prepareStatement(sql)) {
-                //TODO apply args.
+            List<List<?>> cacheRes = cache.queryFields(new 
SqlFieldsQuery(sql).setArgs(args)).getAll();
 
-                rs = st.executeQuery();
+//            print("H2 query result.", h2Res);
+            print("Ignite Cache query result.", cacheRes);
 
-                return new RsHandler().handle(rs);
-            }
+            assertRsEquals(h2Rs, cacheRes);
         }
         finally {
-            U.closeQuiet(rs);
+            U.closeQuiet(st);
+            U.closeQuiet(h2Rs);
         }
     }
 
     private void print(String msg, List<List<?>> rs) {
         log.info(msg);
-        
+
         for (List<?> objects : rs)
             log.info(objects.toString());
     }
 
+    private void assertRsEquals(ResultSet rs, List<List<?>> actualRs) throws 
SQLException {
+        int rsRowsCnt = 0;
+
+        while (rs.next()) {
+            boolean currRowFound = false;
+
+            for (List<?> row : actualRs) {
+                if (rowEqualsCurrentRsRow(rs, row)) {
+                    currRowFound = true;
+
+                    break;
+                }
+            }
+
+            assertTrue("A row with number " + rsRowsCnt + " from " + rs + " 
not found at " + actualRs + '.',
+                currRowFound);
+
+            rsRowsCnt++;
+        }
+
+        assertEquals("Count of results.", rsRowsCnt, actualRs.size());
+    }
+
+    private boolean rowEqualsCurrentRsRow(ResultSet rs, List<?> row) throws 
SQLException {
+        for (int columnNum = 0; columnNum < row.size(); columnNum++) {
+            Object o1 = row.get(columnNum);
+
+            Object o2 = extractColumn(rs, columnNum + 1, o1.getClass());
+
+            if (!o1.equals(o2))
+                return false;
+        }
+
+        return true;
+    }
+
+    protected Object extractColumn(ResultSet rs, int idx, Class<?> propType)
+        throws SQLException {
+
+        if (!propType.isPrimitive() && rs.getObject(idx) == null)
+            return null;
+
+        if (propType.equals(String.class))
+            return rs.getString(idx);
+        else if (propType.equals(Integer.TYPE) || 
propType.equals(Integer.class))
+            return rs.getInt(idx);
+        else if (propType.equals(Boolean.TYPE) || 
propType.equals(Boolean.class))
+            return rs.getBoolean(idx);
+        else if (propType.equals(Long.TYPE) || propType.equals(Long.class))
+            return rs.getLong(idx);
+        else if (propType.equals(Double.TYPE) || propType.equals(Double.class))
+            return rs.getDouble(idx);
+        else if (propType.equals(Float.TYPE) || propType.equals(Float.class))
+            return rs.getFloat(idx);
+        else if (propType.equals(Short.TYPE) || propType.equals(Short.class))
+            return rs.getShort(idx);
+        else if (propType.equals(Byte.TYPE) || propType.equals(Byte.class))
+            return rs.getByte(idx);
+        else if (propType.equals(Timestamp.class))
+            return rs.getTimestamp(idx);
+        else if (propType.equals(SQLXML.class))
+            return rs.getSQLXML(idx);
+        else // Object.
+            return rs.getObject(idx);
+
+    }
+
     private void assertRsEquals(List<List<?>> expRs, List<List<?>> actualRs) {
         assertEquals("Count of results.", expRs.size(), actualRs.size());
 
@@ -518,7 +579,7 @@ public class IgniteVsH2QueryTest extends 
GridCommonAbstractTest {
     }
 
     /**
-     * Product class. 
+     * Product class.
      */
     private static class Product implements Serializable {
         /** Primary key. */
@@ -528,14 +589,14 @@ public class IgniteVsH2QueryTest extends 
GridCommonAbstractTest {
         /** Product name. */
         @QuerySqlField
         private String name;
-        
+
         /** Product price */
         @QuerySqlField
         private int price;
 
         /**
          * Create Product.
-         *  
+         *
          * @param id Product ID.
          * @param name Product name.
          * @param price Product price.
@@ -597,19 +658,4 @@ public class IgniteVsH2QueryTest extends 
GridCommonAbstractTest {
             return "Purchase [id=" + id + ", productId=" + productId + ", 
personId=" + personId + ']';
         }
     }
-    
-    private static class RsHandler implements ResultSetHandler<List<List<?>>> {
-        private final ArrayListHandler handler = new ArrayListHandler();
-
-        @Override public List<List<?>> handle(ResultSet rs) throws 
SQLException {
-            List<Object[]> listOfArrays = handler.handle(rs);
-
-            List<List<?>> res = new ArrayList<>(listOfArrays.size());
-
-            for (Object[] arr : listOfArrays)
-                res.add(Arrays.asList(arr));
-            
-            return res; 
-        }
-    }
 }

Reply via email to