001: /*
002: Copyright (C) 2002-2004 MySQL AB
003:
004: This program is free software; you can redistribute it and/or modify
005: it under the terms of version 2 of the GNU General Public License as
006: published by the Free Software Foundation.
007:
008: There are special exceptions to the terms and conditions of the GPL
009: as it is applied to this software. View the full text of the
010: exception in file EXCEPTIONS-CONNECTOR-J in the directory of this
011: software distribution.
012:
013: This program is distributed in the hope that it will be useful,
014: but WITHOUT ANY WARRANTY; without even the implied warranty of
015: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
016: GNU General Public License for more details.
017:
018: You should have received a copy of the GNU General Public License
019: along with this program; if not, write to the Free Software
020: Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
021:
022:
023:
024: */
025: package testsuite.simple;
026:
027: import java.sql.ResultSet;
028: import java.sql.SQLException;
029: import java.sql.Statement;
030:
031: import testsuite.BaseTestCase;
032:
033: /**
034: * Tests result set traversal methods.
035: *
036: * @author Mark Matthews
037: * @version $Id: TraversalTest.java 3726 2005-05-19 15:52:24Z mmatthews $
038: */
039: public class TraversalTest extends BaseTestCase {
040:
041: // ~ Constructors ..........................................................
042:
043: /**
044: * Creates a new TraversalTest object.
045: *
046: * @param name
047: * DOCUMENT ME!
048: */
049: public TraversalTest(String name) {
050: super (name);
051: }
052:
053: // ~ Methods ...............................................................
054:
055: /**
056: * Runs all test cases in this test suite
057: *
058: * @param args
059: */
060: public static void main(String[] args) {
061: junit.textui.TestRunner.run(TraversalTest.class);
062: }
063:
064: /**
065: * DOCUMENT ME!
066: *
067: * @throws Exception
068: * DOCUMENT ME!
069: */
070: public void setUp() throws Exception {
071: super .setUp();
072: createTestTable();
073: }
074:
075: /**
076: * DOCUMENT ME!
077: *
078: * @throws SQLException
079: * DOCUMENT ME!
080: */
081: public void testTraversal() throws SQLException {
082:
083: Statement scrollableStmt = null;
084:
085: try {
086: scrollableStmt = this .conn.createStatement(
087: ResultSet.TYPE_SCROLL_SENSITIVE,
088: ResultSet.CONCUR_READ_ONLY);
089: this .rs = scrollableStmt
090: .executeQuery("SELECT * FROM TRAVERSAL ORDER BY pos");
091:
092: // Test isFirst()
093: if (this .rs.first()) {
094: assertTrue("ResultSet.isFirst() failed", this .rs
095: .isFirst());
096: this .rs.relative(-1);
097: assertTrue("ResultSet.isBeforeFirst() failed", this .rs
098: .isBeforeFirst());
099: }
100:
101: // Test isLast()
102: if (this .rs.last()) {
103: assertTrue("ResultSet.isLast() failed", this .rs
104: .isLast());
105: this .rs.relative(1);
106: assertTrue("ResultSet.isAfterLast() failed", this .rs
107: .isAfterLast());
108: }
109:
110: int count = 0;
111: this .rs.beforeFirst();
112:
113: boolean forwardOk = true;
114:
115: while (this .rs.next()) {
116:
117: int pos = this .rs.getInt("POS");
118:
119: // test case-sensitive column names
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: pos = this .rs.getInt("pOs");
126: pos = this .rs.getInt("poS");
127:
128: if (pos != count) {
129: forwardOk = false;
130: }
131:
132: assertTrue("ResultSet.getRow() failed.",
133: pos == (this .rs.getRow() - 1));
134:
135: count++;
136:
137: }
138:
139: assertTrue("Only traversed " + count + " / 100 rows",
140: forwardOk);
141:
142: boolean isAfterLast = this .rs.isAfterLast();
143: assertTrue("ResultSet.isAfterLast() failed", isAfterLast);
144: this .rs.afterLast();
145:
146: // Scroll backwards
147: count = 99;
148:
149: boolean reverseOk = true;
150:
151: while (this .rs.previous()) {
152:
153: int pos = this .rs.getInt("pos");
154:
155: if (pos != count) {
156: reverseOk = false;
157: }
158:
159: count--;
160: }
161:
162: assertTrue("ResultSet.previous() failed", reverseOk);
163:
164: boolean isBeforeFirst = this .rs.isBeforeFirst();
165: assertTrue("ResultSet.isBeforeFirst() failed",
166: isBeforeFirst);
167:
168: this .rs.next();
169: boolean isFirst = this .rs.isFirst();
170: assertTrue("ResultSet.isFirst() failed", isFirst);
171:
172: // Test absolute positioning
173: this .rs.absolute(50);
174: int pos = this .rs.getInt("pos");
175: assertTrue("ResultSet.absolute() failed", pos == 49);
176:
177: // Test relative positioning
178: this .rs.relative(-1);
179: pos = this .rs.getInt("pos");
180: assertTrue("ResultSet.relative(-1) failed", pos == 48);
181:
182: // Test bogus absolute index
183: boolean onResultSet = this .rs.absolute(200);
184: assertTrue(
185: "ResultSet.absolute() to point off result set failed",
186: onResultSet == false);
187: onResultSet = this .rs.absolute(100);
188: assertTrue(
189: "ResultSet.absolute() from off this.rs to on this.rs failed",
190: onResultSet);
191:
192: onResultSet = this .rs.absolute(-99);
193: assertTrue("ResultSet.absolute(-99) failed", onResultSet);
194: assertTrue("ResultSet absolute(-99) failed", this .rs
195: .getInt(1) == 1);
196: } finally {
197:
198: if (scrollableStmt != null) {
199:
200: try {
201: scrollableStmt.close();
202: } catch (SQLException sqlEx) {
203: ;
204: }
205: }
206: }
207: }
208:
209: private void createTestTable() throws SQLException {
210:
211: //
212: // Catch the error, the table might exist
213: //
214: try {
215: this .stmt.executeUpdate("DROP TABLE TRAVERSAL");
216: } catch (SQLException SQLE) {
217: ;
218: }
219:
220: this .stmt
221: .executeUpdate("CREATE TABLE TRAVERSAL (pos int PRIMARY KEY, stringdata CHAR(32))");
222:
223: for (int i = 0; i < 100; i++) {
224: this .stmt.executeUpdate("INSERT INTO TRAVERSAL VALUES ("
225: + i + ", 'StringData')");
226: }
227: }
228: }
|