1 33 package smallsql.junit; 34 35 import junit.framework.*; 36 import java.sql.*; 37 38 public class TestExceptions extends BasicTestCase { 39 40 private TestValue testValue; 41 private static boolean init; 42 43 private static final int SYNTAX = 1; 44 private static final int RUNTIME= 2; 45 46 private static final TestValue[] TESTS = new TestValue[]{ 47 a( "01000", 0, SYNTAX, "SELECT 23 FROM"), a( "01000", 0, SYNTAX, "SELECT c FROM exceptions Group By i"), a( "01000", 0, SYNTAX, "SELECT first(c) FROM exceptions Group By i ORDER by c"), a( "01000", 0, SYNTAX, "SELECT 1 ORDER BY substring('qwert', 2, -3)"), a( "01000", 0, RUNTIME, "SELECT abs('abc')"), }; 54 55 56 TestExceptions(TestValue testValue){ 57 super(testValue.sql); 58 this.testValue = testValue; 59 } 60 61 62 private void init() throws Exception { 63 if(init) return; 64 Connection con = AllTests.getConnection(); 65 Statement st = con.createStatement(); 66 dropTable( con, "exceptions"); 67 st.execute("Create Table exceptions (c varchar(30), i int)"); 68 init = true; 69 } 70 71 72 public void runTest() throws Exception { 73 init(); 74 Connection con = AllTests.getConnection(); 75 Statement st = con.createStatement(); 76 ResultSet rs = null; 77 try{ 78 rs = st.executeQuery( testValue.sql ); 79 }catch(SQLException sqle){ 80 assertTrue( "There should no Syntaxerror", SYNTAX == testValue.errorType); 81 assertSQLException( testValue.sqlstate, testValue.errorCode, sqle ); 82 } 83 if(testValue.errorType == SYNTAX){ 84 assertNull("There should be a Syntaxerrror", rs); 85 return; 86 } 87 try{ 88 while(rs.next()){ 89 for(int i=1; i<rs.getMetaData().getColumnCount(); i++){ 90 rs.getObject(i); 91 } 92 } 93 }catch(SQLException sqle){ 94 assertSQLException( testValue.sqlstate, testValue.errorCode, sqle ); 95 } 96 } 97 98 99 public static Test suite() throws Exception { 100 TestSuite theSuite = new TestSuite("Exceptions"); 101 for(int i=0; i<TESTS.length; i++){ 102 theSuite.addTest(new TestExceptions( TESTS[i] ) ); 103 } 104 return theSuite; 105 } 106 107 108 private static TestValue a(String sqlstate, int errorCode, int errorType, String sql ){ 109 TestValue value = new TestValue(); 110 value.sql = sql; 111 value.sqlstate = sqlstate; 112 value.errorCode = errorCode; 113 value.errorType = errorType; 114 return value; 115 } 116 117 118 private static class TestValue{ 119 String sql; 120 String sqlstate; 121 int errorCode; 122 int errorType; 123 } 124 125 } | Popular Tags |