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.sql; 24 25 import java.io.FileInputStream; 26 import java.io.FileOutputStream; 27 import java.io.ObjectInputStream; 28 import java.io.ObjectOutputStream; 29 import java.sql.SQLFeatureNotSupportedException; 30 import java.sql.SQLException; 31 import java.sql.SQLNonTransientException; 32 import static org.testng.Assert.*; 33 import org.testng.annotations.AfterClass; 34 import org.testng.annotations.AfterMethod; 35 import org.testng.annotations.BeforeClass; 36 import org.testng.annotations.BeforeMethod; 37 import org.testng.annotations.Test; 38 39 public class SQLFeatureNotSupportedExceptionTests { 40 41 private final String reason = "reason"; 42 private final String state = "SQLState"; 43 private final String cause = "java.lang.Throwable: cause"; 44 private final Throwable t = new Throwable("cause"); 45 private final Throwable t1 = new Throwable("cause 1"); 46 private final Throwable t2 = new Throwable("cause 2"); 47 private final int errorCode = 21; 48 private final String[] msgs = {"Exception 1", "cause 1", "Exception 2", 49 "Exception 3", "cause 2"}; 50 51 public SQLFeatureNotSupportedExceptionTests() { 52 } 53 54 @BeforeClass 55 public static void setUpClass() throws Exception { 56 } 57 58 @AfterClass 59 public static void tearDownClass() throws Exception { 60 } 61 62 @BeforeMethod 63 public void setUpMethod() throws Exception { 64 } 65 66 @AfterMethod 67 public void tearDownMethod() throws Exception { 68 } 69 70 /** 71 * Create SQLFeatureNotSupportedException and setting all objects to null 72 */ 73 @Test 74 public void test() { 75 SQLFeatureNotSupportedException e = 76 new SQLFeatureNotSupportedException(null, null, errorCode, null); 77 assertTrue(e.getMessage() == null && e.getSQLState() == null 78 && e.getCause() == null && e.getErrorCode() == errorCode); 79 } 80 81 /** 82 * Create SQLFeatureNotSupportedException with no-arg constructor 83 */ 84 @Test 85 public void test1() { 86 SQLFeatureNotSupportedException ex = new SQLFeatureNotSupportedException(); 87 assertTrue(ex.getMessage() == null 88 && ex.getSQLState() == null 89 && ex.getCause() == null 90 && ex.getErrorCode() == 0); 91 } 92 93 /** 94 * Create SQLFeatureNotSupportedException with message 95 */ 96 @Test 97 public void test2() { 98 SQLFeatureNotSupportedException ex = 99 new SQLFeatureNotSupportedException(reason); 100 assertTrue(ex.getMessage().equals(reason) 101 && ex.getSQLState() == null 102 && ex.getCause() == null 103 && ex.getErrorCode() == 0); 104 } 105 106 /** 107 * Create SQLFeatureNotSupportedException with message, and SQLState 108 */ 109 @Test 110 public void test3() { 111 SQLFeatureNotSupportedException ex = 112 new SQLFeatureNotSupportedException(reason, state); 113 assertTrue(ex.getMessage().equals(reason) 114 && ex.getSQLState().equals(state) 115 && ex.getCause() == null 116 && ex.getErrorCode() == 0); 117 } 118 119 /** 120 * Create SQLFeatureNotSupportedException with message, SQLState, and error code 121 */ 122 @Test 123 public void test4() { 124 SQLFeatureNotSupportedException ex = 125 new SQLFeatureNotSupportedException(reason, state, errorCode); 126 assertTrue(ex.getMessage().equals(reason) 127 && ex.getSQLState().equals(state) 128 && ex.getCause() == null 129 && ex.getErrorCode() == errorCode); 130 } 131 132 /** 133 * Create SQLFeatureNotSupportedException with message, SQLState, errorCode, and Throwable 134 */ 135 @Test 136 public void test5() { 137 SQLFeatureNotSupportedException ex = 138 new SQLFeatureNotSupportedException(reason, state, errorCode, t); 139 assertTrue(ex.getMessage().equals(reason) 140 && ex.getSQLState().equals(state) 141 && cause.equals(ex.getCause().toString()) 142 && ex.getErrorCode() == errorCode); 143 } 144 145 /** 146 * Create SQLFeatureNotSupportedException with message, SQLState, and Throwable 147 */ 148 @Test 149 public void test6() { 150 SQLFeatureNotSupportedException ex = 151 new SQLFeatureNotSupportedException(reason, state, t); 152 assertTrue(ex.getMessage().equals(reason) 153 && ex.getSQLState().equals(state) 154 && cause.equals(ex.getCause().toString()) 155 && ex.getErrorCode() == 0); 156 } 157 158 /** 159 * Create SQLFeatureNotSupportedException with message, and Throwable 160 */ 161 @Test 162 public void test7() { 163 SQLFeatureNotSupportedException ex = 164 new SQLFeatureNotSupportedException(reason, t); 165 assertTrue(ex.getMessage().equals(reason) 166 && ex.getSQLState() == null 167 && cause.equals(ex.getCause().toString()) 168 && ex.getErrorCode() == 0); 169 } 170 171 /** 172 * Create SQLFeatureNotSupportedException with null Throwable 173 */ 174 @Test 175 public void test8() { 176 SQLFeatureNotSupportedException ex = 177 new SQLFeatureNotSupportedException((Throwable) null); 178 assertTrue(ex.getMessage() == null 179 && ex.getSQLState() == null 180 && ex.getCause() == null 181 && ex.getErrorCode() == 0); 182 } 183 184 /** 185 * Create SQLFeatureNotSupportedException with Throwable 186 */ 187 @Test 188 public void test9() { 189 SQLFeatureNotSupportedException ex = 190 new SQLFeatureNotSupportedException(t); 191 assertTrue(ex.getMessage().equals(cause) 192 && ex.getSQLState() == null 193 && cause.equals(ex.getCause().toString()) 194 && ex.getErrorCode() == 0); 195 } 196 197 /** 198 * Serialize a SQLFeatureNotSupportedException and make sure you can read it back properly 199 */ 200 @Test 201 public void test10() throws Exception { 202 SQLFeatureNotSupportedException e = 203 new SQLFeatureNotSupportedException(reason, state, errorCode, t); 204 ObjectOutputStream out 205 = new ObjectOutputStream( 206 new FileOutputStream("SQLFeatureNotSupportedException.ser")); 207 out.writeObject(e); 208 ObjectInputStream is = new ObjectInputStream( 209 new FileInputStream("SQLFeatureNotSupportedException.ser")); 210 SQLFeatureNotSupportedException ex1 = (SQLFeatureNotSupportedException) is.readObject(); 211 assertTrue(reason.equals(ex1.getMessage()) 212 && ex1.getSQLState().equals(state) 213 && cause.equals(ex1.getCause().toString()) 214 && ex1.getErrorCode() == errorCode); 215 } 216 217 /** 218 * Validate that the ordering of the returned Exceptions is correct 219 * using for-each loop 220 */ 221 @Test 222 public void test11() { 223 SQLFeatureNotSupportedException ex = 224 new SQLFeatureNotSupportedException("Exception 1", t1); 225 SQLFeatureNotSupportedException ex1 = 226 new SQLFeatureNotSupportedException("Exception 2"); 227 SQLFeatureNotSupportedException ex2 = 228 new SQLFeatureNotSupportedException("Exception 3", t2); 229 ex.setNextException(ex1); 230 ex.setNextException(ex2); 231 int num = 0; 232 for (Throwable e : ex) { 233 assertTrue(msgs[num++].equals(e.getMessage())); 234 } 235 } 236 237 /** 238 * Validate that the ordering of the returned Exceptions is correct 239 * using traditional while loop 240 */ 241 @Test 242 public void test12() { 243 SQLFeatureNotSupportedException ex = 244 new SQLFeatureNotSupportedException("Exception 1", t1); 245 SQLFeatureNotSupportedException ex1 = 246 new SQLFeatureNotSupportedException("Exception 2"); 247 SQLFeatureNotSupportedException ex2 = 248 new SQLFeatureNotSupportedException("Exception 3", t2); 249 ex.setNextException(ex1); 250 ex.setNextException(ex2); 251 int num = 0; 252 SQLException sqe = ex; 253 while (sqe != null) { 254 assertTrue(msgs[num++].equals(sqe.getMessage())); 255 Throwable c = sqe.getCause(); 256 while (c != null) { 257 assertTrue(msgs[num++].equals(c.getMessage())); 258 c = c.getCause(); 259 } 260 sqe = sqe.getNextException(); 261 } 262 } 263 264 /** 265 * Create SQLFeatureNotSupportedException and validate it is an instance of 266 * SQLNonTransientException 267 */ 268 @Test 269 public void test13() { 270 Exception ex = new SQLFeatureNotSupportedException(); 271 assertTrue(ex instanceof SQLNonTransientException); 272 } 273 }