diff --git a/data/src/androidTest/java/de/skymatic/android_issue153521693/BugTest.kt b/data/src/androidTest/java/de/skymatic/android_issue153521693/BugTest.kt index 3d7f4caa1..c0f4db973 100644 --- a/data/src/androidTest/java/de/skymatic/android_issue153521693/BugTest.kt +++ b/data/src/androidTest/java/de/skymatic/android_issue153521693/BugTest.kt @@ -43,204 +43,204 @@ import org.junit.runner.RunWith @RunWith(AndroidJUnit4::class) class BugTest { - private val TEST_DB = "bug-test" - - private val context = InstrumentationRegistry.getInstrumentation().context - - private lateinit var db: SupportSQLiteDatabase - - @Before - fun setup() { - context.getDatabasePath(TEST_DB).delete() //Clean up last test - - //The bug doesn't seem to appear if everything is done is one session (at least with this suite), so let's simulate two sessions - /* Database is created */ - SupportSQLiteOpenHelper.Configuration(context, TEST_DB, - object : SupportSQLiteOpenHelper.Callback(1) { - override fun onCreate(db: SupportSQLiteDatabase) = createDb(db) - override fun onUpgrade( - db: SupportSQLiteDatabase, - oldVersion: Int, - newVersion: Int - ) = throw AssertionError() - }).let { FrameworkSQLiteOpenHelperFactory().create(it).writableDatabase }.close() - - /* Database is closed, e.g. the app has been closed */ - - //////////////////////////////////////////// - - /* Database is opened, not created */ - - db = SupportSQLiteOpenHelper.Configuration(context, TEST_DB, - object : SupportSQLiteOpenHelper.Callback(1) { - override fun onCreate(db: SupportSQLiteDatabase) = throw AssertionError() - override fun onUpgrade( - db: SupportSQLiteDatabase, - oldVersion: Int, - newVersion: Int - ) = throw AssertionError() - }).let { FrameworkSQLiteOpenHelperFactory().create(it).writableDatabase } - } - - @After - fun tearDown() { - if (this::db.isInitialized) { - db.close() - } - } - - private fun createDb(db: SupportSQLiteDatabase) { - db.execSQL( - "CREATE TABLE `TEST_TABLE` (" + // - "`column0` TEXT NOT NULL," + - "`column1` TEXT NOT NULL" + - ")" - ) - db.execSQL("INSERT INTO `TEST_TABLE` (`column0`, `column1`) VALUES ('content0', 'content1')") - - db.version = 1 - } - - private fun modifySchema(db: SupportSQLiteDatabase) { - db.execSQL("ALTER TABLE `TEST_TABLE` RENAME TO `TEST_TABLE_OLD`") - db.execSQL( - "CREATE TABLE `TEST_TABLE` (" + // - "`column0` TEXT NOT NULL," + - "`column1` TEXT NOT NULL," + - "`column2` TEXT" + - ")" - ) - db.execSQL("INSERT INTO `TEST_TABLE` (`column0`, `column1`) SELECT `column0`, `column1` FROM `TEST_TABLE_OLD`") - db.execSQL("DROP TABLE `TEST_TABLE_OLD`") - db.update( - "TEST_TABLE", - CONFLICT_NONE, - ContentValues().also { it.put("column2", "content2"); }, - null, - null - ) - } - - private fun assertBroken(cursor: Cursor) { - //cursor.moveToFirst() //This doesn't help because it only fixes *future* cursors - assertEquals(2, cursor.columnCount) //Should be 3! - assertArrayEquals( - arrayOf("column0", "column1"), - cursor.columnNames - ) //Should be ["column0", "column1", "column2"] - assertEquals(-1, cursor.getColumnIndex("column2")) //Should be 2 - } - - private fun assertNotBroken(cursor: Cursor) { - //cursor.moveToFirst() //Not needed and it wouldn't fix this cursor either way. - assertEquals(3, cursor.columnCount) - assertArrayEquals(arrayOf("column0", "column1", "column2"), cursor.columnNames) - assertEquals(2, cursor.getColumnIndex("column2")) - } - - @Test - fun causeBugWithoutBackticks() { - db.query("SELECT * FROM TEST_TABLE").close() - - modifySchema(db) - - db.query("SELECT * FROM TEST_TABLE").use { - assertBroken(it) - } - } - - @Test - fun causeBugWithBackticks() { - db.query("SELECT * FROM `TEST_TABLE`").close() - - modifySchema(db) - - db.query("SELECT * FROM `TEST_TABLE`").use { - assertBroken(it) - } - } - - @Test - fun causeBugWithMove1() { - db.query("SELECT * FROM `TEST_TABLE`").close() - - modifySchema(db) - - db.query("SELECT * FROM `TEST_TABLE`").use { - it.moveToFirst() //This doesn't help because it only fixes *future* cursors - assertBroken(it) - } - - db.query("SELECT * FROM `TEST_TABLE`").use { - //it.moveToFirst() //Not needed and it wouldn't fix this cursor either way. - assertNotBroken(it) - } - } - - @Test - fun causeBugWithMove2() { - db.query("SELECT * FROM TEST_TABLE").close() - db.query("SELECT * FROM `TEST_TABLE`").close() - - modifySchema(db) - - db.query("SELECT * FROM `TEST_TABLE`").use { - assertBroken(it) - } - - db.query("SELECT * FROM TEST_TABLE").use { - it.moveToFirst() //This doesn't help because it only fixes *future* cursors - assertBroken(it) - } - - db.query("SELECT * FROM `TEST_TABLE`").use { - assertBroken(it) - } - } - - @Test - fun doNotCauseBugWithMixedBackticks1() { - db.query("SELECT * FROM `TEST_TABLE`").close() - - modifySchema(db) - - db.query("SELECT * FROM TEST_TABLE").use { - assertNotBroken(it) - } - } - - @Test - fun doNotCauseBugWithMixedBackticks2() { - db.query("SELECT * FROM TEST_TABLE").close() - - modifySchema(db) - - db.query("SELECT * FROM `TEST_TABLE`").use { - assertNotBroken(it) - } - } - - @Test - fun doNotCauseBugWithoutFirstCall() { - modifySchema(db) - - db.query("SELECT * FROM `TEST_TABLE`").use { - assertNotBroken(it) - } - } + private val TEST_DB = "bug-test" + + private val context = InstrumentationRegistry.getInstrumentation().context + + private lateinit var db: SupportSQLiteDatabase + + @Before + fun setup() { + context.getDatabasePath(TEST_DB).delete() //Clean up last test + + //The bug doesn't seem to appear if everything is done is one session (at least with this suite), so let's simulate two sessions + /* Database is created */ + SupportSQLiteOpenHelper.Configuration(context, TEST_DB, + object : SupportSQLiteOpenHelper.Callback(1) { + override fun onCreate(db: SupportSQLiteDatabase) = createDb(db) + override fun onUpgrade( + db: SupportSQLiteDatabase, + oldVersion: Int, + newVersion: Int + ) = throw AssertionError() + }).let { FrameworkSQLiteOpenHelperFactory().create(it).writableDatabase }.close() + + /* Database is closed, e.g. the app has been closed */ + + //////////////////////////////////////////// + + /* Database is opened, not created */ + + db = SupportSQLiteOpenHelper.Configuration(context, TEST_DB, + object : SupportSQLiteOpenHelper.Callback(1) { + override fun onCreate(db: SupportSQLiteDatabase) = throw AssertionError() + override fun onUpgrade( + db: SupportSQLiteDatabase, + oldVersion: Int, + newVersion: Int + ) = throw AssertionError() + }).let { FrameworkSQLiteOpenHelperFactory().create(it).writableDatabase } + } + + @After + fun tearDown() { + if (this::db.isInitialized) { + db.close() + } + } + + private fun createDb(db: SupportSQLiteDatabase) { + db.execSQL( + "CREATE TABLE `TEST_TABLE` (" + // + "`column0` TEXT NOT NULL," + + "`column1` TEXT NOT NULL" + + ")" + ) + db.execSQL("INSERT INTO `TEST_TABLE` (`column0`, `column1`) VALUES ('content0', 'content1')") + + db.version = 1 + } + + private fun modifySchema(db: SupportSQLiteDatabase) { + db.execSQL("ALTER TABLE `TEST_TABLE` RENAME TO `TEST_TABLE_OLD`") + db.execSQL( + "CREATE TABLE `TEST_TABLE` (" + // + "`column0` TEXT NOT NULL," + + "`column1` TEXT NOT NULL," + + "`column2` TEXT" + + ")" + ) + db.execSQL("INSERT INTO `TEST_TABLE` (`column0`, `column1`) SELECT `column0`, `column1` FROM `TEST_TABLE_OLD`") + db.execSQL("DROP TABLE `TEST_TABLE_OLD`") + db.update( + "TEST_TABLE", + CONFLICT_NONE, + ContentValues().also { it.put("column2", "content2"); }, + null, + null + ) + } + + private fun assertBroken(cursor: Cursor) { + //cursor.moveToFirst() //This doesn't help because it only fixes *future* cursors + assertEquals(2, cursor.columnCount) //Should be 3! + assertArrayEquals( + arrayOf("column0", "column1"), + cursor.columnNames + ) //Should be ["column0", "column1", "column2"] + assertEquals(-1, cursor.getColumnIndex("column2")) //Should be 2 + } + + private fun assertNotBroken(cursor: Cursor) { + //cursor.moveToFirst() //Not needed and it wouldn't fix this cursor either way. + assertEquals(3, cursor.columnCount) + assertArrayEquals(arrayOf("column0", "column1", "column2"), cursor.columnNames) + assertEquals(2, cursor.getColumnIndex("column2")) + } + + @Test + fun causeBugWithoutBackticks() { + db.query("SELECT * FROM TEST_TABLE").close() + + modifySchema(db) + + db.query("SELECT * FROM TEST_TABLE").use { + assertBroken(it) + } + } + + @Test + fun causeBugWithBackticks() { + db.query("SELECT * FROM `TEST_TABLE`").close() + + modifySchema(db) + + db.query("SELECT * FROM `TEST_TABLE`").use { + assertBroken(it) + } + } + + @Test + fun causeBugWithMove1() { + db.query("SELECT * FROM `TEST_TABLE`").close() + + modifySchema(db) + + db.query("SELECT * FROM `TEST_TABLE`").use { + it.moveToFirst() //This doesn't help because it only fixes *future* cursors + assertBroken(it) + } + + db.query("SELECT * FROM `TEST_TABLE`").use { + //it.moveToFirst() //Not needed and it wouldn't fix this cursor either way. + assertNotBroken(it) + } + } + + @Test + fun causeBugWithMove2() { + db.query("SELECT * FROM TEST_TABLE").close() + db.query("SELECT * FROM `TEST_TABLE`").close() + + modifySchema(db) + + db.query("SELECT * FROM `TEST_TABLE`").use { + assertBroken(it) + } + + db.query("SELECT * FROM TEST_TABLE").use { + it.moveToFirst() //This doesn't help because it only fixes *future* cursors + assertBroken(it) + } + + db.query("SELECT * FROM `TEST_TABLE`").use { + assertBroken(it) + } + } + + @Test + fun doNotCauseBugWithMixedBackticks1() { + db.query("SELECT * FROM `TEST_TABLE`").close() + + modifySchema(db) + + db.query("SELECT * FROM TEST_TABLE").use { + assertNotBroken(it) + } + } + + @Test + fun doNotCauseBugWithMixedBackticks2() { + db.query("SELECT * FROM TEST_TABLE").close() + + modifySchema(db) + + db.query("SELECT * FROM `TEST_TABLE`").use { + assertNotBroken(it) + } + } + + @Test + fun doNotCauseBugWithoutFirstCall() { + modifySchema(db) + + db.query("SELECT * FROM `TEST_TABLE`").use { + assertNotBroken(it) + } + } - @Test - fun doNotCauseBugWithRefresh() { - db.query("SELECT * FROM `TEST_TABLE`").close() + @Test + fun doNotCauseBugWithRefresh() { + db.query("SELECT * FROM `TEST_TABLE`").close() - modifySchema(db) + modifySchema(db) - db.query("SELECT * FROM `TEST_TABLE`").use { - it.count //Alternatively: it.moveToNext(), it.moveToFirst() - } + db.query("SELECT * FROM `TEST_TABLE`").use { + it.count //Alternatively: it.moveToNext(), it.moveToFirst() + } - db.query("SELECT * FROM `TEST_TABLE`").use { - assertNotBroken(it) - } - } + db.query("SELECT * FROM `TEST_TABLE`").use { + assertNotBroken(it) + } + } } \ No newline at end of file diff --git a/data/src/androidTest/java/de/skymatic/android_issue153521693/SmallBugTest.kt b/data/src/androidTest/java/de/skymatic/android_issue153521693/SmallBugTest.kt index 2a467326e..181b83051 100644 --- a/data/src/androidTest/java/de/skymatic/android_issue153521693/SmallBugTest.kt +++ b/data/src/androidTest/java/de/skymatic/android_issue153521693/SmallBugTest.kt @@ -41,96 +41,96 @@ import org.junit.runner.RunWith @RunWith(AndroidJUnit4::class) class SmallBugTest { - private val TEST_DB = "small-bug-test" - - private val context = InstrumentationRegistry.getInstrumentation().context - - private lateinit var db: SupportSQLiteDatabase - - @Before - fun setup() { - context.getDatabasePath(TEST_DB).delete() //Clean up last test - - //The bug doesn't seem to appear if everything is done is one session (at least with this suite), so let's simulate two sessions - /* Database is created */ - SupportSQLiteOpenHelper.Configuration(context, TEST_DB, - object : SupportSQLiteOpenHelper.Callback(1) { - override fun onCreate(db: SupportSQLiteDatabase) = createDb(db) - override fun onUpgrade( - db: SupportSQLiteDatabase, - oldVersion: Int, - newVersion: Int - ) = throw AssertionError() - }).let { FrameworkSQLiteOpenHelperFactory().create(it).writableDatabase }.close() - - /* Database is closed, e.g. the app has been closed */ - - //////////////////////////////////////////// - - /* Database is opened, not created */ - - db = SupportSQLiteOpenHelper.Configuration(context, TEST_DB, - object : SupportSQLiteOpenHelper.Callback(1) { - override fun onCreate(db: SupportSQLiteDatabase) = throw AssertionError() - override fun onUpgrade( - db: SupportSQLiteDatabase, - oldVersion: Int, - newVersion: Int - ) = throw AssertionError() - }).let { FrameworkSQLiteOpenHelperFactory().create(it).writableDatabase } - } - - @After - fun tearDown() { - if (this::db.isInitialized) { - db.close() - } - } - - private fun createDb(db: SupportSQLiteDatabase) { - db.execSQL( - "CREATE TABLE `TEST_TABLE` (" + // - "`column0` TEXT NOT NULL," + - "`column1` TEXT NOT NULL" + - ")" - ) - db.execSQL("INSERT INTO `TEST_TABLE` (`column0`, `column1`) VALUES ('content0', 'content1')") - - db.version = 1 - } - - private fun modifySchema(db: SupportSQLiteDatabase) { - db.execSQL("ALTER TABLE `TEST_TABLE` RENAME TO `TEST_TABLE_OLD`") - db.execSQL( - "CREATE TABLE `TEST_TABLE` (" + // - "`column0` TEXT NOT NULL," + - "`column1` TEXT NOT NULL," + - "`column2` TEXT" + - ")" - ) - db.execSQL("INSERT INTO `TEST_TABLE` (`column0`, `column1`) SELECT `column0`, `column1` FROM `TEST_TABLE_OLD`") - db.execSQL("DROP TABLE `TEST_TABLE_OLD`") - db.update( - "TEST_TABLE", - CONFLICT_NONE, - ContentValues().also { it.put("column2", "content2"); }, - null, - null - ) - } - - @Test - fun causeBug() { //If this test is successful, the bug occurred - db.query("SELECT * FROM TEST_TABLE").close() - - modifySchema(db) - - db.query("SELECT * FROM TEST_TABLE").use { - it.moveToFirst() - assertArrayEquals( - arrayOf("column0", "column1"), - it.columnNames - ) //Should be ["column0", "column1", "column2"] - } - } + private val TEST_DB = "small-bug-test" + + private val context = InstrumentationRegistry.getInstrumentation().context + + private lateinit var db: SupportSQLiteDatabase + + @Before + fun setup() { + context.getDatabasePath(TEST_DB).delete() //Clean up last test + + //The bug doesn't seem to appear if everything is done is one session (at least with this suite), so let's simulate two sessions + /* Database is created */ + SupportSQLiteOpenHelper.Configuration(context, TEST_DB, + object : SupportSQLiteOpenHelper.Callback(1) { + override fun onCreate(db: SupportSQLiteDatabase) = createDb(db) + override fun onUpgrade( + db: SupportSQLiteDatabase, + oldVersion: Int, + newVersion: Int + ) = throw AssertionError() + }).let { FrameworkSQLiteOpenHelperFactory().create(it).writableDatabase }.close() + + /* Database is closed, e.g. the app has been closed */ + + //////////////////////////////////////////// + + /* Database is opened, not created */ + + db = SupportSQLiteOpenHelper.Configuration(context, TEST_DB, + object : SupportSQLiteOpenHelper.Callback(1) { + override fun onCreate(db: SupportSQLiteDatabase) = throw AssertionError() + override fun onUpgrade( + db: SupportSQLiteDatabase, + oldVersion: Int, + newVersion: Int + ) = throw AssertionError() + }).let { FrameworkSQLiteOpenHelperFactory().create(it).writableDatabase } + } + + @After + fun tearDown() { + if (this::db.isInitialized) { + db.close() + } + } + + private fun createDb(db: SupportSQLiteDatabase) { + db.execSQL( + "CREATE TABLE `TEST_TABLE` (" + // + "`column0` TEXT NOT NULL," + + "`column1` TEXT NOT NULL" + + ")" + ) + db.execSQL("INSERT INTO `TEST_TABLE` (`column0`, `column1`) VALUES ('content0', 'content1')") + + db.version = 1 + } + + private fun modifySchema(db: SupportSQLiteDatabase) { + db.execSQL("ALTER TABLE `TEST_TABLE` RENAME TO `TEST_TABLE_OLD`") + db.execSQL( + "CREATE TABLE `TEST_TABLE` (" + // + "`column0` TEXT NOT NULL," + + "`column1` TEXT NOT NULL," + + "`column2` TEXT" + + ")" + ) + db.execSQL("INSERT INTO `TEST_TABLE` (`column0`, `column1`) SELECT `column0`, `column1` FROM `TEST_TABLE_OLD`") + db.execSQL("DROP TABLE `TEST_TABLE_OLD`") + db.update( + "TEST_TABLE", + CONFLICT_NONE, + ContentValues().also { it.put("column2", "content2"); }, + null, + null + ) + } + + @Test + fun causeBug() { //If this test is successful, the bug occurred + db.query("SELECT * FROM TEST_TABLE").close() + + modifySchema(db) + + db.query("SELECT * FROM TEST_TABLE").use { + it.moveToFirst() + assertArrayEquals( + arrayOf("column0", "column1"), + it.columnNames + ) //Should be ["column0", "column1", "column2"] + } + } } \ No newline at end of file