KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > derbyTesting > functionTests > tests > jdbcapi > testRelative


1 /*
2
3    Derby - Class org.apache.derbyTesting.functionTests.tests.jdbcapi.testRelative
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.jdbcapi;
23
24
25 import java.sql.*;
26
27 import org.apache.derby.tools.ij;
28 import org.apache.derby.tools.JDBCDisplayUtil;
29 import org.apache.derbyTesting.functionTests.util.TestUtil;
30
31 public class testRelative {
32     
33     static final String JavaDoc NO_CURRENT_ROW_SQL_STATE =
34         (TestUtil.isNetFramework() ?
35          "XJ121" : "24000");
36    
37    public static void main(String JavaDoc[] args) {
38        System.out.println("Test testRelative starting");
39        Connection con = null;
40        try {
41            // use the ij utility to read the property file and
42
// make the initial connection.
43
ij.getPropertyArg(args);
44            con = ij.startJBMS();
45            test1(con);
46        } catch (Exception JavaDoc e)
47        {
48            unexpectedException(e);
49        }
50     }
51     
52     public static void test1(Connection con) {
53         ResultSet rs = null;
54         PreparedStatement pStmt = null;
55         Statement stmt1 = null;
56         String JavaDoc returnValue = null;
57         
58         try
59         {
60             con.setAutoCommit(false);
61             pStmt = con.prepareStatement("create table testRelative(name varchar(10), i int)");
62             pStmt.executeUpdate();
63             con.commit();
64             
65             pStmt = con.prepareStatement("insert into testRelative values (?,?)");
66                         
67             pStmt.setString(1,"work1");
68             pStmt.setNull(2,1);
69             pStmt.addBatch();
70             
71             pStmt.setString(1,"work2");
72             pStmt.setNull(2,2);
73             pStmt.addBatch();
74             
75             pStmt.setString(1,"work3");
76             pStmt.setNull(2,3);
77             pStmt.addBatch();
78             
79             pStmt.setString(1,"work4");
80             pStmt.setNull(2,4);
81             pStmt.addBatch();
82
83         
84             pStmt.executeBatch();
85             con.commit();
86         } catch(SQLException se) {
87             unexpectedSQLException(se);
88         } catch(Throwable JavaDoc t) {
89             System.out.println("FAIL--unexpected exception: "+t.getMessage());
90             t.printStackTrace(System.out);
91         }
92         try {
93             testScrolling(ResultSet.CONCUR_READ_ONLY, con);
94             testScrolling(ResultSet.CONCUR_UPDATABLE, con);
95         } catch(Throwable JavaDoc e) {
96             System.out.println("FAIL -- unexpected exception: "+e.getMessage());
97             e.printStackTrace(System.out);
98             
99         }
100     }
101
102     private static void testScrolling(int concurrency, Connection con)
103         throws SQLException
104     {
105         Statement stmt1 = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, concurrency);
106         ResultSet rs = stmt1.executeQuery("select * from testRelative");
107         
108         rs.next(); // First Record
109
System.out.println("Value = " + rs.getString("name"));
110         
111         rs.relative(2);
112         System.out.println("Value = " + rs.getString("name"));
113         System.out.println("isFirst = " + rs.isFirst() +
114                            " isLast = " + rs.isLast() +
115                            " isAfterLast = " + rs.isAfterLast());
116         rs.relative(-2);
117         System.out.println("Value = " + rs.getString("name"));
118         
119         try {
120             rs.relative(10);
121             System.out.println("Value = " + rs.getString("name"));
122             System.out.println("isFirst = " + rs.isFirst() +
123                                " isLast = " + rs.isLast() +
124                                " isAfterLast = " + rs.isAfterLast());
125         } catch(SQLException sqle) {
126             
127             expectedException(sqle, NO_CURRENT_ROW_SQL_STATE);
128         }
129     }
130      
131       /**
132        * Print the expected Exception's details if the SQLException SQLState
133        * matches the expected SQLState. Otherwise fail
134        *
135        * @param se SQLException that was thrown by the test
136        * @param expectedSQLState The SQLState that we expect.
137        *
138        **/

139     static private void expectedException (SQLException se, String JavaDoc expectedSQLState) {
140            if( se.getSQLState() != null && (se.getSQLState().equals(expectedSQLState))) {
141                 System.out.println("PASS -- expected exception");
142             } else {
143             System.out.println("FAIL--Unexpected SQLException: " +
144                                "SQLSTATE(" +se.getSQLState() + ")" +
145                                se.getMessage());
146             while (se != null) {
147                 System.out.println("SQLSTATE("+se.getSQLState()+"): "+se.getMessage());
148                 se.printStackTrace(System.out);
149                 se = se.getNextException();
150             }
151              
152         }
153     }
154
155      /**
156        * We are here because we got an exception when did not expect one.
157        * Hence printing the message and stack trace here.
158        **/

159      static private void unexpectedSQLException(SQLException se) {
160      System.out.println("FAIL -- Unexpected Exception: "+
161                         "SQLSTATE(" +se.getSQLState() +")" +
162                         se.getMessage());
163      se.printStackTrace(System.out);
164      }
165
166     static private void unexpectedException(Exception JavaDoc e) {
167         System.out.println("FAIL -- Unexpected Exception: "+
168                            e.getMessage());
169     }
170     
171 }
172
Popular Tags