-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improve the fidelity of legacy SQLite error messages
Previously, in the legacy SQLite mode, all exceptions thrown by SQLite were wrapped in an arbitrary RuntimeExceptions. This is not consistent with real Android, which always throws exceptions of type android.database.sqlite.SQLiteException (including subclasses). This has impact on data layer libraries such as Room that explicitly catch Android SQLiteExceptions in order to execute error callback logic. For #8469 PiperOrigin-RevId: 582331582
- Loading branch information
Showing
10 changed files
with
347 additions
and
79 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import org.robolectric.gradle.AndroidProjectConfigPlugin | ||
|
||
apply plugin: 'com.android.library' | ||
apply plugin: AndroidProjectConfigPlugin | ||
|
||
android { | ||
compileSdk 34 | ||
namespace 'org.robolectric.integrationtests.room' | ||
|
||
defaultConfig { | ||
minSdk 19 | ||
targetSdk 34 | ||
} | ||
|
||
compileOptions { | ||
sourceCompatibility = '1.8' | ||
targetCompatibility = '1.8' | ||
} | ||
|
||
testOptions { | ||
unitTests { | ||
includeAndroidResources = true | ||
} | ||
} | ||
|
||
} | ||
|
||
dependencies { | ||
// Testing dependencies | ||
testImplementation project(path: ':testapp') | ||
testImplementation project(":robolectric") | ||
testImplementation libs.junit4 | ||
testImplementation libs.guava.testlib | ||
testImplementation libs.guava.testlib | ||
testImplementation libs.truth | ||
implementation 'androidx.room:room-runtime:2.6.0' | ||
annotationProcessor 'androidx.room:room-compiler:2.6.0' | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<!-- | ||
Manifest for androidx memoryleaks test module | ||
--> | ||
<manifest xmlns:android="http://schemas.android.com/apk/res/android" | ||
package="org.robolectric.integrationtests.room"> | ||
|
||
<application> | ||
</application> | ||
</manifest> |
15 changes: 15 additions & 0 deletions
15
integration_tests/room/src/main/java/org/robolectric/integrationtests/room/User.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package org.robolectric.integrationtests.room; | ||
|
||
import androidx.room.Entity; | ||
|
||
/** A simple User entity */ | ||
@Entity( | ||
tableName = "users", | ||
primaryKeys = {"id"}) | ||
public class User { | ||
public long id; | ||
|
||
public String username; | ||
|
||
public String email; | ||
} |
20 changes: 20 additions & 0 deletions
20
integration_tests/room/src/main/java/org/robolectric/integrationtests/room/UserDao.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package org.robolectric.integrationtests.room; | ||
|
||
import androidx.room.Dao; | ||
import androidx.room.Insert; | ||
import androidx.room.Query; | ||
import androidx.room.Upsert; | ||
import java.util.List; | ||
|
||
/** Dao for {@link User} */ | ||
@Dao | ||
public interface UserDao { | ||
@Upsert | ||
void upsert(User user); | ||
|
||
@Insert | ||
void insert(User user); | ||
|
||
@Query("SELECT * FROM users") | ||
List<User> getAllUsers(); | ||
} |
24 changes: 24 additions & 0 deletions
24
integration_tests/room/src/main/java/org/robolectric/integrationtests/room/UserDatabase.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package org.robolectric.integrationtests.room; | ||
|
||
import android.content.Context; | ||
import androidx.room.Database; | ||
import androidx.room.Room; | ||
import androidx.room.RoomDatabase; | ||
|
||
/** Room database for {@link User} */ | ||
@Database( | ||
entities = {User.class}, | ||
version = 1, | ||
exportSchema = false) | ||
public abstract class UserDatabase extends RoomDatabase { | ||
|
||
public abstract UserDao userDao(); | ||
|
||
public static synchronized UserDatabase getInstance(Context context) { | ||
return Room.databaseBuilder( | ||
context.getApplicationContext(), UserDatabase.class, "user_database") | ||
.fallbackToDestructiveMigration() | ||
.allowMainThreadQueries() | ||
.build(); | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
...tion_tests/room/src/test/java/org/robolectric/integrationtests/room/UserDatabaseTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package org.robolectric.integrationtests.room; | ||
|
||
import static com.google.common.truth.Truth.assertThat; | ||
|
||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.robolectric.RobolectricTestRunner; | ||
import org.robolectric.RuntimeEnvironment; | ||
import org.robolectric.annotation.SQLiteMode; | ||
|
||
@RunWith(RobolectricTestRunner.class) | ||
public final class UserDatabaseTest { | ||
|
||
/** | ||
* There was an issue using Room with {@link SQLiteMode.Mode.LEGACY}. The {@link | ||
* android.database.sqlite.SQLiteException} exceptions were wrapped in a way that was not | ||
* compatible with Room. | ||
*/ | ||
@Test | ||
@SQLiteMode(SQLiteMode.Mode.LEGACY) | ||
public void upsert_conflict_usingLegacySQLite() { | ||
UserDatabase db = UserDatabase.getInstance(RuntimeEnvironment.getApplication()); | ||
|
||
User user = new User(); | ||
user.id = 12; | ||
user.username = "username"; | ||
user.email = "username@example.com"; | ||
|
||
UserDao dao = db.userDao(); | ||
dao.upsert(user); | ||
dao.upsert(user); // Should succeed | ||
|
||
assertThat(dao.getAllUsers()).hasSize(1); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.