1 /*
   2  * Copyright (c) 2014, 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 
  24 /**
  25  * @test
  26  * @modules java.sql.rowset/com.sun.rowset
  27  *          java.sql.rowset/com.sun.rowset.internal
  28  *          java.sql.rowset/com.sun.rowset.providers
  29  */
  30 
  31 package test.rowset.serial;
  32 
  33 import java.sql.Array;
  34 import java.sql.SQLException;
  35 import java.util.Arrays;
  36 import java.util.HashMap;
  37 import java.util.Map;
  38 import javax.sql.rowset.serial.SerialArray;
  39 import javax.sql.rowset.serial.SerialException;
  40 import static org.testng.Assert.*;
  41 import org.testng.annotations.BeforeMethod;
  42 import org.testng.annotations.Test;
  43 import util.BaseTest;
  44 import util.StubArray;
  45 
  46 public class SerialArrayTests extends BaseTest {
  47 
  48     private Object[] coffees;
  49     private final String sqlType = "VARCHAR";
  50     private Array a;
  51     private Map<String, Class<?>> map;
  52 
  53     @BeforeMethod
  54     public void setUpMethod() throws Exception {
  55         coffees = new Object[]{"Espresso", "Colombian", "French Roast",
  56             "Cappuccino"};
  57         a = new StubArray(sqlType, coffees);
  58         map = new HashMap<>();
  59     }
  60 
  61     /*
  62      * Validate a SerialArray can be created from an Array
  63      */
  64     @Test
  65     public void test01() throws Exception {
  66         SerialArray sa = new SerialArray(a);
  67     }
  68 
  69     /*
  70      * Validate a SQLException is thrown if the map is null
  71      */
  72     @Test(expectedExceptions = SQLException.class)
  73     public void test02() throws Exception {
  74         SerialArray sa = new SerialArray(a, null);
  75     }
  76 
  77     /*
  78      * Validate a SerialException is thrown when getResultSet() is called
  79      */
  80     @Test(expectedExceptions = SerialException.class)
  81     public void test03() throws Exception {
  82         SerialArray sa = new SerialArray(a);
  83         sa.getResultSet();
  84     }
  85 
  86     /*
  87      * Validate a SerialException is thrown when getResultSet() is called
  88      */
  89     @Test(expectedExceptions = SerialException.class)
  90     public void test04() throws Exception {
  91         SerialArray sa = new SerialArray(a);
  92         sa.getResultSet(null);
  93     }
  94 
  95     /*
  96      * Validate a SerialException is thrown when getResultSet() is called
  97      */
  98     @Test(expectedExceptions = SerialException.class)
  99     public void test05() throws Exception {
 100         SerialArray sa = new SerialArray(a);
 101         sa.getResultSet(1, 1);
 102     }
 103 
 104     /*
 105      * Validate a SerialException is thrown when getResultSet() is called
 106      */
 107     @Test(expectedExceptions = SerialException.class)
 108     public void test06() throws Exception {
 109         SerialArray sa = new SerialArray(a);
 110         sa.getResultSet(1, 1, null);
 111     }
 112 
 113     /*
 114      * Validate a SerialException is thrown when  getArray() is invoked after
 115      * free() is called
 116      */
 117     @Test(expectedExceptions = SerialException.class)
 118     public void test07() throws Exception {
 119         SerialArray sa = new SerialArray(a);
 120         sa.free();
 121         sa.getArray();
 122     }
 123 
 124     /*
 125      * Validate a SerialException is thrown when  getArray() is invoked after
 126      * free() is called
 127      */
 128     @Test(expectedExceptions = SerialException.class)
 129     public void test08() throws Exception {
 130         SerialArray sa = new SerialArray(a);
 131         sa.free();
 132         sa.getArray(map);
 133     }
 134 
 135     /*
 136      * Validate a SerialException is thrown when  getArray() is invoked after
 137      * free() is called
 138      */
 139     @Test(expectedExceptions = SerialException.class)
 140     public void test09() throws Exception {
 141         SerialArray sa = new SerialArray(a);
 142         sa.free();
 143         sa.getArray(1, 1, map);
 144     }
 145 
 146     /*
 147      * Validate a SerialException is thrown when  getArray() is invoked after
 148      * free() is called
 149      */
 150     @Test(expectedExceptions = SerialException.class)
 151     public void test10() throws Exception {
 152         SerialArray sa = new SerialArray(a);
 153         sa.free();
 154         sa.getArray(1, 1);
 155     }
 156 
 157     /*
 158      * Validate a SerialException is thrown when  getBaseType() is invoked after
 159      * free() is called
 160      */
 161     @Test(expectedExceptions = SerialException.class)
 162     public void test11() throws Exception {
 163         SerialArray sa = new SerialArray(a);
 164         sa.free();
 165         sa.getBaseType();
 166     }
 167 
 168     /*
 169      * Validate a SerialException is thrown when  getBaseTypeName() is invoked after
 170      * free() is called
 171      */
 172     @Test(expectedExceptions = SerialException.class)
 173     public void test12() throws Exception {
 174         SerialArray sa = new SerialArray(a);
 175         sa.free();
 176         sa.getBaseTypeName();
 177     }
 178 
 179     /*
 180      * Validate getArray() returns the same Object[] used to create the
 181      * SerialArray
 182      */
 183     @Test
 184     public void test13() throws Exception {
 185         SerialArray sa = new SerialArray(a);
 186         Object[] o = (Object[]) sa.getArray();
 187         assertTrue(Arrays.equals(o, coffees));
 188     }
 189 
 190     /*
 191      * Validate getArray() returns the same Object[] used to create the
 192      * SerialArray
 193      */
 194     @Test
 195     public void test14() throws Exception {
 196         SerialArray sa = new SerialArray(a);
 197         Object[] o = (Object[]) sa.getArray(map);
 198         assertTrue(Arrays.equals(o, coffees));
 199     }
 200 
 201     /*
 202      * Validate getArray() returns the same Object[] used to create the
 203      * SerialArray
 204      */
 205     @Test
 206     public void test15() throws Exception {
 207         SerialArray sa = new SerialArray(a);
 208         Object[] o = (Object[]) sa.getArray(1, 2);
 209         assertTrue(Arrays.equals(o, Arrays.copyOfRange(coffees, 1, 3)));
 210     }
 211 
 212     /*
 213      * Validate getArray() returns the same Object[] used to create the
 214      * SerialArray
 215      */
 216     @Test
 217     public void test16() throws Exception {
 218         SerialArray sa = new SerialArray(a);
 219         Object[] o = (Object[]) sa.getArray(1, 2, map);
 220         assertTrue(Arrays.equals(o, Arrays.copyOfRange(coffees, 1, 3)));
 221     }
 222 
 223     /*
 224      * clone() a SerialArray and check that it is equal to the
 225      * object it was cloned from
 226      */
 227     @Test
 228     public void test17() throws Exception {
 229         SerialArray sa = new SerialArray(a);
 230         SerialArray sa1 = (SerialArray) sa.clone();
 231         assertTrue(sa.equals(sa1));
 232     }
 233 
 234     /*
 235      * Validate that a SerialArray that is serialized & deserialized is equal to
 236      * itself
 237      */
 238     @Test
 239     public void test18() throws Exception {
 240         SerialArray sa = new SerialArray(a);
 241         SerialArray sa1 = serializeDeserializeObject(sa);;
 242         assertTrue(sa.equals(sa1));
 243     }
 244 }