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);
}
}
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());
}
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("#"));
}
}
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
}
}
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
}
}
Aggregations