Search in sources :

Example 26 with Preferences

use of org.osgi.service.prefs.Preferences in project eclipse.platform.runtime by eclipse.

the class EclipsePreferencesTest method testClear.

public void testClear() {
    Preferences node = getScopeRoot().node(getUniqueString());
    String[] keys = new String[] { "foo", "bar", "quux" };
    String[] values = new String[] { getUniqueString(), getUniqueString(), getUniqueString() };
    // none to start with
    try {
        assertEquals("1.0", 0, node.keys().length);
    } catch (BackingStoreException e) {
        fail("1.99", e);
    }
    // fill the node up with values
    try {
        for (int i = 0; i < keys.length; i++) node.put(keys[i], values[i]);
        assertEquals("2.0", keys.length, node.keys().length);
        assertEquals("2.1", keys, node.keys(), false);
    } catch (BackingStoreException e) {
        fail("2.99", e);
    }
    // clear the values and check
    try {
        node.clear();
        assertEquals("3.0", 0, node.keys().length);
        for (int i = 0; i < keys.length; i++) assertNull("3.1." + i, node.get(keys[i], null));
    } catch (BackingStoreException e) {
        fail("3.99", e);
    }
}
Also used : BackingStoreException(org.osgi.service.prefs.BackingStoreException) Preferences(org.osgi.service.prefs.Preferences) EclipsePreferences(org.eclipse.core.internal.preferences.EclipsePreferences)

Example 27 with Preferences

use of org.osgi.service.prefs.Preferences in project eclipse.platform.runtime by eclipse.

the class EclipsePreferencesTest method testParent.

public void testParent() {
    // parent of the root is null
    assertNull("1.0", Platform.getPreferencesService().getRootNode().parent());
    // parent of the scope root is the root
    Preferences node = Platform.getPreferencesService().getRootNode().node(TestScope.SCOPE);
    Preferences parent = node.parent();
    assertEquals("2.0", "/", parent.absolutePath());
    // parent of a child is the scope root
    node = getScopeRoot().node(getUniqueString());
    parent = node.parent();
    assertEquals("2.0", "/" + TestScope.SCOPE, parent.absolutePath());
}
Also used : Preferences(org.osgi.service.prefs.Preferences) EclipsePreferences(org.eclipse.core.internal.preferences.EclipsePreferences)

Example 28 with Preferences

use of org.osgi.service.prefs.Preferences in project eclipse.platform.runtime by eclipse.

the class EclipsePreferencesTest method test_342709.

/*
	 * Bug 342709 - [prefs] Don't write date/timestamp comment in preferences file
	 */
public void test_342709() {
    // set some prefs
    IEclipsePreferences root = Platform.getPreferencesService().getRootNode();
    String one = getUniqueString();
    String two = getUniqueString();
    String three = getUniqueString();
    String key = "key";
    String value = "value";
    Preferences node = root.node(TestScope2.SCOPE).node(one).node(two).node(three);
    node.put(key, value);
    // save the prefs to disk
    try {
        node.flush();
    } catch (BackingStoreException e) {
        fail("1.99", e);
    }
    assertTrue("2.0", node instanceof TestScope2);
    // read the file outside of the pref mechanism
    IPath location = ((TestScope2) node).getLocation();
    Collection<String> lines = null;
    try {
        lines = read(location);
    } catch (IOException e) {
        fail("4.99", e);
    }
    // ensure there is no comment or timestamp in the file
    for (String line : lines) {
        assertFalse("3." + line, line.startsWith("#"));
    }
}
Also used : BackingStoreException(org.osgi.service.prefs.BackingStoreException) Preferences(org.osgi.service.prefs.Preferences) EclipsePreferences(org.eclipse.core.internal.preferences.EclipsePreferences)

Example 29 with Preferences

use of org.osgi.service.prefs.Preferences in project eclipse.platform.runtime by eclipse.

the class EclipsePreferencesTest method testString.

