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.Struct; 26 import java.util.Arrays; 27 import java.util.HashMap; 28 import java.util.Map; 29 import javax.sql.rowset.serial.SerialStruct; 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.StubStruct; 35 import util.SuperHero; 36 37 public class SerialStructTests extends BaseTest { 38 39 private static Map<String, Class<?>> map = new HashMap<>(); 40 private Object[] attributes; 41 private Struct struct; 42 private final String sqlType = "SUPERHERO"; 43 private SuperHero hero; 44 45 @BeforeMethod 46 public void setUpMethod() throws Exception { 47 attributes = new Object[]{"Bruce", "Wayne", 1939, 48 "Batman"}; 49 map.put(sqlType, Class.forName("util.SuperHero")); 50 struct = new StubStruct(sqlType, attributes); 51 hero = new SuperHero(sqlType, "Bruce", "Wayne", 1939, "Batman"); 52 } 53 54 /* 55 * Validate that getSQLTypeName() returns the same SQLType specified by 56 * the Struct used to create the object 57 */ 58 @Test 59 public void test01() throws Exception { 60 SerialStruct ss = new SerialStruct(struct, map); 61 assertEquals(ss.getSQLTypeName(), sqlType); 62 } 63 64 /* 65 * Validate that getSQLTypeName() returns the same SQLType specified by 66 * the Struct used to create the object 67 */ 68 @Test 69 public void test02() throws Exception { 70 SerialStruct ss = new SerialStruct(hero, map); 71 assertEquals(ss.getSQLTypeName(), sqlType); 72 } 73 74 /* 75 * Validate that getAttributes() returns the same attributes specified by 76 * the Struct used to create the object 77 */ 78 @Test 79 public void test03() throws Exception { 80 SerialStruct ss = new SerialStruct(struct, map); 81 assertTrue(Arrays.equals(attributes, 82 ss.getAttributes())); 83 } 84 85 /* 86 * Validate that getAttributes() returns the same attributes specified by 87 * the Struct used to create the object 88 */ 89 @Test 90 public void test04() throws Exception { 91 SerialStruct ss = new SerialStruct(hero, map); 92 assertTrue(Arrays.equals(attributes, 93 ss.getAttributes())); 94 } 95 96 /* 97 * Validate that getAttributes() returns the 98 same attributes specified by 99 * the Struct used to create the object 100 */ 101 @Test 102 public void test05() throws Exception { 103 SerialStruct ss = new SerialStruct(struct, map); 104 assertTrue(Arrays.equals(attributes, 105 ss.getAttributes(map))); 106 } 107 108 /* 109 * Validate that getAttributes() returns the same attributes specified by 110 * the Struct used to create the object 111 */ 112 @Test 113 public void test06() throws Exception { 114 SerialStruct ss = new SerialStruct(hero, map); 115 assertTrue(Arrays.equals(attributes, 116 ss.getAttributes(map))); 117 } 118 119 /* 120 * clone() a SerialStruct and check that it is equal to the 121 * object it was cloned from 122 */ 123 @Test 124 public void test07() throws Exception { 125 SerialStruct ss = new SerialStruct(struct, map); 126 SerialStruct ss1 = (SerialStruct) ss.clone(); 127 assertTrue(ss.equals(ss1)); 128 } 129 130 /** 131 * Validate that a SerialStruct that is serialized & deserialized is equal 132 * to itself 133 */ 134 @Test 135 public void test08() throws Exception { 136 SerialStruct ss = new SerialStruct(struct, map); 137 SerialStruct ss1 = serializeDeserializeObject(ss);; 138 assertTrue(ss.equals(ss1)); 139 } 140 }