KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > smallsql > junit > TestExceptions


1 /* =============================================================
2  * SmallSQL : a free Java DBMS library for the Java(tm) platform
3  * =============================================================
4  *
5  * (C) Copyright 2004-2006, by Volker Berlin.
6  *
7  * Project Info: http://www.smallsql.de/
8  *
9  * This library is free software; you can redistribute it and/or modify it
10  * under the terms of the GNU Lesser General Public License as published by
11  * the Free Software Foundation; either version 2.1 of the License, or
12  * (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful, but
15  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
16  * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
17  * License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with this library; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
22  * USA.
23  *
24  * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
25  * in the United States and other countries.]
26  *
27  * ---------------
28  * TestExceptions.java
29  * ---------------
30  * Author: Volker Berlin
31  *
32  */

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"), // missing table
48
a( "01000", 0, SYNTAX, "SELECT c FROM exceptions Group By i"), //c is not in group by
49
a( "01000", 0, SYNTAX, "SELECT first(c) FROM exceptions Group By i ORDER by c"), //c is not in group by
50
a( "01000", 0, SYNTAX, "SELECT 1 ORDER BY substring('qwert', 2, -3)"), //invalid length
51
a( "01000", 0, RUNTIME, "SELECT abs('abc')"), //Unsupported datatype conversion
52
//FIXME getXXX auf Spalte die nicht existiert
53
};
54     
55
56     TestExceptions(TestValue testValue){
57         super(testValue.sql);
58         this.testValue = testValue;
59     }
60     
61
62     private void init() throws Exception JavaDoc{
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 JavaDoc{
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 JavaDoc{
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 JavaDoc sqlstate, int errorCode, int errorType, String JavaDoc 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 JavaDoc sql;
120         String JavaDoc sqlstate;
121         int errorCode;
122         int errorType;
123     }
124
125 }
Popular Tags