001: /*
002:
003: Derby - Class
004: org.apache.derbyTesting.functionTests.tests.jdbc4.TestJDBC40Exception
005:
006: Licensed to the Apache Software Foundation (ASF) under one or more
007: contributor license agreements. See the NOTICE file distributed with
008: this work for additional information regarding copyright ownership.
009: The ASF licenses this file to you under the Apache License, Version 2.0
010: (the "License"); you may not use this file except in compliance with
011: the License. You may obtain a copy of the License at
012:
013: http://www.apache.org/licenses/LICENSE-2.0
014:
015: Unless required by applicable law or agreed to in writing, software
016: distributed under the License is distributed on an "AS IS" BASIS,
017: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
018: See the License for the specific language governing permissions and
019: limitations under the License.
020:
021: */
022:
023: package org.apache.derbyTesting.functionTests.tests.jdbc4;
024:
025: import java.sql.Connection;
026: import java.sql.Statement;
027: import java.sql.SQLDataException;
028: import java.sql.SQLException;
029: import java.sql.SQLIntegrityConstraintViolationException;
030: import java.sql.SQLSyntaxErrorException;
031: import java.sql.SQLTransientConnectionException;
032: import java.sql.SQLTransactionRollbackException;
033: import junit.framework.Test;
034: import junit.framework.TestResult;
035: import junit.framework.TestSuite;
036: import org.apache.derby.tools.ij;
037: import org.apache.derbyTesting.functionTests.tests.derbynet.testconnection;
038:
039: public class TestJDBC40Exception {
040:
041: private static final String EXCEPTION_TABLE1 = "EXCEPTION_TABLE1";
042:
043: private static String[] _startupArgs;
044:
045: public TestJDBC40Exception() {
046: }
047:
048: /*
049: * Stub methods to be removed after 623 is fixed and test is
050: * moved to use junit
051: */
052: private Connection getConnection() throws Exception {
053: // use the ij utility to read the property file and
054: // make the initial connection.
055: ij.getPropertyArg(_startupArgs);
056:
057: Connection conn_main = ij.startJBMS();
058:
059: return conn_main;
060: }
061:
062: /*
063: * Stub methods to be removed after 623 is fixed and test is
064: * moved to use junit
065: */
066: private void close(Connection conn) throws SQLException {
067: conn.close();
068: }
069:
070: /*
071: * Stub methods to be removed after 623 is fixed and test is
072: * moved to use junit
073: */
074: private void execute(Connection conn, String sql)
075: throws SQLException {
076: Statement stmt = conn.createStatement();
077: stmt.execute(sql);
078: stmt.close();
079: }
080:
081: public static Test suite() {
082: TestSuite testSuite = new TestSuite();
083: testSuite.addTestSuite(TestJDBC40Exception.class);
084: return testSuite;
085: }
086:
087: public void testException() throws Exception {
088: Connection conn = getConnection();
089: execute(conn, "create table " + EXCEPTION_TABLE1
090: + "(id integer " + "primary key, data varchar (5))");
091: execute(conn, "insert into " + EXCEPTION_TABLE1 + "(id, data)"
092: + "values (1, 'data1')");
093: close(conn);
094: checkDataException();
095: checkIntegrityConstraintViolationException();
096: checkSyntaxErrorException();
097: checkConnectionException();
098: checkTimeout();
099: }
100:
101: private void checkIntegrityConstraintViolationException()
102: throws Exception {
103: Connection conn = getConnection();
104: try {
105: execute(conn, "insert into " + EXCEPTION_TABLE1
106: + "(id, data)" + "values (1, 'data1')");
107: } catch (SQLIntegrityConstraintViolationException e) {
108: if (!e.getSQLState().startsWith("23"))
109: System.out.println("Unexpected SQL State"
110: + e.getSQLState());
111: }
112: }
113:
114: private void checkDataException() throws Exception {
115: Connection conn = getConnection();
116: try {
117: execute(conn, "insert into " + EXCEPTION_TABLE1
118: + "(id, data)" + "values (2, 'data1234556')");
119: } catch (SQLDataException e) {
120: if (!e.getSQLState().startsWith("22"))
121: System.out.println("Unexpected SQL State"
122: + e.getSQLState());
123: }
124: }
125:
126: private void checkConnectionException() throws Exception {
127: Statement stmt = null;
128: Connection con = null;
129: try {
130: con = getConnection();
131: stmt = con.createStatement();
132: con.close();
133: stmt.execute("select * from exception1");
134: } catch (SQLTransientConnectionException cone) {
135: if (!cone.getSQLState().startsWith("08"))
136: System.out.println("Unexpected SQL State"
137: + cone.getSQLState());
138: }
139: }
140:
141: private void checkSyntaxErrorException() throws Exception {
142: Connection conn = getConnection();
143: try {
144: execute(conn, "insert into " + EXCEPTION_TABLE1
145: + "(id, data)" + "values ('2', 'data1')");
146: } catch (SQLSyntaxErrorException e) {
147: if (!e.getSQLState().startsWith("42"))
148: System.out.println("Unexpected SQL State"
149: + e.getSQLState());
150: }
151: }
152:
153: private void checkTimeout() throws Exception {
154: Connection con1 = getConnection();
155: Connection con2 = getConnection();
156: try {
157: con1.setAutoCommit(false);
158: con2.setAutoCommit(false);
159: con1
160: .setTransactionIsolation(Connection.TRANSACTION_SERIALIZABLE);
161: con2
162: .setTransactionIsolation(Connection.TRANSACTION_SERIALIZABLE);
163: execute(con1, "select * from " + EXCEPTION_TABLE1
164: + " for update");
165: execute(con2, "select * from " + EXCEPTION_TABLE1
166: + " for update");
167: } catch (SQLTransactionRollbackException e) {
168: if (!e.getSQLState().startsWith("40"))
169: System.out.println("Unexpected SQL State"
170: + e.getSQLState());
171: }
172: }
173:
174: public static void main(String[] args) throws Exception {
175: TestJDBC40Exception test = new TestJDBC40Exception();
176:
177: _startupArgs = args;
178: test.testException();
179: }
180: }
|