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 javax.sql.rowset.serial.SQLInputImpl;
36 import static org.testng.Assert.*;
37 import org.testng.annotations.BeforeMethod;
38 import org.testng.annotations.Test;
39 import util.BaseTest;
40 import util.StubArray;
41 import util.StubBlob;
42 import util.StubClob;
43 import util.StubRef;
44 import util.StubStruct;
45 import util.SuperHero;
46 import util.TestSQLDataImpl;
47 
48 public class SQLInputImplTests extends BaseTest {
49 
50     // Copy of the array of data type values
51     private Object[] typeValues;
52     private TestSQLDataImpl impl;
53     private Map<String, Class<?>> map ;
54     private SuperHero hero;
55     private final String sqlType = "SUPERHERO";
56 
57     @BeforeMethod
58     @Override
setUpMethod()59     public void setUpMethod() throws Exception {
60         map = new HashMap<>();
61         impl = new TestSQLDataImpl("TestSQLData");
62         typeValues = Arrays.copyOf(TestSQLDataImpl.attributes,
63                 TestSQLDataImpl.attributes.length);
64         hero = new SuperHero(sqlType, "Bruce", "Wayne",
65                 1939, "Batman");
66     }
67 
68     /*
69      * Validate that a SQLException is thrown if the attribute value is
70      * null
71      */
72     @Test(expectedExceptions = SQLException.class)
test()73     public void test() throws Exception {
74         SQLInputImpl x = new SQLInputImpl(null, map);
75     }
76 
77     /*
78      * Validate that a SQLException is thrown if the map value is
79      * null
80      */
81     @Test(expectedExceptions = SQLException.class)
test02()82     public void test02() throws Exception {
83         SQLInputImpl x = new SQLInputImpl(typeValues, null);
84     }
85 
86     /*
87      * Read in the various datatypes via readSQL (which will exercise the
88      * various readXXX methods and validate that results are as expected
89      */
90     @Test()
test03()91     public void test03() throws Exception {
92         impl.readSQL(new SQLInputImpl(typeValues, map), "misc");
93         assertTrue(Arrays.equals(impl.toArray(), typeValues));
94         // Null out a field and make sure the arrays do not match
95         typeValues[2] = null;
96         assertFalse(Arrays.equals(impl.toArray(), typeValues));
97     }
98 
99     /*
100      * Validate that wasNull indicates if a null was read in
101      */
102     @Test()
test04()103     public void test04() throws Exception {
104         Object[] values = {"Hello", null, 1};
105         SQLInputImpl sqli = new SQLInputImpl(values, map);
106         String s = sqli.readString();
107         assertFalse(sqli.wasNull());
108         s = sqli.readString();
109         assertTrue(sqli.wasNull());
110         int i = sqli.readInt();
111         assertFalse(sqli.wasNull());
112     }
113 
114     /*
115      * Validate that readObject returns the correct value
116      */
117     @Test()
test05()118     public void test05() throws Exception {
119         Object[] values = {hero};
120         SQLInputImpl sqli = new SQLInputImpl(values, map);
121         Object o = sqli.readObject();
122         assertTrue(hero.equals(o));
123 
124     }
125 
126     /*
127      * Validate a Array can be read
128      */
129     @Test(enabled = true)
test06()130     public void test06() throws Exception {
131         Object[] coffees = new Object[]{"Espresso", "Colombian", "French Roast",
132             "Cappuccino"};
133         Array a = new StubArray("VARCHAR", coffees);
134         Object[] values = {a};
135         SQLInputImpl sqli = new SQLInputImpl(values, map);
136         Array a2 = sqli.readArray();
137         assertTrue(Arrays.equals((Object[]) a2.getArray(), (Object[]) a.getArray()));
138         assertTrue(a.getBaseTypeName().equals(a2.getBaseTypeName()));
139     }
140 
141     /*
142      * Validate a Blob can be read
143      */
144     @Test(enabled = true)
test07()145     public void test07() throws Exception {
146         Blob b = new StubBlob();
147         Object[] values = {b};
148         SQLInputImpl sqli = new SQLInputImpl(values, map);
149         Blob b2 = sqli.readBlob();
150         assertTrue(Arrays.equals(
151                 b.getBytes(1, (int) b.length()),
152                 b2.getBytes(1, (int) b2.length())));
153     }
154 
155     /*
156      * Validate a Clob can be read
157      */
158     @Test(enabled = true)
test08()159     public void test08() throws Exception {
160         Clob c = new StubClob();
161         Object[] values = {c};
162         SQLInputImpl sqli = new SQLInputImpl(values, map);
163         Clob c2 = sqli.readClob();
164         assertTrue(c.getSubString(1,
165                 (int) c.length()).equals(c2.getSubString(1, (int) c2.length())));
166     }
167 
168     /*
169      * Validate a Ref can be read
170      */
171     @Test(enabled = true)
test09()172     public void test09() throws Exception {
173         Ref ref = new StubRef(sqlType, hero);
174         Object[] values = {ref};
175         SQLInputImpl sqli = new SQLInputImpl(values, map);
176         Ref ref2 = sqli.readRef();
177         assertTrue(ref.getObject().equals(ref2.getObject()));
178         assertTrue(ref.getBaseTypeName().equals(ref2.getBaseTypeName()));
179     }
180 
181     /*
182      * Validate a URL can be read
183      */
184     @Test(enabled = true)
test10()185     public void test10() throws Exception {
186         URL u = new URL("http://www.oracle.com/");;
187         Object[] values = {u};
188         SQLInputImpl sqli = new SQLInputImpl(values, map);
189         URL u2 = sqli.readURL();
190         assertTrue(u2.equals(u));
191         assertTrue(u2.sameFile(u));
192     }
193 
194     /*
195      * Validate that readObject returns the correct value when a Struct is
196      * next on the stream
197      */
198     @Test()
test11()199     public void test11() throws Exception {
200         Object[] attributes = new Object[]{"Bruce", "Wayne", 1939,
201             "Batman"};
202         map.put(sqlType, Class.forName("util.SuperHero"));
203         Struct struct = new StubStruct(sqlType, attributes);
204         Object[] values = {struct};
205         SQLInputImpl sqli = new SQLInputImpl(values, map);
206         Object o = sqli.readObject();
207 
208         assertTrue(hero.equals(o));
209 
210     }
211 }
212