1 /*
   2  * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 package test.rowset.filteredrowset;
  24 
  25 import java.sql.SQLException;
  26 import javax.sql.RowSet;
  27 import javax.sql.rowset.FilteredRowSet;
  28 import javax.sql.rowset.Predicate;
  29 import static org.testng.Assert.assertEquals;
  30 import static org.testng.Assert.assertNull;
  31 import static org.testng.Assert.assertTrue;
  32 import org.testng.annotations.AfterMethod;
  33 import org.testng.annotations.BeforeMethod;
  34 import org.testng.annotations.Test;
  35 import test.rowset.webrowset.CommonWebRowSetTests;
  36 
  37 public class FilteredRowSetTests extends CommonWebRowSetTests {
  38 
  39     private FilteredRowSet frs;
  40 
  41     @BeforeMethod
  42     public void setUpMethod() throws Exception {
  43         frs = createCoffeeHousesRowSet();
  44     }
  45 
  46     @AfterMethod
  47     public void tearDownMethod() throws Exception {
  48         frs.close();
  49     }
  50 
  51     protected FilteredRowSet newInstance() throws SQLException {
  52         return rsf.createFilteredRowSet();
  53     }
  54 
  55     /*
  56      * Validate getFilter returns null if setFilter has not been called
  57      */
  58     @Test
  59     public void FilteredRowSetTest0000() throws SQLException {
  60         assertNull(frs.getFilter());
  61     }
  62 
  63     /*
  64      * Call setFilter to set a Predicate and validate that getFilter
  65      * returns the correct Predicate
  66      */
  67     @Test
  68     public void FilteredRowSetTest0001() throws SQLException {
  69         Predicate p = new PrimaryKeyFilter(0, 100030, 1);
  70         frs.setFilter(p);
  71         assertTrue(frs.getFilter().equals(p));
  72         frs.setFilter(null);
  73         assertNull(frs.getFilter());
  74     }
  75 
  76     /*
  77      * Validate that the correct rows are returned when a Predicate using
  78      * a column index is used
  79      */
  80     @Test
  81     public void FilteredRowSetTest0002() throws SQLException {
  82         Object[] expectedKeys = {
  83             10023, 10040, 10042, 10024, 10039, 10041, 10035, 10037, 10034
  84         };
  85         frs.setFilter(new PrimaryKeyFilter(10000, 10999, 1));
  86         assertEquals(getPrimaryKeys(frs), expectedKeys);
  87     }
  88 
  89     /*
  90      * Validate that the correct rows are returned when a Predicate using
  91      * a column Label is used
  92      */
  93     @Test
  94     public void FilteredRowSetTest0003() throws SQLException {
  95         Object[] expectedKeys = {
  96             10023, 10040, 10042, 10024, 10039, 10041, 10035, 10037, 10034
  97         };
  98         frs.setFilter(new PrimaryKeyFilter(10000, 10999, "STORE_ID"));
  99         assertEquals(getPrimaryKeys(frs), expectedKeys);
 100 
 101     }
 102 
 103     /*
 104      * Validate that the correct rows are returned when a Predicate using
 105      * a column index is used
 106      */
 107     @Test
 108     public void FilteredRowSetTest0004() throws SQLException {
 109         Object[] expectedKeys = {
 110             10040, 10042, 10041, 10035, 10037
 111         };
 112         String[] cityArray = {"SF", "LA"};
 113         frs.setFilter(new CityFilter(cityArray, 2));
 114         assertEquals(getPrimaryKeys(frs), expectedKeys);
 115     }
 116 
 117     /*
 118      * Validate that the correct rows are returned when a Predicate using
 119      * a column Label is used
 120      */
 121     @Test
 122     public void FilteredRowSetTest0005() throws SQLException {
 123         Object[] expectedKeys = {
 124             10040, 10042, 10041, 10035, 10037
 125         };
 126         String[] cityArray = {"SF", "LA"};
 127         frs.setFilter(new CityFilter(cityArray, "CITY"));
 128         assertEquals(getPrimaryKeys(frs), expectedKeys);
 129     }
 130 
 131 
 132     // Tests that are common but need to be disabled due to an implementation bug
 133 
 134 
 135     @Test(dataProvider = "rowSetType", enabled = false)
 136     public void commonCachedRowSetTest0043(RowSet rs) throws Exception {
 137         // Need to fix bug in FilteredRowSets
 138     }
 139 
 140 }