Home | History | Annotate | Download | only in sql
      1 /*
      2  * Copyright (C) 2007 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *     http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package tests.java.sql;
     18 
     19 import dalvik.annotation.KnownFailure;
     20 import dalvik.annotation.TestTargetClass;
     21 import dalvik.annotation.TestTargets;
     22 import dalvik.annotation.TestLevel;
     23 import dalvik.annotation.TestTargetNew;
     24 
     25 import java.io.CharArrayReader;
     26 import java.math.BigDecimal;
     27 import java.sql.Connection;
     28 import java.sql.Date;
     29 import java.sql.PreparedStatement;
     30 import java.sql.ResultSet;
     31 import java.sql.SQLException;
     32 import java.sql.Statement;
     33 import java.sql.Time;
     34 import java.util.ArrayList;
     35 import java.util.Calendar;
     36 import java.util.HashMap;
     37 import java.util.HashSet;
     38 
     39 import tests.support.DatabaseCreator;
     40 import tests.support.Support_SQL;
     41 import junit.extensions.TestSetup;
     42 import junit.framework.Test;
     43 import junit.framework.TestCase;
     44 import junit.framework.TestSuite;
     45 
     46 @TestTargetClass(Statement.class)
     47 public class SelectFunctionalityTest extends TestCase {
     48 
     49     private static Connection conn;
     50 
     51     private static Statement statement;
     52 
     53     private static Date date;
     54 
     55     private static Time time;
     56 
     57     public void setUp() throws Exception {
     58         super.setUp();
     59         Support_SQL.loadDriver();
     60         try {
     61             conn = Support_SQL.getConnection();
     62             statement = conn.createStatement();
     63             createTestTables();
     64         } catch (SQLException e) {
     65             fail("Unexpected SQLException " + e.toString());
     66         }
     67     }
     68 
     69     public void tearDown() throws Exception {
     70         deleteTestTables();
     71         conn.close();
     72         statement.close();
     73         super.tearDown();
     74     }
     75 
     76     private void createTestTables() {
     77         try {
     78             ResultSet userTab = conn.getMetaData().getTables(null,
     79                     null, null, null);
     80 
     81             while (userTab.next()) {
     82                 String tableName = userTab.getString("TABLE_NAME");
     83                 if (tableName.equals(DatabaseCreator.TEST_TABLE2)) {
     84                     statement.execute(DatabaseCreator.DROP_TABLE2);
     85                 } else if (tableName
     86                         .equals(DatabaseCreator.SALESPEOPLE_TABLE)) {
     87                     statement
     88                             .execute(DatabaseCreator.DROP_TABLE_SALESPEOPLE);
     89                 } else if (tableName
     90                         .equals(DatabaseCreator.CUSTOMERS_TABLE)) {
     91                     statement
     92                             .execute(DatabaseCreator.DROP_TABLE_CUSTOMERS);
     93                 } else if (tableName
     94                         .equals(DatabaseCreator.ORDERS_TABLE)) {
     95                     statement
     96                             .execute(DatabaseCreator.DROP_TABLE_ORDERS);
     97                 }
     98             }
     99             userTab.close();
    100 
    101             statement.execute(DatabaseCreator.CREATE_TABLE2);
    102             statement.execute(DatabaseCreator.CREATE_TABLE_SALESPEOPLE);
    103             statement.execute(DatabaseCreator.CREATE_TABLE_CUSTOMERS);
    104             statement.execute(DatabaseCreator.CREATE_TABLE_ORDERS);
    105 
    106             long currentTime = Calendar.getInstance().getTimeInMillis();
    107             date = new Date(currentTime);
    108             time = new Time(currentTime);
    109 
    110             DatabaseCreator.fillTestTable2(conn, 1, 5, currentTime);
    111             DatabaseCreator.fillCustomersTable(conn);
    112             DatabaseCreator.fillOrdersTable(conn);
    113             DatabaseCreator.fillSalesPeopleTable(conn);
    114 
    115         } catch (SQLException e) {
    116             fail("Unexpected SQLException " + e.toString());
    117         }
    118     }
    119 
    120     private void deleteTestTables() {
    121         try {
    122             statement.execute(DatabaseCreator.DROP_TABLE2);
    123             statement.execute(DatabaseCreator.DROP_TABLE_SALESPEOPLE);
    124             statement.execute(DatabaseCreator.DROP_TABLE_CUSTOMERS);
    125             statement.execute(DatabaseCreator.DROP_TABLE_ORDERS);
    126         } catch (SQLException e) {
    127             fail("Unexpected SQLException " + e.toString());
    128         }
    129     }
    130 
    131     /**
    132      * @tests SelectFunctionalityTest#test_SelectSimple(). Selects all records
    133      *        from the table
    134      */
    135     @TestTargetNew(
    136         level = TestLevel.PARTIAL_COMPLETE,
    137         notes = "Functionality test: Selects all records from the table",
    138         method = "executeQuery",
    139         args = {java.lang.String.class}
    140     )
    141     public void test_SelectSimple() throws SQLException {
    142         String sql = "SELECT * FROM " + DatabaseCreator.TEST_TABLE2;
    143         ResultSet result = statement.executeQuery(sql);
    144         int counter = 0;
    145 
    146         while (result.next()) {
    147             int id = result.getInt("finteger");
    148             assertEquals("expected value doesn't equal actual",
    149                     DatabaseCreator.defaultString + id, result
    150                             .getString("ftext"));
    151             assertEquals("expected value doesn't equal actual",
    152                     DatabaseCreator.defaultCharacter + id, result
    153                             .getString("fcharacter"));
    154 
    155             // TODO getBigDecimal is not supported
    156 //            assertEquals("expected value doesn't equal actual", BigDecimal
    157 //                    .valueOf(id + 0.1), result.getBigDecimal("fdecimal"));
    158 //            assertEquals("expected value doesn't equal actual", BigDecimal
    159 //                    .valueOf(id + 0.1), result.getBigDecimal("fnumeric"));
    160 //            assertEquals("expected value doesn't equal actual", id, result
    161 //                    .getInt("fsmallint"));
    162             assertEquals("expected value doesn't equal actual", BigDecimal
    163                     .valueOf(id + 0.1).floatValue(), result.getFloat("ffloat"));
    164             assertEquals("expected value doesn't equal actual", BigDecimal
    165                     .valueOf(id + 0.1).doubleValue(), result.getDouble("freal"));
    166             assertEquals("expected value doesn't equal actual", BigDecimal
    167                     .valueOf(id + 0.1).doubleValue(), result
    168                     .getDouble("fdouble"));
    169             assertEquals("expected value doesn't equal actual",
    170                     date.toString(), result.getDate("fdate").toString());
    171             assertEquals("expected value doesn't equal actual",
    172                     time.toString(), result.getTime("ftime").toString());
    173             counter++;
    174         }
    175 
    176         assertEquals("number of rows in ResultSet is wrong", 5, counter);
    177         result.close();
    178     }
    179 
    180     /**
    181      * @tests SelectFunctionalityTest#test_SelectPrepared(). Selects all records
    182      *        from the table using parametric query
    183      */
    184     @TestTargetNew(
    185         level = TestLevel.PARTIAL_COMPLETE,
    186         notes = "Functionality test: Selects all records from the table using parametric query",
    187         method = "executeQuery",
    188         args = {java.lang.String.class}
    189     )
    190     public void test_SelectPrepared() throws SQLException {
    191         String sql = "SELECT finteger, ftext, fcharacter, fdecimal, fnumeric,"
    192                 + " fsmallint, ffloat, freal, fdouble, fdate, ftime" + " FROM "
    193                 + DatabaseCreator.TEST_TABLE2
    194                 + " WHERE finteger = ? AND ftext = ? AND fcharacter = ? AND"
    195                 + " fdecimal = ? AND fnumeric = ? AND fsmallint = ? AND"
    196                 + " freal = ? AND fdouble = ? AND fdate = ?" + " AND ftime = ?";
    197         PreparedStatement prepStatement = conn.prepareStatement(sql);
    198 
    199         CharArrayReader reader = new CharArrayReader(new String(
    200                 DatabaseCreator.defaultCharacter + "1").toCharArray());
    201         prepStatement.setInt(1, 1);
    202         prepStatement.setString(2, DatabaseCreator.defaultString + "1");
    203 //      TODO setCharacterStream and setBigDecimal are not supported
    204 //        prepStatement.setCharacterStream(3, reader, 4);
    205 //        prepStatement.setBigDecimal(4, BigDecimal.valueOf(1.1));
    206 //        prepStatement.setBigDecimal(5, BigDecimal.valueOf(1.1));
    207         prepStatement.setInt(6, 1);
    208         prepStatement.setDouble(7, 1.1);
    209         prepStatement.setDouble(8, 1.1);
    210         prepStatement.setDate(9, date);
    211         prepStatement.setTime(10, time);
    212 
    213         int counter = 0;
    214         ResultSet result = prepStatement.executeQuery();
    215         while (result.next()) {
    216             int id = result.getInt("finteger");
    217             assertEquals("expected value doesn't equal actual",
    218                     DatabaseCreator.defaultString + id, result
    219                             .getString("ftext"));
    220             assertEquals("expected value doesn't equal actual",
    221                     DatabaseCreator.defaultCharacter + id, result
    222                             .getString("fcharacter"));
    223 //            TODO getBigDecimal is not supported
    224 //            assertEquals("expected value doesn't equal actual", BigDecimal
    225 //                    .valueOf(1.1), result.getBigDecimal("fdecimal"));
    226 //            assertEquals("expected value doesn't equal actual", BigDecimal
    227 //                    .valueOf(1.1), result.getBigDecimal("fnumeric"));
    228             assertEquals("expected value doesn't equal actual", id, result
    229                     .getInt("fsmallint"));
    230             assertEquals("expected value doesn't equal actual",
    231                     (float) (id + 0.1), result.getFloat("ffloat"));
    232             assertEquals("expected value doesn't equal actual",
    233                     (double) (id + 0.1), result.getDouble("freal"));
    234             assertEquals("expected value doesn't equal actual",
    235                     (double) (id + 0.1), result.getDouble("fdouble"));
    236             assertEquals("expected value doesn't equal actual",
    237                     date.toString(), result.getDate("fdate").toString());
    238             assertEquals("expected value doesn't equal actual",
    239                     time.toString(), result.getTime("ftime").toString());
    240             counter++;
    241         }
    242 //       TODO query wasn't executed due to "not supported" methods
    243 //        assertEquals("number of rows in ResultSet is wrong", 1, counter);
    244         prepStatement.close();
    245         result.close();
    246     }
    247 
    248     /**
    249      * @tests SelectFunctionalityTest#test_SubSelect(). Selects records from the
    250      *        table using subselect
    251      */
    252     @TestTargetNew(
    253         level = TestLevel.PARTIAL_COMPLETE,
    254         notes = "Functionality test: Selects records from the table using subselect",
    255         method = "executeQuery",
    256         args = {java.lang.String.class}
    257     )
    258     public void test_SubSelect() throws SQLException {
    259         String sql = "SELECT finteger," + " (SELECT ftext FROM "
    260                 + DatabaseCreator.TEST_TABLE2 + " WHERE finteger = 1) as ftext"
    261                 + " FROM " + DatabaseCreator.TEST_TABLE2;
    262         ResultSet result = statement.executeQuery(sql);
    263 
    264         HashMap<Integer, String> value = new HashMap<Integer, String>();
    265         value.put(1, DatabaseCreator.defaultString + "1");
    266         value.put(2, DatabaseCreator.defaultString + "1");
    267         value.put(3, DatabaseCreator.defaultString + "1");
    268         value.put(4, DatabaseCreator.defaultString + "1");
    269         value.put(5, DatabaseCreator.defaultString + "1");
    270 
    271         while (result.next()) {
    272             int key = result.getInt("finteger");
    273             String val = result.getString("ftext");
    274             assertTrue("wrong value of finteger field", value.containsKey(key));
    275             assertEquals("wrong value of ftext field", value.get(key), val);
    276             value.remove(key);
    277         }
    278         assertTrue("expected rows number doesn't equal actual rows number",
    279                 value.isEmpty());
    280         result.close();
    281     }
    282 
    283     /**
    284      * @tests SelectFunctionalityTest#test_SelectThreeTables(). Selects records
    285      *        from a few tables
    286      */
    287     @TestTargetNew(
    288         level = TestLevel.PARTIAL_COMPLETE,
    289         notes = "Functionality test: Selects records from a few tables",
    290         method = "executeQuery",
    291         args = {java.lang.String.class}
    292     )
    293     public void test_SelectThreeTables() throws SQLException {
    294         String sql = "SELECT onum, " + DatabaseCreator.ORDERS_TABLE + ".cnum"
    295                 + " FROM " + DatabaseCreator.SALESPEOPLE_TABLE + ", "
    296                 + DatabaseCreator.CUSTOMERS_TABLE + ", "
    297                 + DatabaseCreator.ORDERS_TABLE + " WHERE "
    298                 + DatabaseCreator.CUSTOMERS_TABLE + ".city <> "
    299                 + DatabaseCreator.SALESPEOPLE_TABLE + ".city" + " AND "
    300                 + DatabaseCreator.ORDERS_TABLE + ".cnum = "
    301                 + DatabaseCreator.CUSTOMERS_TABLE + ".cnum" + " AND "
    302                 + DatabaseCreator.ORDERS_TABLE + ".snum = "
    303                 + DatabaseCreator.SALESPEOPLE_TABLE + ".snum";
    304         ResultSet result = statement.executeQuery(sql);
    305 
    306         HashMap<Integer, Integer> value = new HashMap<Integer, Integer>();
    307         value.put(3001, 2008);
    308         value.put(3002, 2007);
    309         value.put(3006, 2008);
    310         value.put(3009, 2002);
    311         value.put(3007, 2004);
    312         value.put(3010, 2004);
    313 
    314         while (result.next()) {
    315             int key = result.getInt("onum");
    316             int val = result.getInt("cnum");
    317             assertTrue("wrong value of onum field", value.containsKey(key));
    318             assertEquals("wrong value of cnum field", value.get(key),
    319                     (Integer) val);
    320             value.remove(key);
    321         }
    322         assertTrue("expected rows number doesn't equal actual rows number",
    323                 value.isEmpty());
    324         result.close();
    325     }
    326 
    327     /**
    328      * @tests SelectFunctionalityTest#test_SelectThreeTables(). Selects records
    329      *        from a table using union
    330      */
    331     @TestTargetNew(
    332         level = TestLevel.PARTIAL_COMPLETE,
    333         notes = "Functionality test: Selects records from a table using union",
    334         method = "executeQuery",
    335         args = {java.lang.String.class}
    336     )
    337     public void test_SelectUnionItself() throws SQLException {
    338         String sql = "SELECT b.cnum, b.cname" + " FROM "
    339                 + DatabaseCreator.CUSTOMERS_TABLE + " a, "
    340                 + DatabaseCreator.CUSTOMERS_TABLE + " b"
    341                 + " WHERE a.snum = 1002" + " AND b.city = a.city";
    342         ResultSet result = statement.executeQuery(sql);
    343 
    344         HashMap<Integer, String> value = new HashMap<Integer, String>();
    345         value.put(2003, "Liu");
    346         value.put(2004, "Grass");
    347         value.put(2008, "Cisneros");
    348 
    349         while (result.next()) {
    350             int key = result.getInt("cnum");
    351             String val = result.getString("cname");
    352             assertTrue("wrong value of cnum field", value.containsKey(key));
    353             assertEquals("wrong value of cname field", value.get(key), val);
    354             value.remove(key);
    355         }
    356         assertTrue("expected rows number doesn't equal actual rows number",
    357                 value.isEmpty());
    358         result.close();
    359     }
    360 
    361     /**
    362      * @tests SelectFunctionalityTest#test_SelectLeftOuterJoin(). Selects
    363      *        records from a table using left join
    364      */
    365     @TestTargetNew(
    366         level = TestLevel.PARTIAL_COMPLETE,
    367         notes = "Functionality test: Selects records from a table using left join",
    368         method = "executeQuery",
    369         args = {java.lang.String.class}
    370     )
    371     public void test_SelectLeftOuterJoin() throws SQLException {
    372         String sql = "SELECT distinct s.snum as ssnum, c.snum as ccnum FROM "
    373                 + DatabaseCreator.CUSTOMERS_TABLE + " c left outer join "
    374                 + DatabaseCreator.SALESPEOPLE_TABLE + " s on s.snum=c.snum";
    375         ResultSet result = statement.executeQuery(sql);
    376 
    377         HashMap<Integer, Integer> value = new HashMap<Integer, Integer>();
    378         value.put(1001, 1001);
    379         value.put(1002, 1002);
    380         value.put(1003, 1003);
    381         value.put(1004, 1004);
    382         value.put(1007, 1007);
    383 
    384         while (result.next()) {
    385             int key = result.getInt("ssnum");
    386             Object val = result.getObject("ccnum");
    387             assertTrue("wrong value of ssnum field", value.containsKey(key));
    388             assertEquals("wrong value of ccnum field", value.get(key),
    389                     (Integer) val);
    390             value.remove(key);
    391         }
    392         assertTrue("expected rows number doesn't equal actual rows number",
    393                 value.isEmpty());
    394         result.close();
    395     }
    396 
    397     /**
    398      * @tests SelectFunctionalityTest#test_SelectRightOuterJoin(). Selects
    399      *        records from a table using right join
    400      *
    401      * TODO RIGHT and FULL OUTER JOINs are not supported
    402      */
    403     @TestTargetNew(
    404             level = TestLevel.PARTIAL_COMPLETE,
    405             notes = "tests right outer joins. RIGHT and FULL OUTER JOINs are not supported",
    406             method = "executeQuery",
    407             args = {java.lang.String.class}
    408         )
    409     @KnownFailure("not supported")
    410     public void test_SelectRightOuterJoin() throws SQLException {
    411         String sql = "SELECT distinct s.snum as ssnum, c.snum as ccnum FROM "
    412                 + DatabaseCreator.CUSTOMERS_TABLE + " c right outer join "
    413                 + DatabaseCreator.SALESPEOPLE_TABLE + " s on s.snum=c.snum";
    414         ResultSet result = statement.executeQuery(sql);
    415 
    416         HashMap<Integer, Integer> value = new HashMap<Integer, Integer>();
    417         value.put(1001, 1001);
    418         value.put(1002, 1002);
    419         value.put(1003, 1003);
    420         value.put(1004, 1004);
    421         value.put(1007, 1007);
    422         value.put(1013, null);
    423 
    424         while (result.next()) {
    425             int key = result.getInt("ssnum");
    426             Object val = result.getObject("ccnum");
    427             assertTrue("wrong value of ssnum field", value.containsKey(key));
    428             assertEquals("wrong value of ccnum field", value.get(key),
    429                     (Integer) val);
    430             value.remove(key);
    431         }
    432         assertTrue("expected rows number doesn't equal actual rows number",
    433                 value.isEmpty());
    434         result.close();
    435     }
    436 
    437     /**
    438      * @tests SelectFunctionalityTest#test_SelectGroupBy(). Selects records from
    439      *        a table using group by
    440      */
    441     @TestTargetNew(
    442         level = TestLevel.PARTIAL_COMPLETE,
    443         notes = "Functionality test: Selects records from a table using group by",
    444         method = "executeQuery",
    445         args = {java.lang.String.class}
    446     )
    447     public void test_SelectGroupBy() throws SQLException {
    448         String selectQuery = "SELECT rating, SUM(snum) AS sum FROM "
    449                 + DatabaseCreator.CUSTOMERS_TABLE + " GROUP BY rating";
    450         ResultSet result = statement.executeQuery(selectQuery);
    451 
    452         HashMap<Integer, Integer> values = new HashMap<Integer, Integer>();
    453         values.put(100, 3006);
    454         values.put(200, 2005);
    455         values.put(300, 2009);
    456 
    457         while (result.next()) {
    458             int rating = result.getInt("rating");
    459             int sum = result.getInt("sum");
    460             assertTrue("Wrong value of rating field", values
    461                     .containsKey(rating));
    462             assertEquals("Wrong value of sum field", values.get(rating),
    463                     new Integer(sum));
    464             assertEquals(new Integer(sum), values.remove(rating));
    465         }
    466         result.close();
    467         assertTrue("Result set has wrong size", values.isEmpty());
    468     }
    469 
    470     /**
    471      * @tests SelectFunctionalityTest#test_SelectOrderBy(). Selects records from
    472      *        a table using order by
    473      */
    474     @TestTargetNew(
    475         level = TestLevel.PARTIAL_COMPLETE,
    476         notes = "Functionality test: Selects records from a table using order by",
    477         method = "executeQuery",
    478         args = {java.lang.String.class}
    479     )
    480     public void test_SelectOrderBy() throws SQLException {
    481         String selectQuery = "SELECT onum FROM " + DatabaseCreator.ORDERS_TABLE
    482                 + " ORDER BY onum";
    483         ResultSet result = statement.executeQuery(selectQuery);
    484 
    485         ArrayList<Integer> values = new ArrayList<Integer>();
    486         values.add(Integer.valueOf(3001));
    487         values.add(Integer.valueOf(3002));
    488         values.add(Integer.valueOf(3003));
    489         values.add(Integer.valueOf(3005));
    490         values.add(Integer.valueOf(3006));
    491         values.add(Integer.valueOf(3007));
    492         values.add(Integer.valueOf(3008));
    493         values.add(Integer.valueOf(3009));
    494         values.add(Integer.valueOf(3010));
    495         values.add(Integer.valueOf(3011));
    496 
    497         int index = 0;
    498         while (result.next()) {
    499             Integer onum = result.getInt("onum");
    500             assertTrue("result set doesn't contain value", values
    501                     .contains(onum));
    502             assertEquals("result set is not sorted", index, values
    503                     .indexOf(onum));
    504             index++;
    505         }
    506         result.close();
    507     }
    508 
    509     /**
    510      * @tests SelectFunctionalityTest#test_SelectDistinct(). Selects records
    511      *        from a table using distinct
    512      */
    513     @TestTargetNew(
    514         level = TestLevel.PARTIAL_COMPLETE,
    515         notes = "Functionality test: Selects records from a table using distinct",
    516         method = "executeQuery",
    517         args = {java.lang.String.class}
    518     )
    519     public void test_SelectDistinct() throws SQLException {
    520         String selectQuery = "SELECT DISTINCT rating FROM "
    521                 + DatabaseCreator.CUSTOMERS_TABLE;
    522         ResultSet result = statement.executeQuery(selectQuery);
    523 
    524         HashSet<Integer> values = new HashSet<Integer>();
    525         values.add(Integer.valueOf(100));
    526         values.add(Integer.valueOf(200));
    527         values.add(Integer.valueOf(300));
    528 
    529         while (result.next()) {
    530             Integer rating = result.getInt("rating");
    531             assertTrue("result set doesn't contain value", values
    532                     .contains(rating));
    533             assertTrue("wrong value in the result set", values.remove(rating));
    534         }
    535         result.close();
    536         assertTrue("Result set has wrong size", values.isEmpty());
    537     }
    538 
    539     /**
    540      * @tests SelectFunctionalityTest#test_SelectAgregateFunctions(). Selects
    541      *        records from a table using agregate functions
    542      */
    543     @TestTargetNew(
    544         level = TestLevel.PARTIAL_COMPLETE,
    545         notes = "Functionality test: Selects records from a table using agregate functions",
    546         method = "executeQuery",
    547         args = {java.lang.String.class}
    548     )
    549     public void test_SelectAgregateFunctions() throws SQLException {
    550         String selectCount = "SELECT count(onum) as count FROM "
    551                 + DatabaseCreator.ORDERS_TABLE;
    552         String selectSum = "SELECT sum(onum) as sum FROM "
    553                 + DatabaseCreator.ORDERS_TABLE;
    554         String selectAvg = "SELECT avg(onum) as avg FROM "
    555                 + DatabaseCreator.ORDERS_TABLE;
    556         String selectMax = "SELECT max(onum) as max FROM "
    557                 + DatabaseCreator.ORDERS_TABLE;
    558         String selectMin = "SELECT min(onum) as min FROM "
    559                 + DatabaseCreator.ORDERS_TABLE;
    560 
    561         func("count", selectCount, 10);
    562         func("sum", selectSum, 30062);
    563         func("avg", selectAvg, 3006);
    564         func("max", selectMax, 3011);
    565         func("min", selectMin, 3001);
    566     }
    567 
    568     private void func(String name, String query, int expected) {
    569         int res = 0;
    570         double resDouble = 0.0;
    571         try {
    572             ResultSet result = statement.executeQuery(query);
    573             while (result.next()) {
    574                 res = result.getInt(name);
    575                 if (res != 0 ) {
    576                 assertEquals(expected,res);
    577                 break;
    578                 }
    579                 // for Double: getInt not supported yet
    580                 resDouble  = Double.parseDouble(result.getString(name));
    581                 res = (int) Math.rint(resDouble);
    582                 assertEquals(expected,res);
    583 
    584             }
    585             assertFalse("wrong size of result set", result.next());
    586             result.close();
    587         } catch (SQLException e) {
    588             fail(e.getMessage());
    589         }
    590     }
    591 
    592     /**
    593      * @tests SelectFunctionalityTest#test_SelectHaving(). Selects records from
    594      *        a table using having
    595      */
    596     @TestTargetNew(
    597         level = TestLevel.PARTIAL_COMPLETE,
    598         notes = "Functionality test: Selects records from a table using having",
    599         method = "executeQuery",
    600         args = {java.lang.String.class}
    601     )
    602     public void test_SelectHaving() throws SQLException {
    603         String selectQuery = "SELECT snum, max(amt) AS max FROM "
    604                 + DatabaseCreator.ORDERS_TABLE
    605                 + " GROUP BY snum HAVING max(amt) > 3000";
    606         ResultSet result = statement.executeQuery(selectQuery);
    607 
    608         HashSet<Double> values = new HashSet<Double>();
    609         values.add(Double.valueOf(9891.88));
    610         values.add(Double.valueOf(5160.45));
    611 
    612         while (result.next()) {
    613             Double max = result.getDouble("max");
    614             assertTrue("result set doesn't contain value", values.contains(max));
    615             assertTrue("wrong value in the result set", values.remove(max));
    616         }
    617         result.close();
    618         assertTrue("Result set has wrong size", values.isEmpty());
    619     }
    620 }
    621