PreferencesUtils { public static <T> void save(Property<? extends T> property, Preferences preferences, Function<? super T, String> serializer) { T value = property.getValue(); if (value == null) { throw new IllegalArgumentException("The property must have a value"); } preferences.put(property.getName(), serializer.apply(value)); } private  PreferencesUtils(); static void save(Property<? extends T> property,
                              Preferences preferences,
                              Function<? super T, String> serializer); static void save(IntegerProperty property, Preferences preferences); static void save(LongProperty property, Preferences preferences); static void save(DoubleProperty property, Preferences preferences); static void save(BooleanProperty property, Preferences preferences); static void save(StringProperty property, Preferences preferences); static void read(Property<? super T> property,
                              Preferences preferences,
                              Function<String, ? extends T> parser); static void read(IntegerProperty property, Preferences preferences); static void read(LongProperty property, Preferences preferences); static void read(DoubleProperty property, Preferences preferences); static void read(BooleanProperty property, Preferences preferences); static void read(StringProperty property, Preferences preferences);  }
@Test public void testSaveThrowsWithNullValue() { Property<?> property = new SimpleObjectProperty<>(null); assertThrows(IllegalArgumentException.class, () -> PreferencesUtils.save(property, preferences, null)); }
@Test public void testSaveInt() { String name = "int"; int value = Integer.MAX_VALUE; IntegerProperty property = new SimpleIntegerProperty(null, name, value); PreferencesUtils.save(property, preferences); int saved = preferences.getInt(name, -1); assertEquals(value, saved); }
@Test public void testSaveLong() { String name = "long"; long value = Long.MAX_VALUE; LongProperty property = new SimpleLongProperty(null, name, value); PreferencesUtils.save(property, preferences); long saved = preferences.getLong(name, -1); assertEquals(value, saved); }
@Test public void testSaveDouble() { String name = "double"; double value = Double.MAX_VALUE; DoubleProperty property = new SimpleDoubleProperty(null, name, value); PreferencesUtils.save(property, preferences); double saved = preferences.getDouble(name, -1); assertEquals(value, saved); }
@Test public void testSaveBoolean() { String name = "boolean"; boolean value = true; BooleanProperty property = new SimpleBooleanProperty(null, name, value); PreferencesUtils.save(property, preferences); boolean saved = preferences.getBoolean(name, !value); assertEquals(value, saved); }
@Test public void testSaveString() { String name = "string"; String value = "foobar"; StringProperty property = new SimpleStringProperty(null, name, value); PreferencesUtils.save(property, preferences); String saved = preferences.get(name, null); assertEquals(value, saved); }