001: /*
002:
003: Derby - Class org.apache.derbyTesting.functionTests.tests.lang.AIjdbc
004:
005: Licensed to the Apache Software Foundation (ASF) under one or more
006: contributor license agreements. See the NOTICE file distributed with
007: this work for additional information regarding copyright ownership.
008: The ASF licenses this file to You under the Apache License, Version 2.0
009: (the "License"); you may not use this file except in compliance with
010: the License. You may obtain a copy of the License at
011:
012: http://www.apache.org/licenses/LICENSE-2.0
013:
014: Unless required by applicable law or agreed to in writing, software
015: distributed under the License is distributed on an "AS IS" BASIS,
016: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
017: See the License for the specific language governing permissions and
018: limitations under the License.
019:
020: */
021:
022: package org.apache.derbyTesting.functionTests.tests.lang;
023:
024: import java.sql.Connection;
025: import java.sql.DriverManager;
026: import java.sql.Statement;
027: import java.sql.PreparedStatement;
028: import java.sql.ResultSet;
029: import java.sql.ResultSetMetaData;
030: import java.sql.DatabaseMetaData;
031: import java.sql.SQLException;
032: import java.sql.SQLWarning;
033:
034: import org.apache.derby.tools.ij;
035: import org.apache.derby.tools.JDBCDisplayUtil;
036:
037: /**
038: Test execution of JDBC method, isAutoincrement.
039:
040: @author manish
041: */
042: public class AIjdbc {
043:
044: public static void main(String[] args) {
045: System.out.println("Test AIjdbc starting");
046: boolean passed = true;
047:
048: try {
049: Connection conn;
050:
051: // use the ij utility to read the property file and
052: // make the initial connection.
053: ij.getPropertyArg(args);
054: conn = ij.startJBMS();
055:
056: // create the table first.
057: passed = createTable(conn) && passed;
058:
059: // do a select from a base table.
060: passed = testSelect(conn) && passed;
061:
062: // do a select from a view.
063: passed = testSelectView(conn) && passed;
064: } catch (Throwable e) {
065: passed = false;
066: System.out.println("FAIL -- unexpected exception:");
067: JDBCDisplayUtil.ShowException(System.out, e);
068: }
069:
070: if (passed)
071: System.out.println("PASS");
072: else
073: System.out.println("FAIL");
074:
075: System.out.println("Test AIjdbc finished");
076: }
077:
078: private static boolean createTable(Connection conn)
079: throws SQLException {
080: Statement s;
081: boolean passed = true;
082:
083: System.out.println("Test AIjdbc:creating objects");
084:
085: try {
086: s = conn.createStatement();
087: s
088: .execute("create table tab1 (x int, y int generated always as identity,z char(2))");
089: s
090: .execute("create view tab1_view (a,b) as select y,y+1 from tab1");
091: } catch (SQLException se) {
092: passed = false;
093: JDBCDisplayUtil.ShowSQLException(System.out, se);
094: }
095:
096: return passed;
097: }
098:
099: private static boolean testSelect(Connection conn) {
100: Statement s;
101: boolean passed = true;
102:
103: System.out.println("Test AIjdbc:select from base table");
104:
105: try {
106: s = conn.createStatement();
107: ResultSet rs = s.executeQuery("select x,z from tab1");
108: ResultSetMetaData rsmd = rs.getMetaData();
109:
110: if (rsmd.getColumnCount() != 2)
111: throw new SQLException("column count doesn't match");
112: if (rsmd.isAutoIncrement(1))
113: throw new SQLException("column 1 is NOT ai!");
114: if (rsmd.isAutoIncrement(2))
115: throw new SQLException("column 2 is NOT ai!");
116: rs.close();
117:
118: rs = s.executeQuery("select y, x,z from tab1");
119: rsmd = rs.getMetaData();
120: if (rsmd.getColumnCount() != 3)
121: throw new SQLException("column count doesn't match");
122: if (!rsmd.isAutoIncrement(1))
123: throw new SQLException("column 1 IS ai!");
124: if (rsmd.isAutoIncrement(2))
125: throw new SQLException("column 2 is NOT ai!");
126: if (rsmd.isAutoIncrement(3))
127: throw new SQLException("column 2 is NOT ai!");
128: rs.close();
129: } catch (SQLException se) {
130: passed = false;
131: JDBCDisplayUtil.ShowSQLException(System.out, se);
132:
133: }
134: return passed;
135: }
136:
137: private static boolean testSelectView(Connection conn) {
138: boolean passed = true;
139: System.out.println("Test AIjdbc:select from view");
140:
141: try {
142: Statement s;
143: s = conn.createStatement();
144: ResultSet rs = s.executeQuery("select * from tab1_view");
145: ResultSetMetaData rsmd = rs.getMetaData();
146:
147: if (rsmd.getColumnCount() != 2)
148: throw new SQLException("column count doesn't match");
149: if (!rsmd.isAutoIncrement(1))
150: throw new SQLException("column 1 IS ai!");
151: if (rsmd.isAutoIncrement(2))
152: throw new SQLException("column 1 is NOT ai!");
153: } catch (SQLException sqle) {
154: passed = false;
155: JDBCDisplayUtil.ShowSQLException(System.out, sqle);
156: }
157: return passed;
158: }
159: }
|