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.webrowset;
  24 
  25 import java.io.ByteArrayInputStream;
  26 import java.io.ByteArrayOutputStream;
  27 import java.io.File;
  28 import java.io.FileInputStream;
  29 import java.io.FileReader;
  30 import java.io.InputStreamReader;
  31 import java.io.ObjectInputStream;
  32 import java.io.ObjectOutputStream;
  33 import java.io.OutputStreamWriter;
  34 import java.math.BigDecimal;
  35 import java.sql.ResultSet;
  36 import java.util.Arrays;
  37 import javax.sql.rowset.WebRowSet;
  38 import static org.testng.Assert.assertEquals;
  39 import static org.testng.Assert.assertEqualsNoOrder;
  40 import static org.testng.Assert.assertFalse;
  41 import static org.testng.Assert.assertTrue;
  42 import org.testng.annotations.Test;
  43 import test.rowset.cachedrowset.CommonCachedRowSetTests;
  44 
  45 public abstract class CommonWebRowSetTests extends CommonCachedRowSetTests {
  46 
  47     protected final String XMLFILEPATH = System.getProperty("test.src", ".")
  48             + File.separatorChar + "xml" + File.separatorChar;
  49     protected final String COFFEE_ROWS_XML = XMLFILEPATH + "COFFEE_ROWS.xml";
  50     protected final String DELETED_COFFEE_ROWS_XML
  51             = XMLFILEPATH + "DELETED_COFFEE_ROWS.xml";
  52     protected final String MODFIED_DELETED_COFFEE_ROWS_XML
  53             = XMLFILEPATH + "MODFIED_DELETED_COFFEE_ROWS.xml";
  54     protected final String UPDATED_COFFEE_ROWS_XML
  55             = XMLFILEPATH + "UPDATED_COFFEE_ROWS.xml";
  56     protected final String INSERTED_COFFEE_ROWS_XML
  57             = XMLFILEPATH + "INSERTED_COFFEE_ROWS.xml";
  58     protected final String UPDATED_INSERTED_COFFEE_ROWS_XML
  59             = XMLFILEPATH + "UPDATED_INSERTED_COFFEE_ROWS.xml";
  60 
  61 
  62     /*
  63      * Utility method to write a WebRowSet XML file via an OutputStream
  64      */
  65     protected ByteArrayOutputStream writeWebRowSetWithOutputStream(WebRowSet rs) throws Exception {
  66         ByteArrayOutputStream baos = new ByteArrayOutputStream();
  67         try (ObjectOutputStream oos = new ObjectOutputStream(baos)) {
  68             rs.writeXml(oos);
  69         }
  70         return baos;
  71     }
  72 
  73     /*
  74      * Utility method to write a WebRowSet XML file via an OutputStream
  75      * and populating the WebRowSet via a ResultSet
  76      */
  77     protected ByteArrayOutputStream writeWebRowSetWithOutputStream(ResultSet rs) throws Exception {
  78         ByteArrayOutputStream baos = new ByteArrayOutputStream();
  79         try (ObjectOutputStream oos = new ObjectOutputStream(baos)) {
  80             WebRowSet wrs = rsf.createWebRowSet();
  81             wrs.writeXml(rs, oos);
  82         }
  83         return baos;
  84     }
  85 
  86 
  87     /*
  88      * Utility method to popoulate a WebRowSet via a InputStream
  89      */
  90     protected WebRowSet readWebRowSetWithOInputStream(ByteArrayOutputStream baos) throws Exception {
  91         WebRowSet wrs1 = rsf.createWebRowSet();
  92         try (ObjectInputStream ois
  93                 = new ObjectInputStream(new ByteArrayInputStream(baos.toByteArray()))) {
  94             wrs1.readXml(ois);
  95         }
  96         return wrs1;
  97     }
  98 
  99     /*
 100      * Utility method to write a WebRowSet XML file via an Writer
 101      */
 102     protected ByteArrayOutputStream writeWebRowSetWithOutputStreamWithWriter(WebRowSet rs) throws Exception {
 103         ByteArrayOutputStream baos = new ByteArrayOutputStream();
 104         OutputStreamWriter osw = new OutputStreamWriter(baos);
 105         rs.writeXml(osw);
 106         return baos;
 107     }
 108 
 109     /*
 110      * Utility method to write a WebRowSet XML file via an Writer and populating
 111      * the WebRowSet via a ResultSet
 112      */
 113     protected ByteArrayOutputStream writeWebRowSetWithOutputStreamWithWriter(ResultSet rs) throws Exception {
 114         ByteArrayOutputStream baos = new ByteArrayOutputStream();
 115         OutputStreamWriter osw = new OutputStreamWriter(baos);
 116         WebRowSet wrs = rsf.createWebRowSet();
 117         wrs.writeXml(rs, osw);
 118         return baos;
 119     }
 120 
 121     /*
 122      * Utility method to popoulate a WebRowSet via a Readar
 123      */
 124     protected WebRowSet readWebRowSetWithOInputStreamWithReader(ByteArrayOutputStream baos) throws Exception {
 125         WebRowSet wrs1 = rsf.createWebRowSet();
 126         InputStreamReader isr = new InputStreamReader(new ByteArrayInputStream(baos.toByteArray()));
 127         wrs1.readXml(isr);
 128         return wrs1;
 129     }
 130 
 131     /*
 132      * Validate the expected Rows are contained within the RowSet
 133      */
 134     @Test(dataProvider = "rowsetUsingCoffees")
 135     public void WebRowSetTest0000(WebRowSet wrs) throws Exception {
 136         assertEquals(getPrimaryKeys(wrs), COFFEES_PRIMARY_KEYS);
 137         assertEquals(wrs.size(), COFFEES_ROWS);
 138         wrs.close();
 139     }
 140 
 141     /*
 142      * Validate the expected Rows are contained within the RowSet
 143      * populated by readXML(Reader)
 144      */
 145     @Test(dataProvider = "rowSetType")
 146     public void WebRowSetTest0001(WebRowSet wrs1) throws Exception {
 147 
 148         try (FileReader fr = new FileReader(COFFEE_ROWS_XML)) {
 149             wrs1.readXml(fr);
 150         }
 151         assertEquals(getPrimaryKeys(wrs1), COFFEES_PRIMARY_KEYS);
 152         assertEquals(wrs1.size(), COFFEES_ROWS);
 153         wrs1.close();
 154 
 155     }
 156 
 157     /*
 158      * Validate the expected Rows are contained within the RowSet
 159      * populated by readXML(InputStream)
 160      */
 161     @Test(dataProvider = "rowSetType")
 162     public void WebRowSetTest0002(WebRowSet wrs1) throws Exception {
 163         try (FileInputStream fis = new FileInputStream(COFFEE_ROWS_XML)) {
 164             wrs1.readXml(fis);
 165         }
 166         assertEquals(getPrimaryKeys(wrs1), COFFEES_PRIMARY_KEYS);
 167         assertEquals(wrs1.size(), COFFEES_ROWS);
 168         wrs1.close();
 169     }
 170 
 171     /*
 172      * Write a WebRowSet via writeXML(OutputStream), read it
 173      * back via readXML(InputStream) and validate the primary  keys
 174      * are the same
 175      */
 176     @Test(dataProvider = "rowsetUsingCoffees")
 177     public void WebRowSetTest0003(WebRowSet wrs) throws Exception {
 178         ByteArrayOutputStream baos = writeWebRowSetWithOutputStream(wrs);
 179         try (WebRowSet wrs1 = readWebRowSetWithOInputStream(baos)) {
 180             assertEquals(getPrimaryKeys(wrs1), COFFEES_PRIMARY_KEYS);
 181             assertEquals(wrs1.size(), COFFEES_ROWS);
 182         }
 183     }
 184 
 185     /*
 186      * Write a ResultSet via writeXML(OutputStream), read it
 187      * back via readXML(InputStream) and validate the primary  keys
 188      * are the same
 189      */
 190     @Test(dataProvider = "rowsetUsingCoffees")
 191     public void WebRowSetTest0004(WebRowSet wrs) throws Exception {
 192         ResultSet rs = wrs;
 193         rs.beforeFirst();
 194         ByteArrayOutputStream baos = writeWebRowSetWithOutputStream(rs);
 195         try (WebRowSet wrs1 = readWebRowSetWithOInputStream(baos)) {
 196             assertEquals(getPrimaryKeys(wrs1), COFFEES_PRIMARY_KEYS);
 197             assertEquals(wrs1.size(), COFFEES_ROWS);
 198         }
 199     }
 200 
 201     /*
 202      * Write a WebRowSet via writeXML(Writer), read it
 203      * back via readXML(Reader) and validate the primary  keys
 204      * are the same
 205      */
 206     @Test(dataProvider = "rowsetUsingCoffees")
 207     public void WebRowSetTest0005(WebRowSet wrs) throws Exception {
 208         ByteArrayOutputStream baos = writeWebRowSetWithOutputStreamWithWriter(wrs);
 209         try (WebRowSet wrs1 = readWebRowSetWithOInputStreamWithReader(baos)) {
 210             assertEquals(getPrimaryKeys(wrs1), COFFEES_PRIMARY_KEYS);
 211             assertEquals(wrs1.size(), COFFEES_ROWS);
 212         }
 213     }
 214 
 215     /*
 216      * Write a WebRowSet via writeXML(Writer), read it
 217      * back via readXML(Reader) and validate the primary  keys
 218      * are the same
 219      */
 220     @Test(dataProvider = "rowsetUsingCoffees")
 221     public void WebRowSetTest0006(WebRowSet wrs) throws Exception {
 222         ResultSet rs = wrs;
 223         rs.beforeFirst();
 224         ByteArrayOutputStream baos = writeWebRowSetWithOutputStreamWithWriter(rs);
 225         try (WebRowSet wrs1 = readWebRowSetWithOInputStreamWithReader(baos)) {
 226             assertEquals(getPrimaryKeys(wrs1), COFFEES_PRIMARY_KEYS);
 227             assertEquals(wrs1.size(), COFFEES_ROWS);
 228         }
 229     }
 230 
 231     /*
 232      * Validate the expected Rows are contained within the RowSet
 233      * after deleting the specified rows
 234      */
 235     @Test(dataProvider = "rowsetUsingCoffees", enabled = false)
 236     public void WebRowSetTest0007(WebRowSet wrs) throws Exception {
 237         assertEquals(getPrimaryKeys(wrs), COFFEES_PRIMARY_KEYS);
 238         int[] rowsToDelete = {2, 4};
 239         assertEquals(getPrimaryKeys(wrs), COFFEES_PRIMARY_KEYS);
 240         for (int row : rowsToDelete) {
 241             assertTrue(deleteRowByPrimaryKey(wrs, row, 1));
 242         }
 243 
 244         FileInputStream fis = new FileInputStream(MODFIED_DELETED_COFFEE_ROWS_XML);
 245         try (WebRowSet wrs1 = rsf.createWebRowSet()) {
 246             wrs1.readXml(fis);
 247             // With setShowDeleted(false) which is the default,
 248             // the deleted row should not be visible
 249             for (int row : rowsToDelete) {
 250                 assertTrue(findRowByPrimaryKey(wrs1, row, 1));
 251             }
 252             assertTrue(wrs.size() == COFFEES_ROWS);
 253             // With setShowDeleted(true), the deleted row should be visible
 254             for (int row : rowsToDelete) {
 255                 assertTrue(findRowByPrimaryKey(wrs, row, 1));
 256             }
 257         }
 258     }
 259 
 260     /*
 261      * Validate the expected Rows are contained within the RowSet
 262      * that was populated by reading an xml file with all rows
 263      * marked as a currentRow
 264      */
 265     @Test(dataProvider = "rowSetType")
 266     public void WebRowSetTest0008(WebRowSet wrs1) throws Exception {
 267         FileInputStream fis = new FileInputStream(COFFEE_ROWS_XML);
 268         wrs1.readXml(fis);
 269         assertTrue(wrs1.size() == COFFEES_ROWS);
 270         assertEquals(getPrimaryKeys(wrs1), COFFEES_PRIMARY_KEYS);
 271         // Validate that the rows are not marked as deleted, inserted or updated
 272         wrs1.beforeFirst();
 273         while (wrs1.next()) {
 274             assertFalse(wrs1.rowDeleted());
 275             assertFalse(wrs1.rowInserted());
 276             assertFalse(wrs1.rowUpdated());
 277         }
 278         wrs1.close();
 279     }
 280 
 281     /*
 282      * Read an XML file to populate a WebRowSet and validate that the rows
 283      * that are marked as deleted are marked as such in the WebRowSet
 284      * Also validate that they are or are not visible based on the
 285      * setShowDeleted value
 286      */
 287     @Test(dataProvider = "rowSetType")
 288     public void WebRowSetTest0009(WebRowSet wrs1) throws Exception {
 289         int[] rowsToDelete = {2, 4};
 290         Object[] expectedRows = {1, 3, 5};
 291         FileInputStream fis = new FileInputStream(DELETED_COFFEE_ROWS_XML);
 292         wrs1.readXml(fis);
 293         assertTrue(wrs1.size() == COFFEES_ROWS);
 294         assertEquals(getPrimaryKeys(wrs1), expectedRows);
 295         // With setShowDeleted(false) which is the default,
 296         // the deleted row should not be visible
 297         for (int row : rowsToDelete) {
 298             assertFalse(findRowByPrimaryKey(wrs1, row, 1));
 299         }
 300         // With setShowDeleted(true), the deleted row should be visible
 301         wrs1.setShowDeleted(true);
 302         for (int row : rowsToDelete) {
 303             assertTrue(findRowByPrimaryKey(wrs1, row, 1));
 304         }
 305         assertEquals(getPrimaryKeys(wrs1), COFFEES_PRIMARY_KEYS);
 306         wrs1.close();
 307 
 308     }
 309 
 310     /*
 311      * Validate that the correct row in the WebRowSet that had been created
 312      * from an xml file is marked as updated and contains the correct values
 313      */
 314     @Test(dataProvider = "rowSetType")
 315     public void WebRowSetTest0010(WebRowSet wrs1) throws Exception {
 316         FileInputStream fis = new FileInputStream(UPDATED_COFFEE_ROWS_XML);
 317         wrs1.readXml(fis);
 318         assertTrue(wrs1.size() == COFFEES_ROWS);
 319         assertEquals(getPrimaryKeys(wrs1), COFFEES_PRIMARY_KEYS);
 320         wrs1.beforeFirst();
 321         while (wrs1.next()) {
 322             if (wrs1.getInt(1) == 3) {
 323                 assertTrue(wrs1.rowUpdated());
 324                 assertTrue(wrs1.getInt(5) == 21 && wrs1.getInt(6) == 69);
 325                 assertFalse(wrs1.rowDeleted());
 326                 assertFalse(wrs1.rowInserted());
 327             } else {
 328                 assertFalse(wrs1.rowUpdated());
 329                 assertFalse(wrs1.rowDeleted());
 330                 assertFalse(wrs1.rowInserted());
 331             }
 332         }
 333         wrs1.close();
 334     }
 335 
 336     /*
 337      * Validate the correct row is marked as inserted in a WebRowSet
 338      * that is read from an xml file
 339      */
 340     @Test(dataProvider = "rowSetType")
 341     public void WebRowSetTest0011(WebRowSet wrs1) throws Exception {
 342         int expectedSize = COFFEES_ROWS + 2;
 343         int addedRowPK = 15;
 344         int addedRowPK2 = 20;
 345         Object[] expected = Arrays.copyOf(COFFEES_PRIMARY_KEYS, expectedSize);
 346         expected[expectedSize - 2] = addedRowPK;
 347         expected[expectedSize - 1] = addedRowPK2;
 348         FileInputStream fis = new FileInputStream(INSERTED_COFFEE_ROWS_XML);
 349         wrs1.readXml(fis);
 350         assertTrue(wrs1.size() == expectedSize);
 351         assertEqualsNoOrder(getPrimaryKeys(wrs1), expected);
 352         wrs1.beforeFirst();
 353         while (wrs1.next()) {
 354             if (wrs1.getInt(1) == 15 || wrs1.getInt(1) == 20) {
 355                 assertTrue(wrs1.rowInserted());
 356                 assertFalse(wrs1.rowDeleted());
 357                 assertFalse(wrs1.rowUpdated());
 358             } else {
 359                 assertFalse(wrs1.rowInserted());
 360                 assertFalse(wrs1.rowDeleted());
 361                 assertFalse(wrs1.rowUpdated());
 362             }
 363         }
 364         wrs1.close();
 365     }
 366 
 367     /*
 368      * Read an xml file which contains a row that was inserted and updated
 369      */
 370     @Test(dataProvider = "rowSetType")
 371     public void WebRowSetTest0012(WebRowSet wrs1) throws Exception {
 372         int expectedSize = COFFEES_ROWS + 1;
 373         int addedRowPK = 100;
 374         Object[] expected = Arrays.copyOf(COFFEES_PRIMARY_KEYS, expectedSize);
 375         expected[expectedSize - 1] = addedRowPK;
 376         FileInputStream fis = new FileInputStream(UPDATED_INSERTED_COFFEE_ROWS_XML);
 377         wrs1.readXml(fis);
 378         assertTrue(wrs1.size() == expectedSize);
 379         assertEquals(getPrimaryKeys(wrs1), expected);
 380         wrs1.beforeFirst();
 381         while (wrs1.next()) {
 382             if (wrs1.getInt(1) == addedRowPK) {
 383                 // Row that was inserted and updated
 384                 assertTrue(wrs1.rowUpdated());
 385                 assertTrue(
 386                         wrs1.getBigDecimal(4).equals(BigDecimal.valueOf(12.99))
 387                         && wrs1.getInt(6) == 125);
 388                 assertFalse(wrs1.rowDeleted());
 389                 assertTrue(wrs1.rowInserted());
 390             } else {
 391                 // Remaining rows should only be inserted
 392                 assertFalse(wrs1.rowUpdated());
 393                 assertFalse(wrs1.rowDeleted());
 394                 assertTrue(wrs1.rowInserted());
 395             }
 396         }
 397         wrs1.close();
 398     }
 399 
 400 }