Changeset 2714
- Timestamp:
- 11/20/08 15:39:18 (7 weeks ago)
- Location:
- XerialJ/trunk/sqlite-jdbc/src/test/java/org/sqlite
- Files:
-
- 2 modified
-
StatementTest.java (modified) (21 diffs)
-
TransactionTest.java (modified) (14 diffs)
Legend:
- Unmodified
- Added
- Removed
-
XerialJ/trunk/sqlite-jdbc/src/test/java/org/sqlite/StatementTest.java
r2254 r2714 1 1 package org.sqlite; 2 2 3 import static org.junit.Assert.assertEquals; 4 import static org.junit.Assert.assertFalse; 5 import static org.junit.Assert.assertNotNull; 6 import static org.junit.Assert.assertNull; 7 import static org.junit.Assert.assertTrue; 8 9 import java.sql.BatchUpdateException; 10 import java.sql.Connection; 11 import java.sql.DriverManager; 12 import java.sql.ResultSet; 13 import java.sql.SQLException; 14 import java.sql.Statement; 15 16 import org.junit.After; 17 import org.junit.Before; 18 import org.junit.BeforeClass; 19 import org.junit.Ignore; 20 import org.junit.Test; 3 import java.sql.*; 4 import org.junit.*; 5 import static org.junit.Assert.*; 21 6 22 7 /** These tests are designed to stress Statements on memory databases. */ … … 26 11 private Statement stat; 27 12 28 @BeforeClass 29 public static void forName() throws Exception 30 { 13 @BeforeClass public static void forName() throws Exception { 31 14 Class.forName("org.sqlite.JDBC"); 32 15 } 33 16 34 @Before 35 public void connect() throws Exception 36 { 17 @Before public void connect() throws Exception { 37 18 conn = DriverManager.getConnection("jdbc:sqlite:"); 38 19 stat = conn.createStatement(); 39 20 } 40 21 41 @After 42 public void close() throws SQLException 43 { 22 @After public void close() throws SQLException { 44 23 stat.close(); 45 24 conn.close(); 46 25 } 47 26 48 @Test 49 public void stmtUpdate() throws SQLException 50 { 27 @Test public void stmtUpdate() throws SQLException { 51 28 assertEquals(stat.executeUpdate("create table s1 (c1);"), 0); 52 29 assertEquals(stat.executeUpdate("insert into s1 values (0);"), 1); … … 54 31 assertEquals(stat.executeUpdate("insert into s1 values (2);"), 1); 55 32 assertEquals(stat.executeUpdate("update s1 set c1 = 5;"), 3); 56 assertEquals(stat.executeUpdate("delete from s1;"), 0); 57 assertEquals(stat.executeUpdate("drop table s1;"), 0); 58 } 59 60 @Test 61 public void emptyRS() throws SQLException 62 { 33 // count_changes_pgrama. truncate_optimization 34 assertEquals(stat.executeUpdate("delete from s1;"), 3); 35 assertEquals(stat.executeUpdate("drop table s1;"), 3); 36 } 37 38 @Test public void emptyRS() throws SQLException { 63 39 ResultSet rs = stat.executeQuery("select null limit 0;"); 64 40 assertFalse(rs.next()); … … 66 42 } 67 43 68 @Test 69 public void singleRowRS() throws SQLException 70 { 44 @Test public void singleRowRS() throws SQLException { 71 45 ResultSet rs = stat.executeQuery("select " + Integer.MAX_VALUE + ";"); 72 46 assertTrue(rs.next()); 73 47 assertEquals(rs.getInt(1), Integer.MAX_VALUE); 74 48 assertEquals(rs.getString(1), Integer.toString(Integer.MAX_VALUE)); 75 assertEquals(rs.getDouble(1), new Integer(Integer.MAX_VALUE).doubleValue()); 76 assertFalse(rs.next()); 77 rs.close(); 78 } 79 80 @Test 81 public void twoRowRS() throws SQLException 82 { 49 assertEquals(rs.getDouble(1), 50 new Integer(Integer.MAX_VALUE).doubleValue()); 51 assertFalse(rs.next()); 52 rs.close(); 53 } 54 55 @Test public void twoRowRS() throws SQLException { 83 56 ResultSet rs = stat.executeQuery("select 9 union all select 7;"); 84 57 assertTrue(rs.next()); … … 90 63 } 91 64 92 @Test 93 public void autoClose() throws SQLException 94 { 65 @Test public void autoClose() throws SQLException { 95 66 conn.createStatement().executeQuery("select 1;"); 96 67 } 97 68 98 @Test 99 public void stringRS() throws SQLException 100 { 69 @Test public void stringRS() throws SQLException { 101 70 ResultSet rs = stat.executeQuery("select \"Russell\";"); 102 71 assertTrue(rs.next()); … … 106 75 } 107 76 108 @Test 109 public void execute() throws SQLException 110 { 77 @Test public void execute() throws SQLException { 111 78 assertTrue(stat.execute("select null;")); 112 79 ResultSet rs = stat.getResultSet(); … … 128 95 } 129 96 130 @Test 131 public void colNameAccess() throws SQLException 132 { 133 assertEquals(stat.executeUpdate("create table tab (id, firstname, surname);"), 0); 134 assertEquals(stat.executeUpdate("insert into tab values (0, 'Bob', 'Builder');"), 1); 135 assertEquals(stat.executeUpdate("insert into tab values (1, 'Fred', 'Blogs');"), 1); 136 assertEquals(stat.executeUpdate("insert into tab values (2, 'John', 'Smith');"), 1); 97 @Test public void colNameAccess() throws SQLException { 98 assertEquals(stat.executeUpdate( 99 "create table tab (id, firstname, surname);"), 0); 100 assertEquals(stat.executeUpdate( 101 "insert into tab values (0, 'Bob', 'Builder');"), 1); 102 assertEquals(stat.executeUpdate( 103 "insert into tab values (1, 'Fred', 'Blogs');"), 1); 104 assertEquals(stat.executeUpdate( 105 "insert into tab values (2, 'John', 'Smith');"), 1); 137 106 ResultSet rs = stat.executeQuery("select * from tab;"); 138 107 assertTrue(rs.next()); … … 146 115 assertTrue(rs.next()); 147 116 assertEquals(rs.getInt("id"), 2); 148 assertEquals( rs.getString("id"), "2");117 assertEquals( rs.getString("id"), "2"); 149 118 assertEquals(rs.getString("firstname"), "John"); 150 119 assertEquals(rs.getString("surname"), "Smith"); … … 154 123 } 155 124 156 @Test 157 public void nulls() throws SQLException 158 { 125 @Test public void nulls() throws SQLException { 159 126 ResultSet rs = stat.executeQuery("select null union all select null;"); 160 127 assertTrue(rs.next()); … … 168 135 } 169 136 170 @Test 171 public void tempTable() throws SQLException 172 { 137 @Test public void tempTable() throws SQLException { 173 138 assertEquals(stat.executeUpdate("create temp table myTemp (a);"), 0); 174 139 assertEquals(stat.executeUpdate("insert into myTemp values (2);"), 1); 175 140 } 176 141 177 @Test 178 public void insert1000() throws SQLException 179 { 142 @Test public void insert1000() throws SQLException { 180 143 assertEquals(stat.executeUpdate("create table in1000 (a);"), 0); 181 144 conn.setAutoCommit(false); 182 for (int i = 0; i < 1000; i++) 183 assertEquals(stat.executeUpdate("insert into in1000 values (" + i + ");"), 1); 145 for (int i=0; i < 1000; i++) 146 assertEquals(stat.executeUpdate( 147 "insert into in1000 values ("+i+");"), 1); 184 148 conn.commit(); 185 149 … … 192 156 } 193 157 194 private void assertArrayEq(int[] a, int[] b) 195 { 158 private void assertArrayEq(int[] a, int[] b) { 196 159 assertNotNull(a); 197 160 assertNotNull(b); 198 161 assertEquals(a.length, b.length); 199 for (int i =0; i < a.length; i++)162 for (int i=0; i < a.length; i++) 200 163 assertEquals(a[i], b[i]); 201 164 } 202 165 203 @Test 204 public void batch() throws SQLException 205 { 166 @Test public void batch() throws SQLException { 206 167 stat.addBatch("create table batch (c1);"); 207 168 stat.addBatch("insert into batch values (1);"); … … 211 172 stat.addBatch("insert into batch values (4);"); 212 173 assertArrayEq(new int[] { 0, 1, 1, 1, 1, 1 }, stat.executeBatch()); 213 assertArrayEq(new int[] { }, stat.executeBatch());174 assertArrayEq(new int[] { }, stat.executeBatch()); 214 175 stat.clearBatch(); 215 176 stat.addBatch("insert into batch values (9);"); … … 228 189 } 229 190 230 @Test 231 public void closeOnFalseNext() throws SQLException 232 { 191 @Test public void closeOnFalseNext() throws SQLException { 233 192 stat.executeUpdate("create table t1 (c1);"); 234 193 conn.createStatement().executeQuery("select * from t1;").next(); … … 236 195 } 237 196 238 @Test 239 public void getGeneratedKeys() throws SQLException 240 { 197 @Test public void getGeneratedKeys() throws SQLException { 241 198 ResultSet rs; 242 199 stat.executeUpdate("create table t1 (c1 integer primary key, v);"); … … 253 210 } 254 211 255 @Test 256 public void isBeforeFirst() throws SQLException 257 { 212 @Test public void isBeforeFirst() throws SQLException { 258 213 ResultSet rs = stat.executeQuery("select 1 union all select 2;"); 259 214 assertTrue(rs.isBeforeFirst()); … … 270 225 } 271 226 272 @Test 273 public void columnNaming() throws SQLException 274 { 227 @Test public void columnNaming() throws SQLException { 275 228 stat.executeUpdate("create table t1 (c1 integer);"); 276 229 stat.executeUpdate("create table t2 (c1 integer);"); 277 230 stat.executeUpdate("insert into t1 values (1);"); 278 231 stat.executeUpdate("insert into t2 values (1);"); 279 ResultSet rs = stat.executeQuery("select a.c1 AS c1 from t1 a, t2 where a.c1=t2.c1;"); 232 ResultSet rs = stat.executeQuery( 233 "select a.c1 AS c1 from t1 a, t2 where a.c1=t2.c1;"); 280 234 assertTrue(rs.next()); 281 235 assertEquals(rs.getInt("c1"), 1); … … 283 237 } 284 238 285 @Test 286 public void nullDate() throws SQLException 287 { 239 @Test public void nullDate() throws SQLException { 288 240 ResultSet rs = stat.executeQuery("select null;"); 289 241 assertTrue(rs.next()); … … 294 246 295 247 @Ignore 296 @Test(expected = SQLException.class) 297 public void ambiguousColumnNaming() throws SQLException 298 { 248 @Test(expected= SQLException.class) 249 public void ambiguousColumnNaming() throws SQLException { 299 250 stat.executeUpdate("create table t1 (c1 int);"); 300 251 stat.executeUpdate("create table t2 (c1 int, c2 int);"); 301 252 stat.executeUpdate("insert into t1 values (1);"); 302 253 stat.executeUpdate("insert into t2 values (2, 1);"); 303 ResultSet rs = stat.executeQuery("select a.c1, b.c1 from t1 a, t2 b where a.c1=b.c2;"); 254 ResultSet rs = stat.executeQuery( 255 "select a.c1, b.c1 from t1 a, t2 b where a.c1=b.c2;"); 304 256 assertTrue(rs.next()); 305 257 assertEquals(rs.getInt("c1"), 1); … … 307 259 } 308 260 309 @Test(expected = SQLException.class) 310 public void failToDropWhenRSOpen() throws SQLException 311 { 261 @Test(expected= SQLException.class) 262 public void failToDropWhenRSOpen() throws SQLException { 312 263 stat.executeUpdate("create table t1 (c1);"); 313 264 stat.executeUpdate("insert into t1 values (4);"); … … 317 268 } 318 269 319 @Test(expected = SQLException.class) 320 public void executeNoRS() throws SQLException 321 { 270 @Test(expected= SQLException.class) 271 public void executeNoRS() throws SQLException { 322 272 assertFalse(stat.execute("insert into test values (8);")); 323 273 stat.getResultSet(); 324 274 } 325 275 326 @Test(expected = SQLException.class) 327 public void executeClearRS() throws SQLException 328 { 276 @Test(expected= SQLException.class) 277 public void executeClearRS() throws SQLException { 329 278 assertTrue(stat.execute("select null;")); 330 279 assertNotNull(stat.getResultSet()); … … 333 282 } 334 283 335 @Test(expected = BatchUpdateException.class) 336 public void batchReturnsResults() throws SQLException 337 { 284 @Test(expected= BatchUpdateException.class) 285 public void batchReturnsResults() throws SQLException { 338 286 stat.addBatch("select null;"); 339 287 stat.executeBatch(); 340 288 } 341 289 342 @Test(expected = SQLException.class) 343 public void noSuchTable() throws SQLException 344 { 290 @Test(expected= SQLException.class) 291 public void noSuchTable() throws SQLException { 345 292 stat.executeQuery("select * from doesnotexist;"); 346 293 } 347 348 @Test(expected = SQLException.class) 349 public void noSuchCol() throws SQLException 350 { 294 295 @Test(expected= SQLException.class) 296 public void noSuchCol() throws SQLException { 351 297 stat.executeQuery("select notacol from (select 1);"); 352 298 } 353 299 354 @Test(expected = SQLException.class) 355 public void noSuchColName() throws SQLException 356 { 300 @Test(expected= SQLException.class) 301 public void noSuchColName() throws SQLException { 357 302 ResultSet rs = stat.executeQuery("select 1;"); 358 303 assertTrue(rs.next()); -
XerialJ/trunk/sqlite-jdbc/src/test/java/org/sqlite/TransactionTest.java
r2254 r2714 1 1 package org.sqlite; 2 2 3 import static org.junit.Assert.assertEquals;4 import static org.junit.Assert.assertFalse;5 import static org.junit.Assert.assertTrue;6 7 3 import java.io.File; 8 import java.sql.Connection; 9 import java.sql.DriverManager; 10 import java.sql.PreparedStatement; 11 import java.sql.ResultSet; 12 import java.sql.SQLException; 13 import java.sql.Statement; 14 15 import org.junit.After; 16 import org.junit.Before; 17 import org.junit.BeforeClass; 18 import org.junit.Test; 19 20 /** 21 * These tests assume that Statements and PreparedStatements are working as per 22 * normal and test the interactions of commit(), rollback() and 23 * setAutoCommit(boolean) with multiple connections to the same db. 24 */ 4 import java.sql.*; 5 import org.junit.*; 6 import static org.junit.Assert.*; 7 8 /** These tests assume that Statements and PreparedStatements are working 9 * as per normal and test the interactions of commit(), rollback() and 10 * setAutoCommit(boolean) with multiple connections to the same db. */ 25 11 public class TransactionTest 26 12 { … … 30 16 boolean done = false; 31 17 32 @BeforeClass 33 public static void forName() throws Exception 34 { 18 @BeforeClass public static void forName() throws Exception { 35 19 Class.forName("org.sqlite.JDBC"); 36 20 } 37 21 38 @Before 39 public void connect() throws Exception 40 { 22 @Before public void connect() throws Exception { 41 23 new File("test-trans.db").delete(); 42 24 conn1 = DriverManager.getConnection("jdbc:sqlite:test-trans.db"); … … 48 30 } 49 31 50 @After 51 public void close() throws Exception 52 { 53 stat1.close(); 54 stat2.close(); 55 stat3.close(); 56 conn1.close(); 57 conn2.close(); 58 conn3.close(); 32 @After public void close() throws Exception { 33 stat1.close(); stat2.close(); stat3.close(); 34 conn1.close(); conn2.close(); conn3.close(); 59 35 new File("test-trans.db").delete(); 60 36 } 61 37 62 @Test 63 public void multiConn() throws SQLException 64 { 38 @Test public void multiConn() throws SQLException { 65 39 stat1.executeUpdate("create table test (c1);"); 66 40 stat1.executeUpdate("insert into test values (1);"); … … 79 53 } 80 54 81 @Test 82 public void locking() throws SQLException 83 { 55 @Test public void locking() throws SQLException { 84 56 stat1.executeUpdate("create table test (c1);"); 85 57 stat1.executeUpdate("begin immediate;"); … … 87 59 } 88 60 89 @Test 90 public void insert() throws SQLException 91 { 61 @Test public void insert() throws SQLException { 92 62 ResultSet rs; 93 63 String countSql = "select count(*) from trans;"; … … 117 87 } 118 88 119 @Test 120 public void rollback() throws SQLException 121 { 89 @Test public void rollback() throws SQLException { 122 90 String select = "select * from trans;"; 123 91 ResultSet rs; … … 138 106 } 139 107 140 @Test 141 public void multiRollback() throws SQLException 142 { 108 @Test public void multiRollback() throws SQLException { 143 109 ResultSet rs; 144 110 … … 160 126 stat1.executeUpdate("insert into t values (5);"); 161 127 conn1.setAutoCommit(false); 162 PreparedStatement p = conn1.prepareStatement("insert into t values (?);"); 128 PreparedStatement p = conn1.prepareStatement( 129 "insert into t values (?);"); 163 130 p.setInt(1, 6); 164 131 p.executeUpdate(); … … 169 136 rs = stat1.executeQuery("select sum(c1) from t;"); 170 137 assertTrue(rs.next()); 171 assertEquals(1 + 2 + 3 + 4 + 5 + 6 +7, rs.getInt(1));138 assertEquals(1+2+3+4+5+6+7, rs.getInt(1)); 172 139 rs.close(); 173 140 rs = stat2.executeQuery("select sum(c1) from t;"); 174 141 assertTrue(rs.next()); 175 assertEquals(1 + 2 + 3 + 4 +5, rs.getInt(1));142 assertEquals(1+2+3+4+5, rs.getInt(1)); 176 143 rs.close(); 177 144 } 178 145 179 146 @Test 180 public void transactionsDontMindReads() throws SQLException 181 { 147 public void transactionsDontMindReads() throws SQLException { 182 148 stat1.executeUpdate("create table t (c1);"); 183 149 stat1.executeUpdate("insert into t values (1);"); … … 194 160 195 161 @Test 196 public void secondConnWillWait() throws Exception 197 { 162 public void secondConnWillWait() throws Exception { 198 163 stat1.executeUpdate("create table t (c1);"); 199 164 stat1.executeUpdate("insert into t values (1);"); … … 204 169 final TransactionTest lock = this; 205 170 lock.done = false; 206 new Thread() { 207 public void run() 208 { 209 try 210 { 211 stat2.executeUpdate("insert into t values (3);"); 212 } 213 catch (SQLException e) 214 { 215 e.printStackTrace(); 216 return; 217 } 218 219 synchronized (lock) 220 { 221 lock.done = true; 222 lock.notify(); 223 } 171 new Thread() { public void run() { 172 try { 173 stat2.executeUpdate("insert into t values (3);"); 174 } catch (SQLException e) { 175 e.printStackTrace(); 176 return; 224 177 } 225 }.start(); 178 179 synchronized (lock) { 180 lock.done = true; 181 lock.notify(); 182 } 183 } }.start(); 226 184 227 185 Thread.sleep(100); 228 186 rs.close(); 229 187 230 synchronized (lock) 231 { 188 synchronized (lock) { 232 189 lock.wait(5000); 233 190 if (!lock.done) … … 236 193 } 237 194 238 @Test(expected = SQLException.class) 239 public void secondConnMustTimeout() throws SQLException 240 { 195 @Test(expected= SQLException.class) 196 public void secondConnMustTimeout() throws SQLException { 241 197 stat1.setQueryTimeout(1); 242 198 stat1.executeUpdate("create table t (c1);"); … … 249 205 } 250 206 251 @Test(expected= SQLException.class)252 public void cantUpdateWhileReading() throws SQLException 253 {207 // @Test(expected= SQLException.class) 208 @Test 209 public void cantUpdateWhileReading() throws SQLException { 254 210 stat1.executeUpdate("create table t (c1);"); 255 211 stat1.executeUpdate("insert into t values (1);"); … … 258 214 assertTrue(rs.next()); 259 215 216 // commit now succeeds since sqlite 3.6.5 260 217 stat1.executeUpdate("insert into t values (3);"); // can't be done 261 218 } 262 219 263 @Test(expected = SQLException.class) 264 public void cantCommit() throws SQLException 265 { 266 conn1.commit(); 267 } 268 269 @Test(expected = SQLException.class) 270 public void cantRollback() throws SQLException 271 { 272 conn1.rollback(); 273 } 220 @Test(expected= SQLException.class) 221 public void cantCommit() throws SQLException { conn1.commit(); } 222 223 @Test(expected= SQLException.class) 224 public void cantRollback() throws SQLException { conn1.rollback(); } 274 225 275 226 }


