Search in sources :

Example 11 with PreferenceForwarder

use of org.eclipse.core.internal.preferences.legacy.PreferenceForwarder in project eclipse.platform.runtime by eclipse.

the class PreferenceForwarderTest method testString.

public void testString() {
    Preferences ps = new PreferenceForwarder(getUniqueString());
    final String k1 = "key1";
    final String[] values = { "", "hello", " x ", "\n" };
    assertEquals("1.0", "", Preferences.STRING_DEFAULT_DEFAULT);
    assertEquals("1.1", ps.getString(k1), Preferences.STRING_DEFAULT_DEFAULT);
    for (String v1 : values) {
        String v2 = v1 + "x";
        ps.setValue(k1, v1);
        assertEquals("1.2", v1, ps.getString(k1));
        ps.setDefault(k1, v2);
        assertEquals("1.3", v2, ps.getDefaultString(k1));
    }
}
Also used : IEclipsePreferences(org.eclipse.core.runtime.preferences.IEclipsePreferences) PreferenceForwarder(org.eclipse.core.internal.preferences.legacy.PreferenceForwarder)

Example 12 with PreferenceForwarder

use of org.eclipse.core.internal.preferences.legacy.PreferenceForwarder in project eclipse.platform.runtime by eclipse.

the class PreferenceForwarderTest method testNeedsSaving.

public void testNeedsSaving() {
    Preferences ps = new PreferenceForwarder(getUniqueString());
    // setValue dirties
    ps = new PreferenceForwarder(getUniqueString());
    assertEquals("1.0", false, ps.needsSaving());
    ps.setValue("b1", true);
    assertEquals("1.1", true, ps.needsSaving());
    ps = new PreferenceForwarder(getUniqueString());
    assertEquals("2.0", false, ps.needsSaving());
    ps.setValue("i1", 1);
    assertEquals("2.1", true, ps.needsSaving());
    ps = new PreferenceForwarder(getUniqueString());
    assertEquals("3.0", false, ps.needsSaving());
    ps.setValue("l1", 2L);
    assertEquals("3.1", true, ps.needsSaving());
    ps = new PreferenceForwarder(getUniqueString());
    assertEquals("4.0", false, ps.needsSaving());
    ps.setValue("f1", 1.0f);
    assertEquals("4.1", true, ps.needsSaving());
    ps = new PreferenceForwarder(getUniqueString());
    assertEquals("5.0", false, ps.needsSaving());
    ps.setValue("d1", 1.0);
    assertEquals("5.1", true, ps.needsSaving());
    ps = new PreferenceForwarder(getUniqueString());
    assertEquals("6.0", false, ps.needsSaving());
    ps.setValue("s1", "x");
    assertEquals("6.1", true, ps.needsSaving());
    // setToDefault does not dirty if value not set
    ps = new PreferenceForwarder(getUniqueString());
    assertEquals("7.0", false, ps.needsSaving());
    ps.setToDefault("any");
    assertEquals("7.1", false, ps.needsSaving());
    // setToDefault dirties if value was set
    try {
        ps = new PreferenceForwarder(getUniqueString());
        assertEquals("7.2", false, ps.needsSaving());
        ps.setValue("any", "x");
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        ps.store(out, "test header");
        ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
        ps.load(in);
        assertEquals("7.3", false, ps.needsSaving());
        ps.setToDefault("any");
        assertEquals("7.4", true, ps.needsSaving());
    } catch (IOException e) {
        assertTrue("7.5", false);
    }
    // setDefault, getT, getDefaultT do not dirty
    ps = new PreferenceForwarder(getUniqueString());
    assertEquals("8.0", false, ps.needsSaving());
    ps.setDefault("b1", true);
    ps.getBoolean("b1");
    ps.getDefaultBoolean("b1");
    ps.setDefault("i1", 1);
    ps.getInt("i1");
    ps.getDefaultInt("i1");
    ps.setDefault("l1", 1L);
    ps.getLong("l1");
    ps.getDefaultLong("l1");
    ps.setDefault("f1", 1.0f);
    ps.getFloat("f1");
    ps.getDefaultFloat("f1");
    ps.setDefault("d1", 1.0);
    ps.getDouble("d1");
    ps.getDefaultDouble("d1");
    ps.setDefault("s1", "x");
    ps.getString("s1");
    ps.getDefaultString("s1");
    assertEquals("8.1", false, ps.needsSaving());
    try {
        ps = new PreferenceForwarder(getUniqueString());
        assertEquals("9.1", false, ps.needsSaving());
        ps.setValue("b1", true);
        assertEquals("9.2", true, ps.needsSaving());
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        // store makes not dirty
        ps.store(out, "test header");
        assertEquals("9.3", false, ps.needsSaving());
        // load comes in not dirty
        ps.setValue("b1", false);
        assertEquals("9.4", true, ps.needsSaving());
        ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
        ps.load(in);
        assertEquals("9.5", false, ps.needsSaving());
    } catch (IOException e) {
        assertTrue("9.0", false);
    }
}
Also used : IEclipsePreferences(org.eclipse.core.runtime.preferences.IEclipsePreferences) PreferenceForwarder(org.eclipse.core.internal.preferences.legacy.PreferenceForwarder)

