KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > derbyTesting > functionTests > tests > lang > AIjdbc


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

21
22 package org.apache.derbyTesting.functionTests.tests.lang;
23
24 import java.sql.Connection JavaDoc;
25 import java.sql.DriverManager JavaDoc;
26 import java.sql.Statement JavaDoc;
27 import java.sql.PreparedStatement JavaDoc;
28 import java.sql.ResultSet JavaDoc;
29 import java.sql.ResultSetMetaData JavaDoc;
30 import java.sql.DatabaseMetaData JavaDoc;
31 import java.sql.SQLException JavaDoc;
32 import java.sql.SQLWarning JavaDoc;
33
34 import org.apache.derby.tools.ij;
35 import org.apache.derby.tools.JDBCDisplayUtil;
36
37 /**
38     Test execution of JDBC method, isAutoincrement.
39
40     @author manish
41  */

42 public class AIjdbc
43 {
44
45     public static void main(String JavaDoc[] args) {
46         System.out.println("Test AIjdbc starting");
47         boolean passed = true;
48
49         try
50         {
51             Connection JavaDoc conn;
52
53             // use the ij utility to read the property file and
54
// make the initial connection.
55
ij.getPropertyArg(args);
56             conn = ij.startJBMS();
57             
58             // create the table first.
59
passed = createTable(conn) && passed;
60
61             // do a select from a base table.
62
passed = testSelect(conn) && passed;
63
64             // do a select from a view.
65
passed = testSelectView(conn) && passed;
66         }
67         catch (Throwable JavaDoc e)
68         {
69             passed = false;
70             System.out.println("FAIL -- unexpected exception:");
71             JDBCDisplayUtil.ShowException(System.out, e);
72         }
73
74         if (passed)
75             System.out.println("PASS");
76         else
77             System.out.println("FAIL");
78         
79         System.out.println("Test AIjdbc finished");
80     }
81     
82     private static boolean createTable(Connection JavaDoc conn) throws SQLException JavaDoc
83     {
84         Statement JavaDoc s;
85         boolean passed = true;
86
87         System.out.println("Test AIjdbc:creating objects");
88
89         try
90         {
91             s = conn.createStatement();
92             s.execute("create table tab1 (x int, y int generated always as identity,z char(2))");
93             s.execute("create view tab1_view (a,b) as select y,y+1 from tab1");
94         }
95         catch (SQLException JavaDoc se)
96         {
97             passed = false;
98             JDBCDisplayUtil.ShowSQLException(System.out,se);
99         }
100         
101         return passed;
102     }
103
104     private static boolean testSelect(Connection JavaDoc conn)
105     {
106         Statement JavaDoc s;
107         boolean passed = true;
108
109         System.out.println("Test AIjdbc:select from base table");
110
111         try
112         {
113             s = conn.createStatement();
114             ResultSet JavaDoc rs = s.executeQuery("select x,z from tab1");
115             ResultSetMetaData JavaDoc rsmd = rs.getMetaData();
116             
117             if (rsmd.getColumnCount() != 2)
118                 throw new SQLException JavaDoc("column count doesn't match");
119             if (rsmd.isAutoIncrement(1))
120                 throw new SQLException JavaDoc("column 1 is NOT ai!");
121             if (rsmd.isAutoIncrement(2))
122                 throw new SQLException JavaDoc("column 2 is NOT ai!");
123             rs.close();
124
125             rs = s.executeQuery("select y, x,z from tab1");
126             rsmd = rs.getMetaData();
127             if (rsmd.getColumnCount() != 3)
128                 throw new SQLException JavaDoc("column count doesn't match");
129             if (!rsmd.isAutoIncrement(1))
130                 throw new SQLException JavaDoc("column 1 IS ai!");
131             if (rsmd.isAutoIncrement(2))
132                 throw new SQLException JavaDoc("column 2 is NOT ai!");
133             if (rsmd.isAutoIncrement(3))
134                 throw new SQLException JavaDoc("column 2 is NOT ai!");
135             rs.close();
136         }
137         catch (SQLException JavaDoc se)
138         {
139             passed = false;
140             JDBCDisplayUtil.ShowSQLException(System.out,se);
141
142         }
143         return passed;
144     }
145
146     private static boolean testSelectView(Connection JavaDoc conn)
147     {
148         boolean passed = true;
149         System.out.println("Test AIjdbc:select from view");
150
151         try
152         {
153             Statement JavaDoc s;
154             s = conn.createStatement();
155             ResultSet JavaDoc rs = s.executeQuery("select * from tab1_view");
156             ResultSetMetaData JavaDoc rsmd = rs.getMetaData();
157             
158             if (rsmd.getColumnCount() != 2)
159                 throw new SQLException JavaDoc("column count doesn't match");
160             if (!rsmd.isAutoIncrement(1))
161                 throw new SQLException JavaDoc("column 1 IS ai!");
162             if (rsmd.isAutoIncrement(2))
163                 throw new SQLException JavaDoc("column 1 is NOT ai!");
164         }
165         catch (SQLException JavaDoc sqle)
166         {
167             passed = false;
168             JDBCDisplayUtil.ShowSQLException(System.out,sqle);
169         }
170         return passed;
171     }
172 }
173
Popular Tags