|
1 | 1 | package net.zetetic.database.sqlcipher.driver |
2 | 2 |
|
3 | 3 | import android.content.Context |
| 4 | +import android.database.Cursor |
| 5 | +import androidx.sqlite.SQLITE_DATA_BLOB |
| 6 | +import androidx.sqlite.SQLITE_DATA_FLOAT |
| 7 | +import androidx.sqlite.SQLITE_DATA_INTEGER |
| 8 | +import androidx.sqlite.SQLITE_DATA_NULL |
| 9 | +import androidx.sqlite.SQLITE_DATA_TEXT |
4 | 10 | import androidx.sqlite.SQLiteConnection |
5 | 11 | import androidx.sqlite.execSQL |
6 | 12 | import androidx.test.ext.junit.runners.AndroidJUnit4 |
7 | 13 | import androidx.test.platform.app.InstrumentationRegistry |
8 | | -import kotlinx.coroutines.test.runTest |
9 | 14 | import net.zetetic.database.sqlcipher.SQLiteDatabase |
10 | 15 | import org.junit.After |
11 | 16 | import org.junit.Assert.assertArrayEquals |
12 | 17 | import org.junit.Assert.assertEquals |
13 | 18 | import org.junit.Assert.assertFalse |
| 19 | +import org.junit.Assert.assertNotNull |
| 20 | +import org.junit.Assert.assertNull |
14 | 21 | import org.junit.Assert.assertThrows |
15 | 22 | import org.junit.Assert.assertTrue |
16 | 23 | import org.junit.Before |
@@ -156,6 +163,18 @@ class SQLCipherConnectionTests { |
156 | 163 | } |
157 | 164 | } |
158 | 165 |
|
| 166 | + @Test |
| 167 | + fun shouldReportColumnTypesUsingSqliteDataConstants() { |
| 168 | + connection.prepare("SELECT NULL, 1, 1.5, 'text', x'00'").use { statement -> |
| 169 | + assertTrue(statement.step()) |
| 170 | + assertEquals(SQLITE_DATA_NULL, statement.getColumnType(0)) |
| 171 | + assertEquals(SQLITE_DATA_INTEGER, statement.getColumnType(1)) |
| 172 | + assertEquals(SQLITE_DATA_FLOAT, statement.getColumnType(2)) |
| 173 | + assertEquals(SQLITE_DATA_TEXT, statement.getColumnType(3)) |
| 174 | + assertEquals(SQLITE_DATA_BLOB, statement.getColumnType(4)) |
| 175 | + } |
| 176 | + } |
| 177 | + |
159 | 178 | @Test |
160 | 179 | fun shouldRoundTripDataThroughStandardStatement() { |
161 | 180 | connection.prepare("INSERT INTO t (id, v) VALUES (?, ?)").use { statement -> |
@@ -233,6 +252,32 @@ class SQLCipherConnectionTests { |
233 | 252 | } |
234 | 253 | } |
235 | 254 |
|
| 255 | + @Test |
| 256 | + fun shouldCloseCursorWhenStatementClosedAfterStep() { |
| 257 | + database.execSQL( |
| 258 | + "INSERT INTO t (id, v) VALUES (?, ?);", |
| 259 | + arrayOf<Any>(1, "one")) |
| 260 | + val statement = SQLCipherStatement( |
| 261 | + database, |
| 262 | + "SELECT id, v FROM t;") |
| 263 | + assertTrue(statement.step()) |
| 264 | + |
| 265 | + val cursor = getCursor(statement) |
| 266 | + assertNotNull(cursor) |
| 267 | + assertFalse(cursor!!.isClosed) |
| 268 | + |
| 269 | + statement.close() |
| 270 | + |
| 271 | + assertTrue(cursor.isClosed) |
| 272 | + assertNull(getCursor(statement)) |
| 273 | + } |
| 274 | + |
| 275 | + private fun getCursor(statement: SQLCipherStatement): Cursor? { |
| 276 | + val field = SQLCipherStatement::class.java.getDeclaredField("cursor") |
| 277 | + field.isAccessible = true |
| 278 | + return field.get(statement) as Cursor? |
| 279 | + } |
| 280 | + |
236 | 281 | private fun exec(sql: String) { |
237 | 282 | connection.prepare(sql).use { it.step() } |
238 | 283 | } |
|
0 commit comments