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.serial;
  24 
  25 import java.net.URL;
  26 import java.sql.Array;
  27 import java.sql.Blob;
  28 import java.sql.Clob;
  29 import java.sql.Ref;
  30 import java.sql.SQLException;
  31 import java.sql.Struct;
  32 import java.util.Arrays;
  33 import java.util.HashMap;
  34 import java.util.Map;
  35 import java.util.Vector;
  36 import javax.sql.rowset.serial.SQLInputImpl;
  37 import javax.sql.rowset.serial.SQLOutputImpl;
  38 import javax.sql.rowset.serial.SerialArray;
  39 import javax.sql.rowset.serial.SerialBlob;
  40 import javax.sql.rowset.serial.SerialClob;
  41 import javax.sql.rowset.serial.SerialDatalink;
  42 import javax.sql.rowset.serial.SerialRef;
  43 import javax.sql.rowset.serial.SerialStruct;
  44 import static org.testng.Assert.*;
  45 import org.testng.annotations.BeforeMethod;
  46 import org.testng.annotations.Test;
  47 import util.BaseTest;
  48 import util.StubArray;
  49 import util.StubBlob;
  50 import util.StubClob;
  51 import util.StubRef;
  52 import util.StubStruct;
  53 import util.SuperHero;
  54 import util.TestSQLDataImpl;
  55 
  56 public class SQLOutputImplTests extends BaseTest {
  57 
  58     // Copy of the array of data type values
  59     private Object[] typeValues;
  60     private TestSQLDataImpl impl;
  61     private Map<String, Class<?>> map = new HashMap<>();
  62     private Vector results;
  63     private final String sqlType = "SUPERHERO";
  64     private SuperHero hero;
  65     private SQLOutputImpl outImpl;
  66 
  67     @BeforeMethod
  68     @Override
  69     public void setUpMethod() throws Exception {
  70         results = new Vector();
  71         impl = new TestSQLDataImpl("TestSQLData");
  72         typeValues = Arrays.copyOf(TestSQLDataImpl.attributes,
  73                 TestSQLDataImpl.attributes.length);
  74         hero = new SuperHero(sqlType, "Bruce", "Wayne", 1939, "Batman");
  75         outImpl = new SQLOutputImpl(results, map);
  76     }
  77 
  78     /*
  79      * Validate that a SQLException is thrown if the attribute value is
  80      * null
  81      */
  82     @Test(expectedExceptions = SQLException.class)
  83     public void test() throws Exception {
  84         SQLOutputImpl x = new SQLOutputImpl(null, map);
  85     }
  86 
  87     /*
  88      * Validate that a SQLException is thrown if the map value is
  89      * null
  90      */
  91     @Test(expectedExceptions = SQLException.class)
  92     public void test02() throws Exception {
  93         SQLOutputImpl x = new SQLOutputImpl(results, null);
  94     }
  95 
  96     /*
  97      * Read in the various datatypes via readSQL (which will exercise the
  98      * various readXXX methods and validate that results are as expected
  99      */
 100     @Test()
 101     public void test03() throws Exception {
 102         impl.readSQL(new SQLInputImpl(typeValues, map), "misc");
 103         impl.writeSQL(outImpl);
 104         assertTrue(Arrays.equals(results.toArray(), typeValues));
 105         // Null out a field and make sure the arrays do not match
 106         typeValues[2] = null;
 107         assertFalse(Arrays.equals(results.toArray(), typeValues));
 108     }
 109 
 110     /*
 111      * Validate a Array can be written and returned
 112      */
 113     @Test(enabled = true)
 114     public void test04() throws Exception {
 115         Object[] coffees = new Object[]{"Espresso", "Colombian", "French Roast",
 116             "Cappuccino"};
 117         Array a = new StubArray("VARCHAR", coffees);
 118         outImpl.writeArray(a);
 119         SerialArray sa = (SerialArray) results.get(0);
 120         assertTrue(Arrays.equals(coffees, (Object[]) sa.getArray()));
 121         assertTrue(a.getBaseTypeName().equals(sa.getBaseTypeName()));
 122     }
 123 
 124     /*
 125      * Validate a Blob can be written and returned
 126      */
 127     @Test(enabled = true)
 128     public void test05() throws Exception {
 129         Blob b = new StubBlob();
 130         outImpl.writeBlob(b);
 131         SerialBlob sb = (SerialBlob) results.get(0);
 132         assertTrue(Arrays.equals(
 133                 b.getBytes(1, (int) b.length()),
 134                 sb.getBytes(1, (int) sb.length())));
 135     }
 136 
 137     /*
 138      * Validate a Clob can be written and returned
 139      */
 140     @Test(enabled = true)
 141     public void test06() throws Exception {
 142         Clob c = new StubClob();
 143         outImpl.writeClob(c);
 144         SerialClob sc = (SerialClob) results.get(0);
 145         assertTrue(c.getSubString(1,
 146                 (int) c.length()).equals(sc.getSubString(1, (int) sc.length())));
 147     }
 148 
 149     /*
 150      * Validate a Ref can be written and returned
 151      */
 152     @Test(enabled = true)
 153     public void test07() throws Exception {
 154         Ref ref = new StubRef(sqlType, hero);
 155         outImpl.writeRef(ref);
 156         SerialRef sr = (SerialRef) results.get(0);
 157         assertTrue(hero.equals(sr.getObject()));
 158     }
 159 
 160     /*
 161      * Validate a Struct can be written and returned
 162      */
 163     @Test(enabled = true)
 164     public void test08() throws Exception {
 165         Object[] attributes = new Object[]{"Bruce", "Wayne", 1939,
 166             "Batman"};
 167         Struct s = new StubStruct(sqlType, attributes);
 168         outImpl.writeStruct(s);
 169         SerialStruct ss = (SerialStruct) results.get(0);
 170         assertTrue(Arrays.equals(attributes, (Object[]) ss.getAttributes()));
 171         assertTrue(sqlType.equals(ss.getSQLTypeName()));
 172     }
 173 
 174     /*
 175      * Validate a DataLink can be written and returned
 176      */
 177     @Test(enabled = true)
 178     public void test09() throws Exception {
 179         URL u = new URL("http://www.oracle.com/");
 180         outImpl.writeURL(u);
 181         SerialDatalink sdl = (SerialDatalink) results.get(0);
 182         URL u2 = sdl.getDatalink();
 183         assertTrue(u2.equals(u));
 184         assertTrue(u2.sameFile(u));
 185     }
 186 
 187     /*
 188      * Validate an Object implementing SQLData can be written and returned
 189      */
 190     @Test(enabled = true)
 191     public void test10() throws Exception {
 192         Object[] attributes = new Object[]{"Bruce", "Wayne", 1939,
 193             "Batman"};
 194         outImpl.writeObject(hero);
 195         SerialStruct ss = (SerialStruct) results.get(0);
 196         assertTrue(Arrays.equals(attributes, (Object[]) ss.getAttributes()));
 197         assertTrue(sqlType.equals(ss.getSQLTypeName()));
 198     }
 199 }