public void testString() {
    String qualifier = getUniqueString();
    Preferences prefs = getScopeRoot().node(qualifier);
    final String key = "key1";
    final String defaultValue = null;
    final String[] values = { "", "hello", " x ", "\n" };
    try {
        // nothing there so expect the default
        assertEquals("1.1", defaultValue, prefs.get(key, defaultValue));
        // try for each value in the set
        for (int i = 0; i < values.length; i++) {
            String v1 = values[i];
            String v2 = values[i] + "x";
            prefs.put(key, v1);
            assertEquals("1.2." + i, v1, prefs.get(key, defaultValue));
            prefs.put(key, v2);
            assertEquals("1.3." + i, v2, prefs.get(key, defaultValue));
            prefs.remove(key);
            assertEquals("1.4." + i, defaultValue, prefs.get(key, defaultValue));
        }
        // spec'd to throw a NPE if key is null
        try {
            prefs.get(null, defaultValue);
            fail("1.5.0");
        } catch (NullPointerException e) {
        // expected
        }
        // spec'd to throw a NPE if key is null
        try {
            prefs.put(null, defaultValue);
            fail("1.5.1");
        } catch (NullPointerException e) {
        // expected
        }
        // spec'd to throw a NPE if value is null
        try {
            prefs.put(key, null);
            fail("1.5.2");
        } catch (NullPointerException e) {
        // expected
        }
    } finally {
        // clean-up
        try {
            prefs.removeNode();
        } catch (BackingStoreException e) {
            fail("0.99", e);
        }
    }
    // spec'd to throw IllegalStateException if node has been removed
    try {
        prefs.get(key, defaultValue);
        fail("1.6");
    } catch (IllegalStateException e) {
    // expected
    }
}
Also used : BackingStoreException(org.osgi.service.prefs.BackingStoreException) Preferences(org.osgi.service.prefs.Preferences) EclipsePreferences(org.eclipse.core.internal.preferences.EclipsePreferences)

Example 30 with Preferences

use of org.osgi.service.prefs.Preferences in project eclipse.platform.runtime by eclipse.

the class EclipsePreferencesTest method testLong.

public void testLong() {
    String qualifier = getUniqueString();
    Preferences prefs = getScopeRoot().node(qualifier);
    final String key = "key1";
    final long defaultValue = 42L;
    final long[] values = { -12345L, 0L, 12345L, Long.MAX_VALUE, Long.MIN_VALUE };
    try {
        // nothing there so expect the default
        assertEquals("1.1", defaultValue, prefs.getLong(key, defaultValue));
        // try for each value in the set
        for (int i = 0; i < values.length; i++) {
            long v1 = values[i];
            long v2 = 54L;
            prefs.putLong(key, v1);
            assertEquals("1.2." + i, v1, prefs.getLong(key, defaultValue));
            prefs.putLong(key, v2);
            assertEquals("1.3." + i, v2, prefs.getLong(key, defaultValue));
            prefs.remove(key);
            assertEquals("1.4." + i, defaultValue, prefs.getLong(key, defaultValue));
        }
        String stringValue = "foo";
        prefs.put(key, stringValue);
        assertEquals("1.5", stringValue, prefs.get(key, null));
        assertEquals("1.6", defaultValue, prefs.getLong(key, defaultValue));
        // spec'd to throw a NPE if key is null
        try {
            prefs.getLong(null, defaultValue);
            fail("2.0");
        } catch (NullPointerException e) {
        // expected
        }
        // spec'd to throw a NPE if key is null
        try {
            prefs.putLong(null, defaultValue);
            fail("2.1");
        } catch (NullPointerException e) {
        // expected
        }
    } finally {
        // clean-up
        try {
            prefs.removeNode();
        } catch (BackingStoreException e) {
            fail("0.99", e);
        }
    }
    // spec'd to throw IllegalStateException if node has been removed
    try {
        prefs.getLong(key, defaultValue);
        fail("3.0");
    } catch (IllegalStateException e) {
    // expected
    }
}
Also used : BackingStoreException(org.osgi.service.prefs.BackingStoreException) Preferences(org.osgi.service.prefs.Preferences) EclipsePreferences(org.eclipse.core.internal.preferences.EclipsePreferences)

Aggregations

Preferences (org.osgi.service.prefs.Preferences)157 BackingStoreException (org.osgi.service.prefs.BackingStoreException)82 EclipsePreferences (org.eclipse.core.internal.preferences.EclipsePreferences)41 IEclipsePreferences (org.eclipse.core.runtime.preferences.IEclipsePreferences)37 ProjectScope (org.eclipse.core.resources.ProjectScope)18 IScopeContext (org.eclipse.core.runtime.preferences.IScopeContext)14 IStatus (org.eclipse.core.runtime.IStatus)10 IOException (java.io.IOException)7 Test (org.junit.Test)7 IProject (org.eclipse.core.resources.IProject)6 Status (org.eclipse.core.runtime.Status)6 PerformanceTestRunner (org.eclipse.core.tests.harness.PerformanceTestRunner)6 InstanceScope (org.eclipse.core.runtime.preferences.InstanceScope)5 ISecurePreferences (org.eclipse.equinox.security.storage.ISecurePreferences)5 Field (java.lang.reflect.Field)4 ArrayList (java.util.ArrayList)4 ExportedPreferences (org.eclipse.core.internal.preferences.ExportedPreferences)4 IExportedPreferences (org.eclipse.core.runtime.preferences.IExportedPreferences)4 ContentTypeEncodingPreferences (org.eclipse.wst.sse.core.internal.encoding.ContentTypeEncodingPreferences)4 IEncodedDocument (org.eclipse.wst.sse.core.internal.provisional.document.IEncodedDocument)4