PreferenceValidator { boolean isValid(Map<String, Object> preferences) { for (Map.Entry<String, Object> entry : defaults.entrySet()) { Object defaultValue = entry.getValue(); Object newValue = preferences.get(entry.getKey()); if (newValue != null) { if (!defaultValue.getClass().isAssignableFrom(newValue.getClass())) { return false; } } } return true; } PreferenceValidator(Map<String, Object> defaults);   }
@Test public void allowsNullNewValues() { defaults.put("null", false); newValues.put("null", null); assertTrue(validator.isValid(newValues)); }
@Test public void allowsNewAndMissingKeys() { defaults.put("one", false); newValues.put("two", false); assertTrue(validator.isValid(newValues)); }
@Test public void validatesBooleanValues() { defaults.put("boolean", false); newValues.put("boolean", "blah"); assertFalse(validator.isValid(newValues)); }
@Test public void validatesStringValues() { defaults.put("string", "blah"); newValues.put("string", false); assertFalse(validator.isValid(newValues)); }
@Test public void validatesIntValues() { defaults.put("int", 1); newValues.put("int", "blah"); assertFalse(validator.isValid(newValues)); }
@Test public void validatesLongValues() { defaults.put("long", 1L); newValues.put("long", "blah"); assertFalse(validator.isValid(newValues)); }
@Test public void validatesFloatValues() { defaults.put("float", 1f); newValues.put("float", "blah"); assertFalse(validator.isValid(newValues)); }
@Test public void isValidPreference_validatesStringSetValues() { defaults.put("string_set", new HashSet<String>()); newValues.put("string_set", "blah"); assertFalse(validator.isValid(newValues)); }