PreferenceValidator { public void validate(final Setting setting) throws PreferenceValidationException { if (setting instanceof FlagSetting) { this.validate((FlagSetting) setting); } else if (setting instanceof RangeSetting) { this.validate((RangeSetting) setting); } else { throw new IllegalStateException("There is no validator for the given type"); } } PreferenceValidator(final UserPreference userPreference); void validate(final Setting setting);  }
@Test public void testFlagValid() { final UserPreference up = new UserPreference("id", "1", NAME, true); this.validator = new PreferenceValidator(up); try { this.validator.validate(this.flag); } catch (final PreferenceValidationException e) { fail(ERROR); } }
@Test public void testFlagInvalidType() { final UserPreference up = new UserPreference("id", "1", NAME, 0.5); this.validator = new PreferenceValidator(up); assertThrows(PreferenceValidationException.class, () -> this.validator.validate(this.flag)); }
@Test public void testRangeValid() { final UserPreference up = new UserPreference("id", "1", NAME, 1); this.validator = new PreferenceValidator(up); try { this.validator.validate(this.range); } catch (final PreferenceValidationException e) { e.printStackTrace(); fail(ERROR); } }
@Test public void testRangeLessThanMin() { final UserPreference up = new UserPreference("id", "1", NAME, MIN - 0.1); this.validator = new PreferenceValidator(up); assertThrows(PreferenceValidationException.class, () -> this.validator.validate(this.range)); }
@Test public void testRangeGreatherThanMax() { final UserPreference up = new UserPreference("id", "1", NAME, MAX + 0.1); this.validator = new PreferenceValidator(up); assertThrows(PreferenceValidationException.class, () -> this.validator.validate(this.range)); }
@Test public void testRangeInvalidType() { final UserPreference up = new UserPreference("id", "1", NAME, "notadouble"); this.validator = new PreferenceValidator(up); assertThrows(PreferenceValidationException.class, () -> this.validator.validate(this.range)); }