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