11package net .zetetic .tests ;
22
33import android .util .Log ;
4- import net .sqlcipher .Cursor ;
4+
5+ import android .database .Cursor ;
6+
57import net .sqlcipher .database .SQLiteDatabase ;
8+ import net .sqlcipher .database .SQLiteException ;
9+
610import net .zetetic .ZeteticApplication ;
711
812import java .io .File ;
@@ -59,13 +63,21 @@ public Writer(int id, int size, DatabaseAccessType accessType) {
5963 public void run () {
6064 android .os .Process .setThreadPriority (android .os .Process .THREAD_PRIORITY_BACKGROUND );
6165 Log .i (TAG , String .format ("writer thread %d beginning" , id ));
66+ // not expected to throw:
6267 SQLiteDatabase writer = getDatabase (accessType );
63- writer .execSQL ("create table if not exists t1(a,b)" );
64- for (int index = 0 ; index < size ; index ++) {
65- Log .i (TAG , String .format ("writer thread %d - insert data for row:%d" , id , index ));
66- writer .execSQL ("insert into t1(a,b) values(?, ?)" ,
68+
69+ try {
70+ writer .execSQL ("create table if not exists t1(a,b)" );
71+
72+ for (int index = 0 ; index < size ; index ++) {
73+ Log .i (TAG , String .format ("writer thread %d - insert data for row:%d" , id , index ));
74+ writer .execSQL ("insert into t1(a,b) values(?, ?)" ,
6775 new Object []{"one for the money" , "two for the show" });
68- }
76+ }
77+ } catch (SQLiteException ex ) { Log .e (TAG , "caught exception, bailing" , ex ); }
78+ // FIX: ADD CATCH #1:
79+ // catch (IllegalStateException ex) { Log.e(TAG, "caught exception, bailing", ex); }
80+
6981 closeDatabase (writer , accessType );
7082 Log .i (TAG , String .format ("writer thread %d terminating" , id ));
7183 }
@@ -99,8 +111,14 @@ public void run() {
99111
100112 synchronized void logRecordsBetween (SQLiteDatabase reader , int start , int end ) {
101113 if (!reader .isOpen ()) return ;
102- Cursor results = reader .rawQuery ("select rowid, * from t1 where rowid between ? and ?" ,
114+ Cursor results = null ;
115+ try {
116+ results = reader .rawQuery ("select rowid, * from t1 where rowid between ? and ?" ,
103117 new String []{String .valueOf (start ), String .valueOf (end )});
118+ } catch (IllegalStateException ex ) { Log .e (TAG , "caught exception, bailing" , ex ); }
119+ // FIX: ADD CATCH #2:
120+ // catch (SQLiteException ex) { Log.e(TAG, "caught exception, bailing", ex); }
121+
104122 if (results != null ) {
105123 Log .i (TAG , String .format ("reader thread %d - writing results %d to %d" , id , start , end ));
106124 while (results .moveToNext ()) {
@@ -113,16 +131,27 @@ synchronized void logRecordsBetween(SQLiteDatabase reader, int start, int end) {
113131
114132 synchronized int getCurrentTableCount (SQLiteDatabase database ) {
115133 int count = 0 ;
134+
135+ if (!database .isOpen ()) return -1 ;
136+ Cursor cursor = null ;
137+
138+ // I. Attempt database.rawQuery()-bail (explicitly return 0) if it throws:
116139 try {
117- if (!database .isOpen ()) return -1 ;
118- Cursor cursor = database .rawQuery ("select count(*) from t1;" , new String []{});
119- if (cursor != null ) {
140+ cursor = database .rawQuery ("select count(*) from t1;" , new String []{});
141+ } catch (IllegalStateException ex ) { Log .e (TAG , "caught exception, bailing with count = " + count , ex ); return 0 ; }
142+ // FIX: ADD CATCH #3:
143+ // catch (SQLiteException ex) { Log.e(TAG, "caught exception, bailing", ex); return 0; }
144+
145+ // II. Attempt to get the count from the cursor:
146+ if (cursor != null ) {
147+ try {
120148 if (cursor .moveToFirst ()) {
121149 count = cursor .getInt (0 );
122150 }
123151 cursor .close ();
124- }
125- } catch (IllegalStateException ex ){}
152+ } catch (IllegalStateException ex ) { Log .e (TAG , "caught exception, bailing with count = " + count , ex ); }
153+ }
154+
126155 return count ;
127156 }
128157 }
@@ -186,4 +215,4 @@ private enum DatabaseAccessType {
186215 public String getName () {
187216 return "Multi-threaded read/write test" ;
188217 }
189- }
218+ }
0 commit comments