PrefMigrator { public static void migrate(SharedPreferences prefs, Migration... migrations) { for (Migration migration : migrations) { migration.apply(prefs); } } private  PrefMigrator(); static void migrate(SharedPreferences prefs, Migration... migrations); static void migrateSharedPrefs();  }
@Test public void renamerShouldRenameKeys() { initPrefs( "colour", "red", "vegetable", "eggplant" ); PrefMigrator.migrate(prefs, RENAMERS); assertPrefs( "couleur", "red", "legume", "eggplant" ); }
@Test public void translatorShouldTranslateValues() { initPrefs( "colour", "red", "vegetable", "eggplant" ); PrefMigrator.migrate(prefs, TRANSLATORS); assertPrefs( "couleur", "rouge", "legume", "aubergine" ); }
@Test public void translatorShouldNotTouchUnknownValues() { initPrefs( "colour", "blue", "vegetable", "eggplant" ); PrefMigrator.migrate(prefs, TRANSLATORS); assertPrefs( "colour", "blue", "legume", "aubergine" ); }
@Test public void renamerSouldNotReplaceExistingNewKeys() { initPrefs( "colour", "red", "vegetable", "eggplant", "couleur", "bleu", "legume", "tomate", "network", true, "connection", "wifi" ); PrefMigrator.migrate(prefs, RENAMERS); assertPrefs( "colour", "red", "vegetable", "eggplant", "couleur", "bleu", "legume", "tomate", "network", true, "connection", "wifi" ); }
@Test public void translatorShouldNotReplaceExistingNewKeys() { initPrefs( "colour", "red", "vegetable", "eggplant", "couleur", "bleu", "legume", "tomate", "network", true, "connection", "wifi" ); PrefMigrator.migrate(prefs, TRANSLATORS); assertPrefs( "colour", "red", "vegetable", "eggplant", "couleur", "bleu", "legume", "tomate", "network", true, "connection", "wifi" ); }
@Test public void translatorShouldSplitKeyAccordingToValue() { initPrefs("fruit", "pear_anjou"); PrefMigrator.migrate(prefs, KEY_SPLITTER); assertPrefs("pear", "anjou"); initPrefs("fruit", "melon_watermelon"); PrefMigrator.migrate(prefs, KEY_SPLITTER); assertPrefs("melon", "watermelon"); }
@Test public void translatorShouldMergeKeys() { initPrefs("pear", "anjou"); PrefMigrator.migrate(prefs, KEY_MERGER); assertPrefs("fruit", "pear_anjou"); initPrefs("melon", "watermelon"); PrefMigrator.migrate(prefs, KEY_MERGER); assertPrefs("fruit", "melon_watermelon"); }
@Test public void combinerShouldCombineKeys() { initPrefs("red", true, "blue", false); PrefMigrator.migrate(prefs, COMBINER); assertPrefs("mix", "red"); initPrefs("red", false, "blue", true); PrefMigrator.migrate(prefs, COMBINER); assertPrefs("mix", "blue"); initPrefs("red", true, "blue", true); PrefMigrator.migrate(prefs, COMBINER); assertPrefs("mix", "purple"); initPrefs("red", false); PrefMigrator.migrate(prefs, COMBINER); assertPrefs("red", false); initPrefs("red", false, "blue", false); PrefMigrator.migrate(prefs, COMBINER); assertPrefs("red", false, "blue", false); initPrefs("red", true); PrefMigrator.migrate(prefs, COMBINER); assertPrefs("mix", "red"); initPrefs("blue", true); PrefMigrator.migrate(prefs, COMBINER); assertPrefs("mix", "blue"); }
@Test public void combinerShouldModifyKey() { initPrefs("vehicle", "bicycle"); PrefMigrator.migrate(prefs, COMBINER_MODIFIER); assertPrefs("vehicle", "bicycle"); initPrefs("vehicle", "bicycle", "fast", true); PrefMigrator.migrate(prefs, COMBINER_MODIFIER); assertPrefs("vehicle", "fast_bicycle"); initPrefs("vehicle", "car"); PrefMigrator.migrate(prefs, COMBINER_MODIFIER); assertPrefs("vehicle", "car"); initPrefs("vehicle", "car", "fast", true); PrefMigrator.migrate(prefs, COMBINER_MODIFIER); assertPrefs("vehicle", "fast_car"); initPrefs("vehicle", "airplane", "fast", true); PrefMigrator.migrate(prefs, COMBINER_MODIFIER); assertPrefs("vehicle", "airplane", "fast", true); }