1 22 23 package org.apache.derbyTesting.functionTests.tests.jdbc4; 24 25 import java.sql.Connection ; 26 import java.sql.Statement ; 27 import java.sql.SQLDataException ; 28 import java.sql.SQLException ; 29 import java.sql.SQLIntegrityConstraintViolationException ; 30 import java.sql.SQLSyntaxErrorException ; 31 import java.sql.SQLTransientConnectionException ; 32 import java.sql.SQLTransactionRollbackException ; 33 import junit.framework.Test; 34 import junit.framework.TestResult; 35 import junit.framework.TestSuite; 36 import org.apache.derby.tools.ij; 37 import org.apache.derbyTesting.functionTests.tests.derbynet.testconnection; 38 39 public class TestJDBC40Exception { 40 41 private static final String EXCEPTION_TABLE1 = "EXCEPTION_TABLE1"; 42 43 private static String [] _startupArgs; 44 45 public TestJDBC40Exception() { 46 } 47 48 52 private Connection getConnection () throws Exception { 53 ij.getPropertyArg( _startupArgs ); 56 57 Connection conn_main = ij.startJBMS(); 58 59 return conn_main; 60 } 61 62 66 private void close (Connection conn) throws SQLException { 67 conn.close (); 68 } 69 70 74 private void execute (Connection conn, String sql) throws SQLException { 75 Statement stmt = conn.createStatement (); 76 stmt.execute (sql); 77 stmt.close (); 78 } 79 80 public static Test suite() { 81 TestSuite testSuite = new TestSuite(); 82 testSuite.addTestSuite(TestJDBC40Exception.class); 83 return testSuite; 84 } 85 86 87 public void testException() throws Exception { 88 Connection conn = getConnection(); 89 execute(conn, "create table " + EXCEPTION_TABLE1 + "(id integer " + 90 "primary key, data varchar (5))"); 91 execute(conn, "insert into " + EXCEPTION_TABLE1 + "(id, data)" + 92 "values (1, 'data1')"); 93 close(conn); 94 checkDataException(); 95 checkIntegrityConstraintViolationException(); 96 checkSyntaxErrorException(); 97 checkConnectionException(); 98 checkTimeout(); 99 } 100 101 private void checkIntegrityConstraintViolationException() throws Exception { 102 Connection conn = getConnection(); 103 try { 104 execute(conn, "insert into " + EXCEPTION_TABLE1 + "(id, data)" + 105 "values (1, 'data1')"); 106 } catch (SQLIntegrityConstraintViolationException e) { 107 if (!e.getSQLState().startsWith ("23")) 108 System.out.println ("Unexpected SQL State" + e.getSQLState()); 109 } 110 } 111 112 private void checkDataException() throws Exception { 113 Connection conn = getConnection(); 114 try { 115 execute(conn, "insert into " + EXCEPTION_TABLE1 + "(id, data)" + 116 "values (2, 'data1234556')"); 117 } catch (SQLDataException e) { 118 if (!e.getSQLState().startsWith ("22")) 119 System.out.println ("Unexpected SQL State" + e.getSQLState()); 120 } 121 } 122 123 private void checkConnectionException() throws Exception { 124 Statement stmt = null; 125 Connection con = null; 126 try { 127 con = getConnection(); 128 stmt = con.createStatement(); 129 con.close(); 130 stmt.execute("select * from exception1"); 131 } catch (SQLTransientConnectionException cone) { 132 if (!cone.getSQLState().startsWith ("08")) 133 System.out.println ("Unexpected SQL State" + cone.getSQLState()); 134 } 135 } 136 137 private void checkSyntaxErrorException() throws Exception { 138 Connection conn = getConnection(); 139 try { 140 execute(conn, "insert into " + EXCEPTION_TABLE1 + "(id, data)" + 141 "values ('2', 'data1')"); 142 } catch (SQLSyntaxErrorException e) { 143 if (!e.getSQLState().startsWith ("42")) 144 System.out.println ("Unexpected SQL State" + e.getSQLState()); 145 } 146 } 147 148 private void checkTimeout() throws Exception { 149 Connection con1 = getConnection(); 150 Connection con2 = getConnection(); 151 try { 152 con1.setAutoCommit(false); 153 con2.setAutoCommit(false); 154 con1.setTransactionIsolation(Connection.TRANSACTION_SERIALIZABLE); 155 con2.setTransactionIsolation(Connection.TRANSACTION_SERIALIZABLE); 156 execute(con1, "select * from " + EXCEPTION_TABLE1 + " for update"); 157 execute(con2, "select * from " + EXCEPTION_TABLE1 + " for update"); 158 } catch (SQLTransactionRollbackException e) { 159 if (!e.getSQLState().startsWith ("40")) 160 System.out.println ("Unexpected SQL State" + e.getSQLState()); 161 } 162 } 163 164 165 public static void main(String [] args) throws Exception { 166 TestJDBC40Exception test = new TestJDBC40Exception (); 167 168 _startupArgs = args; 169 test.testException (); 170 } 171 } 172 | Popular Tags |