1 25 package testsuite.simple; 26 27 import java.sql.ResultSet ; 28 import java.sql.SQLException ; 29 import java.sql.Statement ; 30 31 import testsuite.BaseTestCase; 32 33 39 public class TraversalTest extends BaseTestCase { 40 41 43 49 public TraversalTest(String name) { 50 super(name); 51 } 52 53 55 60 public static void main(String [] args) { 61 junit.textui.TestRunner.run(TraversalTest.class); 62 } 63 64 70 public void setUp() throws Exception { 71 super.setUp(); 72 createTestTable(); 73 } 74 75 81 public void testTraversal() throws SQLException { 82 83 Statement 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 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 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 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 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 this.rs.absolute(50); 170 int pos = this.rs.getInt("pos"); 171 assertTrue("ResultSet.absolute() failed", pos == 49); 172 173 this.rs.relative(-1); 175 pos = this.rs.getInt("pos"); 176 assertTrue("ResultSet.relative(-1) failed", pos == 48); 177 178 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 sqlEx) { 197 ; 198 } 199 } 200 } 201 } 202 203 private void createTestTable() throws SQLException { 204 205 try { 209 this.stmt.executeUpdate("DROP TABLE TRAVERSAL"); 210 } catch (SQLException 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 |