FormMetadataMigrator { @SuppressLint("ApplySharedPref") public static void migrate(SharedPreferences sharedPreferences) { boolean migrationAlreadyDone = sharedPreferences.getBoolean(KEY_METADATA_MIGRATED, false); Timber.i("migrate called, %s", migrationAlreadyDone ? "migration already done" : "will migrate"); if (!migrationAlreadyDone) { SharedPreferences.Editor editor = sharedPreferences.edit(); for (String[] pair : SOURCE_TARGET_VALUE_PAIRS) { String migratingValue = sharedPreferences.getString(pair[0], "").trim(); if (!migratingValue.isEmpty()) { Timber.i("Copying %s from %s to %s", migratingValue, pair[0], pair[1]); editor.putString(pair[1], migratingValue); } } editor.putBoolean(KEY_METADATA_MIGRATED, true); editor.commit(); } } private  FormMetadataMigrator(); @SuppressLint("ApplySharedPref") static void migrate(SharedPreferences sharedPreferences);  }
@Test public void shouldMigrateDataCorrectly() { setPreferencesToPreMigrationValues(); displayAffectedPreferences("Before calling migrate"); migrate(sharedPreferences); displayAffectedPreferences("After calling migrate"); checkPostMigrationValues(); setPreferencesToValues(modifiedMetadataValuePairs); displayAffectedPreferences("After changing metadata"); migrate(sharedPreferences); displayAffectedPreferences("After calling migrate again"); ensureSecondMigrationCallPreservesMetadata(); }