|
| 1 | +package net.zetetic.database.sqlcipher_cts; |
| 2 | + |
| 3 | +import static org.junit.Assert.assertEquals; |
| 4 | + |
| 5 | +import android.content.Context; |
| 6 | + |
| 7 | +import androidx.annotation.CallSuper; |
| 8 | +import androidx.annotation.NonNull; |
| 9 | +import androidx.sqlite.db.SupportSQLiteDatabase; |
| 10 | +import androidx.sqlite.db.SupportSQLiteOpenHelper; |
| 11 | +import androidx.test.ext.junit.runners.AndroidJUnit4; |
| 12 | +import androidx.test.platform.app.InstrumentationRegistry; |
| 13 | + |
| 14 | +import net.zetetic.database.sqlcipher.SupportHelper; |
| 15 | + |
| 16 | +import org.junit.Before; |
| 17 | +import org.junit.Test; |
| 18 | +import org.junit.runner.RunWith; |
| 19 | + |
| 20 | +@RunWith(AndroidJUnit4.class) |
| 21 | +public class SupportHelperTest { |
| 22 | + |
| 23 | + private static final String DATABASE_NAME = "DB-Test.db"; |
| 24 | + private static final int CREATION_INDEX = 0; |
| 25 | + private static final int UPGRADE_INDEX = 1; |
| 26 | + |
| 27 | + @Before |
| 28 | + public void setup() { |
| 29 | + Context context = InstrumentationRegistry.getInstrumentation().getTargetContext(); |
| 30 | + System.loadLibrary("sqlcipher"); |
| 31 | + for (String databaseName : context.databaseList()) { |
| 32 | + context.deleteDatabase(databaseName); |
| 33 | + } |
| 34 | + } |
| 35 | + |
| 36 | + @Test |
| 37 | + public void shouldCreateDatabaseNormallyWithInitialVersion() { |
| 38 | + FakeCallback callbackWrapper = new FakeCallback(1); |
| 39 | + |
| 40 | + SupportSQLiteOpenHelper.Configuration configuration = createConfiguration(callbackWrapper); |
| 41 | + SupportHelper helper = new SupportHelper(configuration, null, null, true); |
| 42 | + |
| 43 | + helper.getWritableDatabase(); |
| 44 | + helper.close(); |
| 45 | + |
| 46 | + assertEquals(1, callbackWrapper.callbackCount[CREATION_INDEX]); |
| 47 | + assertEquals(0, callbackWrapper.callbackCount[UPGRADE_INDEX]); |
| 48 | + } |
| 49 | + |
| 50 | + @Test |
| 51 | + public void shouldRunUpgradeFromVersion1ToVersion2() { |
| 52 | + FakeCallback initialCallback = new FakeCallback(1); |
| 53 | + |
| 54 | + SupportHelper initialHelper = new SupportHelper(createConfiguration(initialCallback), null, null, true); |
| 55 | + |
| 56 | + initialHelper.getWritableDatabase(); |
| 57 | + initialHelper.close(); |
| 58 | + |
| 59 | + assertEquals(1, initialCallback.callbackCount[CREATION_INDEX]); |
| 60 | + assertEquals(0, initialCallback.callbackCount[UPGRADE_INDEX]); |
| 61 | + |
| 62 | + FakeCallback callbackWrapper = new FakeCallback(2); |
| 63 | + |
| 64 | + // minSupportedVersion = 1 |
| 65 | + SupportHelper helper = new SupportHelper(createConfiguration(callbackWrapper), null, null, true, 1); |
| 66 | + |
| 67 | + helper.getWritableDatabase(); |
| 68 | + helper.close(); |
| 69 | + |
| 70 | + assertEquals(0, callbackWrapper.callbackCount[CREATION_INDEX]); |
| 71 | + assertEquals(1, callbackWrapper.callbackCount[UPGRADE_INDEX]); |
| 72 | + } |
| 73 | + |
| 74 | + private SupportSQLiteOpenHelper.Configuration createConfiguration(SupportSQLiteOpenHelper.Callback callback) { |
| 75 | + Context context = InstrumentationRegistry.getInstrumentation().getTargetContext(); |
| 76 | + return SupportSQLiteOpenHelper.Configuration.builder(context) |
| 77 | + .name(DATABASE_NAME) |
| 78 | + .callback(callback) |
| 79 | + .build(); |
| 80 | + } |
| 81 | + |
| 82 | + private static class FakeCallback extends SupportSQLiteOpenHelper.Callback { |
| 83 | + public final int[] callbackCount = {0, 0}; |
| 84 | + |
| 85 | + public FakeCallback(int version) { |
| 86 | + super(version); |
| 87 | + } |
| 88 | + |
| 89 | + SupportSQLiteOpenHelper.Callback callback = new SupportSQLiteOpenHelper.Callback(version) { |
| 90 | + @Override |
| 91 | + public void onCreate(@NonNull SupportSQLiteDatabase db) { |
| 92 | + callbackCount[CREATION_INDEX] += 1; |
| 93 | + } |
| 94 | + |
| 95 | + @Override |
| 96 | + public void onUpgrade(@NonNull SupportSQLiteDatabase db, int oldVersion, int newVersion) { |
| 97 | + callbackCount[UPGRADE_INDEX] += 1; |
| 98 | + } |
| 99 | + }; |
| 100 | + |
| 101 | + @Override |
| 102 | + @CallSuper |
| 103 | + public void onCreate(@NonNull SupportSQLiteDatabase db) { |
| 104 | + callback.onCreate(db); |
| 105 | + } |
| 106 | + |
| 107 | + @Override |
| 108 | + @CallSuper |
| 109 | + public void onUpgrade(@NonNull SupportSQLiteDatabase db, int oldVersion, int newVersion) { |
| 110 | + callback.onUpgrade(db, oldVersion, newVersion); |
| 111 | + } |
| 112 | + } |
| 113 | +} |
0 commit comments