Example 13 with PreferenceForwarder

use of org.eclipse.core.internal.preferences.legacy.PreferenceForwarder in project eclipse.platform.runtime by eclipse.

the class PreferenceForwarderTest method testPropertyNames.

public void testPropertyNames() {
    Preferences ps = new PreferenceForwarder(getUniqueString());
    // there are no properties initially
    assertEquals("1.0", 0, ps.propertyNames().length);
    String[] keys = { "a", "b", "c", "d" };
    // setting defaults does not add name to set
    for (String key : keys) {
        ps.setDefault(key, "default");
    }
    assertEquals("1.1", 0, ps.propertyNames().length);
    // setting real values does add name to set
    for (String key : keys) {
        ps.setValue(key, "actual");
    }
    assertEquals("1.2", keys.length, ps.propertyNames().length);
    Set<String> s1 = new HashSet<>(Arrays.asList(keys));
    Set<String> s2 = new HashSet<>(Arrays.asList(ps.propertyNames()));
    assertEquals("1.3", s1, s2);
    // setting to default does remove name from set
    for (int i = 0; i < keys.length; i++) {
        ps.setToDefault(keys[i]);
        Set<String> s = new HashSet<>(Arrays.asList(ps.propertyNames()));
        assertTrue("1.4", !s.contains(keys[i]));
    }
    assertEquals("1.5", 0, ps.propertyNames().length);
}
Also used : IEclipsePreferences(org.eclipse.core.runtime.preferences.IEclipsePreferences) PreferenceForwarder(org.eclipse.core.internal.preferences.legacy.PreferenceForwarder)

Example 14 with PreferenceForwarder

use of org.eclipse.core.internal.preferences.legacy.PreferenceForwarder in project eclipse.platform.runtime by eclipse.

the class PreferenceForwarderTest method testLoadStore.

public void testLoadStore() {
    final Preferences ps = new PreferenceForwarder(getUniqueString());
    ps.setValue("b1", true);
    ps.setValue("i1", 1);
    ps.setValue("l1", 2L);
    ps.setValue("f1", 1.0f);
    ps.setValue("d1", 1.0);
    ps.setValue("s1", "x");
    String[] keys = { "b1", "i1", "l1", "f1", "d1", "s1" };
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    try {
        ps.store(out, "test header");
    } catch (IOException e) {
        assertTrue("0.1", false);
    }
    ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
    final Preferences ps2 = new PreferenceForwarder(getUniqueString());
    try {
        ps2.load(in);
    } catch (IOException e) {
        assertTrue("0.2", false);
    }
    assertEquals("1.1", true, ps2.getBoolean("b1"));
    assertEquals("1.2", 1, ps2.getInt("i1"));
    assertEquals("1.3", 2L, ps2.getLong("l1"));
    assertEquals("1.4", 1.0f, ps2.getFloat("f1"), 1e-20f);
    assertEquals("1.5", 1.0, ps2.getDouble("d1"), 1e-20);
    assertEquals("1.6", "x", ps2.getString("s1"));
    Set<String> s1 = new HashSet<>(Arrays.asList(keys));
    Set<String> s2 = new HashSet<>(Arrays.asList(ps2.propertyNames()));
    assertEquals("1.7", s1, s2);
    // load discards current values
    in = new ByteArrayInputStream(out.toByteArray());
    final Preferences ps3 = new PreferenceForwarder(getUniqueString());
    ps3.setValue("s1", "y");
    try {
        ps3.load(in);
        assertEquals("1.8", "x", ps3.getString("s1"));
        Set<String> k1 = new HashSet<>(Arrays.asList(keys));
        Set<String> k2 = new HashSet<>(Arrays.asList(ps3.propertyNames()));
        assertEquals("1.9", k1, k2);
    } catch (IOException e) {
        assertTrue("1.10", false);
    }
}
Also used : IEclipsePreferences(org.eclipse.core.runtime.preferences.IEclipsePreferences) PreferenceForwarder(org.eclipse.core.internal.preferences.legacy.PreferenceForwarder)

Example 15 with PreferenceForwarder

use of org.eclipse.core.internal.preferences.legacy.PreferenceForwarder in project eclipse.platform.runtime by eclipse.

the class PreferenceForwarderTest method testBasics.

