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.sql.Ref;
26 import java.sql.SQLException;
27 import java.util.HashMap;
28 import java.util.Map;
29 import javax.sql.rowset.serial.SerialRef;
30 import static org.testng.Assert.*;
31 import org.testng.annotations.BeforeMethod;
32 import org.testng.annotations.Test;
33 import util.BaseTest;
34 import util.StubRef;
35 import util.SuperHero;
36 
37 public class SerialRefTests extends BaseTest {
38 
39     private static Map<String, Class<?>> map = new HashMap<>();
40     private Ref ref;
41     private final String sqlType = "SUPERHERO";
42     private SuperHero hero;
43 
44     @BeforeMethod
setUpMethod()45     public void setUpMethod() throws Exception {
46         map.put(sqlType, Class.forName("util.SuperHero"));
47         hero = new SuperHero(sqlType, "Bruce", "Wayne", 1939, "Batman");
48         ref = new StubRef(sqlType, hero);
49     }
50 
51     /*
52      * Validate that a SQLException() is thrown if the Ref is null
53      */
54     @Test(expectedExceptions = SQLException.class)
test01()55     public void test01() throws Exception {
56         SerialRef sr = new SerialRef(null);
57     }
58 
59     /*
60      * Validate that a SQLException() is thrown if the typeName is null in the
61      * Ref used to create the SerialRef
62      */
63     @Test(expectedExceptions = SQLException.class)
test02()64     public void test02() throws Exception {
65         SerialRef sr = new SerialRef(new StubRef(null, hero));
66     }
67 
68     /*
69      * Validate that getBaseTypeName() returns the same SQLType specified
70      * to create the Ref
71      */
72     @Test
test03()73     public void test03() throws Exception {
74         SerialRef sr = new SerialRef(ref);
75         assertEquals(sr.getBaseTypeName(), sqlType);
76     }
77 
78     /*
79      * Validate that getObject() returns the same object used to create the Ref
80      */
81     @Test
test04()82     public void test04() throws Exception {
83         SerialRef sr = new SerialRef(ref);
84         assertTrue(hero.equals(sr.getObject()));
85     }
86 
87     /*
88      * Validate that getObject() returns the same object used to create the Ref
89      */
90     @Test(enabled = false)
test05()91     public void test05() throws Exception {
92         SerialRef sr = new SerialRef(ref);
93         assertTrue(hero.equals(sr.getObject(map)));
94     }
95 
96     /*
97      * Validate that setObject() can be used to change the value of the object
98      * pointed to by the SerialRef
99      */
100     @Test
test06()101     public void test06() throws Exception {
102         SerialRef sr = new SerialRef(ref);
103         assertTrue(hero.equals(sr.getObject()));
104         SuperHero h = new SuperHero(sqlType, "Dick", "Grayson", 1940, "Robin");
105         sr.setObject(h);
106         assertFalse(hero.equals(sr.getObject()));
107     }
108 
109     /*
110      * clone() a SerialRef and check that it is equal to the
111      * object it was cloned from
112      */
113     @Test
test09()114     public void test09() throws Exception {
115         SerialRef sr = new SerialRef(ref);
116         SerialRef sr1 = (SerialRef) sr.clone();
117         assertTrue(sr.equals(sr1));
118     }
119 
120     /**
121      * Validate that a SerialRef that is serialized & deserialized is equal to
122      * itself for the Object & baseTypeName
123      */
124     @Test
test10()125     public void test10() throws Exception {
126         SerialRef sr = new SerialRef(ref);
127         SerialRef sr1 = serializeDeserializeObject(sr);
128         assertTrue(sr1.getObject().equals(sr.getObject())
129                 && sr1.getBaseTypeName().equals(sr.getBaseTypeName()));
130     }
131 }
132