KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > derbyTesting > functionTests > tests > jdbc4 > TestJDBC40Exception


1 /*
2  
3    Derby - Class
4         org.apache.derbyTesting.functionTests.tests.jdbc4.TestJDBC40Exception
5  
6    Licensed to the Apache Software Foundation (ASF) under one or more
7    contributor license agreements. See the NOTICE file distributed with
8    this work for additional information regarding copyright ownership.
9    The ASF licenses this file to you under the Apache License, Version 2.0
10    (the "License"); you may not use this file except in compliance with
11    the License. You may obtain a copy of the License at
12  
13       http://www.apache.org/licenses/LICENSE-2.0
14  
15    Unless required by applicable law or agreed to in writing, software
16    distributed under the License is distributed on an "AS IS" BASIS,
17    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18    See the License for the specific language governing permissions and
19    limitations under the License.
20  
21  */

22
23 package org.apache.derbyTesting.functionTests.tests.jdbc4;
24
25 import java.sql.Connection JavaDoc;
26 import java.sql.Statement JavaDoc;
27 import java.sql.SQLDataException JavaDoc;
28 import java.sql.SQLException JavaDoc;
29 import java.sql.SQLIntegrityConstraintViolationException JavaDoc;
30 import java.sql.SQLSyntaxErrorException JavaDoc;
31 import java.sql.SQLTransientConnectionException JavaDoc;
32 import java.sql.SQLTransactionRollbackException JavaDoc;
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 JavaDoc EXCEPTION_TABLE1 = "EXCEPTION_TABLE1";
42
43     private static String JavaDoc[] _startupArgs;
44     
45     public TestJDBC40Exception() {
46     }
47     
48     /*
49      * Stub methods to be removed after 623 is fixed and test is
50      * moved to use junit
51      */

52     private Connection JavaDoc getConnection () throws Exception JavaDoc {
53         // use the ij utility to read the property file and
54
// make the initial connection.
55
ij.getPropertyArg( _startupArgs );
56         
57         Connection JavaDoc conn_main = ij.startJBMS();
58         
59         return conn_main;
60     }
61     
62     /*
63      * Stub methods to be removed after 623 is fixed and test is
64      * moved to use junit
65      */

66     private void close (Connection JavaDoc conn) throws SQLException JavaDoc {
67         conn.close ();
68     }
69
70     /*
71      * Stub methods to be removed after 623 is fixed and test is
72      * moved to use junit
73      */

74     private void execute (Connection JavaDoc conn, String JavaDoc sql) throws SQLException JavaDoc {
75         Statement JavaDoc 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 JavaDoc{
88         Connection JavaDoc 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 JavaDoc {
102         Connection JavaDoc conn = getConnection();
103         try {
104             execute(conn, "insert into " + EXCEPTION_TABLE1 + "(id, data)" +
105                     "values (1, 'data1')");
106         } catch (SQLIntegrityConstraintViolationException JavaDoc 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 JavaDoc{
113         Connection JavaDoc conn = getConnection();
114         try {
115             execute(conn, "insert into " + EXCEPTION_TABLE1 + "(id, data)" +
116                     "values (2, 'data1234556')");
117         } catch (SQLDataException JavaDoc 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 JavaDoc {
124         Statement JavaDoc stmt = null;
125         Connection JavaDoc con = null;
126         try {
127             con = getConnection();
128             stmt = con.createStatement();
129             con.close();
130             stmt.execute("select * from exception1");
131         } catch (SQLTransientConnectionException JavaDoc 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 JavaDoc{
138         Connection JavaDoc conn = getConnection();
139         try {
140             execute(conn, "insert into " + EXCEPTION_TABLE1 + "(id, data)" +
141                     "values ('2', 'data1')");
142         } catch (SQLSyntaxErrorException JavaDoc 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 JavaDoc {
149         Connection JavaDoc con1 = getConnection();
150         Connection JavaDoc 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 JavaDoc 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 JavaDoc [] args) throws Exception JavaDoc {
166         TestJDBC40Exception test = new TestJDBC40Exception ();
167
168         _startupArgs = args;
169         test.testException ();
170     }
171 }
172
Popular Tags