Changeset 2714

Show
Ignore:
Timestamp:
11/20/08 15:39:18 (7 weeks ago)
Author:
leo
Message:
 
Location:
XerialJ/trunk/sqlite-jdbc/src/test/java/org/sqlite
Files:
2 modified

Legend:

Unmodified
Added
Removed
  • XerialJ/trunk/sqlite-jdbc/src/test/java/org/sqlite/StatementTest.java

    r2254 r2714  
    11package org.sqlite; 
    22 
    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; 
     3import java.sql.*; 
     4import org.junit.*; 
     5import static org.junit.Assert.*; 
    216 
    227/** These tests are designed to stress Statements on memory databases. */ 
     
    2611    private Statement stat; 
    2712 
    28     @BeforeClass 
    29     public static void forName() throws Exception 
    30     { 
     13    @BeforeClass public static void forName() throws Exception { 
    3114        Class.forName("org.sqlite.JDBC"); 
    3215    } 
    3316 
    34     @Before 
    35     public void connect() throws Exception 
    36     { 
     17    @Before public void connect() throws Exception { 
    3718        conn = DriverManager.getConnection("jdbc:sqlite:"); 
    3819        stat = conn.createStatement(); 
    3920    } 
    4021 
    41     @After 
    42     public void close() throws SQLException 
    43     { 
     22    @After public void close() throws SQLException { 
    4423        stat.close(); 
    4524        conn.close(); 
    4625    } 
    4726 
    48     @Test 
    49     public void stmtUpdate() throws SQLException 
    50     { 
     27    @Test public void stmtUpdate() throws SQLException { 
    5128        assertEquals(stat.executeUpdate("create table s1 (c1);"), 0); 
    5229        assertEquals(stat.executeUpdate("insert into s1 values (0);"), 1); 
     
    5431        assertEquals(stat.executeUpdate("insert into s1 values (2);"), 1); 
    5532        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 { 
    6339        ResultSet rs = stat.executeQuery("select null limit 0;"); 
    6440        assertFalse(rs.next()); 
     
    6642    } 
    6743 
    68     @Test 
    69     public void singleRowRS() throws SQLException 
    70     { 
     44    @Test public void singleRowRS() throws SQLException { 
    7145        ResultSet rs = stat.executeQuery("select " + Integer.MAX_VALUE + ";"); 
    7246        assertTrue(rs.next()); 
    7347        assertEquals(rs.getInt(1), Integer.MAX_VALUE); 
    7448        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 { 
    8356        ResultSet rs = stat.executeQuery("select 9 union all select 7;"); 
    8457        assertTrue(rs.next()); 
     
    9063    } 
    9164 
    92     @Test 
    93     public void autoClose() throws SQLException 
    94     { 
     65    @Test public void autoClose() throws SQLException { 
    9566        conn.createStatement().executeQuery("select 1;"); 
    9667    } 
    9768 
    98     @Test 
    99     public void stringRS() throws SQLException 
    100     { 
     69    @Test public void stringRS() throws SQLException { 
    10170        ResultSet rs = stat.executeQuery("select \"Russell\";"); 
    10271        assertTrue(rs.next()); 
     
    10675    } 
    10776 
    108     @Test 
    109     public void execute() throws SQLException 
    110     { 
     77    @Test public void execute() throws SQLException { 
    11178        assertTrue(stat.execute("select null;")); 
    11279        ResultSet rs = stat.getResultSet(); 
     
    12895    } 
    12996 
    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); 
    137106        ResultSet rs = stat.executeQuery("select * from tab;"); 
    138107        assertTrue(rs.next()); 
     
    146115        assertTrue(rs.next()); 
    147116        assertEquals(rs.getInt("id"), 2); 
    148         assertEquals(rs.getString("id"), "2"); 
     117        assertEquals( rs.getString("id"), "2"); 
    149118        assertEquals(rs.getString("firstname"), "John"); 
    150119        assertEquals(rs.getString("surname"), "Smith"); 
     
    154123    } 
    155124 
    156     @Test 
    157     public void nulls() throws SQLException 
    158     { 
     125    @Test public void nulls() throws SQLException { 
    159126        ResultSet rs = stat.executeQuery("select null union all select null;"); 
    160127        assertTrue(rs.next()); 
     
    168135    } 
    169136 
    170     @Test 
    171     public void tempTable() throws SQLException 
    172     { 
     137    @Test public void tempTable() throws SQLException { 
    173138        assertEquals(stat.executeUpdate("create temp table myTemp (a);"), 0); 
    174139        assertEquals(stat.executeUpdate("insert into myTemp values (2);"), 1); 
    175140    } 
    176141 
    177     @Test 
    178     public void insert1000() throws SQLException 
    179     { 
     142    @Test public void insert1000() throws SQLException { 
    180143        assertEquals(stat.executeUpdate("create table in1000 (a);"), 0); 
    181144        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); 
    184148        conn.commit(); 
    185149 
     
    192156    } 
    193157 
    194     private void assertArrayEq(int[] a, int[] b) 
    195     { 
     158    private void assertArrayEq(int[] a, int[] b) { 
    196159        assertNotNull(a); 
    197160        assertNotNull(b); 
    198161        assertEquals(a.length, b.length); 
    199         for (int i = 0; i < a.length; i++) 
     162        for (int i=0; i < a.length; i++) 
    200163            assertEquals(a[i], b[i]); 
    201164    } 
    202165 
    203     @Test 
    204     public void batch() throws SQLException 
    205     { 
     166    @Test public void batch() throws SQLException { 
    206167        stat.addBatch("create table batch (c1);"); 
    207168        stat.addBatch("insert into batch values (1);"); 
     
    211172        stat.addBatch("insert into batch values (4);"); 
    212173        assertArrayEq(new int[] { 0, 1, 1, 1, 1, 1 }, stat.executeBatch()); 
    213         assertArrayEq(new int[] {}, stat.executeBatch()); 
     174        assertArrayEq(new int[] { }, stat.executeBatch()); 
    214175        stat.clearBatch(); 
    215176        stat.addBatch("insert into batch values (9);"); 
     
    228189    } 
    229190 
    230     @Test 
    231     public void closeOnFalseNext() throws SQLException 
    232     { 
     191    @Test public void closeOnFalseNext() throws SQLException { 
    233192        stat.executeUpdate("create table t1 (c1);"); 
    234193        conn.createStatement().executeQuery("select * from t1;").next(); 
     
    236195    } 
    237196 
    238     @Test 
    239     public void getGeneratedKeys() throws SQLException 
    240     { 
     197    @Test public void getGeneratedKeys() throws SQLException { 
    241198        ResultSet rs; 
    242199        stat.executeUpdate("create table t1 (c1 integer primary key, v);"); 
     
    253210    } 
    254211 
    255     @Test 
    256     public void isBeforeFirst() throws SQLException 
    257     { 
     212    @Test public void isBeforeFirst() throws SQLException { 
    258213        ResultSet rs = stat.executeQuery("select 1 union all select 2;"); 
    259214        assertTrue(rs.isBeforeFirst()); 
     
    270225    } 
    271226 
    272     @Test 
    273     public void columnNaming() throws SQLException 
    274     { 
     227    @Test public void columnNaming() throws SQLException { 
    275228        stat.executeUpdate("create table t1 (c1 integer);"); 
    276229        stat.executeUpdate("create table t2 (c1 integer);"); 
    277230        stat.executeUpdate("insert into t1 values (1);"); 
    278231        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;"); 
    280234        assertTrue(rs.next()); 
    281235        assertEquals(rs.getInt("c1"), 1); 
     
    283237    } 
    284238 
    285     @Test 
    286     public void nullDate() throws SQLException 
    287     { 
     239    @Test public void nullDate() throws SQLException { 
    288240        ResultSet rs = stat.executeQuery("select null;"); 
    289241        assertTrue(rs.next()); 
     
    294246 
    295247    @Ignore 
    296     @Test(expected = SQLException.class) 
    297     public void ambiguousColumnNaming() throws SQLException 
    298     { 
     248    @Test(expected= SQLException.class) 
     249    public void ambiguousColumnNaming() throws SQLException { 
    299250        stat.executeUpdate("create table t1 (c1 int);"); 
    300251        stat.executeUpdate("create table t2 (c1 int, c2 int);"); 
    301252        stat.executeUpdate("insert into t1 values (1);"); 
    302253        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;"); 
    304256        assertTrue(rs.next()); 
    305257        assertEquals(rs.getInt("c1"), 1); 
     
    307259    } 
    308260 
    309     @Test(expected = SQLException.class) 
    310     public void failToDropWhenRSOpen() throws SQLException 
    311     { 
     261    @Test(expected= SQLException.class) 
     262    public void failToDropWhenRSOpen() throws SQLException { 
    312263        stat.executeUpdate("create table t1 (c1);"); 
    313264        stat.executeUpdate("insert into t1 values (4);"); 
     
    317268    } 
    318269 
    319     @Test(expected = SQLException.class) 
    320     public void executeNoRS() throws SQLException 
    321     { 
     270    @Test(expected= SQLException.class) 
     271    public void executeNoRS() throws SQLException { 
    322272        assertFalse(stat.execute("insert into test values (8);")); 
    323273        stat.getResultSet(); 
    324274    } 
    325275 
    326     @Test(expected = SQLException.class) 
    327     public void executeClearRS() throws SQLException 
    328     { 
     276    @Test(expected= SQLException.class) 
     277    public void executeClearRS() throws SQLException { 
    329278        assertTrue(stat.execute("select null;")); 
    330279        assertNotNull(stat.getResultSet()); 
     
    333282    } 
    334283 
    335     @Test(expected = BatchUpdateException.class) 
    336     public void batchReturnsResults() throws SQLException 
    337     { 
     284    @Test(expected= BatchUpdateException.class) 
     285    public void batchReturnsResults() throws SQLException { 
    338286        stat.addBatch("select null;"); 
    339287        stat.executeBatch(); 
    340288    } 
    341289 
    342     @Test(expected = SQLException.class) 
    343     public void noSuchTable() throws SQLException 
    344     { 
     290    @Test(expected= SQLException.class) 
     291    public void noSuchTable() throws SQLException { 
    345292        stat.executeQuery("select * from doesnotexist;"); 
    346293    } 
    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 { 
    351297        stat.executeQuery("select notacol from (select 1);"); 
    352298    } 
    353299 
    354     @Test(expected = SQLException.class) 
    355     public void noSuchColName() throws SQLException 
    356     { 
     300    @Test(expected= SQLException.class) 
     301    public void noSuchColName() throws SQLException { 
    357302        ResultSet rs = stat.executeQuery("select 1;"); 
    358303        assertTrue(rs.next()); 
  • XerialJ/trunk/sqlite-jdbc/src/test/java/org/sqlite/TransactionTest.java

    r2254 r2714  
    11package org.sqlite; 
    22 
    3 import static org.junit.Assert.assertEquals; 
    4 import static org.junit.Assert.assertFalse; 
    5 import static org.junit.Assert.assertTrue; 
    6  
    73import 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  */ 
     4import java.sql.*; 
     5import org.junit.*; 
     6import 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. */ 
    2511public class TransactionTest 
    2612{ 
     
    3016    boolean done = false; 
    3117 
    32     @BeforeClass 
    33     public static void forName() throws Exception 
    34     { 
     18    @BeforeClass public static void forName() throws Exception { 
    3519        Class.forName("org.sqlite.JDBC"); 
    3620    } 
    3721 
    38     @Before 
    39     public void connect() throws Exception 
    40     { 
     22    @Before public void connect() throws Exception { 
    4123        new File("test-trans.db").delete(); 
    4224        conn1 = DriverManager.getConnection("jdbc:sqlite:test-trans.db"); 
     
    4830    } 
    4931 
    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(); 
    5935        new File("test-trans.db").delete(); 
    6036    } 
    6137 
    62     @Test 
    63     public void multiConn() throws SQLException 
    64     { 
     38    @Test public void multiConn() throws SQLException { 
    6539        stat1.executeUpdate("create table test (c1);"); 
    6640        stat1.executeUpdate("insert into test values (1);"); 
     
    7953    } 
    8054 
    81     @Test 
    82     public void locking() throws SQLException 
    83     { 
     55    @Test public void locking() throws SQLException { 
    8456        stat1.executeUpdate("create table test (c1);"); 
    8557        stat1.executeUpdate("begin immediate;"); 
     
    8759    } 
    8860 
    89     @Test 
    90     public void insert() throws SQLException 
    91     { 
     61    @Test public void insert() throws SQLException { 
    9262        ResultSet rs; 
    9363        String countSql = "select count(*) from trans;"; 
     
    11787    } 
    11888 
    119     @Test 
    120     public void rollback() throws SQLException 
    121     { 
     89    @Test public void rollback() throws SQLException { 
    12290        String select = "select * from trans;"; 
    12391        ResultSet rs; 
     
    138106    } 
    139107 
    140     @Test 
    141     public void multiRollback() throws SQLException 
    142     { 
     108    @Test public void multiRollback() throws SQLException { 
    143109        ResultSet rs; 
    144110 
     
    160126        stat1.executeUpdate("insert into t values (5);"); 
    161127        conn1.setAutoCommit(false); 
    162         PreparedStatement p = conn1.prepareStatement("insert into t values (?);"); 
     128        PreparedStatement p = conn1.prepareStatement( 
     129                "insert into t values (?);"); 
    163130        p.setInt(1, 6); 
    164131        p.executeUpdate(); 
     
    169136        rs = stat1.executeQuery("select sum(c1) from t;"); 
    170137        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)); 
    172139        rs.close(); 
    173140        rs = stat2.executeQuery("select sum(c1) from t;"); 
    174141        assertTrue(rs.next()); 
    175         assertEquals(1 + 2 + 3 + 4 + 5, rs.getInt(1)); 
     142        assertEquals(1+2+3+4+5, rs.getInt(1)); 
    176143        rs.close(); 
    177144    } 
    178145 
    179146    @Test 
    180     public void transactionsDontMindReads() throws SQLException 
    181     { 
     147    public void transactionsDontMindReads() throws SQLException { 
    182148        stat1.executeUpdate("create table t (c1);"); 
    183149        stat1.executeUpdate("insert into t values (1);"); 
     
    194160 
    195161    @Test 
    196     public void secondConnWillWait() throws Exception 
    197     { 
     162    public void secondConnWillWait() throws Exception { 
    198163        stat1.executeUpdate("create table t (c1);"); 
    199164        stat1.executeUpdate("insert into t values (1);"); 
     
    204169        final TransactionTest lock = this; 
    205170        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; 
    224177            } 
    225         }.start(); 
     178 
     179            synchronized (lock) { 
     180                lock.done = true; 
     181                lock.notify(); 
     182            } 
     183        } }.start(); 
    226184 
    227185        Thread.sleep(100); 
    228186        rs.close(); 
    229187 
    230         synchronized (lock) 
    231         { 
     188        synchronized (lock) { 
    232189            lock.wait(5000); 
    233190            if (!lock.done) 
     
    236193    } 
    237194 
    238     @Test(expected = SQLException.class) 
    239     public void secondConnMustTimeout() throws SQLException 
    240     { 
     195    @Test(expected= SQLException.class) 
     196    public void secondConnMustTimeout() throws SQLException { 
    241197        stat1.setQueryTimeout(1); 
    242198        stat1.executeUpdate("create table t (c1);"); 
     
    249205    } 
    250206 
    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 { 
    254210        stat1.executeUpdate("create table t (c1);"); 
    255211        stat1.executeUpdate("insert into t values (1);"); 
     
    258214        assertTrue(rs.next()); 
    259215 
     216                // commit now succeeds since sqlite 3.6.5 
    260217        stat1.executeUpdate("insert into t values (3);"); // can't be done 
    261218    } 
    262219 
    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(); } 
    274225 
    275226}