KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > testsuite > simple > TraversalTest


1 /*
2  Copyright (C) 2002-2004 MySQL AB
3
4  This program is free software; you can redistribute it and/or modify
5  it under the terms of version 2 of the GNU General Public License as
6  published by the Free Software Foundation.
7
8  There are special exceptions to the terms and conditions of the GPL
9  as it is applied to this software. View the full text of the
10  exception in file EXCEPTIONS-CONNECTOR-J in the directory of this
11  software distribution.
12
13  This program is distributed in the hope that it will be useful,
14  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  GNU General Public License for more details.
17
18  You should have received a copy of the GNU General Public License
19  along with this program; if not, write to the Free Software
20  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21
22
23  
24  */

25 package testsuite.simple;
26
27 import java.sql.ResultSet JavaDoc;
28 import java.sql.SQLException JavaDoc;
29 import java.sql.Statement JavaDoc;
30
31 import testsuite.BaseTestCase;
32
33 /**
34  * Tests result set traversal methods.
35  *
36  * @author Mark Matthews
37  * @version $Id: TraversalTest.java,v 1.1.2.2 2005/05/19 15:52:24 mmatthews Exp $
38  */

39 public class TraversalTest extends BaseTestCase {
40
41     // ~ Constructors ..........................................................
42

43     /**
44      * Creates a new TraversalTest object.
45      *
46      * @param name
47      * DOCUMENT ME!
48      */

49     public TraversalTest(String JavaDoc name) {
50         super(name);
51     }
52
53     // ~ Methods ...............................................................
54

55     /**
56      * Runs all test cases in this test suite
57      *
58      * @param args
59      */

60     public static void main(String JavaDoc[] args) {
61         junit.textui.TestRunner.run(TraversalTest.class);
62     }
63
64     /**
65      * DOCUMENT ME!
66      *
67      * @throws Exception
68      * DOCUMENT ME!
69      */

70     public void setUp() throws Exception JavaDoc {
71         super.setUp();
72         createTestTable();
73     }
74
75     /**
76      * DOCUMENT ME!
77      *
78      * @throws SQLException
79      * DOCUMENT ME!
80      */

81     public void testTraversal() throws SQLException JavaDoc {
82
83         Statement JavaDoc scrollableStmt = null;
84
85         try {
86             scrollableStmt = this.conn
87                     .createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
88                             ResultSet.CONCUR_READ_ONLY);
89             this.rs = scrollableStmt
90                     .executeQuery("SELECT * FROM TRAVERSAL ORDER BY pos");
91
92             // Test isFirst()
93
if (this.rs.first()) {
94                 assertTrue("ResultSet.isFirst() failed", this.rs.isFirst());
95                 this.rs.relative(-1);
96                 assertTrue("ResultSet.isBeforeFirst() failed", this.rs
97                         .isBeforeFirst());
98             }
99
100             // Test isLast()
101
if (this.rs.last()) {
102                 assertTrue("ResultSet.isLast() failed", this.rs.isLast());
103                 this.rs.relative(1);
104                 assertTrue("ResultSet.isAfterLast() failed", this.rs
105                         .isAfterLast());
106             }
107
108             int count = 0;
109             this.rs.beforeFirst();
110
111             boolean forwardOk = true;
112
113             while (this.rs.next()) {
114
115                 int pos = this.rs.getInt("POS");
116
117                 // test case-sensitive column names
118
pos = this.rs.getInt("pos");
119                 pos = this.rs.getInt("Pos");
120                 pos = this.rs.getInt("POs");
121                 pos = this.rs.getInt("PoS");
122                 pos = this.rs.getInt("pOS");
123                 pos = this.rs.getInt("pOs");
124                 pos = this.rs.getInt("poS");
125
126                 if (pos != count) {
127                     forwardOk = false;
128                 }
129
130                 assertTrue("ResultSet.getRow() failed.", pos == (this.rs
131                         .getRow() - 1));
132
133                 count++;
134
135             }
136
137             assertTrue("Only traversed " + count + " / 100 rows", forwardOk);
138
139             boolean isAfterLast = this.rs.isAfterLast();
140             assertTrue("ResultSet.isAfterLast() failed", isAfterLast);
141             this.rs.afterLast();
142
143             // Scroll backwards
144
count = 99;
145
146             boolean reverseOk = true;
147
148             while (this.rs.previous()) {
149
150                 int pos = this.rs.getInt("pos");
151
152                 if (pos != count) {
153                     reverseOk = false;
154                 }
155
156                 count--;
157             }
158
159             assertTrue("ResultSet.previous() failed", reverseOk);
160
161             boolean isBeforeFirst = this.rs.isBeforeFirst();
162             assertTrue("ResultSet.isBeforeFirst() failed", isBeforeFirst);
163
164             this.rs.next();
165             boolean isFirst = this.rs.isFirst();
166             assertTrue("ResultSet.isFirst() failed", isFirst);
167
168             // Test absolute positioning
169
this.rs.absolute(50);
170             int pos = this.rs.getInt("pos");
171             assertTrue("ResultSet.absolute() failed", pos == 49);
172
173             // Test relative positioning
174
this.rs.relative(-1);
175             pos = this.rs.getInt("pos");
176             assertTrue("ResultSet.relative(-1) failed", pos == 48);
177
178             // Test bogus absolute index
179
boolean onResultSet = this.rs.absolute(200);
180             assertTrue("ResultSet.absolute() to point off result set failed",
181                     onResultSet == false);
182             onResultSet = this.rs.absolute(100);
183             assertTrue(
184                     "ResultSet.absolute() from off this.rs to on this.rs failed",
185                     onResultSet);
186
187             onResultSet = this.rs.absolute(-99);
188             assertTrue("ResultSet.absolute(-99) failed", onResultSet);
189             assertTrue("ResultSet absolute(-99) failed", this.rs.getInt(1) == 1);
190         } finally {
191
192             if (scrollableStmt != null) {
193
194                 try {
195                     scrollableStmt.close();
196                 } catch (SQLException JavaDoc sqlEx) {
197                     ;
198                 }
199             }
200         }
201     }
202
203     private void createTestTable() throws SQLException JavaDoc {
204
205         //
206
// Catch the error, the table might exist
207
//
208
try {
209             this.stmt.executeUpdate("DROP TABLE TRAVERSAL");
210         } catch (SQLException JavaDoc SQLE) {
211             ;
212         }
213
214         this.stmt
215                 .executeUpdate("CREATE TABLE TRAVERSAL (pos int PRIMARY KEY, stringdata CHAR(32))");
216
217         for (int i = 0; i < 100; i++) {
218             this.stmt.executeUpdate("INSERT INTO TRAVERSAL VALUES (" + i
219                     + ", 'StringData')");
220         }
221     }
222 }
Popular Tags