public void testBasics() {
    Preferences ps = new PreferenceForwarder(getUniqueString());
    final String k1 = "key1";
    final String v1 = "1";
    final String v2 = "2";
    final String v3 = "3";
    // check that a random property in a newly created store
    // appearchs to have default-default values of whatever type asked for
    assertEquals("1.0", true, ps.isDefault(k1));
    assertEquals("1.1", Preferences.BOOLEAN_DEFAULT_DEFAULT, ps.getBoolean(k1));
    assertEquals("1.2", Preferences.INT_DEFAULT_DEFAULT, ps.getInt(k1));
    assertEquals("1.3", Preferences.LONG_DEFAULT_DEFAULT, ps.getLong(k1));
    assertEquals("1.4", Preferences.FLOAT_DEFAULT_DEFAULT, ps.getFloat(k1), 0.0f);
    assertEquals("1.5", Preferences.DOUBLE_DEFAULT_DEFAULT, ps.getDouble(k1), 0.0);
    assertEquals("1.6", Preferences.STRING_DEFAULT_DEFAULT, ps.getString(k1));
    assertEquals("1.7", Preferences.BOOLEAN_DEFAULT_DEFAULT, ps.getDefaultBoolean(k1));
    assertEquals("1.8", Preferences.INT_DEFAULT_DEFAULT, ps.getDefaultInt(k1));
    assertEquals("1.9", Preferences.LONG_DEFAULT_DEFAULT, ps.getDefaultLong(k1));
    assertEquals("1.10", Preferences.FLOAT_DEFAULT_DEFAULT, ps.getDefaultFloat(k1), 0.0f);
    assertEquals("1.11", Preferences.DOUBLE_DEFAULT_DEFAULT, ps.getDefaultDouble(k1), 0.0);
    assertEquals("1.12", Preferences.STRING_DEFAULT_DEFAULT, ps.getDefaultString(k1));
    // test set/getString
    // give it a value
    ps.setValue(k1, v1);
    assertTrue("2.0", ps.isDefault(k1) == false);
    assertEquals("2.1", v1, ps.getString(k1));
    assertEquals("2.2", Preferences.STRING_DEFAULT_DEFAULT, ps.getDefaultString(k1));
    // change the value
    ps.setValue(k1, v2);
    assertTrue("2.3", ps.isDefault(k1) == false);
    assertEquals("2.4", v2, ps.getString(k1));
    assertEquals("2.5", Preferences.STRING_DEFAULT_DEFAULT, ps.getDefaultString(k1));
    // change to same value as default
    ps.setValue(k1, ps.getDefaultString(k1));
    assertTrue("2.6", ps.isDefault(k1) == true);
    assertEquals("2.7", ps.getDefaultString(k1), ps.getString(k1));
    assertEquals("2.8", Preferences.STRING_DEFAULT_DEFAULT, ps.getDefaultString(k1));
    // reset to default
    ps.setValue(k1, v2);
    ps.setToDefault(k1);
    assertTrue("2.9", ps.isDefault(k1) == true);
    assertEquals("2.10", Preferences.STRING_DEFAULT_DEFAULT, ps.getString(k1));
    assertEquals("2.11", Preferences.STRING_DEFAULT_DEFAULT, ps.getDefaultString(k1));
    // change default
    ps.setDefault(k1, v1);
    assertTrue("2.12", ps.isDefault(k1) == true);
    assertEquals("2.13", v1, ps.getString(k1));
    assertEquals("2.14", v1, ps.getDefaultString(k1));
    // set the value
    ps.setValue(k1, v2);
    assertTrue("2.15", ps.isDefault(k1) == false);
    assertEquals("2.16", v2, ps.getString(k1));
    assertEquals("2.17", v1, ps.getDefaultString(k1));
    // change to same value as default
    ps.setValue(k1, ps.getDefaultString(k1));
    assertTrue("2.18", ps.isDefault(k1) == true);
    assertEquals("2.19", ps.getDefaultString(k1), ps.getString(k1));
    assertEquals("2.20", v1, ps.getDefaultString(k1));
    // reset to default
    ps.setValue(k1, v2);
    ps.setToDefault(k1);
    assertTrue("2.21", ps.isDefault(k1) == true);
    assertEquals("2.22", v1, ps.getString(k1));
    assertEquals("2.23", v1, ps.getDefaultString(k1));
    // change default
    ps.setDefault(k1, v3);
    assertTrue("2.24", ps.isDefault(k1) == true);
    assertEquals("2.25", v3, ps.getString(k1));
    assertEquals("2.26", v3, ps.getDefaultString(k1));
}
Also used : IEclipsePreferences(org.eclipse.core.runtime.preferences.IEclipsePreferences) PreferenceForwarder(org.eclipse.core.internal.preferences.legacy.PreferenceForwarder)

Aggregations

PreferenceForwarder (org.eclipse.core.internal.preferences.legacy.PreferenceForwarder)15 IEclipsePreferences (org.eclipse.core.runtime.preferences.IEclipsePreferences)15 IPropertyChangeListener (org.eclipse.core.runtime.Preferences.IPropertyChangeListener)2 PropertyChangeEvent (org.eclipse.core.runtime.Preferences.PropertyChangeEvent)2 BackingStoreException (org.osgi.service.prefs.BackingStoreException)1