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.SQLSyntaxErrorException; 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 SQLSyntaxErrorExceptionTests { 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 SQLSyntaxErrorExceptionTests() { 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 SQLSyntaxErrorException and setting all objects to null 72 */ 73 @Test 74 public void test() { 75 SQLSyntaxErrorException e = new SQLSyntaxErrorException(null, 76 null, errorCode, null); 77 assertTrue(e.getMessage() == null && e.getSQLState() == null 78 && e.getCause() == null && e.getErrorCode() == errorCode); 79 } 80 81 /** 82 * Create SQLSyntaxErrorException with no-arg constructor 83 */ 84 @Test 85 public void test1() { 86 SQLSyntaxErrorException ex = new SQLSyntaxErrorException(); 87 assertTrue(ex.getMessage() == null 88 && ex.getSQLState() == null 179 /** 180 * Create SQLSyntaxErrorException with Throwable 181 */ 182 @Test 183 public void test9() { 184 SQLSyntaxErrorException ex = new SQLSyntaxErrorException(t); 185 assertTrue(ex.getMessage().equals(cause) 186 && ex.getSQLState() == null 187 && cause.equals(ex.getCause().toString()) 188 && ex.getErrorCode() == 0); 189 } 190 191 /** 192 * Serialize a SQLSyntaxErrorException and make sure you can read it back properly 193 */ 194 @Test 195 public void test10() throws Exception { 196 197 SQLSyntaxErrorException e = 198 new SQLSyntaxErrorException(reason, state, errorCode, t); 199 ObjectOutputStream out 200 = new ObjectOutputStream( 201 new FileOutputStream("SQLSyntaxErrorException.ser")); 202 out.writeObject(e); 203 ObjectInputStream is = new ObjectInputStream( 204 new FileInputStream("SQLSyntaxErrorException.ser")); 205 SQLSyntaxErrorException ex1 = (SQLSyntaxErrorException) is.readObject(); 206 assertTrue(reason.equals(ex1.getMessage()) 207 && ex1.getSQLState().equals(state) 208 && cause.equals(ex1.getCause().toString()) 209 && ex1.getErrorCode() == errorCode); 210 } 211 212 /** 213 * Validate that the ordering of the returned Exceptions is correct 214 * using for-each loop 215 */ 216 @Test 217 public void test11() { 218 SQLSyntaxErrorException ex = new SQLSyntaxErrorException("Exception 1", t1); 219 SQLSyntaxErrorException ex1 = new SQLSyntaxErrorException("Exception 2"); 220 SQLSyntaxErrorException ex2 = new SQLSyntaxErrorException("Exception 3", t2); 221 ex.setNextException(ex1); 222 ex.setNextException(ex2); 223 int num = 0; 224 for (Throwable e : ex) { 225 assertTrue(msgs[num++].equals(e.getMessage())); | 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.sql.SQLException; 26 import java.sql.SQLNonTransientException; 27 import java.sql.SQLSyntaxErrorException; 28 import static org.testng.Assert.*; 29 import org.testng.annotations.Test; 30 import util.BaseTest; 31 32 public class SQLSyntaxErrorExceptionTests extends BaseTest { 33 34 /** 35 * Create SQLSyntaxErrorException and setting all objects to null 36 */ 37 @Test 38 public void test() { 39 SQLSyntaxErrorException e = new SQLSyntaxErrorException(null, 40 null, errorCode, null); 41 assertTrue(e.getMessage() == null && e.getSQLState() == null 42 && e.getCause() == null && e.getErrorCode() == errorCode); 43 } 44 45 /** 46 * Create SQLSyntaxErrorException with no-arg constructor 47 */ 48 @Test 49 public void test1() { 50 SQLSyntaxErrorException ex = new SQLSyntaxErrorException(); 51 assertTrue(ex.getMessage() == null 52 && ex.getSQLState() == null 143 /** 144 * Create SQLSyntaxErrorException with Throwable 145 */ 146 @Test 147 public void test9() { 148 SQLSyntaxErrorException ex = new SQLSyntaxErrorException(t); 149 assertTrue(ex.getMessage().equals(cause) 150 && ex.getSQLState() == null 151 && cause.equals(ex.getCause().toString()) 152 && ex.getErrorCode() == 0); 153 } 154 155 /** 156 * Serialize a SQLSyntaxErrorException and make sure you can read it back properly 157 */ 158 @Test 159 public void test10() throws Exception { 160 161 SQLSyntaxErrorException e = 162 new SQLSyntaxErrorException(reason, state, errorCode, t); 163 SQLSyntaxErrorException ex1 = 164 createSerializedException(e); 165 assertTrue(reason.equals(ex1.getMessage()) 166 && ex1.getSQLState().equals(state) 167 && cause.equals(ex1.getCause().toString()) 168 && ex1.getErrorCode() == errorCode); 169 } 170 171 /** 172 * Validate that the ordering of the returned Exceptions is correct 173 * using for-each loop 174 */ 175 @Test 176 public void test11() { 177 SQLSyntaxErrorException ex = new SQLSyntaxErrorException("Exception 1", t1); 178 SQLSyntaxErrorException ex1 = new SQLSyntaxErrorException("Exception 2"); 179 SQLSyntaxErrorException ex2 = new SQLSyntaxErrorException("Exception 3", t2); 180 ex.setNextException(ex1); 181 ex.setNextException(ex2); 182 int num = 0; 183 for (Throwable e : ex) { 184 assertTrue(msgs[num++].equals(e.getMessage())); |